Subversion Repositories SmartDukaan

Rev

Rev 643 | Rev 685 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
557 chandransh 1
'''
2
Created on 10-May-2010
3
 
4
@author: ashish
5
'''
6
from elixir import *
576 chandransh 7
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User
557 chandransh 8
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, ShoppingCartException
9
import datetime
576 chandransh 10
from shop2020.utils.Utils import to_py_date, to_java_date
557 chandransh 11
 
576 chandransh 12
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as TTransaction,\
13
    TransactionStatus as TTransactionStatus, Order as TOrder, LineItem as TLineItem,\
14
    PaymentInfo as TPaymentInfo, OrderStatus 
557 chandransh 15
 
576 chandransh 16
from shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItem
17
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo as TLogisticsInfo
18
 
19
from shop2020.clients.TransactionClient import TransactionClient
20
from shop2020.clients.InventoryClient import InventoryClient
21
from shop2020.clients.LogisticsClient import LogisticsClient
22
from shop2020.model.v1 import user
23
 
557 chandransh 24
def get_cart(user_id):
25
    query = Cart.query.filter_by(user_id=user_id)      
26
    query = query.filter_by(cart_status = CartStatus.ACTIVE)
27
    try:
28
        cart = query.one()
29
        cart.accessed_on = datetime.datetime.now()
30
        session.commit()
31
    except:
32
        return None
33
#        raise ShoppingCartException(101, "Cart does not exist")
34
    return cart
35
 
36
def get_cart_by_id(id):
37
    cart = Cart.get_by(id=id)
38
    return cart
39
 
40
def create_cart(user_id):
41
    cart = get_cart(user_id)
42
    if not cart: 
43
        cart = Cart()
44
        cart.user_id = user_id
45
        cart.created_on = datetime.datetime.now()
46
        cart.updated_on = datetime.datetime.now()
47
        cart.cart_status = CartStatus.ACTIVE
48
        session.commit()
49
 
50
    return cart
51
 
52
def get_cart_by_user_id_and_status(user_id, status):
53
 
54
    query = Cart.query.filter_by(user_id=user_id)
55
    if status:
56
        query = query.filter_by(cart_status=status)
57
    carts = query.all()
58
    return carts
59
 
60
def get_carts_by_status(status):
61
    return Cart.query.filter_by(cart_status=status).all()
62
 
63
 
64
def get_carts_between(start_time, end_time, status):
65
    init_time = to_py_date(start_time)
66
    finish_time = to_py_date(end_time)
67
 
68
    query = Cart.query
69
    if status:
70
        query = query.filter(Cart.cart_status==status)
71
    if init_time:
72
        query = query.filter(Cart.created_on >= init_time)
73
    if finish_time:
74
        query = query.filter(Cart.created_on <= finish_time)
75
 
76
    carts = query.all()
77
    return carts
78
 
643 chandransh 79
def get_line(item_id, cart_id, status, single):
557 chandransh 80
    #get cart first 
81
    try:
82
        found_cart = Cart.get_by(id=cart_id)
83
    except:
84
        raise ShoppingCartException(101, "cart not found ")
643 chandransh 85
    query = Line.query.filter_by(cart = found_cart, item_id = item_id)
557 chandransh 86
 
87
    if status:
88
        query = query.filter_by(line_status = status)
89
    else:
90
        query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
91
    try:
92
        if single:
93
            return query.one()
94
        else:
95
            return query.all()
96
    except:
97
        return None
98
 
99
def change_cart_status(cart_id, status):
100
    cart = get_cart_by_id(id)
101
    if not cart:
102
        raise ShoppingCartException(101, "no cart attached to this id")
103
    if not status:
104
        raise ShoppingCartException(101, "invalid status")
105
 
106
    cart.cart_status = status
107
 
108
    session.commit()
109
 
110
def add_item_to_cart(cart_id, item_id, quantity):
111
    if not item_id:
112
        raise ShoppingCartException(101, "item_id cannot be null")
113
 
114
    if not quantity:
115
        raise ShoppingCartException(101, "quantity cannot be null")    
643 chandransh 116
 
117
    cart = Cart.get_by(id = cart_id)
557 chandransh 118
    if not cart:
119
        raise ShoppingCartException(101, "no cart attached to this id")
120
 
643 chandransh 121
    current_time = datetime.datetime.now()
122
    cart.updated_on = current_time
123
    line = get_line(item_id, cart_id, None,True)
124
    if line:
125
        #change the quantity only
126
        line.quantity = quantity
127
    else:
128
        line = Line()
129
        line.cart = cart
130
        line.item_id = item_id
131
        line.quantity = quantity
132
        line.created_on = current_time
133
        line.updated_on = current_time
134
        line.line_status = LineStatus.LINE_ACTIVE
557 chandransh 135
    session.commit()
136
 
137
def delete_item_from_cart(cart_id, item_id):
138
    if not item_id:
139
        raise ShoppingCartException(101, "item_id cannot be null")
643 chandransh 140
    item = get_line(item_id, cart_id, None, True)
557 chandransh 141
    item.delete()
142
    session.commit()
143
 
144
def change_item_status(cart_id, item_id, status):
145
    if not status:
146
        raise ShoppingCartException(101, "Status cannot be made null")
643 chandransh 147
    item = get_line(item_id, cart_id, None, True)
557 chandransh 148
    if item:
149
        item.line_status = status
150
        session.commit()
151
    else:
152
        raise ShoppingCartException(101, "Unable to probe the item you desired")
153
 
154
def add_address_to_cart(cart_id, address_id):
155
    if not cart_id:
156
        raise ShoppingCartException(101, "cart id cannot be made null")
157
 
158
    if not address_id:
159
        raise ShoppingCartException(101, "address id cannot be made null")
160
 
161
    cart = get_cart_by_id(cart_id)
162
    if not cart:
163
        raise ShoppingCartException(101, "no cart for this id")
576 chandransh 164
 
165
    address = Address.get_by(id=address_id)
166
    if not address:
167
        raise ShoppingCartException(101, "No address for this id")
168
 
557 chandransh 169
    cart.address_id = address_id
170
    session.commit()
171
 
576 chandransh 172
def commit_cart(cart_id):   
557 chandransh 173
    cart = get_cart_by_id(cart_id)
174
    cart.cart_status = CartStatus.COMMITTED
175
    cart.committed_on = datetime.datetime.now()
576 chandransh 176
 
557 chandransh 177
    #now we have a cart. Need to create a transaction with it
576 chandransh 178
    txn = TTransaction()
179
    txn.paymentInfo = TPaymentInfo()
180
    txn.shoppingCartid = cart_id
181
    txn.customer_id = cart.user_id
182
    txn.createdOn = to_java_date(datetime.datetime.now())
183
    txn.transactionStatus = TTransactionStatus.INIT
184
    txn.statusDescription = "New Order"
557 chandransh 185
 
576 chandransh 186
    txn.orders = create_orders(cart)
187
 
188
    transaction_client = TransactionClient().get_client()
189
    txn_id = transaction_client.createTransaction(txn)
190
    session.commit()
191
 
192
    new_cart = create_cart(cart.user_id)
193
    user = User.get_by(id=cart.user_id)
194
    user.active_cart_id = new_cart.id
195
    session.commit()
196
    return txn_id
197
 
198
def create_orders(cart):
557 chandransh 199
    cart_lines = cart.lines
576 chandransh 200
    orders = []
201
 
557 chandransh 202
    for line in cart_lines:
576 chandransh 203
        if line.line_status == LineStatus.LINE_ACTIVE:
204
            i = 0
205
            while i< line.quantity:
206
                t_line_item = create_line_item(line.item_id)
207
                t_order = create_order(cart.user_id, cart.address_id, t_line_item)
208
                orders.append(t_order)
209
                i += 1
210
    return orders
211
 
212
def create_order(user_id, address_id, t_line_item):
213
    user = User.get_by(id=user_id)
214
    address = Address.get_by(id=address_id)
215
    t_order = TOrder()
557 chandransh 216
 
576 chandransh 217
    t_order.customer_id = user.id
218
    t_order.customer_name = user.name
219
    t_order.customer_email = user.email
220
 
221
    t_order.customer_pincode = address.pin
222
    t_order.customer_address = get_address_string(address)
669 chandransh 223
    t_order.customer_city = address.city
224
    t_order.customer_state = address.state
576 chandransh 225
    t_order.customer_mobilenumber = address.phone
226
 
227
    t_order.total_amount = t_line_item.total_price
228
    t_order.total_weight = t_line_item.total_weight
229
    t_order.lineitems = [t_line_item]
230
 
231
    t_order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
232
    t_order.statusDescription = "Submitted to warehouse"
233
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
234
 
235
    logistics_client = LogisticsClient().get_client()
236
    logistics_info = logistics_client.getLogisticsInfo(t_order.customer_pincode, t_line_item.sku_id)
237
 
238
    t_order.logistics_provider_id = logistics_info.provider_id
239
    t_order.airwaybill_no = logistics_info.airway_billno
240
    t_order.tracking_id = t_order.airwaybill_no
241
    t_order.expected_delivery_time = to_java_date(datetime.datetime.now()) + 60*60*1000*logistics_info.delivery_estimate
242
    t_order.warehouse_id = logistics_info.warehouse_id
243
 
244
    return t_order
245
 
636 rajveer 246
def create_line_item(item_id):
576 chandransh 247
    inventory_client = InventoryClient().get_client()
636 rajveer 248
    item = inventory_client.getItem(item_id)
576 chandransh 249
    t_line_item = TLineItem()
636 rajveer 250
    t_line_item.model_name = item.modelName
251
    t_line_item.model_number = item.modelNumber
669 chandransh 252
    if item.color is None or item.color == "NA":
253
        t_line_item.color = ""
254
    else:
255
        t_line_item.color = item.color        
636 rajveer 256
    t_line_item.extra_info = item.featureDescription
257
    t_line_item.brand = item.manufacturerName
258
    t_line_item.sku_id = item.vendorItemId
576 chandransh 259
    t_line_item.quantity = 1
636 rajveer 260
    t_line_item.unit_price = item.sellingPrice
261
    t_line_item.unit_weight = item.weight
262
    t_line_item.total_price = item.sellingPrice
263
    t_line_item.total_weight = item.weight
576 chandransh 264
    return t_line_item
265
 
266
def get_address_string(address):
669 chandransh 267
    return address.line_1 + "\n" + address.line_2 + "\n Landmark: " + address.landmark + "\n"
576 chandransh 268
 
269
def validate_cart(cartId):
270
    inventory_client = InventoryClient().get_client()
271
    logistics_client = LogisticsClient().get_client()
563 chandransh 272
    retval = True
557 chandransh 273
    # No need to validate duplicate items since there are only two ways
274
    # to add items to a cart and both of them check whether the item being
275
    # added is a duplicate of an already existing item.
563 chandransh 276
    cart = Cart.get_by(id=cartId)
277
    cart_lines = cart.lines
576 chandransh 278
    customer_pincode = ""
279
    if cart.address_id:
280
        address = Address.get_by(id=cart.address_id)
281
        customer_pincode = address.pin
563 chandransh 282
    for line in cart_lines:
612 chandransh 283
        old_estimate = line.estimate
636 rajveer 284
        item_id = line.item_id
285
        if inventory_client.isActive(item_id):
576 chandransh 286
            if customer_pincode:
287
                #FIXME: Provider ID should be returned by the same call and shouldn't be hard-coded as 1
612 chandransh 288
                try:
643 chandransh 289
                    item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
612 chandransh 290
                except:
643 chandransh 291
                    item_delivery_estimate = 3
612 chandransh 292
                if old_estimate != item_delivery_estimate:
293
                    line.estimate = item_delivery_estimate
294
                    retval = False
576 chandransh 295
        else:
563 chandransh 296
            line.delete()
297
            retval = False
612 chandransh 298
 
299
    session.commit()
563 chandransh 300
    return retval
576 chandransh 301
 
557 chandransh 302
def merge_cart(fromCartId, toCartId):
303
    fromCart = Cart.get_by(id=fromCartId)
304
    toCart = Cart.get_by(id=toCartId)
305
 
306
    old_lines = fromCart.lines
307
    new_lines = toCart.lines
308
 
309
    for line in old_lines:
310
        flag = True
311
        for new_line in new_lines:
312
            if line.item_id == new_line.item_id:
313
                flag = False
576 chandransh 314
        if flag:
557 chandransh 315
            toCart.lines.append(line)
316
 
317
    toCart.updatedOn = datetime.datetime.now() 
318
 
319
    fromCart.expired_on = datetime.datetime.now()
320
    fromCart.cart_status = CartStatus.INACTIVE
643 chandransh 321
    session.commit()