Subversion Repositories SmartDukaan

Rev

Rev 14503 | Rev 14505 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14450 amit.gupta 1
'''
2
Created on Mar 13, 2015
3
 
4
@author: amit
5
'''
6
from datetime import date, datetime, timedelta
7
from dtr.storage.Mysql import getOrdersAfterDate
8
from email import encoders
9
from email.mime.base import MIMEBase
10
from email.mime.multipart import MIMEMultipart
11
from email.mime.text import MIMEText
12
from pymongo.mongo_client import MongoClient
14460 amit.gupta 13
from xlrd import open_workbook
14
from xlwt.Workbook import Workbook
14450 amit.gupta 15
import MySQLdb
14460 amit.gupta 16
from xlutils.copy import copy
14450 amit.gupta 17
import smtplib
18
import time
19
import xlwt
14460 amit.gupta 20
#from xlwt import 
14450 amit.gupta 21
 
22
client = MongoClient('mongodb://localhost:27017/')
23
 
24
SENDER = "cnc.center@shop2020.in"
25
PASSWORD = "5h0p2o2o"
26
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
27
SMTP_SERVER = "smtp.gmail.com"
28
SMTP_PORT = 587    
29
 
14460 amit.gupta 30
XLS_FILENAME = "dtrsourcesreco.xls"
31
boldStyle = xlwt.XFStyle()
32
f = xlwt.Font()
33
f.bold = True
34
boldStyle.font = f
35
i = -1
14450 amit.gupta 36
def generateFlipkartReco():
14460 amit.gupta 37
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 2)
38
    db = client.Dtr
39
    rb = open_workbook(XLS_FILENAME)
40
    wb = copy(rb)
41
    #wb = xlwt.Workbook()
42
    row = 0
43
    worksheet = wb.add_sheet("Flipkart")
44
    worksheet.write(row, inc(), 'Order Id', boldStyle)
45
    worksheet.write(row, inc(), 'User Id', boldStyle)
14474 amit.gupta 46
    worksheet.write(row, inc(), 'Username', boldStyle)
14460 amit.gupta 47
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
48
    worksheet.write(row, inc(), 'Product Title', boldStyle)
49
    worksheet.write(row, inc(), 'Price', boldStyle)
50
    worksheet.write(row, inc(), 'Quantity', boldStyle)
51
    worksheet.write(row, inc(), 'Status', boldStyle)
52
    worksheet.write(row, inc(), 'Detailed Status', boldStyle)
53
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
54
    worksheet.write(row, inc(), 'Our CashBack', boldStyle)
55
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
56
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
57
    worksheet.write(row, inc(), 'Category', boldStyle)
58
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
59
    worksheet.write(row, inc(), 'Aff Sale Amount', boldStyle)
60
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
61
    for row1 in curs:
62
        orderId = row1[0]
63
        order = db.merchantOrder.find_one({"orderId":orderId})
64
        if order is None:
65
            continue
66
        saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%d %B, %Y %I:%M %p").replace(hour=0, minute=0, second=0, microsecond=0).timetuple()))
67
        #saleTime1 = int(time.mktime((datetime.strptime(order['placedOn'], "%b %d, %Y %I:%M %p") + timedelta(seconds=-60)).timetuple()))
68
 
69
        for subOrder in order['subOrders']: 
14489 amit.gupta 70
            affs = list(db.flipkartOrderAffiliateInfo.find({"subOrders.productCode":subOrder.get("productCode"), "saleDateInt":saleTime}))
14460 amit.gupta 71
            row += 1
72
            global i
73
            i=-1
74
            worksheet.write(row, inc(), orderId)
75
            worksheet.write(row, inc(), row1[1])
14474 amit.gupta 76
            worksheet.write(row, inc(), row1[-1])
14460 amit.gupta 77
            worksheet.write(row, inc(), order['merchantOrderId'])
78
            worksheet.write(row, inc(), subOrder['productTitle'])
79
            worksheet.write(row, inc(), subOrder['amountPaid'])
80
            worksheet.write(row, inc(), subOrder['quantity'])
81
            worksheet.write(row, inc(), subOrder['status'])
82
            worksheet.write(row, inc(), subOrder['detailedStatus'])
83
            worksheet.write(row, inc(), subOrder['cashBackStatus'])
84
            worksheet.write(row, inc(), subOrder['cashBackAmount'])
85
            worksheet.write(row, inc(), subOrder['placedOn'])
86
            worksheet.write(row, inc(), order['subTagId'])
87
            for aff in affs:
14487 amit.gupta 88
                if aff['subTagId']== subOrder['subTagId']:
14460 amit.gupta 89
                    worksheet.write(row, inc(), aff['category'])
90
                    worksheet.write(row, inc(), aff['payOut'])
91
                    worksheet.write(row, inc(), aff['saleAmount'])
92
                    worksheet.write(row, inc(), aff['saleDate'])
93
                    break
94
 
95
 
96
        wb.save(XLS_FILENAME)
14450 amit.gupta 97
def generateSnapDealReco():
14460 amit.gupta 98
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 3)
14450 amit.gupta 99
    db = client.Dtr
100
 
14487 amit.gupta 101
    matchedList = []
14450 amit.gupta 102
    workbook = xlwt.Workbook()
14501 amit.gupta 103
    worksheet = workbook.add_sheet("Snapdeal Reconciled")
104
    worksheet1 = workbook.add_sheet("Snapdeal Reconciled All")
105
    setHeaders(worksheet, worksheet1)
14450 amit.gupta 106
    row = 0
14501 amit.gupta 107
    anotherrow = 0
14450 amit.gupta 108
    for row1 in curs:
109
        orderId = row1[0]
110
        order = db.merchantOrder.find_one({"orderId":orderId})
111
        if order is None:
112
            continue
14496 amit.gupta 113
        #saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
14450 amit.gupta 114
        aff = list(db.snapdealOrderAffiliateInfo.find({"subTagId":order["subTagId"]}))
14501 amit.gupta 115
        anotherrow += 1
14488 amit.gupta 116
        if len(aff) > 0 and order["subTagId"] not in matchedList: 
14501 amit.gupta 117
            matchedList.append(order["subTagId"])
118
            aff[0]['reconciled'] = True
119
            db.snapdealOrderAffiliateInfo.save(aff[0])
120
            order['reconciled'] = True
121
            db.merchantOrder.save(order)
122
            row += 1
123
            global i
124
            i=-1 
125
            worksheet.write(row, inc(), orderId)
126
            worksheet1.write(anotherrow, i, orderId)
127
            worksheet.write(row, inc(), row1[1])
128
            worksheet1.write(anotherrow, i, row1[1])
129
            worksheet.write(row, inc(), row1[-1])
130
            worksheet1.write(anotherrow, i, row1[-1])
131
            worksheet.write(row, inc(), order['merchantOrderId'])
132
            worksheet1.write(anotherrow, i, order['merchantOrderId'])
133
            worksheet.write(row, inc(), order['subTagId'])
134
            worksheet1.write(anotherrow, i, order['subTagId'])
135
            worksheet.write(row, inc(), order['placedOn'])
136
            worksheet1.write(anotherrow, i, order['placedOn'])
137
            worksheet.write(row, inc(), int(order['paidAmount']))
138
            worksheet1.write(anotherrow, i, int(order['paidAmount']))
14500 amit.gupta 139
            if int(order["paidAmount"]) == aff[0]["saleAmount"]:
14501 amit.gupta 140
                worksheet.write(row, inc(), aff[0]['saleDate'])
141
                worksheet1.write(anotherrow, i, aff[0]['saleDate'])
142
                worksheet.write(row, inc(), aff[0]['saleAmount'])
143
                worksheet1.write(anotherrow, i, aff[0]['saleAmount'])
144
                worksheet.write(row, inc(), aff[0]['payOut'])
145
                worksheet1.write(anotherrow, i, aff[0]['payOut'])
146
            k = i
14504 amit.gupta 147
            row -= 1
148
            anotherrow -= 1
14501 amit.gupta 149
            for subOrder in order['subOrders']:
150
                i = k
14503 amit.gupta 151
                row += 1
152
                anotherrow += 1
14501 amit.gupta 153
                worksheet.write(row, inc(), subOrder['productTitle'])
154
                worksheet1.write(anotherrow, i, subOrder['productTitle'])
155
                worksheet.write(row, inc(), subOrder['amountPaid'])
156
                worksheet1.write(anotherrow, i, subOrder['amountPaid'])
157
                worksheet.write(row, inc(), subOrder['quantity'])
158
                worksheet1.write(anotherrow, i, subOrder['quantity'])
159
                worksheet.write(row, inc(), subOrder['status'])
160
                worksheet1.write(anotherrow, i, subOrder['status'])
161
                worksheet.write(row, inc(), subOrder['cashBackStatus'])
162
                worksheet1.write(anotherrow, i, subOrder['cashBackStatus'])
163
                worksheet.write(row, inc(), subOrder['cashBackAmount'])
164
                worksheet1.write(anotherrow, i, subOrder['cashBackAmount'])
14504 amit.gupta 165
            row += 1
166
            anotherrow += 1
167
 
14501 amit.gupta 168
        else:
14502 amit.gupta 169
            i=-1 
14501 amit.gupta 170
            worksheet1.write(anotherrow, inc(), orderId)
171
            worksheet1.write(anotherrow, inc(), row1[1])
172
            worksheet1.write(anotherrow, inc(), row1[-1])
173
            worksheet1.write(anotherrow, inc(), order['merchantOrderId'])
174
            worksheet1.write(anotherrow, inc(), order['subTagId'])
175
            worksheet1.write(anotherrow, inc(), order['placedOn'])
176
            worksheet1.write(anotherrow, inc(), int(order['paidAmount']))
177
            worksheet1.write(anotherrow, inc(), "")
178
            worksheet1.write(anotherrow, inc(), "")
179
            worksheet1.write(anotherrow, inc(), "")
180
            worksheet1.write(anotherrow, inc(), 'Product Title', boldStyle)
181
            worksheet1.write(anotherrow, inc(), 'Price', boldStyle)
182
            worksheet1.write(anotherrow, inc(), 'Amoun Paid', boldStyle)
183
            worksheet1.write(anotherrow, inc(), 'Status', boldStyle)
184
            worksheet1.write(anotherrow, inc(), 'Cashback Status', boldStyle)
185
            worksheet1.write(anotherrow, inc(), 'CashBack', boldStyle)
186
            k = i
14504 amit.gupta 187
            anotherrow -= 1
14501 amit.gupta 188
            for subOrder in order['subOrders']:
14503 amit.gupta 189
                anotherrow += 1
14501 amit.gupta 190
                i = k
191
                worksheet.write(anotherrow, inc(), subOrder['productTitle'])
192
                worksheet.write(anotherrow, inc(), subOrder['amountPaid'])
193
                worksheet.write(anotherrow, inc(), subOrder['quantity'])
194
                worksheet.write(anotherrow, inc(), subOrder['status'])
195
                worksheet.write(anotherrow, inc(), subOrder['cashBackStatus'])
196
                worksheet.write(anotherrow, inc(), subOrder['cashBackAmount'])
14504 amit.gupta 197
            anotherrow += 1
14501 amit.gupta 198
 
199
 
14460 amit.gupta 200
        workbook.save(XLS_FILENAME)
14450 amit.gupta 201
 
202
def sendmail(email, message, title):
203
    if email == "":
204
        return
205
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
206
    mailServer.ehlo()
207
    mailServer.starttls()
208
    mailServer.ehlo()
209
 
210
    # Create the container (outer) email message.
211
    msg = MIMEMultipart()
212
    msg['Subject'] = title
213
    msg.preamble = title
214
    html_msg = MIMEText(message, 'html')
215
    msg.attach(html_msg)
216
 
217
    #snapdeal more to be added here
218
    snapdeal = MIMEBase('application', 'vnd.ms-excel')
14460 amit.gupta 219
    snapdeal.set_payload(file(XLS_FILENAME).read())
14450 amit.gupta 220
    encoders.encode_base64(snapdeal)
14460 amit.gupta 221
    snapdeal.add_header('Content-Disposition', 'attachment;filename=' + XLS_FILENAME)
14450 amit.gupta 222
    msg.attach(snapdeal)
223
 
224
 
225
    email.append('amit.gupta@shop2020.in')
226
    MAILTO = email 
227
    mailServer.login(SENDER, PASSWORD)
228
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
229
 
14501 amit.gupta 230
def setHeaders(worksheet, worksheet1):
231
    row = 0
232
    global i
233
    i=-1    
234
    worksheet.write(row, inc(), 'Order Id', boldStyle)
235
    worksheet.write(row, inc(), 'User Id', boldStyle)
236
    worksheet.write(row, inc(), 'Username', boldStyle)
237
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
238
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
239
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
240
    worksheet.write(row, inc(), 'Sale Amount', boldStyle)
241
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
242
    worksheet.write(row, inc(), 'Aff Sale Amt', boldStyle)
243
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
244
    worksheet.write(row, inc(), 'Product Title', boldStyle)
245
    worksheet.write(row, inc(), 'Price', boldStyle)
246
    worksheet.write(row, inc(), 'Quantity', boldStyle)
247
    worksheet.write(row, inc(), 'Status', boldStyle)
248
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
249
    worksheet.write(row, inc(), 'CashBack', boldStyle)
250
 
251
    worksheet1.write(row, inc(), 'Order Id', boldStyle)
252
    worksheet1.write(row, inc(), 'User Id', boldStyle)
253
    worksheet1.write(row, inc(), 'Username', boldStyle)
254
    worksheet1.write(row, inc(), 'Merchant Order Id', boldStyle)
255
    worksheet1.write(row, inc(), 'SubtagId', boldStyle)
256
    worksheet1.write(row, inc(), 'Sale Date', boldStyle)
257
    worksheet1.write(row, inc(), 'Sale Amount', boldStyle)
258
    worksheet1.write(row, inc(), 'Aff Sale Date', boldStyle)
259
    worksheet1.write(row, inc(), 'Aff Sale Amt', boldStyle)
260
    worksheet1.write(row, inc(), 'Aff PayOut', boldStyle)
261
    worksheet1.write(row, inc(), 'Product Title', boldStyle)
262
    worksheet1.write(row, inc(), 'Price', boldStyle)
263
    worksheet1.write(row, inc(), 'Quantity', boldStyle)
264
    worksheet1.write(row, inc(), 'Status', boldStyle)
265
    worksheet1.write(row, inc(), 'Cashback Status', boldStyle)
266
    worksheet1.write(row, inc(), 'CashBack', boldStyle) 
14450 amit.gupta 267
 
14460 amit.gupta 268
 
269
def inc():
270
    global i
271
    i+=1
272
    return i
273
 
14450 amit.gupta 274
def main():
275
    generateSnapDealReco()
14460 amit.gupta 276
    generateFlipkartReco()
14452 amit.gupta 277
    sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco")
14450 amit.gupta 278
 
279
if __name__ == '__main__':
14460 amit.gupta 280
    main()
281