Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4069 chandransh 1
#!/usr/bin/python
2
'''
4090 chandransh 3
One-time script to update the data of RTO orders since the time
4
BlueDart changes their report format.
5
 
4069 chandransh 6
Created on 16-Nov-2011
7
 
8
@author: Chandranshu
9
'''
10
 
11
import optparse
12
import xlrd
13
import time
14
import datetime
15
 
16
if __name__ == '__main__' and __package__ is None:
17
    import os
18
    import sys
19
    sys.path.insert(0, os.getcwd())
20
 
21
from shop2020.clients.TransactionClient import TransactionClient
22
from shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceException
23
 
24
REASON = 'reason'
25
DATE = 'date'
26
RECEIVER = 'receiver'
27
 
28
def load_rto_data(filename):
29
    print "Reading RTO details from:" + filename
30
    workbook = xlrd.open_workbook(filename)
31
    sheet = workbook.sheet_by_index(0)
32
    num_rows = sheet.nrows
33
    orders = {}
34
    returned_orders = {}
35
    orderIdString = ''
36
    for rownum in range(1, num_rows):
37
        orderId, awb, reason, unused_return_awb, date, unused_receiver = sheet.row_values(rownum)[0:7]
38
        orderIdString = orderIdString + str(int(orderId)) + ','
39
        orders[int(orderId)] = reason
40
        delivery_date = str(get_py_datetime(date, None))
41
        returned_orders[str(int(awb))] = delivery_date + "|" + reason
42
 
43
    print orderIdString
44
    return orders, returned_orders
45
 
46
def mark_orders_as_sales_returned(returned_orders):
47
    txnClient = TransactionClient().get_client()
48
    try:
4910 phani.kuma 49
        txnClient.markAsRTOrders(1, returned_orders)
4069 chandransh 50
    except TransactionServiceException as tex:
51
        print tex.message        
52
 
53
def receive_returns(orders):
54
    txnClient = TransactionClient().get_client()
55
    for orderId, unused_reason in orders.iteritems():
56
        try:
57
            txnClient.receiveReturn(orderId)
58
        except TransactionServiceException as tex:
59
            print tex.message
60
 
61
def refund_orders(orders):
62
    txnClient = TransactionClient().get_client()
63
    for orderId, reason in orders.iteritems():
64
        try:
65
            txnClient.refundOrder(orderId, 'ys-hadmin', reason);
66
        except TransactionServiceException as tex:
67
            print tex.message
68
 
69
def get_py_datetime(date, timeval):
70
    # This should be a command line argument.
71
    # Refer http://docs.python.org/library/time.html#time.strftime to
72
    # get a complete list of format specifiers available for date time.
73
    time_format = "%d-%b-%y %H%M"
74
    if timeval is None or timeval == '--':
75
        timeval='0000'
76
    time_string = date + " " + timeval
77
    mytime = time.strptime(time_string, time_format)
78
    return datetime.datetime(*mytime[:6])
79
 
80
 
81
def main():
82
    parser = optparse.OptionParser()
83
    parser.set_usage("%prog [options] filename")
84
    (options, args) = parser.parse_args()
85
    if len(args) != 1:
86
        parser.error("You've not supplied required arguments. Please see the usage.")
87
    filename = args[0]
88
    orders, returned_orders = load_rto_data(filename)
89
    mark_orders_as_sales_returned(returned_orders)
90
    receive_returns(orders)
91
    refund_orders(orders)
92
 
93
if __name__ == '__main__':
94
    main()