Subversion Repositories SmartDukaan

Rev

Rev 4607 | Rev 4636 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python

'''
Creates a CSV report of all the shipped orders and send mail to Sandeep.

Created on 20-Feb-2012

@author: Rajveer
'''
import xlwt
import datetime



if __name__ == '__main__' and __package__ is None:
    import sys
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.utils.Utils import to_py_date
from shop2020.clients.TransactionClient import TransactionClient
from shop2020.thriftpy.model.v1.order.ttypes import  Order, DelayReason
from shop2020.utils.EmailAttachmentSender import mail, get_attachment_part

def main():
    txn_client = TransactionClient().get_client()
    slipped_orders = txn_client.getSlippedSippingDateOrders()
    
    wbk = xlwt.Workbook()
    sheet = wbk.add_sheet('main')

    heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
    sheet.set_panes_frozen(True)
    sheet.set_horz_split_pos(1)
    sheet.set_remove_splits(True)
    
    excel_integer_format = '0'
    integer_style = xlwt.XFStyle()
    integer_style.num_format_str = excel_integer_format

    date_style = xlwt.XFStyle()
    date_style.num_format_str = "M/D/YY"
    
    sheet.write(0, 0, "Order ID", heading_xf)
    sheet.write(0, 1, "Mode.", heading_xf)
    sheet.write(0, 2, "Brand", heading_xf)
    sheet.write(0, 3, "Model Name", heading_xf)
    sheet.write(0, 4, "Model Number", heading_xf)
    sheet.write(0, 5, "Color", heading_xf)
    sheet.write(0, 6, "Creation Date", heading_xf)
    sheet.write(0, 7, "Promised Shipping Date", heading_xf)
    sheet.write(0, 8, "Expected Shipping Date", heading_xf)
    sheet.write(0, 9, "Delay Reason", heading_xf)
        

    order = Order()
    mode = {}
    mode[0] = 'Prepaid'
    mode[1] = 'COD'
    
    i = 1
    for order in slipped_orders:
        sheet.write(i, 0, order.id) 
        sheet.write(i, 1, mode[order.cod])
        
        lineitem = order.lineitems[0]
        sheet.write(i, 2, lineitem.brand)
        sheet.write(i, 3, lineitem.model_name)
        sheet.write(i, 4, lineitem.model_number)
        sheet.write(i, 5, lineitem.color)
        
        sheet.write(i, 6, to_py_date(order.created_timestamp), date_style)
        sheet.write(i, 7, to_py_date(order.promised_shipping_time), date_style)
        sheet.write(i, 8, to_py_date(order.expected_shipping_time), date_style)
        if order.delayReason:
            sheet.write(i, 9, DelayReason._VALUES_TO_NAMES[order.delayReason])
        
        i= i+1
    
    today = datetime.date.today()
    datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
    filename = "/tmp/SlippedOrders-" + datestr + ".xls" 
    wbk.save(filename)
    
    try:
        part = get_attachment_part(filename)
        mail("cnc.center@shop2020.in", "5h0p2o2o", ["sandeep.sachdeva@shop2020.in","suraj.sharma@shop2020.in","pankaj.jain@spiceglobal.com", "pankaj.kankar@shop2020.in"], "Slipped Orders as on: " + datestr, "PFA attached xls files for the orders which missed the shipping date.", [part])
    finally:
        os.remove(filename)

if __name__ == '__main__':
    main()