Subversion Repositories SmartDukaan

Rev

Rev 717 | Rev 743 | 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
 
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()
131
 
132
def delete_item_from_cart(cart_id, item_id):
133
    if not item_id:
134
        raise ShoppingCartException(101, "item_id cannot be null")
685 chandransh 135
    cart = Cart.get_by(id = cart_id)
136
    if not cart:
137
        raise ShoppingCartException(101, "no cart attached to this id")
643 chandransh 138
    item = get_line(item_id, cart_id, None, True)
557 chandransh 139
    item.delete()
685 chandransh 140
    current_time = datetime.datetime.now()
141
    cart.updated_on = current_time
557 chandransh 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")
685 chandransh 147
    line = get_line(item_id, cart_id, None, True)
148
    if line:
149
        line.line_status = status
150
        current_time = datetime.datetime.now()
151
        line.updated_on = current_time
152
        line.cart.updated_on = current_time
557 chandransh 153
        session.commit()
154
    else:
685 chandransh 155
        raise ShoppingCartException(101, "Unable to probe the line you desired")
557 chandransh 156
 
157
def add_address_to_cart(cart_id, address_id):
158
    if not cart_id:
159
        raise ShoppingCartException(101, "cart id cannot be made null")
160
 
161
    if not address_id:
162
        raise ShoppingCartException(101, "address id cannot be made null")
163
 
164
    cart = get_cart_by_id(cart_id)
165
    if not cart:
166
        raise ShoppingCartException(101, "no cart for this id")
576 chandransh 167
 
168
    address = Address.get_by(id=address_id)
169
    if not address:
170
        raise ShoppingCartException(101, "No address for this id")
171
 
557 chandransh 172
    cart.address_id = address_id
685 chandransh 173
    current_time = datetime.datetime.now()
716 rajveer 174
    #cart.updated_on = current_time
557 chandransh 175
    session.commit()
176
 
576 chandransh 177
def commit_cart(cart_id):   
690 chandransh 178
    cart = get_cart_by_id(cart_id)   
557 chandransh 179
    #now we have a cart. Need to create a transaction with it
576 chandransh 180
    txn = TTransaction()
181
    txn.shoppingCartid = cart_id
182
    txn.customer_id = cart.user_id
183
    txn.createdOn = to_java_date(datetime.datetime.now())
184
    txn.transactionStatus = TTransactionStatus.INIT
185
    txn.statusDescription = "New Order"
557 chandransh 186
 
576 chandransh 187
    txn.orders = create_orders(cart)
188
 
189
    transaction_client = TransactionClient().get_client()
190
    txn_id = transaction_client.createTransaction(txn)
191
    session.commit()
192
 
685 chandransh 193
    #new_cart = create_cart(cart.user_id)
194
    #user = User.get_by(id=cart.user_id)
195
    #user.active_cart_id = new_cart.id
196
    #session.commit()
576 chandransh 197
    return txn_id
198
 
199
def create_orders(cart):
557 chandransh 200
    cart_lines = cart.lines
576 chandransh 201
    orders = []
202
 
557 chandransh 203
    for line in cart_lines:
576 chandransh 204
        if line.line_status == LineStatus.LINE_ACTIVE:
205
            i = 0
206
            while i< line.quantity:
207
                t_line_item = create_line_item(line.item_id)
208
                t_order = create_order(cart.user_id, cart.address_id, t_line_item)
209
                orders.append(t_order)
210
                i += 1
211
    return orders
212
 
213
def create_order(user_id, address_id, t_line_item):
214
    user = User.get_by(id=user_id)
215
    address = Address.get_by(id=address_id)
216
    t_order = TOrder()
557 chandransh 217
 
576 chandransh 218
    t_order.customer_id = user.id
219
    t_order.customer_name = user.name
220
    t_order.customer_email = user.email
221
 
222
    t_order.customer_pincode = address.pin
738 chandransh 223
    t_order.customer_address1 = address.line_1
224
    t_order.customer_address2 = address.line_2
669 chandransh 225
    t_order.customer_city = address.city
226
    t_order.customer_state = address.state
576 chandransh 227
    t_order.customer_mobilenumber = address.phone
228
 
229
    t_order.total_amount = t_line_item.total_price
230
    t_order.total_weight = t_line_item.total_weight
231
    t_order.lineitems = [t_line_item]
232
 
690 chandransh 233
    t_order.status = OrderStatus.PAYMENT_PENDING
576 chandransh 234
    t_order.statusDescription = "Submitted to warehouse"
235
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
236
 
237
    logistics_client = LogisticsClient().get_client()
716 rajveer 238
    logistics_info = logistics_client.getLogisticsInfo(t_order.customer_pincode, t_line_item.item_id)
576 chandransh 239
 
716 rajveer 240
    t_order.logistics_provider_id = logistics_info.providerId
576 chandransh 241
    t_order.airwaybill_no = logistics_info.airway_billno
242
    t_order.tracking_id = t_order.airwaybill_no
716 rajveer 243
    t_order.expected_delivery_time = to_java_date(datetime.datetime.now()) + 60*60*1000*logistics_info.deliveryTime
244
    t_order.warehouse_id = logistics_info.warehouseId
576 chandransh 245
 
246
    return t_order
247
 
636 rajveer 248
def create_line_item(item_id):
576 chandransh 249
    inventory_client = InventoryClient().get_client()
636 rajveer 250
    item = inventory_client.getItem(item_id)
576 chandransh 251
    t_line_item = TLineItem()
636 rajveer 252
    t_line_item.model_name = item.modelName
253
    t_line_item.model_number = item.modelNumber
669 chandransh 254
    if item.color is None or item.color == "NA":
255
        t_line_item.color = ""
256
    else:
257
        t_line_item.color = item.color        
636 rajveer 258
    t_line_item.extra_info = item.featureDescription
259
    t_line_item.brand = item.manufacturerName
702 chandransh 260
    t_line_item.item_id = item.id
576 chandransh 261
    t_line_item.quantity = 1
636 rajveer 262
    t_line_item.unit_price = item.sellingPrice
263
    t_line_item.unit_weight = item.weight
264
    t_line_item.total_price = item.sellingPrice
265
    t_line_item.total_weight = item.weight
576 chandransh 266
    return t_line_item
267
 
268
def validate_cart(cartId):
269
    inventory_client = InventoryClient().get_client()
270
    logistics_client = LogisticsClient().get_client()
563 chandransh 271
    retval = True
557 chandransh 272
    # No need to validate duplicate items since there are only two ways
273
    # to add items to a cart and both of them check whether the item being
274
    # added is a duplicate of an already existing item.
563 chandransh 275
    cart = Cart.get_by(id=cartId)
276
    cart_lines = cart.lines
576 chandransh 277
    customer_pincode = ""
690 chandransh 278
    current_time = datetime.datetime.now()
576 chandransh 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:
612 chandransh 287
                try:
643 chandransh 288
                    item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
612 chandransh 289
                except:
643 chandransh 290
                    item_delivery_estimate = 3
612 chandransh 291
                if old_estimate != item_delivery_estimate:
292
                    line.estimate = item_delivery_estimate
690 chandransh 293
                    cart.updated_on = current_time
612 chandransh 294
                    retval = False
576 chandransh 295
        else:
563 chandransh 296
            line.delete()
297
            retval = False
716 rajveer 298
    if cart.checked_out_on is not None:
299
        if cart.updated_on > cart.checked_out_on:
300
            retval = False
612 chandransh 301
    session.commit()
563 chandransh 302
    return retval
576 chandransh 303
 
557 chandransh 304
def merge_cart(fromCartId, toCartId):
305
    fromCart = Cart.get_by(id=fromCartId)
306
    toCart = Cart.get_by(id=toCartId)
307
 
308
    old_lines = fromCart.lines
309
    new_lines = toCart.lines
310
 
311
    for line in old_lines:
312
        flag = True
313
        for new_line in new_lines:
314
            if line.item_id == new_line.item_id:
315
                flag = False
576 chandransh 316
        if flag:
557 chandransh 317
            toCart.lines.append(line)
318
 
319
    toCart.updatedOn = datetime.datetime.now() 
320
 
321
    fromCart.expired_on = datetime.datetime.now()
322
    fromCart.cart_status = CartStatus.INACTIVE
643 chandransh 323
    session.commit()
691 chandransh 324
 
325
def check_out(cartId):
326
    if cartId is None:
327
        raise ShoppingCartException(101, "Cart id not specified")
716 rajveer 328
    cart = Cart.get_by(id = cartId)
691 chandransh 329
    if cart is None:
330
        raise ShoppingCartException(102, "The specified cart couldn't be found")
331
    cart.checked_out_on = datetime.datetime.now()
332
    session.commit()
333
    return True
334
 
335
def reset_cart(cartId, items):
336
    if cartId is None:
337
        raise ShoppingCartException(101, "Cart id not specified")
338
    for item_id, quantity in items.iteritems():
339
        line = Line.query.filter_by(cart_id=cartId, item_id=item_id).one()
340
        if line is not None:
341
            line.quantity = line.quantity - quantity
342
            if line.quantity == 0:
343
                line.delete()
717 rajveer 344
    cart = Cart.get_by(id=cartId)
691 chandransh 345
    cart.updated_on = datetime.datetime.now()
346
    session.commit()
347
    return True