Subversion Repositories SmartDukaan

Rev

Rev 14613 | Rev 14616 | 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
14613 amit.gupta 7
from dtr.main import Store
8
from dtr.storage import Mysql
9
from dtr.storage.Mysql import getOrdersAfterDate, getOrdersByTag
14450 amit.gupta 10
from email import encoders
11
from email.mime.base import MIMEBase
12
from email.mime.multipart import MIMEMultipart
13
from email.mime.text import MIMEText
14
from pymongo.mongo_client import MongoClient
14460 amit.gupta 15
from xlrd import open_workbook
14524 amit.gupta 16
from xlutils.copy import copy
14460 amit.gupta 17
from xlwt.Workbook import Workbook
14450 amit.gupta 18
import MySQLdb
19
import smtplib
20
import time
21
import xlwt
14460 amit.gupta 22
#from xlwt import 
14450 amit.gupta 23
 
24
client = MongoClient('mongodb://localhost:27017/')
25
 
26
SENDER = "cnc.center@shop2020.in"
27
PASSWORD = "5h0p2o2o"
28
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
29
SMTP_SERVER = "smtp.gmail.com"
30
SMTP_PORT = 587    
31
 
14460 amit.gupta 32
XLS_FILENAME = "dtrsourcesreco.xls"
33
boldStyle = xlwt.XFStyle()
34
f = xlwt.Font()
35
f.bold = True
36
boldStyle.font = f
37
i = -1
14450 amit.gupta 38
def generateFlipkartReco():
14508 amit.gupta 39
    global i
40
    i = -1
14460 amit.gupta 41
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 2)
42
    db = client.Dtr
43
    rb = open_workbook(XLS_FILENAME)
44
    wb = copy(rb)
45
    #wb = xlwt.Workbook()
46
    row = 0
47
    worksheet = wb.add_sheet("Flipkart")
48
    worksheet.write(row, inc(), 'Order Id', boldStyle)
49
    worksheet.write(row, inc(), 'User Id', boldStyle)
14474 amit.gupta 50
    worksheet.write(row, inc(), 'Username', boldStyle)
14460 amit.gupta 51
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
52
    worksheet.write(row, inc(), 'Product Title', boldStyle)
53
    worksheet.write(row, inc(), 'Price', boldStyle)
54
    worksheet.write(row, inc(), 'Quantity', boldStyle)
55
    worksheet.write(row, inc(), 'Status', boldStyle)
56
    worksheet.write(row, inc(), 'Detailed Status', boldStyle)
57
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
58
    worksheet.write(row, inc(), 'Our CashBack', boldStyle)
59
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
60
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
61
    worksheet.write(row, inc(), 'Category', boldStyle)
62
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
63
    worksheet.write(row, inc(), 'Aff Sale Amount', boldStyle)
64
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
65
    for row1 in curs:
66
        orderId = row1[0]
67
        order = db.merchantOrder.find_one({"orderId":orderId})
68
        if order is None:
69
            continue
70
        saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%d %B, %Y %I:%M %p").replace(hour=0, minute=0, second=0, microsecond=0).timetuple()))
71
        #saleTime1 = int(time.mktime((datetime.strptime(order['placedOn'], "%b %d, %Y %I:%M %p") + timedelta(seconds=-60)).timetuple()))
72
 
73
        for subOrder in order['subOrders']: 
14489 amit.gupta 74
            affs = list(db.flipkartOrderAffiliateInfo.find({"subOrders.productCode":subOrder.get("productCode"), "saleDateInt":saleTime}))
14460 amit.gupta 75
            row += 1
76
            i=-1
77
            worksheet.write(row, inc(), orderId)
78
            worksheet.write(row, inc(), row1[1])
14474 amit.gupta 79
            worksheet.write(row, inc(), row1[-1])
14460 amit.gupta 80
            worksheet.write(row, inc(), order['merchantOrderId'])
81
            worksheet.write(row, inc(), subOrder['productTitle'])
82
            worksheet.write(row, inc(), subOrder['amountPaid'])
83
            worksheet.write(row, inc(), subOrder['quantity'])
84
            worksheet.write(row, inc(), subOrder['status'])
85
            worksheet.write(row, inc(), subOrder['detailedStatus'])
86
            worksheet.write(row, inc(), subOrder['cashBackStatus'])
87
            worksheet.write(row, inc(), subOrder['cashBackAmount'])
88
            worksheet.write(row, inc(), subOrder['placedOn'])
89
            worksheet.write(row, inc(), order['subTagId'])
90
            for aff in affs:
14487 amit.gupta 91
                if aff['subTagId']== subOrder['subTagId']:
14460 amit.gupta 92
                    worksheet.write(row, inc(), aff['category'])
93
                    worksheet.write(row, inc(), aff['payOut'])
94
                    worksheet.write(row, inc(), aff['saleAmount'])
95
                    worksheet.write(row, inc(), aff['saleDate'])
96
                    break
97
 
98
 
99
        wb.save(XLS_FILENAME)
14450 amit.gupta 100
def generateSnapDealReco():
14524 amit.gupta 101
    global i
14460 amit.gupta 102
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 3)
14450 amit.gupta 103
    db = client.Dtr
14524 amit.gupta 104
    notReconciled = {}
14487 amit.gupta 105
    matchedList = []
14450 amit.gupta 106
    workbook = xlwt.Workbook()
14501 amit.gupta 107
    worksheet = workbook.add_sheet("Snapdeal Reconciled")
108
    worksheet1 = workbook.add_sheet("Snapdeal Reconciled All")
14508 amit.gupta 109
    worksheet2 = workbook.add_sheet("Snapdeal Orders not reconciled")
110
    affNotReconciledSheet = workbook.add_sheet("Snapdeal Aff not reconciled")
111
    setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet)
14450 amit.gupta 112
    row = 0
14501 amit.gupta 113
    anotherrow = 0
14508 amit.gupta 114
    row2 = 0
14450 amit.gupta 115
    for row1 in curs:
116
        orderId = row1[0]
117
        order = db.merchantOrder.find_one({"orderId":orderId})
118
        if order is None:
119
            continue
14496 amit.gupta 120
        #saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
14450 amit.gupta 121
        aff = list(db.snapdealOrderAffiliateInfo.find({"subTagId":order["subTagId"]}))
14501 amit.gupta 122
        anotherrow += 1
14524 amit.gupta 123
        if len(aff) > 0 and order["subTagId"] not in matchedList and int(order["paidAmount"])==int(aff[0]["saleAmount"]): 
14501 amit.gupta 124
            matchedList.append(order["subTagId"])
14610 amit.gupta 125
            db.snapdealOrderAffiliateInfo.update({"adId":aff[0].get("adId")}, {"$set":{"reconciled":True, "orderId":orderId}}, multi=True)
14501 amit.gupta 126
            order['reconciled'] = True
14610 amit.gupta 127
            order['adId'] = aff[0].get("adId")
14501 amit.gupta 128
            db.merchantOrder.save(order)
129
            row += 1
130
            i=-1 
131
            worksheet.write(row, inc(), orderId)
132
            worksheet1.write(anotherrow, i, orderId)
133
            worksheet.write(row, inc(), row1[1])
134
            worksheet1.write(anotherrow, i, row1[1])
14601 amit.gupta 135
            worksheet.write(row, inc(), row1[-2])
136
            worksheet1.write(anotherrow, i, row1[-2])
14501 amit.gupta 137
            worksheet.write(row, inc(), row1[-1])
138
            worksheet1.write(anotherrow, i, row1[-1])
139
            worksheet.write(row, inc(), order['merchantOrderId'])
140
            worksheet1.write(anotherrow, i, order['merchantOrderId'])
141
            worksheet.write(row, inc(), order['subTagId'])
142
            worksheet1.write(anotherrow, i, order['subTagId'])
143
            worksheet.write(row, inc(), order['placedOn'])
144
            worksheet1.write(anotherrow, i, order['placedOn'])
145
            worksheet.write(row, inc(), int(order['paidAmount']))
146
            worksheet1.write(anotherrow, i, int(order['paidAmount']))
14524 amit.gupta 147
            worksheet.write(row, inc(), aff[0]['saleDate'])
148
            worksheet1.write(anotherrow, i, aff[0]['saleDate'])
149
            worksheet.write(row, inc(), aff[0]['saleAmount'])
150
            worksheet1.write(anotherrow, i, aff[0]['saleAmount'])
151
            worksheet.write(row, inc(), aff[0]['payOut'])
152
            worksheet1.write(anotherrow, i, aff[0]['payOut'])
14601 amit.gupta 153
            worksheet.write(row, inc(), aff[0]['ip'])
154
            worksheet1.write(anotherrow, i, aff[0]['ip'])
14501 amit.gupta 155
            k = i
14504 amit.gupta 156
            row -= 1
157
            anotherrow -= 1
14501 amit.gupta 158
            for subOrder in order['subOrders']:
159
                i = k
14503 amit.gupta 160
                row += 1
161
                anotherrow += 1
14501 amit.gupta 162
                worksheet.write(row, inc(), subOrder['productTitle'])
163
                worksheet1.write(anotherrow, i, subOrder['productTitle'])
164
                worksheet.write(row, inc(), subOrder['amountPaid'])
165
                worksheet1.write(anotherrow, i, subOrder['amountPaid'])
166
                worksheet.write(row, inc(), subOrder['quantity'])
167
                worksheet1.write(anotherrow, i, subOrder['quantity'])
168
                worksheet.write(row, inc(), subOrder['status'])
169
                worksheet1.write(anotherrow, i, subOrder['status'])
170
                worksheet.write(row, inc(), subOrder['cashBackStatus'])
171
                worksheet1.write(anotherrow, i, subOrder['cashBackStatus'])
172
                worksheet.write(row, inc(), subOrder['cashBackAmount'])
173
                worksheet1.write(anotherrow, i, subOrder['cashBackAmount'])
14504 amit.gupta 174
 
14501 amit.gupta 175
        else:
14524 amit.gupta 176
            i=-1
14501 amit.gupta 177
            worksheet1.write(anotherrow, inc(), orderId)
178
            worksheet1.write(anotherrow, inc(), row1[1])
14601 amit.gupta 179
            worksheet1.write(anotherrow, inc(), row1[-2])
14501 amit.gupta 180
            worksheet1.write(anotherrow, inc(), row1[-1])
181
            worksheet1.write(anotherrow, inc(), order['merchantOrderId'])
182
            worksheet1.write(anotherrow, inc(), order['subTagId'])
183
            worksheet1.write(anotherrow, inc(), order['placedOn'])
184
            worksheet1.write(anotherrow, inc(), int(order['paidAmount']))
14524 amit.gupta 185
            timestamp1 = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
186
            notReconciled[orderId] = (anotherrow, i, int(order['paidAmount']), timestamp1, order["subTagId"]) 
187
            inc()
188
            inc()
189
            inc()
14501 amit.gupta 190
            k = i
14504 amit.gupta 191
            anotherrow -= 1
14501 amit.gupta 192
            for subOrder in order['subOrders']:
14503 amit.gupta 193
                anotherrow += 1
14501 amit.gupta 194
                i = k
14505 amit.gupta 195
                worksheet1.write(anotherrow, inc(), subOrder['productTitle'])
196
                worksheet1.write(anotherrow, inc(), subOrder['amountPaid'])
197
                worksheet1.write(anotherrow, inc(), subOrder['quantity'])
198
                worksheet1.write(anotherrow, inc(), subOrder['status'])
199
                worksheet1.write(anotherrow, inc(), subOrder['cashBackStatus'])
200
                worksheet1.write(anotherrow, inc(), subOrder['cashBackAmount'])
14570 amit.gupta 201
 
14524 amit.gupta 202
 
203
    last30Days =  date.today() - timedelta(days=30)
204
    int(time.mktime(last30Days.timetuple()))
205
    row = 0
206
    for offer in db.snapdealOrderAffiliateInfo.find({"reconciled":{"$exists":False}, 
207
                                                     "saleTime":{"$gt":int(time.mktime(last30Days.timetuple())),
208
                                                                 "$lt":int(time.mktime(date.today().timetuple()))}} ):
209
        subTagId = offer.get("subTagId")
210
        saleTime = offer.get("saleTime")
211
        orders = getOrdersByTag(subTagId, 3)
212
        for order in orders:
213
            if notReconciled.has_key(order[0]):
214
                (roww, column, paidAmount, timestamp1, oSubTagId) = notReconciled[order[0]]
215
                if paidAmount==int(offer.get("saleAmount")) and timestamp1 <= saleTime and timestamp1 + 120 >= saleTime:
216
                    i = column
14570 amit.gupta 217
                    worksheet1.write(roww, inc(), offer.get("saleDate"))        
14527 amit.gupta 218
                    worksheet1.write(roww, inc(), offer.get("saleAmount"))        
219
                    worksheet1.write(roww, inc(), offer.get("payOut"))
220
                    del notReconciled[order[0]]
14530 amit.gupta 221
                    print "found match for user", order[1]
14610 amit.gupta 222
                    db.snapdealOrderAffiliateInfo.update({"adId":offer.get("adId")}, {"$set":{"reconciled":True, "orderId":order[0]}}, multi=True)
223
                    db.merchantOrder.update({"orderId":order[0]}, {"$set":{"reconciled":True, "adId":offer.get("adId")}}, multi=True)
14570 amit.gupta 224
                    break
14530 amit.gupta 225
 
14570 amit.gupta 226
    unreconciledOrders = notReconciled.keys()
227
    row2=0
228
    for row1 in curs:
229
        if row1[0] in unreconciledOrders:
230
            for order in db.merchantOrder.find({"orderId":row1[0]}):
231
                row2 += 1
232
                i=-1
233
                worksheet2.write(row2, inc(), order["orderId"])
234
                worksheet2.write(row2, inc(), row1[1])
14605 amit.gupta 235
                worksheet2.write(row2, inc(), row1[-2])
14570 amit.gupta 236
                worksheet2.write(row2, inc(), row1[-1])
237
                worksheet2.write(row2, inc(), order['merchantOrderId'])
238
                worksheet2.write(row2, inc(), order['subTagId'])
239
                worksheet2.write(row2, inc(), order['placedOn'])
240
                worksheet2.write(row2, inc(), int(order['paidAmount']))
241
                row2 -=1
242
                k=i
243
                for subOrder in order['subOrders']:
244
                    i = k
245
                    row2 += 1
246
                    worksheet2.write(row2, inc(), subOrder['productTitle'])
247
                    worksheet2.write(row2, inc(), subOrder['amountPaid'])
248
                    worksheet2.write(row2, inc(), subOrder['quantity'])
249
                    worksheet2.write(row2, inc(), subOrder['status'])
250
                    worksheet2.write(row2, inc(), subOrder['cashBackStatus'])
251
                    worksheet2.write(row2, inc(), subOrder['cashBackAmount'])
252
 
253
 
254
    for offer in db.snapdealOrderAffiliateInfo.find({"reconciled":{"$exists":False}, 
255
                                                     "saleTime":{"$gt":int(time.mktime(last30Days.timetuple())),
256
                                                                 "$lt":int(time.mktime(date.today().timetuple()))}} ):
257
        row += 1
258
        i=-1
259
        affNotReconciledSheet.write(row, inc(), offer.get("adId")) 
260
        affNotReconciledSheet.write(row, inc(), offer.get("subTagId")) 
261
        affNotReconciledSheet.write(row, inc(), offer.get("saleDate")) 
262
        affNotReconciledSheet.write(row, inc(), offer.get("saleAmount")) 
263
        affNotReconciledSheet.write(row, inc(), offer.get("payOut")) 
264
        affNotReconciledSheet.write(row, inc(), offer.get("conversionStatus"))
14601 amit.gupta 265
        affNotReconciledSheet.write(row, inc(), offer.get("ip"))
14570 amit.gupta 266
 
14460 amit.gupta 267
        workbook.save(XLS_FILENAME)
14450 amit.gupta 268
 
269
def sendmail(email, message, title):
270
    if email == "":
271
        return
272
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
273
    mailServer.ehlo()
274
    mailServer.starttls()
275
    mailServer.ehlo()
276
 
277
    # Create the container (outer) email message.
278
    msg = MIMEMultipart()
279
    msg['Subject'] = title
280
    msg.preamble = title
281
    html_msg = MIMEText(message, 'html')
282
    msg.attach(html_msg)
283
 
284
    #snapdeal more to be added here
285
    snapdeal = MIMEBase('application', 'vnd.ms-excel')
14460 amit.gupta 286
    snapdeal.set_payload(file(XLS_FILENAME).read())
14450 amit.gupta 287
    encoders.encode_base64(snapdeal)
14460 amit.gupta 288
    snapdeal.add_header('Content-Disposition', 'attachment;filename=' + XLS_FILENAME)
14450 amit.gupta 289
    msg.attach(snapdeal)
290
 
291
 
292
    email.append('amit.gupta@shop2020.in')
293
    MAILTO = email 
294
    mailServer.login(SENDER, PASSWORD)
295
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
296
 
14508 amit.gupta 297
def setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet):
14505 amit.gupta 298
    boldStyle = xlwt.XFStyle()
299
    f = xlwt.Font()
300
    f.bold = True
301
    boldStyle.font = f
14501 amit.gupta 302
    row = 0
303
    global i
304
    i=-1    
305
    worksheet.write(row, inc(), 'Order Id', boldStyle)
306
    worksheet.write(row, inc(), 'User Id', boldStyle)
14601 amit.gupta 307
    worksheet.write(row, inc(), 'Version Code', boldStyle)
14501 amit.gupta 308
    worksheet.write(row, inc(), 'Username', boldStyle)
309
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
310
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
311
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
312
    worksheet.write(row, inc(), 'Sale Amount', boldStyle)
313
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
314
    worksheet.write(row, inc(), 'Aff Sale Amt', boldStyle)
315
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
14601 amit.gupta 316
    worksheet.write(row, inc(), 'IP', boldStyle)
14501 amit.gupta 317
    worksheet.write(row, inc(), 'Product Title', boldStyle)
318
    worksheet.write(row, inc(), 'Price', boldStyle)
319
    worksheet.write(row, inc(), 'Quantity', boldStyle)
320
    worksheet.write(row, inc(), 'Status', boldStyle)
321
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
322
    worksheet.write(row, inc(), 'CashBack', boldStyle)
323
 
14505 amit.gupta 324
    i = -1
14501 amit.gupta 325
    worksheet1.write(row, inc(), 'Order Id', boldStyle)
326
    worksheet1.write(row, inc(), 'User Id', boldStyle)
14601 amit.gupta 327
    worksheet1.write(row, inc(), 'Version Code', boldStyle)
14501 amit.gupta 328
    worksheet1.write(row, inc(), 'Username', boldStyle)
329
    worksheet1.write(row, inc(), 'Merchant Order Id', boldStyle)
330
    worksheet1.write(row, inc(), 'SubtagId', boldStyle)
331
    worksheet1.write(row, inc(), 'Sale Date', boldStyle)
332
    worksheet1.write(row, inc(), 'Sale Amount', boldStyle)
333
    worksheet1.write(row, inc(), 'Aff Sale Date', boldStyle)
334
    worksheet1.write(row, inc(), 'Aff Sale Amt', boldStyle)
335
    worksheet1.write(row, inc(), 'Aff PayOut', boldStyle)
14601 amit.gupta 336
    worksheet1.write(row, inc(), 'IP', boldStyle)
14501 amit.gupta 337
    worksheet1.write(row, inc(), 'Product Title', boldStyle)
338
    worksheet1.write(row, inc(), 'Price', boldStyle)
339
    worksheet1.write(row, inc(), 'Quantity', boldStyle)
340
    worksheet1.write(row, inc(), 'Status', boldStyle)
341
    worksheet1.write(row, inc(), 'Cashback Status', boldStyle)
342
    worksheet1.write(row, inc(), 'CashBack', boldStyle) 
14508 amit.gupta 343
 
344
    i = -1
345
    worksheet2.write(row, inc(), 'Order Id', boldStyle)
346
    worksheet2.write(row, inc(), 'User Id', boldStyle)
14605 amit.gupta 347
    worksheet2.write(row, inc(), 'Version Code', boldStyle)
14508 amit.gupta 348
    worksheet2.write(row, inc(), 'Username', boldStyle)
349
    worksheet2.write(row, inc(), 'Merchant Order Id', boldStyle)
350
    worksheet2.write(row, inc(), 'SubtagId', boldStyle)
351
    worksheet2.write(row, inc(), 'Sale Date', boldStyle)
352
    worksheet2.write(row, inc(), 'Sale Amount', boldStyle)
353
    worksheet2.write(row, inc(), 'Product Title', boldStyle)
354
    worksheet2.write(row, inc(), 'Price', boldStyle)
355
    worksheet2.write(row, inc(), 'Quantity', boldStyle)
356
    worksheet2.write(row, inc(), 'Status', boldStyle)
357
    worksheet2.write(row, inc(), 'Cashback Status', boldStyle)
358
    worksheet2.write(row, inc(), 'CashBack', boldStyle)
14450 amit.gupta 359
 
14508 amit.gupta 360
    i =-1
361
    affNotReconciledSheet.write(row, inc(), 'AdId', boldStyle) 
362
    affNotReconciledSheet.write(row, inc(), 'subTagId', boldStyle) 
363
    affNotReconciledSheet.write(row, inc(), 'Sale Date', boldStyle) 
364
    affNotReconciledSheet.write(row, inc(), 'Sale Amount', boldStyle) 
365
    affNotReconciledSheet.write(row, inc(), 'Pay Out', boldStyle) 
366
    affNotReconciledSheet.write(row, inc(), 'Status', boldStyle) 
14601 amit.gupta 367
    affNotReconciledSheet.write(row, inc(), 'IP', boldStyle)
14524 amit.gupta 368
 
369
def getUserAmountReconciled():
370
    pass
371
 
14613 amit.gupta 372
def getUserOrders():
373
    global i
374
    rb = open_workbook(XLS_FILENAME,formatting_info=True)
375
    wb = copy(rb)
376
    worksheet = wb.add_sheet("All Users")
377
    worksheet1 = wb.add_sheet("Suspected Users")
14524 amit.gupta 378
 
14613 amit.gupta 379
    db=client.Dtr
380
    results = Mysql.fetchResult('''
381
        select ow.*, u.username, crm1.order_count, u.referrer from order_view ow left join users u on u.id = ow.user_id left join (select user_id, count(*) as order_count from order_view where status = 'ORDER_CREATED'  group by user_id) as crm1 on ow.user_id = crm1.user_id where lower(u.referrer) not like 'emp%' 
382
        ''',)
383
    orders = []
384
    orders.append("")
385
    orderH = []
386
    row = 0
387
    i=-1
388
    worksheet.write(row, inc(), 'User Id', boldStyle)
389
    worksheet1.write(row, i, 'User Id', boldStyle)
390
    worksheet.write(row, inc(), 'User name', boldStyle)
391
    worksheet1.write(row, i, 'User name', boldStyle)
392
    worksheet.write(row, inc(), 'Order Id', boldStyle)
393
    worksheet1.write(row, i, 'Order Id', boldStyle)
394
    worksheet.write(row, inc(), 'Created On', boldStyle)
395
    worksheet1.write(row, i, 'Created On', boldStyle)
396
    worksheet.write(row, inc(), 'Store Id', boldStyle)
397
    worksheet1.write(row, i, 'Store Id', boldStyle)
398
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
399
    worksheet1.write(row, i, 'Merchant Order Id', boldStyle)
400
    worksheet.write(row, inc(), 'Amount Paid', boldStyle)
401
    worksheet1.write(row, i, 'Amount Paid', boldStyle)
402
    worksheet.write(row, inc(), 'Cashback', boldStyle)
403
    worksheet1.write(row, i, 'Cashback', boldStyle)
404
    worksheet.write(row, inc(), 'Order Status', boldStyle)
405
    worksheet1.write(row, i, 'Order Status', boldStyle)
406
    worksheet.write(row, inc(), 'Detailed Status', boldStyle)
407
    worksheet1.write(row, i, 'Detailed Status', boldStyle)
408
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
409
    worksheet1.write(row, i, 'Cashback Status', boldStyle)
410
    worksheet.write(row, inc(), 'Reconciled', boldStyle)
411
    worksheet1.write(row, i, 'Reconciled', boldStyle)
412
    worksheet.write(row, inc(), 'IP', boldStyle)
413
    worksheet1.write(row, i, 'IP', boldStyle)
414
 
415
    row=0
416
    row2=0
417
    for result in results:
418
        morder = db.merchantOrder.find_one({"orderId":result[0]})
419
        if morder is not None:
420
            subOrders = morder.get("subOrders")
421
            if subOrders is not None:
422
                for  subOrder in subOrders:
423
                    if subOrder.get("cashBackStatus") == Store.CB_APPROVED or subOrder.get("cashBackStatus") == Store.CB_PENDING:
424
                        row +=1
425
                        i=-1
426
                        worksheet.write(row, inc(), result[0])
427
                        worksheet.write(row, inc(), result[-3])
428
                        worksheet.write(row, inc(), result[1])
429
                        worksheet.write(row, inc(), result[-4])
430
                        worksheet.write(row, inc(), result[2])
431
                        worksheet.write(row, inc(), morder.get("merchantOrderId"))
432
                        #order.append(subOrder.get("merchantSubOrderId"))
433
                        worksheet.write(row, inc(), subOrder.get("amountPaid"))
434
                        worksheet.write(row, inc(), subOrder.get("cashBackAmount"))
435
                        worksheet.write(row, inc(), subOrder.get("status"))
436
                        worksheet.write(row, inc(), subOrder.get("detailedStatus"))
437
                        worksheet.write(row, inc(), subOrder.get("cashBackStatus"))
438
                        worksheet.write(row, inc(), False if morder.get("reconciled") is None else True)
439
                        if morder.get("reconciled") and morder.get("storeId")==3:
440
                            try:
441
                                worksheet.write(row, inc(), db.snapdealOrderAffiliateInfo.find_one({"adId":morder.get("adId"), "reconciled":True}).get("ip"))
442
                            except:
443
                                print "Could not find", morder.get("adId"), "that is reconcired for order", morder.get("orderId")
444
 
445
                        if result[-2] >= 10 and result[-1].lower().startswith("crm"):
446
                            row2 +=1 
447
                            i=-1
448
                            worksheet1.write(row2, inc(), result[0])
449
                            worksheet1.write(row2, inc(), result[-3])
450
                            worksheet1.write(row2, inc(), result[1])
451
                            worksheet1.write(row2, inc(), result[-4])
452
                            worksheet1.write(row2, inc(), result[2])
453
                            worksheet1.write(row2, inc(), morder.get("merchantOrderId"))
454
                            worksheet1.write(row2, inc(), subOrder.get("amountPaid"))
455
                            worksheet1.write(row2, inc(), subOrder.get("cashBackAmount"))
456
                            worksheet1.write(row2, inc(), subOrder.get("status"))
457
                            worksheet1.write(row2, inc(), subOrder.get("detailedStatus"))
458
                            worksheet1.write(row2, inc(), subOrder.get("cashBackStatus"))
459
                            worksheet1.write(row2, inc(), False if morder.get("reconciled") is None else True)
460
                            if morder.get("reconciled") and morder.get("storeId")==3:
461
                                try:
462
                                    worksheet1.write(row2, inc(), db.snapdealOrderAffiliateInfo.find_one({"adId":morder.get("adId"), "reconciled":True}).get("ip"))
463
                                except:
464
                                    print "Could not find", morder.get("adId"), "that is reconcired for order", morder.get("orderId")
465
    wb.save(XLS_FILENAME)
466
 
14460 amit.gupta 467
def inc():
468
    global i
469
    i+=1
470
    return i
471
 
14450 amit.gupta 472
def main():
473
    generateSnapDealReco()
14613 amit.gupta 474
    getUserOrders()
14604 amit.gupta 475
    sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco")
14450 amit.gupta 476
 
477
if __name__ == '__main__':
14460 amit.gupta 478
    main()
479