Subversion Repositories SmartDukaan

Rev

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

from elixir import * 
from shop2020.model.v1.catalog.impl import DataService
from shop2020.model.v1.catalog.impl.DataService import PrivateDeals, Item
from sqlalchemy.sql.functions import now
from datetime import datetime, timedelta
import pymongo
from dtr.utils.utils import to_java_date
import optparse
from dtr.storage.MemCache import MemCache
from dtr.utils.utils import getCashBack
from dtr.utils.utils import getNlcPoints, DEAL_PRIORITY
import traceback

dealsMap = {}
con = None
dealsCatalogIds = []
itemCatalogMap = {}

parser = optparse.OptionParser()
parser.add_option("-H", "--host", dest="hostname",
                      default="localhost",
                      type="string", help="The HOST where the DB server is running",
                      metavar="db_host")
parser.add_option("-m", "--m", dest="mongoHost",
                      default="localhost",
                      type="string", help="The HOST where the mongo server is running",
                      metavar="mongo_host")

(options, args) = parser.parse_args()

mc = MemCache(options.mongoHost)

DataService.initialize(db_hostname=options.hostname)

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

def get_mongo_connection(host=options.mongoHost, 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 getNetPriceForItem(itemId, source_id, category_id ,price):
    cash_back_type = 0
    cash_back = 0
    try:
        cashBack = getCashBack(itemId, source_id, category_id, mc, options.mongoHost)
        if not cashBack or cashBack.get('cash_back_status')!=1:
            cash_back_type = 0
            cash_back = 0 
            
        else:
            if cashBack['cash_back_type'] in (1,2):
                
                if cashBack.get('maxCashBack') is not None:
                    
                    if cashBack.get('cash_back_type') ==1 and (float(cashBack.get('cash_back'))*price)/100 > cashBack.get('maxCashBack'):
                        cashBack['cash_back_type'] = 2
                        cashBack['cash_back'] = cashBack['maxCashBack']
                    elif cashBack.get('cash_back_type') ==2 and cashBack.get('cash_back') > cashBack.get('maxCashBack'):
                        cashBack['cash_back'] = cashBack['maxCashBack']
                    else:
                        pass
                
                
                
                cash_back_type = cashBack['cash_back_type']
                cash_back = float(cashBack['cash_back'])
    except Exception as cashBackEx:
        pass
    
    if cash_back_type ==1:
        return (price - float(cash_back)*price/100)
    elif cash_back_type ==2:
        return (price - cash_back)
    else:
        return price


def getPrivateDeals():
    try:
        global dealsMap
        dealsMap = dict()
        all_active_items_query =  session.query(PrivateDeals).filter(PrivateDeals.isActive==True).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate))
        all_active_private_deals = all_active_items_query.all()
        if all_active_private_deals is not None or all_active_private_deals!=[]:
            for active_private_deal in all_active_private_deals:
                print active_private_deal.item_id
                item = Item.get_by(id = active_private_deal.item_id)
                if item.sellingPrice >  active_private_deal.dealPrice and item.status==3:
                    dealsMap[active_private_deal.item_id] = active_private_deal
    finally:
        session.close()

def getItemsToUpdate():
    global dealsCatalogIds
    global itemCatalogMap
    toScrapMap = {}
    bestSellers = list(get_mongo_connection().Catalog.MasterData.find({ "$or": [ { "category_id": 6} , { "rank":{"$gt":0} } ] }))
    for bestSeller in bestSellers: 
        saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':4}))
        for d in saholicCatalogIds:
            if d['source_id']!=4:
                continue
            d['dealFlag'] = 0
            d['dealType'] = 0
            toScrapMap[d['_id']] = d
            dealsCatalogIds.append(long(d['identifier'].strip()))
    dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':4,'showDeal':1,'totalPoints':{'$gt':-100}}))
    for deal in dealFlagged:
        if not toScrapMap.has_key(deal['_id']):
            data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
            data[0]['dealFlag'] = 0
            data[0]['dealType'] = 0
            toScrapMap[deal['_id']] = data[0]
            if long(data[0]['identifier'].strip()) not in dealsCatalogIds: 
                dealsCatalogIds.append(long(data[0]['identifier'].strip()))
    manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':4}))
    for manualDeal in manualDeals:
        if not toScrapMap.has_key(manualDeal['sku']):
            data = list(get_mongo_connection().Catalog.MasterData.find({'_id':manualDeal['sku']}))
            if len(data) > 0:
                data[0]['dealFlag'] = 1
                data[0]['dealType'] = manualDeal['dealType']
                toScrapMap[manualDeal['sku']] = data[0]
                if long(data[0]['identifier'].strip()) not in dealsCatalogIds: 
                    dealsCatalogIds.append(long(data[0]['identifier'].strip()))
        else:
            data = toScrapMap.get(manualDeal['sku'])
            data['dealFlag'] = 1
            data['dealType'] = manualDeal['dealType']
    
    items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()
    for item in items:
        temp = []
        if not itemCatalogMap.has_key(item.catalog_item_id):
            temp.append(item)
            print "****",item.catalog_item_id
            itemCatalogMap[item.catalog_item_id] = temp
        else:
            val = itemCatalogMap.get(item.catalog_item_id)
            for l in val:
                temp.append(l)
            temp.append(item)
            itemCatalogMap[item.catalog_item_id] = temp
            
    for saholicCatalogId in toScrapMap.itervalues():
        if saholicCatalogId['source_id']!=4:
            continue
        d_items = itemCatalogMap.get(long(saholicCatalogId['identifier'].strip()))
        available_price = None
        in_stock = 0
        if d_items is not None:
            for d_item in d_items:
                in_stock = 0 
                if d_item.status == 3:
                    in_stock =1
                else:
                    continue
                if dealsMap.get(d_item.id) is not None:
                    available_price = dealsMap.get(d_item.id).dealPrice
                if (available_price !=None):
                    break
        if (available_price is None):
            in_stock = 0
            if d_items is not None:
                for d_item in d_items:
                    if d_item.status == 3:
                        available_price = d_item.sellingPrice
                        in_stock =1
                        break
        print long(saholicCatalogId['identifier'])
        print in_stock
        print available_price
        print dealsMap.get(d_item.id)
        print "++++++++++++++++++++++++++"
        if available_price > 0 or available_price is not None:
            netPriceAfterCashBack = getNetPriceForItem(saholicCatalogId['_id'], SOURCE_MAP.get('SAHOLIC'), saholicCatalogId['category_id'], available_price)
            get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':in_stock}}, multi=True)
            get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price , 'in_stock':in_stock,'netPriceAfterCashBack':netPriceAfterCashBack}}, multi=True)
        else:
            netPriceAfterCashBack = getNetPriceForItem(saholicCatalogId['_id'], SOURCE_MAP.get('SAHOLIC'), saholicCatalogId['category_id'], saholicCatalogId['available_price'])
            get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':in_stock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
            get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'in_stock':in_stock,'netPriceAfterCashBack':netPriceAfterCashBack}}, multi=True)
        
        try:
            recomputeDeal(saholicCatalogId)
        except:
            print "Unable to compute deal for ",saholicCatalogId['skuBundleId']

def populateNegativeDeals():
    negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
    mc.set("negative_deals", negativeDeals, 600)
    
#def recomputePoints(item, deal):
#    try:
#        if item.get('available_price') == deal['available_price']:
#            print "No need to compute points for %d , as price is still same" %(item['_id'])
#            raise
#    except:
#        traceback.print_exc()
#        nlcPoints = deal['nlcPoints']
#    
#    bundleDealPoints = list(get_mongo_connection().Catalog.DealPoints.find({'skuBundleId':item['skuBundleId'],'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}}))
#    if len(bundleDealPoints) > 0:
#        item['manualDealThresholdPrice'] = bundleDealPoints[0]['dealThresholdPrice']
#        dealPoints = bundleDealPoints[0]['dealPoints']
#    else:
#        dealPoints = 0
#        item['manualDealThresholdPrice'] = None
#    
#    
#    get_mongo_connection().Catalog.Deals.update({'_id':deal['_id']},{"$set":{'totalPoints':deal['totalPoints'] - deal['nlcPoints'] + nlcPoints - deal['dealPoints'] +dealPoints , 'nlcPoints': nlcPoints, 'dealPoints': dealPoints, 'manualDealThresholdPrice': item['manualDealThresholdPrice']}})
    
def recomputeDeal(item):
    """Lets recompute deal for this bundle"""
    print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
    skuBundleId = item['skuBundleId']
    
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('netPriceAfterCashBack',pymongo.ASCENDING)]))
    bestPrice = float("inf")
    bestOne = None
    toUpdate = []
    prepaidBestPrice = float("inf")
    prepaidBestOne = None
    for similarItem in similarItems:
        if similarItem['codAvailable'] ==1:
            if mc.get("negative_deals") is None:
                populateNegativeDeals()
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
                continue
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
                continue
            if similarItem.get('netPriceAfterCashBack') < bestPrice:
                bestOne = similarItem
                bestPrice = similarItem.get('netPriceAfterCashBack')
            elif similarItem.get('netPriceAfterCashBack') == bestPrice:
                
                try:
                    if (DEAL_PRIORITY.index(int(similarItem['source_id'])) > DEAL_PRIORITY.index(int(bestOne['source_id']))):
                        continue
                except:
                    traceback.print_exc()
                
                
                bestOne = similarItem
                bestPrice = similarItem.get('netPriceAfterCashBack')
            else:
                pass
        else:
            if mc.get("negative_deals") is None:
                populateNegativeDeals()
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
                continue
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
                continue
            if similarItem.get('netPriceAfterCashBack') < prepaidBestPrice:
                prepaidBestOne = similarItem
                prepaidBestPrice = similarItem.get('netPriceAfterCashBack')
            elif similarItem.get('netPriceAfterCashBack') == prepaidBestPrice:
                
                try:
                    if (DEAL_PRIORITY.index(int(similarItem['source_id'])) > DEAL_PRIORITY.index(int(prepaidBestOne['source_id']))):
                        continue
                except:
                    traceback.print_exc()
                
                prepaidBestOne = similarItem
                prepaidBestPrice = similarItem.get('netPriceAfterCashBack')
            else:
                pass
    if bestOne is not None or prepaidBestOne is not None:
        for similarItem in similarItems:
            toUpdate.append(similarItem['_id'])
        if bestOne is not None:
            toUpdate.remove(bestOne['_id'])
            get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
        if prepaidBestOne is not None:
            if bestOne is not None:
                if prepaidBestOne.get('netPriceAfterCashBack') < bestOne.get('netPriceAfterCashBack'): 
                    toUpdate.remove(prepaidBestOne['_id'])
                    get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
            else:
                toUpdate.remove(prepaidBestOne['_id'])
                get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
    if len(toUpdate) > 0:
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)

        
def main():
    getPrivateDeals()
    try:
        getItemsToUpdate()
    finally:
        session.close()
    print "Done with saholic pricing"

if __name__=='__main__':
    main()