Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
import pymongofrom datetime import datetime,time, timedelta, datefrom dtr.main import refundToWalletfrom dtr.storage import Mongofrom dtr.utils import utilsimport mathimport timecon = Nonedef get_mongo_connection(host='localhost', port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn conuserAmountMap = {}def calculateCashbackForUser(i):cashbackToBeCredited = 0deliveredValue = 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)*deliveredValueelif deliveredValue>=target2Amount:cashbackToBeCredited = (target2CashBackPercentage/100.0)*deliveredValueif cashbackToBeCredited > maximumCashback:cashbackToBeCredited = maximumCashbackelse:passreturn cashbackToBeCrediteddef offerCashback():cursor = get_mongo_connection().Catalog.PromoOffer.find()global userAmountMaptotalDeliveredSum = 0totalCashbackAmount = 0for 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 totalCashbackAmountdef 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',batchIdif __name__ == '__main__':main()