Subversion Repositories SmartDukaan

Rev

Rev 18189 | Rev 19194 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18189 Rev 19144
Line 1... Line 1...
1
import pymongo
1
import pymongo
2
from elixir import *
2
from elixir import *
3
from shop2020.model.v1.catalog.impl import DataService
3
from shop2020.model.v1.catalog.impl import DataService
4
from shop2020.model.v1.catalog.impl.DataService import Item
4
from shop2020.model.v1.catalog.impl.DataService import Item
5
from shop2020.clients.InventoryClient import InventoryClient
5
from shop2020.clients.InventoryClient import InventoryClient
6
from dtr.utils.utils import to_java_date
6
from dtr.utils.utils import to_java_date, getCashBack
7
from datetime import datetime, timedelta
7
from datetime import datetime, timedelta
8
import time
8
import time
9
import optparse
9
import optparse
10
from dtr.storage.MemCache import MemCache
10
from dtr.storage.MemCache import MemCache
11
import traceback
11
import traceback
Line 45... Line 45...
45
 
45
 
46
class __SkuInfo:
46
class __SkuInfo:
47
    
47
    
48
    def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
48
    def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
49
                 maxDiscount, discountType, dp, nlcPoints, status, in_stock, maxprice, brand, dealType, brand_id, manualDealThresholdPrice,\
49
                 maxDiscount, discountType, dp, nlcPoints, status, in_stock, maxprice, brand, dealType, brand_id, manualDealThresholdPrice,\
50
                 codAvailable,showDp,gross_price, subCategoryId,subCategory,shippingCost):
50
                 codAvailable,showDp,gross_price, subCategoryId,subCategory,shippingCost,netPriceAfterCashBack):
51
        self._id = _id
51
        self._id = _id
52
        self.skuBundleId = skuBundleId
52
        self.skuBundleId = skuBundleId
53
        self.category_id = category_id
53
        self.category_id = category_id
54
        self.mrp = mrp
54
        self.mrp = mrp
55
        self.available_price = available_price
55
        self.available_price = available_price
Line 74... Line 74...
74
        self.showDp = showDp
74
        self.showDp = showDp
75
        self.gross_price = gross_price
75
        self.gross_price = gross_price
76
        self.subCategoryId = subCategoryId
76
        self.subCategoryId = subCategoryId
77
        self.subCategory = subCategory
77
        self.subCategory = subCategory
78
        self.shippingCost = shippingCost
78
        self.shippingCost = shippingCost
-
 
79
        self.netPriceAfterCashBack = netPriceAfterCashBack
79
 
80
 
80
 
81
 
81
def get_mongo_connection(host=options.mongoHost, port=27017):
82
def get_mongo_connection(host=options.mongoHost, port=27017):
82
    global con
83
    global con
83
    if con is None:
84
    if con is None:
Line 102... Line 103...
102
            break
103
            break
103
        #topSkus = collection.find( {'_id':664})
104
        #topSkus = collection.find( {'_id':664})
104
        for sku in topSkus:
105
        for sku in topSkus:
105
            """Fix this """
106
            """Fix this """
106
            #TODO Compute deal flags else where.
107
            #TODO Compute deal flags else where.
-
 
108
            if sku['source_id'] != SOURCE_MAP.get('PAYTM.COM'):
-
 
109
                netPriceAfterCashBack = getNetPriceForItem(sku['_id'], sku['source_id'], sku['category_id'], sku['available_price'])
-
 
110
            else:
-
 
111
                if sku['codAvailable'] ==0:
-
 
112
                    netPriceAfterCashBack = getNetPriceForItem(sku['_id'], sku['source_id'], sku['category_id'], sku['gross_price'])
-
 
113
                else:
-
 
114
                    netPriceAfterCashBack = getNetPriceForItem(sku['_id'], sku['source_id'], sku['category_id'], sku['available_price'])
-
 
115
            
107
            info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \
116
            info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \
108
                             None, None, None, None, sku['status'], sku['in_stock'],sku['maxPrice'],sku['brand'].strip().upper(), 0, sku['brand_id'], None, sku['codAvailable'], 0, sku['gross_price'], sku['subCategoryId'], \
117
                             None, None, None, None, sku['status'], sku['in_stock'],sku['maxPrice'],sku['brand'].strip().upper(), 0, sku['brand_id'], None, sku['codAvailable'], 0, sku['gross_price'], sku['subCategoryId'], \
109
                             sku['subCategory'],sku['shippingCost'])
118
                             sku['subCategory'],sku['shippingCost'],netPriceAfterCashBack)
110
            exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'skuBundleId':info.skuBundleId}, {'overrideNlc':1} ]} ))
119
            exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'skuBundleId':info.skuBundleId}, {'overrideNlc':1} ]} ))
111
            if len(exceptionalNlc) > 0:
120
            if len(exceptionalNlc) > 0:
112
                """Exceptional nlc found, no need to calculate max and min R-nlc"""
121
                """Exceptional nlc found, no need to calculate max and min R-nlc"""
113
                info.maxNlc = exceptionalNlc[0]['maxNlc']
122
                info.maxNlc = exceptionalNlc[0]['maxNlc']
114
                info.minNlc = exceptionalNlc[0]['minNlc']
123
                info.minNlc = exceptionalNlc[0]['minNlc']
Line 323... Line 332...
323
 
332
 
324
def eliminateSimilarDeals():
333
def eliminateSimilarDeals():
325
    allItems = get_mongo_connection().Catalog.Deals.find().distinct('skuBundleId')
334
    allItems = get_mongo_connection().Catalog.Deals.find().distinct('skuBundleId')
326
    for skuBundleId in allItems:
335
    for skuBundleId in allItems:
327
        print skuBundleId
336
        print skuBundleId
328
        similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
337
        similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('netPriceAfterCashBack',pymongo.ASCENDING)]))
329
        bestPrice = float("inf")
338
        bestPrice = float("inf")
330
        bestOne = None
339
        bestOne = None
331
        bestSellerPoints = 0
340
        bestSellerPoints = 0
332
        toUpdate = []
341
        toUpdate = []
333
        prepaidBestPrice = float("inf")
342
        prepaidBestPrice = float("inf")
Line 341... Line 350...
341
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
350
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
342
                    continue
351
                    continue
343
                if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0 and similarItem['category_id']!=6:
352
                if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0 and similarItem['category_id']!=6:
344
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
353
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
345
                    continue
354
                    continue
346
                if similarItem['available_price'] < bestPrice:
355
                if similarItem['netPriceAfterCashBack'] < bestPrice:
347
                    bestOne = similarItem
356
                    bestOne = similarItem
348
                    bestPrice = similarItem['available_price']
357
                    bestPrice = similarItem['netPriceAfterCashBack']
349
                    bestSellerPoints = similarItem['bestSellerPoints']
358
                    bestSellerPoints = similarItem['bestSellerPoints']
350
                elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
359
                elif similarItem['netPriceAfterCashBack'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
351
                    bestOne = similarItem
360
                    bestOne = similarItem
352
                    bestPrice = similarItem['available_price']
361
                    bestPrice = similarItem['netPriceAfterCashBack']
353
                    bestSellerPoints = similarItem['bestSellerPoints']
362
                    bestSellerPoints = similarItem['bestSellerPoints']
354
                else:
363
                else:
355
                    pass
364
                    pass
356
            else:
365
            else:
357
                if mc.get("negative_deals") is None:
366
                if mc.get("negative_deals") is None:
Line 360... Line 369...
360
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
369
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
361
                    continue
370
                    continue
362
                if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0 and similarItem['category_id']!=6:
371
                if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0 and similarItem['category_id']!=6:
363
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
372
                    get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
364
                    continue
373
                    continue
365
                if similarItem['source_id'] == SOURCE_MAP.get('PAYTM.COM'):
-
 
366
                    similarItem['available_price'] = similarItem['gross_price'] 
-
 
367
                if similarItem['available_price'] < prepaidBestPrice:
374
                if similarItem['netPriceAfterCashBack'] < prepaidBestPrice:
368
                    prepaidBestOne = similarItem
375
                    prepaidBestOne = similarItem
369
                    prepaidBestPrice = similarItem['available_price']
376
                    prepaidBestPrice = similarItem['netPriceAfterCashBack']
370
                    prepaidBestSellerPoints = similarItem['bestSellerPoints']
377
                    prepaidBestSellerPoints = similarItem['bestSellerPoints']
371
                elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
378
                elif similarItem['netPriceAfterCashBack'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
372
                    prepaidBestOne = similarItem
379
                    prepaidBestOne = similarItem
373
                    prepaidBestPrice = similarItem['available_price']
380
                    prepaidBestPrice = similarItem['netPriceAfterCashBack']
374
                    prepaidBestSellerPoints = similarItem['bestSellerPoints']
381
                    prepaidBestSellerPoints = similarItem['bestSellerPoints']
375
                else:
382
                else:
376
                    pass
383
                    pass
377
        if bestOne is not None or prepaidBestOne is not None:
384
        if bestOne is not None or prepaidBestOne is not None:
378
            for similarItem in similarItems:
385
            for similarItem in similarItems:
Line 380... Line 387...
380
            if bestOne is not None:
387
            if bestOne is not None:
381
                toUpdate.remove(bestOne['_id'])
388
                toUpdate.remove(bestOne['_id'])
382
                get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
389
                get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
383
            if prepaidBestOne is not None:
390
            if prepaidBestOne is not None:
384
                if bestOne is not None:
391
                if bestOne is not None:
385
                    if prepaidBestOne['available_price'] < bestOne['available_price']: 
392
                    if prepaidBestOne['netPriceAfterCashBack'] < bestOne['netPriceAfterCashBack']: 
386
                        toUpdate.remove(prepaidBestOne['_id'])
393
                        toUpdate.remove(prepaidBestOne['_id'])
387
                        get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
394
                        get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
388
                else:
395
                else:
389
                    toUpdate.remove(prepaidBestOne['_id'])
396
                    toUpdate.remove(prepaidBestOne['_id'])
390
                    get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
397
                    get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
Line 394... Line 401...
394
def populateDealPointsBundle():
401
def populateDealPointsBundle():
395
    global DEAL_POINTS_MAP
402
    global DEAL_POINTS_MAP
396
    activeDealPoints = get_mongo_connection().Catalog.DealPoints.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}})
403
    activeDealPoints = get_mongo_connection().Catalog.DealPoints.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}})
397
    for activeDeal in activeDealPoints:
404
    for activeDeal in activeDealPoints:
398
        dealPoints = __DealPoints(activeDeal['dealThresholdPrice'], activeDeal['dealPoints'])
405
        dealPoints = __DealPoints(activeDeal['dealThresholdPrice'], activeDeal['dealPoints'])
399
        DEAL_POINTS_MAP[activeDeal['skuBundleId']] = dealPoints 
406
        DEAL_POINTS_MAP[activeDeal['skuBundleId']] = dealPoints
-
 
407
 
-
 
408
def getNetPriceForItem(itemId, source_id, category_id ,price):
-
 
409
    cash_back_type = 0
-
 
410
    cash_back = 0
-
 
411
    try:
-
 
412
        cashBack = getCashBack(itemId, source_id, category_id, mc, options.mongoHost)
-
 
413
        if not cashBack or cashBack.get('cash_back_status')!=1:
-
 
414
            cash_back_type = 0
-
 
415
            cash_back = 0 
-
 
416
            
-
 
417
        else:
-
 
418
            if cashBack['cash_back_type'] in (1,2):
-
 
419
                
-
 
420
                if cashBack.get('maxCashBack') is not None:
-
 
421
                    
-
 
422
                    if cashBack.get('cash_back_type') ==1 and (float(cashBack.get('cash_back'))*price)/100 > cashBack.get('maxCashBack'):
-
 
423
                        cashBack['cash_back_type'] = 2
-
 
424
                        cashBack['cash_back'] = cashBack['maxCashBack']
-
 
425
                    elif cashBack.get('cash_back_type') ==2 and cashBack.get('cash_back') > cashBack.get('maxCashBack'):
-
 
426
                        cashBack['cash_back'] = cashBack['maxCashBack']
-
 
427
                    else:
-
 
428
                        pass
-
 
429
                
-
 
430
                
-
 
431
                
-
 
432
                cash_back_type = cashBack['cash_back_type']
-
 
433
                cash_back = float(cashBack['cash_back'])
-
 
434
    except Exception as cashBackEx:
-
 
435
        pass
-
 
436
    
-
 
437
    if cash_back_type ==1:
-
 
438
        return (price - float(cash_back)*price/100)
-
 
439
    elif cash_back_type ==2:
-
 
440
        return (price - cash_back)
-
 
441
    else:
-
 
442
        return price
-
 
443
 
-
 
444
 
-
 
445
 
-
 
446
# def calculateNetPriceAfterCashback():
-
 
447
#     for data in LATEST_UPDATED_ITEMS:
-
 
448
#         master = list(get_mongo_connection().Catalog.MasterData.find({'_id':data._id}))
-
 
449
#         if master[0]['source_id'] != SOURCE_MAP.get('PAYTM.COM'):
-
 
450
#             netPriceAfterCashBack = getNetPriceForItem(data._id, master[0]['source_id'], master[0]['category_id'], master[0]['available_price'])
-
 
451
#             get_mongo_connection().Catalog.Deals.update({'_id':data._id},{"$set":{'available_price':master[0]['available_price'],'in_stock':master[0]['in_stock'],'netPriceAfterCashBack':netPriceAfterCashBack}})
-
 
452
#         else:
-
 
453
#             if master[0]['codAvailable'] ==0:
-
 
454
#                 netPriceAfterCashBack = getNetPriceForItem(data._id, master[0]['source_id'], master[0]['category_id'], master[0]['gross_price'])
-
 
455
#             else:
-
 
456
#                 netPriceAfterCashBack = getNetPriceForItem(data._id, master[0]['source_id'], master[0]['category_id'], master[0]['available_price'])
-
 
457
#             get_mongo_connection().Catalog.Deals.update({'_id':data._id},{"$set":{'available_price':master[0]['available_price'],'gross_price':master[0]['gross_price'],'in_stock':master[0]['in_stock'],'netPriceAfterCashBack':netPriceAfterCashBack}})
-
 
458
#         
400
 
459
 
401
def main():
460
def main():
402
    try:
461
    try:
403
        populateStuff()
462
        populateStuff()
404
        calculateNlc()
463
        calculateNlc()