Subversion Repositories SmartDukaan

Rev

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