| 1132 |
chandransh |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
import datetime
|
|
|
4 |
import optparse
|
|
|
5 |
import sys
|
|
|
6 |
import csv
|
|
|
7 |
import xlrd
|
|
|
8 |
|
|
|
9 |
if __name__ == '__main__' and __package__ is None:
|
|
|
10 |
import os
|
|
|
11 |
sys.path.insert(0, os.getcwd())
|
|
|
12 |
|
|
|
13 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
|
|
14 |
from shop2020.clients.TransactionClient import TransactionClient
|
|
|
15 |
from shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceException
|
|
|
16 |
from shop2020.utils.EmailAttachmentDownloader import download_attachment
|
|
|
17 |
from shop2020.utils.Utils import to_py_date
|
|
|
18 |
|
|
|
19 |
def process_pickup_records(provider):
|
|
|
20 |
filename = fetch_report('PICKUP REPORT')
|
|
|
21 |
pickup_details = read_pickup_report(filename)
|
|
|
22 |
orders_not_picked_up = update_picked_orders(provider.id, pickup_details)
|
|
|
23 |
if orders_not_picked_up:
|
|
|
24 |
print "Some of our orders were not picked up. Printing report to: PickupMismatch.csv"
|
|
|
25 |
print_discrepancy_report("PickupMismatch.csv", orders_not_picked_up)
|
|
|
26 |
|
|
|
27 |
def process_delivery_report(provider):
|
|
|
28 |
#filename = fetch_report('DELIVERED AND RTO REPORT')
|
|
|
29 |
filename = 'delivery_report.xls'
|
|
|
30 |
delivered_orders, returned_orders = read_delivery_report(filename)
|
|
|
31 |
update_delivered_orders(provider.id, delivered_orders)
|
|
|
32 |
update_returned_orders(returned_orders)
|
|
|
33 |
#TODO: Alert someone about the returned orders
|
|
|
34 |
|
|
|
35 |
def process_non_delivery_report(provider):
|
|
|
36 |
filename = fetch_report('UNDELIVERED REPORT')
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
def get_provider_by_name(provider_name):
|
|
|
40 |
logistics_client = LogisticsClient().get_client()
|
|
|
41 |
#FIXME: Write a thrift call to get a provider by name
|
|
|
42 |
provider = None
|
|
|
43 |
providers = logistics_client.getAllProviders()
|
|
|
44 |
for p in providers:
|
|
|
45 |
if p.name == provider_name:
|
|
|
46 |
provider=p
|
|
|
47 |
break
|
|
|
48 |
if provider == None:
|
|
|
49 |
sys.exit("Can't continue execution: No such provider")
|
|
|
50 |
return provider
|
|
|
51 |
|
|
|
52 |
def fetch_report(type):
|
|
|
53 |
filename = download_attachment(type, todays_date_string())
|
|
|
54 |
if filename is None:
|
|
|
55 |
sys.exit("The " + type + " report is not yet available.")
|
|
|
56 |
return filename
|
|
|
57 |
|
|
|
58 |
def read_pickup_report(filename):
|
|
|
59 |
print "Reading pickup report from:" + filename
|
|
|
60 |
workbook = xlrd.open_workbook(filename)
|
|
|
61 |
sheet = workbook.sheet_by_index(0)
|
|
|
62 |
num_rows = sheet.nrows
|
|
|
63 |
picked_up_orders = {}
|
|
|
64 |
for rownum in range(1, num_rows):
|
|
|
65 |
unused_customer_code, awb, date, time = sheet.row_values(rownum)[0:4]
|
|
|
66 |
picked_up_orders[awb] = 0 #TODO: Use the date and time value in the report
|
|
|
67 |
#print picked_up_orders
|
|
|
68 |
return picked_up_orders
|
|
|
69 |
|
|
|
70 |
def read_delivery_report(filename):
|
|
|
71 |
print "Reading delivery details from:" + filename
|
|
|
72 |
workbook = xlrd.open_workbook(filename)
|
|
|
73 |
sheet = workbook.sheet_by_index(0)
|
|
|
74 |
num_rows = sheet.nrows
|
|
|
75 |
delivered_orders = {}
|
|
|
76 |
returned_orders = {}
|
|
|
77 |
for rownum in range(1, num_rows):
|
|
|
78 |
unused_customer_code, awb, date, time, status, receiver, reason_for_return = sheet.row_values(rownum)[0:7]
|
|
|
79 |
if receiver: #TODO: Use status for this check
|
|
|
80 |
delivered_orders[awb] = str(datetime.datetime.now()) + "|" + receiver #TODO: Use the date and time value in the report
|
|
|
81 |
else:
|
|
|
82 |
returned_orders[awb] = str(datetime.datetime.now()) + reason_for_return
|
|
|
83 |
print "Delivered Orders:"
|
|
|
84 |
print delivered_orders
|
|
|
85 |
|
|
|
86 |
print "Returned Orders:"
|
|
|
87 |
print returned_orders
|
|
|
88 |
return delivered_orders, returned_orders
|
|
|
89 |
|
|
|
90 |
def update_picked_orders(provider_id, pickup_details):
|
|
|
91 |
txnClient = TransactionClient().get_client()
|
|
|
92 |
try:
|
|
|
93 |
orders_not_picked_up = txnClient.markOrdersAsPickedUp(provider_id, pickup_details)
|
|
|
94 |
return orders_not_picked_up
|
|
|
95 |
except TransactionServiceException as tex:
|
|
|
96 |
print tex.message
|
|
|
97 |
|
|
|
98 |
def update_delivered_orders(providerId, delivered_orders):
|
|
|
99 |
txnClient = TransactionClient().get_client()
|
|
|
100 |
try:
|
|
|
101 |
txnClient.markOrdersAsDelivered(providerId, delivered_orders)
|
|
|
102 |
except TransactionServiceException as tex:
|
|
|
103 |
print tex.message
|
|
|
104 |
|
|
|
105 |
def update_returned_orders(returned_orders):
|
|
|
106 |
pass
|
|
|
107 |
|
|
|
108 |
def print_discrepancy_report(filename, orders):
|
|
|
109 |
writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
|
|
|
110 |
writer.writerow(['Order Id', 'AWB No', 'Shipping timestamp'])
|
|
|
111 |
for order in orders:
|
|
|
112 |
writer.writerow([order.id, order.airwaybill_no, to_py_date(order.shipping_timestamp)])
|
|
|
113 |
|
|
|
114 |
def todays_date_string():
|
|
|
115 |
return '"24-Mar-2011"'
|
|
|
116 |
|
|
|
117 |
def main():
|
|
|
118 |
parser = optparse.OptionParser()
|
|
|
119 |
parser.add_option("-p", "--pickup", dest="pickup_report",
|
|
|
120 |
action="store_true",
|
|
|
121 |
help="Run the pickup reconciliation")
|
|
|
122 |
parser.add_option("-d", "--delivery", dest="delivery_report",
|
|
|
123 |
action="store_true",
|
|
|
124 |
help="Run the delivery reconciliation")
|
|
|
125 |
parser.add_option("-n", "--non-delivery", dest="non_delivery_report",
|
|
|
126 |
action="store_true",
|
|
|
127 |
help="Run the non delivery reconciliation")
|
|
|
128 |
parser.add_option("-a", "--all", dest="all_reports",
|
|
|
129 |
action="store_true",
|
|
|
130 |
help="Run all reconciliations")
|
|
|
131 |
parser.add_option("-P", "--provider", dest="provider",
|
|
|
132 |
default="BlueDart", type="string",
|
|
|
133 |
help="The PROVIDER this report is for",
|
|
|
134 |
metavar="PROVIDER")
|
|
|
135 |
parser.set_defaults(pickup_report=False, delivery_report=False, non_delivery_report=False, all_reports=False)
|
|
|
136 |
(options, args) = parser.parse_args()
|
|
|
137 |
if len(args) != 0:
|
|
|
138 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
139 |
|
|
|
140 |
if options.all_reports:
|
|
|
141 |
options.pickup_report = True
|
|
|
142 |
options.delivery_report = True
|
|
|
143 |
options.non_delivery_report = True
|
|
|
144 |
|
|
|
145 |
provider = get_provider_by_name(options.provider)
|
|
|
146 |
|
|
|
147 |
if options.pickup_report:
|
|
|
148 |
process_pickup_records(provider)
|
|
|
149 |
if options.delivery_report:
|
|
|
150 |
process_delivery_report(provider)
|
|
|
151 |
if options.non_delivery_report:
|
|
|
152 |
process_non_delivery_report(provider)
|
|
|
153 |
|
|
|
154 |
if __name__ == '__main__':
|
|
|
155 |
main()
|