Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14650 amit.gupta 1
'''
2
Created on Mar 27, 2015
3
 
4
@author: amit
5
'''
6
from datetime import datetime, timedelta
7
from dtr.storage.Mysql import getOrdersAfterDate
8
from pymongo.mongo_client import MongoClient
9
import time
10
import xlwt
11
 
12
client = MongoClient('mongodb://localhost:27017/')
13
def generateAmazonReco(fileName):
14
    global i
15
    db = client.Dtr
16
    notReconciled = {}
17
    matchedList = []
18
    workbook = xlwt.Workbook()
19
    worksheet = workbook.add_sheet("Orders Reconciled")
20
    worksheet1 = workbook.add_sheet("Orders Reconciled All")
21
    worksheet2 = workbook.add_sheet("Orders Not Reconciled")
22
    worksheet3 = workbook.add_sheet("Affiliate Not Reconciled")
23
    addHeaders(worksheet, worksheet1, worksheet2, worksheet3)
24
 
25
    curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 1)
26
    placedOnSince = int(time.mktime((datetime.now() - timedelta(days=30)).timetuple()))
27
    row = 0
28
    row1 = 0
29
    row2 = 0
30
    row3= 0
31
 
32
    for ro in curs:
33
        orderId = ro[0]
34
        order = db.merchantOrder.find_one({"orderId":orderId})
35
        if order is None:
36
            continue
37
 
38
        subOrders = order.get("subOrders")
39
        if subOrders is None:
40
            continue
41
        placedOnTime = int(time.mktime(datetime.strptime(order.get("placedOn"), "%d %B %Y").timetuple()))
42
        aff = db.amazonAffiliateInfo.find_one({"subTagId":order["subTagId"], "time": placedOnTime})
43
        for subOrder in subOrders:
44
            row1 += 1
45
            i =-1
46
            worksheet1.write(row1, inc(), orderId)
47
            worksheet1.write(row1, inc(), ro[1])
48
            worksheet1.write(row1, inc(), ro[-2])
49
            worksheet1.write(row1, inc(), ro[-1])
50
            worksheet1.write(row1, inc(), order['merchantOrderId'])
51
            worksheet1.write(row1, inc(), subOrder['productTitle'])
52
            worksheet1.write(row1, inc(), subOrder['amountPaid'])
53
            worksheet1.write(row1, inc(), subOrder['quantity'])
54
            worksheet1.write(row1, inc(), subOrder['status'])
55
            worksheet1.write(row1, inc(), subOrder['detailedStatus'])
56
            worksheet1.write(row1, inc(), subOrder['cashBackStatus'])
57
            worksheet1.write(row1, inc(), subOrder['cashBackAmount'])
58
            worksheet1.write(row1, inc(), subOrder['placedOn'])
59
            worksheet1.write(row1, inc(), order['subTagId'])
60
            if aff is not None:
61
                row += 1
62
                i =-1
63
                worksheet.write(row, inc(), orderId)
64
                worksheet.write(row, inc(), ro[1])
65
                worksheet.write(row, inc(), ro[-2])
66
                worksheet.write(row, inc(), ro[-1])
67
                worksheet.write(row, inc(), order['merchantOrderId'])
68
                worksheet.write(row, inc(), subOrder['productTitle'])
69
                worksheet.write(row, inc(), subOrder['amountPaid'])
70
                worksheet.write(row, inc(), subOrder['quantity'])
71
                worksheet.write(row, inc(), subOrder['status'])
72
                worksheet.write(row, inc(), subOrder['detailedStatus'])
73
                worksheet.write(row, inc(), subOrder['cashBackStatus'])
74
                worksheet.write(row, inc(), subOrder['cashBackAmount'])
75
                worksheet.write(row, inc(), subOrder['placedOn'])
76
                worksheet.write(row, inc(), order['subTagId'])
77
                worksheet.write(row, inc(),"Yes")
78
                worksheet1.write(row1, i,"Yes")
79
                db.amazonAffiliateInfo.update({"subTagId":order["subTagId"], "time": placedOnTime}, 
80
                                              {"$set":{"reconciled":True}})
81
                db.merchantOrder.update({"merchantOrderId":order.get("merchantOrderId")},{"$set":{"reconciled":True}})
82
            else:
83
                row2 +=1
84
                i =-1
85
                worksheet2.write(row2, inc(), orderId)
86
                worksheet2.write(row2, inc(), ro[1])
87
                worksheet2.write(row2, inc(), ro[-2])
88
                worksheet2.write(row2, inc(), ro[-1])
89
                worksheet2.write(row2, inc(), order['merchantOrderId'])
90
                worksheet2.write(row2, inc(), subOrder['productTitle'])
91
                worksheet2.write(row2, inc(), subOrder['amountPaid'])
92
                worksheet2.write(row2, inc(), subOrder['quantity'])
93
                worksheet2.write(row2, inc(), subOrder['status'])
94
                worksheet2.write(row2, inc(), subOrder['detailedStatus'])
95
                worksheet2.write(row2, inc(), subOrder['cashBackStatus'])
96
                worksheet2.write(row2, inc(), subOrder['cashBackAmount'])
97
                worksheet2.write(row2, inc(), subOrder['placedOn'])
98
                worksheet2.write(row2, inc(), order['subTagId'])
99
                worksheet1.write(row1, inc(), "No")
100
 
101
    for aff in db.amazonAffiliateInfo.find({"reconciled":{"$exists":False}, "time":{"$gt":placedOnSince}}):
102
        row3 += 1
103
        i=-1
104
        worksheet3.write(row3, inc(), aff.get("linkType"))
105
        worksheet3.write(row3, inc(), aff.get("totalOrderedQuantity"))
106
        worksheet3.write(row3, inc(), aff.get("dateOrdered"))
107
        worksheet3.write(row3, inc(), aff.get("directOrderedQuantity"))
108
        worksheet3.write(row3, inc(), aff.get("indirectOrderedQuantity"))
109
        worksheet3.write(row3, inc(), aff.get("directClicks"))
110
        worksheet3.write(row3, inc(), aff.get("trackingId"))
111
        worksheet3.write(row3, inc(), aff.get("subTagId"))
112
        worksheet3.write(row3, inc(), aff.get("productLine"))
113
 
114
 
115
    workbook.save(fileName)
116
 
117
 
118
 
119
def addHeaders(worksheet, worksheet1, worksheet2, worksheet3):
120
    global i
121
    i=-1
122
    boldStyle = xlwt.XFStyle()
123
    f = xlwt.Font()
124
    f.bold = True
125
    boldStyle.font = f
126
    worksheet1.write(0, inc(), 'Order Id', boldStyle)
127
    worksheet.write(0, i, 'Order Id', boldStyle)
128
    worksheet2.write(0, i, 'Order Id', boldStyle)
129
    worksheet.write(0, inc(), 'User Id', boldStyle)
130
    worksheet1.write(0, i, 'User Id', boldStyle)
131
    worksheet2.write(0, i, 'User Id', boldStyle)
132
    worksheet.write(0, inc(), 'Version Code', boldStyle)
133
    worksheet1.write(0, i, 'Version Code', boldStyle)
134
    worksheet2.write(0, i, 'Version Code', boldStyle)
135
    worksheet.write(0, inc(), 'Username', boldStyle)
136
    worksheet1.write(0, i, 'Username', boldStyle)
137
    worksheet2.write(0, i, 'Username', boldStyle)
138
    worksheet.write(0, inc(), 'Merchant Order Id', boldStyle)
139
    worksheet1.write(0, i, 'Merchant Order Id', boldStyle)
140
    worksheet2.write(0, i, 'Merchant Order Id', boldStyle)
141
    worksheet.write(0, inc(), 'Product Title', boldStyle)
142
    worksheet1.write(0, i, 'Product Title', boldStyle)
143
    worksheet2.write(0, i, 'Product Title', boldStyle)
144
    worksheet.write(0, inc(), 'Price', boldStyle)
145
    worksheet1.write(0, i, 'Price', boldStyle)
146
    worksheet2.write(0, i, 'Price', boldStyle)
147
    worksheet.write(0, inc(), 'Quantity', boldStyle)
148
    worksheet1.write(0, i, 'Quantity', boldStyle)
149
    worksheet2.write(0, i, 'Quantity', boldStyle)
150
    worksheet.write(0, inc(), 'Status', boldStyle)
151
    worksheet1.write(0, i, 'Status', boldStyle)
152
    worksheet2.write(0, i, 'Status', boldStyle)
153
    worksheet.write(0, inc(), 'Detailed Status', boldStyle)
154
    worksheet1.write(0, i, 'Detailed Status', boldStyle)
155
    worksheet2.write(0, i, 'Detailed Status', boldStyle)
156
    worksheet.write(0, inc(), 'Cashback Status', boldStyle)
157
    worksheet1.write(0, i, 'Cashback Status', boldStyle)
158
    worksheet2.write(0, i, 'Cashback Status', boldStyle)
159
    worksheet.write(0, inc(), 'CashBack', boldStyle)
160
    worksheet1.write(0, i, 'CashBack', boldStyle)
161
    worksheet2.write(0, i, 'CashBack', boldStyle)
162
    worksheet.write(0, inc(), 'SubtagId', boldStyle)
163
    worksheet1.write(0, i, 'SubtagId', boldStyle)
164
    worksheet2.write(0, i, 'SubtagId', boldStyle)
165
    worksheet.write(0, inc(), 'Sale Date', boldStyle)
166
    worksheet1.write(0, i, 'Sale Date', boldStyle)
167
    worksheet2.write(0, i, 'Sale Date', boldStyle)
168
    worksheet.write(0, inc(), 'Reconciled', boldStyle)
169
    worksheet1.write(0, i, 'Reconciled', boldStyle)
170
    i=-1
171
    worksheet3.write(0, inc(), 'linkType',boldStyle)
172
    worksheet3.write(0, inc(), 'totalOrderedQuantity',boldStyle)
173
    worksheet3.write(0, inc(), 'dateOrdered',boldStyle)
174
    worksheet3.write(0, inc(), 'directOrderedQuantity',boldStyle)
175
    worksheet3.write(0, inc(), 'indirectOrderedQuantity',boldStyle)
176
    worksheet3.write(0, inc(), 'directClicks',boldStyle)
177
    worksheet3.write(0, inc(), 'trackingId',boldStyle)
178
    worksheet3.write(0, inc(), 'subTagId',boldStyle)
179
    worksheet3.write(0, inc(), 'productLine',boldStyle)
180
 
181
def main():
182
    generateAmazonReco("amazon.xls")
183
 
184
 
185
def inc():
186
    global i
187
    i+=1
188
    return i
189
 
190
 
191
if __name__ == '__main__':
192
    main()