Subversion Repositories SmartDukaan

Rev

Rev 4090 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
'''
One-time script to update the data of RTO orders since the time
BlueDart changes their report format.

Created on 16-Nov-2011

@author: Chandranshu
'''

import optparse
import xlrd
import time
import datetime

if __name__ == '__main__' and __package__ is None:
    import os
    import sys
    sys.path.insert(0, os.getcwd())

from shop2020.clients.TransactionClient import TransactionClient
from shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceException

REASON = 'reason'
DATE = 'date'
RECEIVER = 'receiver'

def load_rto_data(filename):
    print "Reading RTO details from:" + filename
    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(0)
    num_rows = sheet.nrows
    orders = {}
    returned_orders = {}
    orderIdString = ''
    for rownum in range(1, num_rows):
        orderId, awb, reason, unused_return_awb, date, unused_receiver = sheet.row_values(rownum)[0:7]
        orderIdString = orderIdString + str(int(orderId)) + ','
        orders[int(orderId)] = reason
        delivery_date = str(get_py_datetime(date, None))
        returned_orders[str(int(awb))] = delivery_date + "|" + reason
    
    print orderIdString
    return orders, returned_orders

def mark_orders_as_sales_returned(returned_orders):
    txnClient = TransactionClient().get_client()
    try:
        txnClient.markAsRTOrders(1, returned_orders)
    except TransactionServiceException as tex:
        print tex.message        

def receive_returns(orders):
    txnClient = TransactionClient().get_client()
    for orderId, unused_reason in orders.iteritems():
        try:
            txnClient.receiveReturn(orderId)
        except TransactionServiceException as tex:
            print tex.message

def refund_orders(orders):
    txnClient = TransactionClient().get_client()
    for orderId, reason in orders.iteritems():
        try:
            txnClient.refundOrder(orderId, 'ys-hadmin', reason);
        except TransactionServiceException as tex:
            print tex.message

def get_py_datetime(date, timeval):
    # This should be a command line argument.
    # Refer http://docs.python.org/library/time.html#time.strftime to
    # get a complete list of format specifiers available for date time.
    time_format = "%d-%b-%y %H%M"
    if timeval is None or timeval == '--':
        timeval='0000'
    time_string = date + " " + timeval
    mytime = time.strptime(time_string, time_format)
    return datetime.datetime(*mytime[:6])
        

def main():
    parser = optparse.OptionParser()
    parser.set_usage("%prog [options] filename")
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("You've not supplied required arguments. Please see the usage.")
    filename = args[0]
    orders, returned_orders = load_rto_data(filename)
    mark_orders_as_sales_returned(returned_orders)
    receive_returns(orders)
    refund_orders(orders)

if __name__ == '__main__':
    main()