Subversion Repositories SmartDukaan

Rev

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

Rev 17648 Rev 17782
Line 21... Line 21...
21
    OrderStatus, OrderSource
21
    OrderStatus, OrderSource
22
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, \
22
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, \
23
    ShoppingCartException, PromotionException, CartPlus
23
    ShoppingCartException, PromotionException, CartPlus
24
from shop2020.utils.Utils import to_py_date, to_java_date
24
from shop2020.utils.Utils import to_py_date, to_java_date
25
import datetime
25
import datetime
-
 
26
import json
26
import math
27
import math
-
 
28
import traceback
27
 
29
 
28
 
30
 
29
 
31
 
30
 
32
 
31
def get_cart(userId):
33
def get_cart(userId):
Line 766... Line 768...
766
    except:
768
    except:
767
        print("Error : Unable to get insurance details for addressId : " + str(addressId))
769
        print("Error : Unable to get insurance details for addressId : " + str(addressId))
768
        return False
770
        return False
769
    return True
771
    return True
770
 
772
 
-
 
773
 
-
 
774
def add_items_to_cart(cartId, itemQty, couponCode=None):
-
 
775
    try: 
-
 
776
        found_cart = Cart.get_by(id=cartId)
-
 
777
        itemQtyMap = {}
-
 
778
        current_time = datetime.datetime.now()
-
 
779
        for itemqty in itemQty:
-
 
780
            itemQtyMap[itemqty.itemId] = itemqty.qty 
-
 
781
            
-
 
782
        for line in found_cart.lines:
-
 
783
            if itemQtyMap.has_key(line.item_id):
-
 
784
                line.delete()
-
 
785
        for itemId,qty in itemQtyMap.iteritems():
-
 
786
            line = Line()
-
 
787
            line.cart = found_cart
-
 
788
            line.item_id = itemId
-
 
789
            line.quantity = qty
-
 
790
            line.created_on = current_time
-
 
791
            line.updated_on = current_time
-
 
792
            line.line_status = LineStatus.LINE_ACTIVE
-
 
793
            line.insurer = 0
-
 
794
            line.insuranceAmount = 0
-
 
795
        if couponCode:
-
 
796
            found_cart.coupon_code = couponCode
-
 
797
        else:
-
 
798
            found_cart.coupon_code = None
-
 
799
        session.commit()
-
 
800
        return True
-
 
801
    except:
-
 
802
        traceback.print_exc()
-
 
803
        return False
-
 
804
             
-
 
805
    
-
 
806
def valiate_cart_new(cartId, customer_pincode, sourceId):
-
 
807
    
-
 
808
    inventory_client = CatalogClient().get_client()
-
 
809
    logistics_client = LogisticsClient().get_client()
-
 
810
    promotion_client = PromotionClient().get_client()
-
 
811
    # No need to validate duplicate items since there are only two ways
-
 
812
    # to add items to a cart and both of them check whether the item being
-
 
813
    # added is a duplicate of an already existing item.
-
 
814
    cart = Cart.get_by(id=cartId)
-
 
815
    cart_lines = cart.lines
-
 
816
    current_time = datetime.datetime.now()
-
 
817
 
-
 
818
    user = User.get_by(active_cart_id = cartId)
-
 
819
    responseMap = {}
-
 
820
    totalQty = 0
-
 
821
    totalAmount = 0 
-
 
822
    shippingCharges=0
-
 
823
    cartMessages=[]
-
 
824
    cartItems = []
-
 
825
    dealItems = []
-
 
826
    
-
 
827
    responseMap['shippingCharges']= shippingCharges
-
 
828
    responseMap['cartMessages']= cartMessages
-
 
829
    responseMap['cartItems']= cartItems
-
 
830
    responseMap['pincode']= customer_pincode
-
 
831
 
-
 
832
    privateDealUser = PrivateDealUser.get_by(id=user.id)    
-
 
833
    itemIds = [cartLine.item_id for cartLine in cart.lines]
-
 
834
    if privateDealUser is not None and privateDealUser.isActive:
-
 
835
        deals = inventory_client.getAllActivePrivateDeals(itemIds, 0)
-
 
836
        dealItems = deals.keys()
-
 
837
 
-
 
838
    cart.total_price = 0
-
 
839
    itemsMap = inventory_client.getItems(itemIds)
-
 
840
    for line in cart_lines:
-
 
841
        cartItem={}
-
 
842
        
-
 
843
        old_estimate = line.estimate
-
 
844
        item_id = line.item_id
-
 
845
        item = itemsMap.get(item_id)
-
 
846
        cartItem['itemId']=line.item_id
-
 
847
        cartItem['quantity']=0
-
 
848
        cartItem['messages']=[]
-
 
849
        cartItemMessages = cartItem['messages']
-
 
850
        cartItem['color'] = item.color
-
 
851
        cartItem['catalogItemId'] = item.catalogItemId
-
 
852
        
-
 
853
        item_shipping_info = inventory_client.isActive(item_id) 
-
 
854
        if item_shipping_info.isActive:
-
 
855
            if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
-
 
856
                line.quantity = item_shipping_info.quantity
-
 
857
                cartMessages.append({"type":"failed", "messageText":"Qty for few items have changed"})
-
 
858
                cartItemMessages.append({"type":"failed", "messageText":"Only " + str(item_shipping_info.quantity) + " available"})
-
 
859
            
-
 
860
            cartItem['maxQuantity'] = min(item_shipping_info.quantity, 20)
-
 
861
            
-
 
862
            if item_id in dealItems:
-
 
863
                line.actual_price = deals[item_id].dealPrice
-
 
864
                if deals[item_id].dealTextOption==0:
-
 
865
                    line.dealText = ''
-
 
866
                if deals[item_id].dealTextOption==2:
-
 
867
                    line.dealText = deals[item_id].dealText
-
 
868
                    
-
 
869
                if deals[item_id].dealFreebieOption==0:
-
 
870
                    line.freebieId = 0
-
 
871
                if deals[item_id].dealFreebieOption==2:
-
 
872
                    line.freebieId =  deals[item_id].dealFreebieItemId
-
 
873
                cartItem['dealText'] = line.dealText
-
 
874
            else:
-
 
875
                line.actual_price = item.sellingPrice
-
 
876
                cartItem['dealText'] = item.dealText 
-
 
877
                line.dealText = None
-
 
878
                line.freebieId = None
-
 
879
            cartItem['sellingPrice'] = line.actual_price
-
 
880
            cartItem['quantity'] = line.quantity
-
 
881
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
-
 
882
            try:
-
 
883
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
-
 
884
            except LogisticsServiceException:
-
 
885
                item_delivery_estimate = -1
-
 
886
                #TODO Use the exception clause to set the retval appropriately
-
 
887
            except :
-
 
888
                item_delivery_estimate = -1
-
 
889
                
-
 
890
            if item_delivery_estimate !=-1:
-
 
891
                inv_client = InventoryClient().get_client()
-
 
892
                itemAvailability = None
-
 
893
                try:
-
 
894
                    itemAvailability = inv_client.getItemAvailabilityAtLocation(item_id, 1)
-
 
895
                except:
-
 
896
                    pass
-
 
897
                
-
 
898
                print 'itemAvailability billling Warehouse ', itemAvailability[2]
-
 
899
                if itemAvailability is not None:
-
 
900
                    billingWarehouse = None
-
 
901
                    try:
-
 
902
                        billingWarehouse = inv_client.getWarehouse(itemAvailability[2])
-
 
903
                    except:
-
 
904
                        pass
-
 
905
                    
-
 
906
                    print 'billingWarehouse Id Location ', billingWarehouse.stateId
-
 
907
                    if billingWarehouse is not None:
-
 
908
                        estimateVal = None
-
 
909
                        if not logistics_client.isAlive() :
-
 
910
                            logistics_client = LogisticsClient().get_client()
-
 
911
                        try:
-
 
912
                            estimateVal = logistics_client.getFirstDeliveryEstimateForWhLocation(customer_pincode, billingWarehouse.stateId)
-
 
913
                            if estimateVal ==-1:
-
 
914
                                item_delivery_estimate =-1
-
 
915
                        except:
-
 
916
                            pass
-
 
917
                        print 'estimateVal Value ', estimateVal
-
 
918
            cartItem['estimate'] = item_delivery_estimate
-
 
919
            if item_delivery_estimate == -1:
-
 
920
                cartItem['quantity'] = 0
-
 
921
                cartMessages.append({"type":"failed", "messageText":"Few items are undeliverable to your pincode"})
-
 
922
                cartItemMessages.append({"type":"failed", "messageText":"Undeliverable"})
-
 
923
            if old_estimate != item_delivery_estimate:
-
 
924
                line.estimate = item_delivery_estimate
-
 
925
                cart.updated_on = current_time
-
 
926
        else:
-
 
927
            cartItem['quantity'] = 0
-
 
928
            cartMessages.append({"type":"failed", "messageText":"Few items are out of Stock"})
-
 
929
            cartItemMessages.append({"type":"failed", "messageText":"Out of Stock"})
-
 
930
            Discount.query.filter(Discount.line==line).delete()
-
 
931
            line.delete()
-
 
932
        totalQty += cartItem['quantity']
-
 
933
        totalAmount += line.actual_price * cartItem['quantity']
-
 
934
        cartItems.append(cartItem)
-
 
935
    if cart.checked_out_on is not None:
-
 
936
        if cart.updated_on > cart.checked_out_on:
-
 
937
            cart.checked_out_on = None
-
 
938
    session.commit()
-
 
939
    
-
 
940
    if cart.coupon_code is not None:
-
 
941
        try:
-
 
942
            updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
-
 
943
            if updated_cart.message is not None:
-
 
944
                emival = updated_cart.message
-
 
945
        except PromotionException as ex:
-
 
946
            remove_coupon(cart.id)
-
 
947
            #retval = ex.message
-
 
948
    session.commit()
-
 
949
    
-
 
950
    cart = Cart.get_by(id=cartId)
-
 
951
    cart_lines = cart.lines
-
 
952
    insurerFlag = False
-
 
953
    for line in cart_lines:
-
 
954
        if line.insurer > 0 or line.dataProtectionInsurer > 0:
-
 
955
            line.insurer = 0
-
 
956
            line.insuranceAmount = 0 
-
 
957
            line.dataProtectionInsurer = 0
-
 
958
            line.dataProtectionAmount = 0
-
 
959
            insurerFlag = True
-
 
960
        
-
 
961
    if insurerFlag:
-
 
962
        cart.updated_on = datetime.datetime.now()
-
 
963
        session.commit()
-
 
964
    session.close()
-
 
965
    responseMap['totalQty']= totalQty
-
 
966
    responseMap['totalAmount']= totalAmount
-
 
967
    if totalAmount < 500:
-
 
968
        shippingCharges = 100
-
 
969
    elif totalAmount < 1000:
-
 
970
        shippingCharges = 50
-
 
971
    responseMap['shippingCharge']
-
 
972
    return json.dumps(responseMap)
-
 
973
    
-
 
974
    
771
def validate_cart_plus(cart_id, source_id, couponCode):
975
def validate_cart_plus(cart_id, source_id, couponCode):
772
    try:
976
    try:
773
        cart_messages = validate_cart(cart_id, source_id, couponCode)
977
        cart_messages = validate_cart(cart_id, source_id, couponCode)
774
        found_cart = Cart.get_by(id=cart_id)
978
        found_cart = Cart.get_by(id=cart_id)
775
        pincode = "110001"
979
        pincode = "110001"
Line 798... Line 1002...
798
        close_session()
1002
        close_session()
799
 
1003
 
800
def close_session():
1004
def close_session():
801
    if session.is_active:
1005
    if session.is_active:
802
        print "session is active. closing it."
1006
        print "session is active. closing it."
803
        session.close()
-
 
804
1007
        session.close()
-
 
1008
        
-
 
1009
805
1010