Subversion Repositories SmartDukaan

Rev

Rev 1408 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1405 ankur.sing 1
#!/usr/bin/python 
4090 chandransh 2
'''
3
Mails a report of orders which should have been delivered
4
but have been not. It is scheduled to run after the LogisticsReconciliation.
1405 ankur.sing 5
 
4090 chandransh 6
@author: Chandranshu
7
'''
1405 ankur.sing 8
import os
9
import sys
1408 ankur.sing 10
import csv
1405 ankur.sing 11
 
12
if __name__ == '__main__' and __package__ is None:
13
    sys.path.insert(0, os.getcwd())
14
 
1408 ankur.sing 15
from shop2020.utils.Utils import to_py_date
1405 ankur.sing 16
from shop2020.utils.EmailAttachmentSender import get_attachment_part, mail
17
from shop2020.clients.TransactionClient import TransactionClient
18
from shop2020.clients.LogisticsClient import LogisticsClient
19
 
20
from_user = 'cnc.center@shop2020.in'
21
from_pwd = '5h0p2o2o'
4090 chandransh 22
to = ['cnc.center@shop2020.in']
1405 ankur.sing 23
 
24
def get_undelivered_orders(providerId = -1, warehouseId = -1):
25
    txnClient = TransactionClient().get_client()
26
    undeliveredOrders = txnClient.getUndeliveredOrders(providerId, warehouseId)
27
    return undeliveredOrders
28
 
29
def generate_undelivered_orders_report(orders):
30
    filename = os.getenv("HOME") + os.sep + "UndeliveredOrders.csv"
31
    logistics_client = LogisticsClient().get_client()
32
    providers = logistics_client.getAllProviders()
33
    writer = csv.writer(open(filename, "wb"), delimiter=',', quoting=csv.QUOTE_NONE)
34
    writer.writerow(['AWB No', 'Shipping Date', 'Courier Name', 'Delivery Location', 'Non-delivery Reason'])
35
    provider = None
36
    for order in orders:
37
        for p in providers:
38
            if p.id == order.logistics_provider_id:
39
                provider=p
40
                break
41
        writer.writerow([order.airwaybill_no, to_py_date(order.shipping_timestamp), provider.name, order.customer_city, order.statusDescription])
42
 
43
    return filename
44
 
45
 
46
def main():
47
    orders = get_undelivered_orders()
48
    filename = generate_undelivered_orders_report(orders)
49
    print "Generated undelivered orders report to:" + filename
50
    part = get_attachment_part(filename)
51
    mail(from_user, from_pwd, to, "Undelivered orders", "This is a system generated email.Please don't reply to it.", part)
52
 
53
if __name__ == '__main__':
54
    main()