| 5708 |
anupam.sin |
1 |
import json
|
|
|
2 |
import urllib2, cookielib
|
|
|
3 |
import MySQLdb
|
|
|
4 |
import datetime
|
|
|
5 |
import sys
|
|
|
6 |
import smtplib
|
|
|
7 |
|
|
|
8 |
from email import encoders
|
|
|
9 |
from email.mime.text import MIMEText
|
|
|
10 |
from email.mime.base import MIMEBase
|
|
|
11 |
from email.mime.multipart import MIMEMultipart
|
|
|
12 |
from pyExcelerator import Workbook, Font, XFStyle
|
|
|
13 |
from datetime import date, datetime
|
|
|
14 |
|
|
|
15 |
# Initialize db connection settings.
|
|
|
16 |
DB_HOST = "localhost"
|
|
|
17 |
DB_USER = "root"
|
|
|
18 |
DB_PASSWORD = "shop2020"
|
|
|
19 |
DB_NAME = "transaction"
|
|
|
20 |
|
|
|
21 |
# KEY NAMES
|
|
|
22 |
MONTHNAME = 'monthname'
|
|
|
23 |
DATES = 'dates'
|
|
|
24 |
|
|
|
25 |
MAILTO = ['anupam.singh@shop2020.in']
|
|
|
26 |
#['rajneesharora@spiceretail.co.in', 'pankaj.kankar@shop2020.in', 'ashutosh.saxena@shop2020.in', 'anupam.singh@shop2020.in']
|
|
|
27 |
SENDER = "cnc.center@shop2020.in"
|
|
|
28 |
PASSWORD = "5h0p2o2o"
|
|
|
29 |
SUBJECT = "Previous Day Accessory Sales Report"
|
|
|
30 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
31 |
SMTP_PORT = 587
|
|
|
32 |
|
|
|
33 |
TMP_FILE="/home/anupam/repeat_sales.xls"
|
|
|
34 |
|
|
|
35 |
def getDbConnection():
|
|
|
36 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
37 |
|
|
|
38 |
def closeConnection(conn):
|
|
|
39 |
conn.close()
|
|
|
40 |
|
|
|
41 |
def getProductSaleData():
|
|
|
42 |
selectSql = '''SELECT customer_id, quantity, left(created_timestamp, 10), total_amount
|
|
|
43 |
FROM `order` o
|
|
|
44 |
JOIN lineitem l
|
|
|
45 |
ON o.id = l.order_id
|
|
|
46 |
WHERE o.status = 12 and created_timestamp < '2012-07-01 00:00:00'
|
|
|
47 |
ORDER BY customer_id
|
|
|
48 |
'''
|
|
|
49 |
|
|
|
50 |
conn = getDbConnection()
|
|
|
51 |
try:
|
|
|
52 |
# prepare a cursor object using cursor() method
|
|
|
53 |
cursor = conn.cursor()
|
|
|
54 |
# Execute the SQL command
|
|
|
55 |
# Fetch source id.
|
|
|
56 |
cursor.execute(selectSql)
|
|
|
57 |
result = cursor.fetchall()
|
|
|
58 |
quantity = 1
|
|
|
59 |
sum = 0
|
|
|
60 |
sumOfDays = 0
|
|
|
61 |
lastSeenId = 0
|
|
|
62 |
listOfTuples = []
|
|
|
63 |
myList = []
|
|
|
64 |
numberOfDays = 1
|
|
|
65 |
numberOfDaysSameDayInclusive = 1
|
|
|
66 |
finalMap = {}
|
|
|
67 |
for r in result:
|
|
|
68 |
if(r[0] == lastSeenId) :
|
|
|
69 |
newDate = datetime.strptime(r[2], '%Y-%m-%d')
|
|
|
70 |
diff = newDate - lastDate
|
|
|
71 |
sumOfDays += diff.days
|
|
|
72 |
quantity += r[1]
|
|
|
73 |
if diff.days != 0 :
|
|
|
74 |
numberOfDays += 1
|
|
|
75 |
numberOfDaysSameDayInclusive += 1
|
|
|
76 |
sum += r[3]
|
|
|
77 |
else :
|
|
|
78 |
#print lastSeenId, quantity, str(sum/quantity), str(sumOfDays/quantity)
|
|
|
79 |
myList = [lastSeenId, quantity, sum/quantity, sumOfDays/numberOfDays, sumOfDays/numberOfDaysSameDayInclusive]
|
|
|
80 |
listOfTuples.append(myList)
|
|
|
81 |
sumOfDays = 0
|
|
|
82 |
lastSeenId = r[0]
|
|
|
83 |
sum = r[3]
|
|
|
84 |
quantity = r[1]
|
|
|
85 |
numberOfDays = 1
|
|
|
86 |
numberOfDaysSameDayInclusive = 1
|
|
|
87 |
lastDate = datetime.strptime(r[2], '%Y-%m-%d')
|
|
|
88 |
|
|
|
89 |
#print listOfTuples
|
|
|
90 |
for l in listOfTuples :
|
|
|
91 |
#print l[0:4]
|
|
|
92 |
if (finalMap.has_key(l[1])):
|
|
|
93 |
temp = finalMap.get(l[1])
|
|
|
94 |
temp[0] += 1
|
|
|
95 |
temp[1] += l[2]
|
|
|
96 |
temp[2] += l[3]
|
|
|
97 |
temp[3] += l[4]
|
|
|
98 |
else:
|
|
|
99 |
finalMap[l[1]] = [1, l[2], l[3], l[4]]
|
|
|
100 |
|
|
|
101 |
except Exception as e:
|
|
|
102 |
print "Error: unable to fetch data"
|
|
|
103 |
print e
|
|
|
104 |
|
|
|
105 |
return finalMap
|
|
|
106 |
|
|
|
107 |
def createXlsReport(finalMap):
|
|
|
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 |
column = 0
|
|
|
117 |
row = 0
|
|
|
118 |
|
|
|
119 |
worksheet.write(row, 0, 'Quantity', boldStyle)
|
|
|
120 |
worksheet.write(row, 1, '# of Users', boldStyle)
|
|
|
121 |
worksheet.write(row, 2, 'Avg Value', boldStyle)
|
|
|
122 |
worksheet.write(row, 3, 'Avg Delay Excluding Same Day repeats', boldStyle)
|
|
|
123 |
worksheet.write(row, 4, 'Avg Delay Including Same Day repeats', boldStyle)
|
|
|
124 |
|
|
|
125 |
row = 1
|
|
|
126 |
|
|
|
127 |
orderQuantities = finalMap.keys()
|
|
|
128 |
orderQuantities.sort()
|
|
|
129 |
for key in orderQuantities :
|
|
|
130 |
temp1 = finalMap.get(key)
|
|
|
131 |
temp1[1] = temp1[1]/temp1[0]
|
|
|
132 |
temp1[2] = temp1[2]/temp1[0]
|
|
|
133 |
temp1[3] = temp1[3]/temp1[0]
|
|
|
134 |
worksheet.write(row, 0, int(key), boldStyle)
|
|
|
135 |
worksheet.write(row, 1, int(temp1[0]))
|
|
|
136 |
worksheet.write(row, 2, int(temp1[1]))
|
|
|
137 |
worksheet.write(row, 3, int(temp1[2]))
|
|
|
138 |
worksheet.write(row, 4, int(temp1[3]))
|
|
|
139 |
row += 1
|
|
|
140 |
|
|
|
141 |
workbook.save(TMP_FILE)
|
|
|
142 |
|
|
|
143 |
def sendmail(message):
|
|
|
144 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
145 |
mailServer.ehlo()
|
|
|
146 |
mailServer.starttls()
|
|
|
147 |
mailServer.ehlo()
|
|
|
148 |
|
|
|
149 |
# Create the container (outer) email message.
|
|
|
150 |
msg = MIMEMultipart()
|
|
|
151 |
msg['Subject'] = SUBJECT + ' - ' + date.today().isoformat()
|
|
|
152 |
msg['From'] = "bi@saholic.com"
|
|
|
153 |
msg['To'] = 'sku-recipients@saholic.com'
|
|
|
154 |
msg.preamble = SUBJECT + ' - ' + date.today().isoformat()
|
|
|
155 |
html_msg = MIMEText(message, 'html')
|
|
|
156 |
msg.attach(html_msg)
|
|
|
157 |
|
|
|
158 |
fileMsg = MIMEBase('application','vnd.ms-excel')
|
|
|
159 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
160 |
encoders.encode_base64(fileMsg)
|
|
|
161 |
fileMsg.add_header('Content-Disposition','attachment;filename=Yesterday-Accessory-Sales' + ' - ' + date.today().isoformat() + '.xls')
|
|
|
162 |
msg.attach(fileMsg)
|
|
|
163 |
|
|
|
164 |
mailServer.login(SENDER, PASSWORD)
|
|
|
165 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
166 |
|
|
|
167 |
def main():
|
|
|
168 |
resultMap = getProductSaleData()
|
|
|
169 |
createXlsReport(resultMap)
|
|
|
170 |
#sendmail(message)
|
|
|
171 |
|
|
|
172 |
if __name__ == '__main__':
|
|
|
173 |
main()
|