Subversion Repositories SmartDukaan

Rev

Rev 14511 | Rev 14524 | 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():
14508 amit.gupta 37
    global i
38
    i = -1
14460 amit.gupta 39
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 2)
40
    db = client.Dtr
41
    rb = open_workbook(XLS_FILENAME)
42
    wb = copy(rb)
43
    #wb = xlwt.Workbook()
44
    row = 0
45
    worksheet = wb.add_sheet("Flipkart")
46
    worksheet.write(row, inc(), 'Order Id', boldStyle)
47
    worksheet.write(row, inc(), 'User Id', boldStyle)
14474 amit.gupta 48
    worksheet.write(row, inc(), 'Username', boldStyle)
14460 amit.gupta 49
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
50
    worksheet.write(row, inc(), 'Product Title', boldStyle)
51
    worksheet.write(row, inc(), 'Price', boldStyle)
52
    worksheet.write(row, inc(), 'Quantity', boldStyle)
53
    worksheet.write(row, inc(), 'Status', boldStyle)
54
    worksheet.write(row, inc(), 'Detailed Status', boldStyle)
55
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
56
    worksheet.write(row, inc(), 'Our CashBack', boldStyle)
57
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
58
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
59
    worksheet.write(row, inc(), 'Category', boldStyle)
60
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
61
    worksheet.write(row, inc(), 'Aff Sale Amount', boldStyle)
62
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
63
    for row1 in curs:
64
        orderId = row1[0]
65
        order = db.merchantOrder.find_one({"orderId":orderId})
66
        if order is None:
67
            continue
68
        saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%d %B, %Y %I:%M %p").replace(hour=0, minute=0, second=0, microsecond=0).timetuple()))
69
        #saleTime1 = int(time.mktime((datetime.strptime(order['placedOn'], "%b %d, %Y %I:%M %p") + timedelta(seconds=-60)).timetuple()))
70
 
71
        for subOrder in order['subOrders']: 
14489 amit.gupta 72
            affs = list(db.flipkartOrderAffiliateInfo.find({"subOrders.productCode":subOrder.get("productCode"), "saleDateInt":saleTime}))
14460 amit.gupta 73
            row += 1
74
            i=-1
75
            worksheet.write(row, inc(), orderId)
76
            worksheet.write(row, inc(), row1[1])
14474 amit.gupta 77
            worksheet.write(row, inc(), row1[-1])
14460 amit.gupta 78
            worksheet.write(row, inc(), order['merchantOrderId'])
79
            worksheet.write(row, inc(), subOrder['productTitle'])
80
            worksheet.write(row, inc(), subOrder['amountPaid'])
81
            worksheet.write(row, inc(), subOrder['quantity'])
82
            worksheet.write(row, inc(), subOrder['status'])
83
            worksheet.write(row, inc(), subOrder['detailedStatus'])
84
            worksheet.write(row, inc(), subOrder['cashBackStatus'])
85
            worksheet.write(row, inc(), subOrder['cashBackAmount'])
86
            worksheet.write(row, inc(), subOrder['placedOn'])
87
            worksheet.write(row, inc(), order['subTagId'])
88
            for aff in affs:
14487 amit.gupta 89
                if aff['subTagId']== subOrder['subTagId']:
14460 amit.gupta 90
                    worksheet.write(row, inc(), aff['category'])
91
                    worksheet.write(row, inc(), aff['payOut'])
92
                    worksheet.write(row, inc(), aff['saleAmount'])
93
                    worksheet.write(row, inc(), aff['saleDate'])
94
                    break
95
 
96
 
97
        wb.save(XLS_FILENAME)
14450 amit.gupta 98
def generateSnapDealReco():
14460 amit.gupta 99
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 3)
14450 amit.gupta 100
    db = client.Dtr
101
 
14487 amit.gupta 102
    matchedList = []
14450 amit.gupta 103
    workbook = xlwt.Workbook()
14501 amit.gupta 104
    worksheet = workbook.add_sheet("Snapdeal Reconciled")
105
    worksheet1 = workbook.add_sheet("Snapdeal Reconciled All")
14508 amit.gupta 106
    worksheet2 = workbook.add_sheet("Snapdeal Orders not reconciled")
107
    affNotReconciledSheet = workbook.add_sheet("Snapdeal Aff not reconciled")
108
    setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet)
14450 amit.gupta 109
    row = 0
14501 amit.gupta 110
    anotherrow = 0
14508 amit.gupta 111
    row2 = 0
14450 amit.gupta 112
    for row1 in curs:
113
        orderId = row1[0]
114
        order = db.merchantOrder.find_one({"orderId":orderId})
115
        if order is None:
116
            continue
14496 amit.gupta 117
        #saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
14450 amit.gupta 118
        aff = list(db.snapdealOrderAffiliateInfo.find({"subTagId":order["subTagId"]}))
14501 amit.gupta 119
        anotherrow += 1
14488 amit.gupta 120
        if len(aff) > 0 and order["subTagId"] not in matchedList: 
14501 amit.gupta 121
            matchedList.append(order["subTagId"])
122
            aff[0]['reconciled'] = True
123
            db.snapdealOrderAffiliateInfo.save(aff[0])
124
            order['reconciled'] = True
125
            db.merchantOrder.save(order)
126
            row += 1
127
            global i
128
            i=-1 
129
            worksheet.write(row, inc(), orderId)
130
            worksheet1.write(anotherrow, i, orderId)
131
            worksheet.write(row, inc(), row1[1])
132
            worksheet1.write(anotherrow, i, row1[1])
133
            worksheet.write(row, inc(), row1[-1])
134
            worksheet1.write(anotherrow, i, row1[-1])
135
            worksheet.write(row, inc(), order['merchantOrderId'])
136
            worksheet1.write(anotherrow, i, order['merchantOrderId'])
137
            worksheet.write(row, inc(), order['subTagId'])
138
            worksheet1.write(anotherrow, i, order['subTagId'])
139
            worksheet.write(row, inc(), order['placedOn'])
140
            worksheet1.write(anotherrow, i, order['placedOn'])
141
            worksheet.write(row, inc(), int(order['paidAmount']))
142
            worksheet1.write(anotherrow, i, int(order['paidAmount']))
14500 amit.gupta 143
            if int(order["paidAmount"]) == aff[0]["saleAmount"]:
14501 amit.gupta 144
                worksheet.write(row, inc(), aff[0]['saleDate'])
145
                worksheet1.write(anotherrow, i, aff[0]['saleDate'])
146
                worksheet.write(row, inc(), aff[0]['saleAmount'])
147
                worksheet1.write(anotherrow, i, aff[0]['saleAmount'])
148
                worksheet.write(row, inc(), aff[0]['payOut'])
149
                worksheet1.write(anotherrow, i, aff[0]['payOut'])
150
            k = i
14504 amit.gupta 151
            row -= 1
152
            anotherrow -= 1
14501 amit.gupta 153
            for subOrder in order['subOrders']:
154
                i = k
14503 amit.gupta 155
                row += 1
156
                anotherrow += 1
14501 amit.gupta 157
                worksheet.write(row, inc(), subOrder['productTitle'])
158
                worksheet1.write(anotherrow, i, subOrder['productTitle'])
159
                worksheet.write(row, inc(), subOrder['amountPaid'])
160
                worksheet1.write(anotherrow, i, subOrder['amountPaid'])
161
                worksheet.write(row, inc(), subOrder['quantity'])
162
                worksheet1.write(anotherrow, i, subOrder['quantity'])
163
                worksheet.write(row, inc(), subOrder['status'])
164
                worksheet1.write(anotherrow, i, subOrder['status'])
165
                worksheet.write(row, inc(), subOrder['cashBackStatus'])
166
                worksheet1.write(anotherrow, i, subOrder['cashBackStatus'])
167
                worksheet.write(row, inc(), subOrder['cashBackAmount'])
168
                worksheet1.write(anotherrow, i, subOrder['cashBackAmount'])
14504 amit.gupta 169
 
14501 amit.gupta 170
        else:
14508 amit.gupta 171
            row2 +=1
14502 amit.gupta 172
            i=-1 
14501 amit.gupta 173
            worksheet1.write(anotherrow, inc(), orderId)
14508 amit.gupta 174
            worksheet2.write(row2, i, orderId)
14501 amit.gupta 175
            worksheet1.write(anotherrow, inc(), row1[1])
14508 amit.gupta 176
            worksheet2.write(row2, i, row1[1])
14501 amit.gupta 177
            worksheet1.write(anotherrow, inc(), row1[-1])
14508 amit.gupta 178
            worksheet2.write(row2, i, row1[-1])
14501 amit.gupta 179
            worksheet1.write(anotherrow, inc(), order['merchantOrderId'])
14508 amit.gupta 180
            worksheet2.write(row2, i, order['merchantOrderId'])
14501 amit.gupta 181
            worksheet1.write(anotherrow, inc(), order['subTagId'])
14508 amit.gupta 182
            worksheet2.write(row2, i, order['subTagId'])
14501 amit.gupta 183
            worksheet1.write(anotherrow, inc(), order['placedOn'])
14508 amit.gupta 184
            worksheet2.write(row2, i, order['placedOn'])
14501 amit.gupta 185
            worksheet1.write(anotherrow, inc(), int(order['paidAmount']))
14508 amit.gupta 186
            worksheet2.write(row2, i, int(order['paidAmount']))
14501 amit.gupta 187
            worksheet1.write(anotherrow, inc(), "")
188
            worksheet1.write(anotherrow, inc(), "")
189
            worksheet1.write(anotherrow, inc(), "")
190
            k = i
14504 amit.gupta 191
            anotherrow -= 1
14508 amit.gupta 192
            row2 -= 1
14501 amit.gupta 193
            for subOrder in order['subOrders']:
14503 amit.gupta 194
                anotherrow += 1
14508 amit.gupta 195
                row2 += 1
14501 amit.gupta 196
                i = k
14505 amit.gupta 197
                worksheet1.write(anotherrow, inc(), subOrder['productTitle'])
14508 amit.gupta 198
                worksheet2.write(row2, i-3, subOrder['productTitle'])
14505 amit.gupta 199
                worksheet1.write(anotherrow, inc(), subOrder['amountPaid'])
14508 amit.gupta 200
                worksheet2.write(row2, i-3, subOrder['amountPaid'])
14505 amit.gupta 201
                worksheet1.write(anotherrow, inc(), subOrder['quantity'])
14508 amit.gupta 202
                worksheet2.write(row2, i-3, subOrder['quantity'])
14505 amit.gupta 203
                worksheet1.write(anotherrow, inc(), subOrder['status'])
14508 amit.gupta 204
                worksheet2.write(row2, i-3, subOrder['status'])
14505 amit.gupta 205
                worksheet1.write(anotherrow, inc(), subOrder['cashBackStatus'])
14508 amit.gupta 206
                worksheet2.write(row2, i-3, subOrder['cashBackStatus'])
14505 amit.gupta 207
                worksheet1.write(anotherrow, inc(), subOrder['cashBackAmount'])
14508 amit.gupta 208
                worksheet2.write(row2, i-3, subOrder['cashBackAmount'])
14501 amit.gupta 209
 
14460 amit.gupta 210
        workbook.save(XLS_FILENAME)
14450 amit.gupta 211
 
212
def sendmail(email, message, title):
213
    if email == "":
214
        return
215
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
216
    mailServer.ehlo()
217
    mailServer.starttls()
218
    mailServer.ehlo()
219
 
220
    # Create the container (outer) email message.
221
    msg = MIMEMultipart()
222
    msg['Subject'] = title
223
    msg.preamble = title
224
    html_msg = MIMEText(message, 'html')
225
    msg.attach(html_msg)
226
 
227
    #snapdeal more to be added here
228
    snapdeal = MIMEBase('application', 'vnd.ms-excel')
14460 amit.gupta 229
    snapdeal.set_payload(file(XLS_FILENAME).read())
14450 amit.gupta 230
    encoders.encode_base64(snapdeal)
14460 amit.gupta 231
    snapdeal.add_header('Content-Disposition', 'attachment;filename=' + XLS_FILENAME)
14450 amit.gupta 232
    msg.attach(snapdeal)
233
 
234
 
235
    email.append('amit.gupta@shop2020.in')
236
    MAILTO = email 
237
    mailServer.login(SENDER, PASSWORD)
238
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
239
 
14508 amit.gupta 240
def setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet):
14505 amit.gupta 241
    boldStyle = xlwt.XFStyle()
242
    f = xlwt.Font()
243
    f.bold = True
244
    boldStyle.font = f
14501 amit.gupta 245
    row = 0
246
    global i
247
    i=-1    
248
    worksheet.write(row, inc(), 'Order Id', boldStyle)
249
    worksheet.write(row, inc(), 'User Id', boldStyle)
250
    worksheet.write(row, inc(), 'Username', boldStyle)
251
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
252
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
253
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
254
    worksheet.write(row, inc(), 'Sale Amount', boldStyle)
255
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
256
    worksheet.write(row, inc(), 'Aff Sale Amt', boldStyle)
257
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
258
    worksheet.write(row, inc(), 'Product Title', boldStyle)
259
    worksheet.write(row, inc(), 'Price', boldStyle)
260
    worksheet.write(row, inc(), 'Quantity', boldStyle)
261
    worksheet.write(row, inc(), 'Status', boldStyle)
262
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
263
    worksheet.write(row, inc(), 'CashBack', boldStyle)
264
 
14505 amit.gupta 265
    i = -1
14501 amit.gupta 266
    worksheet1.write(row, inc(), 'Order Id', boldStyle)
267
    worksheet1.write(row, inc(), 'User Id', boldStyle)
268
    worksheet1.write(row, inc(), 'Username', boldStyle)
269
    worksheet1.write(row, inc(), 'Merchant Order Id', boldStyle)
270
    worksheet1.write(row, inc(), 'SubtagId', boldStyle)
271
    worksheet1.write(row, inc(), 'Sale Date', boldStyle)
272
    worksheet1.write(row, inc(), 'Sale Amount', boldStyle)
273
    worksheet1.write(row, inc(), 'Aff Sale Date', boldStyle)
274
    worksheet1.write(row, inc(), 'Aff Sale Amt', boldStyle)
275
    worksheet1.write(row, inc(), 'Aff PayOut', boldStyle)
276
    worksheet1.write(row, inc(), 'Product Title', boldStyle)
277
    worksheet1.write(row, inc(), 'Price', boldStyle)
278
    worksheet1.write(row, inc(), 'Quantity', boldStyle)
279
    worksheet1.write(row, inc(), 'Status', boldStyle)
280
    worksheet1.write(row, inc(), 'Cashback Status', boldStyle)
281
    worksheet1.write(row, inc(), 'CashBack', boldStyle) 
14508 amit.gupta 282
 
283
    i = -1
284
    worksheet2.write(row, inc(), 'Order Id', boldStyle)
285
    worksheet2.write(row, inc(), 'User Id', boldStyle)
286
    worksheet2.write(row, inc(), 'Username', boldStyle)
287
    worksheet2.write(row, inc(), 'Merchant Order Id', boldStyle)
288
    worksheet2.write(row, inc(), 'SubtagId', boldStyle)
289
    worksheet2.write(row, inc(), 'Sale Date', boldStyle)
290
    worksheet2.write(row, inc(), 'Sale Amount', boldStyle)
291
    worksheet2.write(row, inc(), 'Product Title', boldStyle)
292
    worksheet2.write(row, inc(), 'Price', boldStyle)
293
    worksheet2.write(row, inc(), 'Quantity', boldStyle)
294
    worksheet2.write(row, inc(), 'Status', boldStyle)
295
    worksheet2.write(row, inc(), 'Cashback Status', boldStyle)
296
    worksheet2.write(row, inc(), 'CashBack', boldStyle)
14450 amit.gupta 297
 
14508 amit.gupta 298
    i =-1
299
    affNotReconciledSheet.write(row, inc(), 'AdId', boldStyle) 
300
    affNotReconciledSheet.write(row, inc(), 'subTagId', boldStyle) 
301
    affNotReconciledSheet.write(row, inc(), 'Sale Date', boldStyle) 
302
    affNotReconciledSheet.write(row, inc(), 'Sale Amount', boldStyle) 
303
    affNotReconciledSheet.write(row, inc(), 'Pay Out', boldStyle) 
304
    affNotReconciledSheet.write(row, inc(), 'Status', boldStyle) 
305
    db = client.Dtr
14511 amit.gupta 306
    last30Days =  date.today() - timedelta(days=30)
307
    int(time.mktime(last30Days.timetuple()))
14512 amit.gupta 308
    for offer in db.snapdealOrderAffiliateInfo.find({"reconciled":{"$exists":False}, 
309
                                                     "saleTime":{"$gt":int(time.mktime(last30Days.timetuple())),
310
                                                                 "$lt":int(time.mktime(date.today().timetuple()))}} ):
14508 amit.gupta 311
        row += 1
312
        i=-1
313
        affNotReconciledSheet.write(row, inc(), offer.get("adId")) 
314
        affNotReconciledSheet.write(row, inc(), offer.get("subTagId")) 
315
        affNotReconciledSheet.write(row, inc(), offer.get("saleDate")) 
316
        affNotReconciledSheet.write(row, inc(), offer.get("saleAmount")) 
317
        affNotReconciledSheet.write(row, inc(), offer.get("payOut")) 
318
        affNotReconciledSheet.write(row, inc(), offer.get("conversionStatus")) 
14460 amit.gupta 319
 
320
def inc():
321
    global i
322
    i+=1
323
    return i
324
 
14450 amit.gupta 325
def main():
326
    generateSnapDealReco()
14460 amit.gupta 327
    generateFlipkartReco()
14506 amit.gupta 328
    sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco")
14450 amit.gupta 329
 
330
if __name__ == '__main__':
14460 amit.gupta 331
    main()
332