Subversion Repositories SmartDukaan

Rev

Rev 14528 | Rev 14530 | 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"])
14529 amit.gupta 124
            db.snapdealOrderAffiliateInfo.update({"adId":aff[0].get("adId")}, {"$set":{"reconciled":True}}, multi=True)
14501 amit.gupta 125
            order['reconciled'] = True
126
            db.merchantOrder.save(order)
127
            row += 1
128
            i=-1 
129
            worksheet.write(row, inc(), orderId)
130
            worksheet1.write(anotherrow, i, orderId)
131
            worksheet.write(row, inc(), row1[1])
132
            worksheet1.write(anotherrow, i, row1[1])
133
            worksheet.write(row, inc(), row1[-1])
134
            worksheet1.write(anotherrow, i, row1[-1])
135
            worksheet.write(row, inc(), order['merchantOrderId'])
136
            worksheet1.write(anotherrow, i, order['merchantOrderId'])
137
            worksheet.write(row, inc(), order['subTagId'])
138
            worksheet1.write(anotherrow, i, order['subTagId'])
139
            worksheet.write(row, inc(), order['placedOn'])
140
            worksheet1.write(anotherrow, i, order['placedOn'])
141
            worksheet.write(row, inc(), int(order['paidAmount']))
142
            worksheet1.write(anotherrow, i, int(order['paidAmount']))
14524 amit.gupta 143
            worksheet.write(row, inc(), aff[0]['saleDate'])
144
            worksheet1.write(anotherrow, i, aff[0]['saleDate'])
145
            worksheet.write(row, inc(), aff[0]['saleAmount'])
146
            worksheet1.write(anotherrow, i, aff[0]['saleAmount'])
147
            worksheet.write(row, inc(), aff[0]['payOut'])
148
            worksheet1.write(anotherrow, i, aff[0]['payOut'])
14501 amit.gupta 149
            k = i
14504 amit.gupta 150
            row -= 1
151
            anotherrow -= 1
14501 amit.gupta 152
            for subOrder in order['subOrders']:
153
                i = k
14503 amit.gupta 154
                row += 1
155
                anotherrow += 1
14501 amit.gupta 156
                worksheet.write(row, inc(), subOrder['productTitle'])
157
                worksheet1.write(anotherrow, i, subOrder['productTitle'])
158
                worksheet.write(row, inc(), subOrder['amountPaid'])
159
                worksheet1.write(anotherrow, i, subOrder['amountPaid'])
160
                worksheet.write(row, inc(), subOrder['quantity'])
161
                worksheet1.write(anotherrow, i, subOrder['quantity'])
162
                worksheet.write(row, inc(), subOrder['status'])
163
                worksheet1.write(anotherrow, i, subOrder['status'])
164
                worksheet.write(row, inc(), subOrder['cashBackStatus'])
165
                worksheet1.write(anotherrow, i, subOrder['cashBackStatus'])
166
                worksheet.write(row, inc(), subOrder['cashBackAmount'])
167
                worksheet1.write(anotherrow, i, subOrder['cashBackAmount'])
14504 amit.gupta 168
 
14501 amit.gupta 169
        else:
14508 amit.gupta 170
            row2 +=1
14524 amit.gupta 171
            i=-1
14501 amit.gupta 172
            worksheet1.write(anotherrow, inc(), orderId)
14508 amit.gupta 173
            worksheet2.write(row2, i, orderId)
14501 amit.gupta 174
            worksheet1.write(anotherrow, inc(), row1[1])
14508 amit.gupta 175
            worksheet2.write(row2, i, row1[1])
14501 amit.gupta 176
            worksheet1.write(anotherrow, inc(), row1[-1])
14508 amit.gupta 177
            worksheet2.write(row2, i, row1[-1])
14501 amit.gupta 178
            worksheet1.write(anotherrow, inc(), order['merchantOrderId'])
14508 amit.gupta 179
            worksheet2.write(row2, i, order['merchantOrderId'])
14501 amit.gupta 180
            worksheet1.write(anotherrow, inc(), order['subTagId'])
14508 amit.gupta 181
            worksheet2.write(row2, i, order['subTagId'])
14501 amit.gupta 182
            worksheet1.write(anotherrow, inc(), order['placedOn'])
14508 amit.gupta 183
            worksheet2.write(row2, i, order['placedOn'])
14501 amit.gupta 184
            worksheet1.write(anotherrow, inc(), int(order['paidAmount']))
14508 amit.gupta 185
            worksheet2.write(row2, i, 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
14508 amit.gupta 193
            row2 -= 1
14501 amit.gupta 194
            for subOrder in order['subOrders']:
14503 amit.gupta 195
                anotherrow += 1
14508 amit.gupta 196
                row2 += 1
14501 amit.gupta 197
                i = k
14505 amit.gupta 198
                worksheet1.write(anotherrow, inc(), subOrder['productTitle'])
14508 amit.gupta 199
                worksheet2.write(row2, i-3, subOrder['productTitle'])
14505 amit.gupta 200
                worksheet1.write(anotherrow, inc(), subOrder['amountPaid'])
14508 amit.gupta 201
                worksheet2.write(row2, i-3, subOrder['amountPaid'])
14505 amit.gupta 202
                worksheet1.write(anotherrow, inc(), subOrder['quantity'])
14508 amit.gupta 203
                worksheet2.write(row2, i-3, subOrder['quantity'])
14505 amit.gupta 204
                worksheet1.write(anotherrow, inc(), subOrder['status'])
14508 amit.gupta 205
                worksheet2.write(row2, i-3, subOrder['status'])
14505 amit.gupta 206
                worksheet1.write(anotherrow, inc(), subOrder['cashBackStatus'])
14508 amit.gupta 207
                worksheet2.write(row2, i-3, subOrder['cashBackStatus'])
14505 amit.gupta 208
                worksheet1.write(anotherrow, inc(), subOrder['cashBackAmount'])
14508 amit.gupta 209
                worksheet2.write(row2, i-3, subOrder['cashBackAmount'])
14524 amit.gupta 210
 
211
    last30Days =  date.today() - timedelta(days=30)
212
    int(time.mktime(last30Days.timetuple()))
213
    row = 0
214
    for offer in db.snapdealOrderAffiliateInfo.find({"reconciled":{"$exists":False}, 
215
                                                     "saleTime":{"$gt":int(time.mktime(last30Days.timetuple())),
216
                                                                 "$lt":int(time.mktime(date.today().timetuple()))}} ):
217
        row += 1
218
        i=-1
219
        affNotReconciledSheet.write(row, inc(), offer.get("adId")) 
220
        affNotReconciledSheet.write(row, inc(), offer.get("subTagId")) 
221
        affNotReconciledSheet.write(row, inc(), offer.get("saleDate")) 
222
        affNotReconciledSheet.write(row, inc(), offer.get("saleAmount")) 
223
        affNotReconciledSheet.write(row, inc(), offer.get("payOut")) 
224
        affNotReconciledSheet.write(row, inc(), offer.get("conversionStatus"))
225
 
226
        subTagId = offer.get("subTagId")
227
        saleTime = offer.get("saleTime")
228
        orders = getOrdersByTag(subTagId, 3)
229
        for order in orders:
230
            if notReconciled.has_key(order[0]):
231
                (roww, column, paidAmount, timestamp1, oSubTagId) = notReconciled[order[0]]
232
                if paidAmount==int(offer.get("saleAmount")) and timestamp1 <= saleTime and timestamp1 + 120 >= saleTime:
233
                    i = column
14527 amit.gupta 234
                    worksheet1.write(roww, inc(), subTagId)        
235
                    worksheet1.write(roww, inc(), offer.get("saleAmount"))        
236
                    worksheet1.write(roww, inc(), offer.get("payOut"))
237
                    del notReconciled[order[0]]
238
                    print "found match for user", order[1] 
14524 amit.gupta 239
 
240
 
14460 amit.gupta 241
        workbook.save(XLS_FILENAME)
14450 amit.gupta 242
 
243
def sendmail(email, message, title):
244
    if email == "":
245
        return
246
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
247
    mailServer.ehlo()
248
    mailServer.starttls()
249
    mailServer.ehlo()
250
 
251
    # Create the container (outer) email message.
252
    msg = MIMEMultipart()
253
    msg['Subject'] = title
254
    msg.preamble = title
255
    html_msg = MIMEText(message, 'html')
256
    msg.attach(html_msg)
257
 
258
    #snapdeal more to be added here
259
    snapdeal = MIMEBase('application', 'vnd.ms-excel')
14460 amit.gupta 260
    snapdeal.set_payload(file(XLS_FILENAME).read())
14450 amit.gupta 261
    encoders.encode_base64(snapdeal)
14460 amit.gupta 262
    snapdeal.add_header('Content-Disposition', 'attachment;filename=' + XLS_FILENAME)
14450 amit.gupta 263
    msg.attach(snapdeal)
264
 
265
 
266
    email.append('amit.gupta@shop2020.in')
267
    MAILTO = email 
268
    mailServer.login(SENDER, PASSWORD)
269
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
270
 
14508 amit.gupta 271
def setHeaders(worksheet, worksheet1, worksheet2, affNotReconciledSheet):
14505 amit.gupta 272
    boldStyle = xlwt.XFStyle()
273
    f = xlwt.Font()
274
    f.bold = True
275
    boldStyle.font = f
14501 amit.gupta 276
    row = 0
277
    global i
278
    i=-1    
279
    worksheet.write(row, inc(), 'Order Id', boldStyle)
280
    worksheet.write(row, inc(), 'User Id', boldStyle)
281
    worksheet.write(row, inc(), 'Username', boldStyle)
282
    worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
283
    worksheet.write(row, inc(), 'SubtagId', boldStyle)
284
    worksheet.write(row, inc(), 'Sale Date', boldStyle)
285
    worksheet.write(row, inc(), 'Sale Amount', boldStyle)
286
    worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
287
    worksheet.write(row, inc(), 'Aff Sale Amt', boldStyle)
288
    worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
289
    worksheet.write(row, inc(), 'Product Title', boldStyle)
290
    worksheet.write(row, inc(), 'Price', boldStyle)
291
    worksheet.write(row, inc(), 'Quantity', boldStyle)
292
    worksheet.write(row, inc(), 'Status', boldStyle)
293
    worksheet.write(row, inc(), 'Cashback Status', boldStyle)
294
    worksheet.write(row, inc(), 'CashBack', boldStyle)
295
 
14505 amit.gupta 296
    i = -1
14501 amit.gupta 297
    worksheet1.write(row, inc(), 'Order Id', boldStyle)
298
    worksheet1.write(row, inc(), 'User Id', boldStyle)
299
    worksheet1.write(row, inc(), 'Username', boldStyle)
300
    worksheet1.write(row, inc(), 'Merchant Order Id', boldStyle)
301
    worksheet1.write(row, inc(), 'SubtagId', boldStyle)
302
    worksheet1.write(row, inc(), 'Sale Date', boldStyle)
303
    worksheet1.write(row, inc(), 'Sale Amount', boldStyle)
304
    worksheet1.write(row, inc(), 'Aff Sale Date', boldStyle)
305
    worksheet1.write(row, inc(), 'Aff Sale Amt', boldStyle)
306
    worksheet1.write(row, inc(), 'Aff PayOut', boldStyle)
307
    worksheet1.write(row, inc(), 'Product Title', boldStyle)
308
    worksheet1.write(row, inc(), 'Price', boldStyle)
309
    worksheet1.write(row, inc(), 'Quantity', boldStyle)
310
    worksheet1.write(row, inc(), 'Status', boldStyle)
311
    worksheet1.write(row, inc(), 'Cashback Status', boldStyle)
312
    worksheet1.write(row, inc(), 'CashBack', boldStyle) 
14508 amit.gupta 313
 
314
    i = -1
315
    worksheet2.write(row, inc(), 'Order Id', boldStyle)
316
    worksheet2.write(row, inc(), 'User Id', boldStyle)
317
    worksheet2.write(row, inc(), 'Username', boldStyle)
318
    worksheet2.write(row, inc(), 'Merchant Order Id', boldStyle)
319
    worksheet2.write(row, inc(), 'SubtagId', boldStyle)
320
    worksheet2.write(row, inc(), 'Sale Date', boldStyle)
321
    worksheet2.write(row, inc(), 'Sale Amount', boldStyle)
322
    worksheet2.write(row, inc(), 'Product Title', boldStyle)
323
    worksheet2.write(row, inc(), 'Price', boldStyle)
324
    worksheet2.write(row, inc(), 'Quantity', boldStyle)
325
    worksheet2.write(row, inc(), 'Status', boldStyle)
326
    worksheet2.write(row, inc(), 'Cashback Status', boldStyle)
327
    worksheet2.write(row, inc(), 'CashBack', boldStyle)
14450 amit.gupta 328
 
14508 amit.gupta 329
    i =-1
330
    affNotReconciledSheet.write(row, inc(), 'AdId', boldStyle) 
331
    affNotReconciledSheet.write(row, inc(), 'subTagId', boldStyle) 
332
    affNotReconciledSheet.write(row, inc(), 'Sale Date', boldStyle) 
333
    affNotReconciledSheet.write(row, inc(), 'Sale Amount', boldStyle) 
334
    affNotReconciledSheet.write(row, inc(), 'Pay Out', boldStyle) 
335
    affNotReconciledSheet.write(row, inc(), 'Status', boldStyle) 
14524 amit.gupta 336
 
337
def getUserAmountReconciled():
338
    pass
339
 
340
 
14460 amit.gupta 341
def inc():
342
    global i
343
    i+=1
344
    return i
345
 
14450 amit.gupta 346
def main():
347
    generateSnapDealReco()
14524 amit.gupta 348
    #generateFlipkartReco()
14528 amit.gupta 349
    sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco")
14450 amit.gupta 350
 
351
if __name__ == '__main__':
14460 amit.gupta 352
    main()
353