| Line 2... |
Line 2... |
| 2 |
from shop2020.config.client.ConfigClient import ConfigClient
|
2 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| 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 SnapdealItem, MarketplaceItems, Item, \
|
4 |
from shop2020.model.v1.catalog.impl.DataService import SnapdealItem, MarketplaceItems, Item, \
|
| 5 |
Category, SourcePercentageMaster, MarketPlaceHistory
|
5 |
Category, SourcePercentageMaster, MarketPlaceHistory
|
| 6 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
|
6 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
|
| 7 |
from shop2020.thriftpy.model.v1.catalog.ttypes import CompetitionCategory, CompetitionBasis, SalesPotential
|
7 |
from shop2020.thriftpy.model.v1.catalog.ttypes import CompetitionCategory, CompetitionBasis, SalesPotential,\
|
| - |
|
8 |
Decision
|
| 8 |
from shop2020.clients.CatalogClient import CatalogClient
|
9 |
from shop2020.clients.CatalogClient import CatalogClient
|
| 9 |
from shop2020.clients.InventoryClient import InventoryClient
|
10 |
from shop2020.clients.InventoryClient import InventoryClient
|
| 10 |
import urllib2
|
11 |
import urllib2
|
| 11 |
import time
|
12 |
import time
|
| 12 |
from datetime import date, datetime
|
13 |
from datetime import date, datetime
|
| Line 14... |
Line 15... |
| 14 |
import sys
|
15 |
import sys
|
| 15 |
|
16 |
|
| 16 |
config_client = ConfigClient()
|
17 |
config_client = ConfigClient()
|
| 17 |
host = config_client.get_property('staging_hostname')
|
18 |
host = config_client.get_property('staging_hostname')
|
| 18 |
DataService.initialize(db_hostname=host)
|
19 |
DataService.initialize(db_hostname=host)
|
| - |
|
20 |
import logging
|
| - |
|
21 |
lgr = logging.getLogger()
|
| - |
|
22 |
lgr.setLevel(logging.DEBUG)
|
| - |
|
23 |
fh = logging.FileHandler('snapdeal-history.log')
|
| - |
|
24 |
fh.setLevel(logging.INFO)
|
| - |
|
25 |
frmt = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
|
| - |
|
26 |
fh.setFormatter(frmt)
|
| - |
|
27 |
lgr.addHandler(fh)
|
| 19 |
|
28 |
|
| 20 |
class __Exception:
|
29 |
class __Exception:
|
| 21 |
SERVER_SIDE=1
|
30 |
SERVER_SIDE=1
|
| 22 |
|
31 |
|
| 23 |
|
32 |
|
| Line 66... |
Line 75... |
| 66 |
self.competitionBasis = competitionBasis
|
75 |
self.competitionBasis = competitionBasis
|
| 67 |
self.ourSp = ourSp
|
76 |
self.ourSp = ourSp
|
| 68 |
self.secondLowestSellerTp = secondLowestSellerTp
|
77 |
self.secondLowestSellerTp = secondLowestSellerTp
|
| 69 |
self.lowestPossibleSp = lowestPossibleSp
|
78 |
self.lowestPossibleSp = lowestPossibleSp
|
| 70 |
|
79 |
|
| - |
|
80 |
def fetchItemsForAutoDecrease(time):
|
| - |
|
81 |
autoDecrementItems = MarketplaceItems.query.filter(MarketplaceItems.autoDecrement==True,MarketplaceItems.source==OrderSource.SNAPDEAL).all()
|
| - |
|
82 |
inventory_client = InventoryClient().get_client()
|
| - |
|
83 |
inventoryMap = inventory_client.getInventorySnapshot(0)
|
| - |
|
84 |
for autoDecrementItem in autoDecrementItems:
|
| - |
|
85 |
mpHistory = MarketPlaceHistory.get_by(source=OrderSource.SNAPDEAL,item_id=autoDecrementItem.itemId,timestamp=time)
|
| - |
|
86 |
if not mpHistory.risky:
|
| - |
|
87 |
markReasonForMpItem(mpHistory,'Item is not risky',Decision.AUTO_DECREMENT_FAILED)
|
| - |
|
88 |
continue
|
| - |
|
89 |
if mpHistory.proposedSellingPrice >= mpHistory.ourSellingPrice:
|
| - |
|
90 |
markReasonForMpItem(mpHistory,'Proposed SP greater than current SP',Decision.AUTO_DECREMENT_FAILED)
|
| - |
|
91 |
continue
|
| - |
|
92 |
if not mpHistory.competitiveCategory == CompetitionCategory.COMPETITIVE:
|
| - |
|
93 |
markReasonForMpItem(mpHistory,'Category is '+CompetitionCategory._VALUES_TO_NAMES.get(mpHistory.competitiveCategory),Decision.AUTO_DECREMENT_FAILED)
|
| - |
|
94 |
continue
|
| - |
|
95 |
if mpHistory.otherInventory < 3:
|
| - |
|
96 |
markReasonForMpItem(mpHistory,'Competition stock is not enough',Decision.AUTO_DECREMENT_FAILED)
|
| - |
|
97 |
continue
|
| - |
|
98 |
oosStatus = inventory_client.getOosStatusesForXDaysForItem(autoDecrementItem.itemId,0,3)
|
| - |
|
99 |
count,sale,daysOfStock = 0,0,0
|
| - |
|
100 |
for obj in oosStatus:
|
| - |
|
101 |
if not obj.is_oos:
|
| - |
|
102 |
count+=1
|
| - |
|
103 |
sale = sale+obj.num_orders
|
| - |
|
104 |
avgSalePerDay=0 if count==0 else (float(sale)/count)
|
| - |
|
105 |
totalAvailability, totalReserved = 0,0
|
| - |
|
106 |
itemInventory=inventoryMap[autoDecrementItem.itemId]
|
| - |
|
107 |
availableMap = itemInventory.availability
|
| - |
|
108 |
reserveMap = itemInventory.reserved
|
| - |
|
109 |
for warehouse,availability in availableMap.iteritems():
|
| - |
|
110 |
if warehouse==16:
|
| - |
|
111 |
continue
|
| - |
|
112 |
totalAvailability = totalAvailability+availability
|
| - |
|
113 |
for warehouse,reserve in reserveMap.iteritems():
|
| - |
|
114 |
if warehouse==16:
|
| - |
|
115 |
continue
|
| - |
|
116 |
totalReserved = totalReserved+reserve
|
| - |
|
117 |
if (totalAvailability-totalReserved)<=0:
|
| - |
|
118 |
markReasonForMpItem(mpHistory,'Our stock is not enough',Decision.AUTO_DECREMENT_FAILED)
|
| - |
|
119 |
continue
|
| - |
|
120 |
daysOfStock = (float(totalAvailability-totalReserved))/avgSalePerDay
|
| - |
|
121 |
if daysOfStock<2:
|
| - |
|
122 |
markReasonForMpItem(mpHistory,'Our stock is not enough',Decision.AUTO_DECREMENT_FAILED)
|
| - |
|
123 |
continue
|
| - |
|
124 |
|
| - |
|
125 |
mpHistory.competitorEnoughStock = True
|
| - |
|
126 |
mpHistory.ourEnoughStock = True
|
| - |
|
127 |
mpHistory.avgSales = avgSalePerDay
|
| - |
|
128 |
mpHistory.decision = Decision.AUTO_DECREMENT_SUCCESS
|
| - |
|
129 |
mpHistory.reason = 'All conditions for auto decrement true'
|
| - |
|
130 |
lgr.info("Auto decrement success for itemId "+str(autoDecrementItem.itemId)+" Days of stock "+str(daysOfStock)+" avg sales "+str(avgSalePerDay))
|
| - |
|
131 |
session.commit()
|
| - |
|
132 |
|
| - |
|
133 |
def fetchItemsForAutoIncrease(time):
|
| - |
|
134 |
autoIncrementItems = MarketplaceItems.query.filter(MarketplaceItems.autoIncrement==True,MarketplaceItems.source==OrderSource.SNAPDEAL).all()
|
| - |
|
135 |
inventory_client = InventoryClient().get_client()
|
| - |
|
136 |
inventoryMap = inventory_client.getInventorySnapshot(0)
|
| - |
|
137 |
for autoIncrementItem in autoIncrementItems:
|
| - |
|
138 |
mpHistory = MarketPlaceHistory.get_by(source=OrderSource.SNAPDEAL,item_id=autoIncrementItem.itemId,timestamp=time)
|
| - |
|
139 |
if mpHistory.proposedSellingPrice <= mpHistory.ourSellingPrice:
|
| - |
|
140 |
markReasonForMpItem(mpHistory,'Proposed SP less than current SP',Decision.AUTO_INCREMENT_FAILED)
|
| - |
|
141 |
continue
|
| - |
|
142 |
if mpHistory.totalSeller==1 and mpHistory.ourRank==1:
|
| - |
|
143 |
markReasonForMpItem(mpHistory,'We are the only seller',Decision.AUTO_INCREMENT_FAILED)
|
| - |
|
144 |
continue
|
| - |
|
145 |
oosStatus = inventory_client.getOosStatusesForXDaysForItem(autoIncrementItem.itemId,0,3)
|
| - |
|
146 |
count,sale,daysOfStock = 0,0,0
|
| - |
|
147 |
for obj in oosStatus:
|
| - |
|
148 |
if not obj.is_oos:
|
| - |
|
149 |
count+=1
|
| - |
|
150 |
sale = sale+obj.num_orders
|
| - |
|
151 |
avgSalePerDay=0 if count==0 else (float(sale)/count)
|
| - |
|
152 |
totalAvailability, totalReserved = 0,0
|
| - |
|
153 |
itemInventory=inventoryMap[autoIncrementItem.itemId]
|
| - |
|
154 |
availableMap = itemInventory.availability
|
| - |
|
155 |
reserveMap = itemInventory.reserved
|
| - |
|
156 |
for warehouse,availability in availableMap.iteritems():
|
| - |
|
157 |
if warehouse==16:
|
| - |
|
158 |
continue
|
| - |
|
159 |
totalAvailability = totalAvailability+availability
|
| - |
|
160 |
for warehouse,reserve in reserveMap.iteritems():
|
| - |
|
161 |
if warehouse==16:
|
| - |
|
162 |
continue
|
| - |
|
163 |
totalReserved = totalReserved+reserve
|
| - |
|
164 |
if (totalAvailability-totalReserved)<=0:
|
| - |
|
165 |
markReasonForMpItem(mpHistory,'Our stock is 0',Decision.AUTO_INCREMENT_FAILED)
|
| - |
|
166 |
continue
|
| - |
|
167 |
daysOfStock = (float(totalAvailability-totalReserved))/avgSalePerDay
|
| - |
|
168 |
if daysOfStock>5:
|
| - |
|
169 |
markReasonForMpItem(mpHistory,'Our stock is enough',Decision.AUTO_INCREMENT_FAILED)
|
| - |
|
170 |
continue
|
| - |
|
171 |
|
| - |
|
172 |
mpHistory.ourEnoughStock = False
|
| - |
|
173 |
mpHistory.avgSales = avgSalePerDay
|
| - |
|
174 |
mpHistory.decision = Decision.AUTO_INCREMENT_SUCCESS
|
| - |
|
175 |
mpHistory.reason = 'All conditions for auto increment true'
|
| - |
|
176 |
lgr.info("Auto increment success for itemId "+str(autoIncrementItem.itemId)+" Days of stock "+str(daysOfStock)+" avg sales "+str(avgSalePerDay))
|
| - |
|
177 |
session.commit()
|
| - |
|
178 |
|
| - |
|
179 |
|
| - |
|
180 |
def markReasonForMpItem(mpHistory,reason,decision):
|
| - |
|
181 |
mpHistory.decision = decision
|
| - |
|
182 |
mpHistory.reason = reason
|
| - |
|
183 |
|
| 71 |
def fetchDetails(supc_code):
|
184 |
def fetchDetails(supc_code):
|
| 72 |
url="http://www.snapdeal.com/json/gvbps?supc=%s&catId=91"%(supc_code)
|
185 |
url="http://www.snapdeal.com/json/gvbps?supc=%s&catId=91"%(supc_code)
|
| 73 |
print url
|
186 |
print url
|
| 74 |
time.sleep(1)
|
187 |
time.sleep(1)
|
| 75 |
req = urllib2.Request(url)
|
188 |
req = urllib2.Request(url)
|
| Line 472... |
Line 585... |
| 472 |
|
585 |
|
| 473 |
def getTargetTp(targetSp,mpItem):
|
586 |
def getTargetTp(targetSp,mpItem):
|
| 474 |
return targetSp- targetSp*(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100));
|
587 |
return targetSp- targetSp*(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100));
|
| 475 |
|
588 |
|
| 476 |
def getTargetSp(targetTp,mpItem):
|
589 |
def getTargetSp(targetTp,mpItem):
|
| 477 |
return targetTp+(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))/(1-((mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))))
|
590 |
return float(targetTp+(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100)))/(1-((mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))))
|
| 478 |
|
591 |
|
| 479 |
def getSalesPotential(lowestOfferPrice,ourNlc):
|
592 |
def getSalesPotential(lowestOfferPrice,ourNlc):
|
| 480 |
if lowestOfferPrice - ourNlc < 0:
|
593 |
if lowestOfferPrice - ourNlc < 0:
|
| 481 |
return 'HIGH'
|
594 |
return 'HIGH'
|
| 482 |
elif (lowestOfferPrice - ourNlc)/lowestOfferPrice >=0 and (lowestOfferPrice - ourNlc)/lowestOfferPrice <=.02:
|
595 |
elif (float(lowestOfferPrice - ourNlc))/lowestOfferPrice >=0 and (float(lowestOfferPrice - ourNlc))/lowestOfferPrice <=.02:
|
| 483 |
return 'MEDIUM'
|
596 |
return 'MEDIUM'
|
| 484 |
else:
|
597 |
else:
|
| 485 |
return 'LOW'
|
598 |
return 'LOW'
|
| 486 |
|
599 |
|
| 487 |
def main():
|
600 |
def main():
|
| Line 493... |
Line 606... |
| 493 |
commitCantCompete(cantCompete,timestamp)
|
606 |
commitCantCompete(cantCompete,timestamp)
|
| 494 |
commitBuyBox(buyBoxItems,timestamp)
|
607 |
commitBuyBox(buyBoxItems,timestamp)
|
| 495 |
commitCompetitive(competitive,timestamp)
|
608 |
commitCompetitive(competitive,timestamp)
|
| 496 |
commitCompetitiveNoInventory(competitiveNoInventory,timestamp)
|
609 |
commitCompetitiveNoInventory(competitiveNoInventory,timestamp)
|
| 497 |
commitNegativeMargin(negativeMargin,timestamp)
|
610 |
commitNegativeMargin(negativeMargin,timestamp)
|
| - |
|
611 |
fetchItemsForAutoDecrease(timestamp)
|
| - |
|
612 |
fetchItemsForAutoIncrease(timestamp)
|
| - |
|
613 |
|
| - |
|
614 |
|
| 498 |
if __name__ == '__main__':
|
615 |
if __name__ == '__main__':
|
| 499 |
main()
|
616 |
main()
|
| 500 |
|
617 |
|