Rev 12480 | Rev 12484 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from elixir import *from sqlalchemy.sql import or_ ,func, asc, desc, and_from shop2020.config.client.ConfigClient import ConfigClientfrom shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import Amazonlisted, Item, \Category, SourcePercentageMaster,SourceCategoryPercentage, SourceItemPercentage, AmazonPromotion, AmazonScrapingHistory, \ItemVatMaster, CategoryVatMasterfrom shop2020.thriftpy.model.v1.order.ttypes import OrderSourcefrom shop2020.thriftpy.model.v1.catalog.ttypes import CompetitionCategory, \Decision, RunType, AmazonPromotionTypefrom shop2020.model.v1.catalog.script import AmazonAsyncScraperfrom shop2020.clients.InventoryClient import InventoryClientfrom shop2020.clients.TransactionClient import TransactionClientimport timefrom time import sleepfrom datetime import date, datetime, timedeltaimport mathimport simplejson as jsonimport xlwtimport optparseimport sysfrom operator import itemgetterconfig_client = ConfigClient()host = config_client.get_property('staging_hostname')syncPrice=config_client.get_property('sync_price_on_marketplace')amazonAsinPrice={}amazonLongTermActivePromotions = {}amazonShortTermActivePromotions = {}saleMap = {}DataService.initialize(db_hostname=host)amScraper = AmazonAsyncScraper.Products("AKIAII3SGRXBJDPCHSGQ", "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg", "AF6E3O0VE0X4D")class __AmazonItemInfo:def __init__(self, asin, nlc, courierCost, sku, product_group, brand, model_name, model_number, color, weight, parent_category, risky, vatRate, runType, parent_category_name, sourcePercentage, ourInventory, state_id, otherCost):self.asin = asinself.nlc = nlcself.courierCost = courierCostself.sku = skuself.product_group = product_groupself.brand = brandself.model_name = model_nameself.model_number = model_numberself.color = colorself.weight = weightself.parent_category = parent_categoryself.risky = riskyself.vatRate = vatRateself.runType = runTypeself.parent_category_name = parent_category_nameself.sourcePercentage = sourcePercentageself.ourInventory = ourInventoryself.state_id = state_idself.otherCost = otherCostclass __AmazonDetails:def __init__(self, sku, ourSp, ourRank, lowestSellerName,lowestSellerSp,secondLowestSellerName, secondLowestSellerSp, thirdLowestSellerName, thirdLowestSellerSp, totalSeller, multipleListings, \promoPrice, isPromotion, lowestSellerShippingTime, lowestSellerRating, secondLowestSellerShippingTime, secondLowestSellerRating, thirdLowestSellerShippingTime , \thirdLowestSellerRating, lowestSellerType, secondLowestSellerType, thirdLowestSellerType):self.sku =skuself.ourSp = ourSpself.ourRank = ourRankself.lowestSellerName = lowestSellerNameself.lowestSellerSp = lowestSellerSpself.secondLowestSellerName = secondLowestSellerNameself.secondLowestSellerSp = secondLowestSellerSpself.thirdLowestSellerName = thirdLowestSellerNameself.thirdLowestSellerSp = thirdLowestSellerSpself.totalSeller = totalSellerself.multipleListings = multipleListingsself.promoPrice = promoPriceself.isPromotion = isPromotionself.lowestSellerShippingTime =lowestSellerShippingTimeself.lowestSellerRating = lowestSellerRatingself.secondLowestSellerShippingTime = secondLowestSellerShippingTimeself.secondLowestSellerRating = secondLowestSellerRatingself.thirdLowestSellerShippingTime= thirdLowestSellerShippingTimeself.thirdLowestSellerRating = thirdLowestSellerRatingself.lowestSellerType = lowestSellerTypeself.secondLowestSellerType = secondLowestSellerTypeself.thirdLowestSellerType = thirdLowestSellerTypeclass __AmazonPricing:def __init__(self, ourSp, lowestPossibleSp):self.ourSp = ourSpself.lowestPossibleSp = lowestPossibleSpclass __Promotion:def __init__(self, promoPrice, subsidy, promotionType,expiryDate):self.promoPrice = promoPriceself.subsidy = subsidyself.promotionType = promotionTypeself.expiryDate = expiryDatedef fetchItemsForAutoDecrease(time):successfulAutoDecrease = []autoDecrementItems = session.query(AmazonScrapingHistory).join((Amazonlisted,AmazonScrapingHistory.item_id==Amazonlisted.itemId))\.filter(AmazonScrapingHistory.timestamp==time).filter(or_(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETE,AmazonScrapingHistory.competitiveCategory==CompetitionCategory.COMPETITIVE, AmazonScrapingHistory.competitiveCategory==CompetitionCategory.ALMOST_COMPETE ))\.filter(Amazonlisted.autoDecrement==True).all()print len(autoDecrementItems)for autoDecrementItem in autoDecrementItems:if autoDecrementItem.warehouseLocation == 1:sku = 'FBA'+str(autoDecrementItem.item_id)else:sku = 'FBB'+str(autoDecrementItem.item_id)if amazonShortTermActivePromotions.has_key(sku):markReasonForItem(autoDecrementItem,'Item in short term promotion',Decision.AUTO_DECREMENT_FAILED)continueif math.ceil(autoDecrementItem.proposedSp) >= autoDecrementItem.ourSellingPrice:markReasonForItem(autoDecrementItem,'Proposed SP greater than or equal to current SP',Decision.AUTO_DECREMENT_FAILED)continueif autoDecrementItem.proposedSp < autoDecrementItem.lowestPossibleSp:markReasonForItem(autoDecrementItem,'Proposed SP less than lowest possible SP',Decision.AUTO_DECREMENT_FAILED)continuetry:daysOfStock = (float(autoDecrementItem.ourInventory))/autoDecrementItem.avgSaleexcept:daysOfStock = float("inf")if autoDecrementItem.competitiveCategory == CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETE:if daysOfStock < 20:markReasonForItem(autoDecrementItem,'Days of stock less than 20',Decision.AUTO_DECREMENT_FAILED)continueif autoDecrementItem.competitiveCategory == CompetitionCategory.COMPETITIVE and not autoDecrementItem.isPromotion:if autoDecrementItem.parentCategoryId in [10006,10009,11001]:if daysOfStock < 1 :markReasonForItem(autoDecrementItem,'Days of stock less than 1',Decision.AUTO_DECREMENT_FAILED)continueelse:if daysOfStock < 3:markReasonForItem(autoDecrementItem,'Days of stock less than 3',Decision.AUTO_DECREMENT_FAILED)continueif autoDecrementItem.competitiveCategory == CompetitionCategory.COMPETITIVE and autoDecrementItem.isPromotion:if autoDecrementItem.parentCategoryId in [10006,10009,11001]:if (amazonLongTermActivePromotions.get(sku).expiryDate - datetime.now()).days >2 and daysOfStock < 1 :markReasonForItem(autoDecrementItem,'Promo Item, expiry after 2 days or not enough stock',Decision.AUTO_DECREMENT_FAILED)continueelse:if (amazonLongTermActivePromotions.get(sku).expiryDate - datetime.now()).days >2 and daysOfStock < 3:markReasonForItem(autoDecrementItem,'Promo Item, expiry after 2 days or not enough stock',Decision.AUTO_DECREMENT_FAILED)continueautoDecrementItem.ourEnoughStock=TrueautoDecrementItem.decision = Decision.AUTO_DECREMENT_SUCCESSautoDecrementItem.reason = 'All conditions for auto decrement true'successfulAutoDecrease.append(autoDecrementItem)session.commit()session.close()return successfulAutoDecreasedef fetchItemsForAutoIncrease(time):successfulAutoIncrease = []autoIncrementItems = session.query(AmazonScrapingHistory).join((Amazonlisted,AmazonScrapingHistory.item_id==Amazonlisted.itemId))\.filter(AmazonScrapingHistory.timestamp==time).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.BUY_BOX)\.filter(Amazonlisted.autoIncrement==True).all()transaction_client = TransactionClient().get_client()print len(autoIncrementItems)for autoIncrementItem in autoIncrementItems:if autoIncrementItem.warehouseLocation == 1:sku = 'FBA'+str(autoIncrementItem.item_id)else:sku = 'FBB'+str(autoIncrementItem.item_id)if amazonShortTermActivePromotions.has_key(sku):markReasonForItem(autoIncrementItem,'Item in short term promotion',Decision.AUTO_INCREMENT_FAILED)continueif autoIncrementItem.totalSeller==1 and autoIncrementItem.ourRank==1:markReasonForItem(autoIncrementItem,'We are the only seller',Decision.AUTO_INCREMENT_FAILED)continueif autoIncrementItem.proposedSp <= autoIncrementItem.ourSellingPrice:markReasonForItem(autoIncrementItem,'Proposed SP less than current SP',Decision.AUTO_INCREMENT_FAILED)continueif autoIncrementItem.proposedSp >=10000 and autoIncrementItem.ourSellingPrice<10000:markReasonForItem(autoIncrementItem,'Proposed SP is greater than 10,000 and current sp is less than 10,000',Decision.AUTO_INCREMENT_FAILED)continueif autoIncrementItem.isPromotion and (math.ceil(autoIncrementItem.promoPrice+max(10,.01*autoIncrementItem.promoPrice)) > (amazonLongTermActivePromotions.get(sku)).promoPrice):markReasonForItem(autoIncrementItem,'Proposed SP cant be greater than promo price',Decision.AUTO_INCREMENT_FAILED)continueif autoIncrementItem.avgSale==0:markReasonForItem(autoIncrementItem,'Avg sale is 0',Decision.AUTO_INCREMENT_FAILED)continuedaysOfStock = (float(autoIncrementItem.ourInventory))/autoIncrementItem.avgSaleif daysOfStock > 5:markReasonForItem(autoIncrementItem,'Days of stock greater than 5',Decision.AUTO_INCREMENT_FAILED)continueantecedentPrice = session.query(AmazonScrapingHistory.ourSellingPrice).filter(AmazonScrapingHistory.item_id==autoIncrementItem.item_id).filter(AmazonScrapingHistory.timestamp>time-timedelta(days=1)).order_by(asc(AmazonScrapingHistory.timestamp)).first()if antecedentPrice is not None:if float(math.ceil(autoIncrementItem.ourSellingPrice+max(10,.01*autoIncrementItem.ourSellingPrice))-math.ceil(antecedentPrice[0]+max(10,.01*antecedentPrice[0])))/math.ceil(antecedentPrice[0]+max(10,.01*antecedentPrice[0]))>.02:markReasonForItem(autoIncrementItem,'Maximum price increase in last 24 hours should be 2%',Decision.AUTO_INCREMENT_FAILED)continuefbaSaleSnapshot = transaction_client.getAmazonFbaSalesLatestSnapshotForItemLocationWise(autoIncrementItem.item_id,autoIncrementItem.warehouseLocation)if getLastDaySale(fbaSaleSnapshot)<=2:markReasonForItem(autoIncrementItem,'Last day sale is less than 3',Decision.AUTO_INCREMENT_FAILED)continueautoIncrementItem.ourEnoughStock = FalseautoIncrementItem.decision = Decision.AUTO_INCREMENT_SUCCESSautoIncrementItem.reason = 'All conditions for auto increment true'successfulAutoIncrease.append(autoIncrementItem)session.commit()return successfulAutoIncreasedef markReasonForItem(amHistory,reason,decision):amHistory.decision = decisionamHistory.reason = reasondef calculateAverageSale(sku):count,sale = 0,0oosStatus = saleMap.get(sku)for obj in oosStatus:if not obj.isOutOfStock:count+=1sale = sale+obj.totalOrderCountavgSalePerDay=0 if count==0 else (float(sale)/count)return round(avgSalePerDay,2)def getOosString(oosStatus):lastNdaySale=""for obj in oosStatus:if obj.isOutOfStock:lastNdaySale += "X-"else:lastNdaySale += str(obj.totalOrderCount) + "-"return lastNdaySale[:-1]def getLastDaySale(fbaSaleSnapshot):if fbaSaleSnapshot.item_id==0:return 0else:return fbaSaleSnapshot.totalOrderCount#def syncAsin():## notListedOnAmazon = []## diffAsins = []## login_url = "https://sellercentral.amazon.in/gp/homepage.html"## br = SellerCentralInventoryReport.login(login_url)## report_url = "https://sellercentral.amazon.in/gp/upload-download-utils/requestReport.html?type=OpenListingReport&marketplaceID=44571&Request+Report="## br = SellerCentralInventoryReport.requestReport(br,report_url)## status_url="https://sellercentral.amazon.in/gp/upload-download-utils/reportStatusData.html"## br, page = SellerCentralInventoryReport.checkStatus(br,status_url)## br, batchId = SellerCentralInventoryReport.getReportBatchId(br,page)## print "*********************************"## print "Batch Id for request is ",batchId## print "*********************************"## ready = False## retryCount = 0## while not ready:## if retryCount == 10:## print "File not available for download after multiple retries"## sys.exit(1)## br, download_link = SellerCentralInventoryReport.downloadReport(br,batchId,status_url)## if download_link is not None:## ready= True## continue## print "File not ready for download yet.Will try again after 30 seconds."## retryCount+=1## time.sleep(30)## fPath = SellerCentralInventoryReport.fetchFile(download_link['href'],br,batchId)# fPath = "/tmp/9940651090.txt"# global amazonAsinPrice# for line in open(fPath):# l = line.split('\t')# if (str(l[0]).startswith('FBA') or str(l[0]).startswith('FBB')):# obj = __AmazonAsinPrice(l[1],l[2])# amazonAsinPrice[l[0]] = obj##Can be used to sync asins, not doing due to multiple asins corresponding to one itemId## systemAsins = session.query(Item,Amazonlisted).join((Amazonlisted,Item.id==Amazonlisted.itemId)).all()## for systemAsin in systemAsins:## item = systemAsin[0]## amListed = systemAsin[1]## if amazonAsinPrice.get('FBA'+str(item.id)) is None:## temp=[]## temp.append(item)## temp.append(amListed)## notListedOnAmazon.append(temp)## continue## else:## temp=[]## temp.append(item)## temp.append(amListed)## if item.asin!=((amazonAsinPrice.get('FBA'+str(item.id))).asin).strip():## diffAsins.append(temp)## continue#### for diffAsin in diffAsins:## item = diffAsin[0]## amListed = diffAsin[1]## item.asin = ((amazonAsinPrice.get('FBA'+str(item.id))).asin).strip()## amListed.asin = ((amazonAsinPrice.get('FBA'+str(item.id))).asin).strip()## session.commit()## session.close()def fetchFbaSale():global saleMaptransaction_client = TransactionClient().get_client()fbaSaleSnapshot = transaction_client.getAmazonFbaSalesSnapshotForDays(4)for saleSnapshot in fbaSaleSnapshot:if saleSnapshot.fcLocation == 0:if saleMap.has_key('FBA'+str(saleSnapshot.item_id)):temp = []val = saleMap.get('FBA'+str(saleSnapshot.item_id))for l in val:temp.append(l)temp.append(saleSnapshot)saleMap['FBA'+str(saleSnapshot.item_id)]=tempelse:temp = []temp.append(saleSnapshot)saleMap['FBA'+str(saleSnapshot.item_id)] = tempelse:if saleMap.has_key('FBB'+str(saleSnapshot.item_id)):temp = []val = saleMap.get('FBB'+str(saleSnapshot.item_id))for l in val:temp.append(l)saleMap['FBB'+str(saleSnapshot.item_id)]=temptemp.append(saleSnapshot)else:temp = []temp.append(saleSnapshot)saleMap['FBB'+str(saleSnapshot.item_id)] = tempdef computeCourierCost(weight):try:cCost = 10.0;slabs = int((weight*1000)/500-.001)for slab in range(0,slabs):cCost = cCost + 10.0;return cCost;except:return 10.0def populateStuff(time,runType):global amazonLongTermActivePromotionsglobal amazonShortTermActivePromotionsitemInfo = []inventory_client = InventoryClient().get_client()fbaAvailableInventorySnapshot = inventory_client.getAllAvailableAmazonFbaItemInventory()print len(fbaAvailableInventorySnapshot)for fbaInventoryItem in fbaAvailableInventorySnapshot:d_amazon_listed = Amazonlisted.get_by(itemId=fbaInventoryItem.item_id)if d_amazon_listed is None:continueif d_amazon_listed.overrrideWanlc:wanlc = d_amazon_listed.exceptionalWanlcelse:wanlc = inventory_client.getWanNlcForSource(fbaInventoryItem.item_id,OrderSource.AMAZON)it = Item.query.filter_by(id=fbaInventoryItem.item_id).one()category = Category.query.filter_by(id=it.category).one()parent_category = Category.query.filter_by(id=category.parent_category_id).first()scp = SourceCategoryPercentage.query.filter(SourceCategoryPercentage.category_id==it.category).filter(SourceCategoryPercentage.source==OrderSource.AMAZON).filter(SourceCategoryPercentage.startDate<=time).filter(SourceCategoryPercentage.expiryDate>=time).first()if scp is not None:sourcePercentage = scpelse:spm = SourcePercentageMaster.get_by(source=OrderSource.AMAZON)sourcePercentage = spmif fbaInventoryItem.location==0:sku = 'FBA'+str(fbaInventoryItem.item_id)state_id = 1elif fbaInventoryItem.location==1:sku = 'FBB'+str(fbaInventoryItem.item_id)state_id = 2else:continuecc = computeCourierCost(it.weight)amazonItemInfo = __AmazonItemInfo(None, wanlc,cc, sku, it.product_group, it.brand, it.model_name, it.model_number, it.color, it.weight, category.parent_category_id, it.risky, None, runType, parent_category.display_name,sourcePercentage,fbaInventoryItem.availability,state_id,d_amazon_listed.otherCost)itemInfo.append(amazonItemInfo)amPromotions = AmazonPromotion.query.filter(AmazonPromotion.startDate<=time).filter(AmazonPromotion.endDate>=time).filter(AmazonPromotion.promotionType==AmazonPromotionType.LONGTERM).filter(AmazonPromotion.promotionActive==True) \.group_by(AmazonPromotion.sku).order_by(desc(AmazonPromotion.addedOn)).all()for amPromotion in amPromotions:amazonLongTermActivePromotions[amPromotion.sku] = __Promotion(amPromotion.salePrice,amPromotion.subsidy,amPromotion.promotionType,amPromotion.endDate)amPromotions = AmazonPromotion.query.filter(AmazonPromotion.startDate<=time).filter(AmazonPromotion.endDate>=time).filter(AmazonPromotion.promotionType==AmazonPromotionType.SHORTTERM).filter(AmazonPromotion.promotionActive==True) \.group_by(AmazonPromotion.sku).order_by(desc(AmazonPromotion.addedOn)).all()for amPromotion in amPromotions:amazonShortTermActivePromotions[amPromotion.sku] = __Promotion(amPromotion.salePrice,amPromotion.subsidy,amPromotion.promotionType,amPromotion.endDate)session.close()print "No of items populated ",len(itemInfo)sleep(5)return itemInfodef getPriceAndAsin(itemInfo):skus = []for item in itemInfo:skus.append(item.sku)ourPricingForSku = amScraper.get_my_pricing_for_sku('A21TJRUUN4KGV', skus)for item in itemInfo:ourPricing = ourPricingForSku.get(item.sku)if ourPricing is None or len(ourPricing.keys())==0:item.ourSp = 0item.promoPrice = 0item.isPromotion = Falseitem.asin = ''else:item.ourSp = ourPricing.get('sellingPrice')item.promoPrice = ourPricing.get('promoPrice')item.isPromotion = ourPricing.get('promotion')item.asin = ourPricing.get('asin')def decideCategory(itemInfo):exceptionList, negativeMargin, cheapest, amongCheapestAndCanCompete, canCompete, almostCompete, cantCompete = [],[],[],[],[],[],[]skus = []for item in itemInfo:skus.append(item.sku)aggResponse = amScraper.get_competitive_pricing_for_sku('A21TJRUUN4KGV', skus)ourPricingForSku = amScraper.get_my_pricing_for_sku('A21TJRUUN4KGV', skus)for val in itemInfo:scrapInfo = aggResponse.get(val.sku)if scrapInfo is None or len(scrapInfo)==0 or val.nlc==0 or len(ourPricingForSku.get(val.sku).keys())==0:temp = []temp.append(val)if val.nlc==0 or val.nlc is None:print "WANLC is 0"temp.append("WANLC is 0")elif ourPricingForSku.get(val.sku) is None or ourPricingForSku.get(val.sku).keys()==0:print "Unable to fetch our price"temp.append("Unable to fetch our price")else:print "Unable to fetch competive pricing"temp.append("Unable to fetch competitive pricing")exceptionList.append(temp)continueval.ourSp = ourPricingForSku.get(val.sku).get('sellingPrice')val.promoPrice = ourPricingForSku.get(val.sku).get('promoPrice')val.isPromo = ourPricingForSku.get(val.sku).get('promotion')iterator = 0sku, lowestSellerName,secondLowestSellerName, thirdLowestSellerName = ('',)*4ourSp, ourRank, lowestSellerSp, secondLowestSellerSp, thirdLowestSellerSp, lowestPossibleSp = (0,)*6lowestSellerShippingTime, lowestSellerRating, secondLowestSellerShippingTime, secondLowestSellerRating, thirdLowestSellerShippingTime , \thirdLowestSellerRating, lowestSellerType, secondLowestSellerType, thirdLowestSellerType = (0,)*9isPromo = Falsesku = val.skumultipleListings = FalseourSkuDetails = ourPricingForSku.get(val.sku)if (ourSkuDetails.get('promotion')!=(amazonLongTermActivePromotions.has_key(val.sku) or amazonShortTermActivePromotions.has_key(val.sku))):temp = []temp.append(val)print "promo misconfigured"temp.append("Promo misconfigured")exceptionList.append(temp)continuescrapInfo.append(ourSkuDetails)sortedScrapInfo = sorted(scrapInfo, key=itemgetter('promoPrice','notOurSku'))for info in sortedScrapInfo:if not info['notOurSku']:ourSp = info['sellingPrice']promoPrice = info['promoPrice']isPromo = info['promotion']ourRank = iterator + 1if iterator == 0:lowestSellerSp = info['promoPrice']lowestSellerShippingTime = info['shippingTime']lowestSellerRating = info['rating']lowestSellerType = info['fulfillmentChannel']if iterator == 1:secondLowestSellerSp = info['promoPrice']secondLowestSellerShippingTime = info['shippingTime']secondLowestSellerRating = info['rating']secondLowestSellerType = info['fulfillmentChannel']if iterator == 2:thirdLowestSellerSp = info['promoPrice']thirdLowestSellerShippingTime = info['shippingTime']thirdLowestSellerRating = info['rating']thirdLowestSellerType = info['fulfillmentChannel']iterator += 1print "terminating iterator"print "Creating object am details",val.skuamDetails = __AmazonDetails(sku, float(ourSp), ourRank, lowestSellerName,float(lowestSellerSp),secondLowestSellerName, float(secondLowestSellerSp), thirdLowestSellerName, float(thirdLowestSellerSp),len(scrapInfo),multipleListings,promoPrice,isPromo, \lowestSellerShippingTime ,lowestSellerRating, secondLowestSellerShippingTime, secondLowestSellerRating, thirdLowestSellerShippingTime , thirdLowestSellerRating, lowestSellerType, secondLowestSellerType, thirdLowestSellerType)print "am details obj created"try:print "inside val getter"itemVatMaster = ItemVatMaster.query.filter(and_(ItemVatMaster.itemId==int(val.sku[3:]), ItemVatMaster.stateId==val.state_id)).first()if itemVatMaster is None:d_item = Item.query.filter_by(id=int(val.sku[3:])).first()if d_item is None:raiseelse:vatMaster = CategoryVatMaster.query.filter(and_(CategoryVatMaster.categoryId==d_item.category, CategoryVatMaster.minVal<=amDetails.promoPrice, CategoryVatMaster.maxVal>=amDetails.promoPrice, CategoryVatMaster.stateId == val.state_id)).first()if vatMaster is None:raiseelse:val.vatRate = vatMaster.vatPercentprint "vat fetched"else:val.vatRate = itemVatMaster.vatPercentageprint "vat fetched"except:print "vat exception"temp = []temp.append(val)temp.append("Vat not available")exceptionList.append(temp)continuelowestPossibleSp = getLowestPossibleSp(amDetails,val,val.sourcePercentage)print "Creating pricing obj"amPricing = __AmazonPricing(ourSp,lowestPossibleSp)if amDetails.promoPrice < amPricing.lowestPossibleSp:temp = []temp.append(val)temp.append(amDetails)temp.append(amPricing)negativeMargin.append(temp)continueif amDetails.ourRank==1:temp = []temp.append(val)temp.append(amDetails)temp.append(amPricing)cheapest.append(temp)continueif (amDetails.lowestSellerSp > amPricing.lowestPossibleSp) and ((((float(float(amDetails.promoPrice) - amDetails.lowestSellerSp))/float(amDetails.promoPrice))<=.01) or ((float(amDetails.promoPrice) - amDetails.lowestSellerSp)<=25)):temp = []temp.append(val)temp.append(amDetails)temp.append(amPricing)amongCheapestAndCanCompete.append(temp)continueif (amDetails.lowestSellerSp > amPricing.lowestPossibleSp):temp = []temp.append(val)temp.append(amDetails)temp.append(amPricing)canCompete.append(temp)continueif amDetails.lowestSellerSp*(1+.01) >= amPricing.lowestPossibleSp:temp = []temp.append(val)temp.append(amDetails)temp.append(amPricing)almostCompete.append(temp)continuetemp = []temp.append(val)temp.append(amDetails)temp.append(amPricing)cantCompete.append(temp)print "Created category..."return exceptionList, negativeMargin, cheapest, amongCheapestAndCanCompete, canCompete, almostCompete, cantCompetedef getLowestPossibleSp(amazonDetails,val,spm):lowestPossibleSp = (val.nlc+(val.courierCost)*(1+(spm.serviceTax/100))*(1+(val.vatRate/100))+(15+val.otherCost)*(1+(val.vatRate)/100))/(1-(spm.commission/100+spm.emiFee/100)*(1+(spm.serviceTax/100))*(1+(val.vatRate)/100)-(spm.returnProvision/100)*(1+(val.vatRate)/100));if val.isPromo:if amazonLongTermActivePromotions.has_key(val.sku):subsidy = (amazonLongTermActivePromotions.get(val.sku)).subsidyelse:subsidy = (amazonShortTermActivePromotions.get(val.sku)).subsidylowestPossibleSp = lowestPossibleSp - subsidyreturn round(lowestPossibleSp,2)def getTargetTp(targetSp,spm,val):targetTp = targetSp- targetSp*(spm.commission/100+spm.emiFee/100)*(1+(spm.serviceTax/100))-(val.courierCost)*(1+(spm.serviceTax/100))return round(targetTp,2)def commitExceptionList(exceptionList,timestamp,runType):for exceptionItem in exceptionList:val = exceptionItem[0]reason = exceptionItem[1]amazonScrapingHistory = AmazonScrapingHistory()amazonScrapingHistory.item_id = val.sku[3:]amazonScrapingHistory.warehouseLocation = val.state_idamazonScrapingHistory.parentCategoryId = val.parent_categoryamazonScrapingHistory.reason = reasonamazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)amazonScrapingHistory.competitiveCategory = CompetitionCategory.EXCEPTIONamazonScrapingHistory.timestamp = timestampsession.commit()def commitNegativeMargin(negativeMargin,timestamp,runType):for negativeMarginItem in negativeMargin:val = negativeMarginItem[0]amDetails = negativeMarginItem[1]amPricing = negativeMarginItem[2]spm = val.sourcePercentageamazonScrapingHistory = AmazonScrapingHistory()amazonScrapingHistory.item_id = val.sku[3:]amazonScrapingHistory.asin = val.asinamazonScrapingHistory.warehouseLocation = val.state_idamazonScrapingHistory.parentCategoryId = val.parent_categoryamazonScrapingHistory.ourSellingPrice = amDetails.ourSpamazonScrapingHistory.promoPrice = amDetails.promoPriceamazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSpamazonScrapingHistory.ourRank = amDetails.ourRankamazonScrapingHistory.ourInventory = val.ourInventoryamazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSpamazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTimeamazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRatingamazonScrapingHistory.lowestSellerType = amDetails.lowestSellerTypeamazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSpamazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTimeamazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRatingamazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerTypeamazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSpamazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTimeamazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRatingamazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerTypeamazonScrapingHistory.wanlc = val.nlcamazonScrapingHistory.otherCost = val.otherCostamazonScrapingHistory.commission = spm.commissionamazonScrapingHistory.competitorCommission = spm.competitorCommissionOtheramazonScrapingHistory.returnProvision = spm.returnProvisionamazonScrapingHistory.courierCost = val.courierCostamazonScrapingHistory.risky = val.riskyamazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)amazonScrapingHistory.totalSeller = amDetails.totalSelleramazonScrapingHistory.competitiveCategory = CompetitionCategory.NEGATIVE_MARGINamazonScrapingHistory.timestamp = timestampamazonScrapingHistory.multipleListings = amDetails.multipleListingsamazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five daysamazonScrapingHistory.isPromotion = val.isPromosession.commit()def commitCheapest(cheapest,timestamp,runType):for cheapestItem in cheapest:val = cheapestItem[0]amDetails = cheapestItem[1]amPricing = cheapestItem[2]spm = val.sourcePercentageamazonScrapingHistory = AmazonScrapingHistory()amazonScrapingHistory.item_id = val.sku[3:]amazonScrapingHistory.asin = val.asinamazonScrapingHistory.warehouseLocation = val.state_idamazonScrapingHistory.parentCategoryId = val.parent_categoryamazonScrapingHistory.ourSellingPrice = amDetails.ourSpamazonScrapingHistory.promoPrice = amDetails.promoPriceamazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSpamazonScrapingHistory.ourRank = amDetails.ourRankamazonScrapingHistory.ourInventory = val.ourInventoryamazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSpamazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTimeamazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRatingamazonScrapingHistory.lowestSellerType = amDetails.lowestSellerTypeamazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSpamazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTimeamazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRatingamazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerTypeamazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSpamazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTimeamazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRatingamazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerTypeamazonScrapingHistory.otherCost = val.otherCostamazonScrapingHistory.wanlc = val.nlcamazonScrapingHistory.commission = spm.commissionamazonScrapingHistory.competitorCommission = spm.competitorCommissionOtheramazonScrapingHistory.returnProvision = spm.returnProvisionamazonScrapingHistory.courierCost = val.courierCostamazonScrapingHistory.risky = val.riskyamazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)amazonScrapingHistory.totalSeller = amDetails.totalSelleramazonScrapingHistory.competitiveCategory = CompetitionCategory.BUY_BOXamazonScrapingHistory.timestamp = timestampamazonScrapingHistory.multipleListings = amDetails.multipleListingsproposed_sp = max(amDetails.secondLowestSellerSp - max((20, amDetails.secondLowestSellerSp*0.002)), amPricing.lowestPossibleSp)if amazonScrapingHistory.isPromotion:if amazonLongTermActivePromotions.has_key(val.sku):proposed_sp = min(proposed_sp,(amazonLongTermActivePromotions.get(val.sku)).salePrice)else:proposed_sp = min(proposed_sp,(amazonShortTermActivePromotions.get(val.sku)).salePrice)#proposed_tp = getTargetTp(proposed_sp,spm,val)amazonScrapingHistory.proposedSp = proposed_sp#amazonScrapingHistory.proposedTp = proposed_tp#amazonScrapingHistory.marginIncreasedPotential = proposed_tp - amPricing.ourTpamazonScrapingHistory.multipleListings = amDetails.multipleListingsamazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five daysamazonScrapingHistory.isPromotion = val.isPromosession.commit()def commitAmongCheapestAndCanCompete(amongCheapestAndCanCompete,timestamp,runType):for amongCheapestAndCanCompeteItem in amongCheapestAndCanCompete:val = amongCheapestAndCanCompeteItem[0]amDetails = amongCheapestAndCanCompeteItem[1]amPricing = amongCheapestAndCanCompeteItem[2]spm = val.sourcePercentageamazonScrapingHistory = AmazonScrapingHistory()amazonScrapingHistory.item_id = val.sku[3:]amazonScrapingHistory.asin = val.asinamazonScrapingHistory.warehouseLocation = val.state_idamazonScrapingHistory.parentCategoryId = val.parent_categoryamazonScrapingHistory.ourSellingPrice = amDetails.ourSpamazonScrapingHistory.promoPrice = amDetails.promoPriceamazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSpamazonScrapingHistory.ourRank = amDetails.ourRankamazonScrapingHistory.ourInventory = val.ourInventoryamazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSpamazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTimeamazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRatingamazonScrapingHistory.lowestSellerType = amDetails.lowestSellerTypeamazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSpamazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTimeamazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRatingamazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerTypeamazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSpamazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTimeamazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRatingamazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerTypeamazonScrapingHistory.otherCost = val.otherCostamazonScrapingHistory.wanlc = val.nlcamazonScrapingHistory.commission = spm.commissionamazonScrapingHistory.competitorCommission = spm.competitorCommissionOtheramazonScrapingHistory.returnProvision = spm.returnProvisionamazonScrapingHistory.courierCost = val.courierCostamazonScrapingHistory.risky = val.riskyamazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)amazonScrapingHistory.totalSeller = amDetails.totalSelleramazonScrapingHistory.competitiveCategory = CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETEamazonScrapingHistory.timestamp = timestampamazonScrapingHistory.multipleListings = amDetails.multipleListingsproposed_sp = max(amDetails.lowestSellerSp - max((5, amDetails.lowestSellerSp*0.001)), amPricing.lowestPossibleSp)#proposed_tp = getTargetTp(proposed_sp,spm,val)amazonScrapingHistory.proposedSp = proposed_sp#amazonScrapingHistory.proposedTp = proposed_tpamazonScrapingHistory.multipleListings = amDetails.multipleListingsamazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five daysamazonScrapingHistory.isPromotion = val.isPromosession.commit()def commitCanCompete(canCompete,timestamp,runType):for canCompeteItem in canCompete:val = canCompeteItem[0]amDetails = canCompeteItem[1]amPricing = canCompeteItem[2]spm = val.sourcePercentageamazonScrapingHistory = AmazonScrapingHistory()amazonScrapingHistory.item_id = val.sku[3:]amazonScrapingHistory.asin = val.asinamazonScrapingHistory.warehouseLocation = val.state_idamazonScrapingHistory.parentCategoryId = val.parent_categoryamazonScrapingHistory.ourSellingPrice = amDetails.ourSpamazonScrapingHistory.promoPrice = amDetails.promoPriceamazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSpamazonScrapingHistory.ourRank = amDetails.ourRankamazonScrapingHistory.ourInventory = val.ourInventoryamazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSpamazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTimeamazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRatingamazonScrapingHistory.lowestSellerType = amDetails.lowestSellerTypeamazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSpamazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTimeamazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRatingamazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerTypeamazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSpamazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTimeamazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRatingamazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerTypeamazonScrapingHistory.otherCost = val.otherCostamazonScrapingHistory.wanlc = val.nlcamazonScrapingHistory.commission = spm.commissionamazonScrapingHistory.competitorCommission = spm.competitorCommissionOtheramazonScrapingHistory.returnProvision = spm.returnProvisionamazonScrapingHistory.courierCost = val.courierCostamazonScrapingHistory.risky = val.riskyamazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)amazonScrapingHistory.totalSeller = amDetails.totalSelleramazonScrapingHistory.competitiveCategory = CompetitionCategory.COMPETITIVEamazonScrapingHistory.timestamp = timestampamazonScrapingHistory.multipleListings = amDetails.multipleListingsproposed_sp = max(amDetails.lowestSellerSp - max((5, amDetails.lowestSellerSp*0.001)), amPricing.lowestPossibleSp)#proposed_tp = getTargetTp(proposed_sp,spm,val)amazonScrapingHistory.proposedSp = proposed_sp#amazonScrapingHistory.proposedTp = proposed_tpamazonScrapingHistory.multipleListings = amDetails.multipleListingsamazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five daysamazonScrapingHistory.isPromotion = val.isPromosession.commit()def commitAlmostCompete(almostCompete,timestamp,runType):for almostCompeteItem in almostCompete:val = almostCompeteItem[0]amDetails = almostCompeteItem[1]amPricing = almostCompeteItem[2]spm = val.sourcePercentageamazonScrapingHistory = AmazonScrapingHistory()amazonScrapingHistory.item_id = val.sku[3:]amazonScrapingHistory.asin = val.asinamazonScrapingHistory.warehouseLocation = val.state_idamazonScrapingHistory.parentCategoryId = val.parent_categoryamazonScrapingHistory.ourSellingPrice = amDetails.ourSpamazonScrapingHistory.promoPrice = amDetails.promoPriceamazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSpamazonScrapingHistory.ourRank = amDetails.ourRankamazonScrapingHistory.ourInventory = val.ourInventoryamazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSpamazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTimeamazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRatingamazonScrapingHistory.lowestSellerType = amDetails.lowestSellerTypeamazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSpamazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTimeamazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRatingamazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerTypeamazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSpamazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTimeamazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRatingamazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerTypeamazonScrapingHistory.otherCost = val.otherCostamazonScrapingHistory.wanlc = val.nlcamazonScrapingHistory.commission = spm.commissionamazonScrapingHistory.competitorCommission = spm.competitorCommissionOtheramazonScrapingHistory.returnProvision = spm.returnProvisionamazonScrapingHistory.courierCost = val.courierCostamazonScrapingHistory.risky = val.riskyamazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)amazonScrapingHistory.totalSeller = amDetails.totalSelleramazonScrapingHistory.competitiveCategory = CompetitionCategory.ALMOST_COMPETEamazonScrapingHistory.timestamp = timestampamazonScrapingHistory.multipleListings = amDetails.multipleListingsproposed_sp = min(amDetails.lowestSellerSp*(1+.01),amPricing.lowestPossibleSp)#proposed_tp = getTargetTp(proposed_sp,spm,val)#target_nlc = proposed_tp - amPricing.lowestPossibleTp + val.nlcamazonScrapingHistory.proposedSp = proposed_sp#amazonScrapingHistory.proposedTp = proposed_tp#amazonScrapingHistory.targetNlc = target_nlcamazonScrapingHistory.multipleListings = amDetails.multipleListingsamazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five daysamazonScrapingHistory.isPromotion = val.isPromosession.commit()def commitCantCompete(cantCompete, timestamp,runType):for cantCompeteItem in cantCompete:val = cantCompeteItem[0]amDetails = cantCompeteItem[1]amPricing = cantCompeteItem[2]spm = val.sourcePercentageamazonScrapingHistory = AmazonScrapingHistory()amazonScrapingHistory.item_id = val.sku[3:]amazonScrapingHistory.asin = val.asinamazonScrapingHistory.warehouseLocation = val.state_idamazonScrapingHistory.parentCategoryId = val.parent_categoryamazonScrapingHistory.ourSellingPrice = amDetails.ourSpamazonScrapingHistory.promoPrice = amDetails.promoPriceamazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSpamazonScrapingHistory.ourRank = amDetails.ourRankamazonScrapingHistory.ourInventory = val.ourInventoryamazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSpamazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTimeamazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRatingamazonScrapingHistory.lowestSellerType = amDetails.lowestSellerTypeamazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSpamazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTimeamazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRatingamazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerTypeamazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSpamazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTimeamazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRatingamazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerTypeamazonScrapingHistory.otherCost = val.otherCostamazonScrapingHistory.wanlc = val.nlcamazonScrapingHistory.commission = spm.commissionamazonScrapingHistory.competitorCommission = spm.competitorCommissionOtheramazonScrapingHistory.returnProvision = spm.returnProvisionamazonScrapingHistory.courierCost = val.courierCostamazonScrapingHistory.risky = val.riskyamazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)amazonScrapingHistory.totalSeller = amDetails.totalSelleramazonScrapingHistory.competitiveCategory = CompetitionCategory.CANT_COMPETEamazonScrapingHistory.timestamp = timestampamazonScrapingHistory.multipleListings = amDetails.multipleListingsproposed_sp = amDetails.lowestSellerSp - max(5, amDetails.lowestSellerSp*0.001)#proposed_tp = getTargetTp(proposed_sp,spm,val)#target_nlc = proposed_tp - amPricing.lowestPossibleTp + val.nlcamazonScrapingHistory.proposedSp = proposed_sp#amazonScrapingHistory.proposedTp = proposed_tp#amazonScrapingHistory.targetNlc = target_nlcamazonScrapingHistory.multipleListings = amDetails.multipleListingsamazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five daysamazonScrapingHistory.isPromotion = val.isPromosession.commit()def markAutoFavourites(time):nowAutoFav = []previouslyAutoFav = []stockList = []saleList = []items = session.query(func.sum(AmazonScrapingHistory.ourInventory),AmazonScrapingHistory.item_id).group_by(AmazonScrapingHistory.item_id).all()allItems = session.query(Amazonlisted).all()for item in items:reason = ""if item[0]>=5:stockList.append(item[1])for sku, val in saleMap.iteritems():totalSale = 0item_id = sku.replace('FBA','').replace('FBB','')val =saleMap.get('FBA'+str(item_id))if val is not None:for sale in val:totalSale += sale.totalOrderCountval =saleMap.get('FBB'+str(item_id))if val is not None:for sale in val:totalSale += sale.totalOrderCountif totalSale > 0:saleList.append(item_id)for aItem in allItems:reason = ""toMark = Falseif aItem.itemId in saleList:toMark = Truereason+="Total FC sale is greater than 1 for last five days.."if aItem.itemId in stockList:toMark = Truereason+="Item is present in buy box in last 3 days"if not aItem.autoFavourite:print "Item is not under auto favourite"if toMark:temp=[]temp.append(aItem.itemId)temp.append(reason)nowAutoFav.append(temp)if (not toMark) and aItem.autoFavourite:previouslyAutoFav.append(aItem.itemId)aItem.autoFavourite = toMarksession.commit()return previouslyAutoFav, nowAutoFavdef writeReport(timestamp,autoDecreaseItems,autoIncreaseItems,previousAutoFav,nowAutoFav,runType):wbk = xlwt.Workbook()sheet = wbk.add_sheet('Can\'t Compete')xstr = lambda s: s or ""heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatsheet.write(0, 0, "Item Id", heading_xf)sheet.write(0, 1, "Amazon Sku", heading_xf)sheet.write(0, 2, "Asin", heading_xf)sheet.write(0, 3, "Location", heading_xf)sheet.write(0, 4, "Brand", heading_xf)sheet.write(0, 5, "Product Name", heading_xf)sheet.write(0, 6, "Weight", heading_xf)sheet.write(0, 7, "Courier Cost", heading_xf)sheet.write(0, 8, "Our SP", heading_xf)sheet.write(0, 9, "Promo Price", heading_xf)sheet.write(0, 10, "Is Promotion", heading_xf)sheet.write(0, 11, "Lowest Possible SP", heading_xf)sheet.write(0, 12, "Rank", heading_xf)sheet.write(0, 13, "Our Inventory", heading_xf)sheet.write(0, 14, "Lowest Seller SP", heading_xf)sheet.write(0, 15, "Lowest Seller Rating", heading_xf)sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)sheet.write(0, 23, "Other Cost", heading_xf)sheet.write(0, 24, "WANLC", heading_xf)sheet.write(0, 25, "Commission", heading_xf)sheet.write(0, 26, "Competitor Commission", heading_xf)sheet.write(0, 27, "Return Provision", heading_xf)sheet.write(0, 28, "Margin", heading_xf)sheet.write(0, 29, "Risky", heading_xf)sheet.write(0, 30, "Proposed Sp", heading_xf)sheet.write(0, 31, "Avg Sale", heading_xf)sheet.write(0, 32, "Sales History", heading_xf)sheet.write(0, 33, "Decision", heading_xf)sheet.write(0, 34, "Reason", heading_xf)sheet.write(0, 35, "Updated Price", heading_xf)sheet_iterator = 1cantCompeteItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.CANT_COMPETE).filter(AmazonScrapingHistory.timestamp==timestamp).all()for cantCompeteItem in cantCompeteItems:amScraping = cantCompeteItem[0]item = cantCompeteItem[1]sheet.write(sheet_iterator, 0, amScraping.item_id)if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'sheet.write(sheet_iterator, 1, sku)sheet.write(sheet_iterator, 2, amScraping.asin)sheet.write(sheet_iterator, 3, loc)sheet.write(sheet_iterator, 4, item.brand)sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))sheet.write(sheet_iterator, 6, item.weight)sheet.write(sheet_iterator, 7, amScraping.courierCost)sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)sheet.write(sheet_iterator, 9, amScraping.promoPrice)if amScraping.isPromotion:sheet.write(sheet_iterator, 10, "Yes")else:sheet.write(sheet_iterator, 10, "Yes")sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)if amScraping.ourRank > 3:sheet.write(sheet_iterator, 12, 'Greater than 3')else:sheet.write(sheet_iterator, 12, amScraping.ourRank)sheet.write(sheet_iterator, 13, amScraping.ourInventory)sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)sheet.write(sheet_iterator, 23, amScraping.otherCost)sheet.write(sheet_iterator, 24, amScraping.wanlc)sheet.write(sheet_iterator, 25, amScraping.commission)sheet.write(sheet_iterator, 26, amScraping.competitorCommission)sheet.write(sheet_iterator, 27, amScraping.returnProvision)sheet.write(sheet_iterator, 28, round(amScraping.ourSellingPrice - amScraping.lowestPossibleSp))sheet.write(sheet_iterator, 29, item.risky)sheet.write(sheet_iterator, 30, amScraping.proposedSp)sheet.write(sheet_iterator, 31, amScraping.avgSale)sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))if amScraping.decision is None:sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')sheet_iterator+=1continuesheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))sheet.write(sheet_iterator, 34, amScraping.reason)if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.ourSellingPrice+max(10,.01*amScraping.ourSellingPrice)))sheet_iterator+=1sheet = wbk.add_sheet('Competitive')xstr = lambda s: s or ""heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatsheet.write(0, 0, "Item Id", heading_xf)sheet.write(0, 1, "Amazon Sku", heading_xf)sheet.write(0, 2, "Asin", heading_xf)sheet.write(0, 3, "Location", heading_xf)sheet.write(0, 4, "Brand", heading_xf)sheet.write(0, 5, "Product Name", heading_xf)sheet.write(0, 6, "Weight", heading_xf)sheet.write(0, 7, "Courier Cost", heading_xf)sheet.write(0, 8, "Our SP", heading_xf)sheet.write(0, 9, "Promo Price", heading_xf)sheet.write(0, 10, "Is Promotion", heading_xf)sheet.write(0, 11, "Lowest Possible SP", heading_xf)sheet.write(0, 12, "Rank", heading_xf)sheet.write(0, 13, "Our Inventory", heading_xf)sheet.write(0, 14, "Lowest Seller SP", heading_xf)sheet.write(0, 15, "Lowest Seller Rating", heading_xf)sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)sheet.write(0, 23, "Other Cost", heading_xf)sheet.write(0, 24, "WANLC", heading_xf)sheet.write(0, 25, "Commission", heading_xf)sheet.write(0, 26, "Competitor Commission", heading_xf)sheet.write(0, 27, "Return Provision", heading_xf)sheet.write(0, 28, "Margin", heading_xf)sheet.write(0, 29, "Risky", heading_xf)sheet.write(0, 30, "Proposed Sp", heading_xf)sheet.write(0, 31, "Avg Sale", heading_xf)sheet.write(0, 32, "Sales History", heading_xf)sheet.write(0, 33, "Decision", heading_xf)sheet.write(0, 34, "Reason", heading_xf)sheet.write(0, 35, "Updated Price", heading_xf)sheet_iterator = 1competitiveItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.COMPETITIVE).filter(AmazonScrapingHistory.timestamp==timestamp).all()for competitiveItem in competitiveItems:amScraping = competitiveItem[0]item = competitiveItem[1]sheet.write(sheet_iterator, 0, amScraping.item_id)if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'sheet.write(sheet_iterator, 1, sku)sheet.write(sheet_iterator, 2, amScraping.asin)sheet.write(sheet_iterator, 3, loc)sheet.write(sheet_iterator, 4, item.brand)sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))sheet.write(sheet_iterator, 6, item.weight)sheet.write(sheet_iterator, 7, amScraping.courierCost)sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)sheet.write(sheet_iterator, 9, amScraping.promoPrice)if amScraping.isPromotion:sheet.write(sheet_iterator, 10, "Yes")else:sheet.write(sheet_iterator, 10, "Yes")sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)if amScraping.ourRank > 3:sheet.write(sheet_iterator, 12, 'Greater than 3')else:sheet.write(sheet_iterator, 12, amScraping.ourRank)sheet.write(sheet_iterator, 13, amScraping.ourInventory)sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)sheet.write(sheet_iterator, 23, amScraping.otherCost)sheet.write(sheet_iterator, 24, amScraping.wanlc)sheet.write(sheet_iterator, 25, amScraping.commission)sheet.write(sheet_iterator, 26, amScraping.competitorCommission)sheet.write(sheet_iterator, 27, amScraping.returnProvision)sheet.write(sheet_iterator, 28, round(amScraping.ourSellingPrice - amScraping.lowestPossibleSp))sheet.write(sheet_iterator, 29, item.risky)sheet.write(sheet_iterator, 30, amScraping.proposedSp)sheet.write(sheet_iterator, 31, amScraping.avgSale)sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))if amScraping.decision is None:sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')sheet_iterator+=1continuesheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))sheet.write(sheet_iterator, 34, amScraping.reason)if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.ourSellingPrice+max(10,.01*amScraping.ourSellingPrice)))sheet_iterator+=1sheet = wbk.add_sheet('Almost Competitive')xstr = lambda s: s or ""heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatsheet.write(0, 0, "Item Id", heading_xf)sheet.write(0, 1, "Amazon Sku", heading_xf)sheet.write(0, 2, "Asin", heading_xf)sheet.write(0, 3, "Location", heading_xf)sheet.write(0, 4, "Brand", heading_xf)sheet.write(0, 5, "Product Name", heading_xf)sheet.write(0, 6, "Weight", heading_xf)sheet.write(0, 7, "Courier Cost", heading_xf)sheet.write(0, 8, "Our SP", heading_xf)sheet.write(0, 9, "Promo Price", heading_xf)sheet.write(0, 10, "Is Promotion", heading_xf)sheet.write(0, 11, "Lowest Possible SP", heading_xf)sheet.write(0, 12, "Rank", heading_xf)sheet.write(0, 13, "Our Inventory", heading_xf)sheet.write(0, 14, "Lowest Seller SP", heading_xf)sheet.write(0, 15, "Lowest Seller Rating", heading_xf)sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)sheet.write(0, 23, "Other Cost", heading_xf)sheet.write(0, 24, "WANLC", heading_xf)sheet.write(0, 25, "Commission", heading_xf)sheet.write(0, 26, "Competitor Commission", heading_xf)sheet.write(0, 27, "Return Provision", heading_xf)sheet.write(0, 28, "Margin", heading_xf)sheet.write(0, 29, "Risky", heading_xf)sheet.write(0, 30, "Proposed Sp", heading_xf)sheet.write(0, 31, "Avg Sale", heading_xf)sheet.write(0, 32, "Sales History", heading_xf)sheet.write(0, 33, "Decision", heading_xf)sheet.write(0, 34, "Reason", heading_xf)sheet.write(0, 35, "Updated Price", heading_xf)sheet_iterator = 1almostCompetitiveItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.ALMOST_COMPETE).filter(AmazonScrapingHistory.timestamp==timestamp).all()for almostCompetitiveItem in almostCompetitiveItems:amScraping = almostCompetitiveItem[0]item = almostCompetitiveItem[1]sheet.write(sheet_iterator, 0, amScraping.item_id)if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'amScraping = competitiveItem[0]item = competitiveItem[1]if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'sheet.write(sheet_iterator, 1, sku)sheet.write(sheet_iterator, 2, amScraping.asin)sheet.write(sheet_iterator, 3, loc)sheet.write(sheet_iterator, 4, item.brand)sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))sheet.write(sheet_iterator, 6, item.weight)sheet.write(sheet_iterator, 7, amScraping.courierCost)sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)sheet.write(sheet_iterator, 9, amScraping.promoPrice)if amScraping.isPromotion:sheet.write(sheet_iterator, 10, "Yes")else:sheet.write(sheet_iterator, 10, "Yes")sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)if amScraping.ourRank > 3:sheet.write(sheet_iterator, 12, 'Greater than 3')else:sheet.write(sheet_iterator, 12, amScraping.ourRank)sheet.write(sheet_iterator, 13, amScraping.ourInventory)sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)sheet.write(sheet_iterator, 23, amScraping.otherCost)sheet.write(sheet_iterator, 24, amScraping.wanlc)sheet.write(sheet_iterator, 25, amScraping.commission)sheet.write(sheet_iterator, 26, amScraping.competitorCommission)sheet.write(sheet_iterator, 27, amScraping.returnProvision)sheet.write(sheet_iterator, 28, round(amScraping.ourSellingPrice - amScraping.lowestPossibleSp))sheet.write(sheet_iterator, 29, item.risky)sheet.write(sheet_iterator, 30, amScraping.proposedSp)sheet.write(sheet_iterator, 31, amScraping.avgSale)sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))if amScraping.decision is None:sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')sheet_iterator+=1continuesheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))sheet.write(sheet_iterator, 34, amScraping.reason)if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.ourSellingPrice+max(10,.01*amScraping.ourSellingPrice)))sheet_iterator+=1sheet = wbk.add_sheet('Among Cheapest')xstr = lambda s: s or ""heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatsheet.write(0, 0, "Item Id", heading_xf)sheet.write(0, 1, "Amazon Sku", heading_xf)sheet.write(0, 2, "Asin", heading_xf)sheet.write(0, 3, "Location", heading_xf)sheet.write(0, 4, "Brand", heading_xf)sheet.write(0, 5, "Product Name", heading_xf)sheet.write(0, 6, "Weight", heading_xf)sheet.write(0, 7, "Courier Cost", heading_xf)sheet.write(0, 8, "Our SP", heading_xf)sheet.write(0, 9, "Promo Price", heading_xf)sheet.write(0, 10, "Is Promotion", heading_xf)sheet.write(0, 11, "Lowest Possible SP", heading_xf)sheet.write(0, 12, "Rank", heading_xf)sheet.write(0, 13, "Our Inventory", heading_xf)sheet.write(0, 14, "Lowest Seller SP", heading_xf)sheet.write(0, 15, "Lowest Seller Rating", heading_xf)sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)sheet.write(0, 23, "Other Cost", heading_xf)sheet.write(0, 24, "WANLC", heading_xf)sheet.write(0, 25, "Commission", heading_xf)sheet.write(0, 26, "Competitor Commission", heading_xf)sheet.write(0, 27, "Return Provision", heading_xf)sheet.write(0, 28, "Margin", heading_xf)sheet.write(0, 29, "Risky", heading_xf)sheet.write(0, 30, "Proposed Sp", heading_xf)sheet.write(0, 31, "Avg Sale", heading_xf)sheet.write(0, 32, "Sales History", heading_xf)sheet.write(0, 33, "Decision", heading_xf)sheet.write(0, 34, "Reason", heading_xf)sheet.write(0, 35, "Updated Price", heading_xf)sheet_iterator = 1amongCheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETE).filter(AmazonScrapingHistory.timestamp==timestamp).all()for amongCheapestItem in amongCheapestItems:amScraping = amongCheapestItem[0]item = amongCheapestItem[1]sheet.write(sheet_iterator, 0, amScraping.item_id)if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'sheet.write(sheet_iterator, 1, sku)sheet.write(sheet_iterator, 2, amScraping.asin)sheet.write(sheet_iterator, 3, loc)sheet.write(sheet_iterator, 4, item.brand)sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))sheet.write(sheet_iterator, 6, item.weight)sheet.write(sheet_iterator, 7, amScraping.courierCost)sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)sheet.write(sheet_iterator, 9, amScraping.promoPrice)if amScraping.isPromotion:sheet.write(sheet_iterator, 10, "Yes")else:sheet.write(sheet_iterator, 10, "Yes")sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)if amScraping.ourRank > 3:sheet.write(sheet_iterator, 12, 'Greater than 3')else:sheet.write(sheet_iterator, 12, amScraping.ourRank)sheet.write(sheet_iterator, 13, amScraping.ourInventory)sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)sheet.write(sheet_iterator, 23, amScraping.otherCost)sheet.write(sheet_iterator, 24, amScraping.wanlc)sheet.write(sheet_iterator, 25, amScraping.commission)sheet.write(sheet_iterator, 26, amScraping.competitorCommission)sheet.write(sheet_iterator, 27, amScraping.returnProvision)sheet.write(sheet_iterator, 28, round(amScraping.ourSellingPrice - amScraping.lowestPossibleSp))sheet.write(sheet_iterator, 29, item.risky)sheet.write(sheet_iterator, 30, amScraping.proposedSp)sheet.write(sheet_iterator, 31, amScraping.avgSale)sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))if amScraping.decision is None:sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')sheet_iterator+=1continuesheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))sheet.write(sheet_iterator, 34, amScraping.reason)if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.ourSellingPrice+max(10,.01*amScraping.ourSellingPrice)))sheet_iterator+=1sheet = wbk.add_sheet('Cheapest')xstr = lambda s: s or ""heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatsheet.write(0, 0, "Item Id", heading_xf)sheet.write(0, 1, "Amazon Sku", heading_xf)sheet.write(0, 2, "Asin", heading_xf)sheet.write(0, 3, "Location", heading_xf)sheet.write(0, 4, "Brand", heading_xf)sheet.write(0, 5, "Product Name", heading_xf)sheet.write(0, 6, "Weight", heading_xf)sheet.write(0, 7, "Courier Cost", heading_xf)sheet.write(0, 8, "Our SP", heading_xf)sheet.write(0, 9, "Promo Price", heading_xf)sheet.write(0, 10, "Is Promotion", heading_xf)sheet.write(0, 11, "Lowest Possible SP", heading_xf)sheet.write(0, 12, "Rank", heading_xf)sheet.write(0, 13, "Our Inventory", heading_xf)sheet.write(0, 14, "Lowest Seller SP", heading_xf)sheet.write(0, 15, "Lowest Seller Rating", heading_xf)sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)sheet.write(0, 23, "Other Cost", heading_xf)sheet.write(0, 24, "WANLC", heading_xf)sheet.write(0, 25, "Commission", heading_xf)sheet.write(0, 26, "Competitor Commission", heading_xf)sheet.write(0, 27, "Return Provision", heading_xf)sheet.write(0, 28, "Margin", heading_xf)sheet.write(0, 29, "Risky", heading_xf)sheet.write(0, 30, "Proposed Sp", heading_xf)sheet.write(0, 31, "Avg Sale", heading_xf)sheet.write(0, 32, "Sales History", heading_xf)sheet.write(0, 33, "Decision", heading_xf)sheet.write(0, 34, "Reason", heading_xf)sheet.write(0, 35, "Updated Price", heading_xf)sheet_iterator = 1cheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.BUY_BOX).filter(AmazonScrapingHistory.timestamp==timestamp).all()for cheapestItem in cheapestItems:amScraping = cheapestItem[0]item = cheapestItem[1]sheet.write(sheet_iterator, 0, amScraping.item_id)if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'sheet.write(sheet_iterator, 1, sku)sheet.write(sheet_iterator, 2, amScraping.asin)sheet.write(sheet_iterator, 3, loc)sheet.write(sheet_iterator, 4, item.brand)sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))sheet.write(sheet_iterator, 6, item.weight)sheet.write(sheet_iterator, 7, amScraping.courierCost)sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)sheet.write(sheet_iterator, 9, amScraping.promoPrice)if amScraping.isPromotion:sheet.write(sheet_iterator, 10, "Yes")else:sheet.write(sheet_iterator, 10, "Yes")sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)if amScraping.ourRank > 3:sheet.write(sheet_iterator, 12, 'Greater than 3')else:sheet.write(sheet_iterator, 12, amScraping.ourRank)sheet.write(sheet_iterator, 13, amScraping.ourInventory)sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)sheet.write(sheet_iterator, 23, amScraping.otherCost)sheet.write(sheet_iterator, 24, amScraping.wanlc)sheet.write(sheet_iterator, 25, amScraping.commission)sheet.write(sheet_iterator, 26, amScraping.competitorCommission)sheet.write(sheet_iterator, 27, amScraping.returnProvision)sheet.write(sheet_iterator, 28, round(amScraping.ourSellingPrice - amScraping.lowestPossibleSp))sheet.write(sheet_iterator, 29, item.risky)sheet.write(sheet_iterator, 30, amScraping.proposedSp)sheet.write(sheet_iterator, 31, amScraping.avgSale)sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))if amScraping.decision is None:sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')sheet_iterator+=1continuesheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))sheet.write(sheet_iterator, 34, amScraping.reason)if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":sheet.write(sheet_iterator, 35, math.ceil(amScraping.ourSellingPrice+max(10,.01*amScraping.ourSellingPrice)))sheet_iterator+=1sheet = wbk.add_sheet('Negative Margin')xstr = lambda s: s or ""heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatsheet.write(0, 0, "Item Id", heading_xf)sheet.write(0, 1, "Amazon Sku", heading_xf)sheet.write(0, 2, "Asin", heading_xf)sheet.write(0, 3, "Location", heading_xf)sheet.write(0, 4, "Brand", heading_xf)sheet.write(0, 5, "Product Name", heading_xf)sheet.write(0, 6, "Weight", heading_xf)sheet.write(0, 7, "Courier Cost", heading_xf)sheet.write(0, 8, "Our SP", heading_xf)sheet.write(0, 9, "Promo Price", heading_xf)sheet.write(0, 10, "Is Promotion", heading_xf)sheet.write(0, 11, "Lowest Possible SP", heading_xf)sheet.write(0, 12, "Rank", heading_xf)sheet.write(0, 13, "Our Inventory", heading_xf)sheet.write(0, 14, "Lowest Seller SP", heading_xf)sheet.write(0, 15, "Lowest Seller Rating", heading_xf)sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)sheet.write(0, 23, "Other Cost", heading_xf)sheet.write(0, 24, "WANLC", heading_xf)sheet.write(0, 25, "Commission", heading_xf)sheet.write(0, 26, "Competitor Commission", heading_xf)sheet.write(0, 27, "Return Provision", heading_xf)sheet.write(0, 28, "Margin", heading_xf)sheet.write(0, 29, "Avg Sale", heading_xf)sheet.write(0, 30, "Sales History", heading_xf)sheet_iterator = 1amongCheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.NEGATIVE_MARGIN).filter(AmazonScrapingHistory.timestamp==timestamp).all()for amongCheapestItem in amongCheapestItems:amScraping = amongCheapestItem[0]item = amongCheapestItem[1]sheet.write(sheet_iterator, 0, amScraping.item_id)if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'sheet.write(sheet_iterator, 1, sku)sheet.write(sheet_iterator, 2, amScraping.asin)sheet.write(sheet_iterator, 3, loc)sheet.write(sheet_iterator, 4, item.brand)sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))sheet.write(sheet_iterator, 6, item.weight)sheet.write(sheet_iterator, 7, amScraping.courierCost)sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)sheet.write(sheet_iterator, 9, amScraping.promoPrice)if amScraping.isPromotion:sheet.write(sheet_iterator, 10, "Yes")else:sheet.write(sheet_iterator, 10, "Yes")sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)if amScraping.ourRank > 3:sheet.write(sheet_iterator, 12, 'Greater than 3')else:sheet.write(sheet_iterator, 12, amScraping.ourRank)sheet.write(sheet_iterator, 13, amScraping.ourInventory)sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)sheet.write(sheet_iterator, 23, amScraping.otherCost)sheet.write(sheet_iterator, 24, amScraping.wanlc)sheet.write(sheet_iterator, 25, amScraping.commission)sheet.write(sheet_iterator, 26, amScraping.competitorCommission)sheet.write(sheet_iterator, 27, amScraping.returnProvision)sheet.write(sheet_iterator, 28, round(amScraping.ourSellingPrice - amScraping.lowestPossibleSp))sheet.write(sheet_iterator, 29, amScraping.avgSale)sheet.write(sheet_iterator, 30, getOosString(saleMap.get(sku)))sheet_iterator+=1sheet = wbk.add_sheet('Exception List')xstr = lambda s: s or ""heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatsheet.write(0, 0, "Item Id", heading_xf)sheet.write(0, 1, "Amazon Sku", heading_xf)sheet.write(0, 2, "Asin", heading_xf)sheet.write(0, 3, "Location", heading_xf)sheet.write(0, 4, "Brand", heading_xf)sheet.write(0, 5, "Product Name", heading_xf)sheet.write(0, 6, "Reason", heading_xf)sheet_iterator = 1amongCheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.EXCEPTION).filter(AmazonScrapingHistory.timestamp==timestamp).all()for amongCheapestItem in amongCheapestItems:amScraping = amongCheapestItem[0]item = amongCheapestItem[1]sheet.write(sheet_iterator, 0, amScraping.item_id)if amScraping.warehouseLocation == 1:sku = 'FBA'+str(amScraping.item_id)loc = 'MUMBAI'else:sku = 'FBB'+str(amScraping.item_id)loc = 'BANGLORE'sheet.write(sheet_iterator, 1, sku)sheet.write(sheet_iterator, 2, amScraping.asin)sheet.write(sheet_iterator, 3, loc)sheet.write(sheet_iterator, 4, item.brand)sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))sheet.write(sheet_iterator, 6, amScraping.reason)sheet_iterator+=1if (runType=='FULL'):sheet = wbk.add_sheet('Auto Favorites')heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')excel_integer_format = '0'integer_style = xlwt.XFStyle()integer_style.num_format_str = excel_integer_formatxstr = lambda s: s or ""sheet.write(0, 0, "Item ID", heading_xf)sheet.write(0, 1, "Brand", heading_xf)sheet.write(0, 2, "Product Name", heading_xf)sheet.write(0, 3, "Auto Favourite", heading_xf)sheet.write(0, 4, "Reason", heading_xf)sheet_iterator=1for autoFav in nowAutoFav:itemId = autoFav[0]reason = autoFav[1]it = Item.query.filter_by(id=itemId).one()sheet.write(sheet_iterator, 0, itemId)sheet.write(sheet_iterator, 1, it.brand)sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))sheet.write(sheet_iterator, 3, "True")sheet.write(sheet_iterator, 4, reason)sheet_iterator+=1for prevFav in previousAutoFav:it = Item.query.filter_by(id=prevFav).one()sheet.write(sheet_iterator, 0, prevFav)sheet.write(sheet_iterator, 1, it.brand)sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))sheet.write(sheet_iterator, 3, "False")sheet_iterator+=1filename = "/tmp/amazon-report-"+runType+" " + str(timestamp) + ".xls"wbk.save(filename)def main():parser = optparse.OptionParser()parser.add_option("-t", "--type", dest="runType",default="FULL", type="string",help="Run type FULL or FAVOURITE")(options, args) = parser.parse_args()if options.runType not in ('FULL','FAVOURITE'):print "Run type argument illegal."sys.exit(1)time.sleep(5)timestamp = datetime.now()fetchFbaSale()itemInfo = populateStuff(timestamp,options.runType)itemsToPopulate = 0toSync = 0lenItems = len(itemInfo)while(toSync < lenItems):oldSync = toSyncif lenItems >= 20:toSync = 20else:toSync = lenItems - oldSyncgetPriceAndAsin(itemInfo[oldSync:toSync+oldSync])toSync = oldSync + toSyncwhile (len(itemInfo)>0):if len(itemInfo) >= 20:itemsToPopulate = 20else:itemsToPopulate = len(itemInfo)print "items to popluate"print itemsToPopulateexceptionList, negativeMargin, cheapest, amongCheapestAndCanCompete, canCompete, almostCompete, cantCompete = decideCategory(itemInfo[0:itemsToPopulate])itemInfo[0:itemsToPopulate] = []commitExceptionList(exceptionList,timestamp,options.runType)commitNegativeMargin(negativeMargin,timestamp,options.runType)commitCheapest(cheapest,timestamp,options.runType)commitAmongCheapestAndCanCompete(amongCheapestAndCanCompete,timestamp,options.runType)commitCanCompete(canCompete,timestamp,options.runType)commitAlmostCompete(almostCompete,timestamp,options.runType)commitCantCompete(cantCompete, timestamp,options.runType)exceptionList[:], negativeMargin[:], cheapest[:], amongCheapestAndCanCompete[:], canCompete[:], almostCompete[:], cantCompete[:] =[],[],[],[],[],[],[]autoDecreaseItems = fetchItemsForAutoDecrease(timestamp)autoIncreaseItems = fetchItemsForAutoIncrease(timestamp)previousAutoFav, nowAutoFav = markAutoFavourites(timestamp)writeReport(timestamp,autoDecreaseItems,autoIncreaseItems,previousAutoFav,nowAutoFav,options.runType)if __name__=='__main__':main()