Subversion Repositories SmartDukaan

Rev

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