Rev 9919 | Rev 9949 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from elixir import *from shop2020.config.client.ConfigClient import ConfigClientfrom shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import SnapdealItem, MarketplaceItems, Item, \Category, SourcePercentageMaster, MarketPlaceHistoryfrom shop2020.thriftpy.model.v1.order.ttypes import OrderSourcefrom shop2020.thriftpy.model.v1.catalog.ttypes import CompetitionCategory, CompetitionBasis, SalesPotential,\Decisionfrom shop2020.clients.CatalogClient import CatalogClientfrom shop2020.clients.InventoryClient import InventoryClientimport urllib2import timefrom datetime import date, datetimeimport simplejson as jsonimport sysconfig_client = ConfigClient()host = config_client.get_property('staging_hostname')DataService.initialize(db_hostname=host)import logginglgr = logging.getLogger()lgr.setLevel(logging.DEBUG)fh = logging.FileHandler('snapdeal-history.log')fh.setLevel(logging.INFO)frmt = logging.Formatter('%(asctime)s - %(name)s - %(message)s')fh.setFormatter(frmt)lgr.addHandler(fh)class __Exception:SERVER_SIDE=1class __SnapdealDetails:def __init__(self, ourSp, ourInventory, otherInventory, rank, lowestSellerName, lowestSellerCode,lowestSp,secondLowestSellerName, secondLowestSellerCode,secondLowestSellerSp,secondLowestSellerInventory,lowestOfferPrice,secondLowestSellerOfferPrice,ourOfferPrice, totalSeller):self.ourSp = ourSpself.ourOfferPrice = ourOfferPriceself.ourInventory = ourInventoryself.otherInventory = otherInventoryself.rank = rankself.lowestSellerName = lowestSellerNameself.lowestSellerCode = lowestSellerCodeself.lowestSp = lowestSpself.lowestOfferPrice = lowestOfferPriceself.secondLowestSellerName = secondLowestSellerNameself.secondLowestSellerCode = secondLowestSellerCodeself.secondLowestSellerSp = secondLowestSellerSpself.secondLowestSellerOfferPrice = secondLowestSellerOfferPriceself.secondLowestSellerInventory = secondLowestSellerInventoryself.totalSeller = totalSellerclass __SnapdealItemInfo:def __init__(self, supc, nlc, courierCost, item_id, product_group, brand, model_name, model_number, color, weight, parent_category, risky, warehouseId, vatRate):self.supc = supcself.nlc = nlcself.courierCost = courierCostself.item_id = item_idself.product_group = product_groupself.brand = brandself.model_name = model_nameself.model_number = model_numberself.color = colorself.weight = weightself.parent_category = parent_categoryself.risky = riskyself.warehouseId = warehouseIdself.vatRate = vatRateclass __SnapdealPricing:def __init__(self, ourSp, ourTp, lowestTp, lowestPossibleTp, competitionBasis, secondLowestSellerTp, lowestPossibleSp):self.ourTp = ourTpself.lowestTp = lowestTpself.lowestPossibleTp = lowestPossibleTpself.competitionBasis = competitionBasisself.ourSp = ourSpself.secondLowestSellerTp = secondLowestSellerTpself.lowestPossibleSp = lowestPossibleSpdef fetchItemsForAutoDecrease(time):autoDecrementItems = session.query(MarketPlaceHistory).join((MarketplaceItems,MarketPlaceHistory.item_id==MarketplaceItems.itemId))\.filter(MarketPlaceHistory.timestamp==time).filter(MarketPlaceHistory.source==OrderSource.SNAPDEAL).filter(MarketPlaceHistory.competitiveCategory==CompetitionCategory.COMPETITIVE)\.filter(MarketplaceItems.source==OrderSource.SNAPDEAL).filter(MarketplaceItems.autoDecrement==True).all()#autoDecrementItems = MarketplaceItems.query.filter(MarketplaceItems.autoDecrement==True).filter(MarketplaceItems.source==OrderSource.SNAPDEAL).all()inventory_client = InventoryClient().get_client()inventoryMap = inventory_client.getInventorySnapshot(0)for autoDecrementItem in autoDecrementItems:#mpHistory = MarketPlaceHistory.get_by(source=OrderSource.SNAPDEAL,item_id=autoDecrementItem.itemId,timestamp=time)if not autoDecrementItem.competitiveCategory == CompetitionCategory.COMPETITIVE:markReasonForMpItem(autoDecrementItem,'Category is '+CompetitionCategory._VALUES_TO_NAMES.get(autoDecrementItem.competitiveCategory),Decision.AUTO_DECREMENT_FAILED)continueif not autoDecrementItem.risky:markReasonForMpItem(autoDecrementItem,'Item is not risky',Decision.AUTO_DECREMENT_FAILED)continueif autoDecrementItem.proposedSellingPrice >= autoDecrementItem.ourSellingPrice:markReasonForMpItem(autoDecrementItem,'Proposed SP greater than current SP',Decision.AUTO_DECREMENT_FAILED)continueif autoDecrementItem.otherInventory < 3:markReasonForMpItem(autoDecrementItem,'Competition stock is not enough',Decision.AUTO_DECREMENT_FAILED)continueoosStatus = inventory_client.getOosStatusesForXDaysForItem(autoDecrementItem.item_id,0,3)count,sale,daysOfStock = 0,0,0for obj in oosStatus:if not obj.is_oos:count+=1sale = sale+obj.num_ordersavgSalePerDay=0 if count==0 else (float(sale)/count)totalAvailability, totalReserved = 0,0if (not inventoryMap.has_key(autoDecrementItem.item_id)):markReasonForMpItem(autoDecrementItem,'Average sale per day is zero',Decision.AUTO_DECREMENT_FAILED)continueitemInventory=inventoryMap[autoDecrementItem.item_id]availableMap = itemInventory.availabilityreserveMap = itemInventory.reservedfor warehouse,availability in availableMap.iteritems():if warehouse==16:continuetotalAvailability = totalAvailability+availabilityfor warehouse,reserve in reserveMap.iteritems():if warehouse==16:continuetotalReserved = totalReserved+reserveif (totalAvailability-totalReserved)<=0:markReasonForMpItem(autoDecrementItem,'Our stock is not enough',Decision.AUTO_DECREMENT_FAILED)continueif (avgSalePerDay==0):markReasonForMpItem(autoDecrementItem,'Average sale per day is zero',Decision.AUTO_DECREMENT_FAILED)continuedaysOfStock = (float(totalAvailability-totalReserved))/avgSalePerDayif daysOfStock<2:markReasonForMpItem(autoDecrementItem,'Our stock is not enough',Decision.AUTO_DECREMENT_FAILED)continueautoDecrementItem.competitorEnoughStock = TrueautoDecrementItem.ourEnoughStock = TrueautoDecrementItem.avgSales = avgSalePerDayautoDecrementItem.decision = Decision.AUTO_DECREMENT_SUCCESSautoDecrementItem.reason = 'All conditions for auto decrement true'lgr.info("Auto decrement success for itemId "+str(autoDecrementItem.item_id)+" Days of stock "+str(daysOfStock)+" avg sales "+str(avgSalePerDay))session.commit()def fetchItemsForAutoIncrease(time):autoIncrementItems = session.query(MarketPlaceHistory).join((MarketplaceItems,MarketPlaceHistory.item_id==MarketplaceItems.itemId))\.filter(MarketPlaceHistory.timestamp==time).filter(MarketPlaceHistory.source==OrderSource.SNAPDEAL).filter(MarketPlaceHistory.competitiveCategory==CompetitionCategory.BUY_BOX)\.filter(MarketplaceItems.source==OrderSource.SNAPDEAL).filter(MarketplaceItems.autoIncrement==True).all()#autoIncrementItems = MarketplaceItems.query.filter(MarketplaceItems.autoIncrement==True).filter(MarketplaceItems.source==OrderSource.SNAPDEAL).all()inventory_client = InventoryClient().get_client()inventoryMap = inventory_client.getInventorySnapshot(0)for autoIncrementItem in autoIncrementItems:#mpHistory = MarketPlaceHistory.get_by(source=OrderSource.SNAPDEAL,item_id=autoIncrementItem.itemId,timestamp=time)if not autoIncrementItem.competitiveCategory == CompetitionCategory.BUY_BOX:markReasonForMpItem(autoIncrementItem,'Category is '+CompetitionCategory._VALUES_TO_NAMES.get(autoIncrementItem.competitiveCategory),Decision.AUTO_INCREMENT_FAILED)continueif autoIncrementItem.totalSeller==1 and autoIncrementItem.ourRank==1:markReasonForMpItem(autoIncrementItem,'We are the only seller',Decision.AUTO_INCREMENT_FAILED)continueif autoIncrementItem.proposedSellingPrice <= autoIncrementItem.ourSellingPrice:markReasonForMpItem(autoIncrementItem,'Proposed SP less than current SP',Decision.AUTO_INCREMENT_FAILED)continueoosStatus = inventory_client.getOosStatusesForXDaysForItem(autoIncrementItem.item_id,0,3)count,sale,daysOfStock = 0,0,0for obj in oosStatus:if not obj.is_oos:count+=1sale = sale+obj.num_ordersavgSalePerDay=0 if count==0 else (float(sale)/count)totalAvailability, totalReserved = 0,0if (not inventoryMap.has_key(autoIncrementItem.item_id)):markReasonForMpItem(autoIncrementItem,'Average sale per day is zero',Decision.AUTO_INCREMENT_FAILED)continueitemInventory=inventoryMap[autoIncrementItem.item_id]availableMap = itemInventory.availabilityreserveMap = itemInventory.reservedfor warehouse,availability in availableMap.iteritems():if warehouse==16:continuetotalAvailability = totalAvailability+availabilityfor warehouse,reserve in reserveMap.iteritems():if warehouse==16:continuetotalReserved = totalReserved+reserveif (totalAvailability-totalReserved)<=0:markReasonForMpItem(autoIncrementItem,'Our stock is 0',Decision.AUTO_INCREMENT_FAILED)continueif (avgSalePerDay==0):markReasonForMpItem(autoIncrementItem,'Average sale per day is zero',Decision.AUTO_INCREMENT_FAILED)continuedaysOfStock = (float(totalAvailability-totalReserved))/avgSalePerDayif daysOfStock>5:markReasonForMpItem(autoIncrementItem,'Our stock is enough',Decision.AUTO_INCREMENT_FAILED)continueautoIncrementItem.ourEnoughStock = FalseautoIncrementItem.avgSales = avgSalePerDayautoIncrementItem.decision = Decision.AUTO_INCREMENT_SUCCESSautoIncrementItem.reason = 'All conditions for auto increment true'lgr.info("Auto increment success for itemId "+str(autoIncrementItem.item_id)+" Days of stock "+str(daysOfStock)+" avg sales "+str(avgSalePerDay))session.commit()def markReasonForMpItem(mpHistory,reason,decision):mpHistory.decision = decisionmpHistory.reason = reasondef fetchDetails(supc_code):url="http://www.snapdeal.com/json/gvbps?supc=%s&catId=91"%(supc_code)print urltime.sleep(1)req = urllib2.Request(url)response = urllib2.urlopen(req)json_input = response.read()vendorInfo = json.loads(json_input)rank ,otherInventory ,ourInventory, ourOfferPrice, ourSp, iterator, secondLowestSellerSp, secondLowestSellerInventory, \lowestOfferPrice, secondLowestSellerOfferPrice = (0,)*10lowestSellerName , lowestSellerCode, secondLowestSellerName, secondLowestSellerCode=('',)*4for vendor in vendorInfo:if iterator == 0:lowestSellerName = vendor['vendorDisplayName']lowestSellerCode = vendor['vendorCode']try:lowestSp = vendor['sellingPriceBefIntCashBack']except:lowestSp = vendor['sellingPrice']lowestOfferPrice = vendor['sellingPrice']if iterator ==1:secondLowestSellerName = vendor['vendorDisplayName']secondLowestSellerCode =vendor['vendorCode']try:secondLowestSellerSp = vendor['sellingPriceBefIntCashBack']except:secondLowestSellerSp = vendor['sellingPrice']secondLowestSellerOfferPrice = vendor['sellingPrice']secondLowestSellerInventory = vendor['buyableInventory']if vendor['vendorDisplayName'] == 'MobilesnMore':ourInventory = vendor['buyableInventory']try:ourSp = vendor['sellingPriceBefIntCashBack']except:ourSp = vendor['sellingPrice']ourOfferPrice = vendor['sellingPrice']rank = iterator +1else:if rank==0:otherInventory = otherInventory +vendor['buyableInventory']iterator+=1snapdealDetails = __SnapdealDetails(ourSp,ourInventory,otherInventory,rank,lowestSellerName, lowestSellerCode,lowestSp,secondLowestSellerName,secondLowestSellerCode,secondLowestSellerSp,secondLowestSellerInventory,lowestOfferPrice,secondLowestSellerOfferPrice,ourOfferPrice,len(vendorInfo))return snapdealDetailsdef populateStuff():itemInfo = []items = session.query(SnapdealItem).all()spm = SourcePercentageMaster.get_by(source=OrderSource.SNAPDEAL)for snapdeal_item in items:it = Item.query.filter_by(id=snapdeal_item.item_id).one()category = Category.query.filter_by(id=it.category).one()snapdealItemInfo = __SnapdealItemInfo(snapdeal_item.supc, snapdeal_item.maxNlc,snapdeal_item.courierCost, it.id, it.product_group, it.brand, it.model_name, it.model_number, it.color, it.weight, category.parent_category_id, it.risky, snapdeal_item.warehouseId, None)itemInfo.append(snapdealItemInfo)return itemInfo, spmdef decideCategory(itemInfo,spm):cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionItems, negativeMargin = [],[],[],[],[],[]catalog_client = CatalogClient().get_client()inventory_client = InventoryClient().get_client()for val in itemInfo:try:snapdealDetails = fetchDetails(val.supc)except:exceptionItems.append(val)continuempItem = MarketplaceItems.get_by(itemId=val.item_id,source=OrderSource.SNAPDEAL)warehouse = inventory_client.getWarehouse(val.warehouseId)if snapdealDetails.rank==0:snapdealDetails.ourSp = mpItem.currentSpsnapdealDetails.ourOfferPrice = mpItem.currentSpourSp = mpItem.currentSpelse:ourSp = snapdealDetails.ourSpvatRate = catalog_client.getVatPercentageForItem(val.item_id, warehouse.stateId, snapdealDetails.ourSp)val.vatRate = vatRateif (snapdealDetails.rank==1):temp=[]temp.append(snapdealDetails)temp.append(val)if (snapdealDetails.secondLowestSellerOfferPrice == snapdealDetails.secondLowestSellerSp) and snapdealDetails.ourOfferPrice==snapdealDetails.ourSp:competitionBasis = 'SP'else:competitionBasis = 'TP'secondLowestTp=0 if snapdealDetails.totalSeller==1 else getOtherTp(snapdealDetails,val,spm)snapdealPricing = __SnapdealPricing(snapdealDetails.ourSp,getOurTp(snapdealDetails,val,spm,mpItem),None,getLowestPossibleTp(snapdealDetails,val,spm,mpItem),competitionBasis,secondLowestTp,getLowestPossibleSp(snapdealDetails,val,spm,mpItem))temp.append(snapdealPricing)temp.append(mpItem)buyBoxItems.append(temp)continuelowestTp = getOtherTp(snapdealDetails,val,spm)ourTp = getOurTp(snapdealDetails,val,spm,mpItem)lowestPossibleTp = getLowestPossibleTp(snapdealDetails,val,spm,mpItem)lowestPossibleSp = getLowestPossibleSp(snapdealDetails,val,spm,mpItem)if (ourTp<lowestPossibleTp):temp=[]temp.append(snapdealDetails)temp.append(val)snapdealPricing = __SnapdealPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,None,None,None)temp.append(snapdealPricing)negativeMargin.append(temp)continueif (snapdealDetails.lowestOfferPrice == snapdealDetails.lowestSp) and snapdealDetails.ourOfferPrice == snapdealDetails.ourSp:competitionBasis ='SP'else:competitionBasis ='TP'if competitionBasis=='SP':if snapdealDetails.lowestSp > lowestPossibleSp and snapdealDetails.ourInventory!=0:temp=[]temp.append(snapdealDetails)temp.append(val)snapdealPricing = __SnapdealPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,'SP',None,lowestPossibleSp)temp.append(snapdealPricing)temp.append(mpItem)competitive.append(temp)continueelse:if lowestTp > lowestPossibleTp and snapdealDetails.ourInventory!=0:temp=[]temp.append(snapdealDetails)temp.append(val)snapdealPricing = __SnapdealPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,'TP',None,lowestPossibleSp)temp.append(snapdealPricing)temp.append(mpItem)competitive.append(temp)continueif competitionBasis=='SP':if snapdealDetails.lowestSp > lowestPossibleSp and snapdealDetails.ourInventory==0:temp=[]temp.append(snapdealDetails)temp.append(val)snapdealPricing = __SnapdealPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,'SP',None,lowestPossibleSp)temp.append(snapdealPricing)temp.append(mpItem)competitiveNoInventory.append(temp)continueelse:if lowestTp > lowestPossibleTp and snapdealDetails.ourInventory==0:temp=[]temp.append(snapdealDetails)temp.append(val)snapdealPricing = __SnapdealPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,'TP',None,lowestPossibleSp)temp.append(snapdealPricing)temp.append(mpItem)competitiveNoInventory.append(temp)continuetemp=[]temp.append(snapdealDetails)temp.append(val)snapdealPricing = __SnapdealPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,competitionBasis,None,lowestPossibleSp)temp.append(snapdealPricing)temp.append(mpItem)cantCompete.append(temp)return cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionItems, negativeMargindef commitExceptionList(exceptionList,timestamp):for item in exceptionList:mpHistory = MarketPlaceHistory()mpHistory.item_id =item.item_idmpHistory.source = OrderSource.SNAPDEALmpHistory.competitiveCategory = CompetitionCategory.EXCEPTIONmpHistory.risky = item.riskympHistory.timestamp = timestampsession.commit()def commitNegativeMargin(negativeMargin,timestamp):for item in negativeMargin:snapdealDetails = item[0]snapdealItemInfo = item[1]snapdealPricing = item[2]mpHistory = MarketPlaceHistory()mpHistory.item_id = snapdealItemInfo.item_idmpHistory.source = OrderSource.SNAPDEALmpHistory.ourOfferPrice = snapdealDetails.ourOfferPricempHistory.ourSellingPrice = snapdealPricing.ourSpmpHistory.ourTp = snapdealPricing.ourTpmpHistory.lowestPossibleTp = snapdealPricing.lowestPossibleTpmpHistory.ourNlc = snapdealItemInfo.nlcmpHistory.ourInventory = snapdealDetails.ourInventorympHistory.otherInventory = snapdealDetails.otherInventorympHistory.ourRank = snapdealDetails.rankmpHistory.risky = snapdealItemInfo.riskympHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTpmpHistory.competitiveCategory = CompetitionCategory.NEGATIVE_MARGINmpHistory.totalSeller = snapdealDetails.totalSellermpHistory.timestamp = timestampsession.commit()def commitCompetitive(competitive,timestamp):for item in competitive:snapdealDetails = item[0]snapdealItemInfo = item[1]snapdealPricing = item[2]mpItem = item[3]mpHistory = MarketPlaceHistory()mpHistory.item_id = snapdealItemInfo.item_idmpHistory.source = OrderSource.SNAPDEALmpHistory.lowestTp = snapdealPricing.lowestTpmpHistory.lowestPossibleTp = snapdealPricing.lowestPossibleTpmpHistory.lowestPossibleSp = snapdealPricing.lowestPossibleSpmpHistory.ourInventory = snapdealDetails.ourInventorympHistory.otherInventory = snapdealDetails.otherInventorympHistory.ourRank = snapdealDetails.rankmpHistory.competitionBasis = CompetitionBasis._NAMES_TO_VALUES.get(snapdealPricing.competitionBasis)mpHistory.competitiveCategory = CompetitionCategory.COMPETITIVEmpHistory.risky = snapdealItemInfo.riskympHistory.lowestOfferPrice = snapdealDetails.lowestOfferPricempHistory.lowestSellingPrice = snapdealDetails.lowestSpmpHistory.lowestSellerName = snapdealDetails.lowestSellerNamempHistory.lowestSellerCode = snapdealDetails.lowestSellerCodempHistory.ourOfferPrice = snapdealDetails.ourOfferPricempHistory.ourSellingPrice = snapdealPricing.ourSpmpHistory.ourTp = snapdealPricing.ourTpmpHistory.ourNlc = snapdealItemInfo.nlcif (snapdealPricing.competitionBasis=='SP'):proposed_sp = max(snapdealDetails.lowestSp - max((10, snapdealDetails.lowestSp*0.001)), snapdealPricing.lowestPossibleSp)proposed_tp = getTargetTp(proposed_sp,mpItem)mpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tpelse:proposed_tp = max(snapdealPricing.lowestTp - max((10, snapdealPricing.lowestTp*0.001)), snapdealPricing.lowestPossibleTp)proposed_sp = getTargetSp(proposed_tp,mpItem)mpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tpmpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTpmpHistory.totalSeller = snapdealDetails.totalSellermpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(snapdealDetails.lowestOfferPrice,snapdealItemInfo.nlc))mpHistory.timestamp = timestampsession.commit()def commitCompetitiveNoInventory(competitiveNoInventory,timestamp):for item in competitiveNoInventory:snapdealDetails = item[0]snapdealItemInfo = item[1]snapdealPricing = item[2]mpItem = item[3]mpHistory = MarketPlaceHistory()mpHistory.item_id = snapdealItemInfo.item_idmpHistory.source = OrderSource.SNAPDEALmpHistory.lowestTp = snapdealPricing.lowestTpmpHistory.lowestPossibleTp = snapdealPricing.lowestPossibleTpmpHistory.lowestPossibleSp = snapdealPricing.lowestPossibleSpmpHistory.ourInventory = snapdealDetails.ourInventorympHistory.otherInventory = snapdealDetails.otherInventorympHistory.ourRank = snapdealDetails.rankmpHistory.competitionBasis = CompetitionBasis._NAMES_TO_VALUES.get(snapdealPricing.competitionBasis)mpHistory.competitiveCategory = CompetitionCategory.COMPETITIVE_NO_INVENTORYmpHistory.risky = snapdealItemInfo.riskympHistory.lowestOfferPrice = snapdealDetails.lowestOfferPricempHistory.lowestSellingPrice = snapdealDetails.lowestSpmpHistory.lowestSellerName = snapdealDetails.lowestSellerNamempHistory.lowestSellerCode = snapdealDetails.lowestSellerCodempHistory.ourOfferPrice = snapdealDetails.ourOfferPricempHistory.ourSellingPrice = snapdealPricing.ourSpmpHistory.ourTp = snapdealPricing.ourTpmpHistory.ourNlc = snapdealItemInfo.nlcif (snapdealPricing.competitionBasis=='SP'):proposed_sp = max(snapdealDetails.lowestSp - max((10, snapdealDetails.lowestSp*0.001)), snapdealPricing.lowestPossibleSp)proposed_tp = getTargetTp(proposed_sp,mpItem)mpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tpelse:proposed_tp = max(snapdealPricing.lowestTp - max((10, snapdealPricing.lowestTp*0.001)), snapdealPricing.lowestPossibleTp)proposed_sp = getTargetSp(proposed_tp,mpItem)mpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tpmpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTpmpHistory.totalSeller = snapdealDetails.totalSellermpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(snapdealDetails.lowestOfferPrice,snapdealItemInfo.nlc))mpHistory.timestamp = timestampsession.commit()def commitCantCompete(cantCompete,timestamp):for item in cantCompete:snapdealDetails = item[0]snapdealItemInfo = item[1]snapdealPricing = item[2]mpItem = item[3]mpHistory = MarketPlaceHistory()mpHistory.item_id = snapdealItemInfo.item_idmpHistory.source = OrderSource.SNAPDEALmpHistory.lowestTp = snapdealPricing.lowestTpmpHistory.lowestPossibleTp = snapdealPricing.lowestPossibleTpmpHistory.lowestPossibleSp = snapdealPricing.lowestPossibleSpmpHistory.ourInventory = snapdealDetails.ourInventorympHistory.otherInventory = snapdealDetails.otherInventorympHistory.ourRank = snapdealDetails.rankmpHistory.competitionBasis = CompetitionBasis._NAMES_TO_VALUES.get(snapdealPricing.competitionBasis)mpHistory.competitiveCategory = CompetitionCategory.CANT_COMPETEmpHistory.risky = snapdealItemInfo.riskympHistory.lowestOfferPrice = snapdealDetails.lowestOfferPricempHistory.lowestSellingPrice = snapdealDetails.lowestSpmpHistory.lowestSellerName = snapdealDetails.lowestSellerNamempHistory.lowestSellerCode = snapdealDetails.lowestSellerCodempHistory.ourOfferPrice = snapdealDetails.ourOfferPricempHistory.ourSellingPrice = snapdealPricing.ourSpmpHistory.ourTp = snapdealPricing.ourTpmpHistory.ourNlc = snapdealItemInfo.nlcif (snapdealPricing.competitionBasis=='SP'):proposed_sp = snapdealDetails.lowestSp - max(10, snapdealDetails.lowestSp*0.001)proposed_tp = getTargetTp(proposed_sp,mpItem)target_nlc = proposed_tp - snapdealPricing.lowestPossibleTp + snapdealItemInfo.nlcmpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tpmpHistory.targetNlc = target_nlcelse:proposed_tp = snapdealPricing.lowestTp - max(10, snapdealPricing.lowestTp*0.001)proposed_sp = getTargetSp(proposed_tp,mpItem)target_nlc = proposed_tp - snapdealPricing.lowestPossibleTp + snapdealItemInfo.nlcmpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tpmpHistory.targetNlc = target_nlcmpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTpmpHistory.totalSeller = snapdealDetails.totalSellermpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(snapdealDetails.lowestOfferPrice,snapdealItemInfo.nlc))mpHistory.timestamp = timestampsession.commit()def commitBuyBox(buyBoxItems,timestamp):for item in buyBoxItems:snapdealDetails = item[0]snapdealItemInfo = item[1]snapdealPricing = item[2]mpItem = item[3]mpHistory = MarketPlaceHistory()mpHistory.item_id = snapdealItemInfo.item_idmpHistory.source = OrderSource.SNAPDEALmpHistory.lowestPossibleTp = snapdealPricing.lowestPossibleTpmpHistory.lowestPossibleSp = snapdealPricing.lowestPossibleSpmpHistory.ourInventory = snapdealDetails.ourInventorympHistory.secondLowestInventory = snapdealDetails.secondLowestSellerInventorympHistory.ourRank = snapdealDetails.rankmpHistory.competitionBasis = CompetitionBasis._NAMES_TO_VALUES.get(snapdealPricing.competitionBasis)mpHistory.competitiveCategory = CompetitionCategory.BUY_BOXmpHistory.risky = snapdealItemInfo.riskympHistory.lowestOfferPrice = snapdealDetails.lowestOfferPricempHistory.lowestSellingPrice = snapdealDetails.lowestSpmpHistory.lowestSellerName = snapdealDetails.lowestSellerNamempHistory.lowestSellerCode = snapdealDetails.lowestSellerCodempHistory.ourOfferPrice = snapdealDetails.ourOfferPricempHistory.ourSellingPrice = snapdealPricing.ourSpmpHistory.ourTp = snapdealPricing.ourTpmpHistory.ourNlc = snapdealItemInfo.nlcmpHistory.secondLowestSellerName = snapdealDetails.secondLowestSellerNamempHistory.secondLowestSellerCode = snapdealDetails.secondLowestSellerCodempHistory.secondLowestSellingPrice = snapdealDetails.secondLowestSellerSpmpHistory.secondLowestOfferPrice = snapdealDetails.secondLowestSellerOfferPricempHistory.secondLowestTp = snapdealPricing.secondLowestSellerTpif (snapdealPricing.competitionBasis=='SP'):proposed_sp = max(snapdealDetails.secondLowestSellerSp - max((20, snapdealDetails.secondLowestSellerSp*0.002)), snapdealPricing.lowestPossibleSp)proposed_tp = getTargetTp(proposed_sp,mpItem)#target_nlc = proposed_tp - snapdealPricing.lowestPossibleTp + snapdealItemInfo.nlcmpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tp#mpHistory.targetNlc = target_nlcelse:proposed_tp = max(snapdealPricing.secondLowestSellerTp - max((20, snapdealPricing.secondLowestSellerTp*0.002)), snapdealPricing.lowestPossibleTp)proposed_sp = getTargetSp(proposed_tp,mpItem)#target_nlc = proposed_tp - snapdealPricing.lowestPossibleTp + snapdealItemInfo.nlcmpHistory.proposedSellingPrice = proposed_spmpHistory.proposedTp = proposed_tp#mpHistory.targetNlc = target_nlcmpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTpmpHistory.marginIncreasedPotential = proposed_tp - snapdealPricing.ourTpmpHistory.totalSeller = snapdealDetails.totalSellermpHistory.timestamp = timestampsession.commit()def getOtherTp(snapdealDetails,val,spm):if val.parent_category==10011:commissionPercentage = spm.competitorCommissionAccessoryelse:commissionPercentage = spm.competitorCommissionOtherif snapdealDetails.rank==1:return snapdealDetails.secondLowestSellerSp- snapdealDetails.secondLowestSellerSp*(commissionPercentage/100+spm.emiFee/100)*(1+(spm.serviceTax/100))-(val.courierCost+spm.closingFee)*(1+(spm.serviceTax/100));return snapdealDetails.lowestSp- snapdealDetails.lowestSp*(commissionPercentage/100+spm.emiFee/100)*(1+(spm.serviceTax/100))-(val.courierCost+spm.closingFee)*(1+(spm.serviceTax/100));def getLowestPossibleTp(snapdealDetails,val,spm,mpItem):if snapdealDetails.rank==0:return mpItem.minimumPossibleTpvat = (snapdealDetails.ourSp/(1+(val.vatRate/100))-(val.nlc/(1+(val.vatRate/100))))*(val.vatRate/100);inHouseCost = 15+vat+(mpItem.returnProvision/100)*snapdealDetails.ourSp+mpItem.otherCost;lowest_possible_tp = val.nlc+inHouseCost;return lowest_possible_tpdef getOurTp(snapdealDetails,val,spm,mpItem):if snapdealDetails.rank==0:return mpItem.currentTpreturn snapdealDetails.ourSp- snapdealDetails.ourSp*(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(val.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100));def getLowestPossibleSp(snapdealDetails,val,spm,mpItem):if snapdealDetails.rank==0:return mpItem.minimumPossibleSpreturn (val.nlc+(val.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))*(1+(val.vatRate/100))+(15+mpItem.otherCost)*(1+(val.vatRate)/100))/(1-(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))*(1+(val.vatRate)/100)-(mpItem.returnProvision/100)*(1+(val.vatRate)/100));def getTargetTp(targetSp,mpItem):return targetSp- targetSp*(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100));def getTargetSp(targetTp,mpItem):return float(targetTp+(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100)))/(1-((mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))))def getSalesPotential(lowestOfferPrice,ourNlc):if lowestOfferPrice - ourNlc < 0:return 'HIGH'elif (float(lowestOfferPrice - ourNlc))/lowestOfferPrice >=0 and (float(lowestOfferPrice - ourNlc))/lowestOfferPrice <=.02:return 'MEDIUM'else:return 'LOW'def main():itemInfo,spm= populateStuff()cantCompete, buyBoxItems, competitive, \competitiveNoInventory, exceptionItems, negativeMargin = decideCategory(itemInfo,spm)timestamp = datetime.now()commitExceptionList(exceptionItems,timestamp)commitCantCompete(cantCompete,timestamp)commitBuyBox(buyBoxItems,timestamp)commitCompetitive(competitive,timestamp)commitCompetitiveNoInventory(competitiveNoInventory,timestamp)commitNegativeMargin(negativeMargin,timestamp)fetchItemsForAutoDecrease(timestamp)fetchItemsForAutoIncrease(timestamp)if __name__ == '__main__':main()