| Line 1... |
Line 1... |
| 1 |
#!/usr/bin/python
|
1 |
#!/usr/bin/python
|
| 2 |
|
2 |
|
| - |
|
3 |
import time
|
| 3 |
import datetime
|
4 |
import datetime
|
| 4 |
import optparse
|
5 |
import optparse
|
| 5 |
import sys
|
6 |
import sys
|
| 6 |
import csv
|
7 |
import csv
|
| 7 |
import xlrd
|
8 |
import xlrd
|
| Line 12... |
Line 13... |
| 12 |
|
13 |
|
| 13 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
14 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
| 14 |
from shop2020.clients.TransactionClient import TransactionClient
|
15 |
from shop2020.clients.TransactionClient import TransactionClient
|
| 15 |
from shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceException
|
16 |
from shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceException
|
| 16 |
from shop2020.utils.EmailAttachmentDownloader import download_attachment
|
17 |
from shop2020.utils.EmailAttachmentDownloader import download_attachment
|
| - |
|
18 |
from shop2020.utils.EmailAttachmentSender import get_attachment_part, mail
|
| 17 |
from shop2020.utils.Utils import to_py_date
|
19 |
from shop2020.utils.Utils import to_py_date
|
| 18 |
|
20 |
|
| - |
|
21 |
from_user = 'cnc.center@shop2020.in'
|
| - |
|
22 |
from_pwd = '5h0p2o2o'
|
| - |
|
23 |
to = 'cnc.center@shop2020.in'
|
| - |
|
24 |
|
| 19 |
def process_pickup_records(provider):
|
25 |
def process_pickup_records(provider):
|
| 20 |
filename = fetch_report('PICKUP REPORT')
|
26 |
filename = fetch_report(provider.name.upper() + ' PICKUP REPORT')
|
| 21 |
pickup_details = read_pickup_report(filename)
|
27 |
pickup_details = read_pickup_report(filename)
|
| 22 |
orders_not_picked_up = update_picked_orders(provider.id, pickup_details)
|
28 |
orders_not_picked_up = update_picked_orders(provider.id, pickup_details)
|
| 23 |
if orders_not_picked_up:
|
29 |
if orders_not_picked_up:
|
| - |
|
30 |
mismatch_file = "PickupMismatch.csv"
|
| 24 |
print "Some of our orders were not picked up. Printing report to: PickupMismatch.csv"
|
31 |
print "Some of our orders were not picked up. Printing report to:" + mismatch_file
|
| 25 |
print_discrepancy_report("PickupMismatch.csv", orders_not_picked_up)
|
32 |
print_pickup_mismatch_report(mismatch_file, orders_not_picked_up)
|
| - |
|
33 |
pickup_mismatch_part = get_attachment_part(mismatch_file)
|
| - |
|
34 |
mail(from_user, from_pwd, to,\
|
| - |
|
35 |
"Pickup Mismatch Report for " + provider.name,\
|
| - |
|
36 |
"This is a system generated email.Please don't reply to it.",\
|
| - |
|
37 |
pickup_mismatch_part)
|
| 26 |
|
38 |
|
| 27 |
def process_delivery_report(provider):
|
39 |
def process_delivery_report(provider):
|
| 28 |
filename = fetch_report('DELIVERED AND RTO REPORT')
|
40 |
filename = fetch_report(provider.name.upper() + ' DELIVERED AND RTO REPORT')
|
| 29 |
#filename = 'delivery_report.xls'
|
41 |
#filename = 'delivery_report.xls'
|
| 30 |
delivered_orders, returned_orders = read_delivery_report(filename)
|
42 |
delivered_orders, returned_orders = read_delivery_report(filename)
|
| - |
|
43 |
if delivered_orders:
|
| 31 |
update_delivered_orders(provider.id, delivered_orders)
|
44 |
update_delivered_orders(provider.id, delivered_orders)
|
| - |
|
45 |
if returned_orders:
|
| 32 |
update_returned_orders(provider.id, returned_orders)
|
46 |
update_returned_orders(provider.id, returned_orders)
|
| - |
|
47 |
|
| - |
|
48 |
mail(from_user, from_pwd, to,\
|
| 33 |
#TODO: Send returned orders in a mail
|
49 |
"Returned Orders Report for " + provider.name,\
|
| - |
|
50 |
"This is a system generated email.Please don't reply to it.",\
|
| - |
|
51 |
None)
|
| 34 |
|
52 |
|
| 35 |
def process_non_delivery_report(provider):
|
53 |
def process_non_delivery_report(provider):
|
| 36 |
filename = fetch_report('UNDELIVERED REPORT')
|
54 |
filename = fetch_report(provider.name.upper() + ' UNDELIVERED REPORT')
|
| - |
|
55 |
undelivered_orders = read_undelivered_report(filename)
|
| - |
|
56 |
update_reason_of_undelivered_orders(provider.id, undelivered_orders)
|
| - |
|
57 |
|
| - |
|
58 |
def update_reason_of_undelivered_orders(provider_id, undelivered_orders):
|
| - |
|
59 |
txnClient = TransactionClient().get_client()
|
| - |
|
60 |
try:
|
| - |
|
61 |
txnClient.updateNonDeliveryReason(provider_id, undelivered_orders)
|
| - |
|
62 |
except TransactionServiceException as tex:
|
| 37 |
|
63 |
print tex.message
|
| - |
|
64 |
|
| 38 |
def get_provider_by_name(provider_name):
|
65 |
def get_provider_by_name(provider_name):
|
| 39 |
logistics_client = LogisticsClient().get_client()
|
66 |
logistics_client = LogisticsClient().get_client()
|
| 40 |
#FIXME: Write a thrift call to get a provider by name
|
67 |
#TODO: Write a thrift call to get a provider by name
|
| 41 |
provider = None
|
68 |
provider = None
|
| 42 |
providers = logistics_client.getAllProviders()
|
69 |
providers = logistics_client.getAllProviders()
|
| 43 |
for p in providers:
|
70 |
for p in providers:
|
| 44 |
if p.name == provider_name:
|
71 |
if p.name == provider_name:
|
| 45 |
provider=p
|
72 |
provider=p
|
| Line 60... |
Line 87... |
| 60 |
sheet = workbook.sheet_by_index(0)
|
87 |
sheet = workbook.sheet_by_index(0)
|
| 61 |
num_rows = sheet.nrows
|
88 |
num_rows = sheet.nrows
|
| 62 |
picked_up_orders = {}
|
89 |
picked_up_orders = {}
|
| 63 |
for rownum in range(1, num_rows):
|
90 |
for rownum in range(1, num_rows):
|
| 64 |
unused_customer_code, awb, date, time = sheet.row_values(rownum)[0:4]
|
91 |
unused_customer_code, awb, date, time = sheet.row_values(rownum)[0:4]
|
| 65 |
picked_up_orders[awb] = 0 #TODO: Use the date and time value in the report
|
92 |
picked_up_orders[awb] = str(get_py_datetime(date, time))
|
| 66 |
|
93 |
|
| 67 |
print "Picked up Orders:"
|
94 |
print "Picked up Orders:"
|
| 68 |
print picked_up_orders
|
95 |
print picked_up_orders
|
| 69 |
return picked_up_orders
|
96 |
return picked_up_orders
|
| 70 |
|
97 |
|
| Line 74... |
Line 101... |
| 74 |
sheet = workbook.sheet_by_index(0)
|
101 |
sheet = workbook.sheet_by_index(0)
|
| 75 |
num_rows = sheet.nrows
|
102 |
num_rows = sheet.nrows
|
| 76 |
delivered_orders = {}
|
103 |
delivered_orders = {}
|
| 77 |
returned_orders = {}
|
104 |
returned_orders = {}
|
| 78 |
for rownum in range(1, num_rows):
|
105 |
for rownum in range(1, num_rows):
|
| 79 |
unused_customer_code, awb, date, time, status, receiver, reason_for_return = sheet.row_values(rownum)[0:7]
|
106 |
unused_customer_code, awb, time, date, unused_status, receiver, reason_for_return = sheet.row_values(rownum)[0:7]
|
| - |
|
107 |
delivery_date = str(get_py_datetime(date, time))
|
| 80 |
if receiver: #TODO: Use status for this check
|
108 |
if receiver: #TODO: Use status for this check
|
| 81 |
delivered_orders[awb] = str(datetime.datetime.now()) + "|" + receiver #TODO: Use the date and time value in the report
|
109 |
delivered_orders[awb] = delivery_date + "|" + receiver
|
| 82 |
else:
|
110 |
else:
|
| 83 |
returned_orders[awb] = str(datetime.datetime.now()) + "|" + reason_for_return
|
111 |
returned_orders[awb] = delivery_date + "|" + reason_for_return
|
| 84 |
|
112 |
|
| 85 |
print "Delivered Orders:"
|
113 |
print "Delivered Orders:"
|
| 86 |
print delivered_orders
|
114 |
print delivered_orders
|
| 87 |
|
115 |
|
| 88 |
print "Returned Orders:"
|
116 |
print "Returned Orders:"
|
| 89 |
print returned_orders
|
117 |
print returned_orders
|
| 90 |
return delivered_orders, returned_orders
|
118 |
return delivered_orders, returned_orders
|
| 91 |
|
119 |
|
| - |
|
120 |
def read_undelivered_report(filename):
|
| - |
|
121 |
print "Reading undelivered details from:" + filename
|
| - |
|
122 |
workbook = xlrd.open_workbook(filename)
|
| - |
|
123 |
sheet = workbook.sheet_by_index(0)
|
| - |
|
124 |
num_rows = sheet.nrows
|
| - |
|
125 |
undelivered_orders = {}
|
| - |
|
126 |
for rownum in range(1, num_rows):
|
| - |
|
127 |
unused_cusotmer_code, awb, reason, time, date = sheet.row_values(rownum)[0:5]
|
| - |
|
128 |
unused_status_date = str(get_py_datetime(date, time))
|
| - |
|
129 |
undelivered_orders[awb] = reason
|
| - |
|
130 |
|
| - |
|
131 |
print "Undelivered Orders"
|
| - |
|
132 |
print undelivered_orders
|
| - |
|
133 |
|
| - |
|
134 |
return undelivered_orders
|
| - |
|
135 |
|
| 92 |
def update_picked_orders(provider_id, pickup_details):
|
136 |
def update_picked_orders(provider_id, pickup_details):
|
| 93 |
txnClient = TransactionClient().get_client()
|
137 |
txnClient = TransactionClient().get_client()
|
| 94 |
try:
|
138 |
try:
|
| 95 |
orders_not_picked_up = txnClient.markOrdersAsPickedUp(provider_id, pickup_details)
|
139 |
orders_not_picked_up = txnClient.markOrdersAsPickedUp(provider_id, pickup_details)
|
| 96 |
return orders_not_picked_up
|
140 |
return orders_not_picked_up
|
| Line 109... |
Line 153... |
| 109 |
try:
|
153 |
try:
|
| 110 |
txnClient.markOrdersAsFailed(provider_id, returned_orders)
|
154 |
txnClient.markOrdersAsFailed(provider_id, returned_orders)
|
| 111 |
except TransactionServiceException as tex:
|
155 |
except TransactionServiceException as tex:
|
| 112 |
print tex.message
|
156 |
print tex.message
|
| 113 |
|
157 |
|
| 114 |
def print_discrepancy_report(filename, orders):
|
158 |
def print_pickup_mismatch_report(filename, orders):
|
| 115 |
writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
|
159 |
writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
|
| 116 |
writer.writerow(['Order Id', 'AWB No', 'Shipping timestamp'])
|
160 |
writer.writerow(['Order Id', 'AWB No', 'Shipping timestamp'])
|
| 117 |
for order in orders:
|
161 |
for order in orders:
|
| 118 |
writer.writerow([order.id, order.airwaybill_no, to_py_date(order.shipping_timestamp)])
|
162 |
writer.writerow([order.id, order.airwaybill_no, to_py_date(order.shipping_timestamp)])
|
| 119 |
|
163 |
|
| 120 |
def todays_date_string():
|
164 |
def todays_date_string():
|
| - |
|
165 |
today_date = time.strftime("%d-%b-%Y")
|
| 121 |
return '"24-Mar-2011"'
|
166 |
return '"' + today_date + '"'
|
| - |
|
167 |
|
| - |
|
168 |
def get_py_datetime(date, time):
|
| - |
|
169 |
# This should be a command line argument.
|
| - |
|
170 |
# Refer http://docs.python.org/library/time.html#time.strftime to
|
| - |
|
171 |
# get a complete list of format specifiers available for date time.
|
| - |
|
172 |
time_format = "%d-%b-%y %H%M"
|
| - |
|
173 |
time_string = date + " " + time
|
| - |
|
174 |
mytime = time.strptime(time_string, time_format)
|
| - |
|
175 |
return datetime.datetime(*mytime[:6])
|
| 122 |
|
176 |
|
| 123 |
def main():
|
177 |
def main():
|
| 124 |
parser = optparse.OptionParser()
|
178 |
parser = optparse.OptionParser()
|
| 125 |
parser.add_option("-p", "--pickup", dest="pickup_report",
|
179 |
parser.add_option("-p", "--pickup", dest="pickup_report",
|
| 126 |
action="store_true",
|
180 |
action="store_true",
|