Subversion Repositories SmartDukaan

Rev

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

Rev 22638 Rev 22697
Line 662... Line 662...
662
                                
662
                                
663
                if order.pickupStoreId:
663
                if order.pickupStoreId:
664
                    order.otg = False
664
                    order.otg = False
665
                    
665
                    
666
                if order.productCondition != ProductCondition.BAD:
666
                if order.productCondition != ProductCondition.BAD:
667
                    inventory_client.reserveItemInWarehouse(item_id, fulfilmentWarehouseId, sourceId, order.id, to_java_date(order.created_timestamp), to_java_date(order.promised_shipping_time), order.lineitems[0].quantity)
667
                    inventory_client.reserveItemInWarehouse(item_id, order.fulfilmentWarehouseId, sourceId, order.id, to_java_date(order.created_timestamp), to_java_date(order.promised_shipping_time), order.lineitems[0].quantity)
668
        
668
        
669
            try:
669
            try:
670
                item_pricing = inventory_client.getItemPricing(item_id, -1)
670
                item_pricing = inventory_client.getItemPricing(item_id, -1)
671
                order.lineitems[0].transfer_price = item_pricing.transferPrice
671
                order.lineitems[0].transfer_price = item_pricing.transferPrice
672
                order.lineitems[0].nlc = item_pricing.nlc
672
                order.lineitems[0].nlc = item_pricing.nlc
Line 2018... Line 2018...
2018
        o.refunded_by= None 
2018
        o.refunded_by= None 
2019
        o.refund_reason= None
2019
        o.refund_reason= None
2020
    session.commit()
2020
    session.commit()
2021
    return True
2021
    return True
2022
    
2022
    
2023
    
-
 
2024
def accept_order(orderId):
-
 
2025
    logging.info("Accepting order no: " + str(orderId))
-
 
2026
    order = get_order(orderId)
-
 
2027
    
-
 
2028
    ordersList = []
-
 
2029
    ordersList.append(order)
-
 
2030
    updatedOrders = logisticsProviderFinalCheck(ordersList)
-
 
2031
    order = updatedOrders[0]
-
 
2032
    logistics_client = LogisticsClient().get_client()
-
 
2033
    provider = logistics_client.getProvider(order.logistics_provider_id)
-
 
2034
    providerLimits = logistics_client.getProviderLimitDetailsForPincode(provider.id, order.customer_pincode)
-
 
2035
    
-
 
2036
    if order.total_weight is None or order.total_weight == 0:
-
 
2037
        raise TransactionServiceException(210, "Weight not defined for this order:- "+str(order.id)+". Contact Category Team")
-
 
2038
    
-
 
2039
    if order.total_weight > provider.bundleWeightLimit:
-
 
2040
        raise TransactionServiceException(210, "Logistics Partner "+provider.name+ " Group Shipment Weight Limit Violated i.e. "+str(provider.bundleWeightLimit)+" Suggestion: Split Order!!!")
-
 
2041
    
-
 
2042
    if order.cod:
-
 
2043
        if order.total_amount > provider.maxCodLimit:
-
 
2044
            raise TransactionServiceException(210, "Logistics Partner "+provider.name+ " Max Cod Amount Collection Limit Violated i.e. "+ str(provider.maxCodLimit)+" Suggestion: Split Order!!!")
-
 
2045
    #SelfPickUp and Runner 
-
 
2046
    if order.logistics_provider_id not in [4,5]:
-
 
2047
    
-
 
2048
        providerCodLimit = float(providerLimits.get("providerCodLimit"))
-
 
2049
        providerPrepaidLimit = float(providerLimits.get("providerPrepaidLimit"))
-
 
2050
        
-
 
2051
        if order.cod:
-
 
2052
            if order.total_amount > providerCodLimit:
-
 
2053
                raise TransactionServiceException(212, "Provider Maximum COD Limit Violated for Customer Pincode i.e. "+str(providerCodLimit)+" Suggestion: Split Order!!!")
-
 
2054
        
-
 
2055
        if not order.cod:
-
 
2056
            if order.total_amount > providerPrepaidLimit:
-
 
2057
                raise TransactionServiceException(212, "Logistics Partner "+provider.name+ " Max Prepaid Amount Limit Violated Rs." +str(providerPrepaidLimit)+" Suggestion: Split Order!!!")
-
 
2058
 
-
 
2059
    if order.status in [OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW, OrderStatus.LOW_INV_PO_RAISED, OrderStatus.LOW_INV_REVERSAL_IN_PROCESS, OrderStatus.LOW_INV_NOT_AVAILABLE_AT_HOTSPOT, OrderStatus.CAPTURE_IN_PROCESS]:
-
 
2060
        if not order.cod:
-
 
2061
            if order.transaction.status == TransactionStatus.AUTHORIZED and order.transaction.payment_option!=capitalFloatPayMethod:
-
 
2062
                __capture_txn(order.transaction_id)
-
 
2063
                change_transaction_status(order.transaction_id, TransactionStatus.IN_PROCESS, "Payment received", PickUpType.COURIER, order.orderType, order.source)
-
 
2064
        order.status = OrderStatus.ACCEPTED
-
 
2065
        order.statusDescription = "Order Accepted"
-
 
2066
        order.accepted_timestamp = datetime.datetime.now()
-
 
2067
        txnSeqRequired = 0
-
 
2068
        txnShipSeq = TransactionShipmentSequence.query.filter(TransactionShipmentSequence.transactionId==order.transaction_id).order_by(desc(TransactionShipmentSequence.id)).first()
-
 
2069
        if txnShipSeq is None:
-
 
2070
            txnShipSeq = TransactionShipmentSequence()
-
 
2071
            txnShipSeq.transactionId = order.transaction_id
-
 
2072
            txnShipSeq.createdTimestamp = datetime.datetime.now()
-
 
2073
            txnShipSeq.sequence = 1
-
 
2074
            txnSeqRequired = 1
-
 
2075
        else:
-
 
2076
            txnShipSeqNew = TransactionShipmentSequence()
-
 
2077
            txnShipSeqNew.transactionId = txnShipSeq.transactionId
-
 
2078
            txnShipSeqNew.createdTimestamp = datetime.datetime.now()
-
 
2079
            txnShipSeqNew.sequence = txnShipSeq.sequence + 1
-
 
2080
            txnSeqRequired = txnShipSeq.sequence + 1
-
 
2081
        order.logisticsTransactionId = str(order.transaction_id)+"-"+str(txnSeqRequired)
-
 
2082
        if order.source == 6 or order.source == 7 or order.source == 8:
-
 
2083
            if order.source == 8 and order.cod:
-
 
2084
                order.cod = False                
-
 
2085
            session.commit()
-
 
2086
            return "True"
-
 
2087
        __amend_fulfilment_warehouse(order)
-
 
2088
        __update_transfer_price(order, order.fulfilmentWarehouseId)
-
 
2089
        
-
 
2090
        if order.orderType == OrderType.B2B:
-
 
2091
            user_client = UserClient().get_client()
-
 
2092
            if user_client.isPrivateDealUser(order.customer_id):
-
 
2093
                billing_address = user_client.getBillingAddressForUser(order.customer_id)
-
 
2094
                if billing_address is not None:
-
 
2095
                    if billing_address.state.strip()!=order.customer_state.strip():
-
 
2096
                        order.orderType=OrderType.B2C
-
 
2097
               
-
 
2098
        session.commit()
-
 
2099
            
-
 
2100
        '''
-
 
2101
        if order.logistics_provider_id != 7 and order.airwaybill_no is None or order.airwaybill_no == "null":
-
 
2102
            logistics_client = LogisticsClient().get_client()
-
 
2103
            if order.cod and order.total_amount > 1:
-
 
2104
                airwaybillNo= logistics_client.getEmptyAWB(order.logistics_provider_id, DeliveryType.COD)
-
 
2105
            else:
-
 
2106
                airwaybillNo= logistics_client.getEmptyAWB(order.logistics_provider_id, DeliveryType.PREPAID)
-
 
2107
            
-
 
2108
            order.airwaybill_no = airwaybillNo
-
 
2109
            order.tracking_id = airwaybillNo           
-
 
2110
            session.commit()
-
 
2111
        '''
-
 
2112
            
-
 
2113
        logging.info("Successfully accepted the order no.:" + str(orderId))
-
 
2114
        return True
-
 
2115
    else:
-
 
2116
        logging.warning("Accept called for the unacceptable order: " + str(orderId))
-
 
2117
        return False
-
 
2118
 
-
 
2119
 
2023
 
2120
def __capture_txn(txnId):
2024
def __capture_txn(txnId):
2121
    logging.info("Capturing payment for merchant txn:" + str(txnId))
2025
    logging.info("Capturing payment for merchant txn:" + str(txnId))
2122
    captured_amount = 0
2026
    captured_amount = 0
2123
    try:
2027
    try:
Line 5032... Line 4936...
5032
    order.status = OrderStatus.CANCEL_REQUEST_CONFIRMED
4936
    order.status = OrderStatus.CANCEL_REQUEST_CONFIRMED
5033
    order.statusDescription = "Cancellation request confirmed"
4937
    order.statusDescription = "Cancellation request confirmed"
5034
    session.commit()
4938
    session.commit()
5035
    refund_order(order.id, "crm-team", "As per Customer's Request")
4939
    refund_order(order.id, "crm-team", "As per Customer's Request")
5036
    
4940
    
5037
def accept_orders_for_item_id(itemId, inventory):
-
 
5038
    """
-
 
5039
    Marks the orders as ACCEPTED for the given itemId and inventory. It also updates the accepted timestamp. If the
-
 
5040
    given order is not a COD order, it also captures the payment if the same has not been captured.
-
 
5041
    
-
 
5042
    Parameters:
-
 
5043
     - itemId
-
 
5044
     - inventory
-
 
5045
    """
-
 
5046
    orders = Order.query.filter(Order.warehouse_id == 9).filter(Order.status.in_(tuple([OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW, OrderStatus.LOW_INV_PO_RAISED, OrderStatus.LOW_INV_REVERSAL_IN_PROCESS]))).order_by(Order.expected_shipping_time).all()
-
 
5047
    for order in orders:
-
 
5048
        if order.source ==6:
-
 
5049
            continue
-
 
5050
        if inventory > 0:
-
 
5051
            lineitem = order.lineitems[0]
-
 
5052
            if itemId == lineitem.item_id:
-
 
5053
                try:
-
 
5054
                    accept_order(order.id)
-
 
5055
                    inventory = inventory - lineitem.quantity
-
 
5056
                except:
-
 
5057
                    logging.info("Unable to accept the order")
-
 
5058
        else:
-
 
5059
            order_outofstock(order.id)
-
 
5060
    return True
-
 
5061
 
-
 
5062
def move_orders_to_correct_warehouse():
4941
def move_orders_to_correct_warehouse():
5063
    completedOrders = []
4942
    completedOrders = []
5064
    pendingOrders = ""
4943
    pendingOrders = ""
5065
    orders = Order.query.filter(Order.status.in_(tuple([OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW, OrderStatus.LOW_INV_PO_RAISED, OrderStatus.LOW_INV_REVERSAL_IN_PROCESS]))).order_by(Order.promised_shipping_time).all()
4944
    orders = Order.query.filter(Order.status.in_(tuple([OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW, OrderStatus.LOW_INV_PO_RAISED, OrderStatus.LOW_INV_REVERSAL_IN_PROCESS]))).order_by(Order.promised_shipping_time).all()
5066
    logistics_client = LogisticsClient().get_client()
4945
    logistics_client = LogisticsClient().get_client()
Line 8882... Line 8761...
8882
    except:
8761
    except:
8883
        conn.close()
8762
        conn.close()
8884
        
8763
        
8885
    conn.close()
8764
    conn.close()
8886
    
8765
    
8887
    print 'Process completed', updatedOrders[0].logistics_provider_id
-
 
8888
    awbNumber = None
-
 
8889
    if updatedOrders[0].logistics_provider_id not in [1,2,7]:
-
 
8890
        deltype = DeliveryType.PREPAID
-
 
8891
        if updatedOrders[0].cod:
-
 
8892
            deltype = DeliveryType.COD
-
 
8893
        if not logistics_client.isAlive():
-
 
8894
            logistics_client = LogisticsClient().get_client()
-
 
8895
        awbNumber = logistics_client.getNewEmptyAwb(provider.id, deltype, totalOrderQuantity)
-
 
8896
    
-
 
8897
    txnSeqRequired = 0
8766
    txnSeqRequired = 0
8898
    txnShipSeq = TransactionShipmentSequence.query.filter(TransactionShipmentSequence.transactionId==updatedOrders[0].transaction_id).order_by(desc(TransactionShipmentSequence.id)).first()
8767
    txnShipSeq = TransactionShipmentSequence.query.filter(TransactionShipmentSequence.transactionId==updatedOrders[0].transaction_id).order_by(desc(TransactionShipmentSequence.id)).first()
8899
    if txnShipSeq is None:
8768
    if txnShipSeq is None:
8900
        txnShipSeq = TransactionShipmentSequence()
8769
        txnShipSeq = TransactionShipmentSequence()
8901
        txnShipSeq.transactionId = updatedOrders[0].transaction_id
8770
        txnShipSeq.transactionId = updatedOrders[0].transaction_id
Line 8911... Line 8780...
8911
    
8780
    
8912
    for order in updatedOrders:
8781
    for order in updatedOrders:
8913
        order.logisticsTransactionId = str(order.transaction_id)+"-"+str(txnSeqRequired)
8782
        order.logisticsTransactionId = str(order.transaction_id)+"-"+str(txnSeqRequired)
8914
                
8783
                
8915
    session.commit()
8784
    session.commit()
8916
    
-
 
8917
    if awbNumber is not None:
-
 
8918
        update_master_order_awb(str(updatedOrders[0].transaction_id)+"-"+str(txnSeqRequired), awbNumber)
-
 
8919
    
-
 
8920
    singleOrder = updatedOrders[0]
-
 
8921
    if singleOrder.orderType == OrderType.B2B:
-
 
8922
        user_client = UserClient().get_client()
-
 
8923
        if user_client.isPrivateDealUser(singleOrder.customer_id):
-
 
8924
            billing_address = user_client.getBillingAddressForUser(singleOrder.customer_id)
-
 
8925
            if billing_address is not None:
-
 
8926
                if billing_address.state.strip()!=singleOrder.customer_state.strip():
-
 
8927
                    totalOrders = get_orders_for_transaction(singleOrder.transaction.id, singleOrder.customer_id)
-
 
8928
                    for order in totalOrders:
-
 
8929
                        order.orderType=OrderType.B2C
-
 
8930
    session.commit()   
-
 
8931
         
-
 
8932
    return True
8785
    return True
8933
 
8786
 
8934
def get_group_orders_by_logistics_txn_id(logisticsTxnId):
8787
def get_group_orders_by_logistics_txn_id(logisticsTxnId):
8935
    orders = Order.query.filter(Order.logisticsTransactionId==logisticsTxnId).all()
8788
    orders = Order.query.filter(Order.logisticsTransactionId==logisticsTxnId).all()
8936
    if not orders:
8789
    if not orders:
Line 10315... Line 10168...
10315
    shipmentWeight = 0
10168
    shipmentWeight = 0
10316
    isCod = False
10169
    isCod = False
10317
    if ordersList[0].cod:
10170
    if ordersList[0].cod:
10318
        isCod = True
10171
        isCod = True
10319
        
10172
        
10320
    if ordersList[0].logistics_provider_id == 4:
10173
    if ordersList[0].logistics_provider_in [4, 5]:
10321
        return ordersList
10174
        return ordersList
10322
    
10175
    
10323
    for order in ordersList:
10176
    for order in ordersList:
10324
        shipmentAmount = shipmentAmount + order.total_amount
10177
        shipmentAmount = shipmentAmount + order.total_amount
10325
        shipmentWeight = shipmentWeight + order.total_weight
10178
        shipmentWeight = shipmentWeight + order.total_weight
Line 11362... Line 11215...
11362
    logisticCostDetail= ShipmentLogisticsCostDetail.get_by(logisticsTransactionId=logisticsTxnId)
11215
    logisticCostDetail= ShipmentLogisticsCostDetail.get_by(logisticsTransactionId=logisticsTxnId)
11363
    if logisticCostDetail is None:
11216
    if logisticCostDetail is None:
11364
        raise
11217
        raise
11365
    return logisticCostDetail
11218
    return logisticCostDetail
11366
 
11219
 
-
 
11220
##Shipment Logistics cost should be added only during billing
11367
def add_shipment_logistic_detail(shipmentLogisticsCostDetail):
11221
def add_shipment_logistic_detail(shipmentLogisticsCostDetail):
11368
    if shipmentLogisticsCostDetail.logisticsTransactionId is None or shipmentLogisticsCostDetail.packageDimensions is None:
11222
    if shipmentLogisticsCostDetail.logisticsTransactionId is None or shipmentLogisticsCostDetail.packageDimensions is None:
11369
        raise ValueError("Null fields logisticsTransactionId/packageDimensions")
11223
        raise ValueError("Null fields logisticsTransactionId/packageDimensions")
11370
    
11224
    
11371
    logisticCostDetail= ShipmentLogisticsCostDetail.get_by(logisticsTransactionId=shipmentLogisticsCostDetail.logisticsTransactionId)
11225
    logisticCostDetail= ShipmentLogisticsCostDetail.get_by(logisticsTransactionId=shipmentLogisticsCostDetail.logisticsTransactionId)