| 17137 |
naman |
1 |
'''
|
|
|
2 |
Created on 24-Sep-2015
|
|
|
3 |
|
|
|
4 |
@author: manish
|
|
|
5 |
'''
|
|
|
6 |
from datetime import date, datetime, timedelta
|
| 17156 |
amit.gupta |
7 |
from dtr.main import sourceMap
|
|
|
8 |
from dtr.storage import Mysql
|
|
|
9 |
from dtr.utils import utils
|
| 17137 |
naman |
10 |
from email.mime.multipart import MIMEMultipart
|
|
|
11 |
from email.mime.text import MIMEText
|
|
|
12 |
from pymongo import MongoClient
|
| 17156 |
amit.gupta |
13 |
import MySQLdb as mdb
|
|
|
14 |
import _mysql
|
| 17137 |
naman |
15 |
import smtplib
|
|
|
16 |
import sys
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
# DataService.initialize()
|
|
|
21 |
# client = MongoClient('mongodb://localhost:27017/')
|
|
|
22 |
SENDER = "cnc.center@shop2020.in"
|
|
|
23 |
PASSWORD = "5h0p2o2o"
|
|
|
24 |
SUBJECT = "Summary"
|
|
|
25 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
26 |
SMTP_PORT = 587
|
|
|
27 |
client = MongoClient()
|
|
|
28 |
con = mdb.connect('localhost','root','shop2020','dtr')
|
|
|
29 |
|
|
|
30 |
def getSkuData(storeId, identifier):
|
| 17139 |
naman |
31 |
if storeId in (1,2,4,5,6,7):
|
| 17137 |
naman |
32 |
skuData = client.Catalog.MasterData.find_one({'identifier':identifier, 'source_id':storeId})
|
|
|
33 |
elif storeId == 3:
|
|
|
34 |
skuData = client.Catalog.MasterData.find_one({'secondaryIdentifier':identifier, 'source_id':storeId})
|
|
|
35 |
return skuData
|
|
|
36 |
|
|
|
37 |
def sendmail(email, message, title, *varargs):
|
|
|
38 |
if email == "":
|
|
|
39 |
return
|
|
|
40 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
41 |
mailServer.ehlo()
|
|
|
42 |
mailServer.starttls()
|
|
|
43 |
mailServer.ehlo()
|
|
|
44 |
|
|
|
45 |
# Create the container (outer) email message.
|
|
|
46 |
msg = MIMEMultipart()
|
|
|
47 |
msg['Subject'] = title
|
|
|
48 |
msg.preamble = title
|
|
|
49 |
html_msg = MIMEText(message, 'html')
|
|
|
50 |
msg.attach(html_msg)
|
|
|
51 |
|
|
|
52 |
# email.append('amit.gupta@shop2020.in')
|
|
|
53 |
MAILTO = email
|
|
|
54 |
mailServer.login(SENDER, PASSWORD)
|
|
|
55 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
56 |
|
|
|
57 |
def addToAllOrders(start_date, end_date=None):
|
|
|
58 |
if not end_date:
|
|
|
59 |
end_date = datetime.now()
|
|
|
60 |
else:
|
|
|
61 |
end_date = end_date + timedelta(days=1)
|
|
|
62 |
try:
|
|
|
63 |
cur = con.cursor()
|
| 17138 |
naman |
64 |
db=client.Dtr
|
| 17137 |
naman |
65 |
db1 = client.Catalog
|
|
|
66 |
cur.execute("delete from allorder where created_on >= %s and created_on < %s", (start_date, end_date))
|
|
|
67 |
con.commit()
|
|
|
68 |
results = Mysql.fetchResult('''
|
| 17211 |
amit.gupta |
69 |
select ow.*, u.username, crm1.order_count, u.referrer, aua.city,aua.state,aua.pincode from order_view ow
|
| 17137 |
naman |
70 |
left join users u on u.id = ow.user_id
|
| 17194 |
amit.gupta |
71 |
left join (select user_id, count(*) as order_count from order_view where created >= %s and created < %s and status in ('ORDER_CREATED','DETAIL_CREATED') group by user_id)as crm1 on ow.user_id = crm1.user_id
|
| 17137 |
naman |
72 |
left join (select * from (select * from all_user_addresses order by source) s group by user_id)aua on aua.user_id=u.id
|
| 17211 |
amit.gupta |
73 |
where (lower(u.referrer) not like %s or u.utm_campaign is not null) and u.activated = 1 and ow.status in ('ORDER_CREATED','DETAIL_CREATED') and ow.created >= %s and ow.created < %s;
|
|
|
74 |
''', start_date, end_date, 'emp%', start_date, end_date, )
|
| 17137 |
naman |
75 |
|
| 17145 |
amit.gupta |
76 |
query = """INSERT INTO allorder
|
| 17211 |
amit.gupta |
77 |
(user_id, user_name, order_id, created_on, store_id, merchant_order_id, status, detailed_status, product_title, referrer, amount_paid, catalog_id, brand, model,category, deal_rank, max_nlc, min_nlc, dp, item_status, city, state, pincode, merchant_suborder_id,cashback_status,cashback_amount,quantity)
|
|
|
78 |
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
| 17145 |
amit.gupta |
79 |
"""
|
| 17137 |
naman |
80 |
for result in results:
|
|
|
81 |
morder = db.merchantOrder.find_one({"orderId":result[0]})
|
|
|
82 |
if morder is not None:
|
|
|
83 |
subOrders = morder.get("subOrders")
|
|
|
84 |
if subOrders is not None:
|
|
|
85 |
for subOrder in subOrders:
|
|
|
86 |
user_id = result[1]
|
|
|
87 |
user_name= result[7]
|
|
|
88 |
order_id = result[0]
|
|
|
89 |
created_on =result[6]
|
|
|
90 |
store_id = sourceMap.get(result[2])
|
|
|
91 |
merchant_order_id = morder.get("merchantOrderId")
|
|
|
92 |
status = subOrder.get("status")
|
|
|
93 |
detailed_status = subOrder.get("detailedStatus")
|
|
|
94 |
product_title= subOrder.get("productTitle")
|
|
|
95 |
referrer = result[9]
|
| 17211 |
amit.gupta |
96 |
amount_paid = int(subOrder.get("amountPaid"))
|
|
|
97 |
quantity=int(subOrder.get("quantity"))
|
| 17137 |
naman |
98 |
skuData = getSkuData(morder.get("storeId"), subOrder.get("productCode"))
|
| 17218 |
amit.gupta |
99 |
if morder.get("storeId") == 3:
|
|
|
100 |
skuData = db1.MasterData.find_one({'secondaryIdentifier':subOrder.get("productCode").strip(), 'source_id':morder.get("storeId")})
|
|
|
101 |
else:
|
| 17137 |
naman |
102 |
skuData = db1.MasterData.find_one({'identifier':subOrder.get("productCode").strip(), 'source_id':morder.get("storeId")})
|
|
|
103 |
if skuData is not None:
|
|
|
104 |
catalog_id = skuData.get("skuBundleId")
|
|
|
105 |
brand= skuData.get("brand")
|
|
|
106 |
model = skuData.get("model_name")
|
|
|
107 |
category = skuData.get("category")
|
|
|
108 |
deal_rank = subOrder.get("dealRank")
|
|
|
109 |
max_nlc = subOrder.get("maxNlc")
|
|
|
110 |
min_nlc = subOrder.get("minNlc")
|
| 17145 |
amit.gupta |
111 |
dp = subOrder.get("db")
|
|
|
112 |
item_status = utils.statusMap.get(subOrder.get("itemStatus"))
|
| 17137 |
naman |
113 |
else:
|
|
|
114 |
catalog_id = 'None'
|
|
|
115 |
brand= 'None'
|
|
|
116 |
model = 'None'
|
|
|
117 |
category = 'None'
|
|
|
118 |
deal_rank = 'None'
|
|
|
119 |
max_nlc = 'None'
|
|
|
120 |
min_nlc = 'None'
|
|
|
121 |
dp = 'None'
|
|
|
122 |
item_status = 'None'
|
|
|
123 |
merchant_suborder_id = subOrder.get("merchantSubOrderId")
|
|
|
124 |
cashback_status = subOrder.get("cashBackStatus")
|
|
|
125 |
cashback_amount = subOrder.get("cashBackAmount")
|
|
|
126 |
city = result[10]
|
|
|
127 |
state = result[11]
|
|
|
128 |
pincode = result[12]
|
|
|
129 |
|
|
|
130 |
print user_id
|
| 17211 |
amit.gupta |
131 |
values = (user_id, user_name, order_id, created_on, store_id, merchant_order_id, status, detailed_status, product_title, referrer, amount_paid, catalog_id, brand, model,category, deal_rank, max_nlc, min_nlc, dp, item_status, city, state, pincode,merchant_suborder_id,cashback_status,cashback_amount, quantity)
|
| 17148 |
amit.gupta |
132 |
cur.execute(query,values)
|
| 17137 |
naman |
133 |
con.commit()
|
|
|
134 |
except _mysql.Error, e:
|
|
|
135 |
print "Error %d: %s" % (e.args[0], e.args[1])
|
|
|
136 |
sys.exit(1)
|
|
|
137 |
|
|
|
138 |
finally:
|
|
|
139 |
|
|
|
140 |
if con:
|
|
|
141 |
con.close()
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
def summaryByBrandAndStore():
|
|
|
145 |
con = mdb.connect('localhost','root','shop2020','dtr')
|
|
|
146 |
try:
|
|
|
147 |
cur = con.cursor()
|
| 17153 |
amit.gupta |
148 |
# ----Data by brand---
|
| 17137 |
naman |
149 |
tbody = []
|
|
|
150 |
rowtemplate_brand="<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>"
|
|
|
151 |
mailBodyTemplate_brand="""
|
|
|
152 |
<html>
|
|
|
153 |
<body>
|
|
|
154 |
<table cellspacing="0" border="1" style="text-align:right">
|
|
|
155 |
<thead>
|
|
|
156 |
<tr>
|
|
|
157 |
<th colspan='4' style="text-align:center">Summary by Brand</th>
|
|
|
158 |
</tr>
|
|
|
159 |
<tr>
|
|
|
160 |
<th style="text-align:center">Brand</th>
|
|
|
161 |
<th style="text-align:center">Amount</th>
|
|
|
162 |
<th style="text-align:center">Quantity</th>
|
|
|
163 |
<th style="text-align:center">Number of Order</th>
|
|
|
164 |
</tr>
|
|
|
165 |
</thead>
|
|
|
166 |
<tbody>
|
|
|
167 |
{0}
|
|
|
168 |
</tbody>
|
|
|
169 |
</table><br><br>
|
|
|
170 |
</body>
|
|
|
171 |
</html>
|
|
|
172 |
"""
|
|
|
173 |
|
| 17149 |
amit.gupta |
174 |
cur.execute('''select brand ,sum(amount_paid) amount,count(brand) quantity, count(distinct order_id) no_of_orders
|
| 17150 |
amit.gupta |
175 |
from allorder where created_on >= CURDATE() - interval 1 day and created_on < CURDATE() group by brand order by count(brand) desc limit 10
|
| 17149 |
amit.gupta |
176 |
''')
|
| 17137 |
naman |
177 |
rows = cur.fetchall()
|
|
|
178 |
row = 1
|
|
|
179 |
alldata =''
|
|
|
180 |
for data in rows:
|
|
|
181 |
row +=1
|
|
|
182 |
if len(alldata)>0:
|
|
|
183 |
alldata = alldata + ",'"+ data[0]+"'"
|
|
|
184 |
else:
|
|
|
185 |
alldata = "'"+data[0]+"'"
|
|
|
186 |
tbody.append(rowtemplate_brand.format(data[0],data[1],data[2],data[3]))
|
|
|
187 |
|
| 17152 |
amit.gupta |
188 |
cur.execute("select sum(amount_paid) amount,count(*) quantity,count(distinct order_id) from allorder where created_on >= CURDATE() - interval 1 day and created_on < CURDATE() and brand not in("+alldata+")")
|
| 17137 |
naman |
189 |
row_other = cur.fetchall()
|
|
|
190 |
for data in row_other:
|
|
|
191 |
tbody.append(rowtemplate_brand.format('Other',data[0],data[1],data[2]))
|
|
|
192 |
tbody.append(rowtemplate_brand.format('','','',''))
|
|
|
193 |
tbody.append(rowtemplate_brand.format('','<b>MTD Amount</b>','<b>MTD Quantity</b>','<b>MTD No. of Order</b>'))
|
|
|
194 |
|
| 17153 |
amit.gupta |
195 |
|
|
|
196 |
|
| 17137 |
naman |
197 |
# Brand by MTD
|
| 17150 |
amit.gupta |
198 |
cur.execute('''select brand ,sum(amount_paid) amount,count(brand) quantity, count(distinct order_id) no_of_orders
|
|
|
199 |
from allorder where created_on >= CURDATE() - interval DAY(CURDATE()-INTERVAL 1 day) day and
|
|
|
200 |
created_on < CURDATE() group by brand order by count(brand) desc limit 10''')
|
| 17137 |
naman |
201 |
rows = cur.fetchall()
|
|
|
202 |
row += 2
|
|
|
203 |
alldata =''
|
|
|
204 |
for data in rows:
|
|
|
205 |
if len(alldata)>0:
|
|
|
206 |
alldata = alldata + ",'"+ data[0]+"'"
|
|
|
207 |
else:
|
|
|
208 |
alldata = "'"+data[0]+"'"
|
|
|
209 |
tbody.append(rowtemplate_brand.format(data[0],data[1],data[2],data[3]))
|
|
|
210 |
|
| 17152 |
amit.gupta |
211 |
cur.execute("select sum(amount_paid) amount,count(*) quantity,count(distinct order_id) from allorder where created_on >= CURDATE() - interval DAY(CURDATE()-INTERVAL 1 day) day and created_on < CURDATE() AND brand not in("+alldata+")")
|
| 17137 |
naman |
212 |
mtd_row_other = cur.fetchall()
|
|
|
213 |
for data in mtd_row_other:
|
|
|
214 |
tbody.append(rowtemplate_brand.format('Other',data[0],data[1],data[2]))
|
|
|
215 |
|
|
|
216 |
message_by_brand = mailBodyTemplate_brand.format("".join(tbody))
|
| 17153 |
amit.gupta |
217 |
|
| 17137 |
naman |
218 |
# Data by Store
|
|
|
219 |
# cur.execute("select store_id store,sum(amount_paid) amount,count(store_id) quantity ,count(distinct order_id) from allorder group by store_id order by store_id ASC;")
|
| 17194 |
amit.gupta |
220 |
cur.execute('''
|
|
|
221 |
select name, c.* from store left join (select b.store_id, a.amount, a.quantity, a.orders, b.mtdamount,b.mtdquantity, b.mtdorders from
|
|
|
222 |
(select ifnull(store_id, 'total') as store_id, sum(amount_paid) mtdamount, count(*) mtdquantity, count(distinct order_id) mtdorders from allorder
|
| 17218 |
amit.gupta |
223 |
where created_on >= CURDATE() - interval DAY(CURDATE()-INTERVAL 1 day) day and created_on < CURDATE() group by store_id with rollup) b
|
| 17194 |
amit.gupta |
224 |
left join
|
|
|
225 |
(select ifnull(store_id, 'total') as store_id, sum(amount_paid) amount, count(*) quantity, count(distinct order_id) orders from allorder
|
| 17219 |
amit.gupta |
226 |
where date(created_on)=curdate()-interval 1 day group by store_id with rollup) a
|
| 17194 |
amit.gupta |
227 |
on a.store_id=b.store_id) as c on name = c.store_id''')
|
| 17137 |
naman |
228 |
rows = cur.fetchall()
|
|
|
229 |
tbody =[]
|
|
|
230 |
rowtemplate="<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>"
|
|
|
231 |
for data in rows:
|
| 17155 |
amit.gupta |
232 |
tbody.append(rowtemplate.format(data[0],data[2] if data[2] else "-",data[3] if data[3] else "-",data[4] if data[4] else "-",
|
|
|
233 |
data[5] if data[5] else "-",data[6] if data[6] else "-",data[7] if data[7] else "-"))
|
| 17137 |
naman |
234 |
|
|
|
235 |
# book.save("/home/manish/Desktop/Summary.xls")
|
|
|
236 |
|
|
|
237 |
mailBodyTemplate="""
|
|
|
238 |
<html>
|
|
|
239 |
<body>
|
|
|
240 |
<table cellspacing="0" border="1" style="text-align:right">
|
|
|
241 |
<thead>
|
|
|
242 |
<tr>
|
|
|
243 |
<th colspan='7' style="text-align:center">Summary by Store</th>
|
|
|
244 |
</tr>
|
|
|
245 |
<tr>
|
| 17154 |
amit.gupta |
246 |
<th style="text-align:center">Store</th>
|
| 17137 |
naman |
247 |
<th style="text-align:center">Amount</th>
|
|
|
248 |
<th style="text-align:center">Quantity</th>
|
|
|
249 |
<th style="text-align:center">Number of Order</th>
|
|
|
250 |
<th style="text-align:center">MTD Amount</th>
|
|
|
251 |
<th style="text-align:center">MTD Quantity</th>
|
|
|
252 |
<th style="text-align:center">MTD Number of Order</th>
|
|
|
253 |
</tr>
|
|
|
254 |
</thead>
|
|
|
255 |
<tbody>
|
|
|
256 |
{0}
|
|
|
257 |
</tbody>
|
|
|
258 |
</table><br><br>
|
|
|
259 |
</body>
|
|
|
260 |
</html>
|
|
|
261 |
"""
|
|
|
262 |
message = mailBodyTemplate.format("".join(tbody))
|
|
|
263 |
return message+message_by_brand
|
|
|
264 |
except _mysql.Error, e:
|
|
|
265 |
|
|
|
266 |
print "Error %d: %s" % (e.args[0], e.args[1])
|
|
|
267 |
sys.exit(1)
|
|
|
268 |
|
|
|
269 |
finally:
|
|
|
270 |
|
|
|
271 |
if con:
|
|
|
272 |
con.close()
|
|
|
273 |
|
|
|
274 |
if __name__ == '__main__':
|
| 17195 |
amit.gupta |
275 |
#addToAllOrders(date(2015,1,1))
|
| 17160 |
amit.gupta |
276 |
#summaryByBrandAndStore()
|
|
|
277 |
pass
|