| 18173 |
manish.sha |
1 |
from datetime import datetime, timedelta
|
| 16727 |
manish.sha |
2 |
from dtr.storage.DataService import user_app_cashbacks
|
|
|
3 |
import sys
|
|
|
4 |
import traceback
|
|
|
5 |
import getopt
|
|
|
6 |
from dtr.main import tprint, refundToWallet
|
|
|
7 |
import time
|
|
|
8 |
from dtr.storage import DataService
|
|
|
9 |
from elixir import *
|
| 16739 |
manish.sha |
10 |
import pymongo
|
|
|
11 |
from dtr.utils import utils
|
| 16727 |
manish.sha |
12 |
|
|
|
13 |
DataService.initialize(db_hostname="localhost")
|
| 16739 |
manish.sha |
14 |
con=None
|
| 16727 |
manish.sha |
15 |
|
| 16739 |
manish.sha |
16 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
17 |
global con
|
|
|
18 |
if con is None:
|
|
|
19 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
20 |
try:
|
|
|
21 |
con = pymongo.MongoClient(host, port)
|
|
|
22 |
except Exception, e:
|
|
|
23 |
print e
|
|
|
24 |
return None
|
|
|
25 |
return con
|
|
|
26 |
|
| 16727 |
manish.sha |
27 |
def settleAppOffersPayBack(runtype='dry'):
|
|
|
28 |
if runtype == 'live':
|
| 17005 |
manish.sha |
29 |
print 'Live Run:- Amount will be credited in the specific users'
|
| 16727 |
manish.sha |
30 |
currentTimestamp = datetime.today()
|
|
|
31 |
currentMonth = currentTimestamp.month
|
|
|
32 |
currentDay = currentTimestamp.day
|
|
|
33 |
currentYear = currentTimestamp.year
|
| 18173 |
manish.sha |
34 |
sevenDaysBeforeTimestamp = datetime.today() - timedelta(days=7)
|
|
|
35 |
sevenDaysBeforeYear = sevenDaysBeforeTimestamp.year
|
|
|
36 |
if currentYear > sevenDaysBeforeYear:
|
|
|
37 |
currentMonth = sevenDaysBeforeTimestamp.month
|
|
|
38 |
currentDay = sevenDaysBeforeTimestamp.day
|
|
|
39 |
currentYear = sevenDaysBeforeYear
|
|
|
40 |
fortNight = (currentMonth - 1)*2 + (currentDay/15)+1
|
|
|
41 |
else:
|
|
|
42 |
fortNight = (currentMonth - 1)*2 + (currentDay/15)
|
|
|
43 |
|
|
|
44 |
print "FortNight: ", fortNight, "Year: ", currentYear
|
| 16727 |
manish.sha |
45 |
|
| 16998 |
manish.sha |
46 |
userCashbacksToBeCredited = user_app_cashbacks.query.filter(user_app_cashbacks.user_id>0).filter(user_app_cashbacks.status=='Approved').filter(user_app_cashbacks.fortnightOfYear<fortNight).filter(user_app_cashbacks.yearVal==currentYear).all()
|
| 16727 |
manish.sha |
47 |
userCashbacksMap = {}
|
|
|
48 |
for userCashback in userCashbacksToBeCredited:
|
|
|
49 |
if userCashback.user_id >0:
|
|
|
50 |
if userCashbacksMap.has_key(userCashback.user_id):
|
|
|
51 |
cashBackAmount = userCashbacksMap.get(userCashback.user_id) + userCashback.amount
|
|
|
52 |
userCashbacksMap[userCashback.user_id] = cashBackAmount
|
|
|
53 |
else:
|
|
|
54 |
userCashbacksMap[userCashback.user_id] = userCashback.amount
|
|
|
55 |
datetimeNow = datetime.now()
|
|
|
56 |
batchId = int(time.mktime(datetimeNow.timetuple()))
|
| 16739 |
manish.sha |
57 |
if(refundToWallet(batchId, userCashbacksMap)):
|
| 16727 |
manish.sha |
58 |
for userCashback in userCashbacksToBeCredited:
|
|
|
59 |
userCashback.status = 'Credited'
|
|
|
60 |
userCashback.batchCreditId = batchId
|
| 16862 |
manish.sha |
61 |
userCashback.creditedDate = datetimeNow.date()
|
| 16727 |
manish.sha |
62 |
session.commit()
|
| 16739 |
manish.sha |
63 |
|
|
|
64 |
for key, value in userCashbacksMap.iteritems():
|
| 17003 |
manish.sha |
65 |
print 'User Id:- ', key, ' Credited Amount:- ', value
|
| 16739 |
manish.sha |
66 |
get_mongo_connection().Dtr.refund.insert({"userId": key, "batch":batchId, "userAmount":value, "timestamp":datetime.strftime(datetimeNow,"%Y-%m-%d %H:%M:%S"), "type":utils.CREDIT_TYPE_APP})
|
|
|
67 |
get_mongo_connection().Dtr.user.update({"userId":key}, {"$inc": { "credited": value, utils.CREDIT_TYPE_APP:value}}, upsert=True)
|
| 16999 |
manish.sha |
68 |
else:
|
|
|
69 |
print 'Refund Process have been failed'
|
| 17000 |
manish.sha |
70 |
else:
|
|
|
71 |
print 'Dry Run:- Only showing user map for which cashback have been updated'
|
|
|
72 |
currentTimestamp = datetime.today()
|
|
|
73 |
currentMonth = currentTimestamp.month
|
|
|
74 |
currentDay = currentTimestamp.day
|
|
|
75 |
currentYear = currentTimestamp.year
|
| 18173 |
manish.sha |
76 |
sevenDaysBeforeTimestamp = datetime.today() - timedelta(days=7)
|
|
|
77 |
sevenDaysBeforeYear = sevenDaysBeforeTimestamp.year
|
|
|
78 |
if currentYear > sevenDaysBeforeYear:
|
|
|
79 |
currentMonth = sevenDaysBeforeTimestamp.month
|
|
|
80 |
currentDay = sevenDaysBeforeTimestamp.day
|
|
|
81 |
currentYear = sevenDaysBeforeYear
|
|
|
82 |
fortNight = (currentMonth - 1)*2 + (currentDay/15)+1
|
|
|
83 |
else:
|
|
|
84 |
fortNight = (currentMonth - 1)*2 + (currentDay/15)
|
|
|
85 |
|
|
|
86 |
print "FortNight: ", fortNight, "Year: ", currentYear
|
| 16727 |
manish.sha |
87 |
|
| 17000 |
manish.sha |
88 |
userCashbacksToBeCredited = user_app_cashbacks.query.filter(user_app_cashbacks.user_id>0).filter(user_app_cashbacks.status=='Approved').filter(user_app_cashbacks.fortnightOfYear<fortNight).filter(user_app_cashbacks.yearVal==currentYear).all()
|
|
|
89 |
userCashbacksMap = {}
|
|
|
90 |
for userCashback in userCashbacksToBeCredited:
|
|
|
91 |
if userCashback.user_id >0:
|
|
|
92 |
if userCashbacksMap.has_key(userCashback.user_id):
|
|
|
93 |
cashBackAmount = userCashbacksMap.get(userCashback.user_id) + userCashback.amount
|
|
|
94 |
userCashbacksMap[userCashback.user_id] = cashBackAmount
|
|
|
95 |
else:
|
|
|
96 |
userCashbacksMap[userCashback.user_id] = userCashback.amount
|
|
|
97 |
|
|
|
98 |
datetimeNow = datetime.now()
|
|
|
99 |
batchId = int(time.mktime(datetimeNow.timetuple()))
|
|
|
100 |
print 'BatchId:- ', batchId
|
|
|
101 |
|
|
|
102 |
for key, value in userCashbacksMap.iteritems():
|
|
|
103 |
print 'User Id:- ', key, ' Cashback Value:- ', value
|
|
|
104 |
|
| 16727 |
manish.sha |
105 |
def main(argv):
|
|
|
106 |
runtype = 'dry'
|
|
|
107 |
try:
|
|
|
108 |
opts, args = getopt.getopt(argv,"ht:",["runtype="])
|
|
|
109 |
except getopt.GetoptError:
|
|
|
110 |
print 'settlementcron.py -t <dry|live>'
|
|
|
111 |
sys.exit(2)
|
|
|
112 |
for opt, arg in opts:
|
|
|
113 |
if opt == '-h':
|
|
|
114 |
print 'settlementcron.py -t <dry|live>'
|
|
|
115 |
sys.exit()
|
|
|
116 |
elif opt == '-t':
|
|
|
117 |
runtype = arg
|
|
|
118 |
|
| 17004 |
manish.sha |
119 |
tprint("Settling payback", runtype)
|
| 16727 |
manish.sha |
120 |
try:
|
|
|
121 |
settleAppOffersPayBack(runtype)
|
|
|
122 |
except:
|
|
|
123 |
tprint("Error")
|
|
|
124 |
traceback.print_exc()
|
| 17002 |
manish.sha |
125 |
session.close()
|
| 16727 |
manish.sha |
126 |
if __name__=='__main__':
|
|
|
127 |
main(sys.argv[1:])
|