Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

import pymongo
from datetime import datetime,time, timedelta, date
from dtr.main import refundToWallet
from dtr.storage import Mongo
from dtr.utils import utils
import math
import time
con = None
def get_mongo_connection(host='localhost', port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con

userAmountMap = {}

def calculateCashbackForUser(i):
    cashbackToBeCredited = 0 
    deliveredValue = i.get('delivered_order_value')
    maximumCashback = i.get('maxCashBack')
    target2CashBackPercentage = i.get('target2_cash_back_percetage')
    target1CashBackPercentage= i.get('target1_cash_back_percetage')
    target1Amount = i.get('target1')
    target2Amount = i.get('target2')
    if deliveredValue>=target1Amount and deliveredValue<target2Amount:
        cashbackToBeCredited = (target1CashBackPercentage/100.0)*deliveredValue
    elif deliveredValue>=target2Amount:
        cashbackToBeCredited = (target2CashBackPercentage/100.0)*deliveredValue
        if cashbackToBeCredited > maximumCashback:
            cashbackToBeCredited = maximumCashback
    else:
        pass
    return cashbackToBeCredited

def offerCashback():
    cursor = get_mongo_connection().Catalog.PromoOffer.find()
    global userAmountMap
    totalDeliveredSum = 0
    totalCashbackAmount = 0
    for i in cursor:
        if i.get('delivered_order_value') !=0:
            totalDeliveredSum = totalDeliveredSum + i.get('delivered_order_value') 
            cashbackAmount = calculateCashbackForUser(i)
            userId = i.get('user_id')
            if cashbackAmount!=0:
                if userAmountMap.has_key(userId):
                    userAmountMap[userId] = userAmountMap.get(userId)+ math.floor(cashbackAmount)
                else:
                    userAmountMap[userId] = math.floor(cashbackAmount)
#     for k,v in userAmountMap.iteritems():
#         print k,v
#         totalCashbackAmount = totalCashbackAmount + v
#     print totalDeliveredSum
#     print totalCashbackAmount
def main():
    offerCashback()
#     message = []
#     datetimeNow = datetime.now()
#     batchId = int(time.mktime(datetimeNow.timetuple()))
#     if refundToWallet(batchId,userAmountMap):
#         sum=0
#         for key, value in userAmountMap.iteritems():
#             sum += value
#             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})
#             get_mongo_connection().Dtr.user.update({"userId":key}, {"$inc": { "credited": value, utils.CREDIT_TYPE_OFFER:value}}, upsert=True)
#             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))
#         message.append("<b>Batch Id - %d</b><br><b>Total Amount Credited - %d</b><br>"%(batchId,sum))
#         utils.sendmail(['manas.kapoor@shop2020.in'], "".join(message), 'Cashback for April Offer Credited Successfully')
#     else:
#         print 'Cashback credit failed for all the users for batch id',batchId
if __name__ == '__main__':
    main()