Subversion Repositories SmartDukaan

Rev

Rev 17003 | Rev 17005 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16727 manish.sha 1
from datetime import datetime
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':
29
        currentTimestamp = datetime.today()
30
        currentMonth = currentTimestamp.month
31
        currentDay = currentTimestamp.day
32
        currentYear = currentTimestamp.year
33
        fortNight = (currentMonth - 1)*2 + (currentDay/15)
34
 
16998 manish.sha 35
        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 36
        userCashbacksMap = {}
37
        for userCashback in userCashbacksToBeCredited:
38
            if userCashback.user_id >0:
39
                if userCashbacksMap.has_key(userCashback.user_id):
40
                    cashBackAmount = userCashbacksMap.get(userCashback.user_id) + userCashback.amount
41
                    userCashbacksMap[userCashback.user_id] = cashBackAmount
42
                else:
43
                    userCashbacksMap[userCashback.user_id] = userCashback.amount
44
        datetimeNow = datetime.now() 
45
        batchId = int(time.mktime(datetimeNow.timetuple()))
16739 manish.sha 46
        if(refundToWallet(batchId, userCashbacksMap)):
16727 manish.sha 47
            for userCashback in userCashbacksToBeCredited:
48
                userCashback.status = 'Credited'
49
                userCashback.batchCreditId = batchId
16862 manish.sha 50
                userCashback.creditedDate = datetimeNow.date()
16727 manish.sha 51
            session.commit()
16739 manish.sha 52
 
53
            for key, value in userCashbacksMap.iteritems():
17003 manish.sha 54
                print 'User Id:- ', key, ' Credited Amount:- ', value
16739 manish.sha 55
                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})
56
                get_mongo_connection().Dtr.user.update({"userId":key}, {"$inc": { "credited": value, utils.CREDIT_TYPE_APP:value}}, upsert=True)
16999 manish.sha 57
        else:
58
            print 'Refund Process have been failed'
17000 manish.sha 59
    else:
60
        print 'Dry Run:- Only showing user map for which cashback have been updated'
61
        currentTimestamp = datetime.today()
62
        currentMonth = currentTimestamp.month
63
        currentDay = currentTimestamp.day
64
        currentYear = currentTimestamp.year
65
        fortNight = (currentMonth - 1)*2 + (currentDay/15)
16727 manish.sha 66
 
17000 manish.sha 67
        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()
68
        userCashbacksMap = {}
69
        for userCashback in userCashbacksToBeCredited:
70
            if userCashback.user_id >0:
71
                if userCashbacksMap.has_key(userCashback.user_id):
72
                    cashBackAmount = userCashbacksMap.get(userCashback.user_id) + userCashback.amount
73
                    userCashbacksMap[userCashback.user_id] = cashBackAmount
74
                else:
75
                    userCashbacksMap[userCashback.user_id] = userCashback.amount
76
 
77
        datetimeNow = datetime.now() 
78
        batchId = int(time.mktime(datetimeNow.timetuple()))
79
        print 'BatchId:- ', batchId
80
 
81
        for key, value in userCashbacksMap.iteritems():
82
            print 'User Id:- ', key, ' Cashback Value:- ', value
83
 
16727 manish.sha 84
def main(argv):
85
    runtype = 'dry'
86
    try:
87
        opts, args = getopt.getopt(argv,"ht:",["runtype="])
88
    except getopt.GetoptError:
89
        print 'settlementcron.py -t <dry|live>'
90
        sys.exit(2)
91
    for opt, arg in opts:
92
        if opt == '-h':
93
            print 'settlementcron.py -t <dry|live>'
94
            sys.exit()
95
        elif opt == '-t':
96
            runtype = arg
97
 
17004 manish.sha 98
        tprint("Settling payback", runtype)
16727 manish.sha 99
        try:
100
            settleAppOffersPayBack(runtype)
101
        except:
102
            tprint("Error")
103
            traceback.print_exc()
17002 manish.sha 104
    session.close()
16727 manish.sha 105
if __name__=='__main__':
106
    main(sys.argv[1:])