Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
19797 manas 1
import pymongo
2
from datetime import datetime,time, timedelta, date
3
from dtr.main import refundToWallet
4
from dtr.storage import Mongo
5
from dtr.utils import utils
6
import math
7
import time
8
con = None
9
def get_mongo_connection(host='localhost', port=27017):
10
    global con
11
    if con is None:
12
        print "Establishing connection %s host and port %d" %(host,port)
13
        try:
14
            con = pymongo.MongoClient(host, port)
15
        except Exception, e:
16
            print e
17
            return None
18
    return con
19
 
20
userAmountMap = {}
21
 
22
def calculateCashbackForUser(i):
23
    cashbackToBeCredited = 0 
24
    deliveredValue = i.get('delivered_order_value')
25
    maximumCashback = i.get('maxCashBack')
26
    target2CashBackPercentage = i.get('target2_cash_back_percetage')
27
    target1CashBackPercentage= i.get('target1_cash_back_percetage')
28
    target1Amount = i.get('target1')
29
    target2Amount = i.get('target2')
30
    if deliveredValue>=target1Amount and deliveredValue<target2Amount:
31
        cashbackToBeCredited = (target1CashBackPercentage/100.0)*deliveredValue
32
    elif deliveredValue>=target2Amount:
33
        cashbackToBeCredited = (target2CashBackPercentage/100.0)*deliveredValue
34
        if cashbackToBeCredited > maximumCashback:
35
            cashbackToBeCredited = maximumCashback
36
    else:
37
        pass
38
    return cashbackToBeCredited
39
 
40
def offerCashback():
41
    cursor = get_mongo_connection().Catalog.PromoOffer.find()
42
    global userAmountMap
43
    totalDeliveredSum = 0
44
    totalCashbackAmount = 0
45
    for i in cursor:
46
        if i.get('delivered_order_value') !=0:
47
            totalDeliveredSum = totalDeliveredSum + i.get('delivered_order_value') 
48
            cashbackAmount = calculateCashbackForUser(i)
49
            userId = i.get('user_id')
50
            if cashbackAmount!=0:
51
                if userAmountMap.has_key(userId):
52
                    userAmountMap[userId] = userAmountMap.get(userId)+ math.floor(cashbackAmount)
53
                else:
54
                    userAmountMap[userId] = math.floor(cashbackAmount)
55
#     for k,v in userAmountMap.iteritems():
56
#         print k,v
57
#         totalCashbackAmount = totalCashbackAmount + v
58
#     print totalDeliveredSum
59
#     print totalCashbackAmount
60
def main():
61
    offerCashback()
62
#     message = []
63
#     datetimeNow = datetime.now()
64
#     batchId = int(time.mktime(datetimeNow.timetuple()))
65
#     if refundToWallet(batchId,userAmountMap):
66
#         sum=0
67
#         for key, value in userAmountMap.iteritems():
68
#             sum += value
69
#             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_OFFER})
70
#             get_mongo_connection().Dtr.user.update({"userId":key}, {"$inc": { "credited": value, utils.CREDIT_TYPE_OFFER:value}}, upsert=True)
71
#             Mongo.sendNotification([key], 'Batch Credit','Cashback Credited for %ss'%(str(utils.CREDIT_TYPE_OFFER)),'Rs.%s has been added to your wallet'%(value),'url','http://api.profittill.com/cashbacks/mine?user_id=%s'%(key), '2999-01-01', True, "TRAN_SMS Dear Customer, Cashback Credited for %ss. Rs.%s has been added to your wallet"%(utils.CREDIT_TYPE_OFFER,value))
72
#         message.append("<b>Batch Id - %d</b><br><b>Total Amount Credited - %d</b><br>"%(batchId,sum))
73
#         utils.sendmail(['manas.kapoor@shop2020.in'], "".join(message), 'Cashback for April Offer Credited Successfully')
74
#     else:
75
#         print 'Cashback credit failed for all the users for batch id',batchId
76
if __name__ == '__main__':
77
    main()