| 5262 |
phani.kuma |
1 |
#!/usr/bin/python
|
|
|
2 |
'''
|
|
|
3 |
It processes the following orders:
|
|
|
4 |
1. Orders in DOA_PICKUP_CONFIRMED status : get details of orders that
|
|
|
5 |
were in DOA_PICKUP_CONFIRMED status from database and
|
|
|
6 |
calls Delhivery api to know whether they are picked up by Delhivery
|
|
|
7 |
and changes the status to DOA_RETURN_IN_TRANSIT if it is done.
|
|
|
8 |
2. Orders in RET_PICKUP_CONFIRMED status : get details of orders that
|
|
|
9 |
were in RET_PICKUP_CONFIRMED status from database and
|
|
|
10 |
calls Delhivery api to know whether they are picked up by Delhivery
|
|
|
11 |
and changes the status to RET_RETURN_IN_TRANSIT if it is done.
|
|
|
12 |
3. Orders in SHIPPED_FROM_WH status: get details of orders that
|
|
|
13 |
were in SHIPPED_FROM_WH status from database and
|
|
|
14 |
calls Delhivery api to know whether they are picked up by Delhivery
|
|
|
15 |
and changes the status to SHIPPED_TO_LOGST if it is done.
|
|
|
16 |
4. Orders in SHIPPED_TO_LOGST status: get details of orders that
|
|
|
17 |
were in SHIPPED_TO_LOGST status from database and
|
|
|
18 |
calls Delhivery api to know their status and changes the status accordingly.
|
|
|
19 |
|
|
|
20 |
It sends out a Pickup mismatch report, Return orders Pickup Mismatch report, Doa Pickup mismatch report,
|
|
|
21 |
Undelivered orders report and Returned Orders report to cnc.center@shop2020.in
|
|
|
22 |
|
|
|
23 |
to track DOA orders and for other orders ConfigClient is called to get Delhivery_update_url
|
|
|
24 |
|
|
|
25 |
@author: Phani Kumar
|
|
|
26 |
'''
|
| 6561 |
amit.gupta |
27 |
from shop2020.clients.CRMClient import CRMClient
|
|
|
28 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
|
|
29 |
from shop2020.clients.TransactionClient import TransactionClient
|
|
|
30 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| 13133 |
manish.sha |
31 |
from shop2020.model.v1.order.script.LogisticUtils import create_crm_tickets_for_delivey_attempted_orders
|
| 6561 |
amit.gupta |
32 |
from shop2020.thriftpy.config.ttypes import ConfigException
|
| 13085 |
amit.gupta |
33 |
from shop2020.thriftpy.crm.ttypes import SearchFilter, TicketCategory, \
|
|
|
34 |
TicketPriority, TicketStatus, Activity, ActivityType
|
| 6561 |
amit.gupta |
35 |
from shop2020.thriftpy.model.v1.order.ttypes import TransactionServiceException, \
|
|
|
36 |
OrderStatus
|
|
|
37 |
from shop2020.utils.EmailAttachmentSender import get_attachment_part, mail
|
|
|
38 |
from shop2020.utils.Utils import to_py_date
|
|
|
39 |
from xml.etree.ElementTree import parse
|
|
|
40 |
import csv
|
| 5262 |
phani.kuma |
41 |
import datetime
|
|
|
42 |
import optparse
|
|
|
43 |
import sys
|
| 6561 |
amit.gupta |
44 |
import time
|
| 5262 |
phani.kuma |
45 |
import traceback
|
|
|
46 |
import urllib2
|
|
|
47 |
|
| 13085 |
amit.gupta |
48 |
|
| 5262 |
phani.kuma |
49 |
if __name__ == '__main__' and __package__ is None:
|
|
|
50 |
import os
|
|
|
51 |
sys.path.insert(0, os.getcwd())
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
try:
|
|
|
55 |
config_client = ConfigClient()
|
|
|
56 |
Delhivery_URL = config_client.get_property("Delhivery_update_url")
|
|
|
57 |
except ConfigException as cex:
|
|
|
58 |
print cex.message
|
|
|
59 |
traceback.print_exc()
|
|
|
60 |
|
| 20088 |
kshitij.so |
61 |
defaultUndeliveredAsssigneeId = 65
|
| 13085 |
amit.gupta |
62 |
dtrUndeliveredAsssigneeId = 33
|
| 5262 |
phani.kuma |
63 |
from_user = 'cnc.center@shop2020.in'
|
|
|
64 |
from_pwd = '5h0p2o2o'
|
| 9173 |
manish.sha |
65 |
to = ["amit.sirohi@shop2020.in", "sandeep.sachdeva@shop2020.in", "sunil.kumar@shop2020.in"]
|
| 5262 |
phani.kuma |
66 |
|
|
|
67 |
def process_dao_pickup_orders(provider):
|
|
|
68 |
try:
|
|
|
69 |
doas_tobe_picked_up = fetch_data(provider.id, [OrderStatus.DOA_PICKUP_CONFIRMED])
|
|
|
70 |
doa_pickup_details = read_dao_return_pickup_orders(doas_tobe_picked_up)
|
|
|
71 |
if doa_pickup_details:
|
|
|
72 |
update_picked_doas(provider.id, doa_pickup_details)
|
|
|
73 |
except:
|
|
|
74 |
print "Some issue while processing the orders in DOA_PICKUP_CONFIRMED status"
|
|
|
75 |
traceback.print_exc()
|
|
|
76 |
|
|
|
77 |
def process_return_pickup_orders(provider):
|
|
|
78 |
try:
|
|
|
79 |
returns_tobe_picked_up = fetch_data(provider.id, [OrderStatus.RET_PICKUP_CONFIRMED])
|
|
|
80 |
returns_pickup_details = read_dao_return_pickup_orders(returns_tobe_picked_up)
|
|
|
81 |
if returns_pickup_details:
|
|
|
82 |
update_picked_returns(provider.id, returns_pickup_details)
|
|
|
83 |
except:
|
|
|
84 |
print "Some issue while processing the orders in RET_PICKUP_CONFIRMED status"
|
|
|
85 |
traceback.print_exc()
|
|
|
86 |
|
|
|
87 |
def process_pickup_records(provider):
|
|
|
88 |
try:
|
|
|
89 |
orders_tobe_picked_up = fetch_data(provider.id, [OrderStatus.SHIPPED_FROM_WH])
|
|
|
90 |
pickup_details = read_pickup_orders(orders_tobe_picked_up)
|
|
|
91 |
if pickup_details:
|
|
|
92 |
update_picked_orders(provider.id, pickup_details)
|
|
|
93 |
except:
|
|
|
94 |
print "Some issue while processing the orders in SHIPPED_FROM_WH status"
|
|
|
95 |
traceback.print_exc()
|
|
|
96 |
|
|
|
97 |
def process_local_connection_orders(provider):
|
|
|
98 |
try:
|
|
|
99 |
orders_tobe_local_connected = fetch_data(provider.id, [OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST])
|
|
|
100 |
local_connected_orders = read_local_connection_orders(orders_tobe_local_connected)
|
|
|
101 |
if local_connected_orders:
|
|
|
102 |
update_local_connected_orders(provider.id, local_connected_orders)
|
|
|
103 |
except:
|
|
|
104 |
print "Some issue while processing the orders for local connection status"
|
|
|
105 |
traceback.print_exc()
|
|
|
106 |
|
|
|
107 |
def process_reached_destination_city_orders(provider):
|
|
|
108 |
try:
|
|
|
109 |
orders_tobe_reached_destination_city = fetch_data(provider.id, [OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.SHIPPED_TO_DESTINATION_CITY])
|
|
|
110 |
destination_city_reached_orders = read_reached_destination_orders(orders_tobe_reached_destination_city)
|
|
|
111 |
if destination_city_reached_orders:
|
|
|
112 |
update_destination_city_reached_orders(provider.id, destination_city_reached_orders)
|
|
|
113 |
except:
|
|
|
114 |
print "Some issue while processing the orders for Reached Destination City status"
|
|
|
115 |
traceback.print_exc()
|
|
|
116 |
|
|
|
117 |
def process_first_delivery_attempt_orders(provider):
|
|
|
118 |
try:
|
|
|
119 |
orders_tobe_first_delivery_attempted = fetch_data(provider.id, [OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.SHIPPED_TO_DESTINATION_CITY, OrderStatus.REACHED_DESTINATION_CITY])
|
|
|
120 |
first_atdl_orders = read_first_delivery_attempt_orders(orders_tobe_first_delivery_attempted)
|
|
|
121 |
if first_atdl_orders:
|
|
|
122 |
update_first_atdl_orders(provider.id, first_atdl_orders)
|
|
|
123 |
except:
|
|
|
124 |
print "Some issue while processing the orders for First delivery attempt status"
|
|
|
125 |
traceback.print_exc()
|
|
|
126 |
|
|
|
127 |
def process_delivery_report(provider):
|
|
|
128 |
try:
|
| 20088 |
kshitij.so |
129 |
orders_tobe_delivered = fetch_data(provider.id, [OrderStatus.SHIPPED_FROM_WH, OrderStatus.SHIPPED_TO_LOGST, OrderStatus.SHIPPED_TO_DESTINATION_CITY, OrderStatus.REACHED_DESTINATION_CITY, OrderStatus.FIRST_DELIVERY_ATTEMPT_MADE, OrderStatus.RTO_IN_TRANSIT])
|
| 5262 |
phani.kuma |
130 |
delivered_orders, returned_orders, undelivered_orders = read_delivery_orders(orders_tobe_delivered)
|
|
|
131 |
if delivered_orders:
|
|
|
132 |
update_delivered_orders(provider.id, delivered_orders)
|
|
|
133 |
if returned_orders:
|
|
|
134 |
update_returned_orders(provider.id, returned_orders)
|
|
|
135 |
if undelivered_orders:
|
|
|
136 |
update_reason_of_undelivered_orders(provider.id, undelivered_orders)
|
|
|
137 |
except:
|
|
|
138 |
print "Some issue while processing the orders for delivery status"
|
|
|
139 |
traceback.print_exc()
|
|
|
140 |
|
|
|
141 |
def generate_reports(provider):
|
|
|
142 |
get_doas_not_picked_up(provider)
|
|
|
143 |
get_returns_not_picked_up(provider)
|
|
|
144 |
get_orders_not_picked_up(provider)
|
|
|
145 |
get_orders_pending_local_connection(provider)
|
|
|
146 |
get_returned_orders(provider)
|
|
|
147 |
get_orders_not_delivered(provider)
|
|
|
148 |
|
|
|
149 |
def get_doas_not_picked_up(provider):
|
|
|
150 |
txnClient = TransactionClient().get_client()
|
|
|
151 |
try:
|
|
|
152 |
doas_not_picked_up = txnClient.getDoasNotPickedUp(provider.id)
|
|
|
153 |
except TransactionServiceException as tex:
|
|
|
154 |
print tex.message
|
|
|
155 |
|
|
|
156 |
try:
|
|
|
157 |
if doas_not_picked_up:
|
|
|
158 |
print "DOAs not Picked up:"
|
|
|
159 |
print doas_not_picked_up
|
|
|
160 |
mismatch_file = "/tmp/Delhivery_DoaPickupMismatch.csv"
|
|
|
161 |
print "Some of our DOA orders were not picked up. Printing report to:" + mismatch_file
|
|
|
162 |
print_dao_return_pickup_mismatch_report(mismatch_file, doas_not_picked_up)
|
|
|
163 |
pickup_mismatch_part = [get_attachment_part(mismatch_file)]
|
|
|
164 |
mail(from_user, from_pwd, to,\
|
|
|
165 |
"DOA Pickup Mismatch for " + provider.name,\
|
|
|
166 |
"This is a system generated email.Please don't reply to it.",\
|
|
|
167 |
pickup_mismatch_part)
|
|
|
168 |
except Exception:
|
|
|
169 |
print "Some issue sending the DOA mismatch report"
|
|
|
170 |
traceback.print_exc()
|
|
|
171 |
|
|
|
172 |
def get_returns_not_picked_up(provider):
|
|
|
173 |
txnClient = TransactionClient().get_client()
|
|
|
174 |
try:
|
|
|
175 |
returns_not_picked_up = txnClient.getReturnOrdersNotPickedUp(provider.id)
|
|
|
176 |
except TransactionServiceException as tex:
|
|
|
177 |
print tex.message
|
|
|
178 |
|
|
|
179 |
try:
|
|
|
180 |
if returns_not_picked_up:
|
|
|
181 |
print "Return Orders not Picked up:"
|
|
|
182 |
print returns_not_picked_up
|
|
|
183 |
mismatch_file = "/tmp/Delhivery_ReturnsPickupMismatch.csv"
|
|
|
184 |
print "Some of our Return orders were not picked up. Printing report to:" + mismatch_file
|
|
|
185 |
print_dao_return_pickup_mismatch_report(mismatch_file, returns_not_picked_up)
|
|
|
186 |
pickup_mismatch_part = [get_attachment_part(mismatch_file)]
|
|
|
187 |
mail(from_user, from_pwd, to,\
|
|
|
188 |
"Return orders Pickup Mismatch for " + provider.name,\
|
|
|
189 |
"This is a system generated email.Please don't reply to it.",\
|
|
|
190 |
pickup_mismatch_part)
|
|
|
191 |
except Exception:
|
|
|
192 |
print "Some issue sending the Return orders mismatch report"
|
|
|
193 |
traceback.print_exc()
|
|
|
194 |
|
|
|
195 |
def get_orders_not_picked_up(provider):
|
|
|
196 |
txnClient = TransactionClient().get_client()
|
|
|
197 |
try:
|
|
|
198 |
orders_not_picked_up = txnClient.getOrdersNotPickedUp(provider.id)
|
|
|
199 |
except TransactionServiceException as tex:
|
|
|
200 |
print tex.message
|
|
|
201 |
|
|
|
202 |
try:
|
|
|
203 |
if orders_not_picked_up:
|
|
|
204 |
print "Orders not Picked up:"
|
|
|
205 |
print orders_not_picked_up
|
|
|
206 |
mismatch_file = "/tmp/Delhivery_PickupMismatch.csv"
|
|
|
207 |
print "Some of our orders were not picked up. Printing report to:" + mismatch_file
|
|
|
208 |
print_pickup_mismatch_report(mismatch_file, orders_not_picked_up)
|
|
|
209 |
pickup_mismatch_part = [get_attachment_part(mismatch_file)]
|
|
|
210 |
mail(from_user, from_pwd, to,\
|
|
|
211 |
"Order Pickup Mismatch for " + provider.name,\
|
|
|
212 |
"This is a system generated email.Please don't reply to it.",\
|
|
|
213 |
pickup_mismatch_part)
|
|
|
214 |
except Exception:
|
|
|
215 |
print "Some issue sending the pickup mismatch report"
|
|
|
216 |
traceback.print_exc()
|
|
|
217 |
|
|
|
218 |
def get_orders_pending_local_connection(provider):
|
|
|
219 |
txnClient = TransactionClient().get_client()
|
|
|
220 |
try:
|
|
|
221 |
orders_pending_local_connection = txnClient.getOrdersNotLocalConnected(provider.id)
|
|
|
222 |
except TransactionServiceException as tex:
|
|
|
223 |
print tex.message
|
|
|
224 |
|
|
|
225 |
try:
|
|
|
226 |
if orders_pending_local_connection:
|
|
|
227 |
print "Local Connection Pending Orders:"
|
|
|
228 |
print orders_pending_local_connection
|
|
|
229 |
mismatch_file = "/tmp/Delhivery_LocalConnectionPendingOrders.csv"
|
|
|
230 |
print "Some of our Orders were not Shipped to Destination yet. Printing report to:" + mismatch_file
|
|
|
231 |
print_undelivered_orders_report(mismatch_file, orders_pending_local_connection)
|
|
|
232 |
pickup_mismatch_part = [get_attachment_part(mismatch_file)]
|
|
|
233 |
mail(from_user, from_pwd, to,\
|
|
|
234 |
"Orders that are not Shipped to Destination yet for " + provider.name,\
|
|
|
235 |
"This is a system generated email.Please don't reply to it.",\
|
|
|
236 |
pickup_mismatch_part)
|
|
|
237 |
except Exception:
|
|
|
238 |
print "Some issue updating and sending the Local Connection orders report"
|
|
|
239 |
traceback.print_exc()
|
|
|
240 |
|
|
|
241 |
def get_returned_orders(provider):
|
|
|
242 |
txnClient = TransactionClient().get_client()
|
|
|
243 |
try:
|
|
|
244 |
returned_orders = txnClient.getRTOrders(provider.id)
|
|
|
245 |
except TransactionServiceException as tex:
|
|
|
246 |
print tex.message
|
|
|
247 |
|
|
|
248 |
try:
|
|
|
249 |
if returned_orders:
|
|
|
250 |
print "Returned Orders:"
|
|
|
251 |
print returned_orders
|
|
|
252 |
returned_orders_file = "/tmp/Delhivery_ReturnedOrders.csv"
|
|
|
253 |
print "Some of our Orders were returned by logistics provider. Printing report to:" + returned_orders_file
|
|
|
254 |
print_rto_orders_report(returned_orders_file, returned_orders)
|
|
|
255 |
returned_orders_report = [get_attachment_part(returned_orders_file)]
|
|
|
256 |
mail(from_user, from_pwd, to,\
|
|
|
257 |
"Returned Orders Report for " + provider.name,\
|
|
|
258 |
"This is a system generated email.Please don't reply to it.",\
|
|
|
259 |
returned_orders_report)
|
|
|
260 |
except:
|
|
|
261 |
print "Some issue sending the returned orders report"
|
|
|
262 |
traceback.print_exc()
|
|
|
263 |
|
|
|
264 |
def get_orders_not_delivered(provider):
|
|
|
265 |
txnClient = TransactionClient().get_client()
|
|
|
266 |
try:
|
|
|
267 |
orders_not_delivered = txnClient.getNonDeliveredOrdersbyCourier(provider.id)
|
|
|
268 |
except TransactionServiceException as tex:
|
|
|
269 |
print tex.message
|
|
|
270 |
|
|
|
271 |
try:
|
|
|
272 |
if orders_not_delivered:
|
|
|
273 |
print "Undelivered Orders:"
|
|
|
274 |
print orders_not_delivered
|
|
|
275 |
mismatch_file = "/tmp/Delhivery_UndeliveredOrders.csv"
|
|
|
276 |
print "Some of our Orders were not delivered. Printing report to:" + mismatch_file
|
|
|
277 |
print_undelivered_orders_report(mismatch_file, orders_not_delivered)
|
|
|
278 |
pickup_mismatch_part = [get_attachment_part(mismatch_file)]
|
|
|
279 |
mail(from_user, from_pwd, to,\
|
|
|
280 |
"Orders that are undelivered but picked up or shipped four days ago for " + provider.name,\
|
|
|
281 |
"This is a system generated email.Please don't reply to it.",\
|
|
|
282 |
pickup_mismatch_part)
|
|
|
283 |
except Exception:
|
|
|
284 |
print "Some issue updating and sending the undelivered orders report"
|
|
|
285 |
traceback.print_exc()
|
|
|
286 |
|
|
|
287 |
def get_provider_by_name(provider_name):
|
|
|
288 |
logistics_client = LogisticsClient().get_client()
|
|
|
289 |
provider = None
|
|
|
290 |
providers = logistics_client.getAllProviders()
|
|
|
291 |
for p in providers:
|
|
|
292 |
if p.name == provider_name:
|
|
|
293 |
provider=p
|
|
|
294 |
break
|
|
|
295 |
if provider == None:
|
|
|
296 |
sys.exit("Can't continue execution: No such provider")
|
|
|
297 |
return provider
|
|
|
298 |
|
|
|
299 |
def fetch_data(provider_id, order_status_list):
|
|
|
300 |
txnClient = TransactionClient().get_client()
|
|
|
301 |
try:
|
|
|
302 |
doas_tobe_picked_up = txnClient.getOrdersForProviderForStatus(provider_id, order_status_list)
|
|
|
303 |
return doas_tobe_picked_up
|
|
|
304 |
except TransactionServiceException as tex:
|
|
|
305 |
print tex.message
|
|
|
306 |
|
|
|
307 |
def read_dao_return_pickup_orders(orders_tobe_picked_up):
|
|
|
308 |
picked_up_orders = {}
|
| 5325 |
phani.kuma |
309 |
list_of_order = []
|
| 5262 |
phani.kuma |
310 |
for order in orders_tobe_picked_up:
|
| 5325 |
phani.kuma |
311 |
list_of_order.append(str(order.pickupRequestNo))
|
| 12961 |
manish.sha |
312 |
|
|
|
313 |
parentAwbList = [list_of_order[x:x+10] for x in xrange(0, len(list_of_order), 10)]
|
| 5325 |
phani.kuma |
314 |
|
| 12961 |
manish.sha |
315 |
for awbList in parentAwbList:
|
|
|
316 |
try:
|
|
|
317 |
uri = Delhivery_URL + 'ref_nos=' + ','.join(awbList)
|
|
|
318 |
root = parse(urllib2.urlopen(uri)).getroot()
|
|
|
319 |
shipments = root.findall('Shipment')
|
|
|
320 |
for shipment in shipments:
|
|
|
321 |
nodes = shipment.findall('Scans/ScanDetail')
|
|
|
322 |
for element in nodes:
|
|
|
323 |
delivery_date = get_py_datetime(element.findtext('ScanDateTime', ''))
|
|
|
324 |
picked_up_orders[shipment.findtext('ReferenceNo', '')] = str(delivery_date)
|
|
|
325 |
break
|
|
|
326 |
except:
|
|
|
327 |
pass
|
| 5262 |
phani.kuma |
328 |
|
|
|
329 |
print "Picked up Orders:"
|
|
|
330 |
print picked_up_orders
|
|
|
331 |
return picked_up_orders
|
|
|
332 |
|
|
|
333 |
def read_pickup_orders(orders_tobe_picked_up):
|
|
|
334 |
picked_up_orders = {}
|
| 5325 |
phani.kuma |
335 |
list_of_order = []
|
| 5262 |
phani.kuma |
336 |
for order in orders_tobe_picked_up:
|
| 5325 |
phani.kuma |
337 |
list_of_order.append(str(order.airwaybill_no))
|
| 12961 |
manish.sha |
338 |
|
|
|
339 |
parentAwbList = [list_of_order[x:x+10] for x in xrange(0, len(list_of_order), 10)]
|
| 5325 |
phani.kuma |
340 |
|
| 12961 |
manish.sha |
341 |
for awbList in parentAwbList:
|
|
|
342 |
try:
|
|
|
343 |
uri = Delhivery_URL + 'waybill=' + ','.join(awbList)
|
|
|
344 |
root = parse(urllib2.urlopen(uri)).getroot()
|
|
|
345 |
shipments = root.findall('Shipment')
|
|
|
346 |
for shipment in shipments:
|
|
|
347 |
nodes = shipment.findall('Scans/ScanDetail')
|
|
|
348 |
for element in nodes:
|
|
|
349 |
delivery_date = get_py_datetime(element.findtext('ScanDateTime', ''))
|
|
|
350 |
picked_up_orders[shipment.findtext('AWB', '')] = str(delivery_date)
|
|
|
351 |
break
|
|
|
352 |
except:
|
|
|
353 |
pass
|
| 5262 |
phani.kuma |
354 |
|
|
|
355 |
print "Picked up Orders:"
|
|
|
356 |
print picked_up_orders
|
|
|
357 |
return picked_up_orders
|
|
|
358 |
|
|
|
359 |
def read_local_connection_orders(orders_tobe_local_connected):
|
|
|
360 |
local_connected_orders = {}
|
| 5325 |
phani.kuma |
361 |
list_of_order = []
|
| 5262 |
phani.kuma |
362 |
for order in orders_tobe_local_connected:
|
| 5325 |
phani.kuma |
363 |
list_of_order.append(str(order.airwaybill_no))
|
|
|
364 |
|
| 12961 |
manish.sha |
365 |
parentAwbList = [list_of_order[x:x+10] for x in xrange(0, len(list_of_order), 10)]
|
| 5262 |
phani.kuma |
366 |
|
| 12961 |
manish.sha |
367 |
for awbList in parentAwbList:
|
|
|
368 |
try:
|
|
|
369 |
uri = Delhivery_URL + 'waybill=' + ','.join(awbList)
|
|
|
370 |
root = parse(urllib2.urlopen(uri)).getroot()
|
|
|
371 |
shipments = root.findall('Shipment')
|
|
|
372 |
for shipment in shipments:
|
|
|
373 |
nodes = shipment.findall('Scans/ScanDetail')
|
|
|
374 |
for element in nodes:
|
|
|
375 |
if element.findtext('ScanType', '') == 'UD' and getTrimedString(element.findtext('Scan', '')) == getTrimedString('In Transit'):
|
|
|
376 |
delivery_date = get_py_datetime(element.findtext('ScanDateTime', ''))
|
|
|
377 |
local_connected_orders[shipment.findtext('AWB', '')] = str(delivery_date)
|
|
|
378 |
break
|
|
|
379 |
except:
|
|
|
380 |
pass
|
|
|
381 |
|
| 5262 |
phani.kuma |
382 |
print "Local Connected Orders"
|
|
|
383 |
print local_connected_orders
|
|
|
384 |
|
|
|
385 |
return local_connected_orders
|
|
|
386 |
|
|
|
387 |
def read_reached_destination_orders(orders_tobe_reached_destination_city):
|
|
|
388 |
destination_city_reached_orders = {}
|
| 5325 |
phani.kuma |
389 |
list_of_order = []
|
| 5262 |
phani.kuma |
390 |
for order in orders_tobe_reached_destination_city:
|
| 5325 |
phani.kuma |
391 |
list_of_order.append(str(order.airwaybill_no))
|
|
|
392 |
|
| 12961 |
manish.sha |
393 |
parentAwbList = [list_of_order[x:x+10] for x in xrange(0, len(list_of_order), 10)]
|
| 5262 |
phani.kuma |
394 |
|
| 12961 |
manish.sha |
395 |
for awbList in parentAwbList:
|
|
|
396 |
try:
|
|
|
397 |
uri = Delhivery_URL + 'waybill=' + ','.join(awbList)
|
|
|
398 |
root = parse(urllib2.urlopen(uri)).getroot()
|
|
|
399 |
shipments = root.findall('Shipment')
|
|
|
400 |
for shipment in shipments:
|
|
|
401 |
nodes = shipment.findall('Scans/ScanDetail')
|
|
|
402 |
for element in nodes:
|
|
|
403 |
if element.findtext('ScanType', '') == 'UD' and getTrimedString(element.findtext('Scan', '')) == getTrimedString('Pending'):
|
|
|
404 |
delivery_date = get_py_datetime(element.findtext('ScanDateTime', ''))
|
|
|
405 |
destination_city_reached_orders[shipment.findtext('AWB', '')] = str(delivery_date)
|
|
|
406 |
break
|
|
|
407 |
except:
|
|
|
408 |
pass
|
|
|
409 |
|
| 5262 |
phani.kuma |
410 |
print "Destination City Reached Orders"
|
|
|
411 |
print destination_city_reached_orders
|
|
|
412 |
|
|
|
413 |
return destination_city_reached_orders
|
|
|
414 |
|
|
|
415 |
def read_first_delivery_attempt_orders(orders_tobe_first_delivery_attempted):
|
|
|
416 |
first_atdl_orders = {}
|
| 5325 |
phani.kuma |
417 |
list_of_order = []
|
| 5262 |
phani.kuma |
418 |
for order in orders_tobe_first_delivery_attempted:
|
| 5325 |
phani.kuma |
419 |
list_of_order.append(str(order.airwaybill_no))
|
| 12961 |
manish.sha |
420 |
|
|
|
421 |
parentAwbList = [list_of_order[x:x+10] for x in xrange(0, len(list_of_order), 10)]
|
| 5325 |
phani.kuma |
422 |
|
| 12961 |
manish.sha |
423 |
for awbList in parentAwbList:
|
|
|
424 |
try:
|
|
|
425 |
uri = Delhivery_URL + 'waybill=' + ','.join(awbList)
|
|
|
426 |
root = parse(urllib2.urlopen(uri)).getroot()
|
|
|
427 |
shipments = root.findall('Shipment')
|
|
|
428 |
for shipment in shipments:
|
|
|
429 |
nodes = shipment.findall('Scans/ScanDetail')
|
|
|
430 |
node_number = 0
|
|
|
431 |
for element in nodes:
|
|
|
432 |
if element.findtext('ScanType', '') == 'UD' and getTrimedString(element.findtext('Scan', '')) == getTrimedString('Dispatched'):
|
|
|
433 |
if node_number != len(nodes)-1 and nodes[node_number+1].findtext('ScanType', '') == 'UD':
|
|
|
434 |
element = nodes[node_number+1]
|
|
|
435 |
delivery_date = get_py_datetime(element.findtext('ScanDateTime', ''))
|
|
|
436 |
reason_for_nondelivery = element.findtext('Instructions', '')
|
|
|
437 |
first_atdl_orders[shipment.findtext('AWB', '')] = str(delivery_date) + "|" + reason_for_nondelivery
|
|
|
438 |
break
|
|
|
439 |
node_number = node_number + 1
|
|
|
440 |
except:
|
|
|
441 |
pass
|
| 5262 |
phani.kuma |
442 |
|
|
|
443 |
print "FIRST DELIVERY ATTEMPT MADE Orders"
|
|
|
444 |
print first_atdl_orders
|
|
|
445 |
|
|
|
446 |
return first_atdl_orders
|
|
|
447 |
|
|
|
448 |
def read_delivery_orders(orders_tobe_delivered):
|
|
|
449 |
delivered_orders = {}
|
|
|
450 |
returned_orders = {}
|
|
|
451 |
undelivered_orders = {}
|
| 5325 |
phani.kuma |
452 |
list_of_order = []
|
| 5262 |
phani.kuma |
453 |
for order in orders_tobe_delivered:
|
| 5325 |
phani.kuma |
454 |
list_of_order.append(str(order.airwaybill_no))
|
|
|
455 |
|
| 12961 |
manish.sha |
456 |
parentAwbList = [list_of_order[x:x+10] for x in xrange(0, len(list_of_order), 10)]
|
| 5262 |
phani.kuma |
457 |
|
| 12960 |
manish.sha |
458 |
for awbList in parentAwbList:
|
|
|
459 |
try:
|
|
|
460 |
uri = Delhivery_URL + 'waybill=' + ','.join(awbList)
|
|
|
461 |
data = urllib2.urlopen(uri)
|
|
|
462 |
root = parse(data).getroot()
|
|
|
463 |
shipments = root.findall('Shipment')
|
|
|
464 |
for shipment in shipments:
|
|
|
465 |
nodes = shipment.findall('Scans/ScanDetail')
|
|
|
466 |
node_number = 0
|
|
|
467 |
for element in nodes:
|
|
|
468 |
|
|
|
469 |
if element.findtext('ScanType', '') == 'DL' and getTrimedString('Delivered') in getTrimedString(element.findtext('Scan', '')) :
|
|
|
470 |
delivery_date = get_py_datetime(element.findtext('ScanDateTime', ''))
|
|
|
471 |
receiver = shipment.findtext('Status/RecievedBy', '')
|
|
|
472 |
delivered_orders[shipment.findtext('AWB', '')] = str(delivery_date) + "|" + receiver
|
|
|
473 |
break
|
|
|
474 |
elif element.findtext('ScanType', '') == 'RT' and getTrimedString(element.findtext('Scan', '')) == getTrimedString('Returned'):
|
|
|
475 |
delivery_date = get_py_datetime(element.findtext('ScanDateTime', ''))
|
|
|
476 |
reason_for_return = element.findtext('Instructions', '')
|
|
|
477 |
returned_orders[shipment.findtext('AWB', '')] = str(delivery_date) + "|" + reason_for_return
|
|
|
478 |
break
|
|
|
479 |
elif node_number == len(nodes)-1:
|
|
|
480 |
reason = element.findtext('Instructions', '')
|
|
|
481 |
undelivered_orders[shipment.findtext('AWB', '')] = reason
|
|
|
482 |
break
|
|
|
483 |
node_number = node_number + 1
|
|
|
484 |
except:
|
|
|
485 |
pass
|
|
|
486 |
|
| 5262 |
phani.kuma |
487 |
print "Delivered Orders:"
|
|
|
488 |
print delivered_orders
|
|
|
489 |
|
|
|
490 |
print "Returned Orders:"
|
|
|
491 |
print returned_orders
|
|
|
492 |
|
|
|
493 |
print "Undelivered Orders"
|
|
|
494 |
print undelivered_orders
|
|
|
495 |
|
|
|
496 |
return delivered_orders, returned_orders, undelivered_orders
|
|
|
497 |
|
|
|
498 |
def update_picked_orders(provider_id, pickup_details):
|
|
|
499 |
txnClient = TransactionClient().get_client()
|
|
|
500 |
try:
|
|
|
501 |
txnClient.markOrdersAsPickedUp(provider_id, pickup_details)
|
|
|
502 |
except TransactionServiceException as tex:
|
|
|
503 |
print tex.message
|
|
|
504 |
|
|
|
505 |
def update_picked_doas(provider_id, doa_pickup_details):
|
|
|
506 |
txnClient = TransactionClient().get_client()
|
|
|
507 |
try:
|
|
|
508 |
txnClient.markDoasAsPickedUp(provider_id, doa_pickup_details)
|
|
|
509 |
except TransactionServiceException as tex:
|
|
|
510 |
print tex.message
|
|
|
511 |
|
|
|
512 |
def update_picked_returns(provider_id, returns_pickup_details):
|
|
|
513 |
txnClient = TransactionClient().get_client()
|
|
|
514 |
try:
|
|
|
515 |
txnClient.markReturnOrdersAsPickedUp(provider_id, returns_pickup_details)
|
|
|
516 |
except TransactionServiceException as tex:
|
|
|
517 |
print tex.message
|
|
|
518 |
|
|
|
519 |
def update_delivered_orders(provider_id, delivered_orders):
|
|
|
520 |
txnClient = TransactionClient().get_client()
|
|
|
521 |
try:
|
|
|
522 |
txnClient.markOrdersAsDelivered(provider_id, delivered_orders)
|
|
|
523 |
except TransactionServiceException as tex:
|
|
|
524 |
print tex.message
|
|
|
525 |
|
|
|
526 |
def update_returned_orders(provider_id, returned_orders):
|
|
|
527 |
txnClient = TransactionClient().get_client()
|
|
|
528 |
try:
|
|
|
529 |
txnClient.markAsRTOrders(provider_id, returned_orders)
|
|
|
530 |
except TransactionServiceException as tex:
|
|
|
531 |
print tex.message
|
|
|
532 |
|
|
|
533 |
def update_reason_of_undelivered_orders(provider_id, undelivered_orders):
|
|
|
534 |
txnClient = TransactionClient().get_client()
|
|
|
535 |
try:
|
|
|
536 |
txnClient.updateNonDeliveryReason(provider_id, undelivered_orders)
|
|
|
537 |
except TransactionServiceException as tex:
|
|
|
538 |
print tex.message
|
|
|
539 |
|
|
|
540 |
def update_local_connected_orders(provider_id, local_connected_orders):
|
|
|
541 |
txnClient = TransactionClient().get_client()
|
|
|
542 |
try:
|
|
|
543 |
txnClient.markOrdersAsLocalConnected(provider_id, local_connected_orders)
|
|
|
544 |
except TransactionServiceException as tex:
|
|
|
545 |
print tex.message
|
|
|
546 |
|
|
|
547 |
def update_destination_city_reached_orders(provider_id, destination_city_reached_orders):
|
|
|
548 |
txnClient = TransactionClient().get_client()
|
|
|
549 |
try:
|
|
|
550 |
txnClient.markOrdersAsDestinationCityReached(provider_id, destination_city_reached_orders)
|
|
|
551 |
except TransactionServiceException as tex:
|
|
|
552 |
print tex.message
|
|
|
553 |
|
|
|
554 |
def update_first_atdl_orders(provider_id, first_atdl_orders):
|
|
|
555 |
txnClient = TransactionClient().get_client()
|
|
|
556 |
try:
|
|
|
557 |
txnClient.markOrdersAsFirstDeliveryAttempted(provider_id, first_atdl_orders)
|
|
|
558 |
except TransactionServiceException as tex:
|
|
|
559 |
print tex.message
|
|
|
560 |
|
|
|
561 |
def print_pickup_mismatch_report(filename, orders):
|
|
|
562 |
writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
|
|
|
563 |
writer.writerow(['Order Id', 'AWB No', 'Shipping timestamp'])
|
|
|
564 |
for order in orders:
|
|
|
565 |
writer.writerow([order.id, order.airwaybill_no, to_py_date(order.shipping_timestamp)])
|
|
|
566 |
|
|
|
567 |
def print_dao_return_pickup_mismatch_report(filename, orders):
|
|
|
568 |
writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
|
|
|
569 |
writer.writerow(['Order Id', 'Pickup Request No', 'Authorization timestamp'])
|
|
|
570 |
for order in orders:
|
|
|
571 |
writer.writerow([order.id, order.pickupRequestNo, to_py_date(order.doa_auth_timestamp)])
|
|
|
572 |
|
|
|
573 |
def print_rto_orders_report(filename, orders):
|
|
|
574 |
writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
|
|
|
575 |
writer.writerow(['Order Id', 'AWB No', 'Return date', 'Reason'])
|
|
|
576 |
for order in orders:
|
|
|
577 |
statusDescription = ''
|
|
|
578 |
if order.statusDescription is not None:
|
|
|
579 |
statusDescription = order.statusDescription.replace(","," ")
|
|
|
580 |
writer.writerow([order.id, order.airwaybill_no, to_py_date(order.delivery_timestamp), statusDescription])
|
|
|
581 |
|
|
|
582 |
def print_undelivered_orders_report(filename, orders):
|
|
|
583 |
writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
|
| 6703 |
rajveer |
584 |
writer.writerow(['Order Id', 'AWB No', 'Status', 'Status Description', 'Shipping timestamp', 'Pickup timestamp', 'Promised delivery date', 'Expected delivery date', 'OTG'])
|
| 5262 |
phani.kuma |
585 |
for order in orders:
|
|
|
586 |
statusDescription = ''
|
|
|
587 |
if order.statusDescription is not None:
|
|
|
588 |
statusDescription = order.statusDescription.replace(","," ")
|
| 6703 |
rajveer |
589 |
writer.writerow([order.id, order.airwaybill_no, order.status, statusDescription, to_py_date(order.shipping_timestamp), to_py_date(order.pickup_timestamp), to_py_date(order.promised_delivery_time), to_py_date(order.expected_delivery_time), 'True' if order.otg else 'False'])
|
| 5262 |
phani.kuma |
590 |
|
|
|
591 |
|
|
|
592 |
def auto_close_crm_tickets_created():
|
|
|
593 |
try:
|
|
|
594 |
ticket_created_orders = []
|
|
|
595 |
tickets_map = {}
|
|
|
596 |
crmServiceClient = CRMClient().get_client()
|
|
|
597 |
searchFilter = SearchFilter()
|
|
|
598 |
searchFilter.ticketCategory = TicketCategory.UNDELIVERED
|
|
|
599 |
searchFilter.ticketAssigneeIds = [defaultUndeliveredAsssigneeId]
|
|
|
600 |
searchFilter.ticketPriority = TicketPriority.HIGH
|
|
|
601 |
searchFilter.ticketStatuses = [TicketStatus.OPEN]
|
|
|
602 |
tickets = crmServiceClient.getTickets(searchFilter)
|
|
|
603 |
print tickets
|
|
|
604 |
for old_ticket in tickets:
|
|
|
605 |
ticket_created_orders.append(old_ticket.orderId)
|
|
|
606 |
tickets_map[old_ticket.orderId] = old_ticket
|
|
|
607 |
print ticket_created_orders
|
|
|
608 |
txnClient = TransactionClient().get_client()
|
|
|
609 |
orders = txnClient.getOrderList(ticket_created_orders)
|
|
|
610 |
for order in orders:
|
| 13085 |
amit.gupta |
611 |
if order.status not in [OrderStatus.FIRST_DELIVERY_ATTEMPT_MADE, OrderStatus.RTO_IN_TRANSIT]:
|
| 5262 |
phani.kuma |
612 |
old_ticket = tickets_map.get(order.id)
|
|
|
613 |
old_ticket.status = TicketStatus.CLOSED
|
|
|
614 |
activity = Activity()
|
|
|
615 |
activity.creatorId = 1
|
|
|
616 |
activity.ticketAssigneeId = old_ticket.assigneeId
|
|
|
617 |
activity.type = ActivityType.OTHER
|
|
|
618 |
activity.description = "Ticket Closed bcoz order status changed to:" + order.statusDescription
|
|
|
619 |
activity.ticketCategory = old_ticket.category
|
|
|
620 |
activity.ticketDescription = old_ticket.description
|
|
|
621 |
activity.ticketPriority = old_ticket.priority
|
|
|
622 |
activity.ticketStatus = old_ticket.status
|
|
|
623 |
|
|
|
624 |
if old_ticket.customerId is None or old_ticket.customerId == -1:
|
|
|
625 |
activity.customerEmailId = old_ticket.customerEmailId
|
|
|
626 |
activity.customerMobileNumber = old_ticket.customerMobileNumber
|
|
|
627 |
activity.customerName = old_ticket.customerName
|
|
|
628 |
else:
|
|
|
629 |
activity.customerId = old_ticket.customerId
|
|
|
630 |
|
|
|
631 |
crmServiceClient.updateTicket(old_ticket, activity)
|
|
|
632 |
except:
|
|
|
633 |
print "Some issue while closing crm tickets for orders in DELIVERY_SUCCESS status"
|
|
|
634 |
traceback.print_exc()
|
|
|
635 |
|
|
|
636 |
def get_py_datetime(time_string):
|
|
|
637 |
# This should be a command line argument.
|
|
|
638 |
# Refer http://docs.python.org/library/time.html#time.strftime to
|
|
|
639 |
# get a complete list of format specifiers available for date time.
|
|
|
640 |
time_format = "%Y-%m-%d %H:%M:%S"
|
|
|
641 |
if time_string == '':
|
|
|
642 |
return None
|
|
|
643 |
date_time_array = time_string.split('T')
|
|
|
644 |
datestring = date_time_array[0]
|
|
|
645 |
timestring = date_time_array[1]
|
|
|
646 |
if timestring == '':
|
|
|
647 |
timestring = '00:00:00'
|
|
|
648 |
else:
|
|
|
649 |
timestring = timestring.split('.')[0]
|
|
|
650 |
time_string = datestring + ' ' + timestring
|
|
|
651 |
mytime = time.strptime(time_string, time_format)
|
|
|
652 |
return datetime.datetime(*mytime[:6])
|
|
|
653 |
|
|
|
654 |
def getTrimedString(text):
|
|
|
655 |
if text is None or text == '':
|
|
|
656 |
return ''
|
|
|
657 |
else:
|
|
|
658 |
text = text.strip().lower()
|
|
|
659 |
text_list = list(text)
|
|
|
660 |
new_text = ''
|
|
|
661 |
for letter in text_list:
|
|
|
662 |
if letter.isalnum():
|
|
|
663 |
new_text = new_text + str(letter)
|
|
|
664 |
return new_text
|
|
|
665 |
|
|
|
666 |
def main():
|
|
|
667 |
parser = optparse.OptionParser()
|
|
|
668 |
parser.add_option("-p", "--pickup", dest="pickup_report",
|
|
|
669 |
action="store_true",
|
|
|
670 |
help="Run the pickup reconciliation")
|
|
|
671 |
parser.add_option("-d", "--delivery", dest="delivery_report",
|
|
|
672 |
action="store_true",
|
|
|
673 |
help="Run the delivery reconciliation")
|
|
|
674 |
parser.add_option("-r", "--reports", dest="gen_reports",
|
|
|
675 |
action="store_true",
|
|
|
676 |
help="Generate logistic reconciliation reports")
|
|
|
677 |
parser.add_option("-a", "--all", dest="all_reports",
|
|
|
678 |
action="store_true",
|
|
|
679 |
help="Run all reconciliations")
|
|
|
680 |
parser.add_option("-P", "--provider", dest="provider",
|
|
|
681 |
default="Delhivery", type="string",
|
|
|
682 |
help="The PROVIDER this report is for",
|
|
|
683 |
metavar="PROVIDER")
|
|
|
684 |
parser.set_defaults(pickup_report=False, delivery_report=False, gen_reports=False, all_reports=False)
|
|
|
685 |
(options, args) = parser.parse_args()
|
|
|
686 |
if len(args) != 0:
|
|
|
687 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
688 |
|
|
|
689 |
if options.all_reports:
|
|
|
690 |
options.pickup_report = True
|
|
|
691 |
options.delivery_report = True
|
|
|
692 |
|
|
|
693 |
provider = get_provider_by_name(options.provider)
|
|
|
694 |
|
|
|
695 |
if options.pickup_report:
|
|
|
696 |
process_pickup_records(provider)
|
|
|
697 |
process_dao_pickup_orders(provider)
|
|
|
698 |
process_return_pickup_orders(provider)
|
|
|
699 |
if options.delivery_report:
|
|
|
700 |
process_local_connection_orders(provider)
|
|
|
701 |
process_reached_destination_city_orders(provider)
|
|
|
702 |
process_first_delivery_attempt_orders(provider)
|
|
|
703 |
process_delivery_report(provider)
|
| 12961 |
manish.sha |
704 |
create_crm_tickets_for_delivey_attempted_orders(provider)
|
| 5262 |
phani.kuma |
705 |
auto_close_crm_tickets_created()
|
|
|
706 |
if options.gen_reports:
|
|
|
707 |
generate_reports(provider)
|
|
|
708 |
|
|
|
709 |
if __name__ == '__main__':
|
| 6619 |
amit.gupta |
710 |
main()
|