| 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])
|
| 14771 |
amit.gupta |
48 |
worksheet1.write(row1, inc(), ro[-1])
|
|
|
49 |
worksheet1.write(row1, inc(), ro[-3])
|
| 14650 |
amit.gupta |
50 |
worksheet1.write(row1, inc(), ro[-2])
|
|
|
51 |
worksheet1.write(row1, inc(), order['merchantOrderId'])
|
|
|
52 |
worksheet1.write(row1, inc(), subOrder['productTitle'])
|
|
|
53 |
worksheet1.write(row1, inc(), subOrder['amountPaid'])
|
|
|
54 |
worksheet1.write(row1, inc(), subOrder['quantity'])
|
|
|
55 |
worksheet1.write(row1, inc(), subOrder['status'])
|
|
|
56 |
worksheet1.write(row1, inc(), subOrder['detailedStatus'])
|
|
|
57 |
worksheet1.write(row1, inc(), subOrder['cashBackStatus'])
|
|
|
58 |
worksheet1.write(row1, inc(), subOrder['cashBackAmount'])
|
|
|
59 |
worksheet1.write(row1, inc(), subOrder['placedOn'])
|
|
|
60 |
worksheet1.write(row1, inc(), order['subTagId'])
|
|
|
61 |
if aff is not None:
|
|
|
62 |
row += 1
|
|
|
63 |
i =-1
|
|
|
64 |
worksheet.write(row, inc(), orderId)
|
|
|
65 |
worksheet.write(row, inc(), ro[1])
|
| 14771 |
amit.gupta |
66 |
worksheet.write(row, inc(), ro[-1])
|
|
|
67 |
worksheet.write(row, inc(), ro[-3])
|
| 14650 |
amit.gupta |
68 |
worksheet.write(row, inc(), ro[-2])
|
|
|
69 |
worksheet.write(row, inc(), order['merchantOrderId'])
|
|
|
70 |
worksheet.write(row, inc(), subOrder['productTitle'])
|
|
|
71 |
worksheet.write(row, inc(), subOrder['amountPaid'])
|
|
|
72 |
worksheet.write(row, inc(), subOrder['quantity'])
|
|
|
73 |
worksheet.write(row, inc(), subOrder['status'])
|
|
|
74 |
worksheet.write(row, inc(), subOrder['detailedStatus'])
|
|
|
75 |
worksheet.write(row, inc(), subOrder['cashBackStatus'])
|
|
|
76 |
worksheet.write(row, inc(), subOrder['cashBackAmount'])
|
|
|
77 |
worksheet.write(row, inc(), subOrder['placedOn'])
|
|
|
78 |
worksheet.write(row, inc(), order['subTagId'])
|
|
|
79 |
worksheet.write(row, inc(),"Yes")
|
|
|
80 |
worksheet1.write(row1, i,"Yes")
|
|
|
81 |
db.amazonAffiliateInfo.update({"subTagId":order["subTagId"], "time": placedOnTime},
|
|
|
82 |
{"$set":{"reconciled":True}})
|
|
|
83 |
db.merchantOrder.update({"merchantOrderId":order.get("merchantOrderId")},{"$set":{"reconciled":True}})
|
|
|
84 |
else:
|
|
|
85 |
row2 +=1
|
|
|
86 |
i =-1
|
|
|
87 |
worksheet2.write(row2, inc(), orderId)
|
|
|
88 |
worksheet2.write(row2, inc(), ro[1])
|
| 14771 |
amit.gupta |
89 |
worksheet2.write(row2, inc(), ro[-1])
|
|
|
90 |
worksheet2.write(row2, inc(), ro[-3])
|
| 14650 |
amit.gupta |
91 |
worksheet2.write(row2, inc(), ro[-2])
|
|
|
92 |
worksheet2.write(row2, inc(), order['merchantOrderId'])
|
|
|
93 |
worksheet2.write(row2, inc(), subOrder['productTitle'])
|
|
|
94 |
worksheet2.write(row2, inc(), subOrder['amountPaid'])
|
|
|
95 |
worksheet2.write(row2, inc(), subOrder['quantity'])
|
|
|
96 |
worksheet2.write(row2, inc(), subOrder['status'])
|
|
|
97 |
worksheet2.write(row2, inc(), subOrder['detailedStatus'])
|
|
|
98 |
worksheet2.write(row2, inc(), subOrder['cashBackStatus'])
|
|
|
99 |
worksheet2.write(row2, inc(), subOrder['cashBackAmount'])
|
|
|
100 |
worksheet2.write(row2, inc(), subOrder['placedOn'])
|
|
|
101 |
worksheet2.write(row2, inc(), order['subTagId'])
|
|
|
102 |
worksheet1.write(row1, inc(), "No")
|
|
|
103 |
|
|
|
104 |
for aff in db.amazonAffiliateInfo.find({"reconciled":{"$exists":False}, "time":{"$gt":placedOnSince}}):
|
|
|
105 |
row3 += 1
|
|
|
106 |
i=-1
|
|
|
107 |
worksheet3.write(row3, inc(), aff.get("linkType"))
|
|
|
108 |
worksheet3.write(row3, inc(), aff.get("totalOrderedQuantity"))
|
|
|
109 |
worksheet3.write(row3, inc(), aff.get("dateOrdered"))
|
|
|
110 |
worksheet3.write(row3, inc(), aff.get("directOrderedQuantity"))
|
|
|
111 |
worksheet3.write(row3, inc(), aff.get("indirectOrderedQuantity"))
|
|
|
112 |
worksheet3.write(row3, inc(), aff.get("directClicks"))
|
|
|
113 |
worksheet3.write(row3, inc(), aff.get("trackingId"))
|
|
|
114 |
worksheet3.write(row3, inc(), aff.get("subTagId"))
|
|
|
115 |
worksheet3.write(row3, inc(), aff.get("productLine"))
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
workbook.save(fileName)
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
def addHeaders(worksheet, worksheet1, worksheet2, worksheet3):
|
|
|
123 |
global i
|
|
|
124 |
i=-1
|
|
|
125 |
boldStyle = xlwt.XFStyle()
|
|
|
126 |
f = xlwt.Font()
|
|
|
127 |
f.bold = True
|
|
|
128 |
boldStyle.font = f
|
|
|
129 |
worksheet1.write(0, inc(), 'Order Id', boldStyle)
|
|
|
130 |
worksheet.write(0, i, 'Order Id', boldStyle)
|
|
|
131 |
worksheet2.write(0, i, 'Order Id', boldStyle)
|
|
|
132 |
worksheet.write(0, inc(), 'User Id', boldStyle)
|
|
|
133 |
worksheet1.write(0, i, 'User Id', boldStyle)
|
|
|
134 |
worksheet2.write(0, i, 'User Id', boldStyle)
|
| 14771 |
amit.gupta |
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(), 'Device', boldStyle)
|
|
|
139 |
worksheet1.write(0, i, 'Device', boldStyle)
|
|
|
140 |
worksheet2.write(0, i, 'Device', boldStyle)
|
| 14650 |
amit.gupta |
141 |
worksheet.write(0, inc(), 'Version Code', boldStyle)
|
|
|
142 |
worksheet1.write(0, i, 'Version Code', boldStyle)
|
|
|
143 |
worksheet2.write(0, i, 'Version Code', boldStyle)
|
|
|
144 |
worksheet.write(0, inc(), 'Merchant Order Id', boldStyle)
|
|
|
145 |
worksheet1.write(0, i, 'Merchant Order Id', boldStyle)
|
|
|
146 |
worksheet2.write(0, i, 'Merchant Order Id', boldStyle)
|
|
|
147 |
worksheet.write(0, inc(), 'Product Title', boldStyle)
|
|
|
148 |
worksheet1.write(0, i, 'Product Title', boldStyle)
|
|
|
149 |
worksheet2.write(0, i, 'Product Title', boldStyle)
|
|
|
150 |
worksheet.write(0, inc(), 'Price', boldStyle)
|
|
|
151 |
worksheet1.write(0, i, 'Price', boldStyle)
|
|
|
152 |
worksheet2.write(0, i, 'Price', boldStyle)
|
|
|
153 |
worksheet.write(0, inc(), 'Quantity', boldStyle)
|
|
|
154 |
worksheet1.write(0, i, 'Quantity', boldStyle)
|
|
|
155 |
worksheet2.write(0, i, 'Quantity', boldStyle)
|
|
|
156 |
worksheet.write(0, inc(), 'Status', boldStyle)
|
|
|
157 |
worksheet1.write(0, i, 'Status', boldStyle)
|
|
|
158 |
worksheet2.write(0, i, 'Status', boldStyle)
|
|
|
159 |
worksheet.write(0, inc(), 'Detailed Status', boldStyle)
|
|
|
160 |
worksheet1.write(0, i, 'Detailed Status', boldStyle)
|
|
|
161 |
worksheet2.write(0, i, 'Detailed Status', boldStyle)
|
|
|
162 |
worksheet.write(0, inc(), 'Cashback Status', boldStyle)
|
|
|
163 |
worksheet1.write(0, i, 'Cashback Status', boldStyle)
|
|
|
164 |
worksheet2.write(0, i, 'Cashback Status', boldStyle)
|
|
|
165 |
worksheet.write(0, inc(), 'CashBack', boldStyle)
|
|
|
166 |
worksheet1.write(0, i, 'CashBack', boldStyle)
|
|
|
167 |
worksheet2.write(0, i, 'CashBack', boldStyle)
|
|
|
168 |
worksheet.write(0, inc(), 'SubtagId', boldStyle)
|
|
|
169 |
worksheet1.write(0, i, 'SubtagId', boldStyle)
|
|
|
170 |
worksheet2.write(0, i, 'SubtagId', boldStyle)
|
|
|
171 |
worksheet.write(0, inc(), 'Sale Date', boldStyle)
|
|
|
172 |
worksheet1.write(0, i, 'Sale Date', boldStyle)
|
|
|
173 |
worksheet2.write(0, i, 'Sale Date', boldStyle)
|
|
|
174 |
worksheet.write(0, inc(), 'Reconciled', boldStyle)
|
|
|
175 |
worksheet1.write(0, i, 'Reconciled', boldStyle)
|
|
|
176 |
i=-1
|
|
|
177 |
worksheet3.write(0, inc(), 'linkType',boldStyle)
|
|
|
178 |
worksheet3.write(0, inc(), 'totalOrderedQuantity',boldStyle)
|
|
|
179 |
worksheet3.write(0, inc(), 'dateOrdered',boldStyle)
|
|
|
180 |
worksheet3.write(0, inc(), 'directOrderedQuantity',boldStyle)
|
|
|
181 |
worksheet3.write(0, inc(), 'indirectOrderedQuantity',boldStyle)
|
|
|
182 |
worksheet3.write(0, inc(), 'directClicks',boldStyle)
|
|
|
183 |
worksheet3.write(0, inc(), 'trackingId',boldStyle)
|
|
|
184 |
worksheet3.write(0, inc(), 'subTagId',boldStyle)
|
|
|
185 |
worksheet3.write(0, inc(), 'productLine',boldStyle)
|
|
|
186 |
|
|
|
187 |
def main():
|
|
|
188 |
generateAmazonReco("amazon.xls")
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
def inc():
|
|
|
192 |
global i
|
|
|
193 |
i+=1
|
|
|
194 |
return i
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
if __name__ == '__main__':
|
|
|
198 |
main()
|