| 4066 |
vikas |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
import json
|
|
|
4 |
import urllib2, cookielib
|
|
|
5 |
import MySQLdb
|
|
|
6 |
import datetime
|
|
|
7 |
import sys
|
|
|
8 |
import smtplib
|
|
|
9 |
|
|
|
10 |
from email import encoders
|
|
|
11 |
from email.mime.text import MIMEText
|
|
|
12 |
from email.mime.base import MIMEBase
|
|
|
13 |
from email.mime.multipart import MIMEMultipart
|
|
|
14 |
from pyExcelerator import Workbook, Font, XFStyle
|
| 4067 |
vikas |
15 |
from datetime import date
|
| 4066 |
vikas |
16 |
|
|
|
17 |
# Initialize db connection settings.
|
|
|
18 |
DB_HOST = "localhost"
|
|
|
19 |
DB_USER = "root"
|
|
|
20 |
DB_PASSWORD = "shop2020"
|
|
|
21 |
DB_NAME = "sales"
|
|
|
22 |
|
|
|
23 |
# KEY NAMES
|
|
|
24 |
MONTHNAME = 'monthname'
|
|
|
25 |
DATES = 'dates'
|
|
|
26 |
|
| 6714 |
anupam.sin |
27 |
MAILTO = ['rajneesharora@spiceretail.co.in', 'chandan.kumar@shop2020.in', 'khushal.bhatia@shop2020.in', 'yukti.jain@spiceretail.co.in', 'sandeep.sachdeva@shop2020.in', 'chaitnaya.vats@shop2020.in', 'anupam.singh@shop2020.in']
|
| 4800 |
rajveer |
28 |
SENDER = "cnc.center@shop2020.in"
|
| 4066 |
vikas |
29 |
PASSWORD = "5h0p2o2o"
|
|
|
30 |
SUBJECT = "Product Report"
|
|
|
31 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
32 |
SMTP_PORT = 587
|
|
|
33 |
|
|
|
34 |
TMP_FILE="/tmp/product_report.xls"
|
|
|
35 |
|
|
|
36 |
def getDbConnection():
|
|
|
37 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
38 |
|
|
|
39 |
def closeConnection(conn):
|
|
|
40 |
conn.close()
|
|
|
41 |
|
|
|
42 |
def getProductSaleData():
|
|
|
43 |
selectSql = '''select i.parent_category, i.category, i.brand, IFNULL(i.model_name, ''), IFNULL(i.model_number, ''),
|
| 4074 |
vikas |
44 |
i.color, d.monthname, d.monthnumber, d.dayofmonth, sum(quantity) as quantity
|
| 4066 |
vikas |
45 |
from sales s
|
|
|
46 |
join item i on (i.id = s.item_id)
|
| 4070 |
vikas |
47 |
join datedim d on (d.date_id = s.date_id)
|
|
|
48 |
join orderstatus os on (s.status = os.status)
|
| 9341 |
anupam.sin |
49 |
where d.fulldate <= curdate() and d.fulldate >= date_sub(curdate(), interval 2 month)
|
| 4809 |
anupam.sin |
50 |
and os.statusGroup in ('In process', 'Delivered', 'Cancelled', 'Return in process', 'Reshipped', 'Refunded')
|
|
|
51 |
and os.statusSubGroup != 'Cod verification failed'
|
| 4066 |
vikas |
52 |
group by i.parent_category, i.category, i.brand, IFNULL(i.model_name, ''),
|
|
|
53 |
IFNULL(i.model_number, ''), i.color, d.monthname, d.monthnumber, d.dayofmonth
|
| 9340 |
anupam.sin |
54 |
|
| 4066 |
vikas |
55 |
'''
|
|
|
56 |
conn = getDbConnection()
|
|
|
57 |
monthdatesmap = {}
|
|
|
58 |
prodsalesmap = {}
|
|
|
59 |
try:
|
|
|
60 |
# prepare a cursor object using cursor() method
|
|
|
61 |
cursor = conn.cursor()
|
|
|
62 |
# Execute the SQL command
|
|
|
63 |
# Fetch source id.
|
|
|
64 |
cursor.execute(selectSql)
|
|
|
65 |
result = cursor.fetchall()
|
|
|
66 |
for r in result:
|
|
|
67 |
(parent, category, brand, model_name, model_number, color) = r[0:6]
|
|
|
68 |
monthname, monthnumber = r[6:8]
|
|
|
69 |
dayofmonth = r[8]
|
|
|
70 |
|
|
|
71 |
if monthdatesmap.has_key(monthnumber):
|
|
|
72 |
monthmap = monthdatesmap.get(monthnumber)
|
|
|
73 |
monthmap.get(DATES).add(dayofmonth)
|
|
|
74 |
else:
|
|
|
75 |
monthmap = {}
|
|
|
76 |
monthmap[MONTHNAME] = monthname
|
|
|
77 |
dates = set([])
|
|
|
78 |
dates.add(dayofmonth)
|
|
|
79 |
monthmap[DATES] = dates
|
|
|
80 |
monthdatesmap[monthnumber] = monthmap
|
|
|
81 |
|
|
|
82 |
if prodsalesmap.has_key((parent, category, brand, model_name, model_number, color)):
|
|
|
83 |
prodsales = prodsalesmap.get((parent, category, brand, model_name, model_number, color))
|
|
|
84 |
if prodsales.has_key(monthnumber):
|
|
|
85 |
monthprodsales = prodsales.get(monthnumber)
|
|
|
86 |
monthprodsales[dayofmonth] = r[9]
|
|
|
87 |
monthprodsales[0] += r[9]
|
|
|
88 |
else:
|
|
|
89 |
monthprodsales = {}
|
|
|
90 |
monthprodsales[dayofmonth] = r[9]
|
|
|
91 |
monthprodsales[0] = r[9]
|
|
|
92 |
prodsales[monthnumber] = monthprodsales
|
|
|
93 |
else:
|
|
|
94 |
prodsale = {}
|
|
|
95 |
monthprodsales = {}
|
|
|
96 |
monthprodsales[dayofmonth] = r[9]
|
|
|
97 |
monthprodsales[0] = r[9]
|
|
|
98 |
prodsale[monthnumber] = monthprodsales
|
|
|
99 |
prodsalesmap[(parent, category, brand, model_name, model_number, color)] = prodsale
|
|
|
100 |
|
|
|
101 |
except Exception as e:
|
|
|
102 |
print "Error: unable to fetch data"
|
|
|
103 |
print e
|
|
|
104 |
|
|
|
105 |
return monthdatesmap, prodsalesmap
|
|
|
106 |
|
|
|
107 |
def createXlsReport(monthdatesmap, prodsalesmap):
|
|
|
108 |
workbook = Workbook()
|
|
|
109 |
worksheet = workbook.add_sheet("Sheet 1")
|
|
|
110 |
boldStyle = XFStyle()
|
|
|
111 |
f = Font()
|
|
|
112 |
f.bold = True
|
|
|
113 |
boldStyle.font = f
|
|
|
114 |
|
|
|
115 |
datecolmap = {}
|
|
|
116 |
col = 6
|
|
|
117 |
for monthnumber in monthdatesmap.keys():
|
|
|
118 |
monthname = monthdatesmap[monthnumber][MONTHNAME]
|
| 4067 |
vikas |
119 |
worksheet.write(0, col, monthname, boldStyle)
|
| 4066 |
vikas |
120 |
worksheet.write(1, col, 'Month', boldStyle)
|
|
|
121 |
datemap ={}
|
|
|
122 |
datemap[0] = col
|
|
|
123 |
datecolmap[monthnumber] = datemap
|
|
|
124 |
col += 1
|
|
|
125 |
|
|
|
126 |
worksheet.write(1, 0, 'Category', boldStyle)
|
|
|
127 |
worksheet.write(1, 1, 'Sub Category', boldStyle)
|
|
|
128 |
worksheet.write(1, 2, 'Brand', boldStyle)
|
|
|
129 |
worksheet.write(1, 3, 'Model Name', boldStyle)
|
|
|
130 |
worksheet.write(1, 4, 'Model Number', boldStyle)
|
|
|
131 |
worksheet.write(1, 5, 'Color', boldStyle)
|
|
|
132 |
for dayofmonth in monthdatesmap[monthnumber][DATES]:
|
|
|
133 |
worksheet.write(1, col, dayofmonth, boldStyle)
|
|
|
134 |
datemap[dayofmonth] = col
|
|
|
135 |
col += 1
|
|
|
136 |
|
|
|
137 |
row = 2
|
|
|
138 |
prodkeys = prodsalesmap.keys()
|
|
|
139 |
prodkeys.sort()
|
|
|
140 |
for prodsale in prodkeys:
|
|
|
141 |
(parent, category, brand, model_name, model_number, color) = prodsale
|
|
|
142 |
worksheet.write(row, 0, parent, boldStyle)
|
|
|
143 |
worksheet.write(row, 1, category, boldStyle)
|
|
|
144 |
worksheet.write(row, 2, brand, boldStyle)
|
|
|
145 |
worksheet.write(row, 3, model_name if model_name is not None else '', boldStyle)
|
|
|
146 |
worksheet.write(row, 4, model_number if model_number is not None else '', boldStyle)
|
|
|
147 |
worksheet.write(row, 5, color if color is not None else 'NA', boldStyle)
|
|
|
148 |
for monthnumber in prodsalesmap[prodsale].keys():
|
|
|
149 |
for dayofmonth in prodsalesmap[prodsale][monthnumber]:
|
|
|
150 |
quantity = prodsalesmap[prodsale][monthnumber][dayofmonth]
|
|
|
151 |
worksheet.write(row, datecolmap[monthnumber][dayofmonth], quantity)
|
|
|
152 |
row += 1
|
|
|
153 |
|
|
|
154 |
worksheet.panes_frozen = True
|
|
|
155 |
worksheet.horz_split_pos = 2
|
|
|
156 |
worksheet.vert_split_pos = 6
|
|
|
157 |
workbook.save(TMP_FILE)
|
|
|
158 |
|
|
|
159 |
def sendmail():
|
|
|
160 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
161 |
mailServer.ehlo()
|
|
|
162 |
mailServer.starttls()
|
|
|
163 |
mailServer.ehlo()
|
|
|
164 |
|
|
|
165 |
# Create the container (outer) email message.
|
|
|
166 |
msg = MIMEMultipart()
|
| 4067 |
vikas |
167 |
msg['Subject'] = SUBJECT + ' - ' + date.today().isoformat()
|
| 4066 |
vikas |
168 |
msg['From'] = "bi@saholic.com"
|
|
|
169 |
msg['To'] = 'sku-recipients@saholic.com'
|
| 4067 |
vikas |
170 |
msg.preamble = SUBJECT + ' - ' + date.today().isoformat()
|
| 4066 |
vikas |
171 |
|
|
|
172 |
fileMsg = MIMEBase('application','vnd.ms-excel')
|
|
|
173 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
174 |
encoders.encode_base64(fileMsg)
|
| 4067 |
vikas |
175 |
fileMsg.add_header('Content-Disposition','attachment;filename=Product-Report' + ' - ' + date.today().isoformat() + '.xls')
|
| 4066 |
vikas |
176 |
msg.attach(fileMsg)
|
|
|
177 |
|
|
|
178 |
mailServer.login(SENDER, PASSWORD)
|
|
|
179 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
180 |
|
|
|
181 |
def main():
|
|
|
182 |
(monthdatesmap, prodsalesmap) = getProductSaleData()
|
|
|
183 |
createXlsReport(monthdatesmap, prodsalesmap)
|
|
|
184 |
sendmail()
|
|
|
185 |
|
|
|
186 |
if __name__ == '__main__':
|
|
|
187 |
main()
|