Rev 15657 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/python'''Send Alert mail when there is mismatch in case of store orders in OCR and our database .Created on 27-Aug-2013@author: Manish Sharma'''from suds.client import Clientfrom shop2020.clients.LogisticsClient import LogisticsClientfrom shop2020.clients.TransactionClient import TransactionClientfrom shop2020.model.v1.order.impl import DataServicefrom shop2020.thriftpy.model.v1.order.ttypes import OrderStatusfrom shop2020.utils.EmailAttachmentSender import mail, get_attachment_partfrom shop2020.model.v1.order.impl.DataService import Orderfrom datetime import date, timedeltafrom elixir import *from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \betweenimport xlwtimport datetimeimport timeif __name__ == '__main__' and __package__ is None:import sysimport ossys.path.insert(0, os.getcwd())hotspot_store_url ='http://125.19.98.100/loaddetect/service.asmx?WSDL'aclient = Nonefrom_user = 'adwords@shop2020.in'from_pwd = 'adwords_shop2020'to = ['kshitij.sood@shop2020.in','manish.sharma@shop2020.in']pickup_stores = {}pickup_stores_names = {}def get_store_account_client():global aclientif aclient is None:aclient = Client(hotspot_store_url,timeout=100)return aclientdef process_store_orders_mismatch_data():DataService.initialize('transaction','192.168.190.114')logistics_client = LogisticsClient().get_client()current_time = datetime.datetime.now()previous_time= current_time - datetime.timedelta(days=600)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))wbk = xlwt.Workbook()sheet = wbk.add_sheet('main')all_pickup_stores= logistics_client.getAllPickupStores()for pickup_st in all_pickup_stores:pickup_stores[pickup_st.id] = pickup_st.hotspotIdpickup_stores_names[pickup_st.hotspotId] = pickup_st.nameheading_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_formatsheet.write(0, 0, "Order ID", heading_xf)sheet.write(0, 1, "Store Id.", heading_xf)sheet.write(0, 2, "Store Name", heading_xf)sheet.write(0, 3, "Order Amount", heading_xf)sheet.write(0, 4, "Delivered Date/Time", heading_xf)o = Order()i = 1for o in store_orders:try:if pickup_stores.get(o.pickupStoreId)!=None:st_order = get_store_account_client().service.GetSaholicDataTransfer(pickup_stores[o.pickupStoreId],str(o.id))else:st_order = Noneexcept Exception as e:print 'Exception Caught'print eif st_order!=None:if st_order.diffgram[0]!='':continueelse:sheet.write(i, 0, o.id)sheet.write(i, 1, pickup_stores[o.pickupStoreId])sheet.write(i, 2, pickup_stores_names[pickup_stores[o.pickupStoreId]])sheet.write(i, 3, o.total_amount)sheet.write(i, 4, o.delivery_timestamp.strftime("%Y-%m-%d %H:%M:%S"))i=i+1else:sheet.write(i, 0, o.id)sheet.write(i, 1, 'Store Deleted')sheet.write(i, 2, 'Store Deleted')sheet.write(i, 3, o.total_amount)sheet.write(i, 4, o.delivery_timestamp.strftime("%Y-%m-%d %H:%M:%S"))i=i+1today = datetime.date.today()datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)filename = "/tmp/StoreMisMatchOrders-" + datestr + ".xls"wbk.save(filename)print 'file saved successfully'try:part = get_attachment_part(filename)mail(from_user, from_pwd, to, "Store Mismatch Orders as on: " + datestr, "PFA attached xls files for the store mismatch orders.", [part])finally:os.remove(filename)def main():process_store_orders_mismatch_data()if __name__ == '__main__':main()