Subversion Repositories SmartDukaan

Rev

Rev 16739 | Go to most recent revision | Details | 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 *
10
 
11
DataService.initialize(db_hostname="localhost") 
12
 
13
def settleAppOffersPayBack(runtype='dry'):
14
    if runtype == 'live':
15
        currentTimestamp = datetime.today()
16
        currentMonth = currentTimestamp.month
17
        currentDay = currentTimestamp.day
18
        currentYear = currentTimestamp.year
19
        fortNight = (currentMonth - 1)*2 + (currentDay/15)
20
 
21
        userCashbacksToBeCredited = user_app_cashbacks.query.filter(user_app_cashbacks.status=='Approved').filter(user_app_cashbacks.fortnightOfYear<fortNight).filter(user_app_cashbacks.yearVal==currentYear).all()
22
        userCashbacksMap = {}
23
        for userCashback in userCashbacksToBeCredited:
24
            if userCashback.user_id >0:
25
                if userCashbacksMap.has_key(userCashback.user_id):
26
                    cashBackAmount = userCashbacksMap.get(userCashback.user_id) + userCashback.amount
27
                    userCashbacksMap[userCashback.user_id] = cashBackAmount
28
                else:
29
                    userCashbacksMap[userCashback.user_id] = userCashback.amount
30
        datetimeNow = datetime.now() 
31
        batchId = int(time.mktime(datetimeNow.timetuple()))
32
        if(refundToWallet(batchId, userCashbacksMap, 'App')):
33
            for userCashback in userCashbacksToBeCredited:
34
                userCashback.status = 'Credited'
35
                userCashback.batchCreditId = batchId
36
            session.commit()
37
 
38
def main(argv):
39
    runtype = 'dry'
40
    try:
41
        opts, args = getopt.getopt(argv,"ht:",["runtype="])
42
    except getopt.GetoptError:
43
        print 'settlementcron.py -t <dry|live>'
44
        sys.exit(2)
45
    for opt, arg in opts:
46
        if opt == '-h':
47
            print 'settlementcron.py -t <dry|live>'
48
            sys.exit()
49
        elif opt == '-t':
50
            runtype = arg
51
 
52
        tprint("Settling payback", type)
53
        try:
54
            settleAppOffersPayBack(runtype)
55
        except:
56
            tprint("Error")
57
            traceback.print_exc()
58
    if session.is_active():
59
        session.close()
60
if __name__=='__main__':
61
    main(sys.argv[1:])