| 6908 |
rajveer |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
from elixir import *
|
|
|
4 |
from datetime import datetime, timedelta, date
|
|
|
5 |
import sys
|
|
|
6 |
from shop2020.model.v1.order.impl import DataService
|
|
|
7 |
from shop2020.model.v1.order.impl.DataAccessors import move_orders_to_correct_warehouse
|
| 6964 |
rajveer |
8 |
from shop2020.model.v1.order.impl.DataService import InsuranceDetailForOrder,\
|
|
|
9 |
Order
|
| 6908 |
rajveer |
10 |
import xlwt
|
|
|
11 |
from shop2020.utils import EmailAttachmentSender
|
|
|
12 |
from shop2020.utils.EmailAttachmentSender import get_attachment_part
|
| 6909 |
rajveer |
13 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus
|
| 6962 |
rajveer |
14 |
from shop2020.clients.CatalogClient import CatalogClient
|
| 8549 |
kshitij.so |
15 |
from suds.client import Client
|
|
|
16 |
from xml.dom.minidom import parseString
|
|
|
17 |
import logging
|
| 8605 |
kshitij.so |
18 |
import re
|
| 6908 |
rajveer |
19 |
|
| 6964 |
rajveer |
20 |
def delivered_orders():
|
| 6974 |
rajveer |
21 |
DataService.initialize(db_hostname="192.168.190.114")
|
| 6908 |
rajveer |
22 |
todate = datetime.now()
|
|
|
23 |
todate = todate-timedelta(days = 10)
|
|
|
24 |
insurances = InsuranceDetailForOrder.query.filter(InsuranceDetailForOrder.isDeclared == False).filter(InsuranceDetailForOrder.startDate >= todate).all()
|
|
|
25 |
if not insurances:
|
|
|
26 |
return
|
|
|
27 |
|
|
|
28 |
wbk = xlwt.Workbook()
|
|
|
29 |
sheet = wbk.add_sheet('main')
|
|
|
30 |
|
| 6941 |
rajveer |
31 |
date_style = xlwt.XFStyle()
|
|
|
32 |
date_style.num_format_str = "D/M/YY"
|
|
|
33 |
|
| 6908 |
rajveer |
34 |
heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
|
|
|
35 |
sheet.set_panes_frozen(True)
|
|
|
36 |
sheet.set_horz_split_pos(1)
|
|
|
37 |
sheet.set_remove_splits(True)
|
|
|
38 |
|
|
|
39 |
excel_integer_format = '0'
|
|
|
40 |
integer_style = xlwt.XFStyle()
|
|
|
41 |
integer_style.num_format_str = excel_integer_format
|
|
|
42 |
|
|
|
43 |
sheet.write(0, 0, "Riskstartdate", heading_xf)
|
|
|
44 |
sheet.write(0, 1, "StoreId", heading_xf)
|
|
|
45 |
sheet.write(0, 2, "SystemInvoiceNumber", heading_xf)
|
|
|
46 |
sheet.write(0, 3, "CustomerName", heading_xf)
|
|
|
47 |
sheet.write(0, 4, "Type", heading_xf)
|
|
|
48 |
sheet.write(0, 5, "Make", heading_xf)
|
|
|
49 |
sheet.write(0, 6, "Model", heading_xf)
|
|
|
50 |
sheet.write(0, 7, "EquipmentIdentificationNumber", heading_xf)
|
|
|
51 |
sheet.write(0, 8, "Qty", heading_xf)
|
|
|
52 |
sheet.write(0, 9, "Rate", heading_xf)
|
|
|
53 |
sheet.write(0, 10, "TaxAmount", heading_xf)
|
|
|
54 |
sheet.write(0, 11, "NetSales", heading_xf)
|
|
|
55 |
sheet.write(0, 12, "FatherName", heading_xf)
|
|
|
56 |
sheet.write(0, 13, "Birthdate", heading_xf)
|
|
|
57 |
sheet.write(0, 14, "SystemInvoiceDate.", heading_xf)
|
|
|
58 |
sheet.write(0, 15, "OrderId", heading_xf)
|
|
|
59 |
sheet.write(0, 16, "OtherDetails3", heading_xf)
|
|
|
60 |
|
|
|
61 |
xstr = lambda s: s or ""
|
|
|
62 |
|
| 6962 |
rajveer |
63 |
declaredAmount= 0.0
|
| 6908 |
rajveer |
64 |
i = 1
|
|
|
65 |
for insurance in insurances:
|
|
|
66 |
order = insurance.order
|
| 6909 |
rajveer |
67 |
if order.status != OrderStatus.DELIVERY_SUCCESS:
|
|
|
68 |
continue
|
| 6908 |
rajveer |
69 |
line = order.lineitems[0]
|
|
|
70 |
insurance.isDeclared = True
|
| 6962 |
rajveer |
71 |
declaredAmount += line.total_price
|
| 6908 |
rajveer |
72 |
|
| 6941 |
rajveer |
73 |
sheet.write(i, 0, insurance.startDate, date_style)
|
| 6908 |
rajveer |
74 |
sheet.write(i, 1, '')
|
|
|
75 |
sheet.write(i, 2, order.invoice_number)
|
|
|
76 |
sheet.write(i, 3, order.customer_name)
|
|
|
77 |
sheet.write(i, 4, 'Mobile Phones')
|
|
|
78 |
sheet.write(i, 5, line.brand)
|
|
|
79 |
sheet.write(i, 6, xstr(line.model_name) + " " + xstr(line.model_number))
|
|
|
80 |
sheet.write(i, 7, line.serial_number)
|
|
|
81 |
sheet.write(i, 8, line.quantity)
|
|
|
82 |
sheet.write(i, 9, line.unit_price)
|
|
|
83 |
sheet.write(i, 10, "0")
|
|
|
84 |
sheet.write(i, 11, line.total_price)
|
|
|
85 |
sheet.write(i, 12, insurance.guardianName)
|
|
|
86 |
sheet.write(i, 13, insurance.dob)
|
| 6941 |
rajveer |
87 |
sheet.write(i, 14, order.billing_timestamp, date_style)
|
| 6908 |
rajveer |
88 |
sheet.write(i, 15, order.id)
|
|
|
89 |
sheet.write(i, 16, '')
|
|
|
90 |
i = i + 1
|
|
|
91 |
|
|
|
92 |
session.commit()
|
| 6962 |
rajveer |
93 |
client = CatalogClient().get_client()
|
|
|
94 |
client.updateInsuranceDeclaredAmount(1, declaredAmount)
|
|
|
95 |
|
| 6908 |
rajveer |
96 |
today = date.today()
|
|
|
97 |
datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
|
|
|
98 |
filename = "/tmp/insurance-" + datestr + ".xls"
|
|
|
99 |
wbk.save(filename)
|
| 11954 |
kshitij.so |
100 |
EmailAttachmentSender.mail("cnc.center@shop2020.in", "5h0p2o2o", ["murthy@ubiassociates.com","kashi@ubiassociates.com"], "Insurance Details for date " + datestr, "Please find attached insurance details for today.", [get_attachment_part(filename)], ["pramit.singh@shop2020.in","anikendra.das@shop2020.in","kshitij.sood@shop2020.in","rajneesh.arora@shop2020.in"], [])
|
| 6964 |
rajveer |
101 |
|
| 7177 |
rajveer |
102 |
insurer = client.getInsurer(1)
|
| 11954 |
kshitij.so |
103 |
EmailAttachmentSender.mail("cnc.center@shop2020.in", "5h0p2o2o", ["pramit.singh@shop2020.in","anikendra.das@shop2020.in","kshitij.sood@shop2020.in","rajneesh.arora@shop2020.in", "amit.tyagi@spiceretail.co.in", "pardeep.panwar@spiceretail.co.in", "j.p.gupta@shop2020.in"], "Declaration Status for insurer " + insurer.name + " as on " + datestr + " : (Declared Amount - " + str(insurer.declaredAmount) + ") (Total Amount - " + str(insurer.creditedAmount), "", [], [], [])
|
| 7177 |
rajveer |
104 |
|
| 6964 |
rajveer |
105 |
def returned_orders():
|
| 6974 |
rajveer |
106 |
DataService.initialize(db_hostname="192.168.190.114")
|
| 6964 |
rajveer |
107 |
todate = datetime.now()
|
|
|
108 |
todate = todate-timedelta(days = 2)
|
|
|
109 |
orders = Order.query.filter(Order.insurer > 0).filter(Order.received_return_timestamp >= todate).all()
|
|
|
110 |
|
|
|
111 |
if not orders:
|
|
|
112 |
return
|
|
|
113 |
|
|
|
114 |
wbk = xlwt.Workbook()
|
|
|
115 |
sheet = wbk.add_sheet('main')
|
|
|
116 |
|
|
|
117 |
date_style = xlwt.XFStyle()
|
|
|
118 |
date_style.num_format_str = "D/M/YY"
|
|
|
119 |
|
|
|
120 |
heading_xf = xlwt.easyxf('font: bold on; align: wrap on, vert centre, horiz center')
|
|
|
121 |
sheet.set_panes_frozen(True)
|
|
|
122 |
sheet.set_horz_split_pos(1)
|
|
|
123 |
sheet.set_remove_splits(True)
|
|
|
124 |
|
|
|
125 |
excel_integer_format = '0'
|
|
|
126 |
integer_style = xlwt.XFStyle()
|
|
|
127 |
integer_style.num_format_str = excel_integer_format
|
|
|
128 |
|
|
|
129 |
sheet.write(0, 0, "SalesReturnDate", heading_xf)
|
|
|
130 |
sheet.write(0, 1, "SalesReturnNumber", heading_xf)
|
|
|
131 |
sheet.write(0, 2, "CustomerName", heading_xf)
|
|
|
132 |
sheet.write(0, 3, "EquipmentIdentificationNumber", heading_xf)
|
|
|
133 |
sheet.write(0, 4, "SystemInvoiceNumber", heading_xf)
|
|
|
134 |
sheet.write(0, 5, "Rate", heading_xf)
|
|
|
135 |
sheet.write(0, 6, "Amount", heading_xf)
|
| 7154 |
rajveer |
136 |
sheet.write(0, 7, "OrderId", heading_xf)
|
| 6964 |
rajveer |
137 |
|
|
|
138 |
xstr = lambda s: s or ""
|
|
|
139 |
|
|
|
140 |
declaredAmount= 0.0
|
|
|
141 |
i = 1
|
|
|
142 |
for order in orders:
|
|
|
143 |
insurance = order.insuranceDetails[0]
|
| 7018 |
rajveer |
144 |
if not (insurance.startDate or insurance.expiryDate) or insurance.expiryDate < datetime.now():
|
| 6964 |
rajveer |
145 |
continue
|
|
|
146 |
|
|
|
147 |
line = order.lineitems[0]
|
|
|
148 |
insurance.expiryDate = order.received_return_timestamp
|
|
|
149 |
|
|
|
150 |
insured_days = ((order.received_return_timestamp).date() - ((insurance.startDate).date())).days
|
|
|
151 |
|
|
|
152 |
declaredAmount += line.total_price/365*(365-insured_days)
|
|
|
153 |
|
|
|
154 |
sheet.write(i, 0, insurance.startDate, date_style)
|
|
|
155 |
sheet.write(i, 1, order.invoice_number)
|
|
|
156 |
sheet.write(i, 2, order.customer_name)
|
|
|
157 |
sheet.write(i, 3, line.serial_number)
|
|
|
158 |
sheet.write(i, 4, order.invoice_number)
|
|
|
159 |
sheet.write(i, 5, line.unit_price)
|
|
|
160 |
sheet.write(i, 6, line.unit_price)
|
| 7154 |
rajveer |
161 |
sheet.write(i, 7, order.id)
|
| 6964 |
rajveer |
162 |
i = i + 1
|
|
|
163 |
|
|
|
164 |
session.commit()
|
|
|
165 |
client = CatalogClient().get_client()
|
|
|
166 |
client.updateInsuranceDeclaredAmount(1, -declaredAmount)
|
|
|
167 |
|
|
|
168 |
today = date.today()
|
|
|
169 |
datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
|
|
|
170 |
filename = "/tmp/insurance-return-" + datestr + ".xls"
|
|
|
171 |
wbk.save(filename)
|
| 11954 |
kshitij.so |
172 |
EmailAttachmentSender.mail("cnc.center@shop2020.in", "5h0p2o2o", ["murthy@ubiassociates.com", "kashi@ubiassociates.com"], "Cancelled Insurance Policy Details for date " + datestr, "Please find attached orders which insurance need to be cancelled for today.", [get_attachment_part(filename)], ["pramit.singh@shop2020.in","anikendra.das@shop2020.in","kshitij.sood@shop2020.in","rajneesh.arora@shop2020.in"], [])
|
| 8549 |
kshitij.so |
173 |
|
| 8599 |
kshitij.so |
174 |
def submitDeliveredOrdersDetails(args):
|
| 8549 |
kshitij.so |
175 |
DataService.initialize(db_hostname="192.168.190.114")
|
|
|
176 |
todate = datetime.now()
|
|
|
177 |
todate = todate-timedelta(days = 10)
|
|
|
178 |
xstr = lambda s: s or ""
|
|
|
179 |
|
|
|
180 |
successful= []
|
|
|
181 |
duplicateRecord = []
|
|
|
182 |
serverSide = []
|
|
|
183 |
dataFormat = []
|
| 9237 |
kshitij.so |
184 |
client = Client('http://115.112.177.176/SSSWebservice/SSSWebservice.asmx?WSDL')
|
| 8549 |
kshitij.so |
185 |
header=client.factory.create('AuthHeader')
|
|
|
186 |
header.Username = 'saholic'
|
|
|
187 |
header.Password = '1Dfgh4@#'
|
|
|
188 |
client.set_options(soapheaders=header)
|
| 8599 |
kshitij.so |
189 |
|
|
|
190 |
|
|
|
191 |
if (len(args)>1):
|
|
|
192 |
insurances = InsuranceDetailForOrder.query.filter(InsuranceDetailForOrder.order_id.in_(args[1:])).all()
|
|
|
193 |
else:
|
|
|
194 |
insurances = InsuranceDetailForOrder.query.filter(InsuranceDetailForOrder.isDeclared == False).filter(InsuranceDetailForOrder.startDate >= todate).all()
|
| 8549 |
kshitij.so |
195 |
if not insurances:
|
|
|
196 |
return
|
|
|
197 |
for insurance in insurances:
|
|
|
198 |
order = insurance.order
|
|
|
199 |
if order.status != OrderStatus.DELIVERY_SUCCESS:
|
|
|
200 |
continue
|
|
|
201 |
try:
|
|
|
202 |
dateOfBirth = getDateFormatForDob(insurance.dob)
|
|
|
203 |
except:
|
|
|
204 |
try:
|
|
|
205 |
dateOfBirth = getDateFormatForDob2(insurance.dob)
|
| 9237 |
kshitij.so |
206 |
except Exception as ex:
|
|
|
207 |
print ex
|
| 8549 |
kshitij.so |
208 |
dataFormat.append(order.id)
|
|
|
209 |
continue
|
|
|
210 |
line = order.lineitems[0]
|
|
|
211 |
Connected =False
|
|
|
212 |
Attempt = 1
|
|
|
213 |
while not Connected:
|
|
|
214 |
try:
|
|
|
215 |
response = client.service.InsertSalesRecord('SAHOLIC',line.serial_number,getValidDateFormat(order.billing_timestamp.date()),order.invoice_number,\
|
|
|
216 |
'Mobile Phones',line.brand,xstr(line.model_name) + " " + xstr(line.model_number),line.unit_price,\
|
| 8605 |
kshitij.so |
217 |
0.0,line.total_price,getValidName(order.customer_name),getValidName(insurance.guardianName),dateOfBirth,str(order.id),\
|
| 8549 |
kshitij.so |
218 |
order.customer_mobilenumber,'')
|
| 8552 |
kshitij.so |
219 |
print ('SAHOLIC',line.serial_number,getValidDateFormat(order.billing_timestamp.date()),order.invoice_number,\
|
|
|
220 |
'Mobile Phones',line.brand,xstr(line.model_name) + " " + xstr(line.model_number),line.unit_price,\
|
| 8605 |
kshitij.so |
221 |
0.0,line.total_price,getValidName(order.customer_name),getValidName(insurance.guardianName),dateOfBirth,str(order.id),\
|
| 8552 |
kshitij.so |
222 |
order.customer_mobilenumber,'')
|
|
|
223 |
print "Response : ",response
|
| 8599 |
kshitij.so |
224 |
if response is None:
|
|
|
225 |
print "Unable to insert sale record.Empty Response came.Attempting again...."
|
|
|
226 |
if Attempt == 3:
|
|
|
227 |
break
|
|
|
228 |
Attempt+=1
|
|
|
229 |
continue
|
| 8549 |
kshitij.so |
230 |
Connected=True
|
|
|
231 |
except:
|
|
|
232 |
print "Unable to insert sale record.Attempting again...."
|
|
|
233 |
Attempt = Attempt+1
|
| 8599 |
kshitij.so |
234 |
if Attempt == 3:
|
| 8549 |
kshitij.so |
235 |
break
|
|
|
236 |
if not Connected:
|
|
|
237 |
serverSide.append(order.id)
|
|
|
238 |
continue
|
|
|
239 |
|
|
|
240 |
dom = parseString("<dom>" + response + "</dom>")
|
|
|
241 |
sssResponse = dom.getElementsByTagName('SSSResponse')[0]
|
|
|
242 |
status = sssResponse.getElementsByTagName('Status')
|
|
|
243 |
statusStr = status[0].childNodes[0].data
|
|
|
244 |
if (statusStr == 'Failure'):
|
|
|
245 |
error = sssResponse.getElementsByTagName('ErrorCode')
|
|
|
246 |
errorCode = int(error[0].childNodes[0].data)
|
|
|
247 |
if (errorCode == 999):
|
|
|
248 |
duplicateRecord.append(order.id)
|
|
|
249 |
elif (errorCode ==9999 or errorCode==99999):
|
|
|
250 |
serverSide.append(order.id)
|
|
|
251 |
else:
|
|
|
252 |
dataFormat.append(order.id)
|
|
|
253 |
|
|
|
254 |
elif (statusStr == 'Success'):
|
|
|
255 |
successful.append(order.id)
|
|
|
256 |
print "Insurance details successfully inserted.Order Id ",order.id
|
|
|
257 |
|
|
|
258 |
else:
|
|
|
259 |
serverSide.append(order.id)
|
|
|
260 |
|
|
|
261 |
today = date.today()
|
|
|
262 |
datestr = str(today.year) + "-" + str(today.month) + "-" + str(today.day)
|
|
|
263 |
mail_body = "Number of records successfully inserted :"+str(len(successful))+"\nFollowing order id's were not processed because\nServer Side Error: "+', '.join(map(str, serverSide))+"\n"+"Data Format Error: "+', '.join(map(str, dataFormat))+"\n"+"Duplicate entry IMEI: "+', '.join(map(str, duplicateRecord))
|
|
|
264 |
print mail_body
|
| 20172 |
aman.kumar |
265 |
EmailAttachmentSender.mail("cnc.center@shop2020.in", "5h0p2o2o", ["anikendra.das@shop2020.in","kshitij.sood@shop2020.in","rajneesh.arora@saholic.com","khushal.bhatia@saholic.com"], "Insurance submission result for date " + datestr, mail_body, [], [], [])
|
| 8599 |
kshitij.so |
266 |
|
|
|
267 |
if(len(args)>1):
|
|
|
268 |
print "Exiting.Dont want to run delivered_orders() and returned_orders() again."
|
|
|
269 |
sys.exit(0)
|
| 8549 |
kshitij.so |
270 |
|
|
|
271 |
|
|
|
272 |
def getValidDateFormat(dateToFormat):
|
|
|
273 |
return str(dateToFormat).replace('-','')
|
|
|
274 |
|
|
|
275 |
def getDateFormatForDob(dateToFormat):
|
| 8550 |
kshitij.so |
276 |
if (dateToFormat=='Organisation'):
|
|
|
277 |
return '19700101'
|
| 8549 |
kshitij.so |
278 |
return str(datetime.strptime(dateToFormat,"%b %d %Y").date()).replace('-','')
|
|
|
279 |
|
|
|
280 |
def getDateFormatForDob2(dateToFormat):
|
| 11953 |
kshitij.so |
281 |
try:
|
|
|
282 |
return str(datetime.strptime(dateToFormat,"%d %b %Y").date()).replace('-','')
|
|
|
283 |
except:
|
|
|
284 |
return str(datetime.strptime(dateToFormat,"%d/%m/%Y").date()).replace('-','')
|
| 8605 |
kshitij.so |
285 |
|
|
|
286 |
def getValidName(nameToFormat):
|
|
|
287 |
pattern = r'[^\.a-zA-Z\ \']'
|
|
|
288 |
return re.sub(pattern,"",nameToFormat)
|
| 6908 |
rajveer |
289 |
|
| 6964 |
rajveer |
290 |
def main():
|
| 8599 |
kshitij.so |
291 |
submitDeliveredOrdersDetails(sys.argv)
|
| 8550 |
kshitij.so |
292 |
delivered_orders()
|
| 6964 |
rajveer |
293 |
returned_orders()
|
|
|
294 |
|
| 6908 |
rajveer |
295 |
if __name__ == '__main__':
|
|
|
296 |
main()
|
|
|
297 |
|