| Line 6... |
Line 6... |
| 6 |
from dtr.utils.utils import to_java_date
|
6 |
from dtr.utils.utils import to_java_date
|
| 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 |
from dtr.utils.utils import getCashBack
|
- |
|
| 12 |
|
11 |
|
| 13 |
parser = optparse.OptionParser()
|
12 |
parser = optparse.OptionParser()
|
| 14 |
parser.add_option("-H", "--host", dest="hostname",
|
13 |
parser.add_option("-H", "--host", dest="hostname",
|
| 15 |
default="localhost",
|
14 |
default="localhost",
|
| 16 |
type="string", help="The HOST where the DB server is running",
|
15 |
type="string", help="The HOST where the DB server is running",
|
| Line 34... |
Line 33... |
| 34 |
now = datetime.now()
|
33 |
now = datetime.now()
|
| 35 |
|
34 |
|
| 36 |
class __SkuInfo:
|
35 |
class __SkuInfo:
|
| 37 |
|
36 |
|
| 38 |
def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
|
37 |
def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
|
| 39 |
maxDiscount, discountType, dp, nlcPoints, status, in_stock, maxprice, brand, dealType, dealPoints, brand_id):
|
38 |
maxDiscount, discountType, dp, nlcPoints, status, in_stock, maxprice, brand, dealType, dealPoints):
|
| 40 |
self._id = _id
|
39 |
self._id = _id
|
| 41 |
self.skuBundleId = skuBundleId
|
40 |
self.skuBundleId = skuBundleId
|
| 42 |
self.category_id = category_id
|
41 |
self.category_id = category_id
|
| 43 |
self.mrp = mrp
|
42 |
self.mrp = mrp
|
| 44 |
self.available_price = available_price
|
43 |
self.available_price = available_price
|
| Line 56... |
Line 55... |
| 56 |
self.in_stock = in_stock
|
55 |
self.in_stock = in_stock
|
| 57 |
self.maxprice = maxprice
|
56 |
self.maxprice = maxprice
|
| 58 |
self.brand = brand
|
57 |
self.brand = brand
|
| 59 |
self.dealType = dealType
|
58 |
self.dealType = dealType
|
| 60 |
self.dealPoints = dealPoints
|
59 |
self.dealPoints = dealPoints
|
| 61 |
self.brand_id = brand_id
|
- |
|
| 62 |
|
60 |
|
| 63 |
|
61 |
|
| 64 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
62 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 65 |
global con
|
63 |
global con
|
| 66 |
if con is None:
|
64 |
if con is None:
|
| Line 86... |
Line 84... |
| 86 |
#topSkus = collection.find( {'_id':664})
|
84 |
#topSkus = collection.find( {'_id':664})
|
| 87 |
for sku in topSkus:
|
85 |
for sku in topSkus:
|
| 88 |
"""Fix this """
|
86 |
"""Fix this """
|
| 89 |
#TODO Compute deal flags else where.
|
87 |
#TODO Compute deal flags else where.
|
| 90 |
info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \
|
88 |
info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \
|
| 91 |
None, None, None, None, sku['status'], sku['in_stock'],None,sku['brand'].strip().upper(), 0, 0, sku['brand_id'])
|
89 |
None, None, None, None, sku['status'], sku['in_stock'],sku['maxPrice'],sku['brand'].strip().upper(), 0, 0)
|
| 92 |
exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'sku':info._id}, {'overrideNlc':1} ]} ))
|
90 |
exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'sku':info._id}, {'overrideNlc':1} ]} ))
|
| 93 |
if len(exceptionalNlc) > 0:
|
91 |
if len(exceptionalNlc) > 0:
|
| 94 |
"""Exceptional nlc found, no need to calculate max and min R-nlc"""
|
92 |
"""Exceptional nlc found, no need to calculate max and min R-nlc"""
|
| 95 |
info.maxNlc = exceptionalNlc[0]['maxNlc']
|
93 |
info.maxNlc = exceptionalNlc[0]['maxNlc']
|
| 96 |
info.minNlc = exceptionalNlc[0]['minNlc']
|
94 |
info.minNlc = exceptionalNlc[0]['minNlc']
|
| - |
|
95 |
if info.maxprice == 0:
|
| 97 |
info.maxprice = exceptionalNlc[0]['maxNlc']
|
96 |
info.maxprice = exceptionalNlc[0]['maxNlc']
|
| 98 |
LATEST_UPDATED_ITEMS.append(info)
|
97 |
LATEST_UPDATED_ITEMS.append(info)
|
| 99 |
continue
|
98 |
continue
|
| 100 |
|
99 |
|
| 101 |
skuSchemeDetails = list(get_mongo_connection().Catalog.SkuSchemeDetails.find( {'sku':info._id}).sort([('addedOn',pymongo.DESCENDING)]).limit(1))
|
100 |
skuSchemeDetails = list(get_mongo_connection().Catalog.SkuSchemeDetails.find( {'sku':info._id}).sort([('addedOn',pymongo.DESCENDING)]).limit(1))
|
| 102 |
if len(skuSchemeDetails) > 0:
|
101 |
if len(skuSchemeDetails) > 0:
|
| Line 147... |
Line 146... |
| 147 |
"""Now mrp is zero, so we have to use saholic MRP"""
|
146 |
"""Now mrp is zero, so we have to use saholic MRP"""
|
| 148 |
if identifier is not None:
|
147 |
if identifier is not None:
|
| 149 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
148 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
| 150 |
obj.mrp = it.mrp
|
149 |
obj.mrp = it.mrp
|
| 151 |
if obj.mrp > 0:
|
150 |
if obj.mrp > 0:
|
| - |
|
151 |
print obj._id
|
| 152 |
obj.minNlc = obj.mrp - (obj.mrp * obj.maxDiscount/100) - obj.schemeAmount
|
152 |
obj.minNlc = obj.mrp - (obj.mrp * obj.maxDiscount/100) - obj.schemeAmount
|
| 153 |
obj.maxNlc = obj.mrp - (obj.mrp * obj.minDiscount/100) - obj.schemeAmount
|
153 |
obj.maxNlc = obj.mrp - (obj.mrp * obj.minDiscount/100) - obj.schemeAmount
|
| - |
|
154 |
if obj.maxprice == 0:
|
| 154 |
obj.maxprice = obj.maxNlc
|
155 |
obj.maxprice = obj.maxNlc
|
| 155 |
elif obj.discountType == DISCOUNT_TYPE.get('DP'):
|
156 |
elif obj.discountType == DISCOUNT_TYPE.get('DP'):
|
| 156 |
if obj.dp == 0:
|
157 |
if obj.dp == 0:
|
| 157 |
"""Now dp is zero, so we have to use saholic minimum dp for item"""
|
158 |
"""Now dp is zero, so we have to use saholic minimum dp for item"""
|
| 158 |
if identifier is not None:
|
159 |
if identifier is not None:
|
| 159 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
160 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
| Line 164... |
Line 165... |
| 164 |
except:
|
165 |
except:
|
| 165 |
pass
|
166 |
pass
|
| 166 |
if obj.dp > 0:
|
167 |
if obj.dp > 0:
|
| 167 |
obj.minNlc = obj.dp - (obj.dp * obj.maxDiscount/100) - obj.schemeAmount
|
168 |
obj.minNlc = obj.dp - (obj.dp * obj.maxDiscount/100) - obj.schemeAmount
|
| 168 |
obj.maxNlc = obj.dp - (obj.dp * obj.minDiscount/100) - obj.schemeAmount
|
169 |
obj.maxNlc = obj.dp - (obj.dp * obj.minDiscount/100) - obj.schemeAmount
|
| - |
|
170 |
if obj.maxprice == 0:
|
| 169 |
obj.maxprice = obj.maxNlc
|
171 |
obj.maxprice = obj.maxNlc
|
| 170 |
else:
|
172 |
else:
|
| 171 |
"""No rule found, use saholic min nlc as max and min R-Nlc"""
|
173 |
"""No rule found, use saholic min nlc as max and min R-Nlc"""
|
| 172 |
if identifier is not None:
|
174 |
if identifier is not None:
|
| 173 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
175 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
| 174 |
try:
|
176 |
try:
|
| 175 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
177 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
| 176 |
min_nlc = min(pricing.nlc for pricing in vendorPricing)
|
178 |
min_nlc = min(pricing.nlc for pricing in vendorPricing)
|
| 177 |
obj.maxNlc = min_nlc
|
179 |
obj.maxNlc = min_nlc
|
| 178 |
obj.minNlc = min_nlc
|
180 |
obj.minNlc = min_nlc
|
| - |
|
181 |
if obj.maxprice == 0:
|
| 179 |
obj.maxprice = obj.maxNlc
|
182 |
obj.maxprice = obj.maxNlc
|
| 180 |
except:
|
183 |
except:
|
| 181 |
pass
|
184 |
pass
|
| 182 |
populated = populated + 300
|
185 |
populated = populated + 300
|
| 183 |
time.sleep(10)
|
186 |
time.sleep(10)
|
| 184 |
|
187 |
|
| Line 199... |
Line 202... |
| 199 |
get_mongo_connection().Catalog.Deals.update({'_id':manualDeal['_id']},{"$set":{'dealType':0}},upsert=False, multi=True)
|
202 |
get_mongo_connection().Catalog.Deals.update({'_id':manualDeal['_id']},{"$set":{'dealType':0}},upsert=False, multi=True)
|
| 200 |
|
203 |
|
| 201 |
|
204 |
|
| 202 |
def calculateNlcPoints():
|
205 |
def calculateNlcPoints():
|
| 203 |
global LATEST_UPDATED_ITEMS
|
206 |
global LATEST_UPDATED_ITEMS
|
| - |
|
207 |
print "inside nlc oints"
|
| 204 |
for sku in LATEST_UPDATED_ITEMS:
|
208 |
for sku in LATEST_UPDATED_ITEMS:
|
| 205 |
if sku.maxNlc and sku.minNlc:
|
209 |
if sku.maxNlc and sku.minNlc:
|
| - |
|
210 |
print sku._id
|
| 206 |
|
211 |
|
| 207 |
"""Create map - TODO"""
|
212 |
"""Create map - TODO"""
|
| 208 |
|
213 |
|
| 209 |
if sku.status == 2:
|
214 |
if sku.status == 2:
|
| 210 |
eolWeight = .60
|
215 |
eolWeight = .60
|
| Line 217... |
Line 222... |
| 217 |
basePointPercentage = 8.0
|
222 |
basePointPercentage = 8.0
|
| 218 |
maxNlcPoints = 150
|
223 |
maxNlcPoints = 150
|
| 219 |
else:
|
224 |
else:
|
| 220 |
basePointPercentage = 10.0
|
225 |
basePointPercentage = 10.0
|
| 221 |
maxNlcPoints = 150
|
226 |
maxNlcPoints = 150
|
| 222 |
discFromMinNlc = (sku.minNlc - sku.available_price)/sku.available_price *100
|
227 |
discFromMinNlc = float((sku.minNlc - sku.available_price))/sku.available_price *100
|
| 223 |
discFromMaxNlc = (sku.maxNlc - sku.available_price)/sku.available_price *100
|
228 |
discFromMaxNlc = float((sku.maxNlc - sku.available_price))/sku.available_price *100
|
| - |
|
229 |
print discFromMinNlc
|
| - |
|
230 |
print discFromMaxNlc
|
| 224 |
if discFromMinNlc > 0:
|
231 |
if discFromMinNlc > 0:
|
| 225 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
232 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
| 226 |
elif discFromMinNlc < 0 and discFromMaxNlc > 0:
|
233 |
elif discFromMinNlc < 0 and discFromMaxNlc > 0:
|
| 227 |
nlcPoints = 0
|
234 |
nlcPoints = 0
|
| 228 |
else:
|
235 |
else:
|
| Line 284... |
Line 291... |
| 284 |
bestSellerPoints = 0
|
291 |
bestSellerPoints = 0
|
| 285 |
toUpdate = []
|
292 |
toUpdate = []
|
| 286 |
for similarItem in similarItems:
|
293 |
for similarItem in similarItems:
|
| 287 |
if mc.get("negative_deals") is None:
|
294 |
if mc.get("negative_deals") is None:
|
| 288 |
populateNegativeDeals()
|
295 |
populateNegativeDeals()
|
| 289 |
try:
|
- |
|
| 290 |
cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)
|
- |
|
| 291 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
- |
|
| 292 |
pass
|
- |
|
| 293 |
else:
|
- |
|
| 294 |
if cashBack['cash_back_type'] ==1:
|
- |
|
| 295 |
similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100
|
- |
|
| 296 |
elif cashBack['cash_back_type'] ==2:
|
- |
|
| 297 |
similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])
|
- |
|
| 298 |
else:
|
- |
|
| 299 |
pass
|
- |
|
| 300 |
except Exception as cashBackEx:
|
- |
|
| 301 |
print cashBackEx
|
- |
|
| 302 |
print "Error calculating cashback."
|
- |
|
| 303 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
296 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
| 304 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
297 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
| 305 |
continue
|
298 |
continue
|
| 306 |
if similarItem['available_price'] < bestPrice:
|
299 |
if similarItem['available_price'] < bestPrice:
|
| 307 |
bestOne = similarItem
|
300 |
bestOne = similarItem
|
| Line 319... |
Line 312... |
| 319 |
toUpdate.remove(bestOne['_id'])
|
312 |
toUpdate.remove(bestOne['_id'])
|
| 320 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
313 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
| 321 |
if len(toUpdate) > 0:
|
314 |
if len(toUpdate) > 0:
|
| 322 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
315 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
| 323 |
|
316 |
|
| 324 |
def elimiateSimilarDealsOld():
|
- |
|
| 325 |
allItems = get_mongo_connection().Catalog.Deals.find().distinct('skuBundleId')
|
- |
|
| 326 |
for skuBundleId in allItems:
|
- |
|
| 327 |
print skuBundleId
|
- |
|
| 328 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
- |
|
| 329 |
bestPrice = float("inf")
|
- |
|
| 330 |
bestOne = None
|
- |
|
| 331 |
bestSellerPoints = 0
|
- |
|
| 332 |
toUpdate = []
|
- |
|
| 333 |
for similarItem in similarItems:
|
- |
|
| 334 |
if mc.get("negative_deals") is None:
|
- |
|
| 335 |
populateNegativeDeals()
|
- |
|
| 336 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
- |
|
| 337 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDealOld':0 }})
|
- |
|
| 338 |
continue
|
- |
|
| 339 |
if similarItem['available_price'] < bestPrice:
|
- |
|
| 340 |
bestOne = similarItem
|
- |
|
| 341 |
bestPrice = similarItem['available_price']
|
- |
|
| 342 |
bestSellerPoints = similarItem['bestSellerPoints']
|
- |
|
| 343 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
- |
|
| 344 |
bestOne = similarItem
|
- |
|
| 345 |
bestPrice = similarItem['available_price']
|
- |
|
| 346 |
bestSellerPoints = similarItem['bestSellerPoints']
|
- |
|
| 347 |
else:
|
- |
|
| 348 |
pass
|
- |
|
| 349 |
if bestOne is not None:
|
- |
|
| 350 |
for similarItem in similarItems:
|
- |
|
| 351 |
toUpdate.append(similarItem['_id'])
|
- |
|
| 352 |
toUpdate.remove(bestOne['_id'])
|
- |
|
| 353 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDealOld':1 }})
|
- |
|
| 354 |
if len(toUpdate) > 0:
|
- |
|
| 355 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDealOld':0 }},upsert=False, multi=True)
|
- |
|
| 356 |
|
- |
|
| 357 |
|
- |
|
| 358 |
|
- |
|
| 359 |
def main():
|
317 |
def main():
|
| 360 |
try:
|
318 |
try:
|
| 361 |
populateStuff()
|
319 |
populateStuff()
|
| 362 |
calculateNlc()
|
320 |
calculateNlc()
|
| 363 |
addManualDealsInfo()
|
321 |
addManualDealsInfo()
|
| Line 365... |
Line 323... |
| 365 |
session.close()
|
323 |
session.close()
|
| 366 |
calculateNlcPoints()
|
324 |
calculateNlcPoints()
|
| 367 |
commitData()
|
325 |
commitData()
|
| 368 |
addBestSellerPoints()
|
326 |
addBestSellerPoints()
|
| 369 |
elimiateSimilarDeals()
|
327 |
elimiateSimilarDeals()
|
| 370 |
elimiateSimilarDealsOld()
|
- |
|
| 371 |
|
328 |
|
| 372 |
|
329 |
|
| 373 |
if __name__=='__main__':
|
330 |
if __name__=='__main__':
|
| 374 |
main()
|
- |
|
| 375 |
|
331 |
main()
|
| - |
|
332 |
|