| 8137 |
manish.sha |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
'''
|
|
|
4 |
Send Alert mail when there is mismatch in case of store orders in OCR and our database .
|
|
|
5 |
|
|
|
6 |
Created on 27-Aug-2013
|
|
|
7 |
@author: Manish Sharma
|
|
|
8 |
'''
|
|
|
9 |
from suds.client import Client
|
|
|
10 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
|
|
11 |
from shop2020.clients.TransactionClient import TransactionClient
|
|
|
12 |
from shop2020.model.v1.order.impl import DataService
|
|
|
13 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus
|
|
|
14 |
from shop2020.utils.EmailAttachmentSender import mail, get_attachment_part
|
|
|
15 |
from shop2020.model.v1.order.impl.DataService import Order
|
|
|
16 |
from datetime import date, timedelta
|
|
|
17 |
from elixir import *
|
|
|
18 |
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
|
|
|
19 |
between
|
|
|
20 |
|
|
|
21 |
import xlwt
|
|
|
22 |
import datetime
|
|
|
23 |
import time
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
if __name__ == '__main__' and __package__ is None:
|
|
|
28 |
import sys
|
|
|
29 |
import os
|
|
|
30 |
sys.path.insert(0, os.getcwd())
|
|
|
31 |
|
| 15656 |
manish.sha |
32 |
hotspot_store_url ='http://125.19.98.100/loaddetect/service.asmx?WSDL'
|
| 8137 |
manish.sha |
33 |
aclient = None
|
| 15656 |
manish.sha |
34 |
from_user = 'adwords@shop2020.in'
|
|
|
35 |
from_pwd = 'adwords_shop2020'
|
|
|
36 |
to = ['kshitij.sood@shop2020.in','manish.sharma@shop2020.in']
|
| 8137 |
manish.sha |
37 |
pickup_stores = {}
|
|
|
38 |
pickup_stores_names = {}
|
|
|
39 |
|
|
|
40 |
def get_store_account_client():
|
|
|
41 |
global aclient
|
|
|
42 |
if aclient is None:
|
|
|
43 |
aclient = Client(hotspot_store_url,timeout=100)
|
|
|
44 |
return aclient
|
|
|
45 |
|
|
|
46 |
def process_store_orders_mismatch_data():
|
|
|
47 |
DataService.initialize('transaction','192.168.190.114')
|
|
|
48 |
logistics_client = LogisticsClient().get_client()
|
|
|
49 |
current_time = datetime.datetime.now()
|
| 15658 |
manish.sha |
50 |
previous_time= current_time - datetime.timedelta(days=600)
|
| 8137 |
manish.sha |
51 |
store_orders = Order.query.filter(Order.pickupStoreId> 0).filter(Order.cod==True).filter((Order.status == OrderStatus.DELIVERY_SUCCESS)).filter(Order.delivery_timestamp.between (previous_time,current_time))
|
|
|
52 |
wbk = xlwt.Workbook()
|
|
|
53 |
sheet = wbk.add_sheet('main')
|
|
|
54 |
|
|
|
55 |
all_pickup_stores= logistics_client.getAllPickupStores()
|
|
|
56 |
for pickup_st in all_pickup_stores:
|
|
|
57 |
pickup_stores[pickup_st.id] = pickup_st.hotspotId
|
|
|
58 |
pickup_stores_names[pickup_st.hotspotId] = pickup_st.name
|
|
|
59 |
|
|
|
60 |
heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
|
|
|
61 |
sheet.set_panes_frozen(True)
|
|
|
62 |
sheet.set_horz_split_pos(1)
|
|
|
63 |
sheet.set_remove_splits(True)
|
|
|
64 |
|
|
|
65 |
excel_integer_format = '0'
|
|
|
66 |
integer_style = xlwt.XFStyle()
|
|
|
67 |
integer_style.num_format_str = excel_integer_format
|
|
|
68 |
|
|
|
69 |
sheet.write(0, 0, "Order ID", heading_xf)
|
|
|
70 |
sheet.write(0, 1, "Store Id.", heading_xf)
|
|
|
71 |
sheet.write(0, 2, "Store Name", heading_xf)
|
|
|
72 |
sheet.write(0, 3, "Order Amount", heading_xf)
|
|
|
73 |
sheet.write(0, 4, "Delivered Date/Time", heading_xf)
|
|
|
74 |
|
|
|
75 |
o = Order()
|
|
|
76 |
i = 1
|
|
|
77 |
for o in store_orders:
|
|
|
78 |
try:
|
|
|
79 |
if pickup_stores.get(o.pickupStoreId)!=None:
|
|
|
80 |
st_order = get_store_account_client().service.GetSaholicDataTransfer(pickup_stores[o.pickupStoreId],str(o.id))
|
|
|
81 |
else:
|
|
|
82 |
st_order = None
|
|
|
83 |
except Exception as e:
|
|
|
84 |
print 'Exception Caught'
|
|
|
85 |
print e
|
|
|
86 |
|
|
|
87 |
if st_order!=None:
|
|
|
88 |
if st_order.diffgram[0]!='':
|
|
|
89 |
continue
|
|
|
90 |
else:
|
|
|
91 |
sheet.write(i, 0, o.id)
|
|
|
92 |
sheet.write(i, 1, pickup_stores[o.pickupStoreId])
|
|
|
93 |
sheet.write(i, 2, pickup_stores_names[pickup_stores[o.pickupStoreId]])
|
|
|
94 |
sheet.write(i, 3, o.total_amount)
|
|
|
95 |
sheet.write(i, 4, o.delivery_timestamp.strftime("%Y-%m-%d %H:%M:%S"))
|
|
|
96 |
i=i+1
|
|
|
97 |
else:
|
|
|
98 |
sheet.write(i, 0, o.id)
|
|
|
99 |
sheet.write(i, 1, 'Store Deleted')
|
|
|
100 |
sheet.write(i, 2, 'Store Deleted')
|
|
|
101 |
sheet.write(i, 3, o.total_amount)
|
|
|
102 |
sheet.write(i, 4, o.delivery_timestamp.strftime("%Y-%m-%d %H:%M:%S"))
|
|
|
103 |
i=i+1
|
|
|
104 |
|
|
|
105 |
today = datetime.date.today()
|
|
|
106 |
datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
|
|
|
107 |
filename = "/tmp/StoreMisMatchOrders-" + datestr + ".xls"
|
|
|
108 |
wbk.save(filename)
|
|
|
109 |
print 'file saved successfully'
|
|
|
110 |
|
|
|
111 |
try:
|
|
|
112 |
part = get_attachment_part(filename)
|
|
|
113 |
mail(from_user, from_pwd, to, "Store Mismatch Orders as on: " + datestr, "PFA attached xls files for the store mismatch orders.", [part])
|
|
|
114 |
finally:
|
|
|
115 |
os.remove(filename)
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
def main():
|
|
|
120 |
process_store_orders_mismatch_data()
|
|
|
121 |
|
|
|
122 |
if __name__ == '__main__':
|
|
|
123 |
main()
|