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