Subversion Repositories SmartDukaan

Rev

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

Rev 13584 Rev 13609
Line 4020... Line 4020...
4020
 
4020
 
4021
def reconcile_cod_collection(collected_amount_map, xferBy, xferTxnId, xferDate):
4021
def reconcile_cod_collection(collected_amount_map, xferBy, xferTxnId, xferDate):
4022
    unprocessed_awbs = {}
4022
    unprocessed_awbs = {}
4023
    for awb, amount in collected_amount_map.iteritems():
4023
    for awb, amount in collected_amount_map.iteritems():
4024
        try:
4024
        try:
4025
            order = Order.query.filter_by(airwaybill_no=awb).one()
4025
            orders = Order.query.filter_by(airwaybill_no=awb).all()
4026
        except NoResultFound:
4026
        except NoResultFound:
4027
            unprocessed_awbs[awb] = "No order was found for the given AWB: " + awb
4027
            unprocessed_awbs[awb] = "No order was found for the given AWB: " + awb
4028
            continue
4028
            continue
-
 
4029
        '''
4029
        except MultipleResultsFound:
4030
        except MultipleResultsFound:
4030
            unprocessed_awbs[awb] = "Multiple orders were found for the given AWB:" + awb
4031
            unprocessed_awbs[awb] = "Multiple orders were found for the given AWB:" + awb
4031
            continue
4032
            continue
-
 
4033
        '''
-
 
4034
        if len(orders) ==1:
-
 
4035
            order = orders[0]
4032
        
4036
        
4033
        if order.cod_reconciliation_timestamp:
4037
            if order.cod_reconciliation_timestamp:
4034
            #This order has been processed already! This may be a re-run. Let's allow other orders to be processed.
4038
                #This order has been processed already! This may be a re-run. Let's allow other orders to be processed.
4035
            continue
4039
                continue
4036
        
4040
            
4037
        if order.status < OrderStatus.DELIVERY_SUCCESS:
4041
            if order.status < OrderStatus.DELIVERY_SUCCESS:
4038
            #Order has not been delivered yet. No way we can receive the payment.
4042
                #Order has not been delivered yet. No way we can receive the payment.
4039
            unprocessed_awbs[awb] = "Payment has been received for order: " + str(order.id) + " before delivery reconciliation"
4043
                unprocessed_awbs[awb] = "Payment has been received for order: " + str(order.id) + " before delivery reconciliation"
4040
            continue
4044
                continue
4041
        
4045
            
4042
        ##As per ticket #743 if amount difference less than 0.5 we should reconcile
4046
            ##As per ticket #743 if amount difference less than 0.5 we should reconcile
4043
        if abs(amount - (order.total_amount - order.gvAmount)) > 0.5:
4047
            if abs(amount - (order.total_amount - order.gvAmount)) > 0.5:
4044
            #Received amount is less than or more than the order amount. Mustn't let the user proceed.
4048
                #Received amount is less than or more than the order amount. Mustn't let the user proceed.
4045
            unprocessed_awbs[awb] = "Payment of Rs. " + str(amount) + " has been received against the total value of Rs. " + str(order.total_amount-order.gvAmount) + " for order: " + str(order.id)
4049
                unprocessed_awbs[awb] = "Payment of Rs. " + str(amount) + " has been received against the total value of Rs. " + str(order.total_amount-order.gvAmount) + " for order: " + str(order.id)
4046
            continue
4050
                continue
4047
        
4051
            
4048
        try:
4052
            try:
4049
            payment_client = PaymentClient().get_client()
4053
                payment_client = PaymentClient().get_client()
4050
            payment_client.partiallyCapturePayment(order.transaction.id, amount, xferBy, xferTxnId, xferDate)
4054
                payment_client.partiallyCapturePayment(order.transaction.id, amount, xferBy, xferTxnId, xferDate)
4051
        except Exception:
4055
            except Exception:
4052
            unprocessed_awbs[awb] = "We were unable to partially capture payment for order id " + str(order.id) + ", AWB: " + awb
4056
                unprocessed_awbs[awb] = "We were unable to partially capture payment for order id " + str(order.id) + ", AWB: " + awb
4053
            continue
4057
                continue
-
 
4058
            
-
 
4059
            #Payment has been recorded now. We can update the order peacefully.
-
 
4060
            order.cod_reconciliation_timestamp = datetime.datetime.now()
-
 
4061
            session.commit()
4054
        
4062
        
-
 
4063
        if len(orders) > 1:
-
 
4064
            totalOrdersAmount = 0
-
 
4065
            logisticsProviderId = orders[0].logistics_provider_id
-
 
4066
            logistics_client = LogisticsClient().get_client()
-
 
4067
            provider = logistics_client.getProvider(logisticsProviderId)
-
 
4068
            
-
 
4069
            if provider.groupShipmentAllowed:
-
 
4070
                for order in orders:
-
 
4071
                    totalOrdersAmount = totalOrdersAmount + (order.total_amount-order.gvAmount)
-
 
4072
                
-
 
4073
                if abs(amount - (totalOrdersAmount)) > 0.5:
-
 
4074
                    unprocessed_awbs[awb] = "Payment of Rs. " + str(amount) + " has been received against the total value of Rs. " + str(totalOrdersAmount) + " for Master Order Id: " + orders[0].logisticsTransactionId
-
 
4075
                    continue
-
 
4076
                try:
4055
        #Payment has been recorded now. We can update the order peacefully.
4077
                    payment_client = PaymentClient().get_client()
-
 
4078
                    payment_client.partiallyCapturePayment(orders[0].transaction.id, amount, xferBy, xferTxnId, xferDate)
-
 
4079
                except Exception:
-
 
4080
                    unprocessed_awbs[awb] = "We were unable to partially capture payment for master order id " + orders[0].logisticsTransactionId + ", AWB: " + awb
-
 
4081
                    continue
-
 
4082
                
-
 
4083
                for order in orders:
4056
        order.cod_reconciliation_timestamp = datetime.datetime.now()
4084
                    order.cod_reconciliation_timestamp = datetime.datetime.now()
4057
        session.commit()
4085
                session.commit()
-
 
4086
            else:
-
 
4087
                unprocessed_awbs[awb] = "Multiple orders were found for the given AWB:" + awb
-
 
4088
                continue           
4058
    
4089
    
4059
    return unprocessed_awbs
4090
    return unprocessed_awbs
4060
 
4091
 
4061
def get_transactions_requiring_extra_processing(category):
4092
def get_transactions_requiring_extra_processing(category):
4062
    """
4093
    """