| Line 212... |
Line 212... |
| 212 |
subOrders = order.get("subOrders")
|
212 |
subOrders = order.get("subOrders")
|
| 213 |
for subOrder in subOrders:
|
213 |
for subOrder in subOrders:
|
| 214 |
if merchantSubOrderId == subOrder.get("merchantSubOrderId"):
|
214 |
if merchantSubOrderId == subOrder.get("merchantSubOrderId"):
|
| 215 |
return subOrder
|
215 |
return subOrder
|
| 216 |
return None
|
216 |
return None
|
| 217 |
|
217 |
|
| 218 |
def settlePayBack(runtype='dry'):
|
218 |
def settlePayBack(runtype='dry'):
|
| 219 |
result = client.Dtr.merchantOrder\
|
219 |
userAmountMap = {}
|
| 220 |
.aggregate([
|
220 |
searchMapList = []
|
| 221 |
{'$match':{'subOrders.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}},
|
221 |
for mo in client.Dtr.merchantOrder.find({"subOrders.cashBackStatus":Store.CB_APPROVED}):
|
| 222 |
{'$unwind':"$subOrders"},
|
222 |
userId = mo.get("userId")
|
| 223 |
{
|
223 |
if mo.get('subOrders') is not None:
|
| 224 |
'$group':{
|
224 |
for so in mo['subOrders']:
|
| - |
|
225 |
if so.get('cashBackStatus') == Store.CB_APPROVED:
|
| - |
|
226 |
searchMapList.append({"orderId":mo.get("orderId"), "subOrders.merchantSubOrderId":so.get("merchantSubOrderId")})
|
| 225 |
'_id':'$userId',
|
227 |
if not userAmountMap.has_key(userId):
|
| 226 |
'amount': { '$sum':'$subOrders.cashBackAmount'},
|
228 |
userAmountMap[userId] = so.get('cashBackAmount')
|
| 227 |
}
|
229 |
else:
|
| - |
|
230 |
userAmountMap[userId] += so.get('cashBackAmount')
|
| - |
|
231 |
print "%s\t%s\t%s\t%s\t%s\t%s\t%s"%(userId, mo.get("orderId"), so.get("merchantSubOrderId"),so.get("productTitle") ,so.get("cashBackStatus"), so.get("cashBackAmount"), so.get("batchId"))
|
| 228 |
}
|
232 |
else:
|
| - |
|
233 |
print "%s\t%s\t%s\t%s\t%s\t%s\t%s"%(userId, mo.get("orderId"), so.get("merchantSubOrderId"),so.get("productTitle") ,so.get("cashBackStatus"), so.get("cashBackAmount"), so.get("batchId"))
|
| 229 |
])['result']
|
234 |
for searchMap in searchMapList:
|
| - |
|
235 |
print "%s\t%s"%(searchMap.get('orderId'),searchMap.get('subOrders.merchantSubOrderId'))
|
| 230 |
if runtype!='live':
|
236 |
if (runtype=='live'):
|
| - |
|
237 |
bulk = client.Dtr.merchantOrder.initialize_ordered_bulk_op()
|
| 231 |
print result
|
238 |
if len(searchMapList) == 0:
|
| 232 |
return
|
239 |
return
|
| 233 |
client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_APPROVED},{'$set':{'subOrders.$.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}}, multi=True)
|
- |
|
| 234 |
userAmountMap = {}
|
240 |
for searchMap in searchMapList:
|
| 235 |
print result
|
241 |
bulk.find(searchMap).update({'$set' : {'subOrders.$.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}})
|
| 236 |
for res in result:
|
242 |
bulk.execute()
|
| 237 |
userAmountMap[res['_id']] = res['amount']
|
- |
|
| - |
|
243 |
|
| 238 |
datetimeNow = datetime.now()
|
244 |
datetimeNow = datetime.now()
|
| 239 |
batchId = int(time.mktime(datetimeNow.timetuple()))
|
245 |
batchId = int(time.mktime(datetimeNow.timetuple()))
|
| 240 |
if refundToWallet(batchId, userAmountMap):
|
246 |
if refundToWallet(batchId, userAmountMap):
|
| - |
|
247 |
bulk = client.Dtr.merchantOrder.initialize_ordered_bulk_op()
|
| - |
|
248 |
for searchMap in searchMapList:
|
| 241 |
client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_CREDIT_IN_PROCESS},{'$set':{'subOrders.$.cashBackStatus':Store.CB_CREDITED, 'subOrders.$.batchId':batchId}}, multi=True)
|
249 |
bulk.find(searchMap).update({'$set' : {'subOrders.$.cashBackStatus':Store.CB_CREDITED, "subOrders.$.batchId":batchId}})
|
| - |
|
250 |
print bulk.execute()
|
| 242 |
for key, value in userAmountMap.iteritems():
|
251 |
for key, value in userAmountMap.iteritems():
|
| 243 |
client.Dtr.refund.insert({"userId": key, "batch":batchId, "userAmount":value, "timestamp":datetime.strftime(datetimeNow,"%Y-%m-%d %H:%M:%S")})
|
252 |
client.Dtr.refund.insert({"userId": key, "batch":batchId, "userAmount":value, "timestamp":datetime.strftime(datetimeNow,"%Y-%m-%d %H:%M:%S")})
|
| 244 |
client.Dtr.user.update({"userId":key}, {'$inc': { "credited": value}}, upsert=True)
|
253 |
client.Dtr.user.update({"userId":key}, {'$inc': { "credited": value}}, upsert=True)
|
| 245 |
tprint("PayBack Settled")
|
254 |
tprint("PayBack Settled")
|
| 246 |
else:
|
255 |
else:
|
| 247 |
tprint("Error Occurred while running batch. Rolling Back")
|
256 |
tprint("Error Occurred while running batch. Rolling Back")
|
| - |
|
257 |
bulk = client.Dtr.merchantOrder.initialize_ordered_bulk_op()
|
| - |
|
258 |
for searchMap in searchMapList:
|
| 248 |
client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_CREDIT_IN_PROCESS},{'$set':{'subOrders.$.cashBackStatus':Store.CB_APPROVED}}, multi=True)
|
259 |
bulk.find(searchMap).update({'$set' : {'subOrders.$.cashBackStatus':Store.CB_APPROVED}})
|
| 249 |
|
- |
|
| 250 |
def refundToWallet(batchId, userAmountMap):
|
260 |
bulk.execute()
|
| 251 |
|
261 |
|
| - |
|
262 |
|
| - |
|
263 |
def settlePayBack1(runtype='dry'):
|
| - |
|
264 |
approvedUserMap = {}
|
| - |
|
265 |
pendingUserMap = {}
|
| - |
|
266 |
creditedToWalletUserMap = {}
|
| - |
|
267 |
#client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_APPROVED},{'$set':{'subOrders.$.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}}, multi=True)
|
| - |
|
268 |
for mo in client.Dtr.merchantOrder.find({"subOrders.cashBackStatus":Store.CB_CREDITED}):
|
| - |
|
269 |
userId = mo.get("userId")
|
| - |
|
270 |
for so in mo['subOrders']:
|
| - |
|
271 |
print "%s\t%s\t%s\t%s\t%s\t%s\t%s"%(userId, mo.get("orderId"), so.get("merchantSubOrderId"),so.get("productTitle") ,so.get("cashBackStatus"), so.get("cashBackAmount"), so.get("batchId"))
|
| - |
|
272 |
|
| - |
|
273 |
for refund in client.Dtr.refund.find():
|
| - |
|
274 |
print "%s\t%s\t%s\t%s"%(refund.get("userId") ,refund.get("timestamp") ,refund.get("userAmount"), refund.get("batch"))
|
| - |
|
275 |
|
| - |
|
276 |
|
| - |
|
277 |
def refundToWallet(batchId, userAmountMap):
|
| - |
|
278 |
return True
|
| 252 |
base64string = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
279 |
base64string = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
| 253 |
batchUpdateMap = {}
|
280 |
batchUpdateMap = {}
|
| 254 |
try :
|
281 |
try :
|
| 255 |
saholicUserAmountMap = {}
|
282 |
saholicUserAmountMap = {}
|
| 256 |
for key, value in userAmountMap.iteritems():
|
283 |
for key, value in userAmountMap.iteritems():
|
| Line 285... |
Line 312... |
| 285 |
# db = client.Dtr
|
312 |
# db = client.Dtr
|
| 286 |
# for order in orders:
|
313 |
# for order in orders:
|
| 287 |
# for subOrder in order.get("subOrders"):
|
314 |
# for subOrder in order.get("subOrders"):
|
| 288 |
# #db.merchantOrder.update({"merchantOrderId":order.getMerchantOrderId,"subOrders.imgUrl":{"$exists":0}, "subOrders.productCode":subOrder.get("productCode")}, {"$set":{"subOrders.$.imgUrl":subOrder.get("imgUrl")}})
|
315 |
# #db.merchantOrder.update({"merchantOrderId":order.getMerchantOrderId,"subOrders.imgUrl":{"$exists":0}, "subOrders.productCode":subOrder.get("productCode")}, {"$set":{"subOrders.$.imgUrl":subOrder.get("imgUrl")}})
|
| 289 |
# db.merchantOrder.findOne()
|
316 |
# db.merchantOrder.findOne()
|
| 290 |
settlePayBack()
|
317 |
settlePayBack('dry')
|
| 291 |
|
318 |
|
| 292 |
|
319 |
|
| 293 |
###
|
320 |
###
|
| 294 |
#Settlement process is suposed to be a batch and run weekly
|
321 |
#Settlement process is suposed to be a batch and run weekly
|
| 295 |
#It is should be running on first hour of mondays. As cron should
|
322 |
#It is should be running on first hour of mondays. As cron should
|