Subversion Repositories SmartDukaan

Rev

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

Rev 21084 Rev 21454
Line 235... Line 235...
235
    delete_discounts_from_cart(cart.id, cart=cart)
235
    delete_discounts_from_cart(cart.id, cart=cart)
236
    cart.discounted_price = None
236
    cart.discounted_price = None
237
    cart.coupon_code = None
237
    cart.coupon_code = None
238
    session.commit()
238
    session.commit()
239
    
239
    
240
def commit_cart(cart_id, sessionSource, sessionTime, firstSource, firstSourceTime, userId, schemeId, orderSource):   
240
def commit_cart(cart_id, sessionSource, sessionTime, firstSource, firstSourceTime, userId, schemeId, orderSource, selfPickup):   
241
    cart = get_cart_by_id(cart_id)   
241
    cart = get_cart_by_id(cart_id)   
242
    #now we have a cart. Need to create a transaction with it
242
    #now we have a cart. Need to create a transaction with it
243
    totalCartVal = 0
243
    totalCartVal = 0
244
    totalQty = 0
244
    totalQty = 0
245
    itemIds = []
245
    itemIds = []
Line 262... Line 262...
262
    txn.sessionSource = sessionSource
262
    txn.sessionSource = sessionSource
263
    txn.sessionStartTime = sessionTime
263
    txn.sessionStartTime = sessionTime
264
    txn.firstSource = firstSource
264
    txn.firstSource = firstSource
265
    txn.firstSourceTime = firstSourceTime
265
    txn.firstSourceTime = firstSourceTime
266
    txn.payment_option = schemeId
266
    txn.payment_option = schemeId
267
    privateDealUser = PrivateDealUser.query.filter(PrivateDealUser.id == userId).filter(PrivateDealUser.isActive==True).first()
-
 
-
 
267
 
268
    perUnitShippingCost = 0
268
    perUnitShippingCost = 0
269
    if privateDealUser is not None:
-
 
270
        if totalQty >= 5:
269
    if totalQty >= 5:
271
            perUnitShippingCost = 30
270
        perUnitShippingCost = 30
272
        else:
271
    else:
273
            perUnitShippingCost = 60
272
        perUnitShippingCost = 60
274
            
273
            
275
        
274
        
276
    txn.totalShippingCost = perUnitShippingCost * totalQty
275
    txn.totalShippingCost = perUnitShippingCost * totalQty
277
        
-
 
278
    txnOrders = create_orders(cart, userId, orderSource, perUnitShippingCost, totalCartVal, itemsMap)
276
    txnOrders = create_orders(cart, userId, orderSource, perUnitShippingCost, totalCartVal, itemsMap, selfPickup)
279
#    shippingCostInOrders = 0
-
 
280
#    for order in txnOrders:
-
 
281
#        shippingCostInOrders = shippingCostInOrders + order.shippingCost
-
 
282
    
-
 
283
    #diff = totalshippingCost - shippingCostInOrders
-
 
284
    #txnOrders[0].shippingCost = txnOrders[0].shippingCost + diff
-
 
285
    
-
 
286
    
-
 
287
    txn.orders = txnOrders
-
 
288
    
277
    
-
 
278
    txn.orders = txnOrders    
289
    transaction_client = TransactionClient().get_client()
279
    transaction_client = TransactionClient().get_client()
290
    txn_id = transaction_client.createTransaction(txn)
280
    txn_id = transaction_client.createTransaction(txn)
291
    
281
    
292
    privateDealUser = PrivateDealUser.query.filter(PrivateDealUser.id == userId).filter(PrivateDealUser.isActive==True).first()
-
 
293
    if privateDealUser is not None and privateDealUser.counter is not None:
-
 
294
        privateDealUser.counter.lastPurchasedOn = datetime.datetime.now()
-
 
295
    session.commit()
282
    session.commit()
296
    
283
    
297
    return txn_id
284
    return txn_id
298
 
285
 
299
def create_orders(cart, userId, orderSource, perUnitShippingCost, totalCartVal, itemsMap):
286
def create_orders(cart, userId, orderSource, perUnitShippingCost, totalCartVal, itemsMap, selfPickup):
300
    cart_lines = cart.lines
287
    cart_lines = cart.lines
301
    orders = []
288
    orders = []
302
    isGv = False
289
    isGv = False
303
    if cart.coupon_code:
290
    if cart.coupon_code:
304
        try:
291
        try:
Line 322... Line 309...
322
                    #i += 1
309
                    #i += 1
323
                quantity_remaining_for_order -= discount.quantity
310
                quantity_remaining_for_order -= discount.quantity
324
            
311
            
325
            if quantity_remaining_for_order > 0:
312
            if quantity_remaining_for_order > 0:
326
                t_line_item = create_line_item(line, line.actual_price, quantity_remaining_for_order, itemsMap.get(line.item_id))
313
                t_line_item = create_line_item(line, line.actual_price, quantity_remaining_for_order, itemsMap.get(line.item_id))
327
                t_order = create_order(userId, cart.address_id, t_line_item, cart.pickupStoreId, 0, line.insurer, (line.insuranceAmount/line.quantity), insuranceDetails, line.dataProtectionInsurer,(line.dataProtectionAmount)/line.quantity, orderSource, line.freebieId, perUnitShippingCost, itemsMap.get(line.item_id))
314
                t_order = create_order(userId, cart.address_id, t_line_item, cart.pickupStoreId, 0, line.insurer, (line.insuranceAmount/line.quantity), insuranceDetails, line.dataProtectionInsurer,(line.dataProtectionAmount)/line.quantity, orderSource, line.freebieId, perUnitShippingCost, itemsMap.get(line.item_id), selfPickup)
328
                orders.append(t_order)
315
                orders.append(t_order)
329
 
316
 
330
    
317
    
331
    wallet_amount = cart.wallet_amount
318
    wallet_amount = cart.wallet_amount
332
    if wallet_amount is None:
319
    if wallet_amount is None:
Line 340... Line 327...
340
        
327
        
341
        order.net_payable_amount = order.total_amount+ order.shippingCost - order.gvAmount - order.wallet_amount
328
        order.net_payable_amount = order.total_amount+ order.shippingCost - order.gvAmount - order.wallet_amount
342
        wallet_amount = wallet_amount - order.wallet_amount
329
        wallet_amount = wallet_amount - order.wallet_amount
343
    return orders
330
    return orders
344
 
331
 
345
def create_order(userId, address_id, t_line_item, pickupStoreId, gvAmount, insurer, insuranceAmount, insuranceDetails, dataProtectionInsurer, dataProtectionAmount, orderSource, freebieId, perUnitShippingCost, item):
332
def create_order(userId, address_id, t_line_item, pickupStoreId, gvAmount, insurer, insuranceAmount, insuranceDetails, dataProtectionInsurer, dataProtectionAmount, orderSource, freebieId, perUnitShippingCost, item, selfPickup):
346
    user = User.get_by(id=userId)
333
    user = User.get_by(id=userId)
347
    address = Address.get_by(id=address_id)
334
    address = Address.get_by(id=address_id)
348
    t_order = TOrder()
335
    t_order = TOrder()
349
    
336
    
350
    t_order.customer_id = user.id
337
    t_order.customer_id = user.id
Line 369... Line 356...
369
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
356
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
370
    
357
    
371
    t_order.pickupStoreId = pickupStoreId 
358
    t_order.pickupStoreId = pickupStoreId 
372
    t_order.insuranceAmount = insuranceAmount 
359
    t_order.insuranceAmount = insuranceAmount 
373
    t_order.insurer = insurer
360
    t_order.insurer = insurer
-
 
361
 
374
    if insuranceDetails:
362
    if insuranceDetails:
375
        t_order.dob = insuranceDetails.dob
363
        t_order.dob = insuranceDetails.dob
376
        t_order.guardianName = insuranceDetails.guardianName
364
        t_order.guardianName = insuranceDetails.guardianName
377
    
365
    
378
    catalog_client = CatalogClient().get_client()
366
    catalog_client = CatalogClient().get_client()
Line 385... Line 373...
385
        freebie_item_id = None if freebieId == 0 else freebieId  
373
        freebie_item_id = None if freebieId == 0 else freebieId  
386
    t_order.source = orderSource
374
    t_order.source = orderSource
387
    t_order.dataProtectionInsurer = dataProtectionInsurer
375
    t_order.dataProtectionInsurer = dataProtectionInsurer
388
    t_order.dataProtectionAmount = dataProtectionAmount
376
    t_order.dataProtectionAmount = dataProtectionAmount
389
    #if item.category in [10006, 10010]:
377
    #if item.category in [10006, 10010]:
-
 
378
    if selfPickup:
-
 
379
        t_order.logistics_provider_id = 4
-
 
380
    else:
390
    t_order.shippingCost = perUnitShippingCost*t_line_item.quantity
381
        t_order.shippingCost = perUnitShippingCost*t_line_item.quantity
391
    #else:
382
    #else:
392
    #    t_order.shippingCost = 0
383
    #    t_order.shippingCost = 0
393
    return t_order
384
    return t_order
394
        
385
        
395
def create_line_item(line, final_price, quantity=1, item=None):
386
def create_line_item(line, final_price, quantity=1, item=None):
Line 864... Line 855...
864
        return False
855
        return False
865
             
856
             
866
    
857
    
867
def validate_cart_new(cartId, customer_pincode, sourceId):
858
def validate_cart_new(cartId, customer_pincode, sourceId):
868
    
859
    
869
    inventory_client = CatalogClient().get_client()
-
 
870
    logistics_client = LogisticsClient().get_client()
-
 
871
    promotion_client = PromotionClient().get_client()
-
 
872
    # No need to validate duplicate items since there are only two ways
860
    # No need to validate duplicate items since there are only two ways
873
    # to add items to a cart and both of them check whether the item being
861
    # to add items to a cart and both of them check whether the item being
874
    # added is a duplicate of an already existing item.
862
    # added is a duplicate of an already existing item.
875
    cart = Cart.get_by(id=cartId)
863
    cart = Cart.get_by(id=cartId)
876
    cart_lines = cart.lines
864
    cart_lines = cart.lines
877
    current_time = datetime.datetime.now()
865
    current_time = datetime.datetime.now()
878
 
866
 
-
 
867
    #if customer_pincode is 000000 pincode should be considered from address or
-
 
868
    #is address is not present treat is as customer input
879
    user = User.get_by(active_cart_id = cartId)
869
    user = User.get_by(active_cart_id = cartId)
-
 
870
    if customer_pincode == "000000":
-
 
871
        address = Address.get_by(id=cart.address_id)
-
 
872
        if address:
-
 
873
            customer_pincode = address.pin
-
 
874
    
-
 
875
    
-
 
876
    inventory_client = CatalogClient().get_client()
-
 
877
    logistics_client = LogisticsClient().get_client()
-
 
878
    promotion_client = PromotionClient().get_client()
-
 
879
 
-
 
880
 
-
 
881
    
880
    responseMap = {}
882
    responseMap = {}
881
    totalQty = 0
883
    totalQty = 0
882
    nonAccessoryQuantity = 0
884
    nonAccessoryQuantity = 0
883
    totalAmount = 0 
885
    totalAmount = 0 
884
    shippingCharges=0
886
    shippingCharges=0
Line 886... Line 888...
886
    cartItems = []
888
    cartItems = []
887
    dealItems = []
889
    dealItems = []
888
    bulkPricingMap ={}
890
    bulkPricingMap ={}
889
    bulkPricingItems =[]
891
    bulkPricingItems =[]
890
    
892
    
891
    privateDealUser = PrivateDealUser.get_by(id=user.id)    
-
 
-
 
893
    
892
    itemIds = [cartLine.item_id for cartLine in cart.lines]
894
    itemIds = [cartLine.item_id for cartLine in cart.lines]
-
 
895
    
-
 
896
    privateDealUser = PrivateDealUser.get_by(id=user.id)    
893
    if privateDealUser is not None and privateDealUser.isActive:
897
    if privateDealUser is not None and privateDealUser.isActive:
894
        deals = inventory_client.getAllActivePrivateDeals(itemIds, 0)
898
        deals = inventory_client.getAllActivePrivateDeals(itemIds, 0)
895
        dealItems = deals.keys()
899
        dealItems = deals.keys()
896
        bulkPricingMap = inventory_client.getBulkPricingForItems(itemIds)
900
        bulkPricingMap = inventory_client.getBulkPricingForItems(itemIds)
897
        bulkPricingItems = bulkPricingMap.keys() 
901
        bulkPricingItems = bulkPricingMap.keys() 
Line 900... Line 904...
900
    itemsMap = inventory_client.getItems(itemIds)
904
    itemsMap = inventory_client.getItems(itemIds)
901
    
905
    
902
    cartMessageChanged = 0
906
    cartMessageChanged = 0
903
    cartMessageOOS = 0
907
    cartMessageOOS = 0
904
    cartMessageUndeliverable = 0
908
    cartMessageUndeliverable = 0
-
 
909
    codAllowed = True
905
    
910
    
906
    
911
    
907
    
912
    
908
    for line in cart_lines:
913
    for line in cart_lines:
909
        itemQuantityChanged=False
914
        itemQuantityChanged=False
Line 951... Line 956...
951
            itemQuantityChanged=True
956
            itemQuantityChanged=True
952
            line.quantity = cartItem['maxQuantity']
957
            line.quantity = cartItem['maxQuantity']
953
        
958
        
954
        cartItem['quantity'] = line.quantity
959
        cartItem['quantity'] = line.quantity
955
        
960
        
-
 
961
        print "at : 961"
-
 
962
        
956
        bulkPrice = None
963
        bulkPrice = None
957
        singleUnitPricing = False
964
        singleUnitPricing = False
958
        if item_id in bulkPricingItems:
965
        if item_id in bulkPricingItems:
959
            #Check quantity qualifies or not
966
            #Check quantity qualifies or not
960
            bulkPricingList = bulkPricingMap.get(item_id)
967
            bulkPricingList = bulkPricingMap.get(item_id)
Line 1001... Line 1008...
1001
            if dictbulkPricing['quantity'] < cartItem['minBuyQuantity'] or dictbulkPricing['quantity'] > cartItem['maxQuantity']:
1008
            if dictbulkPricing['quantity'] < cartItem['minBuyQuantity'] or dictbulkPricing['quantity'] > cartItem['maxQuantity']:
1002
                toRemove.append(dictbulkPricing)
1009
                toRemove.append(dictbulkPricing)
1003
        for removePricing in toRemove:
1010
        for removePricing in toRemove:
1004
            cartItem['bulkPricing'].remove(removePricing)
1011
            cartItem['bulkPricing'].remove(removePricing)
1005
        cartItem['bulkPricing'] = sorted(cartItem['bulkPricing'], key=lambda k: k['quantity'],reverse=False)
1012
        cartItem['bulkPricing'] = sorted(cartItem['bulkPricing'], key=lambda k: k['quantity'],reverse=False)
1006
        
1013
 
-
 
1014
        print "item_shipping_info", item_shipping_info        
1007
        if item_shipping_info.isActive:
1015
        if item_shipping_info.isActive:
1008
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
1016
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
1009
            try:
1017
            try:
1010
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
1018
                item_delivery_estimate_tuple = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID)
-
 
1019
                item_delivery_estimate = item_delivery_estimate_tuple.deliveryTime
-
 
1020
                print "item_delivery_estimate", item_delivery_estimate 
-
 
1021
                if item_delivery_estimate:
-
 
1022
                    codAllowed = codAllowed and item_delivery_estimate_tuple.codAllowed 
1011
            except LogisticsServiceException:
1023
            except LogisticsServiceException:
-
 
1024
                traceback.print_exc()
1012
                item_delivery_estimate = -1
1025
                item_delivery_estimate = -1
1013
                #TODO Use the exception clause to set the retval appropriately
1026
                #TODO Use the exception clause to set the retval appropriately
1014
            except :
1027
            except :
-
 
1028
                traceback.print_exc()
1015
                item_delivery_estimate = -1
1029
                item_delivery_estimate = -1
1016
                
1030
                
1017
            if item_delivery_estimate !=-1:
1031
            if item_delivery_estimate !=-1:
1018
                inv_client = InventoryClient().get_client()
1032
                inv_client = InventoryClient().get_client()
1019
                itemAvailability = None
1033
                itemAvailability = None
Line 1026... Line 1040...
1026
                if itemAvailability is not None:
1040
                if itemAvailability is not None:
1027
                    billingWarehouse = None
1041
                    billingWarehouse = None
1028
                    try:
1042
                    try:
1029
                        billingWarehouse = inv_client.getWarehouse(itemAvailability[2])
1043
                        billingWarehouse = inv_client.getWarehouse(itemAvailability[2])
1030
                    except:
1044
                    except:
-
 
1045
                        traceback.print_exc()
1031
                        pass
1046
                        pass
1032
                    
1047
                    
1033
                    print 'billingWarehouse Id Location ', billingWarehouse.stateId
1048
                    print 'billingWarehouse Id Location ', billingWarehouse.stateId
1034
                    if billingWarehouse is not None:
1049
                    if billingWarehouse is not None:
1035
                        estimateVal = None
1050
                        estimateVal = None
Line 1038... Line 1053...
1038
                        try:
1053
                        try:
1039
                            estimateVal = logistics_client.getFirstDeliveryEstimateForWhLocation(customer_pincode, billingWarehouse.stateId)
1054
                            estimateVal = logistics_client.getFirstDeliveryEstimateForWhLocation(customer_pincode, billingWarehouse.stateId)
1040
                            if estimateVal ==-1:
1055
                            if estimateVal ==-1:
1041
                                item_delivery_estimate =-1
1056
                                item_delivery_estimate =-1
1042
                        except:
1057
                        except:
-
 
1058
                            traceback.print_exc()
1043
                            pass
1059
                            pass
1044
                        print 'estimateVal Value ', estimateVal
1060
                        print 'estimateVal Value ', estimateVal
1045
            cartItem['estimate'] = item_delivery_estimate
1061
            cartItem['estimate'] = item_delivery_estimate
1046
            if item_delivery_estimate == -1:
1062
            if item_delivery_estimate == -1:
1047
                Discount.query.filter(Discount.line==line).delete()
1063
                Discount.query.filter(Discount.line==line).delete()
Line 1121... Line 1137...
1121
    responseMap['shippingCharge']=shippingCharges
1137
    responseMap['shippingCharge']=shippingCharges
1122
    responseMap['cartMessageChanged'] = cartMessageChanged
1138
    responseMap['cartMessageChanged'] = cartMessageChanged
1123
    responseMap['cartMessageOOS'] = cartMessageOOS
1139
    responseMap['cartMessageOOS'] = cartMessageOOS
1124
    responseMap['cartMessageUndeliverable'] = cartMessageUndeliverable
1140
    responseMap['cartMessageUndeliverable'] = cartMessageUndeliverable
1125
    responseMap['nonAccessoryQuantity'] = nonAccessoryQuantity
1141
    responseMap['nonAccessoryQuantity'] = nonAccessoryQuantity
-
 
1142
    responseMap['codAllowed'] = codAllowed
1126
    return json.dumps(responseMap)
1143
    return json.dumps(responseMap)
1127
    
1144
    
1128
    
1145
    
1129
def validate_cart_plus(cart_id, source_id, couponCode):
1146
def validate_cart_plus(cart_id, source_id, couponCode):
1130
    try:
1147
    try: