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 timeBlueDart changes their report format.Created on 16-Nov-2011@author: Chandranshu'''import optparseimport xlrdimport timeimport datetimeif __name__ == '__main__' and __package__ is None:import osimport syssys.path.insert(0, os.getcwd())from shop2020.clients.TransactionClient import TransactionClientfrom shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceExceptionREASON = 'reason'DATE = 'date'RECEIVER = 'receiver'def load_rto_data(filename):print "Reading RTO details from:" + filenameworkbook = xlrd.open_workbook(filename)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowsorders = {}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)] = reasondelivery_date = str(get_py_datetime(date, None))returned_orders[str(int(awb))] = delivery_date + "|" + reasonprint orderIdStringreturn orders, returned_ordersdef mark_orders_as_sales_returned(returned_orders):txnClient = TransactionClient().get_client()try:txnClient.markAsRTOrders(1, returned_orders)except TransactionServiceException as tex:print tex.messagedef receive_returns(orders):txnClient = TransactionClient().get_client()for orderId, unused_reason in orders.iteritems():try:txnClient.receiveReturn(orderId)except TransactionServiceException as tex:print tex.messagedef 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.messagedef 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 + " " + timevalmytime = 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()