Subversion Repositories SmartDukaan

Rev

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