Subversion Repositories SmartDukaan

Rev

Rev 16739 | Rev 16998 | 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
 
35
        userCashbacksToBeCredited = user_app_cashbacks.query.filter(user_app_cashbacks.status=='Approved').filter(user_app_cashbacks.fortnightOfYear<fortNight).filter(user_app_cashbacks.yearVal==currentYear).all()
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)
16727 manish.sha 56
 
57
def main(argv):
58
    runtype = 'dry'
59
    try:
60
        opts, args = getopt.getopt(argv,"ht:",["runtype="])
61
    except getopt.GetoptError:
62
        print 'settlementcron.py -t <dry|live>'
63
        sys.exit(2)
64
    for opt, arg in opts:
65
        if opt == '-h':
66
            print 'settlementcron.py -t <dry|live>'
67
            sys.exit()
68
        elif opt == '-t':
69
            runtype = arg
70
 
71
        tprint("Settling payback", type)
72
        try:
73
            settleAppOffersPayBack(runtype)
74
        except:
75
            tprint("Error")
76
            traceback.print_exc()
77
    if session.is_active():
78
        session.close()
79
if __name__=='__main__':
80
    main(sys.argv[1:])