Subversion Repositories SmartDukaan

Rev

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