Subversion Repositories SmartDukaan

Rev

Rev 22959 | Rev 22961 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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