Subversion Repositories SmartDukaan

Rev

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

from bson.objectid import ObjectId
from datetime import datetime, timedelta
from dtr.config import PythonPropertyReader
from dtr.dao import FeaturedDeals, AppTransactions
from dtr.storage import DataService
from dtr.storage.DataService import price_preferences, brand_preferences, \
    user_actions, Brands, app_offers, approved_app_transactions, user_app_cashbacks, user_app_installs
from dtr.storage.MemCache import MemCache
from dtr.utils.FetchLivePrices import returnLatestPrice
from dtr.utils.utils import to_java_date, CB_PENDING, CB_APPROVED, CB_INIT, to_py_date
from elixir import *
from operator import itemgetter
import pymongo
import random
import re
import traceback
import time

con = None

DataService.initialize(db_hostname="localhost")
mc = MemCache("127.0.0.1")

SOURCE_MAP = {1:'AMAZON',2:'FLIPKART',3:'SNAPDEAL',4:'SAHOLIC',5:"SHOPCLUES.COM",6:"PAYTM.COM"}

COLLECTION_MAP = {
                  'ExceptionalNlc':'skuBundleId',
                  'SkuDealerPrices':'skuBundleId',
                  'SkuDiscountInfo':'skuBundleId',
                  'SkuSchemeDetails':'skuBundleId',
                  'DealPoints':'skuBundleId'
                  }

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

def populateCashBack():
    print "Populating cashback"
    cashBackMap = {}
    itemCashBackMap = {}
    cashBack = list(get_mongo_connection().Catalog.CategoryCashBack.find())
    for row in cashBack:
        temp_map = {}
        temp_list = []
        if cashBackMap.has_key(row['source_id']):
            arr = cashBackMap.get(row['source_id'])
            for val in arr:
                temp_list.append(val)
            temp_map[row['category_id']] = row
            temp_list.append(temp_map)
            cashBackMap[row['source_id']] = temp_list 
        else:
            temp_map[row['category_id']] = row
            temp_list.append(temp_map)
            cashBackMap[row['source_id']] = temp_list
    itemCashBack = list(get_mongo_connection().Catalog.ItemCashBack.find())
    for row in itemCashBack:
        if not itemCashBackMap.has_key(row['skuId']):
            itemCashBackMap[row['skuId']] = row
    mc.set("item_cash_back", itemCashBackMap, 24 * 60 * 60)
    mc.set("category_cash_back", cashBackMap, 24 * 60 * 60)

def addCategoryDiscount(data):
    collection = get_mongo_connection().Catalog.CategoryDiscount
    query = []
    data['brand'] = data['brand'].strip().upper()
    data['discountType'] = data['discountType'].upper().strip()
    query.append({"brand":data['brand']})
    query.append({"category_id":data['category_id']})
    r = collection.find({"$and":query})
    if r.count() > 0:
        return {0:"Brand & Category info already present."}
    else:
        collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'brand':data['brand'],'category_id':data['category_id']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}

def updateCategoryDiscount(data,_id):
    try:
        collection = get_mongo_connection().Catalog.CategoryDiscount
        collection.update({'_id':ObjectId(_id)},{"$set":{'min_discount':data['min_discount'],'max_discount':data['max_discount'],'discountType':data['discountType'].upper().strip()}},upsert=False, multi = False)
        get_mongo_connection().Catalog.MasterData.update({'brand':data['brand'],'category_id':data['category_id']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data updated successfully"}
    except:
        return {0:"Data not updated."}
    

def getAllCategoryDiscount():
    data = []
    collection = get_mongo_connection().Catalog.CategoryDiscount
    cursor = collection.find()
    for val in cursor:
        data.append(val)
    return data

def __getBundledSkusfromSku(sku):
    masterData =  get_mongo_connection().Catalog.MasterData.find_one({"_id":sku},{"skuBundleId":1})
    if masterData is not None:
        return list(get_mongo_connection().Catalog.MasterData.find({"skuBundleId":masterData.get('skuBundleId')},{'_id':1,'skuBundleId':1}))
    else:
        return []

def addSchemeDetailsForSku(data):
    collection = get_mongo_connection().Catalog.SkuSchemeDetails
    result = collection.find_one({'skuBundleId':data['skuBundleId']})
    if result is None:
        collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}
    return {1:"BundleId info already present"}
    
def getAllSkuWiseSchemeDetails(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.SkuSchemeDetails
    cursor = collection.find().skip(offset).limit(limit)
    for val in cursor:
        master = get_mongo_connection().Catalog.MasterData.find_one({'skuBundleId':val['skuBundleId']})
        if master is not None:
            val['brand'] = master['brand']
            val['source_product_name'] = master['source_product_name']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
        data.append(val)
    return data

def addSkuDiscountInfo(data):
    collection = get_mongo_connection().Catalog.SkuDiscountInfo
    cursor = collection.find_one({"skuBundleId":data['skuBundleId']})
    if cursor is not None:
        return {0:"BundleId information already present."}
    else:
        collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}

def getallSkuDiscountInfo(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.SkuDiscountInfo
    cursor = collection.find().skip(offset).limit(limit)
    for val in cursor:
        master = get_mongo_connection().Catalog.MasterData.find_one({'skuBundleId':val['skuBundleId']})
        if master is not None:
            val['brand'] = master['brand']
            val['source_product_name'] = master['source_product_name']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
        data.append(val)
    return data

def updateSkuDiscount(data,_id):
    try:
        collection = get_mongo_connection().Catalog.SkuDiscountInfo
        collection.update({'_id':ObjectId(_id)},{"$set":{'min_discount':data['min_discount'],'max_discount':data['max_discount'],'discountType':data['discountType'].upper().strip()}},upsert=False, multi = False)
        get_mongo_connection().Catalog.MasterData.update({'_id':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data updated successfully"}
    except:
        return {0:"Data not updated."}


def addExceptionalNlc(data):
    collection = get_mongo_connection().Catalog.ExceptionalNlc
    cursor = collection.find_one({"skuBundleId":data['skuBundleId']})
    if cursor is not None:
        return {0:"BundleId information already present."}
    else:
        collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}

def getAllExceptionlNlcItems(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.ExceptionalNlc
    cursor = collection.find().skip(offset).limit(limit)
    for val in cursor:
        master = get_mongo_connection().Catalog.MasterData.find_one({'skuBundleId':val['skuBundleId']})
        if master is not None:
            val['brand'] = master['brand']
            val['source_product_name'] = master['source_product_name']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
        data.append(val)
    return data

def getMerchantOrdersByUser(userId, page=1, window=50, searchMap={}):
    if searchMap is None:
        searchMap = {}
    if page==None:
        page = 1
        
    if window==None:
        window = 50
    result = {}
    skip = (page-1)*window
    
    if userId is not None:
        searchMap['userId'] = userId
    collection = get_mongo_connection().Dtr.merchantOrder
    cursor = collection.find(searchMap).sort("orderId",-1)
    total_count = cursor.count()
    pages = total_count/window + (0 if total_count%window==0 else 1)  
    print "total_count", total_count
    if total_count > skip:
        cursor = cursor.skip(skip).limit(window)
        orders = []
        for order in cursor:
            del(order["_id"])
            orders.append(order)
        result['data'] = orders
        result['window'] = window
        result['totalCount'] = total_count 
        result['currCount'] = cursor.count()
        result['totalPages'] = pages
        result['currPage'] = page    
        return result
    else:
        return result

def getRefunds(userId, page=1, window=10):
    if page==None:
        page = 1
        
    if window==None:
        window = 10
    result = {}
    skip = (page-1)*window
    collection = get_mongo_connection().Dtr.refund
    cursor = collection.find({"userId":userId})
    total_count = cursor.count()
    pages = total_count/window + (0 if total_count%window==0 else 1)  
    print "total_count", total_count
    if total_count > skip:
        cursor = cursor.skip(skip).limit(window).sort([('batch',-1)])
        refunds = []
        for refund in cursor:
            del(refund["_id"])
            refunds.append(refund)
        result['data'] = refunds
        result['window'] = window
        result['totalCount'] = total_count 
        result['currCount'] = cursor.count()
        result['totalPages'] = pages
        result['currPage'] = page    
        return result
    else:
        return result
    
def getPendingRefunds(userId):
    print type(userId)
    result = get_mongo_connection().Dtr.merchantOrder\
        .aggregate([
                    {'$match':{'subOrders.cashBackStatus':CB_APPROVED, 'userId':userId}},
                    {'$unwind':"$subOrders"},
                    {'$match':{'subOrders.cashBackStatus':CB_APPROVED}},
                    { 
                     '$group':{
                               '_id':None,
                               'amount': { '$sum':'$subOrders.cashBackAmount'},
                               }
                     }
                ])['result']

    if len(result)>0:
        result = result[0]        
        result.pop("_id")
    else:
        result={}
        result['amount'] = 0.0
    result['nextCredit'] = datetime.strftime(next_weekday(datetime.now(), int(PythonPropertyReader.getConfig('CREDIT_DAY_OF_WEEK'))),"%Y-%m-%d %H:%M:%S")
    return result

def getPendingCashbacks(userId):
    result = get_mongo_connection().Dtr.merchantOrder\
        .aggregate([
                    {'$match':{'subOrders.cashBackStatus':CB_PENDING, 'userId':userId}},
                    {'$unwind':"$subOrders"},
                    {'$match':{'subOrders.cashBackStatus':CB_PENDING}},
                    { 
                     '$group':{
                               '_id':None,
                               'amount': { '$sum':'$subOrders.cashBackAmount'},
                               }
                     }
                ])['result']

    if len(result)>0:
        result = result[0]        
        result.pop("_id")
    else:
        result={}
        result['amount'] = 0.0
    return result

def __populateCache(userId):
    print "Populating memcache for userId",userId
    outer_query = []
    outer_query.append({ "$or": [ { "showDeal": 1} , { "prepaidDeal": 1 } ] })
    query = {}
    query['$gt'] = -100
    outer_query.append({'totalPoints':query})
    brandPrefMap = {}
    pricePrefMap = {}
    actionsMap = {}
    brand_p = session.query(price_preferences).filter_by(user_id=userId).all()
    for x in brand_p:
        pricePrefMap[x.category_id] = [x.min_price,x.max_price]
    for x in session.query(brand_preferences).filter_by(user_id=userId).all():
        temp_map = {}
        if brandPrefMap.has_key((x.brand).strip().upper()):
            val = brandPrefMap.get((x.brand).strip().upper())
            temp_map[x.category_id] = 1 if x.status == 'show' else 0
            val.append(temp_map)
        else:
            temp = []
            temp_map[x.category_id] = 1 if x.status == 'show' else 0
            temp.append(temp_map)
            brandPrefMap[(x.brand).strip().upper()] = temp
        
    for x in session.query(user_actions).filter_by(user_id=userId).all():
        actionsMap[x.store_product_id] = 1 if x.action == 'like' else 0
    all_deals = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query},{'_id':1,'category_id':1,'brand':1,'totalPoints':1,'bestSellerPoints':1,'nlcPoints':1,'rank':1,'available_price':1,'dealType':1,'source_id':1,'brand_id':1,'skuBundleId':1,'dp':1, 'showDp':1}).sort([('totalPoints',pymongo.DESCENDING),('bestSellerPoints',pymongo.DESCENDING),('nlcPoints',pymongo.DESCENDING),('rank',pymongo.DESCENDING)]))
    all_category_deals = []
    mobile_deals = []
    tablet_deals = []
    for deal in all_deals:
        if actionsMap.get(deal['_id']) == 0:
            fav_weight =.25
        elif actionsMap.get(deal['_id']) == 1:
            fav_weight = 1.5
        else:
            fav_weight = 1
        
        if brandPrefMap.get(deal['brand'].strip().upper()) is not None:
            brand_weight = 1
            for brandInfo in brandPrefMap.get(deal['brand'].strip().upper()):
                if brandInfo.get(deal['category_id']) is not None:
                    if brandInfo.get(deal['category_id']) == 1:
                        brand_weight = 2.0
        else:
            brand_weight = 1
        
        if pricePrefMap.get(deal['category_id']) is not None:
        
            if deal['available_price'] >= pricePrefMap.get(deal['category_id'])[0] and deal['available_price'] <= pricePrefMap.get(deal['category_id'])[1]:
                asp_weight = 1.5
            elif  deal['available_price'] >= pricePrefMap.get(deal['category_id'])[0] - 0.5 * pricePrefMap.get(deal['category_id'])[0] and deal['available_price'] <= pricePrefMap.get(deal['category_id'])[1] + 0.5 * pricePrefMap.get(deal['category_id'])[1]:
                asp_weight = 1.2
            else:
                asp_weight = 1
        else:
            asp_weight = 1
        
        persPoints = deal['totalPoints'] * fav_weight * brand_weight * asp_weight
        deal['persPoints'] = persPoints
        
        if deal['category_id'] ==3:
            mobile_deals.append(deal)
        elif deal['category_id'] ==5:
            tablet_deals.append(deal)
        else:
            continue
        all_category_deals.append(deal)
    
    session.close()
    mem_cache_val = {3:mobile_deals, 5:tablet_deals, 0:all_deals}
    mc.set(str(userId), mem_cache_val)
    

def __populateFeaturedDeals():
    all_category_fd = []
    mobile_fd = []
    tablet_fd = []
    activeFeaturedDeals = get_mongo_connection().Catalog.FeaturedDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}}).sort({'rank':pymongo.ASCENDING})
    for activeFeaturedDeal in activeFeaturedDeals:
        for k,v in activeFeaturedDeal['rankDetails']:
            featuredDeal = FeaturedDeals(activeFeaturedDeal['sku'], int(k), activeFeaturedDeal['thresholdPrice'], int(v))
            if featuredDeal.category_id == 0:
                all_category_fd.append(featuredDeal)
            elif featuredDeal.category_id == 3:
                mobile_fd.append(featuredDeal)
            elif featuredDeal.category_id == 5:
                tablet_fd.append(featuredDeal)
            else:
                continue
    mc.set("featured_deals_category_"+str(0), all_category_fd, 3600)
    mc.set("featured_deals_category_"+str(3), mobile_fd, 3600)
    mc.set("featured_deals_category_"+str(5), tablet_fd, 3600)

def getNewDeals(userId, category_id, offset, limit, sort, direction, filterData=None):
    if not bool(mc.get("category_cash_back")):
        populateCashBack()
    
    try:
        if mc.get("featured_deals_category_"+str(category_id)) is None:
            __populateFeaturedDeals()
    except:
        pass 
    
    rank = 1
    dealsListMap = []
    user_specific_deals = mc.get(str(userId))
    if user_specific_deals is None:
        __populateCache(userId)
        user_specific_deals = mc.get(str(userId))
    else:
        print "Getting user deals from cache"
    category_specific_deals = user_specific_deals.get(category_id)
    
    if sort is None or direction is None:
        sorted_deals = sorted(category_specific_deals, key = lambda x: (x['persPoints'],x['totalPoints'],x['bestSellerPoints'], x['nlcPoints'], x['rank']),reverse=True)
    else:
        if sort == "bestSellerPoints":
            sorted_deals = sorted(category_specific_deals, key = lambda x: (x['bestSellerPoints'], x['rank'], x['nlcPoints']),reverse=True)
        else:
            if direction == -1:
                rev = True
            else:
                rev = False
            sorted_deals = sorted(category_specific_deals, key = lambda x: (x['available_price']),reverse=rev)
    
    
    print "============================"
    if filterData is not None:
        try:
            sorted_deals = filterDeals(sorted_deals, filterData)
        except:
            traceback.print_exc()
    
    
    sortedMap = {}
    rankMap = {}
    rank = 0
    for sorted_deal in sorted_deals:
        if sortedMap.get(sorted_deal['skuBundleId']) is None:
            sortedMap[sorted_deal['skuBundleId']] = {rank:[sorted_deal]}
            rankMap[rank] = (sortedMap[sorted_deal['skuBundleId']].values())[0]
            rank = rank +1
        else:
            for temp_list in sortedMap.get(sorted_deal['skuBundleId']).itervalues():
                temp_list.append(sorted_deal)
            rankMap[(sortedMap.get(sorted_deal['skuBundleId']).keys())[0]] = temp_list
                
    for dealList in [rankMap.get(k, []) for k in range(offset, offset+limit)]:
        temp = []
        for d in dealList:
            item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
            if len(item) ==0:
                continue
            item[0]['persPoints'] = d['persPoints']
            if d['dealType'] == 1 and d['source_id'] ==1:
                try:
                    manualDeal = list(get_mongo_connection().Catalog.ManualDeals.find({'sku':d['_id'],'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}}))
                    item[0]['marketPlaceUrl'] = manualDeal[0]['dealUrl'].strip()
                except:
                    pass
            elif d['source_id'] ==3:
                item[0]['marketPlaceUrl'] = item[0]['marketPlaceUrl']+'?supc='+item[0].get('identifier')
            else:
                pass 
            try:
                cashBack = getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
                if not cashBack or cashBack.get('cash_back_status')!=1:
                    item[0]['cash_back_type'] = 0
                    item[0]['cash_back'] = 0
                else:
                    item[0]['cash_back_type'] = int(cashBack['cash_back_type'])
                    item[0]['cash_back'] = cashBack['cash_back']
            except:
                print "Error in adding cashback to deals"
                item[0]['cash_back_type'] = 0
                item[0]['cash_back'] = 0
            try:
                item[0]['dp'] = d['dp']
                item[0]['showDp'] = d['showDp']
            except:
                item[0]['dp'] = 0.0
                item[0]['showDp'] = 0
            try:
                item[0]['thumbnail'] = item[0]['thumbnail'].strip()
            except:
                pass 
            temp.append(item[0])
        if len(temp) > 1:
            temp = sorted(temp, key = lambda x: (x['available_price']),reverse=False)
        dealsListMap.append(temp)
    return dealsListMap

def filterDeals(deals, filterData):
    dealFiltered = []
    brandsFiltered = []
    filterArray = filterData.split('|')
    for data in filterArray:
        try:
            filter, info = data.split(':')
        except Exception as ex:
            traceback.print_exc()
            continue
        if filter == 'dealFilter':
            toFilter = info.split('^')
            print "deal filter ",toFilter
            if 'deals' in toFilter:
                dealFiltered = deals
                continue
            for filterVal in toFilter:
                if filterVal == 'dod':
                    for deal in deals:
                        if deal['dealType'] == 1:
                            dealFiltered.append(deal)
        elif filter == 'brandFilter':
            toFilter = info.split('^')
            print "brand filter ",toFilter
            if len(toFilter) == 0 or (len(toFilter)==1 and toFilter[0]==''):
                brandsFiltered = deals
            for deal in deals:
                if str(int(deal['brand_id'])) in toFilter:
                    brandsFiltered.append(deal)
    if len(dealFiltered) == 0:
        return brandsFiltered
    return [i for i in dealFiltered for j in brandsFiltered if i['_id']==j['_id']]
    

def getDeals(userId, category_id, offset, limit, sort, direction):
    if not bool(mc.get("category_cash_back")):
        populateCashBack()
    rank = 1
    deals = {}
    outer_query = []
    outer_query.append({"showDeal":1})
    query = {}
    query['$gt'] = 0
    outer_query.append({'totalPoints':query})
    if category_id in (3,5):
        outer_query.append({'category_id':category_id})
    if sort is None or direction is None:
        direct = -1
        print outer_query
        data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('totalPoints',direct),('bestSellerPoints',direct),('nlcPoints',direct),('rank',direct)]).skip(offset).limit(limit))
    else:
        print outer_query
        direct = direction
        if sort == "bestSellerPoints":
            print "yes,sorting by bestSellerPoints"
            data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('bestSellerPoints',direct),('rank',direct),('nlcPoints',direct)]).skip(offset).limit(limit))
        else:
            data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([(sort,direct)]).skip(offset).limit(limit))
    for d in data:
        item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
        if not deals.has_key(item[0]['identifier']):
            item[0]['dealRank'] = rank
            try:
                cashBack = getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
                if not cashBack or cashBack.get('cash_back_status')!=1:
                    item[0]['cash_back_type'] = 0
                    item[0]['cash_back'] = 0
                else:
                    item[0]['cash_back_type'] = int(cashBack['cash_back_type'])
                    item[0]['cash_back'] = cashBack['cash_back']
            except:
                print "Error in adding cashback to deals"
                item[0]['cash_back_type'] = 0
                item[0]['cash_back'] = 0
            deals[item[0]['identifier']] = item[0]
            
            rank +=1
    return sorted(deals.values(), key=itemgetter('dealRank'))

def getItem(skuId,showDp=None):
    temp = []
    if not bool(mc.get("category_cash_back")):
        populateCashBack()
    try:
        skuData = get_mongo_connection().Catalog.MasterData.find_one({'_id':int(skuId)})
        if skuData is None:
            raise
        try:
            cashBack = getCashBack(skuData['_id'], skuData['source_id'], skuData['category_id'])
            if not cashBack or cashBack.get('cash_back_status')!=1:
                skuData['cash_back_type'] = 0
                skuData['cash_back'] = 0
            else:
                skuData['cash_back_type'] = cashBack['cash_back_type']
                skuData['cash_back'] = cashBack['cash_back']
        except:
            print "Error in adding cashback to deals"
            skuData['cash_back_type'] = 0
            skuData['cash_back'] = 0
        skuData['in_stock'] = int(skuData['in_stock'])
        skuData['is_shortage'] = int(skuData['is_shortage'])
        skuData['category_id'] = int(skuData['category_id'])
        skuData['status'] = int(skuData['status'])
        try:
            skuData['thumbnail'] = skuData['thumbnail'].strip()
        except:
            pass 
        if showDp is not None:
            dealerPrice = get_mongo_connection().Catalog.SkuDealerPrices.find_one({'skuBundleId':skuData['skuBundleId']})
            skuData['dp'] = 0.0
            skuData['showDp'] = 0
            if dealerPrice is not None:
                skuData['dp'] = dealerPrice['dp']
                skuData['showDp'] = dealerPrice['showDp']
        temp.append(skuData)
        return temp
    except:
        return []

def getCashBack(skuId, source_id, category_id):
    if not bool(mc.get("category_cash_back")):
        populateCashBack()
    itemCashBackMap = mc.get("item_cash_back")
    itemCashBack = itemCashBackMap.get(skuId)
    if itemCashBack is not None:
        return itemCashBack
    cashBackMap = mc.get("category_cash_back")
    sourceCashBack = cashBackMap.get(source_id)
    if sourceCashBack is not None and len(sourceCashBack) > 0:
        for cashBack in sourceCashBack:
            if cashBack.get(category_id) is None:
                continue
            else:
                return cashBack.get(category_id)
    else:
        return {}
    
def getBundleBySourceSku(source_id, identifier):
    if source_id in (1,2,4,5):
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
    elif source_id == 3:
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
    else:
        return {}
    
    if not skuData:
        return {}
    else:
        bundleId = skuData[0]["skuBundleId"]
        itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bundleId, 'in_stock':1, 'source_id':{"$in":[1,2,3,5,6]}}, 
                       {"source_id":1, "available_price":1, "marketPlaceUrl":1, "gross_price":1, "codAvailable":1, "source_product_name":1, "offer":1, "coupon":1}))
        return {'products':itemIds}
        
    

def getDealRank(identifier, source_id, userId):
    if source_id in (1,2,4,5,6):
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
    elif source_id == 3:
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
    else:
        return {'rank':0, 'description':'Source not valid','maxNlc':None, 'minNlc':None,'status':None,'dp':None}
    if len(skuData) == 0:
        return {'rank':0, 'description':'No matching product identifier found','maxNlc':None, 'minNlc':None,'status':None,'dp':None}
    user_specific_deals = mc.get(str(userId))
    if user_specific_deals is None:
        __populateCache(userId)
        user_specific_deals = mc.get(str(userId))
    else:
        print "Getting user deals from cache"
    category_id = skuData[0]['category_id']
    category_specific_deals = user_specific_deals.get(category_id)
    
    dealsData = get_mongo_connection().Catalog.Deals.find_one({'skuBundleId':skuData[0]['skuBundleId']})
    if dealsData is None:
        dealsData = {}
    
    if category_specific_deals is None or len(category_specific_deals) ==0:
        return {'rank':0,'description':'Category specific deals is empty','maxNlc':dealsData.get('maxNlc'),'minNlc':dealsData.get('minNlc'),'status':dealsData.get('status'),'dp':dealsData.get('dp')}
    sorted_deals = sorted(category_specific_deals, key = lambda x: (x['persPoints'],x['totalPoints'],x['bestSellerPoints'], x['nlcPoints'], x['rank']),reverse=True)
    sortedMap = {}
    rankMap = {}
    rank = 0
    for sorted_deal in sorted_deals:
        if sortedMap.get(sorted_deal['skuBundleId']) is None:
            sortedMap[sorted_deal['skuBundleId']] = {rank:[sorted_deal]}
            rankMap[rank] = (sortedMap[sorted_deal['skuBundleId']].values())[0]
            rank = rank +1
        else:
            for temp_list in sortedMap.get(sorted_deal['skuBundleId']).itervalues():
                temp_list.append(sorted_deal)
            rankMap[(sortedMap.get(sorted_deal['skuBundleId']).keys())[0]] = temp_list
                
    for dealRank ,dealList in rankMap.iteritems():
        for d in dealList:
            if d['skuBundleId'] == skuData[0]['skuBundleId']:
                return {'rank':dealRank+1,'description':'Rank found','maxNlc':dealsData.get('maxNlc'),'minNlc':dealsData.get('minNlc'),'status':dealsData.get('status'),'dp':dealsData.get('dp')}
    
    return {'rank':0,'description':'Rank not found','maxNlc':dealsData.get('maxNlc'),'minNlc':dealsData.get('minNlc'),'status':dealsData.get('status'),'dp':dealsData.get('dp')}
    

def getCashBackDetails(identifier, source_id):
    if not bool(mc.get("category_cash_back")):
        populateCashBack()
    
    if source_id in (1,2,4,5,6):
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
    elif source_id == 3:
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
    else:
        return {}
    if len(skuData) > 0:
        itemCashBackMap = mc.get("item_cash_back")
        itemCashBack = itemCashBackMap.get(skuData[0]['_id'])
        if itemCashBack is not None:
            return itemCashBack
        cashBackMap = mc.get("category_cash_back")
        sourceCashBack = cashBackMap.get(source_id)
        if sourceCashBack is not None and len(sourceCashBack) > 0:
            for cashBack in sourceCashBack:
                if cashBack.get(skuData[0]['category_id']) is None:
                    continue
                else:
                    return cashBack.get(skuData[0]['category_id'])
        else:
            return {} 
    else:
        return {}
    
def getImgSrc(identifier, source_id):
    skuData = None
    if source_id in (1,2,4,5,6):
        skuData = get_mongo_connection().Catalog.MasterData.find_one({'identifier':identifier.strip(), 'source_id':source_id})
    elif source_id == 3:
        skuData = get_mongo_connection().Catalog.MasterData.find_one({'secondaryIdentifier':identifier.strip(), 'source_id':source_id})
    if skuData is None:
        return {}
    else:
        try:
            return {'thumbnail':skuData.get('thumbnail').strip()}
        except:
            return {'thumbnail':skuData.get('thumbnail')}
    
def next_weekday(d, weekday):
    days_ahead = weekday - d.weekday()
    if days_ahead <= 0: # Target day already happened this week
        days_ahead += 7
    return d + timedelta(days_ahead)
    

def getAllDealerPrices(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.SkuDealerPrices
    cursor = collection.find().skip(offset).limit(limit)
    for val in cursor:
        master = get_mongo_connection().Catalog.MasterData.find_one({'skuBundleId':val['skuBundleId']})
        if master is not None:
            val['brand'] = master['brand']
            val['source_product_name'] = master['source_product_name']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
        val['showDp'] = int(val['showDp'])
        data.append(val)
    return data

def addSkuDealerPrice(data):
    collection = get_mongo_connection().Catalog.SkuDealerPrices
    cursor = collection.find_one({"skuBundleId":data['skuBundleId']})
    if cursor is not None:
        return {0:"BundleId information already present."}
    else:
        collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}

def updateSkuDealerPrice(data, _id):
    try:
        collection = get_mongo_connection().Catalog.SkuDealerPrices
        collection.update({'_id':ObjectId(_id)},{"$set":{'dp':data['dp']}},upsert=False, multi = False)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data updated successfully"}
    except:
        return {0:"Data not updated."}

def updateExceptionalNlc(data, _id):
    try:
        collection = get_mongo_connection().Catalog.ExceptionalNlc
        collection.update({'_id':ObjectId(_id)},{"$set":{'maxNlc':data['maxNlc'], 'minNlc':data['minNlc'], 'overrideNlc':data['overrideNlc']}},upsert=False, multi = False)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data updated successfully"}
    except:
        return {0:"Data not updated."}

def resetCache(userId):
    try:
        mc.delete(userId)
        return {1:'Cache cleared.'}
    except:
        return {0:'Unable to clear cache.'}
    
def updateCollection(data):
    print data
    try:
        collection = get_mongo_connection().Catalog[data['class']]
        class_name = data.pop('class')
        _id = data.pop('oid')
        result = collection.update({'_id':ObjectId(_id)},{"$set":data},upsert=False, multi = False)
        if class_name != "Notifications":
            record = list(collection.find({'_id':ObjectId(_id)}))
            if class_name !="CategoryDiscount":
                if record[0].has_key('sku'):
                    field = '_id'
                    val = record[0]['sku']
                else:
                    field = 'skuBundleId'
                    val = record[0]['skuBundleId']
                get_mongo_connection().Catalog.MasterData.update({field:val},{"$set":{'updatedOn':to_java_date(datetime.now())}},upsert=False, multi = True)
            else:
                get_mongo_connection().Catalog.MasterData.update({'brand':re.compile(record[0]['brand'], re.IGNORECASE),'category_id':record[0]['category_id']}, \
                                                                 {"$set":{'updatedOn':to_java_date(datetime.now())}},upsert=False,multi=True)
        if class_name =='SkuDealerPrices':
            get_mongo_connection().Catalog.Deals.update({'skuBundleId':val},{"$set":data},upsert=False, multi=True)
        return {1:"Data updated successfully"}
    except Exception as e:
        print e
        return {0:"Data not updated."}
        
    
def addNegativeDeals(data, multi):
    if multi !=1: 
        collection = get_mongo_connection().Catalog.NegativeDeals
        cursor = collection.find({"sku":data['sku']})
        if cursor.count() > 0:
            return {0:"Sku information already present."}
        else:
            collection.insert(data)
            return {1:"Data added successfully"}
    else:
        skuIds = __getBundledSkusfromSku(data['sku'])
        for sku in skuIds:
            data['sku'] = sku.get('_id')
            collection = get_mongo_connection().Catalog.NegativeDeals
            cursor = collection.find({"sku":data['sku']})
            if cursor.count() > 0:
                continue
            else:
                data.pop('_id',None)
                collection.insert(data)
        return {1:"Data added successfully"}

def getAllNegativeDeals(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.NegativeDeals
    cursor = collection.find().skip(offset).limit(limit)
    for val in cursor:
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
        if len(master) > 0:
            val['brand'] = master[0]['brand']
            val['source_product_name'] = master[0]['source_product_name']
            val['skuBundleId'] = master[0]['skuBundleId']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
            val['skuBundleId'] = ""
        data.append(val)
    return data

def getAllManualDeals(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.ManualDeals
    cursor = collection.find().skip(offset).limit(limit)
    for val in cursor:
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
        if len(master) > 0:
            val['brand'] = master[0]['brand']
            val['source_product_name'] = master[0]['source_product_name']
            val['skuBundleId'] = master[0]['skuBundleId']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
            val['skuBundleId'] = ""
        data.append(val)
    return data
    
def addManualDeal(data, multi):
    if multi !=1:
        collection = get_mongo_connection().Catalog.ManualDeals
        cursor = collection.find({'sku':data['sku']})
        if cursor.count() > 0:
            return {0:"Sku information already present."}
        else:
            collection.insert(data)
            get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
            return {1:"Data added successfully"}
    else:
        skuIds = __getBundledSkusfromSku(data['sku'])
        for sku in skuIds:
            data['sku'] = sku.get('_id')
            collection = get_mongo_connection().Catalog.ManualDeals
            cursor = collection.find({'sku':data['sku']})
            if cursor.count() > 0:
                continue
            else:
                data.pop('_id',None)
                collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}
        
def deleteDocument(data):
    print "inside detete document"
    print data
    try:
        collection = get_mongo_connection().Catalog[data['class']]
        class_name = data.pop('class')
        _id = data.pop('oid')
        record = list(collection.find({'_id':ObjectId(_id)}))
        collection.remove({'_id':ObjectId(_id)})
        if class_name != "Notifications":
            if class_name !="CategoryDiscount":
                print record[0]
                if record[0].has_key('sku'):
                    field = '_id'
                    val = record[0]['sku']
                else:
                    field = 'skuBundleId'
                    val = record[0]['skuBundleId']
                print "Updating master"
                print field
                print val
                get_mongo_connection().Catalog.MasterData.update({field:val},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
            else:
                get_mongo_connection().Catalog.MasterData.update({'brand':re.compile(record[0]['brand'], re.IGNORECASE),'category_id':record[0]['category_id']}, \
                                                                 {"$set":{'updatedOn':to_java_date(datetime.now())}},upsert=False,multi=True)
        return {1:"Document deleted successfully"}
    except Exception as e:
        print e
        return {0:"Document not deleted."}

def searchMaster(offset, limit, search_term):
    data = []
    if search_term is not None:
        terms = search_term.split(' ')
        outer_query = []
        for term in terms:
            outer_query.append({"source_product_name":re.compile(term, re.IGNORECASE)})
        try:
            collection = get_mongo_connection().Catalog.MasterData.find({"$and":outer_query,'source_id':{'$in':SOURCE_MAP.keys()}}).skip(offset).limit(limit)
            for record in collection:
                data.append(record)
        except:
            pass
    else:
        collection = get_mongo_connection().Catalog.MasterData.find({'source_id':{'$in':SOURCE_MAP.keys()}}).skip(offset).limit(limit)
        for record in collection:
            data.append(record)
    return data

def getAllFeaturedDeals(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.FeaturedDeals
    cursor = collection.find({'endDate':{'$gte':to_java_date(datetime.now())}}).skip(offset).limit(limit)
    for val in cursor:
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
        if len(master) > 0:
            val['brand'] = master[0]['brand']
            val['source_product_name'] = master[0]['source_product_name']
            val['skuBundleId'] = master[0]['skuBundleId']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
            val['skuBundleId'] = ""
        data.append(val)
    return data
    
def addFeaturedDeal(data, multi):
    if multi !=1:
        collection = get_mongo_connection().Catalog.FeaturedDeals
        cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
        if cursor.count() > 0:
            return {0:"Sku information already present."}
        else:
            collection.insert(data)
            get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
            return {1:"Data added successfully"}
    else:
        skuIds = __getBundledSkusfromSku(data['sku'])
        for sku in skuIds:
            data['sku'] = sku.get('_id')
            collection = get_mongo_connection().Catalog.FeaturedDeals
            cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
            if cursor.count() > 0:
                continue
            else:
                data.pop('_id',None)
                collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}

def searchCollection(class_name, sku, skuBundleId):
    data = []
    collection = get_mongo_connection().Catalog[class_name]
    if class_name == "Notifications":
        cursor = collection.find({'skuBundleId':skuBundleId})
        for val in cursor:
            master = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':val['skuBundleId']}))
            if len(master) > 0:
                val['brand'] = master[0]['brand']
                val['model_name'] = master[0]['model_name']
                val['skuBundleId'] = master[0]['skuBundleId']
            else:
                val['brand'] = ""
                val['model_name'] = ""
                val['skuBundleId'] = val['skuBundleId']
            data.append(val)
        return data
    master = None
    if sku is not None:
        if COLLECTION_MAP.has_key(class_name):
            master = get_mongo_connection().Catalog.MasterData.find_one({'_id':sku})
            cursor = collection.find({'skuBundleId':master['skuBundleId']})
        else:
            cursor = collection.find({'sku':sku})
        for val in cursor:
            if master is None:
                master = get_mongo_connection().Catalog.MasterData.find_one({'_id':val['sku']})
            if master is not None:
                val['brand'] = master['brand']
                val['source_product_name'] = master['source_product_name']
                val['skuBundleId'] = master['skuBundleId']
            else:
                val['brand'] = ""
                val['source_product_name'] = ""
                val['skuBundleId'] = ""
            data.append(val)
        return data
    else:
        if not COLLECTION_MAP.has_key(class_name):
            skuIds = get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId}).distinct('_id')
            for sku in skuIds:
                cursor = collection.find({'sku':sku})
                for val in cursor:
                    master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
                    if len(master) > 0:
                        val['brand'] = master[0]['brand']
                        val['source_product_name'] = master[0]['source_product_name']
                        val['skuBundleId'] = master[0]['skuBundleId']
                    else:
                        val['brand'] = ""
                        val['source_product_name'] = ""
                        val['skuBundleId'] = ""
                    data.append(val)
            return data
        else:
            cursor = collection.find({'skuBundleId':skuBundleId})
            for val in cursor:
                master = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':val['skuBundleId']}))
                if len(master) > 0:
                    val['brand'] = master[0]['brand']
                    val['source_product_name'] = master[0]['source_product_name']
                else:
                    val['brand'] = ""
                    val['source_product_name'] = ""
                data.append(val)
            return data

def __getBrandIdForBrand(brandName, category_id):
    brandInfo = Brands.query.filter(Brands.category_id==category_id).filter(Brands.name == brandName).all()
    if brandInfo is None or len(brandInfo)!=1:
        raise
    else:
        return brandInfo[0].id

def addNewItem(data):
    try:
        data['updatedOn'] = to_java_date(datetime.now())
        data['addedOn'] = to_java_date(datetime.now())
        data['priceUpdatedOn'] = to_java_date(datetime.now())
        max_id = list(get_mongo_connection().Catalog.MasterData.find().sort([('_id',pymongo.DESCENDING)]).limit(1))
        max_bundle = list(get_mongo_connection().Catalog.MasterData.find().sort([('skuBundleId',pymongo.DESCENDING)]).limit(1))
        data['_id'] = max_id[0]['_id'] + 1
        data['skuBundleId'] = max_bundle[0]['skuBundleId'] + 1
        data['identifier'] = str(data['identifier'])
        data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
        data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
        get_mongo_connection().Catalog.MasterData.insert(data)
        return {1:'Data added successfully'}
    except Exception as e:
        print e
        return {0:'Unable to add data.'}
    finally:
        session.close()
    
def addItemToExistingBundle(data):
    try:
        data['updatedOn'] = to_java_date(datetime.now())
        data['addedOn'] = to_java_date(datetime.now())
        data['priceUpdatedOn'] = to_java_date(datetime.now())
        max_id = list(get_mongo_connection().Catalog.MasterData.find().sort([('_id',pymongo.DESCENDING)]).limit(1))
        data['_id'] = max_id[0]['_id'] + 1
        data['identifier'] = str(data['identifier'])
        data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
        data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
        get_mongo_connection().Catalog.MasterData.insert(data)
        return {1:'Data added successfully.'}
    except Exception as e:
        print e
        return {0:'Unable to add data.'}
    finally:
        session.close()

def updateMaster(data, multi):
    try:
        print data
        if multi != 1:
            _id = data.pop('_id')
            skuBundleId = data.pop('skuBundleId')
            data['updatedOn'] = to_java_date(datetime.now())
            data['identifier'] = str(data['identifier'])
            data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
            data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
            get_mongo_connection().Catalog.MasterData.update({'_id':_id},{"$set":data},upsert=False)
            return {1:'Data updated successfully.'}
        else:
            _id = data.pop('_id')
            skuBundleId = data.pop('skuBundleId')
            data['updatedOn'] = to_java_date(datetime.now())
            data['identifier'] = str(data['identifier'])
            data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
            data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
            get_mongo_connection().Catalog.MasterData.update({'_id':_id},{"$set":data},upsert=False)
            similarItems = get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId})
            for item in similarItems:
                if item['_id'] == _id:
                    continue
                item['updatedOn'] = to_java_date(datetime.now())
                item['thumbnail'] = data['thumbnail']
                item['category'] = data['category']
                item['category_id'] = data['category_id']
                item['tagline'] = data['tagline']
                item['is_shortage'] = data['is_shortage']
                item['mrp'] = data['mrp']
                item['status'] = data['status']
                item['maxPrice'] = data['maxPrice']
                item['brand_id'] = data['brand_id']
                similar_item_id = item.pop('_id')
                get_mongo_connection().Catalog.MasterData.update({'_id':similar_item_id},{"$set":item},upsert=False)
            return {1:'Data updated successfully.'}
    finally:
        session.close()

def getLiveCricScore():
    return mc.get('liveScore')

def addBundleToNotification(data):
    try:
        collection = get_mongo_connection().Catalog.Notifications
        cursor = collection.find({'skuBundleId':data['skuBundleId']})
        if cursor.count() > 0:
            return {0:"SkuBundleId information already present."}
        else:
            collection.insert(data)
            return {1:'Data updated successfully.'}
    except:
        return {0:'Unable to add data.'}

def getAllNotifications(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.Notifications
    cursor = collection.find().sort([('endDate',pymongo.DESCENDING)]).skip(offset).limit(limit)
    for val in cursor:
        master = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':val['skuBundleId']}))
        if len(master) > 0:
            val['brand'] = master[0]['brand']
            val['model_name'] = master[0]['model_name']
            val['skuBundleId'] = master[0]['skuBundleId']
        else:
            val['brand'] = ""
            val['model_name'] = ""
            val['skuBundleId'] = val['skuBundleId']
        data.append(val)
    return data

def getBrandsForFilter(category_id):
    if mc.get("brandFilter") is None:
        print "Populating brand data for category_id %d" %(category_id)
        tabData, mobData = [], []
        mobileDeals = get_mongo_connection().Catalog.Deals.aggregate([
                                                                      {"$match":{"category_id":3,"showDeal":1,"totalPoints":{"$gt":-100}}
                                                                    },
                                                                 {"$group" : 
                                                                  {'_id':{'brand_id':'$brand_id','brand':'$brand'},'count':{'$sum':1}}
                                                                  }
                                                                ])
        
        tabletDeals = get_mongo_connection().Catalog.Deals.aggregate([
                                                                      {"$match":{"category_id":5,"showDeal":1,"totalPoints":{"$gt":-100}}
                                                                    },
                                                                 {"$group" : 
                                                                  {'_id':{'brand_id':'$brand_id','brand':'$brand'},'count':{'$sum':1}}
                                                                  }
                                                                ])
        
        allDeals = get_mongo_connection().Catalog.Deals.aggregate([
                                                                   {"$match":{"showDeal":1,"totalPoints":{"$gt":-100}}
                                                                    },
                                                                 {"$group" : 
                                                                  {'_id':{'brand_id':'$brand_id','brand':'$brand'},'count':{'$sum':1}}
                                                                  }
                                                                ])
        #print mobileDeals
        #print "==========Mobile data ends=========="
        
        #print tabletDeals
        #print "==========Tablet data ends=========="
        
        #print allDeals
        #print "==========All deal data ends========="
        
        for mobileDeal in mobileDeals['result']:
            if mobileDeal.get('_id').get('brand_id') != 0:
                tempMap = {}
                tempMap['brand'] = mobileDeal.get('_id').get('brand')
                tempMap['brand_id'] = mobileDeal.get('_id').get('brand_id')
                tempMap['count'] = mobileDeal.get('count')
                mobData.append(tempMap)
        
        for tabletDeal in tabletDeals['result']:
            if tabletDeal.get('_id').get('brand_id') != 0:
                tempMap = {}
                tempMap['brand'] = tabletDeal.get('_id').get('brand')
                tempMap['brand_id'] = tabletDeal.get('_id').get('brand_id')
                tempMap['count'] = tabletDeal.get('count')
                tabData.append(tempMap)
        
        
        brandMap = {}
        for allDeal in allDeals['result']:
            if allDeal.get('_id').get('brand_id') != 0:
                if brandMap.has_key(allDeal.get('_id').get('brand')):
                    brand_ids = brandMap.get(allDeal.get('_id').get('brand')).get('brand_ids')
                    brand_ids.append(allDeal.get('_id').get('brand_id'))
                    brandMap[allDeal.get('_id').get('brand')] = {'brand_ids':brand_ids,'count':brandMap.get(allDeal.get('_id').get('brand')).get('count') + allDeal.get('count')}
                else:
                    temp = []
                    temp.append(allDeal.get('_id').get('brand_id'))
                    brandMap[allDeal.get('_id').get('brand')] = {'brand_ids':temp,'count':allDeal.get('count')}
      
        mc.set("brandFilter",{0:brandMap, 3:mobData, 5:tabData}, 600)  
    
    return sorted(mc.get("brandFilter").get(category_id), key = lambda x: (-x['count'], x['brand']))

def getStaticDeals(offset, limit, category_id, direction):
    user_specific_deals = mc.get("staticDeals")
    if user_specific_deals is None:
        __populateStaticDeals()
        user_specific_deals = mc.get("staticDeals")
    rev = False
    if direction is None or direction == -1:
        rev=True
    return sorted((user_specific_deals.get(category_id))[offset:offset+limit],reverse=rev)

def __populateStaticDeals():
    print "Populating memcache for static deals"
    outer_query = []
    outer_query.append({"showDeal":1})
    query = {}
    query['$gt'] = -100
    outer_query.append({'totalPoints':query})
    all_deals = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query},{'_id':1,'category_id':1,'brand':1,'totalPoints':1,'bestSellerPoints':1,'nlcPoints':1,'rank':1,'available_price':1,'dealType':1,'source_id':1,'brand_id':1,'skuBundleId':1}).sort([('totalPoints',pymongo.DESCENDING),('bestSellerPoints',pymongo.DESCENDING),('nlcPoints',pymongo.DESCENDING),('rank',pymongo.DESCENDING)]))
    mobile_deals = []
    tablet_deals = []
    for deal in all_deals:
        item = get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']})
        if deal['category_id'] ==3:
            mobile_deals.append(getItemObjForStaticDeals(item[0]))
        elif deal['category_id'] ==5:
            tablet_deals.append(getItemObjForStaticDeals(item[0]))
        else:
            continue
    
    random.shuffle(mobile_deals,random.random)
    random.shuffle(tablet_deals,random.random)
    
    mem_cache_val = {3:mobile_deals, 5:tablet_deals}
    mc.set("staticDeals", mem_cache_val, 3600)

def getItemObjForStaticDeals(item):
    return {'marketPlaceUrl':item.get('marketPlaceUrl'),'available_price':item.get('available_price'),'source_product_name':item.get('source_product_name'),'thumbnail':item.get('thumbnail'),'source_id':int(item.get('source_id'))}

def getItemByMerchantIdentifier(identifier, source_id):
    skuData = None
    if source_id in (1,2,4,5,6):
        skuData = get_mongo_connection().Catalog.MasterData.find_one({'identifier':identifier.strip(), 'source_id':source_id})
    elif source_id == 3:
        skuData = get_mongo_connection().Catalog.MasterData.find_one({'secondaryIdentifier':identifier.strip(), 'source_id':source_id})
    if skuData is None:
        return {}
    else:
        return skuData
    
def getDealsForNotification(skuBundleIds):
    bundles= [int(skuBundleId) for skuBundleId in skuBundleIds.split(',')]
    print bundles
    dealsList = []
    dealsListMap = []
    for bundleId in bundles:
        outer_query = []
        outer_query.append({ "$or": [ { "showDeal": 1} , { "prepaidDeal": 1 } ] })
        outer_query.append({'skuBundleId':bundleId})
        deals = get_mongo_connection().Catalog.Deals.find({"$and":outer_query})
        for deal in deals:
            dealsList.append(deal)
    sortedMap = {}
    rankMap = {}
    rank = 0
    for sorted_deal in dealsList:
        if sortedMap.get(sorted_deal['skuBundleId']) is None:
            sortedMap[sorted_deal['skuBundleId']] = {rank:[sorted_deal]}
            rankMap[rank] = (sortedMap[sorted_deal['skuBundleId']].values())[0]
            rank = rank +1
        else:
            for temp_list in sortedMap.get(sorted_deal['skuBundleId']).itervalues():
                temp_list.append(sorted_deal)
            rankMap[(sortedMap.get(sorted_deal['skuBundleId']).keys())[0]] = temp_list
                
    for dealList in [rankMap.get(k, []) for k in range(0, len(bundles))]:
        temp = []
        for d in dealList:
            item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
            if len(item) ==0:
                continue
            if d['dealType'] == 1 and d['source_id'] ==1:
                item[0]['marketPlaceUrl'] = "http://www.amazon.in/dp/%s"%(item[0]['identifier'].strip())
            elif d['source_id'] ==3:
                item[0]['marketPlaceUrl'] = item[0]['marketPlaceUrl']+'?supc='+item[0].get('identifier')
            else:
                pass 
            try:
                cashBack = getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
                if not cashBack or cashBack.get('cash_back_status')!=1:
                    item[0]['cash_back_type'] = 0
                    item[0]['cash_back'] = 0
                else:
                    item[0]['cash_back_type'] = int(cashBack['cash_back_type'])
                    item[0]['cash_back'] = cashBack['cash_back']
            except:
                print "Error in adding cashback to deals"
                item[0]['cash_back_type'] = 0
                item[0]['cash_back'] = 0
            try:
                item[0]['dp'] = d['dp']
                item[0]['showDp'] = d['showDp']
            except:
                item[0]['dp'] = 0.0
                item[0]['showDp'] = 0
            temp.append(item[0])
        if len(temp) > 1:
            temp = sorted(temp, key = lambda x: (x['available_price']),reverse=False)
        dealsListMap.append(temp)
    return dealsListMap

def getSkuBrandData(sku):
    if sku is None:
        return {}
    else:
        orders = get_mongo_connection().Catalog.MasterData.find_one({'skuBundleId':sku})
        if orders is None:
            return {}
        return orders

def addDealPoints(data):
    collection = get_mongo_connection().Catalog.DealPoints
    cursor = collection.find({'skuBundleId':data['skuBundleId']})
    if cursor.count() > 0:
        return {0:"Sku information already present."}
    else:
        collection.insert(data)
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':data['skuBundleId']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
        return {1:"Data added successfully"}

def getAllBundlesWithDealPoints(offset, limit):
    data = []
    collection = get_mongo_connection().Catalog.DealPoints
    cursor = collection.find().sort([('endDate',pymongo.DESCENDING)]).skip(offset).limit(limit)
    for val in cursor:
        master = get_mongo_connection().Catalog.MasterData.find_one({'skuBundleId':val.get('skuBundleId')})
        if master is not None:
            val['brand'] = master['brand']
            val['source_product_name'] = master['product_name']
        else:
            val['brand'] = ""
            val['source_product_name'] = ""
        data.append(val)
    return data

def generateRedirectUrl(retailer_id, app_id):
    try:
        d_app_offer = app_offers.get_by(id=app_id)
    except:
        traceback.print_exc()
    finally:
        session.close()
    if d_app_offer is None:
        return {'url':"","message":"App id doesn't exist"}
    
    appTransactions = AppTransactions(app_id, retailer_id, to_java_date(datetime.now()), None, 1, CB_INIT, 1, CB_INIT, None, None, d_app_offer.offer_price, d_app_offer.overriden_payout, d_app_offer.override_payout, d_app_offer.user_payout)
    
    get_mongo_connection().AppOrder.AppTransaction.insert(appTransactions.__dict__)
    embedd = str((appTransactions.__dict__).get('_id'))
    try:
        index = d_app_offer.link.index(".freeb.co.in")
    except:
        traceback.print_exc()
        return {'url':"","message":"Substring not found"}
    redirect_url = d_app_offer.link[0:index]+"."+embedd+d_app_offer.link[index:]
    get_mongo_connection().AppOrder.AppTransaction.update({'_id':ObjectId(embedd)},{"$set":{'redirect_url':redirect_url}})
    return {'url':redirect_url,"message":"Success"}

def addPayout(payout, transaction_id):
    try:
        transaction = list(get_mongo_connection().AppOrder.AppTransaction.find({'_id':ObjectId(transaction_id)}))
        if len(transaction) > 0:
            if (transaction[0])['payout_status'] ==1:
                get_mongo_connection().AppOrder.AppTransaction.update({'_id':ObjectId(transaction_id)},{"$set":{'payout_amount':float(payout), 'payout_description': CB_APPROVED,'payout_status':2}})
            
                approvedAppTransaction = approved_app_transactions()
                approvedAppTransaction.app_id = transaction[0]['app_id']
                approvedAppTransaction.cash_back_description = transaction[0]['cash_back_description']
                approvedAppTransaction.retailer_id = transaction[0]['retailer_id']
                approvedAppTransaction.transaction_id = transaction_id
                approvedAppTransaction.transaction_time = to_py_date(long(transaction[0]['transaction_time']))
                approvedAppTransaction.redirect_url = transaction[0]['redirect_url']
                approvedAppTransaction.payout_status = long(transaction[0]['payout_status'])
                approvedAppTransaction.payout_description = transaction[0]['payout_description']
                approvedAppTransaction.cashback_status = long(transaction[0]['cashback_status']) 
                approvedAppTransaction.payout_amount = long(payout)
                approvedAppTransaction.payout_time = datetime.now()
                approvedAppTransaction.offer_price = long(transaction[0]['offer_price']) 
                approvedAppTransaction.overridenCashBack = transaction[0]['overridenCashBack']
                if str(transaction[0]['isCashBackOverriden']) == 'false' or str(transaction[0]['isCashBackOverriden']) == 'False':
                    approvedAppTransaction.isCashBackOverriden = False
                else:
                    approvedAppTransaction.isCashBackOverriden = True
                approvedAppTransaction.user_payout = transaction[0]['user_payout']
                approvedAppTransaction.cashBackConsidered = False
                session.commit()
                
                updateResult = _updateApprovedCashbackToUser(approvedAppTransaction.id)
                return {'status':'ok','message':'Payout updated'}
            elif (transaction[0])['payout_status'] ==2:
                return {'status':'ok','message':'Payout already processed'}
            else:
                return {'status':'fail','message':'Something is wrong'}
        else:
            return {'status':'fail','message':'transaction_id not found'}
    except:
        try:
            get_mongo_connection().AppOrder.AppTransaction.update({'_id':ObjectId(transaction_id)},{"$set":{'payout_amount':None, 'payout_description': CB_INIT,'payout_status':1}})
        except:
            print 'Data Inconsistency in Cashback Process'
            print traceback.print_exc()
        return  {'status':'fail','message':'Something is wrong'}
        
def _updateApprovedCashbackToUser(approvedAppTransactionId):
    approvedAppTransaction = approved_app_transactions.get_by(id=approvedAppTransactionId)
    currentMonth = datetime.today().month
    currentDay = datetime.today().day
    currentYear = datetime.today().year
    fortNight = (currentMonth - 1)*2 + (currentDay/15)
    if currentDay == 30 or currentDay ==31:
        fortNight = fortNight-1
    userAppCashbackObj = user_app_cashbacks.query.filter(user_app_cashbacks.yearVal==currentYear).filter(user_app_cashbacks.user_id==approvedAppTransaction.retailer_id).filter(user_app_cashbacks.fortnightOfYear==fortNight).filter(user_app_cashbacks.status=='Approved').first()
    userAppInstallObj = user_app_installs.query.filter(user_app_installs.user_id==approvedAppTransaction.retailer_id).filter(user_app_installs.app_id==approvedAppTransaction.app_id).filter(user_app_installs.transaction_date==datetime(currentYear, currentMonth, currentDay, 0, 0, 0, 0).date()).first()
    try:
        if userAppCashbackObj is None:
            userAppCashbackObj = user_app_cashbacks()
            userAppCashbackObj.user_id = approvedAppTransaction.retailer_id
            userAppCashbackObj.status = 'Approved'
            userAppCashbackObj.amount = approvedAppTransaction.user_payout
            userAppCashbackObj.fortnightOfYear = fortNight
            userAppCashbackObj.yearVal = currentYear
        else:
            userAppCashbackObj.amount = userAppCashbackObj.amount + approvedAppTransaction.user_payout
            
        if userAppInstallObj is None:
            app_offer = app_offers.get_by(id=approvedAppTransaction.app_id)
            userAppInstallObj = user_app_installs()
            userAppInstallObj.user_id = approvedAppTransaction.retailer_id
            userAppInstallObj.fortnightOfYear = fortNight
            userAppInstallObj.transaction_date = datetime(currentYear, currentMonth, currentDay, 0, 0, 0, 0).date()
            userAppInstallObj.app_id = approvedAppTransaction.app_id
            userAppInstallObj.app_name = app_offer.app_name
            userAppInstallObj.installCount = 1
            userAppInstallObj.payoutAmount = approvedAppTransaction.user_payout
        else:
            userAppInstallObj.installCount = userAppInstallObj.installCount + 1
            userAppInstallObj.payoutAmount = userAppInstallObj.payoutAmount +approvedAppTransaction.user_payout
            
        approvedAppTransaction.cashBackConsidered = True
        session.commit()
        return True
    except:
        session.rollback()
        print traceback.print_exc()
        return False
        
    
    
    
def main():
    #generateRedirectUrl(101,1
    print addPayout("10", "55db82c0bcabd7fc59e0a71")
    
    
    
if __name__=='__main__':
    main()