Subversion Repositories SmartDukaan

Rev

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