Subversion Repositories SmartDukaan

Rev

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

Rev 3858 Rev 3956
Line 36... Line 36...
36
import datetime
36
import datetime
37
import logging
37
import logging
38
import os
38
import os
39
import sys
39
import sys
40
import time
40
import time
-
 
41
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
41
logging.basicConfig(level=logging.DEBUG)
42
logging.basicConfig(level=logging.DEBUG)
42
 
43
 
43
 
44
 
44
 
45
 
45
 
46
 
Line 1325... Line 1326...
1325
        order.delay_reason = DelayReason._VALUES_TO_NAMES[reason]
1326
        order.delay_reason = DelayReason._VALUES_TO_NAMES[reason]
1326
        session.commit()
1327
        session.commit()
1327
        return True
1328
        return True
1328
    except:
1329
    except:
1329
        return False
1330
        return False
-
 
1331
 
-
 
1332
def reconcile_cod_collection(collected_amount_map, xferBy, xferTxnId, xferDate):
-
 
1333
    unprocessed_awbs = {}
-
 
1334
    for awb, amount in collected_amount_map.iteritems():
-
 
1335
        try:
-
 
1336
            order = Order.query.filter_by(airwaybill_no=awb).one()
-
 
1337
        except NoResultFound:
-
 
1338
            unprocessed_awbs[awb] = "No order was found for the given AWB: " + awb
-
 
1339
            continue
-
 
1340
        except MultipleResultsFound:
-
 
1341
            unprocessed_awbs[awb] = "Multiple orders were found for the given AWB:" + awb
-
 
1342
            continue
-
 
1343
        
-
 
1344
        if order.cod_reconciliation_timestamp:
-
 
1345
            #This order has been processed already! This may be a re-run. Let's allow other orders to be processed.
-
 
1346
            continue
-
 
1347
        
-
 
1348
        if order.status < OrderStatus.DELIVERY_SUCCESS:
-
 
1349
            #Order has not been delivered yet. No way we can receive the payment.
-
 
1350
            unprocessed_awbs[awb] = "Payment has been received for order: " + str(order.id) + " before delivery reconciliation"
-
 
1351
            continue
-
 
1352
        
-
 
1353
        if amount != order.total_amount:
-
 
1354
            #Received amount is less than or more than the order amount. Mustn't let the user proceed.
-
 
1355
            unprocessed_awbs[awb] = "Payment of Rs. " + str(amount) + " has been received against the total value of Rs. " + str(order.total_amount) + " for order: " + str(order.id)
-
 
1356
            continue
-
 
1357
        
-
 
1358
        try:
-
 
1359
            payment_client = PaymentClient().get_client()
-
 
1360
            payment_client.partiallyCapturePayment(order.transaction.id, amount, xferBy, xferTxnId, xferDate)
-
 
1361
        except Exception:
-
 
1362
            unprocessed_awbs[awb] = "We were unable to partially capture payment for order id " + str(order.id) + ", AWB: " + awb
-
 
1363
            continue
-
 
1364
        
-
 
1365
        #Payment has been recorded now. We can update the order peacefully.
-
 
1366
        order.cod_reconciliation_timestamp = datetime.datetime.now()
-
 
1367
        session.commit()
-
 
1368
    
-
 
1369
    return unprocessed_awbs
1330
    
1370
    
1331
def close_session():
1371
def close_session():
1332
    if session.is_active:
1372
    if session.is_active:
1333
        print "session is active. closing it."
1373
        print "session is active. closing it."
1334
        session.close()
1374
        session.close()