Subversion Repositories SmartDukaan

Rev

Rev 14524 | Rev 14527 | 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
14524 amit.gupta 7
from dtr.storage.Mysql import getOrdersAfterDate, \
8
    getOrdersByTag
14450 amit.gupta 9
from email import encoders
10
from email.mime.base import MIMEBase
11
from email.mime.multipart import MIMEMultipart
12
from email.mime.text import MIMEText
13
from pymongo.mongo_client import MongoClient
14460 amit.gupta 14
from xlrd import open_workbook
14524 amit.gupta 15
from xlutils.copy import copy
14460 amit.gupta 16
from xlwt.Workbook import Workbook
14450 amit.gupta 17
import MySQLdb
18
import smtplib
19
import time
20
import xlwt
14460 amit.gupta 21
#from xlwt import 
14450 amit.gupta 22
 
23
client = MongoClient('mongodb://localhost:27017/')
24
 
25
SENDER = "cnc.center@shop2020.in"
26
PASSWORD = "5h0p2o2o"
27
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
28
SMTP_SERVER = "smtp.gmail.com"
29
SMTP_PORT = 587    
30
 
14460 amit.gupta 31
XLS_FILENAME = "dtrsourcesreco.xls"
32
boldStyle = xlwt.XFStyle()
33
f = xlwt.Font()
34
f.bold = True
35
boldStyle.font = f
36
i = -1
14450 amit.gupta 37
def generateFlipkartReco():
14508 amit.gupta 38
    global i
39
    i = -1
14460 amit.gupta 40
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 2)
41
    db = client.Dtr
42
    rb = open_workbook(XLS_FILENAME)
43
    wb = copy(rb)
44
    #wb = xlwt.Workbook()
45
    row = 0
46
    worksheet = wb.add_sheet("Flipkart")
47
    worksheet.write(row, inc(), 'Order Id', boldStyle)
48
    worksheet.write(row, inc(), 'User Id', boldStyle)
14474 amit.gupta 49
    worksheet.write(row, inc(), 'Username', boldStyle)
14460 amit.gupta 50
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
51
    worksheet.write(row, inc(), 'Product Title', boldStyle)
52
    worksheet.write(row, inc(), 'Price', boldStyle)
53
    worksheet.write(row, inc(), 'Quantity', boldStyle)
54
    worksheet.write(row, inc(), 'Status', boldStyle)
55
    worksheet.write(row, inc(), 'Detailed Status', boldStyle)
56
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
57
    worksheet.write(row, inc(), 'Our CashBack', boldStyle)
58
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
59
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
60
    worksheet.write(row, inc(), 'Category', boldStyle)
61
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
62
    worksheet.write(row, inc(), 'Aff Sale Amount', boldStyle)
63
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
64
    for row1 in curs:
65
        orderId = row1[0]
66
        order = db.merchantOrder.find_one({"orderId":orderId})
67
        if order is None:
68
            continue
69
        saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%d %B, %Y %I:%M %p").replace(hour=0, minute=0, second=0, microsecond=0).timetuple()))
70
        #saleTime1 = int(time.mktime((datetime.strptime(order['placedOn'], "%b %d, %Y %I:%M %p") + timedelta(seconds=-60)).timetuple()))
71
 
72
        for subOrder in order['subOrders']: 
14489 amit.gupta 73
            affs = list(db.flipkartOrderAffiliateInfo.find({"subOrders.productCode":subOrder.get("productCode"), "saleDateInt":saleTime}))
14460 amit.gupta 74
            row += 1
75
            i=-1
76
            worksheet.write(row, inc(), orderId)
77
            worksheet.write(row, inc(), row1[1])
14474 amit.gupta 78
            worksheet.write(row, inc(), row1[-1])
14460 amit.gupta 79
            worksheet.write(row, inc(), order['merchantOrderId'])
80
            worksheet.write(row, inc(), subOrder['productTitle'])
81
            worksheet.write(row, inc(), subOrder['amountPaid'])
82
            worksheet.write(row, inc(), subOrder['quantity'])
83
            worksheet.write(row, inc(), subOrder['status'])
84
            worksheet.write(row, inc(), subOrder['detailedStatus'])
85
            worksheet.write(row, inc(), subOrder['cashBackStatus'])
86
            worksheet.write(row, inc(), subOrder['cashBackAmount'])
87
            worksheet.write(row, inc(), subOrder['placedOn'])
88
            worksheet.write(row, inc(), order['subTagId'])
89
            for aff in affs:
14487 amit.gupta 90
                if aff['subTagId']== subOrder['subTagId']:
14460 amit.gupta 91
                    worksheet.write(row, inc(), aff['category'])
92
                    worksheet.write(row, inc(), aff['payOut'])
93
                    worksheet.write(row, inc(), aff['saleAmount'])
94
                    worksheet.write(row, inc(), aff['saleDate'])
95
                    break
96
 
97
 
98
        wb.save(XLS_FILENAME)
14450 amit.gupta 99
def generateSnapDealReco():
14524 amit.gupta 100
    global i
14460 amit.gupta 101
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 3)
14450 amit.gupta 102
    db = client.Dtr
14524 amit.gupta 103
    notReconciled = {}
14487 amit.gupta 104
    matchedList = []
14450 amit.gupta 105
    workbook = xlwt.Workbook()
14501 amit.gupta 106
    worksheet = workbook.add_sheet("Snapdeal Reconciled")
107
    worksheet1 = workbook.add_sheet("Snapdeal Reconciled All")
14508 amit.gupta 108
    worksheet2 = workbook.add_sheet("Snapdeal Orders not reconciled")
109
    affNotReconciledSheet = workbook.add_sheet("Snapdeal Aff not reconciled")
110
    setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet)
14450 amit.gupta 111
    row = 0
14501 amit.gupta 112
    anotherrow = 0
14508 amit.gupta 113
    row2 = 0
14450 amit.gupta 114
    for row1 in curs:
115
        orderId = row1[0]
116
        order = db.merchantOrder.find_one({"orderId":orderId})
117
        if order is None:
118
            continue
14496 amit.gupta 119
        #saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
14450 amit.gupta 120
        aff = list(db.snapdealOrderAffiliateInfo.find({"subTagId":order["subTagId"]}))
14501 amit.gupta 121
        anotherrow += 1
14524 amit.gupta 122
        if len(aff) > 0 and order["subTagId"] not in matchedList and int(order["paidAmount"])==int(aff[0]["saleAmount"]): 
14501 amit.gupta 123
            matchedList.append(order["subTagId"])
124
            aff[0]['reconciled'] = True
14524 amit.gupta 125
            db.snapdealOrderAffiliateInfo.update({"adId":aff[0].get("adId")}, {"$set":{"reconcile":True}}, multi=True)
14501 amit.gupta 126
            order['reconciled'] = True
127
            db.merchantOrder.save(order)
128
            row += 1
129
            i=-1 
130
            worksheet.write(row, inc(), orderId)
131
            worksheet1.write(anotherrow, i, orderId)
132
            worksheet.write(row, inc(), row1[1])
133
            worksheet1.write(anotherrow, i, row1[1])
134
            worksheet.write(row, inc(), row1[-1])
135
            worksheet1.write(anotherrow, i, row1[-1])
136
            worksheet.write(row, inc(), order['merchantOrderId'])
137
            worksheet1.write(anotherrow, i, order['merchantOrderId'])
138
            worksheet.write(row, inc(), order['subTagId'])
139
            worksheet1.write(anotherrow, i, order['subTagId'])
140
            worksheet.write(row, inc(), order['placedOn'])
141
            worksheet1.write(anotherrow, i, order['placedOn'])
142
            worksheet.write(row, inc(), int(order['paidAmount']))
143
            worksheet1.write(anotherrow, i, int(order['paidAmount']))
14524 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'])
14501 amit.gupta 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
14524 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']))
14524 amit.gupta 187
            timestamp1 = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
188
            notReconciled[orderId] = (anotherrow, i, int(order['paidAmount']), timestamp1, order["subTagId"]) 
189
            inc()
190
            inc()
191
            inc()
14501 amit.gupta 192
            k = i
14504 amit.gupta 193
            anotherrow -= 1
14508 amit.gupta 194
            row2 -= 1
14501 amit.gupta 195
            for subOrder in order['subOrders']:
14503 amit.gupta 196
                anotherrow += 1
14508 amit.gupta 197
                row2 += 1
14501 amit.gupta 198
                i = k
14505 amit.gupta 199
                worksheet1.write(anotherrow, inc(), subOrder['productTitle'])
14508 amit.gupta 200
                worksheet2.write(row2, i-3, subOrder['productTitle'])
14505 amit.gupta 201
                worksheet1.write(anotherrow, inc(), subOrder['amountPaid'])
14508 amit.gupta 202
                worksheet2.write(row2, i-3, subOrder['amountPaid'])
14505 amit.gupta 203
                worksheet1.write(anotherrow, inc(), subOrder['quantity'])
14508 amit.gupta 204
                worksheet2.write(row2, i-3, subOrder['quantity'])
14505 amit.gupta 205
                worksheet1.write(anotherrow, inc(), subOrder['status'])
14508 amit.gupta 206
                worksheet2.write(row2, i-3, subOrder['status'])
14505 amit.gupta 207
                worksheet1.write(anotherrow, inc(), subOrder['cashBackStatus'])
14508 amit.gupta 208
                worksheet2.write(row2, i-3, subOrder['cashBackStatus'])
14505 amit.gupta 209
                worksheet1.write(anotherrow, inc(), subOrder['cashBackAmount'])
14508 amit.gupta 210
                worksheet2.write(row2, i-3, subOrder['cashBackAmount'])
14524 amit.gupta 211
 
212
    last30Days =  date.today() - timedelta(days=30)
213
    int(time.mktime(last30Days.timetuple()))
214
    row = 0
215
    for offer in db.snapdealOrderAffiliateInfo.find({"reconciled":{"$exists":False}, 
216
                                                     "saleTime":{"$gt":int(time.mktime(last30Days.timetuple())),
217
                                                                 "$lt":int(time.mktime(date.today().timetuple()))}} ):
218
        row += 1
219
        i=-1
220
        affNotReconciledSheet.write(row, inc(), offer.get("adId")) 
221
        affNotReconciledSheet.write(row, inc(), offer.get("subTagId")) 
222
        affNotReconciledSheet.write(row, inc(), offer.get("saleDate")) 
223
        affNotReconciledSheet.write(row, inc(), offer.get("saleAmount")) 
224
        affNotReconciledSheet.write(row, inc(), offer.get("payOut")) 
225
        affNotReconciledSheet.write(row, inc(), offer.get("conversionStatus"))
226
 
227
        subTagId = offer.get("subTagId")
228
        saleTime = offer.get("saleTime")
229
        orders = getOrdersByTag(subTagId, 3)
230
        for order in orders:
231
            if notReconciled.has_key(order[0]):
232
                (roww, column, paidAmount, timestamp1, oSubTagId) = notReconciled[order[0]]
233
                if paidAmount==int(offer.get("saleAmount")) and timestamp1 <= saleTime and timestamp1 + 120 >= saleTime:
234
                    i = column
235
                    try:
236
                        worksheet1.write(roww, inc(), subTagId)        
237
                        worksheet1.write(roww, inc(), offer.get("saleAmount"))        
238
                        worksheet1.write(roww, inc(), offer.get("payOut"))
239
                        print "found match for user", order[1] 
240
                    except:
241
                        print "found another match for user", order[1] 
242
                        i=17
243
                        worksheet1.write(roww, inc(), subTagId)        
244
                        worksheet1.write(roww, inc(), offer.get("saleAmount"))        
245
                        worksheet1.write(roww, inc(), offer.get("payOut"))
246
 
247
 
14460 amit.gupta 248
        workbook.save(XLS_FILENAME)
14450 amit.gupta 249
 
250
def sendmail(email, message, title):
251
    if email == "":
252
        return
253
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
254
    mailServer.ehlo()
255
    mailServer.starttls()
256
    mailServer.ehlo()
257
 
258
    # Create the container (outer) email message.
259
    msg = MIMEMultipart()
260
    msg['Subject'] = title
261
    msg.preamble = title
262
    html_msg = MIMEText(message, 'html')
263
    msg.attach(html_msg)
264
 
265
    #snapdeal more to be added here
266
    snapdeal = MIMEBase('application', 'vnd.ms-excel')
14460 amit.gupta 267
    snapdeal.set_payload(file(XLS_FILENAME).read())
14450 amit.gupta 268
    encoders.encode_base64(snapdeal)
14460 amit.gupta 269
    snapdeal.add_header('Content-Disposition', 'attachment;filename=' + XLS_FILENAME)
14450 amit.gupta 270
    msg.attach(snapdeal)
271
 
272
 
273
    email.append('amit.gupta@shop2020.in')
274
    MAILTO = email 
275
    mailServer.login(SENDER, PASSWORD)
276
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
277
 
14508 amit.gupta 278
def setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet):
14505 amit.gupta 279
    boldStyle = xlwt.XFStyle()
280
    f = xlwt.Font()
281
    f.bold = True
282
    boldStyle.font = f
14501 amit.gupta 283
    row = 0
284
    global i
285
    i=-1    
286
    worksheet.write(row, inc(), 'Order Id', boldStyle)
287
    worksheet.write(row, inc(), 'User Id', boldStyle)
288
    worksheet.write(row, inc(), 'Username', boldStyle)
289
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
290
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
291
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
292
    worksheet.write(row, inc(), 'Sale Amount', boldStyle)
293
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
294
    worksheet.write(row, inc(), 'Aff Sale Amt', boldStyle)
295
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
296
    worksheet.write(row, inc(), 'Product Title', boldStyle)
297
    worksheet.write(row, inc(), 'Price', boldStyle)
298
    worksheet.write(row, inc(), 'Quantity', boldStyle)
299
    worksheet.write(row, inc(), 'Status', boldStyle)
300
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
301
    worksheet.write(row, inc(), 'CashBack', boldStyle)
302
 
14505 amit.gupta 303
    i = -1
14501 amit.gupta 304
    worksheet1.write(row, inc(), 'Order Id', boldStyle)
305
    worksheet1.write(row, inc(), 'User Id', boldStyle)
306
    worksheet1.write(row, inc(), 'Username', boldStyle)
307
    worksheet1.write(row, inc(), 'Merchant Order Id', boldStyle)
308
    worksheet1.write(row, inc(), 'SubtagId', boldStyle)
309
    worksheet1.write(row, inc(), 'Sale Date', boldStyle)
310
    worksheet1.write(row, inc(), 'Sale Amount', boldStyle)
311
    worksheet1.write(row, inc(), 'Aff Sale Date', boldStyle)
312
    worksheet1.write(row, inc(), 'Aff Sale Amt', boldStyle)
313
    worksheet1.write(row, inc(), 'Aff PayOut', boldStyle)
314
    worksheet1.write(row, inc(), 'Product Title', boldStyle)
315
    worksheet1.write(row, inc(), 'Price', boldStyle)
316
    worksheet1.write(row, inc(), 'Quantity', boldStyle)
317
    worksheet1.write(row, inc(), 'Status', boldStyle)
318
    worksheet1.write(row, inc(), 'Cashback Status', boldStyle)
319
    worksheet1.write(row, inc(), 'CashBack', boldStyle) 
14508 amit.gupta 320
 
321
    i = -1
322
    worksheet2.write(row, inc(), 'Order Id', boldStyle)
323
    worksheet2.write(row, inc(), 'User Id', boldStyle)
324
    worksheet2.write(row, inc(), 'Username', boldStyle)
325
    worksheet2.write(row, inc(), 'Merchant Order Id', boldStyle)
326
    worksheet2.write(row, inc(), 'SubtagId', boldStyle)
327
    worksheet2.write(row, inc(), 'Sale Date', boldStyle)
328
    worksheet2.write(row, inc(), 'Sale Amount', boldStyle)
329
    worksheet2.write(row, inc(), 'Product Title', boldStyle)
330
    worksheet2.write(row, inc(), 'Price', boldStyle)
331
    worksheet2.write(row, inc(), 'Quantity', boldStyle)
332
    worksheet2.write(row, inc(), 'Status', boldStyle)
333
    worksheet2.write(row, inc(), 'Cashback Status', boldStyle)
334
    worksheet2.write(row, inc(), 'CashBack', boldStyle)
14450 amit.gupta 335
 
14508 amit.gupta 336
    i =-1
337
    affNotReconciledSheet.write(row, inc(), 'AdId', boldStyle) 
338
    affNotReconciledSheet.write(row, inc(), 'subTagId', boldStyle) 
339
    affNotReconciledSheet.write(row, inc(), 'Sale Date', boldStyle) 
340
    affNotReconciledSheet.write(row, inc(), 'Sale Amount', boldStyle) 
341
    affNotReconciledSheet.write(row, inc(), 'Pay Out', boldStyle) 
342
    affNotReconciledSheet.write(row, inc(), 'Status', boldStyle) 
14524 amit.gupta 343
 
344
def getUserAmountReconciled():
345
    pass
346
 
347
 
14460 amit.gupta 348
def inc():
349
    global i
350
    i+=1
351
    return i
352
 
14450 amit.gupta 353
def main():
354
    generateSnapDealReco()
14524 amit.gupta 355
    #generateFlipkartReco()
14525 amit.gupta 356
    sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco")
14450 amit.gupta 357
 
358
if __name__ == '__main__':
14460 amit.gupta 359
    main()
360