Subversion Repositories SmartDukaan

Rev

Rev 1894 | Rev 1983 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1894 Rev 1976
Line 18... Line 18...
18
 
18
 
19
from shop2020.clients.TransactionClient import TransactionClient
19
from shop2020.clients.TransactionClient import TransactionClient
20
from shop2020.clients.InventoryClient import InventoryClient
20
from shop2020.clients.InventoryClient import InventoryClient
21
from shop2020.clients.LogisticsClient import LogisticsClient
21
from shop2020.clients.LogisticsClient import LogisticsClient
22
from shop2020.model.v1 import user
22
from shop2020.model.v1 import user
-
 
23
from shop2020.clients.PromotionClient import PromotionClient
23
 
24
 
24
def get_cart(user_id):
25
def get_cart(user_id):
25
    query = Cart.query.filter_by(user_id=user_id)      
26
    query = Cart.query.filter_by(user_id=user_id)      
26
    query = query.filter_by(cart_status = CartStatus.ACTIVE)
27
    query = query.filter_by(cart_status = CartStatus.ACTIVE)
27
    try:
28
    try:
Line 173... Line 174...
173
    cart.address_id = address_id
174
    cart.address_id = address_id
174
    current_time = datetime.datetime.now()
175
    current_time = datetime.datetime.now()
175
    #cart.updated_on = current_time
176
    #cart.updated_on = current_time
176
    session.commit()
177
    session.commit()
177
    
178
    
-
 
179
def apply_coupon_to_cart(cart_id, coupon_code, total_price, discounted_price):
-
 
180
    cart = get_cart_by_id(cart_id)
-
 
181
    if not cart:
-
 
182
        raise ShoppingCartException(101, "no cart attached to this id")
-
 
183
    cart.total_price = total_price
-
 
184
    cart.discounted_price = discounted_price
-
 
185
    cart.coupon_code = coupon_code
-
 
186
    session.commit()
-
 
187
 
-
 
188
def remove_coupon(cart_id):
-
 
189
    cart = get_cart_by_id(cart_id)
-
 
190
    if not cart:
-
 
191
        raise ShoppingCartException(101, "no cart attached to this id")
-
 
192
    cart.discounted_price = None
-
 
193
    cart.coupon_code = None
-
 
194
    session.commit()
-
 
195
    
178
def commit_cart(cart_id):   
196
def commit_cart(cart_id):   
179
    cart = get_cart_by_id(cart_id)   
197
    cart = get_cart_by_id(cart_id)   
180
    #now we have a cart. Need to create a transaction with it
198
    #now we have a cart. Need to create a transaction with it
181
    txn = TTransaction()
199
    txn = TTransaction()
182
    txn.shoppingCartid = cart_id
200
    txn.shoppingCartid = cart_id
Line 203... Line 221...
203
    
221
    
204
    for line in cart_lines:
222
    for line in cart_lines:
205
        if line.line_status == LineStatus.LINE_ACTIVE:
223
        if line.line_status == LineStatus.LINE_ACTIVE:
206
            i = 0
224
            i = 0
207
            while i< line.quantity:
225
            while i< line.quantity:
208
                t_line_item = create_line_item(line.item_id)
226
                t_line_item = create_line_item(line.item_id, line.discounted_price)
209
                t_order = create_order(cart.user_id, cart.address_id, t_line_item)
227
                t_order = create_order(cart.user_id, cart.address_id, t_line_item)
210
                orders.append(t_order)
228
                orders.append(t_order)
211
                i += 1
229
                i += 1
212
    return orders
230
    return orders
213
 
231
 
Line 226... Line 244...
226
    t_order.customer_city = address.city
244
    t_order.customer_city = address.city
227
    t_order.customer_state = address.state
245
    t_order.customer_state = address.state
228
    t_order.customer_mobilenumber = address.phone
246
    t_order.customer_mobilenumber = address.phone
229
    
247
    
230
    t_order.total_amount = t_line_item.total_price
248
    t_order.total_amount = t_line_item.total_price
-
 
249
    t_order.discounted_amount = t_line_item.discounted_price
-
 
250
    
231
    t_order.total_weight = t_line_item.total_weight
251
    t_order.total_weight = t_line_item.total_weight
232
    t_order.lineitems = [t_line_item]
252
    t_order.lineitems = [t_line_item]
233
    
253
    
234
    t_order.status = OrderStatus.PAYMENT_PENDING
254
    t_order.status = OrderStatus.PAYMENT_PENDING
235
    t_order.statusDescription = "Payment Pending"
255
    t_order.statusDescription = "Payment Pending"
Line 247... Line 267...
247
    t_order.warehouse_id = logistics_info.warehouseId
267
    t_order.warehouse_id = logistics_info.warehouseId
248
    '''
268
    '''
249
    
269
    
250
    return t_order
270
    return t_order
251
        
271
        
252
def create_line_item(item_id):
272
def create_line_item(item_id, discounted_price):
253
    inventory_client = InventoryClient().get_client()
273
    inventory_client = InventoryClient().get_client()
254
    item = inventory_client.getItem(item_id)
274
    item = inventory_client.getItem(item_id)
255
    t_line_item = TLineItem()
275
    t_line_item = TLineItem()
256
    t_line_item.productGroup = item.productGroup
276
    t_line_item.productGroup = item.productGroup
257
    t_line_item.brand = item.brand
277
    t_line_item.brand = item.brand
Line 265... Line 285...
265
    t_line_item.item_id = item.id
285
    t_line_item.item_id = item.id
266
    t_line_item.quantity = 1
286
    t_line_item.quantity = 1
267
    t_line_item.unit_price = item.sellingPrice
287
    t_line_item.unit_price = item.sellingPrice
268
    t_line_item.unit_weight = item.weight
288
    t_line_item.unit_weight = item.weight
269
    t_line_item.total_price = item.sellingPrice
289
    t_line_item.total_price = item.sellingPrice
-
 
290
    t_line_item.discounted_price = discounted_price
270
    t_line_item.total_weight = item.weight
291
    t_line_item.total_weight = item.weight
271
    return t_line_item
292
    return t_line_item
272
    
293
    
273
def validate_cart(cartId):
294
def validate_cart(cartId):
274
    inventory_client = InventoryClient().get_client()
295
    inventory_client = InventoryClient().get_client()
275
    logistics_client = LogisticsClient().get_client()
296
    logistics_client = LogisticsClient().get_client()
-
 
297
    promotion_client = PromotionClient().get_client()
276
    retval = ""
298
    retval = ""
277
    # No need to validate duplicate items since there are only two ways
299
    # 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
300
    # 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.
301
    # added is a duplicate of an already existing item.
280
    cart = Cart.get_by(id=cartId)
302
    cart = Cart.get_by(id=cartId)
Line 291... Line 313...
291
            address = Address.get_by(id = default_address_id)
313
            address = Address.get_by(id = default_address_id)
292
            customer_pincode = address.pin
314
            customer_pincode = address.pin
293
    if not customer_pincode:
315
    if not customer_pincode:
294
        #FIXME should not be hard coded. May be we can pick from config server.
316
        #FIXME should not be hard coded. May be we can pick from config server.
295
        customer_pincode = "110001"
317
        customer_pincode = "110001"
-
 
318
    cart.total_price = 0
296
    for line in cart_lines:
319
    for line in cart_lines:
297
        old_estimate = line.estimate
320
        old_estimate = line.estimate
298
        item_id = line.item_id
321
        item_id = line.item_id
-
 
322
        item = inventory_client.getItem(item_id)
299
        if inventory_client.isActive(item_id):
323
        if inventory_client.isActive(item_id):
-
 
324
            line.actual_price = item.sellingPrice
-
 
325
            cart.total_price = cart.total_price + line.actual_price
300
            try:
326
            try:
301
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
327
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
302
            except LogisticsServiceException:
328
            except LogisticsServiceException:
303
                item_delivery_estimate = -1
329
                item_delivery_estimate = -1
304
                #TODO Use the exception clause to set the retval appropriately
330
                #TODO Use the exception clause to set the retval appropriately
Line 316... Line 342...
316
        if cart.updated_on > cart.checked_out_on:
342
        if cart.updated_on > cart.checked_out_on:
317
            cart.checked_out_on = None
343
            cart.checked_out_on = None
318
            if retval == "":
344
            if retval == "":
319
                retval = "Your cart has been updated after the last checkout."
345
                retval = "Your cart has been updated after the last checkout."
320
    session.commit()
346
    session.commit()
-
 
347
    
-
 
348
    if cart.coupon_code is not None:
-
 
349
        updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
-
 
350
        for t_line in updated_cart.lines:
-
 
351
            #Find the line in the database which corresponds to this line
-
 
352
            line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
-
 
353
            #Update its discounted price.
-
 
354
            line.discounted_price = t_line.discountedPrice
-
 
355
        cart.discounted_price = updated_cart.discountedPrice
-
 
356
        session.commit()
321
    return retval
357
    return retval
322
 
358
 
323
def merge_cart(fromCartId, toCartId):
359
def merge_cart(fromCartId, toCartId):
324
    fromCart = Cart.get_by(id=fromCartId)
360
    fromCart = Cart.get_by(id=fromCartId)
325
    toCart = Cart.get_by(id=toCartId)
361
    toCart = Cart.get_by(id=toCartId)
Line 361... Line 397...
361
            if line.quantity == 0:
397
            if line.quantity == 0:
362
                line.delete()
398
                line.delete()
363
    cart = Cart.get_by(id=cartId)
399
    cart = Cart.get_by(id=cartId)
364
    cart.updated_on = datetime.datetime.now()
400
    cart.updated_on = datetime.datetime.now()
365
    cart.checked_out_on = None
401
    cart.checked_out_on = None
-
 
402
    
-
 
403
    # Removing Coupon
-
 
404
    cart.total_price = None
-
 
405
    cart.discounted_price = None
-
 
406
    cart.coupon_code = None
-
 
407
    
366
    session.commit()
408
    session.commit()
367
    return True
409
    return True
368
 
410
 
369
def close_session():
411
def close_session():
370
    if session.is_active:
412
    if session.is_active: