Subversion Repositories SmartDukaan

Rev

Rev 16727 | Rev 16862 | 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
50
            session.commit()
16739 manish.sha 51
 
52
            for key, value in userCashbacksMap.iteritems():
53
                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})
54
                get_mongo_connection().Dtr.user.update({"userId":key}, {"$inc": { "credited": value, utils.CREDIT_TYPE_APP:value}}, upsert=True)
16727 manish.sha 55
 
56
def main(argv):
57
    runtype = 'dry'
58
    try:
59
        opts, args = getopt.getopt(argv,"ht:",["runtype="])
60
    except getopt.GetoptError:
61
        print 'settlementcron.py -t <dry|live>'
62
        sys.exit(2)
63
    for opt, arg in opts:
64
        if opt == '-h':
65
            print 'settlementcron.py -t <dry|live>'
66
            sys.exit()
67
        elif opt == '-t':
68
            runtype = arg
69
 
70
        tprint("Settling payback", type)
71
        try:
72
            settleAppOffersPayBack(runtype)
73
        except:
74
            tprint("Error")
75
            traceback.print_exc()
76
    if session.is_active():
77
        session.close()
78
if __name__=='__main__':
79
    main(sys.argv[1:])