Subversion Repositories SmartDukaan

Rev

Rev 1621 | Rev 1976 | 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
844 chandransh 16
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo as TLogisticsInfo,\
17
    LogisticsServiceException
576 chandransh 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
        session.commit()
30
    except:
31
        return None
32
#        raise ShoppingCartException(101, "Cart does not exist")
33
    return cart
34
 
35
def get_cart_by_id(id):
36
    cart = Cart.get_by(id=id)
37
    return cart
38
 
39
def create_cart(user_id):
40
    cart = get_cart(user_id)
41
    if not cart: 
42
        cart = Cart()
43
        cart.user_id = user_id
44
        cart.created_on = datetime.datetime.now()
45
        cart.updated_on = datetime.datetime.now()
46
        cart.cart_status = CartStatus.ACTIVE
47
        session.commit()
48
    return cart
49
 
50
def get_cart_by_user_id_and_status(user_id, status):
51
    query = Cart.query.filter_by(user_id=user_id)
52
    if status:
53
        query = query.filter_by(cart_status=status)
54
    carts = query.all()
55
    return carts
56
 
57
def get_carts_by_status(status):
58
    return Cart.query.filter_by(cart_status=status).all()
59
 
60
def get_carts_between(start_time, end_time, status):
61
    init_time = to_py_date(start_time)
62
    finish_time = to_py_date(end_time)
63
 
64
    query = Cart.query
65
    if status:
66
        query = query.filter(Cart.cart_status==status)
67
    if init_time:
68
        query = query.filter(Cart.created_on >= init_time)
69
    if finish_time:
70
        query = query.filter(Cart.created_on <= finish_time)
71
 
72
    carts = query.all()
73
    return carts
74
 
643 chandransh 75
def get_line(item_id, cart_id, status, single):
557 chandransh 76
    #get cart first 
77
    try:
78
        found_cart = Cart.get_by(id=cart_id)
79
    except:
80
        raise ShoppingCartException(101, "cart not found ")
643 chandransh 81
    query = Line.query.filter_by(cart = found_cart, item_id = item_id)
557 chandransh 82
 
83
    if status:
84
        query = query.filter_by(line_status = status)
85
    else:
86
        query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
87
    try:
88
        if single:
89
            return query.one()
90
        else:
91
            return query.all()
92
    except:
93
        return None
94
 
95
def change_cart_status(cart_id, status):
96
    cart = get_cart_by_id(id)
97
    if not cart:
98
        raise ShoppingCartException(101, "no cart attached to this id")
99
    if not status:
100
        raise ShoppingCartException(101, "invalid status")
690 chandransh 101
    cart.cart_status = status
557 chandransh 102
    session.commit()
103
 
104
def add_item_to_cart(cart_id, item_id, quantity):
105
    if not item_id:
106
        raise ShoppingCartException(101, "item_id cannot be null")
107
 
108
    if not quantity:
109
        raise ShoppingCartException(101, "quantity cannot be null")    
643 chandransh 110
 
111
    cart = Cart.get_by(id = cart_id)
557 chandransh 112
    if not cart:
113
        raise ShoppingCartException(101, "no cart attached to this id")
114
 
643 chandransh 115
    current_time = datetime.datetime.now()
116
    cart.updated_on = current_time
117
    line = get_line(item_id, cart_id, None,True)
118
    if line:
119
        #change the quantity only
120
        line.quantity = quantity
685 chandransh 121
        line.updated_on = current_time
643 chandransh 122
    else:
123
        line = Line()
124
        line.cart = cart
125
        line.item_id = item_id
126
        line.quantity = quantity
127
        line.created_on = current_time
128
        line.updated_on = current_time
129
        line.line_status = LineStatus.LINE_ACTIVE
557 chandransh 130
    session.commit()
1621 rajveer 131
    #validate_cart(cart_id)
557 chandransh 132
 
133
def delete_item_from_cart(cart_id, item_id):
134
    if not item_id:
135
        raise ShoppingCartException(101, "item_id cannot be null")
685 chandransh 136
    cart = Cart.get_by(id = cart_id)
137
    if not cart:
138
        raise ShoppingCartException(101, "no cart attached to this id")
643 chandransh 139
    item = get_line(item_id, cart_id, None, True)
557 chandransh 140
    item.delete()
685 chandransh 141
    current_time = datetime.datetime.now()
142
    cart.updated_on = current_time
557 chandransh 143
    session.commit()
144
 
145
def change_item_status(cart_id, item_id, status):
146
    if not status:
147
        raise ShoppingCartException(101, "Status cannot be made null")
685 chandransh 148
    line = get_line(item_id, cart_id, None, True)
149
    if line:
150
        line.line_status = status
151
        current_time = datetime.datetime.now()
152
        line.updated_on = current_time
153
        line.cart.updated_on = current_time
557 chandransh 154
        session.commit()
155
    else:
685 chandransh 156
        raise ShoppingCartException(101, "Unable to probe the line you desired")
557 chandransh 157
 
158
def add_address_to_cart(cart_id, address_id):
159
    if not cart_id:
160
        raise ShoppingCartException(101, "cart id cannot be made null")
161
 
162
    if not address_id:
163
        raise ShoppingCartException(101, "address id cannot be made null")
164
 
165
    cart = get_cart_by_id(cart_id)
166
    if not cart:
167
        raise ShoppingCartException(101, "no cart for this id")
576 chandransh 168
 
169
    address = Address.get_by(id=address_id)
170
    if not address:
171
        raise ShoppingCartException(101, "No address for this id")
172
 
557 chandransh 173
    cart.address_id = address_id
685 chandransh 174
    current_time = datetime.datetime.now()
716 rajveer 175
    #cart.updated_on = current_time
557 chandransh 176
    session.commit()
177
 
576 chandransh 178
def commit_cart(cart_id):   
690 chandransh 179
    cart = get_cart_by_id(cart_id)   
557 chandransh 180
    #now we have a cart. Need to create a transaction with it
576 chandransh 181
    txn = TTransaction()
182
    txn.shoppingCartid = cart_id
183
    txn.customer_id = cart.user_id
184
    txn.createdOn = to_java_date(datetime.datetime.now())
185
    txn.transactionStatus = TTransactionStatus.INIT
186
    txn.statusDescription = "New Order"
557 chandransh 187
 
576 chandransh 188
    txn.orders = create_orders(cart)
189
 
190
    transaction_client = TransactionClient().get_client()
191
    txn_id = transaction_client.createTransaction(txn)
192
    session.commit()
193
 
685 chandransh 194
    #new_cart = create_cart(cart.user_id)
195
    #user = User.get_by(id=cart.user_id)
196
    #user.active_cart_id = new_cart.id
197
    #session.commit()
576 chandransh 198
    return txn_id
199
 
200
def create_orders(cart):
557 chandransh 201
    cart_lines = cart.lines
576 chandransh 202
    orders = []
203
 
557 chandransh 204
    for line in cart_lines:
576 chandransh 205
        if line.line_status == LineStatus.LINE_ACTIVE:
206
            i = 0
207
            while i< line.quantity:
208
                t_line_item = create_line_item(line.item_id)
209
                t_order = create_order(cart.user_id, cart.address_id, t_line_item)
210
                orders.append(t_order)
211
                i += 1
212
    return orders
213
 
214
def create_order(user_id, address_id, t_line_item):
215
    user = User.get_by(id=user_id)
216
    address = Address.get_by(id=address_id)
217
    t_order = TOrder()
557 chandransh 218
 
576 chandransh 219
    t_order.customer_id = user.id
220
    t_order.customer_email = user.email
221
 
910 rajveer 222
    t_order.customer_name = address.name
576 chandransh 223
    t_order.customer_pincode = address.pin
738 chandransh 224
    t_order.customer_address1 = address.line_1
225
    t_order.customer_address2 = address.line_2
669 chandransh 226
    t_order.customer_city = address.city
227
    t_order.customer_state = address.state
576 chandransh 228
    t_order.customer_mobilenumber = address.phone
229
 
230
    t_order.total_amount = t_line_item.total_price
231
    t_order.total_weight = t_line_item.total_weight
232
    t_order.lineitems = [t_line_item]
233
 
690 chandransh 234
    t_order.status = OrderStatus.PAYMENT_PENDING
970 chandransh 235
    t_order.statusDescription = "Payment Pending"
576 chandransh 236
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
237
 
743 rajveer 238
    # this code is moved somewhere else, as now order will be created but we dont want to add awb number etc
239
    '''
576 chandransh 240
    logistics_client = LogisticsClient().get_client()
716 rajveer 241
    logistics_info = logistics_client.getLogisticsInfo(t_order.customer_pincode, t_line_item.item_id)
576 chandransh 242
 
716 rajveer 243
    t_order.logistics_provider_id = logistics_info.providerId
576 chandransh 244
    t_order.airwaybill_no = logistics_info.airway_billno
245
    t_order.tracking_id = t_order.airwaybill_no
716 rajveer 246
    t_order.expected_delivery_time = to_java_date(datetime.datetime.now()) + 60*60*1000*logistics_info.deliveryTime
247
    t_order.warehouse_id = logistics_info.warehouseId
743 rajveer 248
    '''
249
 
576 chandransh 250
    return t_order
251
 
636 rajveer 252
def create_line_item(item_id):
576 chandransh 253
    inventory_client = InventoryClient().get_client()
636 rajveer 254
    item = inventory_client.getItem(item_id)
576 chandransh 255
    t_line_item = TLineItem()
963 chandransh 256
    t_line_item.productGroup = item.productGroup
257
    t_line_item.brand = item.brand
636 rajveer 258
    t_line_item.model_number = item.modelNumber
669 chandransh 259
    if item.color is None or item.color == "NA":
260
        t_line_item.color = ""
261
    else:
917 chandransh 262
        t_line_item.color = item.color
963 chandransh 263
    t_line_item.model_name = item.modelName
636 rajveer 264
    t_line_item.extra_info = item.featureDescription
702 chandransh 265
    t_line_item.item_id = item.id
576 chandransh 266
    t_line_item.quantity = 1
636 rajveer 267
    t_line_item.unit_price = item.sellingPrice
268
    t_line_item.unit_weight = item.weight
269
    t_line_item.total_price = item.sellingPrice
270
    t_line_item.total_weight = item.weight
576 chandransh 271
    return t_line_item
272
 
273
def validate_cart(cartId):
274
    inventory_client = InventoryClient().get_client()
275
    logistics_client = LogisticsClient().get_client()
1466 ankur.sing 276
    retval = ""
557 chandransh 277
    # No need to validate duplicate items since there are only two ways
278
    # to add items to a cart and both of them check whether the item being
279
    # added is a duplicate of an already existing item.
563 chandransh 280
    cart = Cart.get_by(id=cartId)
281
    cart_lines = cart.lines
776 rajveer 282
    customer_pincode = None
690 chandransh 283
    current_time = datetime.datetime.now()
576 chandransh 284
    if cart.address_id:
285
        address = Address.get_by(id=cart.address_id)
286
        customer_pincode = address.pin
776 rajveer 287
    if not customer_pincode:
785 rajveer 288
        user = User.get_by(active_cart_id = cartId)
289
        default_address_id = user.default_address_id
776 rajveer 290
        if default_address_id:
291
            address = Address.get_by(id = default_address_id)
292
            customer_pincode = address.pin
293
    if not customer_pincode:
294
        #FIXME should not be hard coded. May be we can pick from config server.
295
        customer_pincode = "110001"
563 chandransh 296
    for line in cart_lines:
612 chandransh 297
        old_estimate = line.estimate
636 rajveer 298
        item_id = line.item_id
299
        if inventory_client.isActive(item_id):
776 rajveer 300
            try:
301
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
844 chandransh 302
            except LogisticsServiceException:
303
                item_delivery_estimate = -1
304
                #TODO Use the exception clause to set the retval appropriately
305
            except :
306
                item_delivery_estimate = -1
776 rajveer 307
            if old_estimate != item_delivery_estimate:
308
                line.estimate = item_delivery_estimate
309
                cart.updated_on = current_time
1466 ankur.sing 310
                if old_estimate != None:
311
                    retval = "Delivery Estimates updated."
576 chandransh 312
        else:
563 chandransh 313
            line.delete()
1466 ankur.sing 314
            retval = "Some items have been removed from the cart."
716 rajveer 315
    if cart.checked_out_on is not None:
316
        if cart.updated_on > cart.checked_out_on:
844 chandransh 317
            cart.checked_out_on = None
1466 ankur.sing 318
            if retval == "":
319
                retval = "Your cart has been updated after the last checkout."
612 chandransh 320
    session.commit()
563 chandransh 321
    return retval
576 chandransh 322
 
557 chandransh 323
def merge_cart(fromCartId, toCartId):
324
    fromCart = Cart.get_by(id=fromCartId)
325
    toCart = Cart.get_by(id=toCartId)
326
 
327
    old_lines = fromCart.lines
328
    new_lines = toCart.lines
329
 
330
    for line in old_lines:
331
        flag = True
332
        for new_line in new_lines:
333
            if line.item_id == new_line.item_id:
334
                flag = False
576 chandransh 335
        if flag:
557 chandransh 336
            toCart.lines.append(line)
337
 
338
    toCart.updatedOn = datetime.datetime.now() 
339
 
340
    fromCart.expired_on = datetime.datetime.now()
341
    fromCart.cart_status = CartStatus.INACTIVE
643 chandransh 342
    session.commit()
691 chandransh 343
 
344
def check_out(cartId):
345
    if cartId is None:
346
        raise ShoppingCartException(101, "Cart id not specified")
716 rajveer 347
    cart = Cart.get_by(id = cartId)
691 chandransh 348
    if cart is None:
349
        raise ShoppingCartException(102, "The specified cart couldn't be found")
350
    cart.checked_out_on = datetime.datetime.now()
351
    session.commit()
352
    return True
353
 
354
def reset_cart(cartId, items):
355
    if cartId is None:
356
        raise ShoppingCartException(101, "Cart id not specified")
357
    for item_id, quantity in items.iteritems():
358
        line = Line.query.filter_by(cart_id=cartId, item_id=item_id).one()
359
        if line is not None:
360
            line.quantity = line.quantity - quantity
361
            if line.quantity == 0:
362
                line.delete()
717 rajveer 363
    cart = Cart.get_by(id=cartId)
691 chandransh 364
    cart.updated_on = datetime.datetime.now()
1894 vikas 365
    cart.checked_out_on = None
691 chandransh 366
    session.commit()
766 rajveer 367
    return True
368
 
369
def close_session():
370
    if session.is_active:
371
        print "session is active. closing it."
1621 rajveer 372
        session.close()