Subversion Repositories SmartDukaan

Rev

Rev 743 | Rev 776 | 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,\
685 chandransh 13
    TransactionStatus as TTransactionStatus, Order as TOrder, LineItem as TLineItem, OrderStatus 
557 chandransh 14
 
576 chandransh 15
from shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItem
16
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo as TLogisticsInfo
17
 
18
from shop2020.clients.TransactionClient import TransactionClient
19
from shop2020.clients.InventoryClient import InventoryClient
20
from shop2020.clients.LogisticsClient import LogisticsClient
21
from shop2020.model.v1 import user
22
 
557 chandransh 23
def get_cart(user_id):
24
    query = Cart.query.filter_by(user_id=user_id)      
25
    query = query.filter_by(cart_status = CartStatus.ACTIVE)
26
    try:
27
        cart = query.one()
28
        session.commit()
29
    except:
30
        return None
31
#        raise ShoppingCartException(101, "Cart does not exist")
32
    return cart
33
 
34
def get_cart_by_id(id):
35
    cart = Cart.get_by(id=id)
36
    return cart
37
 
38
def create_cart(user_id):
39
    cart = get_cart(user_id)
40
    if not cart: 
41
        cart = Cart()
42
        cart.user_id = user_id
43
        cart.created_on = datetime.datetime.now()
44
        cart.updated_on = datetime.datetime.now()
45
        cart.cart_status = CartStatus.ACTIVE
46
        session.commit()
47
    return cart
48
 
49
def get_cart_by_user_id_and_status(user_id, status):
50
    query = Cart.query.filter_by(user_id=user_id)
51
    if status:
52
        query = query.filter_by(cart_status=status)
53
    carts = query.all()
54
    return carts
55
 
56
def get_carts_by_status(status):
57
    return Cart.query.filter_by(cart_status=status).all()
58
 
59
def get_carts_between(start_time, end_time, status):
60
    init_time = to_py_date(start_time)
61
    finish_time = to_py_date(end_time)
62
 
63
    query = Cart.query
64
    if status:
65
        query = query.filter(Cart.cart_status==status)
66
    if init_time:
67
        query = query.filter(Cart.created_on >= init_time)
68
    if finish_time:
69
        query = query.filter(Cart.created_on <= finish_time)
70
 
71
    carts = query.all()
72
    return carts
73
 
643 chandransh 74
def get_line(item_id, cart_id, status, single):
557 chandransh 75
    #get cart first 
76
    try:
77
        found_cart = Cart.get_by(id=cart_id)
78
    except:
79
        raise ShoppingCartException(101, "cart not found ")
643 chandransh 80
    query = Line.query.filter_by(cart = found_cart, item_id = item_id)
557 chandransh 81
 
82
    if status:
83
        query = query.filter_by(line_status = status)
84
    else:
85
        query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
86
    try:
87
        if single:
88
            return query.one()
89
        else:
90
            return query.all()
91
    except:
92
        return None
93
 
94
def change_cart_status(cart_id, status):
95
    cart = get_cart_by_id(id)
96
    if not cart:
97
        raise ShoppingCartException(101, "no cart attached to this id")
98
    if not status:
99
        raise ShoppingCartException(101, "invalid status")
690 chandransh 100
    cart.cart_status = status
557 chandransh 101
    session.commit()
102
 
103
def add_item_to_cart(cart_id, item_id, quantity):
104
    if not item_id:
105
        raise ShoppingCartException(101, "item_id cannot be null")
106
 
107
    if not quantity:
108
        raise ShoppingCartException(101, "quantity cannot be null")    
643 chandransh 109
 
110
    cart = Cart.get_by(id = cart_id)
557 chandransh 111
    if not cart:
112
        raise ShoppingCartException(101, "no cart attached to this id")
113
 
643 chandransh 114
    current_time = datetime.datetime.now()
115
    cart.updated_on = current_time
116
    line = get_line(item_id, cart_id, None,True)
117
    if line:
118
        #change the quantity only
119
        line.quantity = quantity
685 chandransh 120
        line.updated_on = current_time
643 chandransh 121
    else:
122
        line = Line()
123
        line.cart = cart
124
        line.item_id = item_id
125
        line.quantity = quantity
126
        line.created_on = current_time
127
        line.updated_on = current_time
128
        line.line_status = LineStatus.LINE_ACTIVE
557 chandransh 129
    session.commit()
130
 
131
def delete_item_from_cart(cart_id, item_id):
132
    if not item_id:
133
        raise ShoppingCartException(101, "item_id cannot be null")
685 chandransh 134
    cart = Cart.get_by(id = cart_id)
135
    if not cart:
136
        raise ShoppingCartException(101, "no cart attached to this id")
643 chandransh 137
    item = get_line(item_id, cart_id, None, True)
557 chandransh 138
    item.delete()
685 chandransh 139
    current_time = datetime.datetime.now()
140
    cart.updated_on = current_time
557 chandransh 141
    session.commit()
142
 
143
def change_item_status(cart_id, item_id, status):
144
    if not status:
145
        raise ShoppingCartException(101, "Status cannot be made null")
685 chandransh 146
    line = get_line(item_id, cart_id, None, True)
147
    if line:
148
        line.line_status = status
149
        current_time = datetime.datetime.now()
150
        line.updated_on = current_time
151
        line.cart.updated_on = current_time
557 chandransh 152
        session.commit()
153
    else:
685 chandransh 154
        raise ShoppingCartException(101, "Unable to probe the line you desired")
557 chandransh 155
 
156
def add_address_to_cart(cart_id, address_id):
157
    if not cart_id:
158
        raise ShoppingCartException(101, "cart id cannot be made null")
159
 
160
    if not address_id:
161
        raise ShoppingCartException(101, "address id cannot be made null")
162
 
163
    cart = get_cart_by_id(cart_id)
164
    if not cart:
165
        raise ShoppingCartException(101, "no cart for this id")
576 chandransh 166
 
167
    address = Address.get_by(id=address_id)
168
    if not address:
169
        raise ShoppingCartException(101, "No address for this id")
170
 
557 chandransh 171
    cart.address_id = address_id
685 chandransh 172
    current_time = datetime.datetime.now()
716 rajveer 173
    #cart.updated_on = current_time
557 chandransh 174
    session.commit()
175
 
576 chandransh 176
def commit_cart(cart_id):   
690 chandransh 177
    cart = get_cart_by_id(cart_id)   
557 chandransh 178
    #now we have a cart. Need to create a transaction with it
576 chandransh 179
    txn = TTransaction()
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
 
685 chandransh 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()
576 chandransh 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
738 chandransh 222
    t_order.customer_address1 = address.line_1
223
    t_order.customer_address2 = address.line_2
669 chandransh 224
    t_order.customer_city = address.city
225
    t_order.customer_state = address.state
576 chandransh 226
    t_order.customer_mobilenumber = address.phone
227
 
228
    t_order.total_amount = t_line_item.total_price
229
    t_order.total_weight = t_line_item.total_weight
230
    t_order.lineitems = [t_line_item]
231
 
690 chandransh 232
    t_order.status = OrderStatus.PAYMENT_PENDING
576 chandransh 233
    t_order.statusDescription = "Submitted to warehouse"
234
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
235
 
743 rajveer 236
    # this code is moved somewhere else, as now order will be created but we dont want to add awb number etc
237
    '''
576 chandransh 238
    logistics_client = LogisticsClient().get_client()
716 rajveer 239
    logistics_info = logistics_client.getLogisticsInfo(t_order.customer_pincode, t_line_item.item_id)
576 chandransh 240
 
716 rajveer 241
    t_order.logistics_provider_id = logistics_info.providerId
576 chandransh 242
    t_order.airwaybill_no = logistics_info.airway_billno
243
    t_order.tracking_id = t_order.airwaybill_no
716 rajveer 244
    t_order.expected_delivery_time = to_java_date(datetime.datetime.now()) + 60*60*1000*logistics_info.deliveryTime
245
    t_order.warehouse_id = logistics_info.warehouseId
743 rajveer 246
    '''
247
 
576 chandransh 248
    return t_order
249
 
636 rajveer 250
def create_line_item(item_id):
576 chandransh 251
    inventory_client = InventoryClient().get_client()
636 rajveer 252
    item = inventory_client.getItem(item_id)
576 chandransh 253
    t_line_item = TLineItem()
636 rajveer 254
    t_line_item.model_name = item.modelName
255
    t_line_item.model_number = item.modelNumber
669 chandransh 256
    if item.color is None or item.color == "NA":
257
        t_line_item.color = ""
258
    else:
259
        t_line_item.color = item.color        
636 rajveer 260
    t_line_item.extra_info = item.featureDescription
261
    t_line_item.brand = item.manufacturerName
702 chandransh 262
    t_line_item.item_id = item.id
576 chandransh 263
    t_line_item.quantity = 1
636 rajveer 264
    t_line_item.unit_price = item.sellingPrice
265
    t_line_item.unit_weight = item.weight
266
    t_line_item.total_price = item.sellingPrice
267
    t_line_item.total_weight = item.weight
576 chandransh 268
    return t_line_item
269
 
270
def validate_cart(cartId):
271
    inventory_client = InventoryClient().get_client()
272
    logistics_client = LogisticsClient().get_client()
563 chandransh 273
    retval = True
557 chandransh 274
    # No need to validate duplicate items since there are only two ways
275
    # to add items to a cart and both of them check whether the item being
276
    # added is a duplicate of an already existing item.
563 chandransh 277
    cart = Cart.get_by(id=cartId)
278
    cart_lines = cart.lines
576 chandransh 279
    customer_pincode = ""
690 chandransh 280
    current_time = datetime.datetime.now()
576 chandransh 281
    if cart.address_id:
282
        address = Address.get_by(id=cart.address_id)
283
        customer_pincode = address.pin
563 chandransh 284
    for line in cart_lines:
612 chandransh 285
        old_estimate = line.estimate
636 rajveer 286
        item_id = line.item_id
287
        if inventory_client.isActive(item_id):
576 chandransh 288
            if customer_pincode:
612 chandransh 289
                try:
643 chandransh 290
                    item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
612 chandransh 291
                except:
643 chandransh 292
                    item_delivery_estimate = 3
612 chandransh 293
                if old_estimate != item_delivery_estimate:
294
                    line.estimate = item_delivery_estimate
690 chandransh 295
                    cart.updated_on = current_time
612 chandransh 296
                    retval = False
576 chandransh 297
        else:
563 chandransh 298
            line.delete()
299
            retval = False
716 rajveer 300
    if cart.checked_out_on is not None:
301
        if cart.updated_on > cart.checked_out_on:
302
            retval = False
612 chandransh 303
    session.commit()
563 chandransh 304
    return retval
576 chandransh 305
 
557 chandransh 306
def merge_cart(fromCartId, toCartId):
307
    fromCart = Cart.get_by(id=fromCartId)
308
    toCart = Cart.get_by(id=toCartId)
309
 
310
    old_lines = fromCart.lines
311
    new_lines = toCart.lines
312
 
313
    for line in old_lines:
314
        flag = True
315
        for new_line in new_lines:
316
            if line.item_id == new_line.item_id:
317
                flag = False
576 chandransh 318
        if flag:
557 chandransh 319
            toCart.lines.append(line)
320
 
321
    toCart.updatedOn = datetime.datetime.now() 
322
 
323
    fromCart.expired_on = datetime.datetime.now()
324
    fromCart.cart_status = CartStatus.INACTIVE
643 chandransh 325
    session.commit()
691 chandransh 326
 
327
def check_out(cartId):
328
    if cartId is None:
329
        raise ShoppingCartException(101, "Cart id not specified")
716 rajveer 330
    cart = Cart.get_by(id = cartId)
691 chandransh 331
    if cart is None:
332
        raise ShoppingCartException(102, "The specified cart couldn't be found")
333
    cart.checked_out_on = datetime.datetime.now()
334
    session.commit()
335
    return True
336
 
337
def reset_cart(cartId, items):
338
    if cartId is None:
339
        raise ShoppingCartException(101, "Cart id not specified")
340
    for item_id, quantity in items.iteritems():
341
        line = Line.query.filter_by(cart_id=cartId, item_id=item_id).one()
342
        if line is not None:
343
            line.quantity = line.quantity - quantity
344
            if line.quantity == 0:
345
                line.delete()
717 rajveer 346
    cart = Cart.get_by(id=cartId)
691 chandransh 347
    cart.updated_on = datetime.datetime.now()
348
    session.commit()
766 rajveer 349
    return True
350
 
351
def close_session():
352
    if session.is_active:
353
        print "session is active. closing it."
354
        session.close()