Subversion Repositories SmartDukaan

Rev

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

Rev 22715 Rev 22721
Line 856... Line 856...
856
    except:
856
    except:
857
        traceback.print_exc()
857
        traceback.print_exc()
858
        return False
858
        return False
859
             
859
             
860
    
860
    
-
 
861
    
-
 
862
def validate_fofo_cart(cart, user, privateDealUser, customer_pincode):
-
 
863
    current_time = datetime.datetime.now()
-
 
864
    totalQty = 0
-
 
865
    totalAmount = 0
-
 
866
    cartMessages = []
-
 
867
    cartItems = []
-
 
868
    responseMap = {}
-
 
869
    cartMessageChanged = 0
-
 
870
    cartMessageOOS = 0
-
 
871
    codAllowed = False
-
 
872
    cartMessageUndeliverable = 0
-
 
873
    
-
 
874
    itemIds = [cartLine.item_id for cartLine in cart.lines]
-
 
875
    
-
 
876
    catalog_client = CatalogClient().get_client()
-
 
877
    #logistics_client = LogisticsClient().get_client()
-
 
878
    #promotion_client = PromotionClient().get_client()
-
 
879
    inv_client = InventoryClient().get_client()
-
 
880
    
-
 
881
    fofoDealsMap = catalog_client.getAllFofoDeals(itemIds, [4, 7])    
-
 
882
    fofoDealsAvailability = inv_client.getFofoAvailability(fofoDealsMap.keys())
-
 
883
    
-
 
884
    
-
 
885
 
-
 
886
    itemsMap = catalog_client.getItems(itemIds)
-
 
887
    cart_lines = cart.lines
-
 
888
    cart.total_price = 0
-
 
889
    for line in cart_lines:
-
 
890
        itemQuantityChanged=False
-
 
891
        cartItem={}
-
 
892
        tempBulkItemList = []
-
 
893
        
-
 
894
        old_estimate = line.estimate
-
 
895
        item_id = line.item_id
-
 
896
        item = itemsMap.get(item_id)
-
 
897
        #in case item is missing remove that line
-
 
898
        #lets silently remvoe fofoDealsMap as fofo customer is not allowed to orders with fofo
-
 
899
        if item is None or item.itemStatus==0 or not fofoDealsMap.has_key(item.id):
-
 
900
            Discount.query.filter(Discount.line==line).delete()
-
 
901
            line.delete()
-
 
902
            continue
-
 
903
        cartItem['itemId']=line.item_id
-
 
904
        cartItem['quantity']=0
-
 
905
        cartItem['cartItemMessages']=[]
-
 
906
        cartItemMessages = cartItem['cartItemMessages']
-
 
907
        cartItem['color'] = item.color
-
 
908
        cartItem['catalogItemId'] = item.catalogItemId
-
 
909
        cartItem['packQuantity'] = item.packQuantity
-
 
910
        cartItem['minBuyQuantity'] = item.minimumBuyQuantity
-
 
911
        cartItem['quantityStep'] = item.quantityStep
-
 
912
        cartItem['bulkPricing'] = tempBulkItemList
-
 
913
        cartItem['maxQuantity'] =0
-
 
914
        
-
 
915
        item_shipping_info = catalog_client.isActive(item_id)
-
 
916
        if not fofoDealsAvailability.has_key(item_id):
-
 
917
            continue
-
 
918
        else:
-
 
919
            availability = fofoDealsAvailability.get(item_id)
-
 
920
            if availability < line.quantity:
-
 
921
                line.quantity = availability
-
 
922
                itemQuantityChanged=True
-
 
923
            cartItem['maxQuantity'] = min(availability,100)
-
 
924
            if item.maximumBuyQuantity is not None and item.maximumBuyQuantity >0:
-
 
925
                cartItem['maxQuantity'] = min(item_shipping_info.quantity, item.maximumBuyQuantity)
-
 
926
            if availability < cartItem['minBuyQuantity']:
-
 
927
                cartItem['minBuyQuantity'] = availability 
-
 
928
        
-
 
929
            if line.quantity < cartItem['minBuyQuantity']:
-
 
930
                itemQuantityChanged=True
-
 
931
                line.quantity = cartItem['minBuyQuantity']
-
 
932
            
-
 
933
            if line.quantity > cartItem['maxQuantity']:
-
 
934
                itemQuantityChanged=True
-
 
935
                line.quantity = cartItem['maxQuantity']
-
 
936
            
-
 
937
            cartItem['quantity'] = line.quantity
-
 
938
            line.actual_price = fofoDealsMap.get(item_id) 
-
 
939
        
-
 
940
        cartItem['sellingPrice'] = line.actual_price
-
 
941
        cartItem['bulkPricing'] = []
-
 
942
 
-
 
943
        print "item_shipping_info", item_shipping_info        
-
 
944
        if item_shipping_info.isActive:
-
 
945
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
-
 
946
            ##Lets assign hardcoded deliveryestimate for fofo
-
 
947
            item_delivery_estimate = 2
-
 
948
            codAllowed = False
-
 
949
            cartItem['estimate'] = item_delivery_estimate
-
 
950
            if itemQuantityChanged:
-
 
951
                cartMessageChanged += 1
-
 
952
                cartItemMessages.append({"type":"danger", "messageText":"Only " + str(item_shipping_info.quantity) + " available"})
-
 
953
            if old_estimate != item_delivery_estimate:
-
 
954
                line.estimate = item_delivery_estimate
-
 
955
                cart.updated_on = current_time
-
 
956
            totalAmount += line.actual_price * cartItem['quantity']
-
 
957
        else:
-
 
958
            cartItem['quantity'] = 0
-
 
959
            cartMessageOOS += 1
-
 
960
            cartItemMessages.append({"type":"danger", "messageText":"Out of Stock"})
-
 
961
            Discount.query.filter(Discount.line==line).delete()
-
 
962
            line.delete()
-
 
963
        totalQty += cartItem['quantity']
-
 
964
 
-
 
965
        if cartItemMessages:
-
 
966
            cartItems.insert(0, cartItem)
-
 
967
        else:
-
 
968
            cartItems.append(cartItem)
-
 
969
    if cart.checked_out_on is not None:
-
 
970
        if cart.updated_on > cart.checked_out_on:
-
 
971
            cart.checked_out_on = None
-
 
972
    session.commit()
-
 
973
    
-
 
974
    responseMap['totalQty']= totalQty
-
 
975
    responseMap['totalAmount']= totalAmount
-
 
976
    responseMap['cartMessages']= cartMessages
-
 
977
    responseMap['cartItems']= cartItems
-
 
978
    responseMap['pincode']= customer_pincode
-
 
979
    responseMap['shippingCharge'] = 0
-
 
980
    responseMap['cartMessageChanged'] = cartMessageChanged
-
 
981
    responseMap['cartMessageOOS'] = cartMessageOOS
-
 
982
    responseMap['cartMessageUndeliverable'] = cartMessageUndeliverable
-
 
983
    responseMap['codAllowed'] = codAllowed
-
 
984
    return json.dumps(responseMap)         
-
 
985
    
-
 
986
    
-
 
987
    
861
def validate_cart_new(cartId, customer_pincode, sourceId):
988
def validate_cart_new(cartId, customer_pincode, sourceId):
862
    
989
    
863
    # No need to validate duplicate items since there are only two ways
990
    # No need to validate duplicate items since there are only two ways
864
    # to add items to a cart and both of them check whether the item being
991
    # to add items to a cart and both of them check whether the item being
865
    # added is a duplicate of an already existing item.
992
    # added is a duplicate of an already existing item.
Line 868... Line 995...
868
    current_time = datetime.datetime.now()
995
    current_time = datetime.datetime.now()
869
 
996
 
870
    #if customer_pincode is 000000 pincode should be considered from address or
997
    #if customer_pincode is 000000 pincode should be considered from address or
871
    #is address is not present treat is as customer input
998
    #is address is not present treat is as customer input
872
    user = User.get_by(active_cart_id = cartId)
999
    user = User.get_by(active_cart_id = cartId)
-
 
1000
    privateDealUser = PrivateDealUser.get_by(id=user.id)
873
    if customer_pincode == "000000":
1001
    if customer_pincode == "000000":
874
        address = Address.get_by(id=cart.address_id)
1002
        address = Address.get_by(id=cart.address_id)
875
        if address:
1003
        if address:
876
            customer_pincode = address.pin
1004
            customer_pincode = address.pin
877
    
1005
    
-
 
1006
    if privateDealUser is not None and privateDealUser.isActive and privateDealUser.isFofo:
-
 
1007
        return validate_fofo_cart(cart, user, privateDealUser, customer_pincode)
878
    
1008
    
879
    catalog_client = CatalogClient().get_client()
1009
    catalog_client = CatalogClient().get_client()
880
    logistics_client = LogisticsClient().get_client()
1010
    logistics_client = LogisticsClient().get_client()
881
    promotion_client = PromotionClient().get_client()
1011
    promotion_client = PromotionClient().get_client()
882
 
1012
 
883
 
-
 
884
    
-
 
885
    responseMap = {}
1013
    responseMap = {}
886
    totalQty = 0
1014
    totalQty = 0
887
    nonAccessoryQuantity = 0
1015
    nonAccessoryQuantity = 0
888
    totalAmount = 0 
1016
    totalAmount = 0 
889
    shippingCharges=0
1017
    shippingCharges=0
Line 895... Line 1023...
895
    bulkPricingItems =[]
1023
    bulkPricingItems =[]
896
    
1024
    
897
    
1025
    
898
    itemIds = [cartLine.item_id for cartLine in cart.lines]
1026
    itemIds = [cartLine.item_id for cartLine in cart.lines]
899
    
1027
    
900
    privateDealUser = PrivateDealUser.get_by(id=user.id)
-
 
901
    fofoDealsMap = {}
-
 
902
    if sourceId==53:
-
 
903
        #get tagId valid
-
 
904
        #get all fofoDeals corresponding to item
-
 
905
        #TODO: hardcoded to be removed
-
 
906
        fofoDealsMap = catalog_client.getAllFofoDeals(itemIds, [53, 4, 7])    
-
 
907
    if privateDealUser is not None and privateDealUser.isActive:
1028
    if privateDealUser is not None and privateDealUser.isActive:
908
        if not fofoDealsMap:
-
 
909
            deals = catalog_client.getAllActivePrivateDeals(itemIds, 0)
1029
        deals = catalog_client.getAllActivePrivateDeals(itemIds, 0)
910
            bulkPricingMap = catalog_client.getBulkPricingForItems(itemIds)
1030
        bulkPricingMap = catalog_client.getBulkPricingForItems(itemIds)
911
        else:
-
 
912
            remainingItemIds = []
-
 
913
            for remainingItemId in itemIds:
-
 
914
                if remainingItemId not  in fofoDealsMap.keys():
-
 
915
                    remainingItemIds.append(remainingItemId)
-
 
916
            if remainingItemIds:
-
 
917
                deals = catalog_client.getAllActivePrivateDeals(remainingItemIds, 0)
-
 
918
                bulkPricingMap = catalog_client.getBulkPricingForItems(remainingItemIds)
-
 
919
        dealItems = deals.keys()
1031
        dealItems = deals.keys()
920
        bulkPricingItems = bulkPricingMap.keys()
1032
        bulkPricingItems = bulkPricingMap.keys()
921
 
1033
 
922
    cart.total_price = 0
1034
    cart.total_price = 0
923
    itemsMap = catalog_client.getItems(itemIds)
1035
    itemsMap = catalog_client.getItems(itemIds)
Line 1006... Line 1118...
1006
            if deals[item_id].dealFreebieOption==0:
1118
            if deals[item_id].dealFreebieOption==0:
1007
                line.freebieId = 0
1119
                line.freebieId = 0
1008
            if deals[item_id].dealFreebieOption==2:
1120
            if deals[item_id].dealFreebieOption==2:
1009
                line.freebieId =  deals[item_id].dealFreebieItemId
1121
                line.freebieId =  deals[item_id].dealFreebieItemId
1010
            cartItem['dealText'] = line.dealText
1122
            cartItem['dealText'] = line.dealText
1011
        elif fofoDealsMap.has_key(item_id):
-
 
1012
            line.actual_price = fofoDealsMap.get(item_id) 
-
 
1013
        else:
1123
        else:
1014
            if not singleUnitPricing and item_id in bulkPricingItems:
1124
            if not singleUnitPricing and item_id in bulkPricingItems:
1015
                tempBulkItemList.append({'quantity':1,'price':item.sellingPrice})
1125
                tempBulkItemList.append({'quantity':1,'price':item.sellingPrice})
1016
            if bulkPrice is None:
1126
            if bulkPrice is None:
1017
                line.actual_price = item.sellingPrice
1127
                line.actual_price = item.sellingPrice
Line 1070... Line 1180...
1070
                    if billingWarehouse is not None:
1180
                    if billingWarehouse is not None:
1071
                        estimateVal = None
1181
                        estimateVal = None
1072
                        if not logistics_client.isAlive() :
1182
                        if not logistics_client.isAlive() :
1073
                            logistics_client = LogisticsClient().get_client()
1183
                            logistics_client = LogisticsClient().get_client()
1074
                        try:
1184
                        try:
1075
                            estimateVal = logistics_client.getFirstDeliveryEstimateForWhLocation(customer_pincode, billingWarehouse.stateId)
1185
                            estimateVal = logistics_client.getFirstDeliveryEstimateForWhLocation(customer_pincode, billingWarehouse.logisticsLocation)
1076
                            if estimateVal ==-1:
1186
                            if estimateVal ==-1:
1077
                                item_delivery_estimate =-1
1187
                                item_delivery_estimate =-1
1078
                        except:
1188
                        except:
1079
                            traceback.print_exc()
1189
                            traceback.print_exc()
1080
                            pass
1190
                            pass