| 6554 |
anupam.sin |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
from datetime import date
|
|
|
4 |
from email import encoders
|
|
|
5 |
from email.mime.base import MIMEBase
|
|
|
6 |
from email.mime.multipart import MIMEMultipart
|
|
|
7 |
from email.mime.text import MIMEText
|
|
|
8 |
from pyExcelerator import Workbook, Font, XFStyle
|
|
|
9 |
import MySQLdb
|
|
|
10 |
import datetime
|
|
|
11 |
import json
|
|
|
12 |
import smtplib
|
|
|
13 |
import sys
|
|
|
14 |
import urllib2
|
|
|
15 |
import cookielib
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
# Initialize db connection settings.
|
|
|
19 |
#DB_HOST = "localhost"
|
|
|
20 |
DB_HOST = "192.168.190.114"
|
|
|
21 |
DB_USER = "root"
|
|
|
22 |
DB_PASSWORD = "shop2020"
|
|
|
23 |
DB_NAME = "transaction"
|
|
|
24 |
|
|
|
25 |
#MAILTO = ['rajneesharora@spiceretail.co.in', 'yukti.jain@spiceretail.co.in', 'sandeep.sachdeva@shop2020.in', 'chaitnaya.vats@shop2020.in', 'anupam.singh@shop2020.in']
|
|
|
26 |
#SENDER = "cnc.center@shop2020.in"
|
|
|
27 |
#PASSWORD = "5h0p2o2o"
|
|
|
28 |
#SUBJECT = "Product Report"
|
|
|
29 |
#SMTP_SERVER = "smtp.gmail.com"
|
|
|
30 |
#SMTP_PORT = 587
|
|
|
31 |
#
|
|
|
32 |
#TMP_FILE="/tmp/wallet_ageing.xls"
|
|
|
33 |
def getDbConnection():
|
|
|
34 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
35 |
|
|
|
36 |
def closeConnection(conn):
|
|
|
37 |
conn.close()
|
|
|
38 |
|
|
|
39 |
def getProductSaleData():
|
|
|
40 |
selectSql = '''SELECT b.userId, b.id AS rechargeId, SUM(h.amount) AS AmountCredited,
|
|
|
41 |
LEFT(b.creationTimestamp,10) AS createdOn, w.amount AS currentWalletAmount
|
|
|
42 |
FROM baseorder b
|
|
|
43 |
JOIN rechargeorder r ON b.id = r.immediateconsumedorder_digitalorder_baseorder_id
|
|
|
44 |
JOIN userwallethistory h ON h.orderId = b.id
|
|
|
45 |
JOIN userwallet w ON b.userId = w.userId
|
|
|
46 |
WHERE w.amount > 0
|
|
|
47 |
GROUP BY b.id
|
|
|
48 |
HAVING SUM(h.amount) > 0
|
|
|
49 |
ORDER BY b.userId, b.id DESC;
|
|
|
50 |
'''
|
|
|
51 |
conn = getDbConnection()
|
|
|
52 |
try:
|
|
|
53 |
# prepare a cursor object using cursor() method
|
|
|
54 |
cursor = conn.cursor()
|
|
|
55 |
# Execute the SQL command
|
|
|
56 |
# Fetch source id.
|
|
|
57 |
cursor.execute(selectSql)
|
|
|
58 |
result = cursor.fetchall()
|
|
|
59 |
activeUser = ''
|
|
|
60 |
toRefund = 0
|
|
|
61 |
amountLeft = 0
|
|
|
62 |
for r in result:
|
|
|
63 |
(userId, rechargeId, AmountCredited, createdOn, currentWalletAmount) = r[0:5]
|
|
|
64 |
int_currentAmount = int(currentWalletAmount)
|
|
|
65 |
int_amountCredited = int(AmountCredited)
|
|
|
66 |
if activeUser != userId:
|
|
|
67 |
if int_currentAmount > int_amountCredited :
|
|
|
68 |
amountLeft = int_currentAmount - int_amountCredited
|
|
|
69 |
toRefund = int_amountCredited
|
|
|
70 |
else :
|
|
|
71 |
amountLeft = 0
|
|
|
72 |
toRefund = int_currentAmount
|
|
|
73 |
else :
|
|
|
74 |
if amountLeft > int_amountCredited :
|
|
|
75 |
amountLeft = amountLeft - int_amountCredited
|
|
|
76 |
toRefund = int_amountCredited
|
|
|
77 |
else :
|
|
|
78 |
amountLeft = 0
|
|
|
79 |
toRefund = amountLeft
|
|
|
80 |
|
|
|
81 |
print userId, rechargeId, AmountCredited, createdOn, toRefund
|
|
|
82 |
activeUser = userId
|
|
|
83 |
|
|
|
84 |
except Exception as e:
|
|
|
85 |
print "Error: unable to fetch data"
|
|
|
86 |
print e
|
|
|
87 |
|
|
|
88 |
#def createXlsReport(monthdatesmap, prodsalesmap):
|
|
|
89 |
# workbook = Workbook()
|
|
|
90 |
# worksheet = workbook.add_sheet("Sheet 1")
|
|
|
91 |
# boldStyle = XFStyle()
|
|
|
92 |
# f = Font()
|
|
|
93 |
# f.bold = True
|
|
|
94 |
# boldStyle.font = f
|
|
|
95 |
#
|
|
|
96 |
# datecolmap = {}
|
|
|
97 |
# col = 6
|
|
|
98 |
# for monthnumber in monthdatesmap.keys():
|
|
|
99 |
# monthname = monthdatesmap[monthnumber][MONTHNAME]
|
|
|
100 |
# worksheet.write(0, col, monthname, boldStyle)
|
|
|
101 |
# worksheet.write(1, col, 'Month', boldStyle)
|
|
|
102 |
# datemap ={}
|
|
|
103 |
# datemap[0] = col
|
|
|
104 |
# datecolmap[monthnumber] = datemap
|
|
|
105 |
# col += 1
|
|
|
106 |
#
|
|
|
107 |
# worksheet.write(1, 0, 'Category', boldStyle)
|
|
|
108 |
# worksheet.write(1, 1, 'Sub Category', boldStyle)
|
|
|
109 |
# worksheet.write(1, 2, 'Brand', boldStyle)
|
|
|
110 |
# worksheet.write(1, 3, 'Model Name', boldStyle)
|
|
|
111 |
# worksheet.write(1, 4, 'Model Number', boldStyle)
|
|
|
112 |
# worksheet.write(1, 5, 'Color', boldStyle)
|
|
|
113 |
# for dayofmonth in monthdatesmap[monthnumber][DATES]:
|
|
|
114 |
# worksheet.write(1, col, dayofmonth, boldStyle)
|
|
|
115 |
# datemap[dayofmonth] = col
|
|
|
116 |
# col += 1
|
|
|
117 |
#
|
|
|
118 |
# row = 2
|
|
|
119 |
# prodkeys = prodsalesmap.keys()
|
|
|
120 |
# prodkeys.sort()
|
|
|
121 |
# for prodsale in prodkeys:
|
|
|
122 |
# (parent, category, brand, model_name, model_number, color) = prodsale
|
|
|
123 |
# worksheet.write(row, 0, parent, boldStyle)
|
|
|
124 |
# worksheet.write(row, 1, category, boldStyle)
|
|
|
125 |
# worksheet.write(row, 2, brand, boldStyle)
|
|
|
126 |
# worksheet.write(row, 3, model_name if model_name is not None else '', boldStyle)
|
|
|
127 |
# worksheet.write(row, 4, model_number if model_number is not None else '', boldStyle)
|
|
|
128 |
# worksheet.write(row, 5, color if color is not None else 'NA', boldStyle)
|
|
|
129 |
# for monthnumber in prodsalesmap[prodsale].keys():
|
|
|
130 |
# for dayofmonth in prodsalesmap[prodsale][monthnumber]:
|
|
|
131 |
# quantity = prodsalesmap[prodsale][monthnumber][dayofmonth]
|
|
|
132 |
# worksheet.write(row, datecolmap[monthnumber][dayofmonth], quantity)
|
|
|
133 |
# row += 1
|
|
|
134 |
#
|
|
|
135 |
# worksheet.panes_frozen = True
|
|
|
136 |
# worksheet.horz_split_pos = 2
|
|
|
137 |
# worksheet.vert_split_pos = 6
|
|
|
138 |
# workbook.save(TMP_FILE)
|
|
|
139 |
#
|
|
|
140 |
#def sendmail():
|
|
|
141 |
# mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
142 |
# mailServer.ehlo()
|
|
|
143 |
# mailServer.starttls()
|
|
|
144 |
# mailServer.ehlo()
|
|
|
145 |
#
|
|
|
146 |
# # Create the container (outer) email message.
|
|
|
147 |
# msg = MIMEMultipart()
|
|
|
148 |
# msg['Subject'] = SUBJECT + ' - ' + date.today().isoformat()
|
|
|
149 |
# msg['From'] = "bi@saholic.com"
|
|
|
150 |
# msg['To'] = 'sku-recipients@saholic.com'
|
|
|
151 |
# msg.preamble = SUBJECT + ' - ' + date.today().isoformat()
|
|
|
152 |
#
|
|
|
153 |
# fileMsg = MIMEBase('application','vnd.ms-excel')
|
|
|
154 |
# fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
155 |
# encoders.encode_base64(fileMsg)
|
|
|
156 |
# fileMsg.add_header('Content-Disposition','attachment;filename=Product-Report' + ' - ' + date.today().isoformat() + '.xls')
|
|
|
157 |
# msg.attach(fileMsg)
|
|
|
158 |
#
|
|
|
159 |
# mailServer.login(SENDER, PASSWORD)
|
|
|
160 |
# mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
161 |
|
|
|
162 |
def main():
|
|
|
163 |
getProductSaleData()
|
|
|
164 |
|
|
|
165 |
if __name__ == '__main__':
|
|
|
166 |
main()
|