Subversion Repositories SmartDukaan

Rev

Rev 14525 | Rev 14528 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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