Subversion Repositories SmartDukaan

Rev

Rev 12483 | Rev 12489 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12363 kshitij.so 1
from elixir import *
12418 kshitij.so 2
from sqlalchemy.sql import or_ ,func, asc, desc, and_
12363 kshitij.so 3
from shop2020.config.client.ConfigClient import ConfigClient
4
from shop2020.model.v1.catalog.impl import DataService
12364 kshitij.so 5
from shop2020.model.v1.catalog.impl.DataService import Amazonlisted, Item, \
12418 kshitij.so 6
Category, SourcePercentageMaster,SourceCategoryPercentage, SourceItemPercentage, AmazonPromotion, AmazonScrapingHistory, \
7
ItemVatMaster, CategoryVatMaster
12363 kshitij.so 8
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
12430 kshitij.so 9
from shop2020.thriftpy.model.v1.catalog.ttypes import CompetitionCategory, \
12363 kshitij.so 10
Decision, RunType, AmazonPromotionType
12430 kshitij.so 11
from shop2020.model.v1.catalog.script import AmazonAsyncScraper
12363 kshitij.so 12
from shop2020.clients.InventoryClient import InventoryClient
13
from shop2020.clients.TransactionClient import TransactionClient
12452 kshitij.so 14
import time
15
from time import sleep
12363 kshitij.so 16
from datetime import date, datetime, timedelta
17
import math
18
import simplejson as json
19
import xlwt
20
import optparse
21
import sys
12430 kshitij.so 22
from operator import itemgetter
12363 kshitij.so 23
 
24
 
25
config_client = ConfigClient()
26
host = config_client.get_property('staging_hostname')
27
syncPrice=config_client.get_property('sync_price_on_marketplace')
28
 
29
amazonAsinPrice={}
12432 kshitij.so 30
amazonLongTermActivePromotions = {}
31
amazonShortTermActivePromotions = {}
12363 kshitij.so 32
saleMap = {}
33
DataService.initialize(db_hostname=host)
34
 
12430 kshitij.so 35
amScraper = AmazonAsyncScraper.Products("AKIAII3SGRXBJDPCHSGQ", "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg", "AF6E3O0VE0X4D")
12363 kshitij.so 36
 
37
class __AmazonItemInfo:
38
 
12447 kshitij.so 39
    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):
12382 kshitij.so 40
        self.asin = asin
12363 kshitij.so 41
        self.nlc = nlc
42
        self.courierCost = courierCost
43
        self.sku = sku
44
        self.product_group = product_group
45
        self.brand = brand
46
        self.model_name = model_name
47
        self.model_number = model_number
48
        self.color = color
49
        self.weight = weight
50
        self.parent_category = parent_category
51
        self.risky = risky
52
        self.vatRate = vatRate
53
        self.runType = runType
54
        self.parent_category_name = parent_category_name
55
        self.sourcePercentage = sourcePercentage
56
        self.ourInventory = ourInventory
57
        self.state_id = state_id
12447 kshitij.so 58
        self.otherCost = otherCost
12363 kshitij.so 59
 
60
class __AmazonDetails:
12430 kshitij.so 61
    def __init__(self, sku, ourSp, ourRank, lowestSellerName,lowestSellerSp,secondLowestSellerName, secondLowestSellerSp, thirdLowestSellerName, thirdLowestSellerSp, totalSeller, multipleListings, \
62
                 promoPrice, isPromotion, lowestSellerShippingTime, lowestSellerRating, secondLowestSellerShippingTime, secondLowestSellerRating, thirdLowestSellerShippingTime , \
63
                 thirdLowestSellerRating, lowestSellerType, secondLowestSellerType, thirdLowestSellerType):
12363 kshitij.so 64
        self.sku =sku
65
        self.ourSp = ourSp
66
        self.ourRank = ourRank
67
        self.lowestSellerName = lowestSellerName
68
        self.lowestSellerSp = lowestSellerSp
69
        self.secondLowestSellerName = secondLowestSellerName
70
        self.secondLowestSellerSp = secondLowestSellerSp
71
        self.thirdLowestSellerName = thirdLowestSellerName
72
        self.thirdLowestSellerSp = thirdLowestSellerSp
73
        self.totalSeller = totalSeller
12430 kshitij.so 74
        self.multipleListings = multipleListings
75
        self.promoPrice = promoPrice
76
        self.isPromotion = isPromotion
77
        self.lowestSellerShippingTime =lowestSellerShippingTime
78
        self.lowestSellerRating = lowestSellerRating
79
        self.secondLowestSellerShippingTime = secondLowestSellerShippingTime
80
        self.secondLowestSellerRating = secondLowestSellerRating
81
        self.thirdLowestSellerShippingTime= thirdLowestSellerShippingTime
82
        self.thirdLowestSellerRating = thirdLowestSellerRating
83
        self.lowestSellerType = lowestSellerType
84
        self.secondLowestSellerType = secondLowestSellerType
12447 kshitij.so 85
        self.thirdLowestSellerType = thirdLowestSellerType
12430 kshitij.so 86
 
12363 kshitij.so 87
 
88
class __AmazonPricing:
89
 
12432 kshitij.so 90
    def __init__(self, ourSp, lowestPossibleSp):
12363 kshitij.so 91
        self.ourSp = ourSp
92
        self.lowestPossibleSp = lowestPossibleSp
93
 
12432 kshitij.so 94
class __Promotion:
12433 kshitij.so 95
    def __init__(self, promoPrice, subsidy, promotionType,expiryDate):
12432 kshitij.so 96
        self.promoPrice = promoPrice
97
        self.subsidy = subsidy
98
        self.promotionType = promotionType
12433 kshitij.so 99
        self.expiryDate = expiryDate 
12432 kshitij.so 100
 
12363 kshitij.so 101
 
12432 kshitij.so 102
 
12396 kshitij.so 103
def fetchItemsForAutoDecrease(time):
104
    successfulAutoDecrease = []
105
    autoDecrementItems = session.query(AmazonScrapingHistory).join((Amazonlisted,AmazonScrapingHistory.item_id==Amazonlisted.itemId))\
106
    .filter(AmazonScrapingHistory.timestamp==time).filter(or_(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETE,AmazonScrapingHistory.competitiveCategory==CompetitionCategory.COMPETITIVE, AmazonScrapingHistory.competitiveCategory==CompetitionCategory.ALMOST_COMPETE ))\
107
    .filter(Amazonlisted.autoDecrement==True).all()
12477 kshitij.so 108
    print len(autoDecrementItems)
12396 kshitij.so 109
    for autoDecrementItem in autoDecrementItems:
110
        if autoDecrementItem.warehouseLocation == 1:
111
            sku = 'FBA'+str(autoDecrementItem.item_id)
112
        else:
113
            sku = 'FBB'+str(autoDecrementItem.item_id)
12432 kshitij.so 114
        if amazonShortTermActivePromotions.has_key(sku):
12396 kshitij.so 115
            markReasonForItem(autoDecrementItem,'Item in short term promotion',Decision.AUTO_DECREMENT_FAILED)
116
            continue
12484 kshitij.so 117
        if math.ceil(autoDecrementItem.proposedSp) >= autoDecrementItem.promoPrice:
12396 kshitij.so 118
            markReasonForItem(autoDecrementItem,'Proposed SP greater than or equal to current SP',Decision.AUTO_DECREMENT_FAILED)
119
            continue
12479 kshitij.so 120
        if autoDecrementItem.proposedSp < autoDecrementItem.lowestPossibleSp:
12396 kshitij.so 121
            markReasonForItem(autoDecrementItem,'Proposed SP less than lowest possible SP',Decision.AUTO_DECREMENT_FAILED)
122
            continue
123
        try:
124
            daysOfStock = (float(autoDecrementItem.ourInventory))/autoDecrementItem.avgSale
125
        except:
126
            daysOfStock = float("inf")
127
        if autoDecrementItem.competitiveCategory == CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETE:
128
            if daysOfStock < 20:
129
                markReasonForItem(autoDecrementItem,'Days of stock less than 20',Decision.AUTO_DECREMENT_FAILED)
12433 kshitij.so 130
                continue
12396 kshitij.so 131
 
12433 kshitij.so 132
        if autoDecrementItem.competitiveCategory == CompetitionCategory.COMPETITIVE and not autoDecrementItem.isPromotion:
12396 kshitij.so 133
            if autoDecrementItem.parentCategoryId in [10006,10009,11001]:
12433 kshitij.so 134
                if daysOfStock < 1 :
12396 kshitij.so 135
                    markReasonForItem(autoDecrementItem,'Days of stock less than 1',Decision.AUTO_DECREMENT_FAILED)
12433 kshitij.so 136
                    continue
137
 
12396 kshitij.so 138
            else:
139
                if daysOfStock < 3:
140
                    markReasonForItem(autoDecrementItem,'Days of stock less than 3',Decision.AUTO_DECREMENT_FAILED)
12433 kshitij.so 141
                    continue
142
 
143
        if autoDecrementItem.competitiveCategory == CompetitionCategory.COMPETITIVE and autoDecrementItem.isPromotion:
144
            if autoDecrementItem.parentCategoryId in [10006,10009,11001]:
145
                if (amazonLongTermActivePromotions.get(sku).expiryDate - datetime.now()).days >2 and daysOfStock < 1 :
146
                    markReasonForItem(autoDecrementItem,'Promo Item, expiry after 2 days or not enough stock',Decision.AUTO_DECREMENT_FAILED)
147
                    continue
148
 
149
            else:
150
                if (amazonLongTermActivePromotions.get(sku).expiryDate - datetime.now()).days >2 and daysOfStock < 3:
151
                    markReasonForItem(autoDecrementItem,'Promo Item, expiry after 2 days or not enough stock',Decision.AUTO_DECREMENT_FAILED)
152
                    continue
12396 kshitij.so 153
 
154
        autoDecrementItem.ourEnoughStock=True
155
        autoDecrementItem.decision = Decision.AUTO_DECREMENT_SUCCESS
156
        autoDecrementItem.reason = 'All conditions for auto decrement true'
157
        successfulAutoDecrease.append(autoDecrementItem)
158
    session.commit()
159
    session.close()
160
    return successfulAutoDecrease
161
 
162
def fetchItemsForAutoIncrease(time):
163
    successfulAutoIncrease = []
164
    autoIncrementItems = session.query(AmazonScrapingHistory).join((Amazonlisted,AmazonScrapingHistory.item_id==Amazonlisted.itemId))\
165
    .filter(AmazonScrapingHistory.timestamp==time).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.BUY_BOX)\
166
    .filter(Amazonlisted.autoIncrement==True).all()
167
    transaction_client = TransactionClient().get_client()
12477 kshitij.so 168
    print len(autoIncrementItems)
12396 kshitij.so 169
    for autoIncrementItem in autoIncrementItems:
170
        if autoIncrementItem.warehouseLocation == 1:
171
            sku = 'FBA'+str(autoIncrementItem.item_id)
172
        else:
173
            sku = 'FBB'+str(autoIncrementItem.item_id)
12432 kshitij.so 174
        if amazonShortTermActivePromotions.has_key(sku):
12396 kshitij.so 175
            markReasonForItem(autoIncrementItem,'Item in short term promotion',Decision.AUTO_INCREMENT_FAILED)
176
            continue
177
        if autoIncrementItem.totalSeller==1 and autoIncrementItem.ourRank==1:
178
            markReasonForItem(autoIncrementItem,'We are the only seller',Decision.AUTO_INCREMENT_FAILED)
179
            continue 
12484 kshitij.so 180
        if autoIncrementItem.proposedSp <= autoIncrementItem.promoPrice:
12396 kshitij.so 181
            markReasonForItem(autoIncrementItem,'Proposed SP less than current SP',Decision.AUTO_INCREMENT_FAILED)
182
            continue
12484 kshitij.so 183
        if autoIncrementItem.proposedSp >=10000 and autoIncrementItem.promoPrice<10000:
12396 kshitij.so 184
            markReasonForItem(autoIncrementItem,'Proposed SP is greater than 10,000 and current sp is less than 10,000',Decision.AUTO_INCREMENT_FAILED)
185
            continue
186
 
12479 kshitij.so 187
        if autoIncrementItem.isPromotion and (math.ceil(autoIncrementItem.promoPrice+max(10,.01*autoIncrementItem.promoPrice)) > (amazonLongTermActivePromotions.get(sku)).promoPrice):
12447 kshitij.so 188
            markReasonForItem(autoIncrementItem,'Proposed SP cant be greater than promo price',Decision.AUTO_INCREMENT_FAILED)
189
            continue
190
 
191
 
12396 kshitij.so 192
        if autoIncrementItem.avgSale==0:
193
            markReasonForItem(autoIncrementItem,'Avg sale is 0',Decision.AUTO_INCREMENT_FAILED)
194
            continue
195
 
196
        daysOfStock = (float(autoIncrementItem.ourInventory))/autoIncrementItem.avgSale
197
        if daysOfStock > 5:
198
            markReasonForItem(autoIncrementItem,'Days of stock greater than 5',Decision.AUTO_INCREMENT_FAILED)
199
            continue
12484 kshitij.so 200
        if autoIncrementItem.isPromotion:
201
            antecedentPrice = session.query(AmazonScrapingHistory.promoPrice).filter(AmazonScrapingHistory.item_id==autoIncrementItem.item_id).filter(AmazonScrapingHistory.timestamp>time-timedelta(days=1)).order_by(asc(AmazonScrapingHistory.timestamp)).first()
202
            if antecedentPrice is not None:
203
                if float(math.ceil(autoIncrementItem.promoPrice+max(10,.01*autoIncrementItem.promoPrice))-math.ceil(antecedentPrice[0]+max(10,.01*antecedentPrice[0])))/math.ceil(antecedentPrice[0]+max(10,.01*antecedentPrice[0]))>.02:
204
                    markReasonForItem(autoIncrementItem,'Maximum price increase in last 24 hours should be 2%',Decision.AUTO_INCREMENT_FAILED)
205
                    continue
206
        else:
207
            antecedentPrice = session.query(AmazonScrapingHistory.ourSellingPrice).filter(AmazonScrapingHistory.item_id==autoIncrementItem.item_id).filter(AmazonScrapingHistory.timestamp>time-timedelta(days=1)).order_by(asc(AmazonScrapingHistory.timestamp)).first()
208
            if antecedentPrice is not None:
209
                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:
210
                    markReasonForItem(autoIncrementItem,'Maximum price increase in last 24 hours should be 2%',Decision.AUTO_INCREMENT_FAILED)
211
                    continue
12396 kshitij.so 212
        fbaSaleSnapshot = transaction_client.getAmazonFbaSalesLatestSnapshotForItemLocationWise(autoIncrementItem.item_id,autoIncrementItem.warehouseLocation)
12480 kshitij.so 213
        if getLastDaySale(fbaSaleSnapshot)<=2:
12396 kshitij.so 214
            markReasonForItem(autoIncrementItem,'Last day sale is less than 3',Decision.AUTO_INCREMENT_FAILED)
215
            continue
216
 
217
        autoIncrementItem.ourEnoughStock = False
218
        autoIncrementItem.decision = Decision.AUTO_INCREMENT_SUCCESS
219
        autoIncrementItem.reason = 'All conditions for auto increment true'
220
        successfulAutoIncrease.append(autoIncrementItem)
221
    session.commit()
222
    return successfulAutoIncrease     
223
 
224
 
225
def markReasonForItem(amHistory,reason,decision):
226
    amHistory.decision = decision
227
    amHistory.reason = reason
228
 
12424 kshitij.so 229
def calculateAverageSale(sku):
230
    count,sale = 0,0
231
    oosStatus = saleMap.get(sku)
232
    for obj in oosStatus:
233
        if not obj.isOutOfStock:
234
            count+=1
235
            sale = sale+obj.totalOrderCount
236
    avgSalePerDay=0 if count==0 else (float(sale)/count)
237
    return round(avgSalePerDay,2)
238
 
239
 
12396 kshitij.so 240
def getOosString(oosStatus):
241
    lastNdaySale=""
242
    for obj in oosStatus:
12423 kshitij.so 243
        if obj.isOutOfStock:
12396 kshitij.so 244
            lastNdaySale += "X-"
245
        else:
12426 kshitij.so 246
            lastNdaySale += str(obj.totalOrderCount) + "-"
12396 kshitij.so 247
    return lastNdaySale[:-1]
248
 
249
def getLastDaySale(fbaSaleSnapshot):
250
    if fbaSaleSnapshot.item_id==0:
251
        return 0
252
    else:
12423 kshitij.so 253
        return fbaSaleSnapshot.totalOrderCount
12396 kshitij.so 254
 
12430 kshitij.so 255
#def syncAsin():
256
##    notListedOnAmazon = []
257
##    diffAsins = []
258
##    login_url = "https://sellercentral.amazon.in/gp/homepage.html"
259
##    br = SellerCentralInventoryReport.login(login_url)
260
##    report_url = "https://sellercentral.amazon.in/gp/upload-download-utils/requestReport.html?type=OpenListingReport&marketplaceID=44571&Request+Report="
261
##    br = SellerCentralInventoryReport.requestReport(br,report_url)
262
##    status_url="https://sellercentral.amazon.in/gp/upload-download-utils/reportStatusData.html"
263
##    br, page = SellerCentralInventoryReport.checkStatus(br,status_url)
264
##    br, batchId = SellerCentralInventoryReport.getReportBatchId(br,page)
265
##    print "*********************************"
266
##    print "Batch Id for request is ",batchId
267
##    print "*********************************"
268
##    ready = False
269
##    retryCount = 0
270
##    while not ready:
271
##        if retryCount == 10:
272
##            print "File not available for download after multiple retries"
273
##            sys.exit(1)
274
##        br, download_link = SellerCentralInventoryReport.downloadReport(br,batchId,status_url)
275
##        if download_link is not None:
276
##            ready= True
277
##            continue
278
##        print "File not ready for download yet.Will try again after 30 seconds."
279
##        retryCount+=1
280
##        time.sleep(30)
281
##    fPath = SellerCentralInventoryReport.fetchFile(download_link['href'],br,batchId)
282
#    fPath = "/tmp/9940651090.txt"
283
#    global amazonAsinPrice
284
#    for line in open(fPath):
285
#        l = line.split('\t')
286
#        if (str(l[0]).startswith('FBA') or str(l[0]).startswith('FBB')):
287
#            obj = __AmazonAsinPrice(l[1],l[2])
288
#            amazonAsinPrice[l[0]] = obj
289
##Can be used to sync asins, not doing due to multiple asins corresponding to one itemId
290
##    systemAsins = session.query(Item,Amazonlisted).join((Amazonlisted,Item.id==Amazonlisted.itemId)).all()
291
##    for systemAsin in systemAsins:
292
##        item = systemAsin[0]
293
##        amListed = systemAsin[1]
294
##        if amazonAsinPrice.get('FBA'+str(item.id)) is None:
295
##            temp=[]
296
##            temp.append(item)
297
##            temp.append(amListed)
298
##            notListedOnAmazon.append(temp)
299
##            continue
300
##        else:
301
##            temp=[]
302
##            temp.append(item)
303
##            temp.append(amListed)
304
##            if item.asin!=((amazonAsinPrice.get('FBA'+str(item.id))).asin).strip():
305
##                diffAsins.append(temp)
306
##                continue
307
##            
308
##    for diffAsin in diffAsins:
309
##        item = diffAsin[0]
310
##        amListed = diffAsin[1]
311
##        item.asin = ((amazonAsinPrice.get('FBA'+str(item.id))).asin).strip()
312
##        amListed.asin = ((amazonAsinPrice.get('FBA'+str(item.id))).asin).strip()
313
##    session.commit()
314
##    session.close()
12363 kshitij.so 315
 
316
def fetchFbaSale():
317
    global saleMap
318
    transaction_client = TransactionClient().get_client()
319
    fbaSaleSnapshot = transaction_client.getAmazonFbaSalesSnapshotForDays(4)
320
    for saleSnapshot in fbaSaleSnapshot:
321
        if saleSnapshot.fcLocation == 0:
322
            if saleMap.has_key('FBA'+str(saleSnapshot.item_id)):
323
                temp = []
12367 kshitij.so 324
                val = saleMap.get('FBA'+str(saleSnapshot.item_id))
325
                for l in val:
12363 kshitij.so 326
                    temp.append(l)
327
                temp.append(saleSnapshot)
12366 kshitij.so 328
                saleMap['FBA'+str(saleSnapshot.item_id)]=temp
12363 kshitij.so 329
            else:
12368 kshitij.so 330
                temp = []
331
                temp.append(saleSnapshot)
332
                saleMap['FBA'+str(saleSnapshot.item_id)] = temp
12363 kshitij.so 333
        else:
334
            if saleMap.has_key('FBB'+str(saleSnapshot.item_id)):
335
                temp = []
12367 kshitij.so 336
                val = saleMap.get('FBB'+str(saleSnapshot.item_id))
337
                for l in val:
12363 kshitij.so 338
                    temp.append(l)
12368 kshitij.so 339
                saleMap['FBB'+str(saleSnapshot.item_id)]=temp
12363 kshitij.so 340
                temp.append(saleSnapshot)
341
            else:
12368 kshitij.so 342
                temp = []
343
                temp.append(saleSnapshot)
344
                saleMap['FBB'+str(saleSnapshot.item_id)] = temp
12363 kshitij.so 345
 
12424 kshitij.so 346
 
12363 kshitij.so 347
def computeCourierCost(weight):
12378 kshitij.so 348
    try:
349
        cCost = 10.0;
350
        slabs = int((weight*1000)/500-.001)
351
        for slab in range(0,slabs):
352
            cCost = cCost + 10.0;
353
        return cCost;
354
    except:
355
        return 10.0
12363 kshitij.so 356
 
357
 
358
def populateStuff(time,runType):
359
    global amazonLongTermActivePromotions
12396 kshitij.so 360
    global amazonShortTermActivePromotions
12363 kshitij.so 361
    itemInfo = []
362
    inventory_client = InventoryClient().get_client()
363
    fbaAvailableInventorySnapshot = inventory_client.getAllAvailableAmazonFbaItemInventory()
12387 kshitij.so 364
    print len(fbaAvailableInventorySnapshot)
12363 kshitij.so 365
    for fbaInventoryItem in fbaAvailableInventorySnapshot:
366
        d_amazon_listed = Amazonlisted.get_by(itemId=fbaInventoryItem.item_id)
367
        if d_amazon_listed is None:
368
            continue
369
        if d_amazon_listed.overrrideWanlc:
370
            wanlc = d_amazon_listed.exceptionalWanlc
371
        else:
372
            wanlc = inventory_client.getWanNlcForSource(fbaInventoryItem.item_id,OrderSource.AMAZON)
373
        it = Item.query.filter_by(id=fbaInventoryItem.item_id).one()
374
        category = Category.query.filter_by(id=it.category).one()
375
        parent_category = Category.query.filter_by(id=category.parent_category_id).first()
376
        scp = SourceCategoryPercentage.query.filter(SourceCategoryPercentage.category_id==it.category).filter(SourceCategoryPercentage.source==OrderSource.AMAZON).filter(SourceCategoryPercentage.startDate<=time).filter(SourceCategoryPercentage.expiryDate>=time).first()
377
        if scp is not None:
378
            sourcePercentage = scp
379
        else:
380
            spm = SourcePercentageMaster.get_by(source=OrderSource.AMAZON)
381
            sourcePercentage = spm
12375 kshitij.so 382
        if fbaInventoryItem.location==0:
12377 kshitij.so 383
            sku = 'FBA'+str(fbaInventoryItem.item_id)
12363 kshitij.so 384
            state_id = 1
12375 kshitij.so 385
        elif fbaInventoryItem.location==1:
12377 kshitij.so 386
            sku = 'FBB'+str(fbaInventoryItem.item_id)
12363 kshitij.so 387
            state_id = 2
388
        else:
389
            continue
390
        cc = computeCourierCost(it.weight)
12379 kshitij.so 391
 
12447 kshitij.so 392
        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)
12363 kshitij.so 393
        itemInfo.append(amazonItemInfo)
394
    amPromotions = AmazonPromotion.query.filter(AmazonPromotion.startDate<=time).filter(AmazonPromotion.endDate>=time).filter(AmazonPromotion.promotionType==AmazonPromotionType.LONGTERM).filter(AmazonPromotion.promotionActive==True) \
395
    .group_by(AmazonPromotion.sku).order_by(desc(AmazonPromotion.addedOn)).all()
396
    for amPromotion in amPromotions:
12433 kshitij.so 397
        amazonLongTermActivePromotions[amPromotion.sku] = __Promotion(amPromotion.salePrice,amPromotion.subsidy,amPromotion.promotionType,amPromotion.endDate)
12396 kshitij.so 398
    amPromotions = AmazonPromotion.query.filter(AmazonPromotion.startDate<=time).filter(AmazonPromotion.endDate>=time).filter(AmazonPromotion.promotionType==AmazonPromotionType.SHORTTERM).filter(AmazonPromotion.promotionActive==True) \
399
    .group_by(AmazonPromotion.sku).order_by(desc(AmazonPromotion.addedOn)).all()
400
    for amPromotion in amPromotions:
12433 kshitij.so 401
        amazonShortTermActivePromotions[amPromotion.sku] = __Promotion(amPromotion.salePrice,amPromotion.subsidy,amPromotion.promotionType,amPromotion.endDate)
12363 kshitij.so 402
    session.close()
12450 kshitij.so 403
    print "No of items populated ",len(itemInfo)
12452 kshitij.so 404
    sleep(5)
12363 kshitij.so 405
    return itemInfo
406
 
12430 kshitij.so 407
def getPriceAndAsin(itemInfo):
408
    skus = []
409
    for item in itemInfo:
410
        skus.append(item.sku)
411
    ourPricingForSku = amScraper.get_my_pricing_for_sku('A21TJRUUN4KGV', skus)
412
    for item in itemInfo:
413
        ourPricing = ourPricingForSku.get(item.sku)
12441 kshitij.so 414
        if ourPricing is None or len(ourPricing.keys())==0:
12430 kshitij.so 415
            item.ourSp = 0
416
            item.promoPrice = 0
417
            item.isPromotion = False
12473 kshitij.so 418
            item.asin = ''
12430 kshitij.so 419
        else:
420
            item.ourSp = ourPricing.get('sellingPrice')
421
            item.promoPrice = ourPricing.get('promoPrice')
422
            item.isPromotion = ourPricing.get('promotion')
12473 kshitij.so 423
            item.asin = ourPricing.get('asin')
12450 kshitij.so 424
 
12430 kshitij.so 425
 
426
 
12363 kshitij.so 427
def decideCategory(itemInfo):
428
    exceptionList, negativeMargin, cheapest, amongCheapestAndCanCompete, canCompete, almostCompete, cantCompete = [],[],[],[],[],[],[] 
12430 kshitij.so 429
    skus = []
12363 kshitij.so 430
    for item in itemInfo:
12430 kshitij.so 431
        skus.append(item.sku)
432
    aggResponse = amScraper.get_competitive_pricing_for_sku('A21TJRUUN4KGV', skus)
433
    ourPricingForSku = amScraper.get_my_pricing_for_sku('A21TJRUUN4KGV', skus)
12403 kshitij.so 434
 
12363 kshitij.so 435
    for val in itemInfo:
12430 kshitij.so 436
        scrapInfo = aggResponse.get(val.sku)
12443 kshitij.so 437
        if scrapInfo is None or len(scrapInfo)==0 or val.nlc==0 or len(ourPricingForSku.get(val.sku).keys())==0:
12363 kshitij.so 438
            temp = []
439
            temp.append(val)
440
            if val.nlc==0 or val.nlc is None:
12456 kshitij.so 441
                print "WANLC is 0"
12363 kshitij.so 442
                temp.append("WANLC is 0")
12443 kshitij.so 443
            elif ourPricingForSku.get(val.sku) is None or ourPricingForSku.get(val.sku).keys()==0:
12456 kshitij.so 444
                print "Unable to fetch our price"
12430 kshitij.so 445
                temp.append("Unable to fetch our price")
12363 kshitij.so 446
            else:
12456 kshitij.so 447
                print "Unable to fetch competive pricing"
12430 kshitij.so 448
                temp.append("Unable to fetch competitive pricing")
12363 kshitij.so 449
            exceptionList.append(temp)
450
            continue
12430 kshitij.so 451
        val.ourSp = ourPricingForSku.get(val.sku).get('sellingPrice')
452
        val.promoPrice = ourPricingForSku.get(val.sku).get('promoPrice')
453
        val.isPromo = ourPricingForSku.get(val.sku).get('promotion')
12363 kshitij.so 454
        iterator = 0
455
        sku, lowestSellerName,secondLowestSellerName, thirdLowestSellerName = ('',)*4
12432 kshitij.so 456
        ourSp, ourRank, lowestSellerSp, secondLowestSellerSp, thirdLowestSellerSp, lowestPossibleSp = (0,)*6
12430 kshitij.so 457
        lowestSellerShippingTime, lowestSellerRating, secondLowestSellerShippingTime, secondLowestSellerRating, thirdLowestSellerShippingTime , \
458
        thirdLowestSellerRating, lowestSellerType, secondLowestSellerType, thirdLowestSellerType = (0,)*9
459
        isPromo = False
12363 kshitij.so 460
        sku = val.sku
461
        multipleListings = False
12430 kshitij.so 462
        ourSkuDetails = ourPricingForSku.get(val.sku)
12475 kshitij.so 463
        if (ourSkuDetails.get('promotion')!=(amazonLongTermActivePromotions.has_key(val.sku) or amazonShortTermActivePromotions.has_key(val.sku))):
12432 kshitij.so 464
            temp = []
465
            temp.append(val)
12456 kshitij.so 466
            print "promo misconfigured"
12432 kshitij.so 467
            temp.append("Promo misconfigured")
12457 kshitij.so 468
            exceptionList.append(temp)
12432 kshitij.so 469
            continue
12475 kshitij.so 470
 
12430 kshitij.so 471
        scrapInfo.append(ourSkuDetails)
472
        sortedScrapInfo =  sorted(scrapInfo, key=itemgetter('promoPrice','notOurSku'))
473
        for info in sortedScrapInfo:
12465 kshitij.so 474
            if  not info['notOurSku']:
12442 kshitij.so 475
                ourSp = info['sellingPrice']
476
                promoPrice = info['promoPrice']
477
                isPromo = info['promotion']
12430 kshitij.so 478
                ourRank = iterator + 1
12363 kshitij.so 479
 
480
            if iterator == 0:
12430 kshitij.so 481
                lowestSellerSp = info['promoPrice']
482
                lowestSellerShippingTime = info['shippingTime']
483
                lowestSellerRating = info['rating']
484
                lowestSellerType = info['fulfillmentChannel']
12363 kshitij.so 485
 
486
            if iterator == 1:
12430 kshitij.so 487
                secondLowestSellerSp = info['promoPrice']
488
                secondLowestSellerShippingTime = info['shippingTime']
489
                secondLowestSellerRating = info['rating']
490
                secondLowestSellerType = info['fulfillmentChannel']
12363 kshitij.so 491
 
492
            if iterator == 2:
12430 kshitij.so 493
                thirdLowestSellerSp = info['promoPrice']
494
                thirdLowestSellerShippingTime = info['shippingTime']
495
                thirdLowestSellerRating = info['rating']
496
                thirdLowestSellerType = info['fulfillmentChannel']
12363 kshitij.so 497
 
498
            iterator += 1
12401 kshitij.so 499
        print "terminating iterator"
12483 kshitij.so 500
 
12408 kshitij.so 501
        print "Creating object am details",val.sku
12430 kshitij.so 502
        amDetails = __AmazonDetails(sku, float(ourSp), ourRank, lowestSellerName,float(lowestSellerSp),secondLowestSellerName, float(secondLowestSellerSp), thirdLowestSellerName, float(thirdLowestSellerSp),len(scrapInfo),multipleListings,promoPrice,isPromo, \
503
                    lowestSellerShippingTime ,lowestSellerRating, secondLowestSellerShippingTime, secondLowestSellerRating, thirdLowestSellerShippingTime , thirdLowestSellerRating, lowestSellerType, secondLowestSellerType, thirdLowestSellerType)
12414 kshitij.so 504
        print "am details obj created"
12363 kshitij.so 505
        try:
12414 kshitij.so 506
            print "inside val getter"
12418 kshitij.so 507
            itemVatMaster = ItemVatMaster.query.filter(and_(ItemVatMaster.itemId==int(val.sku[3:]), ItemVatMaster.stateId==val.state_id)).first()
508
            if itemVatMaster is None:
12419 kshitij.so 509
                d_item = Item.query.filter_by(id=int(val.sku[3:])).first()
510
                if d_item is None:
511
                    raise 
512
                else:
12430 kshitij.so 513
                    vatMaster = CategoryVatMaster.query.filter(and_(CategoryVatMaster.categoryId==d_item.category, CategoryVatMaster.minVal<=amDetails.promoPrice,  CategoryVatMaster.maxVal>=amDetails.promoPrice,  CategoryVatMaster.stateId == val.state_id)).first()
12419 kshitij.so 514
                if vatMaster is None:
12418 kshitij.so 515
                    raise
12419 kshitij.so 516
                else:
517
                    val.vatRate = vatMaster.vatPercent
518
                    print "vat fetched"
12418 kshitij.so 519
            else:
520
                val.vatRate = itemVatMaster.vatPercentage
12419 kshitij.so 521
                print "vat fetched"
12363 kshitij.so 522
        except:
12414 kshitij.so 523
            print "vat exception"
12363 kshitij.so 524
            temp = []
525
            temp.append(val)
526
            temp.append("Vat not available")
527
            exceptionList.append(temp)
528
            continue
529
 
530
        lowestPossibleSp = getLowestPossibleSp(amDetails,val,val.sourcePercentage)
12408 kshitij.so 531
        print "Creating pricing obj"
12432 kshitij.so 532
        amPricing = __AmazonPricing(ourSp,lowestPossibleSp)
12483 kshitij.so 533
        print "sku ",val.sku
534
        print "oursp ",ourSp
535
        print "promoPrice ",promoPrice
536
        print "lowestpossbile sp ",lowestPossibleSp
537
        print "objlowestPossiblesp ",amPricing.lowestPossibleSp
12363 kshitij.so 538
 
12467 kshitij.so 539
        if amDetails.promoPrice < amPricing.lowestPossibleSp:
12363 kshitij.so 540
            temp = []
541
            temp.append(val)
542
            temp.append(amDetails)
543
            temp.append(amPricing)
544
            negativeMargin.append(temp)
12483 kshitij.so 545
            print "val sku cat negative ",val.sku
12363 kshitij.so 546
            continue
547
 
548
        if amDetails.ourRank==1:
549
            temp = []
550
            temp.append(val)
551
            temp.append(amDetails)
552
            temp.append(amPricing)
553
            cheapest.append(temp)
12483 kshitij.so 554
            print "val sku cat cheapest ",val.sku
12363 kshitij.so 555
            continue
556
 
12469 kshitij.so 557
        if (amDetails.lowestSellerSp > amPricing.lowestPossibleSp) and ((((float(float(amDetails.promoPrice) - amDetails.lowestSellerSp))/float(amDetails.promoPrice))<=.01) or ((float(amDetails.promoPrice) - amDetails.lowestSellerSp)<=25)):
12363 kshitij.so 558
            temp = []
559
            temp.append(val)
560
            temp.append(amDetails)
561
            temp.append(amPricing)
562
            amongCheapestAndCanCompete.append(temp)
12483 kshitij.so 563
            print "val sku cat amongCheapestAndCanCompete  ",val.sku
12363 kshitij.so 564
            continue
565
 
566
        if (amDetails.lowestSellerSp > amPricing.lowestPossibleSp):
567
            temp = []
568
            temp.append(val)
569
            temp.append(amDetails)
570
            temp.append(amPricing)
571
            canCompete.append(temp)
12483 kshitij.so 572
            print "val sku cat can compete  ",val.sku
12363 kshitij.so 573
            continue
574
 
12403 kshitij.so 575
        if amDetails.lowestSellerSp*(1+.01) >= amPricing.lowestPossibleSp:
12396 kshitij.so 576
            temp = []
577
            temp.append(val)
578
            temp.append(amDetails)
579
            temp.append(amPricing)
580
            almostCompete.append(temp)
12483 kshitij.so 581
            print "val sku cat almost compete  ",val.sku
12396 kshitij.so 582
            continue
583
 
584
 
12363 kshitij.so 585
        temp = []
586
        temp.append(val)
587
        temp.append(amDetails)
588
        temp.append(amPricing)
12483 kshitij.so 589
        print "val sku cat cant compete  ",val.sku
12363 kshitij.so 590
        cantCompete.append(temp)
12414 kshitij.so 591
    print "Created category..."
12363 kshitij.so 592
 
593
    return exceptionList, negativeMargin, cheapest, amongCheapestAndCanCompete, canCompete, almostCompete, cantCompete
12396 kshitij.so 594
 
12363 kshitij.so 595
 
596
def getLowestPossibleSp(amazonDetails,val,spm):
12447 kshitij.so 597
    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));
12432 kshitij.so 598
    if val.isPromo:
599
        if amazonLongTermActivePromotions.has_key(val.sku):
12466 kshitij.so 600
            subsidy = (amazonLongTermActivePromotions.get(val.sku)).subsidy
12432 kshitij.so 601
        else:
12466 kshitij.so 602
            subsidy = (amazonShortTermActivePromotions.get(val.sku)).subsidy
12432 kshitij.so 603
        lowestPossibleSp = lowestPossibleSp - subsidy
12363 kshitij.so 604
    return round(lowestPossibleSp,2)
605
 
606
def getTargetTp(targetSp,spm,val):
12424 kshitij.so 607
    targetTp = targetSp- targetSp*(spm.commission/100+spm.emiFee/100)*(1+(spm.serviceTax/100))-(val.courierCost)*(1+(spm.serviceTax/100))
12363 kshitij.so 608
    return round(targetTp,2)
609
 
610
def commitExceptionList(exceptionList,timestamp,runType):
611
    for exceptionItem in exceptionList:
612
        val = exceptionItem[0]
613
        reason = exceptionItem[1]
614
        amazonScrapingHistory = AmazonScrapingHistory()
615
        amazonScrapingHistory.item_id = val.sku[3:]
616
        amazonScrapingHistory.warehouseLocation = val.state_id
12396 kshitij.so 617
        amazonScrapingHistory.parentCategoryId = val.parent_category
12363 kshitij.so 618
        amazonScrapingHistory.reason = reason
619
        amazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)
620
        amazonScrapingHistory.competitiveCategory = CompetitionCategory.EXCEPTION
621
        amazonScrapingHistory.timestamp = timestamp
622
    session.commit()
623
 
624
def commitNegativeMargin(negativeMargin,timestamp,runType):
625
    for negativeMarginItem in negativeMargin:
626
        val = negativeMarginItem[0]
627
        amDetails = negativeMarginItem[1]
628
        amPricing = negativeMarginItem[2]
629
        spm = val.sourcePercentage
630
        amazonScrapingHistory = AmazonScrapingHistory()
631
        amazonScrapingHistory.item_id = val.sku[3:]
12471 kshitij.so 632
        amazonScrapingHistory.asin = val.asin
12363 kshitij.so 633
        amazonScrapingHistory.warehouseLocation = val.state_id
12396 kshitij.so 634
        amazonScrapingHistory.parentCategoryId = val.parent_category
12363 kshitij.so 635
        amazonScrapingHistory.ourSellingPrice = amDetails.ourSp
12432 kshitij.so 636
        amazonScrapingHistory.promoPrice = amDetails.promoPrice
12363 kshitij.so 637
        amazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSp
638
        amazonScrapingHistory.ourRank = amDetails.ourRank
639
        amazonScrapingHistory.ourInventory = val.ourInventory
640
        amazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSp
12468 kshitij.so 641
        amazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTime
642
        amazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRating
643
        amazonScrapingHistory.lowestSellerType = amDetails.lowestSellerType
12363 kshitij.so 644
        amazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSp
12468 kshitij.so 645
        amazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTime
646
        amazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRating
647
        amazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerType
12363 kshitij.so 648
        amazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSp
12468 kshitij.so 649
        amazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTime
650
        amazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRating
651
        amazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerType
12363 kshitij.so 652
        amazonScrapingHistory.wanlc = val.nlc
12447 kshitij.so 653
        amazonScrapingHistory.otherCost = val.otherCost
12363 kshitij.so 654
        amazonScrapingHistory.commission = spm.commission
12422 kshitij.so 655
        amazonScrapingHistory.competitorCommission = spm.competitorCommissionOther
12363 kshitij.so 656
        amazonScrapingHistory.returnProvision = spm.returnProvision
657
        amazonScrapingHistory.courierCost = val.courierCost
658
        amazonScrapingHistory.risky = val.risky
659
        amazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)
660
        amazonScrapingHistory.totalSeller = amDetails.totalSeller
661
        amazonScrapingHistory.competitiveCategory = CompetitionCategory.NEGATIVE_MARGIN
662
        amazonScrapingHistory.timestamp = timestamp
663
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
664
        amazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five days
12432 kshitij.so 665
        amazonScrapingHistory.isPromotion = val.isPromo
12363 kshitij.so 666
    session.commit()
667
 
668
 
669
def commitCheapest(cheapest,timestamp,runType):
670
    for cheapestItem in cheapest:
671
        val = cheapestItem[0]
672
        amDetails = cheapestItem[1]
673
        amPricing = cheapestItem[2]
674
        spm = val.sourcePercentage
675
        amazonScrapingHistory = AmazonScrapingHistory()
676
        amazonScrapingHistory.item_id = val.sku[3:]
12471 kshitij.so 677
        amazonScrapingHistory.asin = val.asin
12363 kshitij.so 678
        amazonScrapingHistory.warehouseLocation = val.state_id
12396 kshitij.so 679
        amazonScrapingHistory.parentCategoryId = val.parent_category
12363 kshitij.so 680
        amazonScrapingHistory.ourSellingPrice = amDetails.ourSp
12432 kshitij.so 681
        amazonScrapingHistory.promoPrice = amDetails.promoPrice
12363 kshitij.so 682
        amazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSp
683
        amazonScrapingHistory.ourRank = amDetails.ourRank
684
        amazonScrapingHistory.ourInventory = val.ourInventory
685
        amazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSp
12430 kshitij.so 686
        amazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTime
687
        amazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRating
688
        amazonScrapingHistory.lowestSellerType = amDetails.lowestSellerType
12363 kshitij.so 689
        amazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSp
12468 kshitij.so 690
        amazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTime
691
        amazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRating
692
        amazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerType
12363 kshitij.so 693
        amazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSp
12468 kshitij.so 694
        amazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTime
695
        amazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRating
696
        amazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerType
12447 kshitij.so 697
        amazonScrapingHistory.otherCost = val.otherCost
12363 kshitij.so 698
        amazonScrapingHistory.wanlc = val.nlc
699
        amazonScrapingHistory.commission = spm.commission
12422 kshitij.so 700
        amazonScrapingHistory.competitorCommission = spm.competitorCommissionOther
12363 kshitij.so 701
        amazonScrapingHistory.returnProvision = spm.returnProvision
702
        amazonScrapingHistory.courierCost = val.courierCost
703
        amazonScrapingHistory.risky = val.risky
704
        amazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)
705
        amazonScrapingHistory.totalSeller = amDetails.totalSeller
706
        amazonScrapingHistory.competitiveCategory = CompetitionCategory.BUY_BOX
707
        amazonScrapingHistory.timestamp = timestamp
708
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
12430 kshitij.so 709
        proposed_sp = max(amDetails.secondLowestSellerSp - max((20, amDetails.secondLowestSellerSp*0.002)), amPricing.lowestPossibleSp)
12433 kshitij.so 710
        if amazonScrapingHistory.isPromotion:
711
            if amazonLongTermActivePromotions.has_key(val.sku):
12466 kshitij.so 712
                proposed_sp = min(proposed_sp,(amazonLongTermActivePromotions.get(val.sku)).salePrice)
12433 kshitij.so 713
            else:
12466 kshitij.so 714
                proposed_sp = min(proposed_sp,(amazonShortTermActivePromotions.get(val.sku)).salePrice)
12468 kshitij.so 715
        #proposed_tp = getTargetTp(proposed_sp,spm,val)
12363 kshitij.so 716
        amazonScrapingHistory.proposedSp = proposed_sp
12468 kshitij.so 717
        #amazonScrapingHistory.proposedTp = proposed_tp
718
        #amazonScrapingHistory.marginIncreasedPotential = proposed_tp - amPricing.ourTp
12363 kshitij.so 719
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
720
        amazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five days
12432 kshitij.so 721
        amazonScrapingHistory.isPromotion = val.isPromo
12363 kshitij.so 722
    session.commit()
723
 
724
 
725
 
726
def commitAmongCheapestAndCanCompete(amongCheapestAndCanCompete,timestamp,runType):
727
    for amongCheapestAndCanCompeteItem in amongCheapestAndCanCompete:
728
        val = amongCheapestAndCanCompeteItem[0]
729
        amDetails = amongCheapestAndCanCompeteItem[1]
730
        amPricing = amongCheapestAndCanCompeteItem[2]
731
        spm = val.sourcePercentage
732
        amazonScrapingHistory = AmazonScrapingHistory()
733
        amazonScrapingHistory.item_id = val.sku[3:]
12471 kshitij.so 734
        amazonScrapingHistory.asin = val.asin
12363 kshitij.so 735
        amazonScrapingHistory.warehouseLocation = val.state_id
12396 kshitij.so 736
        amazonScrapingHistory.parentCategoryId = val.parent_category
12363 kshitij.so 737
        amazonScrapingHistory.ourSellingPrice = amDetails.ourSp
12432 kshitij.so 738
        amazonScrapingHistory.promoPrice = amDetails.promoPrice
12363 kshitij.so 739
        amazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSp
740
        amazonScrapingHistory.ourRank = amDetails.ourRank
741
        amazonScrapingHistory.ourInventory = val.ourInventory
742
        amazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSp
12430 kshitij.so 743
        amazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTime
744
        amazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRating
745
        amazonScrapingHistory.lowestSellerType = amDetails.lowestSellerType
12363 kshitij.so 746
        amazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSp
12468 kshitij.so 747
        amazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTime
748
        amazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRating
749
        amazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerType
12470 kshitij.so 750
        amazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSp
12468 kshitij.so 751
        amazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTime
752
        amazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRating
753
        amazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerType
12447 kshitij.so 754
        amazonScrapingHistory.otherCost = val.otherCost
12363 kshitij.so 755
        amazonScrapingHistory.wanlc = val.nlc
756
        amazonScrapingHistory.commission = spm.commission
12422 kshitij.so 757
        amazonScrapingHistory.competitorCommission = spm.competitorCommissionOther
12363 kshitij.so 758
        amazonScrapingHistory.returnProvision = spm.returnProvision
759
        amazonScrapingHistory.courierCost = val.courierCost
760
        amazonScrapingHistory.risky = val.risky
761
        amazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)
762
        amazonScrapingHistory.totalSeller = amDetails.totalSeller
763
        amazonScrapingHistory.competitiveCategory = CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETE
764
        amazonScrapingHistory.timestamp = timestamp
765
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
766
        proposed_sp = max(amDetails.lowestSellerSp - max((5, amDetails.lowestSellerSp*0.001)), amPricing.lowestPossibleSp)
12468 kshitij.so 767
        #proposed_tp = getTargetTp(proposed_sp,spm,val)
12363 kshitij.so 768
        amazonScrapingHistory.proposedSp = proposed_sp
12468 kshitij.so 769
        #amazonScrapingHistory.proposedTp = proposed_tp
12363 kshitij.so 770
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
771
        amazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five days
12432 kshitij.so 772
        amazonScrapingHistory.isPromotion = val.isPromo
12363 kshitij.so 773
    session.commit()
774
 
775
def commitCanCompete(canCompete,timestamp,runType):
776
    for canCompeteItem in canCompete:
777
        val = canCompeteItem[0]
778
        amDetails = canCompeteItem[1]
779
        amPricing = canCompeteItem[2]
780
        spm = val.sourcePercentage
781
        amazonScrapingHistory = AmazonScrapingHistory()
782
        amazonScrapingHistory.item_id = val.sku[3:]
12471 kshitij.so 783
        amazonScrapingHistory.asin = val.asin
12363 kshitij.so 784
        amazonScrapingHistory.warehouseLocation = val.state_id
12396 kshitij.so 785
        amazonScrapingHistory.parentCategoryId = val.parent_category
12363 kshitij.so 786
        amazonScrapingHistory.ourSellingPrice = amDetails.ourSp
12432 kshitij.so 787
        amazonScrapingHistory.promoPrice = amDetails.promoPrice
12363 kshitij.so 788
        amazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSp
789
        amazonScrapingHistory.ourRank = amDetails.ourRank
790
        amazonScrapingHistory.ourInventory = val.ourInventory
791
        amazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSp
12430 kshitij.so 792
        amazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTime
793
        amazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRating
794
        amazonScrapingHistory.lowestSellerType = amDetails.lowestSellerType
12363 kshitij.so 795
        amazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSp
12468 kshitij.so 796
        amazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTime
797
        amazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRating
798
        amazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerType
12363 kshitij.so 799
        amazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSp
12468 kshitij.so 800
        amazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTime
801
        amazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRating
802
        amazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerType
12447 kshitij.so 803
        amazonScrapingHistory.otherCost = val.otherCost
12363 kshitij.so 804
        amazonScrapingHistory.wanlc = val.nlc
805
        amazonScrapingHistory.commission = spm.commission
12422 kshitij.so 806
        amazonScrapingHistory.competitorCommission = spm.competitorCommissionOther
12363 kshitij.so 807
        amazonScrapingHistory.returnProvision = spm.returnProvision
808
        amazonScrapingHistory.courierCost = val.courierCost
809
        amazonScrapingHistory.risky = val.risky
810
        amazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)
811
        amazonScrapingHistory.totalSeller = amDetails.totalSeller
812
        amazonScrapingHistory.competitiveCategory = CompetitionCategory.COMPETITIVE
813
        amazonScrapingHistory.timestamp = timestamp
814
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
815
        proposed_sp = max(amDetails.lowestSellerSp - max((5, amDetails.lowestSellerSp*0.001)), amPricing.lowestPossibleSp)
12468 kshitij.so 816
        #proposed_tp = getTargetTp(proposed_sp,spm,val)
12363 kshitij.so 817
        amazonScrapingHistory.proposedSp = proposed_sp
12468 kshitij.so 818
        #amazonScrapingHistory.proposedTp = proposed_tp
12363 kshitij.so 819
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
820
        amazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five days
12432 kshitij.so 821
        amazonScrapingHistory.isPromotion = val.isPromo
12363 kshitij.so 822
    session.commit()
823
 
12383 kshitij.so 824
def commitAlmostCompete(almostCompete,timestamp,runType):
12396 kshitij.so 825
    for almostCompeteItem in almostCompete:
826
        val = almostCompeteItem[0]
827
        amDetails = almostCompeteItem[1]
828
        amPricing = almostCompeteItem[2]
829
        spm = val.sourcePercentage
830
        amazonScrapingHistory = AmazonScrapingHistory()
831
        amazonScrapingHistory.item_id = val.sku[3:]
12471 kshitij.so 832
        amazonScrapingHistory.asin = val.asin
12396 kshitij.so 833
        amazonScrapingHistory.warehouseLocation = val.state_id
834
        amazonScrapingHistory.parentCategoryId = val.parent_category
835
        amazonScrapingHistory.ourSellingPrice = amDetails.ourSp
12432 kshitij.so 836
        amazonScrapingHistory.promoPrice = amDetails.promoPrice
12396 kshitij.so 837
        amazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSp
838
        amazonScrapingHistory.ourRank = amDetails.ourRank
839
        amazonScrapingHistory.ourInventory = val.ourInventory
840
        amazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSp
12430 kshitij.so 841
        amazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTime
842
        amazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRating
843
        amazonScrapingHistory.lowestSellerType = amDetails.lowestSellerType
12396 kshitij.so 844
        amazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSp
12468 kshitij.so 845
        amazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTime
846
        amazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRating
847
        amazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerType
12396 kshitij.so 848
        amazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSp
12468 kshitij.so 849
        amazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTime
850
        amazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRating
851
        amazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerType
12447 kshitij.so 852
        amazonScrapingHistory.otherCost = val.otherCost
12396 kshitij.so 853
        amazonScrapingHistory.wanlc = val.nlc
854
        amazonScrapingHistory.commission = spm.commission
12422 kshitij.so 855
        amazonScrapingHistory.competitorCommission = spm.competitorCommissionOther
12396 kshitij.so 856
        amazonScrapingHistory.returnProvision = spm.returnProvision
857
        amazonScrapingHistory.courierCost = val.courierCost
858
        amazonScrapingHistory.risky = val.risky
859
        amazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)
860
        amazonScrapingHistory.totalSeller = amDetails.totalSeller
861
        amazonScrapingHistory.competitiveCategory = CompetitionCategory.ALMOST_COMPETE
862
        amazonScrapingHistory.timestamp = timestamp
863
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
12425 kshitij.so 864
        proposed_sp = min(amDetails.lowestSellerSp*(1+.01),amPricing.lowestPossibleSp)
12468 kshitij.so 865
        #proposed_tp = getTargetTp(proposed_sp,spm,val)
866
        #target_nlc = proposed_tp - amPricing.lowestPossibleTp + val.nlc
12396 kshitij.so 867
        amazonScrapingHistory.proposedSp = proposed_sp
12468 kshitij.so 868
        #amazonScrapingHistory.proposedTp = proposed_tp
869
        #amazonScrapingHistory.targetNlc = target_nlc
12396 kshitij.so 870
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
871
        amazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five days
12432 kshitij.so 872
        amazonScrapingHistory.isPromotion = val.isPromo
12396 kshitij.so 873
    session.commit()
12363 kshitij.so 874
 
12396 kshitij.so 875
 
12363 kshitij.so 876
def commitCantCompete(cantCompete, timestamp,runType):
877
    for cantCompeteItem in cantCompete:
878
        val = cantCompeteItem[0]
879
        amDetails = cantCompeteItem[1]
880
        amPricing = cantCompeteItem[2]
881
        spm = val.sourcePercentage
882
        amazonScrapingHistory = AmazonScrapingHistory()
883
        amazonScrapingHistory.item_id = val.sku[3:]
12471 kshitij.so 884
        amazonScrapingHistory.asin = val.asin
12363 kshitij.so 885
        amazonScrapingHistory.warehouseLocation = val.state_id
12396 kshitij.so 886
        amazonScrapingHistory.parentCategoryId = val.parent_category
12363 kshitij.so 887
        amazonScrapingHistory.ourSellingPrice = amDetails.ourSp
12432 kshitij.so 888
        amazonScrapingHistory.promoPrice = amDetails.promoPrice
12363 kshitij.so 889
        amazonScrapingHistory.lowestPossibleSp = amPricing.lowestPossibleSp
890
        amazonScrapingHistory.ourRank = amDetails.ourRank
891
        amazonScrapingHistory.ourInventory = val.ourInventory
892
        amazonScrapingHistory.lowestSellerSp = amDetails.lowestSellerSp
12430 kshitij.so 893
        amazonScrapingHistory.lowestSellerShippingTime = amDetails.lowestSellerShippingTime
894
        amazonScrapingHistory.lowestSellerRating = amDetails.lowestSellerRating
895
        amazonScrapingHistory.lowestSellerType = amDetails.lowestSellerType
12363 kshitij.so 896
        amazonScrapingHistory.secondLowestSellerSp = amDetails.secondLowestSellerSp
12468 kshitij.so 897
        amazonScrapingHistory.secondLowestSellerShippingTime = amDetails.secondLowestSellerShippingTime
898
        amazonScrapingHistory.secondLowestSellerRating = amDetails.secondLowestSellerRating
899
        amazonScrapingHistory.secondLowestSellerType = amDetails.secondLowestSellerType
12363 kshitij.so 900
        amazonScrapingHistory.thirdLowestSellerSp = amDetails.thirdLowestSellerSp
12468 kshitij.so 901
        amazonScrapingHistory.thirdLowestSellerShippingTime = amDetails.thirdLowestSellerShippingTime
902
        amazonScrapingHistory.thirdLowestSellerRating = amDetails.thirdLowestSellerRating
903
        amazonScrapingHistory.thirdLowestSellerType = amDetails.thirdLowestSellerType
12447 kshitij.so 904
        amazonScrapingHistory.otherCost = val.otherCost
12363 kshitij.so 905
        amazonScrapingHistory.wanlc = val.nlc
906
        amazonScrapingHistory.commission = spm.commission
12422 kshitij.so 907
        amazonScrapingHistory.competitorCommission = spm.competitorCommissionOther
12363 kshitij.so 908
        amazonScrapingHistory.returnProvision = spm.returnProvision
909
        amazonScrapingHistory.courierCost = val.courierCost
910
        amazonScrapingHistory.risky = val.risky
911
        amazonScrapingHistory.runType = RunType._NAMES_TO_VALUES.get(runType)
912
        amazonScrapingHistory.totalSeller = amDetails.totalSeller
913
        amazonScrapingHistory.competitiveCategory = CompetitionCategory.CANT_COMPETE
914
        amazonScrapingHistory.timestamp = timestamp
915
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
916
        proposed_sp = amDetails.lowestSellerSp - max(5, amDetails.lowestSellerSp*0.001)
12468 kshitij.so 917
        #proposed_tp = getTargetTp(proposed_sp,spm,val)
918
        #target_nlc = proposed_tp - amPricing.lowestPossibleTp + val.nlc
12363 kshitij.so 919
        amazonScrapingHistory.proposedSp = proposed_sp
12468 kshitij.so 920
        #amazonScrapingHistory.proposedTp = proposed_tp
921
        #amazonScrapingHistory.targetNlc = target_nlc
12363 kshitij.so 922
        amazonScrapingHistory.multipleListings = amDetails.multipleListings
923
        amazonScrapingHistory.avgSale = calculateAverageSale(val.sku) #Last five days
12432 kshitij.so 924
        amazonScrapingHistory.isPromotion = val.isPromo
12363 kshitij.so 925
    session.commit()
926
 
12396 kshitij.so 927
def markAutoFavourites(time):
928
    nowAutoFav = []
929
    previouslyAutoFav = []
930
    stockList = []
931
    saleList = []
932
    items = session.query(func.sum(AmazonScrapingHistory.ourInventory),AmazonScrapingHistory.item_id).group_by(AmazonScrapingHistory.item_id).all()
933
    allItems = session.query(Amazonlisted).all()
934
    for item in items:
935
        reason = ""
936
        if item[0]>=5:
937
            stockList.append(item[1])
938
 
939
    for sku, val in saleMap.iteritems():
940
        totalSale = 0
941
        item_id = sku.replace('FBA','').replace('FBB','')
942
        val =saleMap.get('FBA'+str(item_id))
943
        if val is not None:
944
            for sale in val:
945
                totalSale += sale.totalOrderCount
946
        val =saleMap.get('FBB'+str(item_id))
947
        if val is not None:
948
            for sale in val:
949
                totalSale += sale.totalOrderCount
950
        if totalSale > 0:
951
            saleList.append(item_id)
952
 
953
    for aItem in allItems:
954
        reason = ""
955
        toMark = False
956
        if aItem.itemId in saleList:
957
            toMark = True
958
            reason+="Total FC sale is greater than 1 for last five days.."
959
        if aItem.itemId in stockList:
960
            toMark = True
961
            reason+="Item is present in buy box in last 3 days"
962
        if not aItem.autoFavourite:
963
            print "Item is not under auto favourite"
964
        if toMark:
965
            temp=[]
966
            temp.append(aItem.itemId)
967
            temp.append(reason)
968
            nowAutoFav.append(temp)
969
        if (not toMark) and aItem.autoFavourite:
970
            previouslyAutoFav.append(aItem.itemId)
971
        aItem.autoFavourite = toMark
972
    session.commit()
973
    return previouslyAutoFav, nowAutoFav
974
 
12444 kshitij.so 975
def writeReport(timestamp,autoDecreaseItems,autoIncreaseItems,previousAutoFav,nowAutoFav,runType):
12396 kshitij.so 976
    wbk = xlwt.Workbook()
977
    sheet = wbk.add_sheet('Can\'t Compete')
978
    xstr = lambda s: s or ""
979
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
980
 
981
    excel_integer_format = '0'
982
    integer_style = xlwt.XFStyle()
983
    integer_style.num_format_str = excel_integer_format
984
 
985
    sheet.write(0, 0, "Item Id", heading_xf)
986
    sheet.write(0, 1, "Amazon Sku", heading_xf)
987
    sheet.write(0, 2, "Asin", heading_xf)
988
    sheet.write(0, 3, "Location", heading_xf)
989
    sheet.write(0, 4, "Brand", heading_xf)
990
    sheet.write(0, 5, "Product Name", heading_xf)
991
    sheet.write(0, 6, "Weight", heading_xf)
992
    sheet.write(0, 7, "Courier Cost", heading_xf)
993
    sheet.write(0, 8, "Our SP", heading_xf)
12432 kshitij.so 994
    sheet.write(0, 9, "Promo Price", heading_xf)
995
    sheet.write(0, 10, "Is Promotion", heading_xf)
996
    sheet.write(0, 11, "Lowest Possible SP", heading_xf)
12396 kshitij.so 997
    sheet.write(0, 12, "Rank", heading_xf)
998
    sheet.write(0, 13, "Our Inventory", heading_xf)
12432 kshitij.so 999
    sheet.write(0, 14, "Lowest Seller SP", heading_xf)
1000
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1001
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
12396 kshitij.so 1002
    sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)
12432 kshitij.so 1003
    sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)
1004
    sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)
1005
    sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)
1006
    sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)
1007
    sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)
12447 kshitij.so 1008
    sheet.write(0, 23, "Other Cost", heading_xf)
1009
    sheet.write(0, 24, "WANLC", heading_xf)
1010
    sheet.write(0, 25, "Commission", heading_xf)
1011
    sheet.write(0, 26, "Competitor Commission", heading_xf)
1012
    sheet.write(0, 27, "Return Provision", heading_xf)
1013
    sheet.write(0, 28, "Margin", heading_xf)
1014
    sheet.write(0, 29, "Risky", heading_xf)
1015
    sheet.write(0, 30, "Proposed Sp", heading_xf)
12468 kshitij.so 1016
    sheet.write(0, 31, "Avg Sale", heading_xf)
1017
    sheet.write(0, 32, "Sales History", heading_xf)
12396 kshitij.so 1018
 
1019
    sheet_iterator = 1
12476 kshitij.so 1020
    cantCompeteItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.CANT_COMPETE).filter(AmazonScrapingHistory.timestamp==timestamp).all()
12396 kshitij.so 1021
    for cantCompeteItem in cantCompeteItems:
1022
        amScraping =  cantCompeteItem[0]
1023
        item = cantCompeteItem[1]
1024
        sheet.write(sheet_iterator, 0, amScraping.item_id)
1025
        if amScraping.warehouseLocation == 1:
1026
            sku = 'FBA'+str(amScraping.item_id)
1027
            loc = 'MUMBAI'
1028
        else:
1029
            sku = 'FBB'+str(amScraping.item_id)
1030
            loc = 'BANGLORE'
1031
        sheet.write(sheet_iterator, 1, sku)
12471 kshitij.so 1032
        sheet.write(sheet_iterator, 2, amScraping.asin)
12396 kshitij.so 1033
        sheet.write(sheet_iterator, 3, loc)
1034
        sheet.write(sheet_iterator, 4, item.brand)
1035
        sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1036
        sheet.write(sheet_iterator, 6, item.weight)
1037
        sheet.write(sheet_iterator, 7, amScraping.courierCost)
1038
        sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)
12432 kshitij.so 1039
        sheet.write(sheet_iterator, 9, amScraping.promoPrice)
1040
        if amScraping.isPromotion:
1041
            sheet.write(sheet_iterator, 10, "Yes")
1042
        else:
12483 kshitij.so 1043
            sheet.write(sheet_iterator, 10, "No")
12432 kshitij.so 1044
        sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)
12396 kshitij.so 1045
        if amScraping.ourRank > 3:
1046
            sheet.write(sheet_iterator, 12, 'Greater than 3')
1047
        else:
1048
            sheet.write(sheet_iterator, 12, amScraping.ourRank)
1049
        sheet.write(sheet_iterator, 13, amScraping.ourInventory)
12432 kshitij.so 1050
        sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)
1051
        sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)
1052
        sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)
12396 kshitij.so 1053
        sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)
12432 kshitij.so 1054
        sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)
1055
        sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)
1056
        sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)
1057
        sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)
1058
        sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)
12447 kshitij.so 1059
        sheet.write(sheet_iterator, 23, amScraping.otherCost)
1060
        sheet.write(sheet_iterator, 24, amScraping.wanlc)
1061
        sheet.write(sheet_iterator, 25, amScraping.commission)
1062
        sheet.write(sheet_iterator, 26, amScraping.competitorCommission)
1063
        sheet.write(sheet_iterator, 27, amScraping.returnProvision)
12484 kshitij.so 1064
        sheet.write(sheet_iterator, 28, round(amScraping.promoPrice - amScraping.lowestPossibleSp))
12447 kshitij.so 1065
        sheet.write(sheet_iterator, 29, item.risky)
1066
        sheet.write(sheet_iterator, 30, amScraping.proposedSp)
12468 kshitij.so 1067
        sheet.write(sheet_iterator, 31, amScraping.avgSale)
1068
        sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))
12396 kshitij.so 1069
        sheet_iterator+=1
1070
 
1071
    sheet = wbk.add_sheet('Competitive')
1072
    xstr = lambda s: s or ""
1073
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1074
 
1075
    excel_integer_format = '0'
1076
    integer_style = xlwt.XFStyle()
1077
    integer_style.num_format_str = excel_integer_format
1078
 
1079
    sheet.write(0, 0, "Item Id", heading_xf)
1080
    sheet.write(0, 1, "Amazon Sku", heading_xf)
1081
    sheet.write(0, 2, "Asin", heading_xf)
1082
    sheet.write(0, 3, "Location", heading_xf)
1083
    sheet.write(0, 4, "Brand", heading_xf)
1084
    sheet.write(0, 5, "Product Name", heading_xf)
1085
    sheet.write(0, 6, "Weight", heading_xf)
1086
    sheet.write(0, 7, "Courier Cost", heading_xf)
1087
    sheet.write(0, 8, "Our SP", heading_xf)
12432 kshitij.so 1088
    sheet.write(0, 9, "Promo Price", heading_xf)
1089
    sheet.write(0, 10, "Is Promotion", heading_xf)
1090
    sheet.write(0, 11, "Lowest Possible SP", heading_xf)
12396 kshitij.so 1091
    sheet.write(0, 12, "Rank", heading_xf)
1092
    sheet.write(0, 13, "Our Inventory", heading_xf)
12432 kshitij.so 1093
    sheet.write(0, 14, "Lowest Seller SP", heading_xf)
1094
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1095
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
12396 kshitij.so 1096
    sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)
12432 kshitij.so 1097
    sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)
1098
    sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)
1099
    sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)
1100
    sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)
1101
    sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)
12447 kshitij.so 1102
    sheet.write(0, 23, "Other Cost", heading_xf)
1103
    sheet.write(0, 24, "WANLC", heading_xf)
1104
    sheet.write(0, 25, "Commission", heading_xf)
1105
    sheet.write(0, 26, "Competitor Commission", heading_xf)
1106
    sheet.write(0, 27, "Return Provision", heading_xf)
1107
    sheet.write(0, 28, "Margin", heading_xf)
1108
    sheet.write(0, 29, "Risky", heading_xf)
1109
    sheet.write(0, 30, "Proposed Sp", heading_xf)
12468 kshitij.so 1110
    sheet.write(0, 31, "Avg Sale", heading_xf)
1111
    sheet.write(0, 32, "Sales History", heading_xf)
1112
    sheet.write(0, 33, "Decision", heading_xf)
1113
    sheet.write(0, 34, "Reason", heading_xf)
1114
    sheet.write(0, 35, "Updated Price", heading_xf)
12396 kshitij.so 1115
 
1116
    sheet_iterator = 1
12476 kshitij.so 1117
    competitiveItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.COMPETITIVE).filter(AmazonScrapingHistory.timestamp==timestamp).all()
12396 kshitij.so 1118
    for competitiveItem in competitiveItems:
1119
        amScraping =  competitiveItem[0]
1120
        item = competitiveItem[1]
1121
        sheet.write(sheet_iterator, 0, amScraping.item_id)
1122
        if amScraping.warehouseLocation == 1:
1123
            sku = 'FBA'+str(amScraping.item_id)
1124
            loc = 'MUMBAI'
1125
        else:
1126
            sku = 'FBB'+str(amScraping.item_id)
1127
            loc = 'BANGLORE'
1128
        sheet.write(sheet_iterator, 1, sku)
12471 kshitij.so 1129
        sheet.write(sheet_iterator, 2, amScraping.asin)
12396 kshitij.so 1130
        sheet.write(sheet_iterator, 3, loc)
1131
        sheet.write(sheet_iterator, 4, item.brand)
1132
        sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1133
        sheet.write(sheet_iterator, 6, item.weight)
1134
        sheet.write(sheet_iterator, 7, amScraping.courierCost)
1135
        sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)
12432 kshitij.so 1136
        sheet.write(sheet_iterator, 9, amScraping.promoPrice)
1137
        if amScraping.isPromotion:
1138
            sheet.write(sheet_iterator, 10, "Yes")
1139
        else:
12483 kshitij.so 1140
            sheet.write(sheet_iterator, 10, "No")
12432 kshitij.so 1141
        sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)
12396 kshitij.so 1142
        if amScraping.ourRank > 3:
1143
            sheet.write(sheet_iterator, 12, 'Greater than 3')
1144
        else:
1145
            sheet.write(sheet_iterator, 12, amScraping.ourRank)
1146
        sheet.write(sheet_iterator, 13, amScraping.ourInventory)
12432 kshitij.so 1147
        sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)
1148
        sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)
1149
        sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)
12396 kshitij.so 1150
        sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)
12432 kshitij.so 1151
        sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)
1152
        sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)
1153
        sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)
1154
        sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)
1155
        sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)
12447 kshitij.so 1156
        sheet.write(sheet_iterator, 23, amScraping.otherCost)
1157
        sheet.write(sheet_iterator, 24, amScraping.wanlc)
1158
        sheet.write(sheet_iterator, 25, amScraping.commission)
1159
        sheet.write(sheet_iterator, 26, amScraping.competitorCommission)
1160
        sheet.write(sheet_iterator, 27, amScraping.returnProvision)
12484 kshitij.so 1161
        sheet.write(sheet_iterator, 28, round(amScraping.promoPrice - amScraping.lowestPossibleSp))
12447 kshitij.so 1162
        sheet.write(sheet_iterator, 29, item.risky)
1163
        sheet.write(sheet_iterator, 30, amScraping.proposedSp)
12468 kshitij.so 1164
        sheet.write(sheet_iterator, 31, amScraping.avgSale)
1165
        sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))
12444 kshitij.so 1166
        if amScraping.decision is None:
12468 kshitij.so 1167
            sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')
12444 kshitij.so 1168
            sheet_iterator+=1
1169
            continue
12468 kshitij.so 1170
        sheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))
1171
        sheet.write(sheet_iterator, 34, amScraping.reason)
12444 kshitij.so 1172
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":
12479 kshitij.so 1173
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))
12444 kshitij.so 1174
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":
12484 kshitij.so 1175
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.promoPrice+max(10,.01*amScraping.promoPrice)))
12396 kshitij.so 1176
        sheet_iterator+=1
1177
 
1178
    sheet = wbk.add_sheet('Almost Competitive')
1179
    xstr = lambda s: s or ""
1180
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1181
 
1182
    excel_integer_format = '0'
1183
    integer_style = xlwt.XFStyle()
1184
    integer_style.num_format_str = excel_integer_format
1185
 
1186
    sheet.write(0, 0, "Item Id", heading_xf)
1187
    sheet.write(0, 1, "Amazon Sku", heading_xf)
1188
    sheet.write(0, 2, "Asin", heading_xf)
1189
    sheet.write(0, 3, "Location", heading_xf)
1190
    sheet.write(0, 4, "Brand", heading_xf)
1191
    sheet.write(0, 5, "Product Name", heading_xf)
1192
    sheet.write(0, 6, "Weight", heading_xf)
1193
    sheet.write(0, 7, "Courier Cost", heading_xf)
1194
    sheet.write(0, 8, "Our SP", heading_xf)
12432 kshitij.so 1195
    sheet.write(0, 9, "Promo Price", heading_xf)
1196
    sheet.write(0, 10, "Is Promotion", heading_xf)
1197
    sheet.write(0, 11, "Lowest Possible SP", heading_xf)
12396 kshitij.so 1198
    sheet.write(0, 12, "Rank", heading_xf)
1199
    sheet.write(0, 13, "Our Inventory", heading_xf)
12432 kshitij.so 1200
    sheet.write(0, 14, "Lowest Seller SP", heading_xf)
1201
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1202
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
12396 kshitij.so 1203
    sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)
12432 kshitij.so 1204
    sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)
1205
    sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)
1206
    sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)
1207
    sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)
1208
    sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)
12447 kshitij.so 1209
    sheet.write(0, 23, "Other Cost", heading_xf)
1210
    sheet.write(0, 24, "WANLC", heading_xf)
1211
    sheet.write(0, 25, "Commission", heading_xf)
1212
    sheet.write(0, 26, "Competitor Commission", heading_xf)
1213
    sheet.write(0, 27, "Return Provision", heading_xf)
1214
    sheet.write(0, 28, "Margin", heading_xf)
1215
    sheet.write(0, 29, "Risky", heading_xf)
1216
    sheet.write(0, 30, "Proposed Sp", heading_xf)
12468 kshitij.so 1217
    sheet.write(0, 31, "Avg Sale", heading_xf)
1218
    sheet.write(0, 32, "Sales History", heading_xf)
1219
    sheet.write(0, 33, "Decision", heading_xf)
1220
    sheet.write(0, 34, "Reason", heading_xf)
1221
    sheet.write(0, 35, "Updated Price", heading_xf)
12396 kshitij.so 1222
 
1223
    sheet_iterator = 1
12476 kshitij.so 1224
    almostCompetitiveItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.ALMOST_COMPETE).filter(AmazonScrapingHistory.timestamp==timestamp).all()
12396 kshitij.so 1225
    for almostCompetitiveItem in almostCompetitiveItems:
1226
        amScraping =  almostCompetitiveItem[0]
1227
        item = almostCompetitiveItem[1]
1228
        sheet.write(sheet_iterator, 0, amScraping.item_id)
1229
        if amScraping.warehouseLocation == 1:
1230
            sku = 'FBA'+str(amScraping.item_id)
1231
            loc = 'MUMBAI'
1232
        else:
1233
            sku = 'FBB'+str(amScraping.item_id)
1234
            loc = 'BANGLORE'
12432 kshitij.so 1235
        amScraping =  competitiveItem[0]
1236
        item = competitiveItem[1]
1237
        if amScraping.warehouseLocation == 1:
1238
            sku = 'FBA'+str(amScraping.item_id)
1239
            loc = 'MUMBAI'
1240
        else:
1241
            sku = 'FBB'+str(amScraping.item_id)
1242
            loc = 'BANGLORE'
12396 kshitij.so 1243
        sheet.write(sheet_iterator, 1, sku)
12471 kshitij.so 1244
        sheet.write(sheet_iterator, 2, amScraping.asin)
12396 kshitij.so 1245
        sheet.write(sheet_iterator, 3, loc)
1246
        sheet.write(sheet_iterator, 4, item.brand)
1247
        sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1248
        sheet.write(sheet_iterator, 6, item.weight)
1249
        sheet.write(sheet_iterator, 7, amScraping.courierCost)
1250
        sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)
12432 kshitij.so 1251
        sheet.write(sheet_iterator, 9, amScraping.promoPrice)
1252
        if amScraping.isPromotion:
1253
            sheet.write(sheet_iterator, 10, "Yes")
1254
        else:
12483 kshitij.so 1255
            sheet.write(sheet_iterator, 10, "No")
12432 kshitij.so 1256
        sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)
12396 kshitij.so 1257
        if amScraping.ourRank > 3:
1258
            sheet.write(sheet_iterator, 12, 'Greater than 3')
1259
        else:
1260
            sheet.write(sheet_iterator, 12, amScraping.ourRank)
1261
        sheet.write(sheet_iterator, 13, amScraping.ourInventory)
12432 kshitij.so 1262
        sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)
1263
        sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)
1264
        sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)
12396 kshitij.so 1265
        sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)
12432 kshitij.so 1266
        sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)
1267
        sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)
1268
        sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)
1269
        sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)
1270
        sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)
12447 kshitij.so 1271
        sheet.write(sheet_iterator, 23, amScraping.otherCost)
1272
        sheet.write(sheet_iterator, 24, amScraping.wanlc)
1273
        sheet.write(sheet_iterator, 25, amScraping.commission)
1274
        sheet.write(sheet_iterator, 26, amScraping.competitorCommission)
1275
        sheet.write(sheet_iterator, 27, amScraping.returnProvision)
12484 kshitij.so 1276
        sheet.write(sheet_iterator, 28, round(amScraping.promoPrice - amScraping.lowestPossibleSp))
12447 kshitij.so 1277
        sheet.write(sheet_iterator, 29, item.risky)
1278
        sheet.write(sheet_iterator, 30, amScraping.proposedSp)
12468 kshitij.so 1279
        sheet.write(sheet_iterator, 31, amScraping.avgSale)
1280
        sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))
12444 kshitij.so 1281
        if amScraping.decision is None:
12468 kshitij.so 1282
            sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')
12444 kshitij.so 1283
            sheet_iterator+=1
1284
            continue
12468 kshitij.so 1285
        sheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))
1286
        sheet.write(sheet_iterator, 34, amScraping.reason)
12444 kshitij.so 1287
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":
12479 kshitij.so 1288
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))
12444 kshitij.so 1289
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":
12484 kshitij.so 1290
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.promoPrice+max(10,.01*amScraping.promoPrice)))
12396 kshitij.so 1291
        sheet_iterator+=1
1292
 
1293
    sheet = wbk.add_sheet('Among Cheapest')
1294
    xstr = lambda s: s or ""
1295
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1296
 
1297
    excel_integer_format = '0'
1298
    integer_style = xlwt.XFStyle()
1299
    integer_style.num_format_str = excel_integer_format
1300
 
1301
    sheet.write(0, 0, "Item Id", heading_xf)
1302
    sheet.write(0, 1, "Amazon Sku", heading_xf)
1303
    sheet.write(0, 2, "Asin", heading_xf)
1304
    sheet.write(0, 3, "Location", heading_xf)
1305
    sheet.write(0, 4, "Brand", heading_xf)
1306
    sheet.write(0, 5, "Product Name", heading_xf)
1307
    sheet.write(0, 6, "Weight", heading_xf)
1308
    sheet.write(0, 7, "Courier Cost", heading_xf)
1309
    sheet.write(0, 8, "Our SP", heading_xf)
12432 kshitij.so 1310
    sheet.write(0, 9, "Promo Price", heading_xf)
1311
    sheet.write(0, 10, "Is Promotion", heading_xf)
1312
    sheet.write(0, 11, "Lowest Possible SP", heading_xf)
12396 kshitij.so 1313
    sheet.write(0, 12, "Rank", heading_xf)
1314
    sheet.write(0, 13, "Our Inventory", heading_xf)
12432 kshitij.so 1315
    sheet.write(0, 14, "Lowest Seller SP", heading_xf)
1316
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1317
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
12396 kshitij.so 1318
    sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)
12432 kshitij.so 1319
    sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)
1320
    sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)
1321
    sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)
1322
    sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)
1323
    sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)
12447 kshitij.so 1324
    sheet.write(0, 23, "Other Cost", heading_xf)
1325
    sheet.write(0, 24, "WANLC", heading_xf)
1326
    sheet.write(0, 25, "Commission", heading_xf)
1327
    sheet.write(0, 26, "Competitor Commission", heading_xf)
1328
    sheet.write(0, 27, "Return Provision", heading_xf)
1329
    sheet.write(0, 28, "Margin", heading_xf)
1330
    sheet.write(0, 29, "Risky", heading_xf)
1331
    sheet.write(0, 30, "Proposed Sp", heading_xf)
12468 kshitij.so 1332
    sheet.write(0, 31, "Avg Sale", heading_xf)
1333
    sheet.write(0, 32, "Sales History", heading_xf)
1334
    sheet.write(0, 33, "Decision", heading_xf)
1335
    sheet.write(0, 34, "Reason", heading_xf)
1336
    sheet.write(0, 35, "Updated Price", heading_xf)
12396 kshitij.so 1337
 
1338
    sheet_iterator = 1
12476 kshitij.so 1339
    amongCheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.AMONG_CHEAPEST_CAN_COMPETE).filter(AmazonScrapingHistory.timestamp==timestamp).all()
12396 kshitij.so 1340
    for amongCheapestItem in amongCheapestItems:
1341
        amScraping =  amongCheapestItem[0]
1342
        item = amongCheapestItem[1]
1343
        sheet.write(sheet_iterator, 0, amScraping.item_id)
1344
        if amScraping.warehouseLocation == 1:
1345
            sku = 'FBA'+str(amScraping.item_id)
1346
            loc = 'MUMBAI'
1347
        else:
1348
            sku = 'FBB'+str(amScraping.item_id)
1349
            loc = 'BANGLORE'
1350
        sheet.write(sheet_iterator, 1, sku)
12471 kshitij.so 1351
        sheet.write(sheet_iterator, 2, amScraping.asin)
12396 kshitij.so 1352
        sheet.write(sheet_iterator, 3, loc)
1353
        sheet.write(sheet_iterator, 4, item.brand)
1354
        sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1355
        sheet.write(sheet_iterator, 6, item.weight)
1356
        sheet.write(sheet_iterator, 7, amScraping.courierCost)
1357
        sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)
12432 kshitij.so 1358
        sheet.write(sheet_iterator, 9, amScraping.promoPrice)
1359
        if amScraping.isPromotion:
1360
            sheet.write(sheet_iterator, 10, "Yes")
1361
        else:
12483 kshitij.so 1362
            sheet.write(sheet_iterator, 10, "No")
12432 kshitij.so 1363
        sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)
12396 kshitij.so 1364
        if amScraping.ourRank > 3:
1365
            sheet.write(sheet_iterator, 12, 'Greater than 3')
1366
        else:
1367
            sheet.write(sheet_iterator, 12, amScraping.ourRank)
1368
        sheet.write(sheet_iterator, 13, amScraping.ourInventory)
12432 kshitij.so 1369
        sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)
1370
        sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)
1371
        sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)
12396 kshitij.so 1372
        sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)
12432 kshitij.so 1373
        sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)
1374
        sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)
1375
        sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)
1376
        sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)
1377
        sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)
12447 kshitij.so 1378
        sheet.write(sheet_iterator, 23, amScraping.otherCost)
1379
        sheet.write(sheet_iterator, 24, amScraping.wanlc)
1380
        sheet.write(sheet_iterator, 25, amScraping.commission)
1381
        sheet.write(sheet_iterator, 26, amScraping.competitorCommission)
1382
        sheet.write(sheet_iterator, 27, amScraping.returnProvision)
12484 kshitij.so 1383
        sheet.write(sheet_iterator, 28, round(amScraping.promoPrice - amScraping.lowestPossibleSp))
12447 kshitij.so 1384
        sheet.write(sheet_iterator, 29, item.risky)
1385
        sheet.write(sheet_iterator, 30, amScraping.proposedSp)
12468 kshitij.so 1386
        sheet.write(sheet_iterator, 31, amScraping.avgSale)
1387
        sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))
12444 kshitij.so 1388
        if amScraping.decision is None:
12468 kshitij.so 1389
            sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')
12444 kshitij.so 1390
            sheet_iterator+=1
1391
            continue
12468 kshitij.so 1392
        sheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))
1393
        sheet.write(sheet_iterator, 34, amScraping.reason)
12444 kshitij.so 1394
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":
12479 kshitij.so 1395
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))
12444 kshitij.so 1396
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":
12484 kshitij.so 1397
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.promoPrice+max(10,.01*amScraping.promoPrice)))
12396 kshitij.so 1398
        sheet_iterator+=1
1399
 
1400
    sheet = wbk.add_sheet('Cheapest')
1401
    xstr = lambda s: s or ""
1402
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1403
 
1404
    excel_integer_format = '0'
1405
    integer_style = xlwt.XFStyle()
1406
    integer_style.num_format_str = excel_integer_format
1407
 
1408
    sheet.write(0, 0, "Item Id", heading_xf)
1409
    sheet.write(0, 1, "Amazon Sku", heading_xf)
1410
    sheet.write(0, 2, "Asin", heading_xf)
1411
    sheet.write(0, 3, "Location", heading_xf)
1412
    sheet.write(0, 4, "Brand", heading_xf)
1413
    sheet.write(0, 5, "Product Name", heading_xf)
1414
    sheet.write(0, 6, "Weight", heading_xf)
1415
    sheet.write(0, 7, "Courier Cost", heading_xf)
1416
    sheet.write(0, 8, "Our SP", heading_xf)
12432 kshitij.so 1417
    sheet.write(0, 9, "Promo Price", heading_xf)
1418
    sheet.write(0, 10, "Is Promotion", heading_xf)
1419
    sheet.write(0, 11, "Lowest Possible SP", heading_xf)
12396 kshitij.so 1420
    sheet.write(0, 12, "Rank", heading_xf)
1421
    sheet.write(0, 13, "Our Inventory", heading_xf)
12432 kshitij.so 1422
    sheet.write(0, 14, "Lowest Seller SP", heading_xf)
1423
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1424
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
12396 kshitij.so 1425
    sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)
12432 kshitij.so 1426
    sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)
1427
    sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)
1428
    sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)
1429
    sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)
1430
    sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)
12447 kshitij.so 1431
    sheet.write(0, 23, "Other Cost", heading_xf)
1432
    sheet.write(0, 24, "WANLC", heading_xf)
1433
    sheet.write(0, 25, "Commission", heading_xf)
1434
    sheet.write(0, 26, "Competitor Commission", heading_xf)
1435
    sheet.write(0, 27, "Return Provision", heading_xf)
1436
    sheet.write(0, 28, "Margin", heading_xf)
1437
    sheet.write(0, 29, "Risky", heading_xf)
1438
    sheet.write(0, 30, "Proposed Sp", heading_xf)
12468 kshitij.so 1439
    sheet.write(0, 31, "Avg Sale", heading_xf)
1440
    sheet.write(0, 32, "Sales History", heading_xf)
1441
    sheet.write(0, 33, "Decision", heading_xf)
1442
    sheet.write(0, 34, "Reason", heading_xf)
1443
    sheet.write(0, 35, "Updated Price", heading_xf)
12396 kshitij.so 1444
 
1445
    sheet_iterator = 1
12476 kshitij.so 1446
    cheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.BUY_BOX).filter(AmazonScrapingHistory.timestamp==timestamp).all()
12396 kshitij.so 1447
    for cheapestItem in cheapestItems:
1448
        amScraping =  cheapestItem[0]
1449
        item = cheapestItem[1]
1450
        sheet.write(sheet_iterator, 0, amScraping.item_id)
1451
        if amScraping.warehouseLocation == 1:
1452
            sku = 'FBA'+str(amScraping.item_id)
1453
            loc = 'MUMBAI'
1454
        else:
1455
            sku = 'FBB'+str(amScraping.item_id)
1456
            loc = 'BANGLORE'
1457
        sheet.write(sheet_iterator, 1, sku)
12471 kshitij.so 1458
        sheet.write(sheet_iterator, 2, amScraping.asin)
12396 kshitij.so 1459
        sheet.write(sheet_iterator, 3, loc)
1460
        sheet.write(sheet_iterator, 4, item.brand)
1461
        sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1462
        sheet.write(sheet_iterator, 6, item.weight)
1463
        sheet.write(sheet_iterator, 7, amScraping.courierCost)
1464
        sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)
12432 kshitij.so 1465
        sheet.write(sheet_iterator, 9, amScraping.promoPrice)
1466
        if amScraping.isPromotion:
1467
            sheet.write(sheet_iterator, 10, "Yes")
1468
        else:
12483 kshitij.so 1469
            sheet.write(sheet_iterator, 10, "No")
12432 kshitij.so 1470
        sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)
12396 kshitij.so 1471
        if amScraping.ourRank > 3:
1472
            sheet.write(sheet_iterator, 12, 'Greater than 3')
1473
        else:
1474
            sheet.write(sheet_iterator, 12, amScraping.ourRank)
1475
        sheet.write(sheet_iterator, 13, amScraping.ourInventory)
12432 kshitij.so 1476
        sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)
1477
        sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)
1478
        sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)
12396 kshitij.so 1479
        sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)
12432 kshitij.so 1480
        sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)
1481
        sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)
1482
        sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)
1483
        sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)
1484
        sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)
12447 kshitij.so 1485
        sheet.write(sheet_iterator, 23, amScraping.otherCost)
1486
        sheet.write(sheet_iterator, 24, amScraping.wanlc)
1487
        sheet.write(sheet_iterator, 25, amScraping.commission)
1488
        sheet.write(sheet_iterator, 26, amScraping.competitorCommission)
1489
        sheet.write(sheet_iterator, 27, amScraping.returnProvision)
12484 kshitij.so 1490
        sheet.write(sheet_iterator, 28, round(amScraping.promoPrice - amScraping.lowestPossibleSp))
12447 kshitij.so 1491
        sheet.write(sheet_iterator, 29, item.risky)
1492
        sheet.write(sheet_iterator, 30, amScraping.proposedSp)
12468 kshitij.so 1493
        sheet.write(sheet_iterator, 31, amScraping.avgSale)
1494
        sheet.write(sheet_iterator, 32, getOosString(saleMap.get(sku)))
12444 kshitij.so 1495
        if amScraping.decision is None:
12468 kshitij.so 1496
            sheet.write(sheet_iterator, 33, 'Auto Pricing Inactive')
12444 kshitij.so 1497
            sheet_iterator+=1
1498
            continue
12468 kshitij.so 1499
        sheet.write(sheet_iterator, 33, Decision._VALUES_TO_NAMES.get(amScraping.decision))
1500
        sheet.write(sheet_iterator, 34, amScraping.reason)
12444 kshitij.so 1501
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_DECREMENT_SUCCESS":
12479 kshitij.so 1502
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.proposedSp))
12444 kshitij.so 1503
        if Decision._VALUES_TO_NAMES.get(amScraping.decision) == "AUTO_INCREMENT_SUCCESS":
12484 kshitij.so 1504
            sheet.write(sheet_iterator, 35, math.ceil(amScraping.promoPrice+max(10,.01*amScraping.promoPrice)))
12396 kshitij.so 1505
        sheet_iterator+=1
1506
 
1507
    sheet = wbk.add_sheet('Negative Margin')
1508
    xstr = lambda s: s or ""
1509
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1510
 
1511
    excel_integer_format = '0'
1512
    integer_style = xlwt.XFStyle()
1513
    integer_style.num_format_str = excel_integer_format
1514
 
1515
    sheet.write(0, 0, "Item Id", heading_xf)
1516
    sheet.write(0, 1, "Amazon Sku", heading_xf)
1517
    sheet.write(0, 2, "Asin", heading_xf)
1518
    sheet.write(0, 3, "Location", heading_xf)
1519
    sheet.write(0, 4, "Brand", heading_xf)
1520
    sheet.write(0, 5, "Product Name", heading_xf)
1521
    sheet.write(0, 6, "Weight", heading_xf)
1522
    sheet.write(0, 7, "Courier Cost", heading_xf)
1523
    sheet.write(0, 8, "Our SP", heading_xf)
12432 kshitij.so 1524
    sheet.write(0, 9, "Promo Price", heading_xf)
1525
    sheet.write(0, 10, "Is Promotion", heading_xf)
1526
    sheet.write(0, 11, "Lowest Possible SP", heading_xf)
12396 kshitij.so 1527
    sheet.write(0, 12, "Rank", heading_xf)
1528
    sheet.write(0, 13, "Our Inventory", heading_xf)
12432 kshitij.so 1529
    sheet.write(0, 14, "Lowest Seller SP", heading_xf)
1530
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1531
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
12396 kshitij.so 1532
    sheet.write(0, 17, "Second Lowest Seller SP", heading_xf)
12432 kshitij.so 1533
    sheet.write(0, 18, "Second Lowest Seller Rating", heading_xf)
1534
    sheet.write(0, 19, "Second Lowest Seller Shipping Time", heading_xf)
1535
    sheet.write(0, 20, "Third Lowest Seller SP", heading_xf)
1536
    sheet.write(0, 21, "Third Lowest Seller Rating", heading_xf)
1537
    sheet.write(0, 22, "Third Lowest Seller Shipping Time", heading_xf)
12447 kshitij.so 1538
    sheet.write(0, 23, "Other Cost", heading_xf)
1539
    sheet.write(0, 24, "WANLC", heading_xf)
1540
    sheet.write(0, 25, "Commission", heading_xf)
1541
    sheet.write(0, 26, "Competitor Commission", heading_xf)
1542
    sheet.write(0, 27, "Return Provision", heading_xf)
1543
    sheet.write(0, 28, "Margin", heading_xf)
1544
    sheet.write(0, 29, "Avg Sale", heading_xf)
1545
    sheet.write(0, 30, "Sales History", heading_xf)
12396 kshitij.so 1546
 
1547
    sheet_iterator = 1
12476 kshitij.so 1548
    amongCheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.NEGATIVE_MARGIN).filter(AmazonScrapingHistory.timestamp==timestamp).all()
12396 kshitij.so 1549
    for amongCheapestItem in amongCheapestItems:
1550
        amScraping =  amongCheapestItem[0]
1551
        item = amongCheapestItem[1]
1552
        sheet.write(sheet_iterator, 0, amScraping.item_id)
1553
        if amScraping.warehouseLocation == 1:
1554
            sku = 'FBA'+str(amScraping.item_id)
1555
            loc = 'MUMBAI'
1556
        else:
1557
            sku = 'FBB'+str(amScraping.item_id)
1558
            loc = 'BANGLORE'
1559
        sheet.write(sheet_iterator, 1, sku)
12471 kshitij.so 1560
        sheet.write(sheet_iterator, 2, amScraping.asin)
12396 kshitij.so 1561
        sheet.write(sheet_iterator, 3, loc)
1562
        sheet.write(sheet_iterator, 4, item.brand)
1563
        sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1564
        sheet.write(sheet_iterator, 6, item.weight)
1565
        sheet.write(sheet_iterator, 7, amScraping.courierCost)
1566
        sheet.write(sheet_iterator, 8, amScraping.ourSellingPrice)
12432 kshitij.so 1567
        sheet.write(sheet_iterator, 9, amScraping.promoPrice)
1568
        if amScraping.isPromotion:
1569
            sheet.write(sheet_iterator, 10, "Yes")
1570
        else:
12483 kshitij.so 1571
            sheet.write(sheet_iterator, 10, "No")
12432 kshitij.so 1572
        sheet.write(sheet_iterator, 11, amScraping.lowestPossibleSp)
12396 kshitij.so 1573
        if amScraping.ourRank > 3:
1574
            sheet.write(sheet_iterator, 12, 'Greater than 3')
1575
        else:
1576
            sheet.write(sheet_iterator, 12, amScraping.ourRank)
1577
        sheet.write(sheet_iterator, 13, amScraping.ourInventory)
12432 kshitij.so 1578
        sheet.write(sheet_iterator, 14, amScraping.lowestSellerSp)
1579
        sheet.write(sheet_iterator, 15, amScraping.lowestSellerRating)
1580
        sheet.write(sheet_iterator, 16, amScraping.lowestSellerShippingTime)
12396 kshitij.so 1581
        sheet.write(sheet_iterator, 17, amScraping.secondLowestSellerSp)
12432 kshitij.so 1582
        sheet.write(sheet_iterator, 18, amScraping.secondLowestSellerRating)
1583
        sheet.write(sheet_iterator, 19, amScraping.secondLowestSellerShippingTime)
1584
        sheet.write(sheet_iterator, 20, amScraping.thirdLowestSellerSp)
1585
        sheet.write(sheet_iterator, 21, amScraping.thirdLowestSellerRating)
1586
        sheet.write(sheet_iterator, 22, amScraping.thirdLowestSellerShippingTime)
12447 kshitij.so 1587
        sheet.write(sheet_iterator, 23, amScraping.otherCost)
1588
        sheet.write(sheet_iterator, 24, amScraping.wanlc)
1589
        sheet.write(sheet_iterator, 25, amScraping.commission)
1590
        sheet.write(sheet_iterator, 26, amScraping.competitorCommission)
1591
        sheet.write(sheet_iterator, 27, amScraping.returnProvision)
12484 kshitij.so 1592
        sheet.write(sheet_iterator, 28, round(amScraping.promoPrice - amScraping.lowestPossibleSp))
12447 kshitij.so 1593
        sheet.write(sheet_iterator, 29, amScraping.avgSale)
1594
        sheet.write(sheet_iterator, 30, getOosString(saleMap.get(sku)))
12396 kshitij.so 1595
        sheet_iterator+=1
1596
 
1597
    sheet = wbk.add_sheet('Exception List')
1598
    xstr = lambda s: s or ""
1599
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1600
 
1601
    excel_integer_format = '0'
1602
    integer_style = xlwt.XFStyle()
1603
    integer_style.num_format_str = excel_integer_format
1604
 
1605
    sheet.write(0, 0, "Item Id", heading_xf)
1606
    sheet.write(0, 1, "Amazon Sku", heading_xf)
1607
    sheet.write(0, 2, "Asin", heading_xf)
1608
    sheet.write(0, 3, "Location", heading_xf)
1609
    sheet.write(0, 4, "Brand", heading_xf)
1610
    sheet.write(0, 5, "Product Name", heading_xf)
1611
    sheet.write(0, 6, "Reason", heading_xf)
1612
 
1613
    sheet_iterator = 1
12476 kshitij.so 1614
    amongCheapestItems = session.query(AmazonScrapingHistory,Item).join((Item,AmazonScrapingHistory.item_id==Item.id)).filter(AmazonScrapingHistory.competitiveCategory==CompetitionCategory.EXCEPTION).filter(AmazonScrapingHistory.timestamp==timestamp).all()
12396 kshitij.so 1615
    for amongCheapestItem in amongCheapestItems:
1616
        amScraping =  amongCheapestItem[0]
1617
        item = amongCheapestItem[1]
1618
        sheet.write(sheet_iterator, 0, amScraping.item_id)
1619
        if amScraping.warehouseLocation == 1:
1620
            sku = 'FBA'+str(amScraping.item_id)
1621
            loc = 'MUMBAI'
1622
        else:
1623
            sku = 'FBB'+str(amScraping.item_id)
1624
            loc = 'BANGLORE'
1625
        sheet.write(sheet_iterator, 1, sku)
12471 kshitij.so 1626
        sheet.write(sheet_iterator, 2, amScraping.asin)
12396 kshitij.so 1627
        sheet.write(sheet_iterator, 3, loc)
1628
        sheet.write(sheet_iterator, 4, item.brand)
1629
        sheet.write(sheet_iterator, 5, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1630
        sheet.write(sheet_iterator, 6, amScraping.reason)
1631
        sheet_iterator+=1      
1632
 
12444 kshitij.so 1633
 
1634
    if (runType=='FULL'):    
1635
        sheet = wbk.add_sheet('Auto Favorites')
1636
 
1637
        heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1638
 
1639
        excel_integer_format = '0'
1640
        integer_style = xlwt.XFStyle()
1641
        integer_style.num_format_str = excel_integer_format
1642
        xstr = lambda s: s or ""
1643
 
1644
        sheet.write(0, 0, "Item ID", heading_xf)
1645
        sheet.write(0, 1, "Brand", heading_xf)
1646
        sheet.write(0, 2, "Product Name", heading_xf)
1647
        sheet.write(0, 3, "Auto Favourite", heading_xf)
1648
        sheet.write(0, 4, "Reason", heading_xf)
1649
 
1650
        sheet_iterator=1
1651
        for autoFav in nowAutoFav:
1652
            itemId = autoFav[0]
1653
            reason = autoFav[1]
1654
            it = Item.query.filter_by(id=itemId).one()
1655
            sheet.write(sheet_iterator, 0, itemId)
1656
            sheet.write(sheet_iterator, 1, it.brand)
1657
            sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))
1658
            sheet.write(sheet_iterator, 3, "True")
1659
            sheet.write(sheet_iterator, 4, reason)
1660
            sheet_iterator+=1
1661
        for prevFav in previousAutoFav:
1662
            it = Item.query.filter_by(id=prevFav).one()
1663
            sheet.write(sheet_iterator, 0, prevFav)
1664
            sheet.write(sheet_iterator, 1, it.brand)
1665
            sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))
1666
            sheet.write(sheet_iterator, 3, "False")
1667
            sheet_iterator+=1
1668
 
12478 kshitij.so 1669
    filename = "/tmp/amazon-report-"+runType+" " + str(timestamp) + ".xls"
12396 kshitij.so 1670
    wbk.save(filename)
1671
 
12363 kshitij.so 1672
def main():
1673
    parser = optparse.OptionParser()
1674
    parser.add_option("-t", "--type", dest="runType",
1675
                   default="FULL", type="string",
1676
                   help="Run type FULL or FAVOURITE")
1677
    (options, args) = parser.parse_args()
1678
    if options.runType not in ('FULL','FAVOURITE'):
1679
        print "Run type argument illegal."
1680
        sys.exit(1)
1681
    time.sleep(5)
1682
    timestamp = datetime.now()
1683
    fetchFbaSale()
1684
    itemInfo = populateStuff(timestamp,options.runType)
1685
    itemsToPopulate = 0
12430 kshitij.so 1686
    toSync = 0
1687
    lenItems = len(itemInfo)
1688
    while(toSync < lenItems):
1689
        oldSync = toSync
1690
        if lenItems >= 20:
1691
            toSync = 20
1692
        else:
1693
            toSync = lenItems - oldSync
1694
        getPriceAndAsin(itemInfo[oldSync:toSync+oldSync])
1695
        toSync = oldSync + toSync
1696
 
12363 kshitij.so 1697
    while (len(itemInfo)>0):
12430 kshitij.so 1698
        if len(itemInfo) >= 20:
1699
            itemsToPopulate = 20
12363 kshitij.so 1700
        else:
1701
            itemsToPopulate = len(itemInfo)
12456 kshitij.so 1702
        print "items to popluate"
12370 kshitij.so 1703
        print itemsToPopulate
12363 kshitij.so 1704
        exceptionList, negativeMargin, cheapest, amongCheapestAndCanCompete, canCompete, almostCompete, cantCompete = decideCategory(itemInfo[0:itemsToPopulate])
1705
        itemInfo[0:itemsToPopulate] = []
1706
        commitExceptionList(exceptionList,timestamp,options.runType)
1707
        commitNegativeMargin(negativeMargin,timestamp,options.runType)
1708
        commitCheapest(cheapest,timestamp,options.runType)
1709
        commitAmongCheapestAndCanCompete(amongCheapestAndCanCompete,timestamp,options.runType)
1710
        commitCanCompete(canCompete,timestamp,options.runType)
1711
        commitAlmostCompete(almostCompete,timestamp,options.runType)
1712
        commitCantCompete(cantCompete, timestamp,options.runType)
12396 kshitij.so 1713
        exceptionList[:], negativeMargin[:], cheapest[:], amongCheapestAndCanCompete[:], canCompete[:], almostCompete[:], cantCompete[:] =[],[],[],[],[],[],[]
1714
    autoDecreaseItems = fetchItemsForAutoDecrease(timestamp)
1715
    autoIncreaseItems = fetchItemsForAutoIncrease(timestamp)
1716
    previousAutoFav, nowAutoFav = markAutoFavourites(timestamp)
12444 kshitij.so 1717
    writeReport(timestamp,autoDecreaseItems,autoIncreaseItems,previousAutoFav,nowAutoFav,options.runType)
12363 kshitij.so 1718
if __name__=='__main__':
1719
    main()