Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4607 rajveer 1
#!/usr/bin/python
2
 
3
'''
4
Creates a CSV report of all the shipped orders and send mail to Sandeep.
5
 
6
Created on 20-Feb-2012
7
 
8
@author: Rajveer
9
'''
5944 mandeep.dh 10
from optparse import OptionParser
11
from shop2020.clients.InventoryClient import InventoryClient
12
from shop2020.clients.LogisticsClient import LogisticsClient
13
from shop2020.clients.TransactionClient import TransactionClient
14
from shop2020.thriftpy.model.v1.order.ttypes import Order, DelayReason
6004 rajveer 15
from shop2020.utils.EmailAttachmentSender import get_attachment_part, mail_html
5944 mandeep.dh 16
from shop2020.utils.Utils import to_py_date, to_java_date
17
import datetime
4607 rajveer 18
import xlwt
19
 
20
 
21
 
22
if __name__ == '__main__' and __package__ is None:
23
    import sys
24
    import os
25
    sys.path.insert(0, os.getcwd())
8405 amar.kumar 26
#to = ["amar.kumar@shop2020.in"]
6668 amar.kumar 27
to = ["sandeep.sachdeva@shop2020.in","amit.sirohi@shop2020.in", "chaitnaya.vats@shop2020.in", "khushal.bhatia@shop2020.in", "rajneesharora@spiceretail.co.in", "chandan.kumar@shop2020.in"]
4607 rajveer 28
 
4709 rajveer 29
def generate_delayed_orders_file():
4607 rajveer 30
    txn_client = TransactionClient().get_client()
31
    slipped_orders = txn_client.getSlippedSippingDateOrders()
32
 
33
    wbk = xlwt.Workbook()
34
    sheet = wbk.add_sheet('main')
35
 
36
    heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
37
    sheet.set_panes_frozen(True)
38
    sheet.set_horz_split_pos(1)
39
    sheet.set_remove_splits(True)
40
 
41
    excel_integer_format = '0'
42
    integer_style = xlwt.XFStyle()
43
    integer_style.num_format_str = excel_integer_format
44
 
45
    date_style = xlwt.XFStyle()
46
    date_style.num_format_str = "M/D/YY"
47
 
48
    sheet.write(0, 0, "Order ID", heading_xf)
8732 amar.kumar 49
    sheet.write(0, 1, "Source", heading_xf)
50
    sheet.write(0, 2, "Mode.", heading_xf)
51
    sheet.write(0, 3, "Brand", heading_xf)
52
    sheet.write(0, 4, "Model Name", heading_xf)
53
    sheet.write(0, 5, "Model Number", heading_xf)
54
    sheet.write(0, 6, "Colour", heading_xf)
55
    sheet.write(0, 7, "Creation Date", heading_xf)
56
    sheet.write(0, 8, "Promised Shipping Date", heading_xf)
57
    sheet.write(0, 9, "Expected Shipping Date", heading_xf)
58
    sheet.write(0, 10, "Delay Reason", heading_xf)
59
    sheet.write(0, 11, "Detailed Reason Text", heading_xf)    
5631 amar.kumar 60
 
61
    html = """\
62
    <html>
63
        <head>
64
            <style type="text/css">
65
                table, tr, th { border :1px solid black;}
66
            </style>
67
        </head>
68
        <body>
69
            <center><h3>Delayed Orders</h3></center>
70
            <table>
71
                <tr>
72
                    <th>Order ID</th>
8732 amar.kumar 73
                    <th>Source</th>
5631 amar.kumar 74
                    <th>Mode</th>
75
                    <th>Brand</th>
76
                    <th>Model Name</th>
77
                    <th>Model Number</th>
78
                    <th>Colour</th>
79
                    <th>Creation Date</th>
80
                    <th>Promised Shipping Date</th>
81
                    <th>Expected Shipping Date</th>
82
                    <th>Delay Reason</th>
83
                    <th>Detailed Reason Text</th>
84
                </tr>
85
    """  
86
 
4607 rajveer 87
    order = Order()
88
    mode = {}
89
    mode[0] = 'Prepaid'
90
    mode[1] = 'COD'
8732 amar.kumar 91
    source = {}
92
    source[1] = 'WEBSITE'
93
    source[2] = 'STORE'
94
    source[3] = 'AMAZON'
95
    source[4] = 'HOMESHOP18'
96
    source[5] = 'NAAPTOL'
97
    source[6] = 'EBAY'
98
    source[7] = 'SNAPDEAL'
9134 vikram.rag 99
    source[8] = 'FLIPKART'
8732 amar.kumar 100
    source[9] = 'UNKNOWN'
101
    source[10] = 'UNKNOWN'
4607 rajveer 102
 
103
    i = 1
104
    for order in slipped_orders:
8732 amar.kumar 105
        #if order.source ==6:
106
            #continue
5631 amar.kumar 107
        html +="<tr>"
108
        sheet.write(i, 0, order.id)
109
        html +="<td>" + str(order.id) + "</td>" 
8732 amar.kumar 110
        sheet.write(i, 1, source[order.source])
111
        html +="<td>" + str(source[order.source]) + "</td>"
112
        sheet.write(i, 2, mode[order.cod])
5631 amar.kumar 113
        html +="<td>" + str(mode[order.cod]) + "</td>"
4607 rajveer 114
        lineitem = order.lineitems[0]
8732 amar.kumar 115
        sheet.write(i, 3, lineitem.brand)
5631 amar.kumar 116
        html +="<td>" + str(lineitem.brand) + "</td>"
8732 amar.kumar 117
        sheet.write(i, 4, lineitem.model_name)
5631 amar.kumar 118
        html +="<td>" + str(lineitem.model_name) + "</td>"
8732 amar.kumar 119
        sheet.write(i, 5, lineitem.model_number)
5631 amar.kumar 120
        html +="<td>" + str(lineitem.model_number) + "</td>"
8732 amar.kumar 121
        sheet.write(i, 6, lineitem.color)
5631 amar.kumar 122
        html +="<td>" + str(lineitem.color) + "</td>"
8732 amar.kumar 123
        sheet.write(i, 7, to_py_date(order.created_timestamp), date_style)
5631 amar.kumar 124
        html +="<td>" + str(to_py_date(order.created_timestamp)) + "</td>"
8732 amar.kumar 125
        sheet.write(i, 8, to_py_date(order.promised_shipping_time), date_style)
5631 amar.kumar 126
        html +="<td>" + str(to_py_date(order.promised_shipping_time)) + "</td>"
8732 amar.kumar 127
        sheet.write(i, 9, to_py_date(order.expected_shipping_time), date_style)
5631 amar.kumar 128
        html +="<td>" + str(to_py_date(order.expected_shipping_time)) + "</td>"
4635 rajveer 129
        if order.delayReason:
8732 amar.kumar 130
            sheet.write(i, 10, DelayReason._VALUES_TO_NAMES[order.delayReason])
5631 amar.kumar 131
            html +="<td>" + str(DelayReason._VALUES_TO_NAMES[order.delayReason]) + "</td>"
8732 amar.kumar 132
        sheet.write(i, 11, order.delayReasonText)
5631 amar.kumar 133
        html +="<td>" + str(order.delayReasonText) + "</td>"
134
        html +="</tr>"
4607 rajveer 135
        i= i+1
136
 
137
    today = datetime.date.today()
138
    datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
139
    filename = "/tmp/SlippedOrders-" + datestr + ".xls" 
140
    wbk.save(filename)
5631 amar.kumar 141
    html +="</table></body></html>"
4607 rajveer 142
    try:
143
        part = get_attachment_part(filename)
5631 amar.kumar 144
        mail_html("cnc.center@shop2020.in", "5h0p2o2o", to, "Slipped Orders as on: " + datestr, html, [part])
4607 rajveer 145
    finally:
146
        os.remove(filename)
147
 
4709 rajveer 148
def generate_cancelled_orders_file():
149
    txn_client = TransactionClient().get_client()
150
    #cancelled_orders = txn_client.getAllOrders(OrderStatus.CANCELLED_DUE_TO_LOW_INVENTORY, to_java_date(datetime.datetime.now() - datetime.timedelta(hours = 24)), to_java_date(datetime.datetime.now()), -1)
151
    cancelled_orders = txn_client.getCancelledOrders(to_java_date(datetime.datetime.now() - datetime.timedelta(hours = 24)), to_java_date(datetime.datetime.now()))
152
 
153
    wbk = xlwt.Workbook()
154
    sheet = wbk.add_sheet('main')
155
 
156
    heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
157
    sheet.set_panes_frozen(True)
158
    sheet.set_horz_split_pos(1)
159
    sheet.set_remove_splits(True)
160
 
161
    excel_integer_format = '0'
162
    integer_style = xlwt.XFStyle()
163
    integer_style.num_format_str = excel_integer_format
164
 
165
    date_style = xlwt.XFStyle()
166
    date_style.num_format_str = "M/D/YY"
167
 
168
    sheet.write(0, 0, "Order ID", heading_xf)
169
    sheet.write(0, 1, "Mode.", heading_xf)
8403 amar.kumar 170
    sheet.write(0, 2, "Source", heading_xf)
171
    sheet.write(0, 3, "Brand", heading_xf)
172
    sheet.write(0, 4, "Model Name", heading_xf)
173
    sheet.write(0, 5, "Model Number", heading_xf)
174
    sheet.write(0, 6, "Colour", heading_xf)
175
    sheet.write(0, 7, "Creation Date", heading_xf)
176
    sheet.write(0, 8, "Promised Shipping Date", heading_xf)
177
    sheet.write(0, 9, "Expected Shipping Date", heading_xf)
178
    sheet.write(0, 10, "Cancellation Reason", heading_xf)
179
    sheet.write(0, 11, "Cancellation Date", heading_xf)    
4709 rajveer 180
 
5631 amar.kumar 181
    html = """\
182
    <html>
183
        <head>
184
            <style type="text/css">
185
                table, tr, th { border :1px solid black;}
186
            </style>
187
        </head>
188
        <body>
9134 vikram.rag 189
            <center><h3>Low Inventory Cancelled Orders</h3></center>
5631 amar.kumar 190
            <table>
191
                <tr>
192
                    <th>Order ID</th>
193
                    <th>Mode</th>
8403 amar.kumar 194
                    <th>Source</th>
5631 amar.kumar 195
                    <th>Brand</th>
196
                    <th>Model Name</th>
197
                    <th>Model Number</th>
198
                    <th>Colour</th>
199
                    <th>Creation Date</th>
200
                    <th>Promised Shipping Date</th>
201
                    <th>Expected Shipping Date</th>
202
                    <th>Cancellation Reason</th>
203
                    <th>Cancellation Date</th>
204
                </tr>
205
    """
206
 
207
 
4709 rajveer 208
    order = Order()
209
    mode = {}
210
    mode[0] = 'Prepaid'
211
    mode[1] = 'COD'
8403 amar.kumar 212
    source = {}
213
    source[1] = 'WEBSITE'
214
    source[2] = 'STORE'
215
    source[3] = 'AMAZON'
216
    source[4] = 'HOMESHOP18'
217
    source[5] = 'NAAPTOL'
218
    source[6] = 'EBAY'
219
    source[7] = 'SNAPDEAL'
9134 vikram.rag 220
    source[8] = 'FLIPKART'
8403 amar.kumar 221
    source[9] = 'UNKNOWN'
222
    source[10] = 'UNKNOWN'
4709 rajveer 223
 
224
    i = 1
225
    for order in cancelled_orders:
5631 amar.kumar 226
        html +="<tr>"
4709 rajveer 227
        sheet.write(i, 0, order.id) 
5631 amar.kumar 228
        html +="<td>" + str(order.id) + "</td>" 
4709 rajveer 229
        sheet.write(i, 1, mode[order.cod])
5631 amar.kumar 230
        html +="<td>" + str(mode[order.cod]) + "</td>"
8404 amar.kumar 231
        sheet.write(i, 2, source[order.source])
232
        html +="<td>" + str(source[order.source]) + "</td>"
4709 rajveer 233
        lineitem = order.lineitems[0]
8403 amar.kumar 234
        sheet.write(i, 3, lineitem.brand)
5631 amar.kumar 235
        html +="<td>" + str(lineitem.brand) + "</td>"
8403 amar.kumar 236
        sheet.write(i, 4, lineitem.model_name)
5631 amar.kumar 237
        html +="<td>" + str(lineitem.model_name) + "</td>"
8403 amar.kumar 238
        sheet.write(i, 5, lineitem.model_number)
5631 amar.kumar 239
        html +="<td>" + str(lineitem.model_number) + "</td>"
8403 amar.kumar 240
        sheet.write(i, 6, lineitem.color)
5631 amar.kumar 241
        html +="<td>" + str(lineitem.color) + "</td>"
4709 rajveer 242
 
8403 amar.kumar 243
        sheet.write(i, 7, to_py_date(order.created_timestamp), date_style)
5631 amar.kumar 244
        html +="<td>" + str(to_py_date(order.created_timestamp)) + "</td>"
8403 amar.kumar 245
        sheet.write(i, 8, to_py_date(order.promised_shipping_time), date_style)
5631 amar.kumar 246
        html +="<td>" + str(to_py_date(order.promised_shipping_time)) + "</td>"
8403 amar.kumar 247
        sheet.write(i, 9, to_py_date(order.expected_shipping_time), date_style)
5631 amar.kumar 248
        html +="<td>" + str(to_py_date(order.expected_shipping_time)) + "</td>"
8403 amar.kumar 249
        sheet.write(i, 10, order.refundReason)
5631 amar.kumar 250
        html +="<td>" + str(order.refundReason) + "</td>"
8403 amar.kumar 251
        sheet.write(i, 11, to_py_date(order.refund_timestamp), date_style)
5631 amar.kumar 252
        html +="<td>" + str(to_py_date(order.refund_timestamp)) + "</td>"
253
        html +="<td>" + str(order.delayReasonText) + "</td>"
254
        html +="</tr>"
4709 rajveer 255
 
256
        i= i+1
257
 
258
    today = datetime.date.today()
259
    datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
260
    filename = "/tmp/CancelledOrders-" + datestr + ".xls" 
261
    wbk.save(filename)
5631 amar.kumar 262
    html +="</table></body></html>"
4709 rajveer 263
 
264
    try:
265
        part = get_attachment_part(filename)
5631 amar.kumar 266
        mail_html("cnc.center@shop2020.in", "5h0p2o2o", to, "Low Inventory Cancelled Orders as on: " + datestr, html, [part])
4709 rajveer 267
    finally:
268
        os.remove(filename)
4783 phani.kuma 269
 
270
def generate_expected_delivery_missed_orders_file():
271
 
272
    txn_client = TransactionClient().get_client()
273
    orders_not_delivered = txn_client.getUndeliveredOrdersExpectedDeliveryDateNotMet()
274
 
275
    print "orders_not_delivered"
276
    print orders_not_delivered
277
 
278
    logistics_client = LogisticsClient().get_client()
279
    providers = logistics_client.getAllProviders()
280
    providers_map = {}
281
    for provider in providers:
282
        providers_map[provider.id] = provider.name
283
 
5944 mandeep.dh 284
    inventory_client = InventoryClient().get_client()
285
    warehouses = inventory_client.getAllWarehouses(True)
4783 phani.kuma 286
    warehouses_map = {}
287
    for warehouse in warehouses:
288
        warehouses_map[warehouse.id] = warehouse.displayName
289
 
290
    wbk = xlwt.Workbook()
291
    sheet = wbk.add_sheet('main')
292
 
293
    heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
294
    sheet.set_panes_frozen(True)
295
    sheet.set_horz_split_pos(1)
296
    sheet.set_remove_splits(True)
297
 
298
    excel_integer_format = '0'
299
    integer_style = xlwt.XFStyle()
300
    integer_style.num_format_str = excel_integer_format
301
 
302
    date_style = xlwt.XFStyle()
303
    date_style.num_format_str = "M/D/YY"
304
 
305
    sheet.write(0, 0, "Order ID", heading_xf)
306
    sheet.write(0, 1, "Mode.", heading_xf)
8403 amar.kumar 307
    sheet.write(0, 2, "Source", heading_xf)
308
    sheet.write(0, 3, "Airway BillNo", heading_xf)
309
    sheet.write(0, 4, "Current Status", heading_xf)
310
    sheet.write(0, 5, "Shipping Date", heading_xf)
311
    sheet.write(0, 6, "Pickup Date", heading_xf)
312
    sheet.write(0, 7, "Expected Delivery Date", heading_xf)
313
    sheet.write(0, 8, "Warehouse", heading_xf)
314
    sheet.write(0, 9, "Warehouse ID", heading_xf)
315
    sheet.write(0, 10, "Logistics Provider", heading_xf)
316
    sheet.write(0, 11, "Logistics Provider ID", heading_xf)
317
    sheet.write(0, 12, "Customer City", heading_xf)
318
    sheet.write(0, 13, "Customer State", heading_xf)
319
    sheet.write(0, 14, "Customer Pincode", heading_xf)
4783 phani.kuma 320
 
5631 amar.kumar 321
    html = """\
322
    <html>
323
        <head>
324
            <style type="text/css">
325
                table, tr, th { border :1px solid black;}
326
            </style>
327
        </head>
328
        <body>
329
            <center><h3>Delayed Orders</h3></center>
330
            <table border=1>
331
                <tr>
332
                    <th>Order ID</th>
333
                    <th>Mode</th>
8403 amar.kumar 334
                    <th>Source</th>
5631 amar.kumar 335
                    <th>Airway BillNo</th>
336
                    <th>Current Status</th>
337
                    <th>Shipping Date</th>
338
                    <th>Pickup Date</th>
339
                    <th>Expected Delivery Date</th>
340
                    <th>Warehouse</th>
341
                    <th>Warehouse ID</th>
342
                    <th>Logistics Provider</th>
343
                    <th>Logistics Provider ID</th>
344
                    <th>Customer City</th>
345
                    <th>Customer State</th>
346
                    <th>Customer Pincode</th>
347
                </tr>
348
    """  
349
 
4783 phani.kuma 350
    order = Order()
351
    mode = {}
352
    mode[0] = 'Prepaid'
353
    mode[1] = 'COD'
354
 
8403 amar.kumar 355
    source = {}
356
    source[1] = 'WEBSITE'
357
    source[2] = 'STORE'
358
    source[3] = 'AMAZON'
359
    source[4] = 'HOMESHOP18'
360
    source[5] = 'NAAPTOL'
361
    source[6] = 'EBAY'
362
    source[7] = 'SNAPDEAL'
9134 vikram.rag 363
    source[8] = 'FLIPKART'
8403 amar.kumar 364
    source[9] = 'UNKNOWN'
365
    source[10] = 'UNKNOWN'
366
 
4783 phani.kuma 367
    i = 1
368
    for order in orders_not_delivered:
5631 amar.kumar 369
        html +="<tr>"
4783 phani.kuma 370
        sheet.write(i, 0, order.id) 
5631 amar.kumar 371
        html +="<td>" + str(order.id) + "</td>"
4783 phani.kuma 372
        sheet.write(i, 1, mode[order.cod])
5631 amar.kumar 373
        html +="<td>" + str(mode[order.cod]) + "</td>"
8404 amar.kumar 374
        sheet.write(i, 2, source[order.source])
375
        html +="<td>" + str(source[order.source]) + "</td>"
8403 amar.kumar 376
        sheet.write(i, 3, order.airwaybill_no)
5631 amar.kumar 377
        html +="<td>" + str(order.airwaybill_no) + "</td>"
8403 amar.kumar 378
        sheet.write(i, 4, order.statusDescription)
5631 amar.kumar 379
        html +="<td>" + order.statusDescription + "</td>"
8403 amar.kumar 380
        sheet.write(i, 5, to_py_date(order.shipping_timestamp), date_style)
5631 amar.kumar 381
        html +="<td>" + str(to_py_date(order.shipping_timestamp)) + "</td>"
8403 amar.kumar 382
        sheet.write(i, 6, to_py_date(order.pickup_timestamp), date_style)
5631 amar.kumar 383
        html +="<td>" + str(to_py_date(order.pickup_timestamp)) + "</td>"
8403 amar.kumar 384
        sheet.write(i, 7, to_py_date(order.expected_delivery_time), date_style)
5631 amar.kumar 385
        html +="<td>" + str(to_py_date(order.expected_delivery_time)) + "</td>"
8403 amar.kumar 386
        sheet.write(i, 8, warehouses_map.get(order.warehouse_id))
5631 amar.kumar 387
        html +="<td>" + str(warehouses_map.get(order.warehouse_id)) + "</td>"
8403 amar.kumar 388
        sheet.write(i, 9, order.warehouse_id)
5631 amar.kumar 389
        html +="<td>" + str(order.warehouse_id) + "</td>"
8403 amar.kumar 390
        sheet.write(i, 10, providers_map.get(order.logistics_provider_id))
5631 amar.kumar 391
        html +="<td>" + str(providers_map.get(order.logistics_provider_id)) + "</td>"
8403 amar.kumar 392
        sheet.write(i, 11, order.logistics_provider_id)
5631 amar.kumar 393
        html +="<td>" + str(order.logistics_provider_id) + "</td>"
8403 amar.kumar 394
        sheet.write(i, 12, order.customer_city)
5631 amar.kumar 395
        html +="<td>" + str(order.customer_city) + "</td>"
8403 amar.kumar 396
        sheet.write(i, 13, order.customer_state)
5631 amar.kumar 397
        html +="<td>" + str(order.customer_state) + "</td>"
8403 amar.kumar 398
        sheet.write(i, 14, order.customer_pincode)
5631 amar.kumar 399
        html +="<td>" + str(order.customer_pincode) + "</td>"
4709 rajveer 400
 
5631 amar.kumar 401
        html +="</tr>"
4783 phani.kuma 402
        i= i+1
403
 
404
    today = datetime.date.today()
405
    datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
406
    filename = "/tmp/UndeliveredOrders-" + datestr + ".xls" 
407
    wbk.save(filename)
5631 amar.kumar 408
    html +="</table></body></html>"
4783 phani.kuma 409
 
410
    try:
411
        part = get_attachment_part(filename)
5631 amar.kumar 412
        mail_html("cnc.center@shop2020.in", "5h0p2o2o", to, "Orders which missed Expected Delivery Date as on: " + datestr, html, [part])
4783 phani.kuma 413
    finally:
414
        os.remove(filename)
415
 
4709 rajveer 416
def main():
417
    parser = OptionParser()
418
    parser.add_option("-d", "--delayed", dest="delayed",
419
                      action="store_true",
420
                      help="")
421
    parser.add_option("-c", "--cancelled", dest="cancelled",
422
                      action="store_true",
423
                      help="")
4783 phani.kuma 424
    parser.add_option("-u", "--undelivered", dest="undelivered",
425
                      action="store_true",
426
                      help="")
4709 rajveer 427
 
428
    (options, args) = parser.parse_args()
429
    if options.delayed:
430
        generate_delayed_orders_file()
431
    if options.cancelled:
432
        generate_cancelled_orders_file()
4783 phani.kuma 433
    if options.undelivered:
434
        generate_expected_delivery_missed_orders_file()
435
 
4607 rajveer 436
if __name__ == '__main__':
437
    main()