Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
11193 kshitij.so 1
from elixir import *
2
from sqlalchemy.sql import or_ ,func, asc
3
from shop2020.config.client.ConfigClient import ConfigClient
4
from shop2020.model.v1.catalog.impl import DataService
5
from shop2020.model.v1.catalog.script import FlipkartScraper
6
from shop2020.model.v1.catalog.impl.DataService import FlipkartItem, MarketplaceItems, Item, \
7
Category, SourcePercentageMaster, MarketPlaceHistory, MarketPlaceUpdateHistory, MarketPlaceItemPrice, \
8
SourceCategoryPercentage, SourceItemPercentage
9
from shop2020.thriftpy.model.v1.order.ttypes import OrderSource
10
from shop2020.thriftpy.model.v1.catalog.ttypes import CompetitionCategory, CompetitionBasis, SalesPotential,\
11
Decision, RunType
12
from shop2020.clients.CatalogClient import CatalogClient
13
from shop2020.clients.InventoryClient import InventoryClient
14
import urllib2
15
import requests
16
import time
17
from datetime import date, datetime, timedelta
18
from shop2020.utils import EmailAttachmentSender
19
from shop2020.utils.EmailAttachmentSender import get_attachment_part
20
import math
21
from operator import itemgetter
11560 kshitij.so 22
from functools import partial
11193 kshitij.so 23
import simplejson as json
24
import xlwt
25
import optparse
26
import sys
27
import smtplib
11560 kshitij.so 28
import threading
29
from multiprocessing import Process 
11193 kshitij.so 30
from email.mime.text import MIMEText
31
import email
32
from email.mime.multipart import MIMEMultipart
33
import email.encoders
34
import cookielib
11561 kshitij.so 35
from multiprocessing import Pool
36
from multiprocessing.dummy import Pool as ThreadPool 
11193 kshitij.so 37
 
38
config_client = ConfigClient()
39
host = config_client.get_property('staging_hostname')
40
syncPrice=config_client.get_property('sync_price_on_marketplace')
41
 
42
 
43
DataService.initialize(db_hostname=host)
44
 
45
inventoryMap = {}
46
itemSaleMap = {}
11562 kshitij.so 47
scraper = FlipkartScraper.FlipkartScraper()
11571 kshitij.so 48
testcount = 0
11193 kshitij.so 49
 
50
class __FlipkartDetails:
51
 
52
    def __init__(self,rank ,ourSp , secondLowestSellerSp, prefSellerSp, lowestSellerSp, lowestSellerScore, prefSellerScore, secondLowestSellerScore, ourScore, shippingTimeLowerLimitLowestSeller,shippingTimeUpperLimitLowestSeller, \
53
    shippingTimeLowerLimitPrefSeller, shippingTimeUpperLimitPrefSeller, shippingTimeLowerLimitOur, shippingTimeUpperLimitOur, shippingTimeLowerLimitSecondLowestSeller, shippingTimeUpperLimitSecondLowestSeller, totalAvailableSeller, lowestSellerName, lowestSellerCode, secondLowestSellerName, secondLowestSellerCode, prefSellerName, prefSellerCode, lowestSellerBuyTrend, \
54
    ourBuyTrend, prefSellerBuyTrend, secondLowestSellerBuyTrend, ourCode ):
55
 
56
        self.rank = rank
57
        self.ourSp = ourSp
58
        self.secondLowestSellerSp = secondLowestSellerSp
59
        self.prefSellerSp = prefSellerSp
60
        self.lowestSellerSp = lowestSellerSp
61
        self.lowestSellerScore = lowestSellerScore
62
        self.prefSellerScore = prefSellerScore
63
        self.secondLowestSellerScore = secondLowestSellerScore
64
        self.ourScore = ourScore
65
        self.shippingTimeLowerLimitLowestSeller = shippingTimeLowerLimitLowestSeller
66
        self.shippingTimeUpperLimitLowestSeller = shippingTimeUpperLimitLowestSeller
67
        self.shippingTimeLowerLimitPrefSeller = shippingTimeLowerLimitPrefSeller
68
        self.shippingTimeUpperLimitPrefSeller = shippingTimeUpperLimitPrefSeller
69
        self.shippingTimeLowerLimitOur = shippingTimeLowerLimitOur
70
        self.shippingTimeUpperLimitOur = shippingTimeUpperLimitOur
71
        self.shippingTimeLowerLimitSecondLowestSeller = shippingTimeLowerLimitSecondLowestSeller
72
        self.shippingTimeUpperLimitSecondLowestSeller = shippingTimeUpperLimitSecondLowestSeller
73
        self.totalAvailableSeller = totalAvailableSeller
74
        self.lowestSellerName = lowestSellerName
75
        self.lowestSellerCode = lowestSellerCode
76
        self.secondLowestSellerName = secondLowestSellerName
77
        self.secondLowestSellerCode = secondLowestSellerCode
78
        self.prefSellerName = prefSellerName
79
        self.prefSellerCode = prefSellerCode
80
        self.lowestSellerBuyTrend = lowestSellerBuyTrend
81
        self.ourBuyTrend = ourBuyTrend
82
        self.prefSellerBuyTrend = prefSellerBuyTrend
83
        self.secondLowestSellerBuyTrend = secondLowestSellerBuyTrend
84
        self.ourCode = ourCode
85
 
86
class __FlipkartItemInfo:
87
 
11581 kshitij.so 88
    def __init__(self, fkSerialNumber, nlc, courierCost, item_id, product_group, brand, model_name, model_number, color, weight, parent_category, risky, warehouseId, vatRate, runType, parent_category_name, sourcePercentage, ourFlipkartInventory, skuAtFlipkart, flipkartDetails, stateId):
11193 kshitij.so 89
 
90
        self.fkSerialNumber = fkSerialNumber
91
        self.nlc = nlc
92
        self.courierCost = courierCost
93
        self.item_id = item_id
94
        self.product_group = product_group
95
        self.brand = brand
96
        self.model_name = model_name
97
        self.model_number = model_number
98
        self.color = color
99
        self.weight = weight
100
        self.parent_category = parent_category
101
        self.risky = risky
102
        self.warehouseId = warehouseId
103
        self.vatRate = vatRate
104
        self.runType = runType
105
        self.parent_category_name = parent_category_name
106
        self.sourcePercentage = sourcePercentage
11560 kshitij.so 107
        self.ourFlipkartInventory = ourFlipkartInventory
11571 kshitij.so 108
        self.skuAtFlipkart = skuAtFlipkart
11581 kshitij.so 109
        self.flipkartDetails = flipkartDetails
110
        self.stateId = stateId  
11193 kshitij.so 111
 
112
class __FlipkartPricing:
113
 
114
    def __init__(self, ourSp, ourTp, lowestTp, lowestPossibleTp, secondLowestSellerTp, lowestPossibleSp, prefSellerTp):
115
        self.ourTp = ourTp
116
        self.lowestTp = lowestTp
117
        self.lowestPossibleTp = lowestPossibleTp
118
        self.ourSp = ourSp
119
        self.secondLowestSellerTp = secondLowestSellerTp
120
        self.lowestPossibleSp = lowestPossibleSp
121
        self.prefSellerTp = prefSellerTp
122
 
123
def markReasonForMpItem(mpHistory,reason,decision):
124
    mpHistory.decision = decision
125
    mpHistory.reason = reason
126
 
127
def fetchItemsForAutoDecrease(time):
128
    successfulAutoDecrease = []
129
    autoDecrementItems = session.query(MarketPlaceHistory).join((MarketplaceItems,MarketPlaceHistory.item_id==MarketplaceItems.itemId))\
130
    .filter(MarketPlaceHistory.timestamp==time).filter(MarketPlaceHistory.source==OrderSource.FLIPKART).filter(MarketPlaceHistory.competitiveCategory==CompetitionCategory.COMPETITIVE)\
131
    .filter(MarketplaceItems.source==OrderSource.FLIPKART).filter(MarketplaceItems.autoDecrement==True).all()
132
    inventory_client = InventoryClient().get_client()
133
    global inventoryMap
134
    inventoryMap = inventory_client.getInventorySnapshot(0)
135
    for autoDecrementItem in autoDecrementItems:
136
        if not autoDecrementItem.competitiveCategory == CompetitionCategory.COMPETITIVE:
137
            markReasonForMpItem(autoDecrementItem,'Category is '+CompetitionCategory._VALUES_TO_NAMES.get(autoDecrementItem.competitiveCategory),Decision.AUTO_DECREMENT_FAILED)
138
            continue
139
        if not autoDecrementItem.risky:
140
            markReasonForMpItem(autoDecrementItem,'Item is not risky',Decision.AUTO_DECREMENT_FAILED)
141
            continue
142
        if math.ceil(autoDecrementItem.proposedSellingPrice) >= autoDecrementItem.ourSellingPrice:
143
            markReasonForMpItem(autoDecrementItem,'Proposed SP greater than or equal to current SP',Decision.AUTO_DECREMENT_FAILED)
144
            continue
145
        if autoDecrementItem.proposedSellingPrice < autoDecrementItem.lowestPossibleSp:
146
            markReasonForMpItem(autoDecrementItem,'Proposed SP less than lowest possible SP',Decision.AUTO_DECREMENT_FAILED)
147
            continue
148
        totalAvailability, totalReserved = 0,0
149
        if (not inventoryMap.has_key(autoDecrementItem.item_id)):
150
            markReasonForMpItem(autoDecrementItem,'Inventory info not available',Decision.AUTO_DECREMENT_FAILED)
151
            continue
152
        itemInventory=inventoryMap[autoDecrementItem.item_id]
153
        availableMap  = itemInventory.availability
154
        reserveMap = itemInventory.reserved
155
        for warehouse,availability in availableMap.iteritems():
156
            if warehouse==16:
157
                continue
158
            totalAvailability = totalAvailability+availability
159
        for warehouse,reserve in reserveMap.iteritems():
160
            if warehouse==16:
161
                continue
162
            totalReserved = totalReserved+reserve
163
        if (totalAvailability-totalReserved)<=0:
164
            markReasonForMpItem(autoDecrementItem,'Net availability is 0',Decision.AUTO_DECREMENT_FAILED)
165
            continue
166
        avgSalePerDay = (itemSaleMap.get(autoDecrementItem.item_id))[2]
167
        try:
168
            daysOfStock = (float(totalAvailability-totalReserved))/avgSalePerDay
169
        except ZeroDivisionError,e:
170
            daysOfStock = float("inf")
171
        if daysOfStock<2:
172
            markReasonForMpItem(autoDecrementItem,'Our stock is not enough',Decision.AUTO_DECREMENT_FAILED)
173
            continue
174
 
175
        autoDecrementItem.ourEnoughStock = True
176
        autoDecrementItem.decision = Decision.AUTO_DECREMENT_SUCCESS
177
        autoDecrementItem.reason = 'All conditions for auto decrement true'
178
        successfulAutoDecrease.append(autoDecrementItem)
179
    session.commit()
180
    return successfulAutoDecrease
181
 
182
def fetchItemsForAutoIncrease(time):
183
    successfulAutoIncrease = []
184
    autoIncrementItems = session.query(MarketPlaceHistory).join((MarketplaceItems,MarketPlaceHistory.item_id==MarketplaceItems.itemId))\
185
    .filter(MarketPlaceHistory.timestamp==time).filter(MarketPlaceHistory.source==OrderSource.FLIPKART).filter(MarketPlaceHistory.competitiveCategory==CompetitionCategory.BUY_BOX)\
186
    .filter(MarketplaceItems.source==OrderSource.FLIPKART).filter(MarketplaceItems.autoIncrement==True).all()
187
    for autoIncrementItem in autoIncrementItems:
188
        if not autoIncrementItem.competitiveCategory == CompetitionCategory.BUY_BOX:
189
            markReasonForMpItem(autoIncrementItem,'Category is '+CompetitionCategory._VALUES_TO_NAMES.get(autoIncrementItem.competitiveCategory),Decision.AUTO_INCREMENT_FAILED)
190
            continue
191
        if autoIncrementItem.totalSeller==1 and autoIncrementItem.ourRank==1:
192
            markReasonForMpItem(autoIncrementItem,'We are the only seller',Decision.AUTO_INCREMENT_FAILED)
193
            continue
194
        if autoIncrementItem.proposedSellingPrice <= autoIncrementItem.ourSellingPrice:
195
            markReasonForMpItem(autoIncrementItem,'Proposed SP less than current SP',Decision.AUTO_INCREMENT_FAILED)
196
            continue
197
        if autoIncrementItem.proposedSellingPrice >=10000 and autoIncrementItem.ourSellingPrice<10000:
198
            markReasonForMpItem(autoIncrementItem,'Proposed SP is greater than 10,000 and current sp is less than 10,000',Decision.AUTO_INCREMENT_FAILED)
199
            continue
200
        if getLastDaySale(autoIncrementItem.item_id)<=2:
201
            markReasonForMpItem(autoIncrementItem,'Last day sale is less than 3',Decision.AUTO_INCREMENT_FAILED)
202
            continue
203
        antecedentPrice = session.query(MarketPlaceHistory.ourSellingPrice).filter(MarketPlaceHistory.item_id==autoIncrementItem.item_id).filter(MarketPlaceHistory.source==OrderSource.FLIPKART).filter(MarketPlaceHistory.timestamp>time-timedelta(days=1)).order_by(asc(MarketPlaceHistory.timestamp)).first()
204
        if antecedentPrice is not None:
205
            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:
206
                markReasonForMpItem(autoIncrementItem,'Maximum price increase in last 24 hours should be 2%',Decision.AUTO_INCREMENT_FAILED)
207
                continue
208
        mpItem = MarketplaceItems.get_by(itemId=autoIncrementItem.item_id,source=OrderSource.FLIPKART)
209
        if mpItem.maximumSellingPrice is not None and mpItem.maximumSellingPrice > 0:
210
            if autoIncrementItem.ourSellingPrice+max(10,.01*autoIncrementItem.ourSellingPrice) > mpItem.maximumSellingPrice:
211
                markReasonForMpItem(autoIncrementItem,'Price cannot exceed Maximum Selling Price',Decision.AUTO_INCREMENT_FAILED)
212
                continue
213
        #oosStatus = inventory_client.getOosStatusesForXDaysForItem(autoIncrementItem.item_id,0,3)
214
        #count,sale,daysOfStock = 0,0,0
215
        #for obj in oosStatus:
216
        #    if not obj.is_oos:
217
        #        count+=1
218
        #        sale = sale+obj.num_orders
219
        #avgSalePerDay=0 if count==0 else (float(sale)/count)
220
        totalAvailability, totalReserved = 0,0
221
        if (not inventoryMap.has_key(autoIncrementItem.item_id)):
222
            markReasonForMpItem(autoIncrementItem,'Inventory info not available',Decision.AUTO_INCREMENT_FAILED)
223
            continue
224
        itemInventory=inventoryMap[autoIncrementItem.item_id]
225
        availableMap  = itemInventory.availability
226
        reserveMap = itemInventory.reserved
227
        for warehouse,availability in availableMap.iteritems():
228
            if warehouse==16:
229
                continue
230
            totalAvailability = totalAvailability+availability
231
        for warehouse,reserve in reserveMap.iteritems():
232
            if warehouse==16:
233
                continue
234
            totalReserved = totalReserved+reserve
235
        #if (totalAvailability-totalReserved)<=0:
236
        #    markReasonForMpItem(autoIncrementItem,'Our stock is 0',Decision.AUTO_INCREMENT_FAILED)
237
        #    continue
238
        avgSalePerDay = (itemSaleMap.get(autoIncrementItem.item_id))[2]
239
        if (avgSalePerDay==0):
240
            markReasonForMpItem(autoIncrementItem,'Average sale per day is zero',Decision.AUTO_INCREMENT_FAILED)
241
            continue
242
        daysOfStock = (float(totalAvailability-totalReserved))/avgSalePerDay
243
        if daysOfStock>5:
244
            markReasonForMpItem(autoIncrementItem,'Our stock is enough',Decision.AUTO_INCREMENT_FAILED)
245
            continue
246
 
247
        autoIncrementItem.ourEnoughStock = False
248
        autoIncrementItem.decision = Decision.AUTO_INCREMENT_SUCCESS
249
        autoIncrementItem.reason = 'All conditions for auto increment true'
250
        successfulAutoIncrease.append(autoIncrementItem)
251
    session.commit()
252
    return successfulAutoIncrease        
253
 
254
 
255
def commitExceptionList(exceptionList,timestamp):
256
    exceptionItems=[]
257
    for item in exceptionList:
258
        mpHistory = MarketPlaceHistory()
259
        mpHistory.item_id =item.item_id
260
        mpHistory.source = OrderSource.FLIPKART 
261
        mpHistory.competitiveCategory = CompetitionCategory.EXCEPTION
262
        mpHistory.risky = item.risky
263
        mpHistory.timestamp = timestamp
264
        mpHistory.run = RunType._NAMES_TO_VALUES.get(item.runType)
265
        exceptionItems.append(mpHistory)
266
    session.commit()
267
    return exceptionItems
268
 
269
def commitCantCompete(cantCompete,timestamp):
270
    cantComepeteItems = []
271
    for item in cantCompete:
272
        flipkartDetails = item[0]
273
        flipkartItemInfo = item[1]
274
        flipkartPricing = item[2]
275
        mpItem = item[3]
276
        mpHistory = MarketPlaceHistory()
277
        mpHistory.item_id = flipkartItemInfo.item_id
278
        mpHistory.source = OrderSource.FLIPKART
279
        mpHistory.lowestTp = flipkartPricing.lowestTp
280
        mpHistory.lowestPossibleTp = flipkartPricing.lowestPossibleTp
281
        mpHistory.lowestPossibleSp = flipkartPricing.lowestPossibleSp
282
        mpHistory.ourInventory = flipkartItemInfo.ourFlipkartInventory
283
        mpHistory.ourRank = flipkartDetails.rank
284
        mpHistory.competitiveCategory = CompetitionCategory.CANT_COMPETE
285
        mpHistory.risky = flipkartItemInfo.risky
286
        mpHistory.lowestSellingPrice = flipkartDetails.lowestSellerSp
287
        mpHistory.lowestSellerName = flipkartDetails.lowestSellerName
288
        mpHistory.lowestSellerCode = flipkartDetails.lowestSellerCode
289
        mpHistory.lowestSellerRating = flipkartDetails.lowestSellerScore
290
        mpHistory.lowestSellerShippingTime = ''
291
        mpHistory.lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
292
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
293
        mpHistory.ourSellingPrice = flipkartPricing.ourSp
294
        mpHistory.ourTp = flipkartPricing.ourTp
295
        mpHistory.ourNlc = flipkartItemInfo.nlc
296
        mpHistory.ourRating = flipkartDetails.ourScore
297
        mpHistory.ourShippingTime = ''
298
        mpHistory.ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
299
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
300
        mpHistory.prefferedSellerName = flipkartDetails.prefSellerName
301
        mpHistory.prefferedSellerCode = flipkartDetails.prefSellerCode
302
        mpHistory.prefferedSellerRating = flipkartDetails.prefSellerScore
303
        mpHistory.prefferedSellerSellingPrice = flipkartDetails.prefSellerSp
304
        mpHistory.prefferedSellerTp = flipkartPricing.prefSellerTp
305
        mpHistory.prefferedSellerShippingTime = ''
306
        mpHistory.prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
307
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
308
        proposed_sp = flipkartDetails.lowestSellerSp - max(10, flipkartDetails.lowestSellerSp*0.001)
309
        proposed_tp = getTargetTp(proposed_sp,mpItem)
310
        target_nlc = proposed_tp - flipkartPricing.lowestPossibleTp + flipkartItemInfo.nlc
311
        mpHistory.proposedSellingPrice = round(proposed_sp,2)
312
        mpHistory.proposedTp = round(proposed_tp,2)
313
        mpHistory.targetNlc = round(target_nlc,2)
314
        mpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTp
315
        mpHistory.totalSeller = flipkartDetails.totalAvailableSeller
316
        mpHistory.avgSales = (itemSaleMap.get(flipkartItemInfo.item_id))[3]
317
        mpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
318
        mpHistory.timestamp = timestamp
319
        mpHistory.run = RunType._NAMES_TO_VALUES.get(flipkartItemInfo.runType)
320
        cantComepeteItems.append(mpHistory)
321
    session.commit()
322
    return cantComepeteItems
323
 
324
def commitBuyBox(buyBoxItems,timestamp):
325
    buyBoxList = []
326
    for item in buyBoxItems:
327
        flipkartDetails = item[0]
328
        flipkartItemInfo = item[1]
329
        flipkartPricing = item[2]
330
        mpItem = item[3]
331
        mpHistory = MarketPlaceHistory()
332
        mpHistory.item_id = flipkartItemInfo.item_id
333
        mpHistory.source = OrderSource.FLIPKART
334
        mpHistory.lowestPossibleTp = flipkartPricing.lowestPossibleTp
335
        mpHistory.lowestPossibleSp = flipkartPricing.lowestPossibleSp
336
        mpHistory.ourInventory = flipkartItemInfo.ourFlipkartInventory
337
        mpHistory.ourRank = flipkartDetails.rank
338
        mpHistory.ourSellingPrice = flipkartPricing.ourSp
339
        mpHistory.ourTp = flipkartPricing.ourTp
340
        mpHistory.ourNlc = flipkartItemInfo.nlc
341
        mpHistory.ourRating = flipkartDetails.ourScore
342
        mpHistory.ourShippingTime = ''
343
        mpHistory.ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
344
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
345
        mpHistory.competitiveCategory = CompetitionCategory.BUY_BOX
346
        mpHistory.risky = flipkartItemInfo.risky
347
        mpHistory.lowestSellingPrice = flipkartDetails.lowestSellerSp
348
        mpHistory.lowestTp = flipkartPricing.lowestTp
349
        mpHistory.lowestSellerName = flipkartDetails.lowestSellerName
350
        mpHistory.lowestSellerCode = flipkartDetails.lowestSellerCode
351
        mpHistory.lowestSellerRating = flipkartDetails.lowestSellerScore
352
        mpHistory.lowestSellerShippingTime = ''
353
        mpHistory.lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
354
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
355
        proposed_sp = max(flipkartDetails.secondLowestSellerSp - max((20, flipkartDetails.secondLowestSellerSp*0.002)), flipkartPricing.lowestPossibleSp)
356
        proposed_tp = getTargetTp(proposed_sp,mpItem)
357
        #target_nlc = proposed_tp - flipkartPricing.lowestPossibleTp + flipkartItemInfo.nlc
358
        mpHistory.proposedSellingPrice = round(proposed_sp,2)
359
        mpHistory.proposedTp = round(proposed_tp,2)
360
        #mpHistory.targetNlc = target_nlc
361
        mpHistory.secondLowestSellerName = flipkartDetails.secondLowestSellerName
362
        mpHistory.secondLowestSellerCode = flipkartDetails.secondLowestSellerCode
363
        mpHistory.secondLowestSellingPrice = flipkartDetails.secondLowestSellerSp
364
        mpHistory.secondLowestTp = flipkartPricing.secondLowestSellerTp
365
        mpHistory.secondLowestSellerRating = flipkartDetails.secondLowestSellerScore
366
        mpHistory.secondLowestSellerShippingTime = ''
367
        mpHistory.secondLowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitSecondLowestSeller) if flipkartDetails.shippingTimeUpperLimitSecondLowestSeller==0\
368
        else str(flipkartDetails.shippingTimeLowerLimitSecondLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitSecondLowestSeller)
369
        mpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTp
370
        mpHistory.marginIncreasedPotential = proposed_tp - flipkartPricing.ourTp
371
        mpHistory.totalSeller = flipkartDetails.totalAvailableSeller
372
        mpHistory.avgSales = (itemSaleMap.get(flipkartItemInfo.item_id))[3]
373
        mpHistory.run = RunType._NAMES_TO_VALUES.get(flipkartItemInfo.runType)
374
        mpHistory.timestamp = timestamp
375
        buyBoxList.append(mpHistory)
376
    session.commit()
377
    return buyBoxList
378
 
379
def commitCompetitiveNoInventory(competitiveNoInventory,timestamp):
380
    competitiveNoInventoryItems = []
381
    for item in competitiveNoInventory:
382
        flipkartDetails = item[0]
383
        flipkartItemInfo = item[1]
384
        flipkartPricing = item[2]
385
        mpItem = item[3]
386
        mpHistory = MarketPlaceHistory()
387
        mpHistory.item_id = flipkartItemInfo.item_id
388
        mpHistory.source = OrderSource.FLIPKART
389
        mpHistory.lowestTp = flipkartPricing.lowestTp
390
        mpHistory.lowestPossibleTp = flipkartPricing.lowestPossibleTp
391
        mpHistory.lowestPossibleSp = flipkartPricing.lowestPossibleSp
392
        mpHistory.ourInventory = flipkartItemInfo.ourFlipkartInventory
393
        mpHistory.ourRank = flipkartDetails.rank
394
        mpHistory.competitiveCategory = CompetitionCategory.COMPETITIVE_NO_INVENTORY
395
        mpHistory.risky = flipkartItemInfo.risky
396
        mpHistory.lowestSellingPrice = flipkartDetails.lowestSellerSp
397
        mpHistory.lowestSellerName = flipkartDetails.lowestSellerName
398
        mpHistory.lowestSellerCode = flipkartDetails.lowestSellerCode
399
        mpHistory.lowestSellerRating = flipkartDetails.lowestSellerScore
400
        mpHistory.lowestSellerShippingTime = ''
401
        mpHistory.lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
402
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
403
        mpHistory.ourSellingPrice = flipkartPricing.ourSp
404
        mpHistory.ourTp = flipkartPricing.ourTp
405
        mpHistory.ourNlc = flipkartItemInfo.nlc
406
        mpHistory.ourRating = flipkartDetails.ourScore
407
        mpHistory.ourShippingTime = ''
408
        mpHistory.ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
409
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
410
        mpHistory.prefferedSellerName = flipkartDetails.prefSellerName
411
        mpHistory.prefferedSellerCode = flipkartDetails.prefSellerCode
412
        mpHistory.prefferedSellerRating = flipkartDetails.prefSellerScore
413
        mpHistory.prefferedSellerSellingPrice = flipkartDetails.prefSellerSp
414
        mpHistory.prefferedSellerTp = flipkartPricing.prefSellerTp
415
        mpHistory.prefferedSellerShippingTime = ''
416
        mpHistory.prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
417
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
418
        proposed_sp = max(flipkartDetails.lowestSellerSp - max((10, flipkartDetails.lowestSellerSp*0.001)), flipkartPricing.lowestPossibleSp)
419
        proposed_tp = getTargetTp(proposed_sp,mpItem)
420
        mpHistory.proposedSellingPrice = round(proposed_sp,2)
421
        mpHistory.proposedTp = round(proposed_tp,2)
422
        mpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTp
423
        mpHistory.totalSeller = flipkartDetails.totalAvailableSeller
424
        mpHistory.avgSales = (itemSaleMap.get(flipkartItemInfo.item_id))[3]
425
        mpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
426
        mpHistory.timestamp = timestamp
427
        mpHistory.run = RunType._NAMES_TO_VALUES.get(flipkartItemInfo.runType)
428
        competitiveNoInventoryItems.append(mpHistory)
429
    session.commit()
430
    return competitiveNoInventoryItems
431
 
432
def commitCompetitive(competitive,timestamp):
433
    competitiveItems = []
434
    for item in competitive:
435
        flipkartDetails = item[0]
436
        flipkartItemInfo = item[1]
437
        flipkartPricing = item[2]
438
        mpItem = item[3]
439
        mpHistory = MarketPlaceHistory()
440
        mpHistory.item_id = flipkartItemInfo.item_id
441
        mpHistory.source = OrderSource.FLIPKART
442
        mpHistory.lowestTp = flipkartPricing.lowestTp
443
        mpHistory.lowestPossibleTp = flipkartPricing.lowestPossibleTp
444
        mpHistory.lowestPossibleSp = flipkartPricing.lowestPossibleSp
445
        mpHistory.ourInventory = flipkartItemInfo.ourFlipkartInventory
446
        mpHistory.ourRank = flipkartDetails.rank
447
        mpHistory.competitiveCategory = CompetitionCategory.COMPETITIVE
448
        mpHistory.risky = flipkartItemInfo.risky
449
        mpHistory.lowestSellingPrice = flipkartDetails.lowestSellerSp
450
        mpHistory.lowestSellerName = flipkartDetails.lowestSellerName
451
        mpHistory.lowestSellerCode = flipkartDetails.lowestSellerCode
452
        mpHistory.lowestSellerRating = flipkartDetails.lowestSellerScore
453
        mpHistory.lowestSellerShippingTime = ''
454
        mpHistory.lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
455
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
456
        mpHistory.ourSellingPrice = flipkartPricing.ourSp
457
        mpHistory.ourTp = flipkartPricing.ourTp
458
        mpHistory.ourNlc = flipkartItemInfo.nlc
459
        mpHistory.ourRating = flipkartDetails.ourScore
460
        mpHistory.ourShippingTime = ''
461
        mpHistory.ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
462
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
463
        mpHistory.prefferedSellerName = flipkartDetails.prefSellerName
464
        mpHistory.prefferedSellerCode = flipkartDetails.prefSellerCode
465
        mpHistory.prefferedSellerRating = flipkartDetails.prefSellerScore
466
        mpHistory.prefferedSellerSellingPrice = flipkartDetails.prefSellerSp
467
        mpHistory.prefferedSellerTp = flipkartPricing.prefSellerTp
468
        mpHistory.prefferedSellerShippingTime = ''
469
        mpHistory.prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
470
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
471
        proposed_sp = max(flipkartDetails.lowestSellerSp - max((10, flipkartDetails.lowestSellerSp*0.001)), flipkartPricing.lowestPossibleSp)
472
        proposed_tp = getTargetTp(proposed_sp,mpItem)
473
        mpHistory.proposedSellingPrice = round(proposed_sp,2)
474
        mpHistory.proposedTp = round(proposed_tp,2)
475
        mpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTp
476
        mpHistory.totalSeller = flipkartDetails.totalAvailableSeller
477
        mpHistory.avgSales = (itemSaleMap.get(flipkartItemInfo.item_id))[3]
478
        mpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
479
        mpHistory.timestamp = timestamp
480
        mpHistory.run = RunType._NAMES_TO_VALUES.get(flipkartItemInfo.runType)
481
        competitiveItems.append(mpHistory)
482
    session.commit()
483
    return competitiveItems
484
 
485
def commitNegativeMargin(negativeMargin,timestamp):
486
    negativeItems = []
487
    for item in negativeMargin:
488
        flipkartDetails = item[0]
489
        flipkartItemInfo = item[1]
490
        flipkartPricing = item[2]
491
        mpHistory = MarketPlaceHistory()
492
        mpHistory.item_id = flipkartItemInfo.item_id
493
        mpHistory.source = OrderSource.FLIPKART
494
        mpHistory.lowestTp = flipkartPricing.lowestTp
495
        mpHistory.lowestPossibleTp = flipkartPricing.lowestPossibleTp
496
        mpHistory.lowestPossibleSp = flipkartPricing.lowestPossibleSp
497
        mpHistory.ourInventory = flipkartItemInfo.ourFlipkartInventory
498
        mpHistory.ourRank = flipkartDetails.rank
499
        mpHistory.competitiveCategory = CompetitionCategory.NEGATIVE_MARGIN
500
        mpHistory.risky = flipkartItemInfo.risky
501
        mpHistory.lowestSellingPrice = flipkartDetails.lowestSellerSp
502
        mpHistory.lowestSellerName = flipkartDetails.lowestSellerName
503
        mpHistory.lowestSellerCode = flipkartDetails.lowestSellerCode
504
        mpHistory.lowestSellerRating = flipkartDetails.lowestSellerScore
505
        mpHistory.lowestSellerShippingTime = ''
506
        mpHistory.lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
507
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
508
        mpHistory.ourSellingPrice = flipkartPricing.ourSp
509
        mpHistory.ourTp = flipkartPricing.ourTp
510
        mpHistory.ourNlc = flipkartItemInfo.nlc
511
        mpHistory.ourRating = flipkartDetails.ourScore
512
        mpHistory.ourShippingTime = ''
513
        mpHistory.ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
514
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
515
        mpHistory.prefferedSellerName = flipkartDetails.prefSellerName
516
        mpHistory.prefferedSellerCode = flipkartDetails.prefSellerCode
517
        mpHistory.prefferedSellerRating = flipkartDetails.prefSellerScore
518
        mpHistory.prefferedSellerSellingPrice = flipkartDetails.prefSellerSp
519
        mpHistory.prefferedSellerTp = flipkartPricing.prefSellerTp
520
        mpHistory.prefferedSellerShippingTime = ''
521
        mpHistory.prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
522
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
523
        mpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTp
524
        mpHistory.totalSeller = flipkartDetails.totalAvailableSeller
525
        mpHistory.avgSales = (itemSaleMap.get(flipkartItemInfo.item_id))[3]
526
        mpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
527
        mpHistory.timestamp = timestamp
528
        mpHistory.run = RunType._NAMES_TO_VALUES.get(flipkartItemInfo.runType)
529
        negativeItems.append(mpHistory)
530
    session.commit()
531
    return negativeItems
532
 
533
def commitCheapButNotPref(cheapButNotPref,timestamp):
534
    cheapButNotPrefItems = []
535
    for item in cheapButNotPref:
536
        flipkartDetails = item[0]
537
        flipkartItemInfo = item[1]
538
        flipkartPricing = item[2]
539
        mpHistory = MarketPlaceHistory()
540
        mpHistory.item_id = flipkartItemInfo.item_id
541
        mpHistory.source = OrderSource.FLIPKART
542
        mpHistory.lowestTp = flipkartPricing.lowestTp
543
        mpHistory.lowestPossibleTp = flipkartPricing.lowestPossibleTp
544
        mpHistory.lowestPossibleSp = flipkartPricing.lowestPossibleSp
545
        mpHistory.ourInventory = flipkartItemInfo.ourFlipkartInventory
546
        mpHistory.ourRank = flipkartDetails.rank
547
        mpHistory.competitiveCategory = CompetitionCategory.CHEAP_BUT_NOT_PREF
548
        mpHistory.risky = flipkartItemInfo.risky
549
        mpHistory.lowestSellingPrice = flipkartDetails.lowestSellerSp
550
        mpHistory.lowestSellerName = flipkartDetails.lowestSellerName
551
        mpHistory.lowestSellerCode = flipkartDetails.lowestSellerCode
552
        mpHistory.lowestSellerRating = flipkartDetails.lowestSellerScore
553
        mpHistory.lowestSellerShippingTime = ''
554
        mpHistory.lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
555
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
556
        mpHistory.ourSellingPrice = flipkartPricing.ourSp
557
        mpHistory.ourTp = flipkartPricing.ourTp
558
        mpHistory.ourNlc = flipkartItemInfo.nlc
559
        mpHistory.ourRating = flipkartDetails.ourScore
560
        mpHistory.ourShippingTime = ''
561
        mpHistory.ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
562
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
563
        mpHistory.prefferedSellerName = flipkartDetails.prefSellerName
564
        mpHistory.prefferedSellerCode = flipkartDetails.prefSellerCode
565
        mpHistory.prefferedSellerRating = flipkartDetails.prefSellerScore
566
        mpHistory.prefferedSellerSellingPrice = flipkartDetails.prefSellerSp
567
        mpHistory.prefferedSellerTp = flipkartPricing.prefSellerTp
568
        mpHistory.prefferedSellerShippingTime = ''
569
        mpHistory.prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
570
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
571
        mpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTp
572
        mpHistory.totalSeller = flipkartDetails.totalAvailableSeller
573
        mpHistory.avgSales = (itemSaleMap.get(flipkartItemInfo.item_id))[3]
574
        mpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
575
        mpHistory.timestamp = timestamp
576
        mpHistory.run = RunType._NAMES_TO_VALUES.get(flipkartItemInfo.runType)
577
        cheapButNotPrefItems.append(mpHistory)
578
    session.commit()
579
    return cheapButNotPrefItems
580
 
581
def commitPrefButNotCheap(prefButNotCheap,timestamp):
582
    prefButNotCheapItems = []
583
    for item in prefButNotCheap:
584
        flipkartDetails = item[0]
585
        flipkartItemInfo = item[1]
586
        flipkartPricing = item[2]
587
        mpHistory = MarketPlaceHistory()
588
        mpHistory.item_id = flipkartItemInfo.item_id
589
        mpHistory.source = OrderSource.FLIPKART
590
        mpHistory.lowestTp = flipkartPricing.lowestTp
591
        mpHistory.lowestPossibleTp = flipkartPricing.lowestPossibleTp
592
        mpHistory.lowestPossibleSp = flipkartPricing.lowestPossibleSp
593
        mpHistory.ourInventory = flipkartItemInfo.ourFlipkartInventory
594
        mpHistory.ourRank = flipkartDetails.rank
595
        mpHistory.competitiveCategory = CompetitionCategory.PREF_BUT_NOT_CHEAP
596
        mpHistory.risky = flipkartItemInfo.risky
597
        mpHistory.lowestSellingPrice = flipkartDetails.lowestSellerSp
598
        mpHistory.lowestSellerName = flipkartDetails.lowestSellerName
599
        mpHistory.lowestSellerCode = flipkartDetails.lowestSellerCode
600
        mpHistory.lowestSellerRating = flipkartDetails.lowestSellerScore
601
        mpHistory.lowestSellerShippingTime = ''
602
        mpHistory.lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
603
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
604
        mpHistory.ourSellingPrice = flipkartPricing.ourSp
605
        mpHistory.ourTp = flipkartPricing.ourTp
606
        mpHistory.ourNlc = flipkartItemInfo.nlc
607
        mpHistory.ourRating = flipkartDetails.ourScore
608
        mpHistory.ourShippingTime = ''
609
        mpHistory.ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
610
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
611
        mpHistory.prefferedSellerName = flipkartDetails.prefSellerName
612
        mpHistory.prefferedSellerCode = flipkartDetails.prefSellerCode
613
        mpHistory.prefferedSellerRating = flipkartDetails.prefSellerScore
614
        mpHistory.prefferedSellerSellingPrice = flipkartDetails.prefSellerSp
615
        mpHistory.prefferedSellerTp = flipkartPricing.prefSellerTp
616
        mpHistory.prefferedSellerShippingTime = ''
617
        mpHistory.prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
618
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
619
        mpHistory.margin = mpHistory.ourTp - mpHistory.lowestPossibleTp
620
        mpHistory.totalSeller = flipkartDetails.totalAvailableSeller
621
        mpHistory.avgSales = (itemSaleMap.get(flipkartItemInfo.item_id))[3]
622
        mpHistory.salesPotential = SalesPotential._NAMES_TO_VALUES.get(getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
623
        mpHistory.timestamp = timestamp
624
        mpHistory.run = RunType._NAMES_TO_VALUES.get(flipkartItemInfo.runType)
625
        prefButNotCheapItems.append(mpHistory)
626
    session.commit()
627
    return prefButNotCheapItems
628
 
629
def populateStuff(runType,time):
11581 kshitij.so 630
    global itemSaleMap
11193 kshitij.so 631
    itemInfo = []
632
    if runType=='FAVOURITE':
633
        items = session.query(FlipkartItem,MarketplaceItems).join((MarketplaceItems,FlipkartItem.item_id==MarketplaceItems.itemId)).filter(MarketplaceItems.source==OrderSource.FLIPKART).\
634
        filter(or_(MarketplaceItems.autoFavourite==True, MarketplaceItems.manualFavourite==True)).all()
635
    else:
636
        #items = session.query(FlipkartItem,MarketplaceItems).join((MarketplaceItems,FlipkartItem.item_id==MarketplaceItems.itemId)).filter(MarketplaceItems.source==OrderSource.FLIPKART).all()
637
        items = session.query(FlipkartItem,MarketplaceItems).join((MarketplaceItems,FlipkartItem.item_id==MarketplaceItems.itemId)).filter(MarketplaceItems.source==OrderSource.FLIPKART).all()
11581 kshitij.so 638
 
639
    inventory_client = InventoryClient().get_client()
640
 
11193 kshitij.so 641
    for item in items:
642
        flipkart_item = item[0]
643
        mp_item = item[1]
644
        it = Item.query.filter_by(id=flipkart_item.item_id).one()
645
        category = Category.query.filter_by(id=it.category).one()
646
        parent_category = Category.query.filter_by(id=category.parent_category_id).first()
647
        sip = SourceItemPercentage.query.filter(SourceItemPercentage.item_id==it.id).filter(SourceItemPercentage.source==OrderSource.FLIPKART).filter(SourceItemPercentage.startDate<=time).filter(SourceItemPercentage.expiryDate>=time).first()
648
        sourcePercentage = None
649
        if sip is not None:
650
            sourcePercentage = sip
651
        else:
652
            scp = SourceCategoryPercentage.query.filter(SourceCategoryPercentage.category_id==it.category).filter(SourceCategoryPercentage.source==OrderSource.FLIPKART).filter(SourceCategoryPercentage.startDate<=time).filter(SourceCategoryPercentage.expiryDate>=time).first()
653
            if scp is not None:
654
                sourcePercentage = scp
655
            else:
656
                spm = SourcePercentageMaster.get_by(source=OrderSource.FLIPKART)
657
                sourcePercentage = spm
11581 kshitij.so 658
 
659
        warehouse = inventory_client.getWarehouse(flipkart_item.warehouseId)    
660
        itemSaleList = []
661
        oosForAllSources = inventory_client.getOosStatusesForXDaysForItem(flipkart_item.item_id, 0, 3)
662
        oosForFlipkart = inventory_client.getOosStatusesForXDaysForItem(flipkart_item.item_id, OrderSource.FLIPKART, 5)
663
        oosForFlipkartLastDay = inventory_client.getOosStatusesForXDaysForItem(flipkart_item.item_id, OrderSource.FLIPKART, 1)
664
        itemSaleList.append(oosForAllSources)
665
        itemSaleList.append(oosForFlipkart)
666
        itemSaleList.append(calculateAverageSale(oosForAllSources))
667
        itemSaleList.append(calculateAverageSale(oosForFlipkart))
668
        itemSaleList.append(calculateAverageSale(oosForFlipkartLastDay))
669
        itemSaleList.append(calculateTotalSale(oosForFlipkart))
670
        itemSaleMap[flipkart_item.item_id]=itemSaleList
671
 
11560 kshitij.so 672
#        try:
673
#            request_url = "https://api.flipkart.net/sellers/skus/%s/listings"%(str(flipkart_item.skuAtFlipkart))
674
#            r = requests.get(request_url, auth=('m2z93iskuj81qiid', '0c7ab6a5-98c0-4cdc-8be3-72c591e0add4'))
675
#            print "Inventory info",r.json()
676
#            stock_count = int((r.json()['attributeValues'])['stock_count'])
677
#        except:
678
#            stock_count = 0
11581 kshitij.so 679
        flipkartItemInfo = __FlipkartItemInfo(flipkart_item.flipkartSerialNumber, flipkart_item.maxNlc,mp_item.courierCost, it.id, it.product_group, it.brand, it.model_name, it.model_number, it.color, it.weight, category.parent_category_id, it.risky, flipkart_item.warehouseId, None, runType, parent_category.display_name,sourcePercentage,None,flipkart_item.skuAtFlipkart,None,warehouse.stateId)
11193 kshitij.so 680
        itemInfo.append(flipkartItemInfo)
681
    return itemInfo
682
 
683
def fetchDetails(flipkartSerialNumber,scraper):
684
    url = "http://www.flipkart.com/ps/%s"%(flipkartSerialNumber)
685
    #url = "http://www.flipkart.com/ps/MOBDTXVZXVY3GFG8"
686
    scraper.read(url)
687
    vendorsData = scraper.createData()
688
    print "Vendor data for flipkartSerialNumber",flipkartSerialNumber
689
    print "Json Data",vendorsData
11581 kshitij.so 690
    sortedVendorsData = []
11193 kshitij.so 691
    sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
692
    rank ,ourSp, iterator, secondLowestSellerSp, prefSellerSp, lowestSellerSp, lowestSellerScore, prefSellerScore, secondLowestSellerScore, ourScore, shippingTimeLowerLimitLowestSeller,shippingTimeUpperLimitLowestSeller, \
693
    shippingTimeLowerLimitPrefSeller, shippingTimeUpperLimitPrefSeller, shippingTimeLowerLimitOur, shippingTimeUpperLimitOur, shippingTimeLowerLimitSecondLowestSeller, shippingTimeUpperLimitSecondLowestSeller, totalAvailableSeller= (0,)*19
694
    lowestSellerName, lowestSellerCode, secondLowestSellerName, secondLowestSellerCode, prefSellerName, prefSellerCode, lowestSellerBuyTrend, \
695
    ourBuyTrend, prefSellerBuyTrend, secondLowestSellerBuyTrend, ourCode = ('',)*11
696
    for data in sortedVendorsData:
697
        if iterator == 0:
698
            lowestSellerName = data['sellerName']
699
            lowestSellerScore = data['sellerScore']
700
            lowestSellerCode = data['sellerCode']
701
            lowestSellerSp = data['sellingPrice']
702
            lowestSellerBuyTrend = data['buyTrend']
703
            try:
704
                shippingTimeLowerLimitLowestSeller, shippingTimeUpperLimitLowestSeller = data['shippingTime'].split('-')
705
            except ValueError:
706
                shippingTimeLowerLimitLowestSeller = int(data['shippingTime'])
707
 
708
        if iterator ==1:
709
            secondLowestSellerName = data['sellerName']
710
            secondLowestSellerScore = data['sellerScore']
711
            secondLowestSellerCode = data['sellerCode']
712
            secondLowestSellerSp = data['sellingPrice']
713
            secondLowestSellerBuyTrend = data['buyTrend']
714
            try:
715
                shippingTimeLowerLimitSecondLowestSeller, shippingTimeUpperLimitSecondLowestSeller = data['shippingTime'].split('-')
716
            except ValueError:
717
                shippingTimeLowerLimitSecondLowestSeller = int(data['shippingTime'])
718
 
719
        if data['sellerName'] == 'Saholic':
720
            ourScore = data['sellerScore']
721
            ourCode = data['sellerCode']
722
            ourSp = data['sellingPrice']
723
            ourBuyTrend = data['buyTrend']
724
            try:
725
                shippingTimeLowerLimitOur, shippingTimeUpperLimitOur = data['shippingTime'].split('-')
726
            except ValueError:
727
                shippingTimeLowerLimitOur = int(data['shippingTime'])
728
            rank = iterator + 1
729
 
730
        if data['buyTrend'] in ('PrefCheap','PrefNCheap',''):
731
            prefSellerName = data['sellerName']
732
            prefSellerScore = data['sellerScore']
733
            prefSellerCode = data['sellerCode']
734
            prefSellerSp = data['sellingPrice']
735
            prefSellerBuyTrend = data['buyTrend']
736
            try:
737
                shippingTimeLowerLimitPrefSeller, shippingTimeUpperLimitPrefSeller = data['shippingTime'].split('-')
738
            except ValueError:
739
                shippingTimeLowerLimitPrefSeller = int(data['shippingTime'])
740
 
741
        iterator+=1
742
    flipkartDetails = __FlipkartDetails(rank ,ourSp , secondLowestSellerSp, prefSellerSp, lowestSellerSp, lowestSellerScore, prefSellerScore, secondLowestSellerScore, ourScore, int(shippingTimeLowerLimitLowestSeller),int(shippingTimeUpperLimitLowestSeller), \
743
    int(shippingTimeLowerLimitPrefSeller), int(shippingTimeUpperLimitPrefSeller), int(shippingTimeLowerLimitOur), int(shippingTimeUpperLimitOur), int(shippingTimeLowerLimitSecondLowestSeller), int(shippingTimeUpperLimitSecondLowestSeller), len(sortedVendorsData), lowestSellerName, lowestSellerCode, secondLowestSellerName, secondLowestSellerCode, prefSellerName, prefSellerCode, lowestSellerBuyTrend, \
744
    ourBuyTrend, prefSellerBuyTrend, secondLowestSellerBuyTrend, ourCode)
745
    return flipkartDetails
746
 
747
def calculateAverageSale(oosStatus):
748
    count,sale = 0,0
749
    for obj in oosStatus:
750
        if not obj.is_oos:
751
            count+=1
752
            sale = sale+obj.num_orders
753
    avgSalePerDay=0 if count==0 else (float(sale)/count)
754
    return round(avgSalePerDay,2)
755
 
756
def calculateTotalSale(oosStatus):
757
    sale = 0
758
    for obj in oosStatus:
759
        if not obj.is_oos:
760
            sale = sale+obj.num_orders
761
    return sale
762
 
763
def getNetAvailability(itemInventory):
764
    totalAvailability, totalReserved = 0,0
765
    availableMap  = itemInventory.availability
766
    reserveMap = itemInventory.reserved
767
    for warehouse,availability in availableMap.iteritems():
768
        if warehouse==16:
769
            continue
770
        totalAvailability = totalAvailability+availability
771
    for warehouse,reserve in reserveMap.iteritems():
772
        if warehouse==16:
773
            continue
774
        totalReserved = totalReserved+reserve
775
    return totalAvailability - totalReserved
776
 
777
def getOosString(oosStatus):
778
    lastNdaySale=""
779
    for obj in oosStatus:
780
        if obj.is_oos:
781
            lastNdaySale += "X-"
782
        else:
783
            lastNdaySale += str(obj.num_orders) + "-"
784
    return lastNdaySale[:-1]
785
 
786
def getLastDaySale(itemId):
787
    return (itemSaleMap.get(itemId))[4]
788
 
789
def getSalesPotential(lowestSellingPrice,ourNlc):
790
    if lowestSellingPrice - ourNlc < 0:
791
        return 'HIGH'
792
    elif (float(lowestSellingPrice - ourNlc))/lowestSellingPrice >=0 and (float(lowestSellingPrice - ourNlc))/lowestSellingPrice <=.02:
793
        return 'MEDIUM'
794
    else:
795
        return 'LOW'  
796
 
11571 kshitij.so 797
def decideCategory(itemInfo):
798
    print "length****",len(itemInfo)
11193 kshitij.so 799
    global itemSaleMap
11560 kshitij.so 800
 
11571 kshitij.so 801
    cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionItems, negativeMargin, cheapButNotPref, prefButNotCheap = [],[],[],[],[],[],[],[]
802
 
11193 kshitij.so 803
    catalog_client = CatalogClient().get_client()
804
 
11571 kshitij.so 805
    for val in itemInfo:
806
        spm = val.sourcePercentage
807
        flipkartDetails = val.flipkartDetails
11581 kshitij.so 808
        if (flipkartDetails is None or flipkartDetails.totalAvailableSeller==0):
11571 kshitij.so 809
            exceptionItems.append(val)
810
            continue
811
 
812
        mpItem = MarketplaceItems.get_by(itemId=val.item_id,source=OrderSource.FLIPKART)
813
        if flipkartDetails.rank==0:
814
            flipkartDetails.ourSp = mpItem.currentSp
815
            ourSp = mpItem.currentSp
816
        else:
817
            ourSp = flipkartDetails.ourSp
11581 kshitij.so 818
        vatRate = catalog_client.getVatPercentageForItem(val.item_id, val.stateId, flipkartDetails.ourSp)
11571 kshitij.so 819
        val.vatRate = vatRate
820
        if (flipkartDetails.ourBuyTrend == 'PrefCheap') or (flipkartDetails.rank==1 and flipkartDetails.totalAvailableSeller==1):
821
            temp=[]
822
            temp.append(flipkartDetails)
823
            temp.append(val)
824
            secondLowestTp=0 if flipkartDetails.totalAvailableSeller==1 else getOtherTp(flipkartDetails,val,spm,False)
825
            prefSellerTp=0 if flipkartDetails.totalAvailableSeller < 2 else getOtherTp(flipkartDetails,val,spm,True)  
826
            flipkartPricing = __FlipkartPricing(flipkartDetails.ourSp,getOurTp(flipkartDetails,val,spm,mpItem),None,getLowestPossibleTp(flipkartDetails,val,spm,mpItem),secondLowestTp,getLowestPossibleSp(flipkartDetails,val,spm,mpItem),prefSellerTp)
827
            temp.append(flipkartPricing)
828
            temp.append(mpItem)
829
            buyBoxItems.append(temp)
830
            continue
831
 
832
        if (flipkartDetails.ourBuyTrend == 'PrefNCheap'):
833
            temp=[]
834
            temp.append(flipkartDetails)
835
            temp.append(val)
836
            secondLowestTp=0 if flipkartDetails.totalAvailableSeller==1 else getOtherTp(flipkartDetails,val,spm,False)
837
            prefSellerTp=0 if flipkartDetails.totalAvailableSeller < 2 else getOtherTp(flipkartDetails,val,spm,True)  
838
            flipkartPricing = __FlipkartPricing(flipkartDetails.ourSp,getOurTp(flipkartDetails,val,spm,mpItem),None,getLowestPossibleTp(flipkartDetails,val,spm,mpItem),secondLowestTp,getLowestPossibleSp(flipkartDetails,val,spm,mpItem),prefSellerTp)
839
            temp.append(flipkartPricing)
840
            temp.append(mpItem)
841
            prefButNotCheap.append(temp)
842
            continue
843
 
844
        if (flipkartDetails.ourBuyTrend == 'NPrefCheap') and (flipkartDetails.rank==1):
845
            temp=[]
846
            temp.append(flipkartDetails)
847
            temp.append(val)
848
            prefSellerTp=0 if flipkartDetails.totalAvailableSeller < 2 else getOtherTp(flipkartDetails,val,spm,True)  
849
            flipkartPricing = __FlipkartPricing(flipkartDetails.ourSp,getOurTp(flipkartDetails,val,spm,mpItem),None,getLowestPossibleTp(flipkartDetails,val,spm,mpItem),None,getLowestPossibleSp(flipkartDetails,val,spm,mpItem),prefSellerTp)
850
            temp.append(flipkartPricing)
851
            temp.append(mpItem)
852
            cheapButNotPref.append(temp)
853
            continue
854
 
855
 
856
        lowestTp = getOtherTp(flipkartDetails,val,spm,False)
857
        ourTp = getOurTp(flipkartDetails,val,spm,mpItem)
858
        lowestPossibleTp = getLowestPossibleTp(flipkartDetails,val,spm,mpItem)
859
        lowestPossibleSp = getLowestPossibleSp(flipkartDetails,val,spm,mpItem)
860
        prefSellerTp = getOtherTp(flipkartDetails,val,spm,True)
861
 
862
        if (ourTp<lowestPossibleTp):
863
            temp=[]
864
            temp.append(flipkartDetails)
865
            temp.append(val)
866
            flipkartPricing = __FlipkartPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,None,None,None)
867
            temp.append(flipkartPricing)
868
            negativeMargin.append(temp)
869
            continue
870
 
871
        if (flipkartDetails.lowestSellerSp > lowestPossibleSp) and val.ourFlipkartInventory!=0:
872
            type(val.ourFlipkartInventory)
873
            temp=[]
874
            temp.append(flipkartDetails)
875
            temp.append(val)
876
            flipkartPricing = __FlipkartPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,None,lowestPossibleSp,prefSellerTp)
877
            temp.append(flipkartPricing)
878
            temp.append(mpItem)
879
            competitive.append(temp)
880
            continue
881
 
882
        if (flipkartDetails.lowestSellerSp) > lowestPossibleSp and val.ourFlipkartInventory==0:
883
            temp=[]
884
            temp.append(flipkartDetails)
885
            temp.append(val)
886
            flipkartPricing = __FlipkartPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,None,lowestPossibleSp,prefSellerTp)
887
            temp.append(flipkartPricing)
888
            temp.append(mpItem)
889
            competitiveNoInventory.append(temp)
890
            continue
891
 
11193 kshitij.so 892
        temp=[]
893
        temp.append(flipkartDetails)
894
        temp.append(val)
895
        flipkartPricing = __FlipkartPricing(ourSp,ourTp,lowestTp,lowestPossibleTp,None,lowestPossibleSp,prefSellerTp)
896
        temp.append(flipkartPricing)
897
        temp.append(mpItem)
11571 kshitij.so 898
        cantCompete.append(temp)
11193 kshitij.so 899
 
11571 kshitij.so 900
    return cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionItems, negativeMargin, cheapButNotPref, prefButNotCheap
11193 kshitij.so 901
 
902
def getOtherTp(flipkartDetails,val,spm,prefferedSeller):
903
    if val.parent_category==10011 or val.parent_category==12001:
904
        commissionPercentage = spm.competitorCommissionAccessory
905
    else:
906
        commissionPercentage = spm.competitorCommissionOther
907
    if flipkartDetails.rank==1 and not prefferedSeller:
908
        otherTp = flipkartDetails.secondLowestSellerSp- flipkartDetails.secondLowestSellerSp*(commissionPercentage/100+spm.emiFee/100)*(1+(spm.serviceTax/100))-(val.courierCost+spm.closingFee)*(1+(spm.serviceTax/100))
909
        return round(otherTp,2)
910
    if prefferedSeller:
911
        otherTp = flipkartDetails.prefSellerSp- flipkartDetails.prefSellerSp*(commissionPercentage/100+spm.emiFee/100)*(1+(spm.serviceTax/100))-(val.courierCost+spm.closingFee)*(1+(spm.serviceTax/100))
912
        return round(otherTp,2)
913
    otherTp = flipkartDetails.lowestSellerSp- flipkartDetails.lowestSellerSp*(commissionPercentage/100+spm.emiFee/100)*(1+(spm.serviceTax/100))-(val.courierCost+spm.closingFee)*(1+(spm.serviceTax/100))
914
    return round(otherTp,2)
915
 
916
def getLowestPossibleTp(flipkartDetails,val,spm,mpItem):
917
    if flipkartDetails.rank==0:
918
        return mpItem.minimumPossibleTp
919
    vat = (flipkartDetails.ourSp/(1+(val.vatRate/100))-(val.nlc/(1+(val.vatRate/100))))*(val.vatRate/100);
920
    inHouseCost = 15+vat+(mpItem.returnProvision/100)*flipkartDetails.ourSp+mpItem.otherCost;
921
    lowest_possible_tp = val.nlc+inHouseCost;
922
    return round(lowest_possible_tp,2)
923
 
924
def getOurTp(flipkartDetails,val,spm,mpItem):
925
    if flipkartDetails.rank==0:
926
        return mpItem.currentTp
927
    ourTp = flipkartDetails.ourSp- flipkartDetails.ourSp*(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(val.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))
928
    return round(ourTp,2)
929
 
930
def getLowestPossibleSp(flipkartDetails,val,spm,mpItem):
931
    if flipkartDetails.rank==0:
932
        return mpItem.minimumPossibleSp
933
    lowestPossibleSp = (val.nlc+(val.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))*(1+(val.vatRate/100))+(15+mpItem.otherCost)*(1+(val.vatRate)/100))/(1-(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))*(1+(val.vatRate)/100)-(mpItem.returnProvision/100)*(1+(val.vatRate)/100));
934
    return round(lowestPossibleSp,2)
935
 
936
def getTargetTp(targetSp,mpItem):
937
    targetTp = targetSp- targetSp*(mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))-(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100))
938
    return round(targetTp,2)
939
 
940
def getTargetSp(targetTp,mpItem,ourSp):
941
    targetSp = float(targetTp+(mpItem.courierCost+mpItem.closingFee)*(1+(mpItem.serviceTax/100)))/(1-((mpItem.commission/100+mpItem.emiFee/100)*(1+(mpItem.serviceTax/100))))
942
    return round(targetSp,2)
943
 
944
def markAutoFavourite():
945
    previouslyAutoFav = []
946
    nowAutoFav = []
947
    marketplaceItems = session.query(MarketplaceItems).filter(MarketplaceItems.source==OrderSource.FLIPKART).all()
948
    fromDate = datetime.now()-timedelta(days = 3, hours=datetime.now().hour, minutes=datetime.now().minute, seconds=datetime.now().second)
949
    toDate = datetime.now()-timedelta(days = 0, hours=datetime.now().hour, minutes=datetime.now().minute, seconds=datetime.now().second)
950
    items = session.query(MarketPlaceHistory.item_id,func.max(MarketPlaceHistory.timestamp)).group_by(MarketPlaceHistory.item_id).filter(MarketPlaceHistory.source==OrderSource.FLIPKART).filter(MarketPlaceHistory.timestamp.between (fromDate,toDate)).filter(MarketPlaceHistory.competitiveCategory==CompetitionCategory.BUY_BOX).all()
951
    toUpdate = [key for key, value in itemSaleMap.items() if value[5] >= 1]
952
    buyBoxLast3days = []
953
    for item in items:
954
        buyBoxLast3days.append(item[0])
955
    for marketplaceItem in marketplaceItems:
956
        reason = ""
957
        toMark = False
958
        if marketplaceItem.itemId in toUpdate:
959
            toMark = True
960
            reason+="Total sale is greater than 1 for last five days (Flipkart)."
961
        if marketplaceItem.itemId in buyBoxLast3days:
962
            toMark = True
963
            reason+="Item is present in buy box in last 3 days"
964
        if not marketplaceItem.autoFavourite:
965
            print "Item is not under auto favourite"
966
        if toMark:
967
            temp=[]
968
            temp.append(marketplaceItem.itemId)
969
            temp.append(reason)
970
            nowAutoFav.append(temp)
971
        if (not toMark) and marketplaceItem.autoFavourite:
972
            previouslyAutoFav.append(marketplaceItem.itemId)
973
        marketplaceItem.autoFavourite = toMark
974
    session.commit()
975
    return previouslyAutoFav, nowAutoFav
976
 
977
def write_report(cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionList, negativeMargin, cheapButNotPref, prefButNotCheap, previousAutoFav, nowAutoFav,timestamp,runType):
978
    wbk = xlwt.Workbook()
979
    sheet = wbk.add_sheet('Can\'t Compete')
980
    xstr = lambda s: s or ""
981
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
982
 
983
    excel_integer_format = '0'
984
    integer_style = xlwt.XFStyle()
985
    integer_style.num_format_str = excel_integer_format
986
 
987
    sheet.write(0, 0, "Item ID", heading_xf)
988
    sheet.write(0, 1, "Category", heading_xf)
989
    sheet.write(0, 2, "Product Group.", heading_xf)
990
    sheet.write(0, 3, "FK Serial Number", heading_xf)
991
    sheet.write(0, 4, "Brand", heading_xf)
992
    sheet.write(0, 5, "Product Name", heading_xf)
993
    sheet.write(0, 6, "Weight", heading_xf)
994
    sheet.write(0, 7, "Courier Cost", heading_xf)
995
    sheet.write(0, 8, "Risky", heading_xf)
996
    sheet.write(0, 9, "Our Rating", heading_xf)
997
    sheet.write(0, 10, "Our Shipping Time", heading_xf)
998
    sheet.write(0, 11, "Our Rank", heading_xf)
999
    sheet.write(0, 12, "Our SP", heading_xf)
1000
    sheet.write(0, 13, "Our TP", heading_xf)
1001
    sheet.write(0, 14, "Lowest Seller", heading_xf)
1002
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1003
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
1004
    sheet.write(0, 17, "Lowest Seller SP", heading_xf)
1005
    sheet.write(0, 18, "Lowest Seller TP", heading_xf)
1006
    sheet.write(0, 19, "Preffered Seller", heading_xf)
1007
    sheet.write(0, 20, "Preffered Seller Rating", heading_xf)
1008
    sheet.write(0, 21, "Preffered Seller Shipping Time", heading_xf)
1009
    sheet.write(0, 22, "Preffer Seller SP", heading_xf)
1010
    sheet.write(0, 23, "Preffered Seller TP", heading_xf)
1011
    sheet.write(0, 24, "Our Flipkart Inventory", heading_xf)
1012
    sheet.write(0, 25, "Our Net Availability",heading_xf)
1013
    sheet.write(0, 26, "Last Five Day Sale", heading_xf)
1014
    sheet.write(0, 27, "Average Sale", heading_xf)
1015
    sheet.write(0, 28, "Our NLC", heading_xf)
1016
    sheet.write(0, 29, "Lowest Possible SP", heading_xf)
1017
    sheet.write(0, 30, "Lowest Possible TP", heading_xf)
1018
    sheet.write(0, 31, "Target SP", heading_xf)
1019
    sheet.write(0, 32, "Target TP", heading_xf)  
1020
    sheet.write(0, 33, "Target NLC", heading_xf)
1021
    sheet.write(0, 34, "Sales Potential", heading_xf)
1022
    sheet.write(0, 35, "Total Seller", heading_xf)
1023
    sheet_iterator = 1
1024
    for item in cantCompete:
1025
        flipkartDetails = item[0]
1026
        flipkartItemInfo = item[1]
1027
        flipkartPricing = item[2]
1028
        mpItem = item[3]
1029
        sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1030
        sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1031
        sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1032
        sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1033
        sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1034
        sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1035
        sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1036
        sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1037
        sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1038
        sheet.write(sheet_iterator,9,flipkartDetails.ourScore)
1039
        ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1040
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1041
        sheet.write(sheet_iterator,10,ourShippingTime)
1042
        sheet.write(sheet_iterator,11,flipkartDetails.rank)
1043
        sheet.write(sheet_iterator,12,flipkartPricing.ourSp)
1044
        sheet.write(sheet_iterator,13,flipkartPricing.ourTp)
1045
        sheet.write(sheet_iterator,14,flipkartDetails.lowestSellerName)
1046
        sheet.write(sheet_iterator,15,flipkartDetails.lowestSellerScore)
1047
        lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
1048
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
1049
        sheet.write(sheet_iterator,16,lowestSellerShippingTime)
1050
        sheet.write(sheet_iterator,17,flipkartDetails.lowestSellerSp)
1051
        sheet.write(sheet_iterator,18,flipkartPricing.lowestTp)
1052
        sheet.write(sheet_iterator,19,flipkartDetails.prefSellerName)
1053
        sheet.write(sheet_iterator,20,flipkartDetails.prefSellerScore)
1054
        prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
1055
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
1056
        sheet.write(sheet_iterator,21,prefferedSellerShippingTime)
1057
        sheet.write(sheet_iterator,22,flipkartDetails.prefSellerSp)
1058
        sheet.write(sheet_iterator,23,flipkartPricing.prefSellerTp)
1059
        sheet.write(sheet_iterator,24,flipkartItemInfo.ourFlipkartInventory)
1060
        if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1061
            sheet.write(sheet_iterator, 25, 'Info not available')
1062
        else:
1063
            sheet.write(sheet_iterator, 25, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1064
        sheet.write(sheet_iterator, 26, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1065
        sheet.write(sheet_iterator, 27, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1066
        sheet.write(sheet_iterator, 28, flipkartItemInfo.nlc)
1067
        sheet.write(sheet_iterator, 29, flipkartPricing.lowestPossibleSp)
1068
        sheet.write(sheet_iterator, 30, flipkartPricing.lowestPossibleTp)
1069
        proposed_sp = flipkartDetails.lowestSellerSp - max(10, flipkartDetails.lowestSellerSp*0.001)
1070
        proposed_tp = getTargetTp(proposed_sp,mpItem)
1071
        target_nlc = proposed_tp - flipkartPricing.lowestPossibleTp + flipkartItemInfo.nlc
1072
        sheet.write(sheet_iterator, 31, proposed_sp)
1073
        sheet.write(sheet_iterator, 32, proposed_tp)
1074
        sheet.write(sheet_iterator, 33, target_nlc)
1075
        sheet.write(sheet_iterator, 34, getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
1076
        sheet.write(sheet_iterator, 35, flipkartDetails.totalAvailableSeller)
1077
        sheet_iterator+=1
1078
 
1079
    sheet = wbk.add_sheet('Pref and Cheap')
1080
 
1081
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1082
 
1083
    excel_integer_format = '0'
1084
    integer_style = xlwt.XFStyle()
1085
    integer_style.num_format_str = excel_integer_format
1086
    xstr = lambda s: s or ""
1087
 
1088
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1089
 
1090
    excel_integer_format = '0'
1091
    integer_style = xlwt.XFStyle()
1092
    integer_style.num_format_str = excel_integer_format
1093
 
1094
    sheet.write(0, 0, "Item ID", heading_xf)
1095
    sheet.write(0, 1, "Category", heading_xf)
1096
    sheet.write(0, 2, "Product Group.", heading_xf)
1097
    sheet.write(0, 3, "FK Serial Number", heading_xf)
1098
    sheet.write(0, 4, "Brand", heading_xf)
1099
    sheet.write(0, 5, "Product Name", heading_xf)
1100
    sheet.write(0, 6, "Weight", heading_xf)
1101
    sheet.write(0, 7, "Courier Cost", heading_xf)
1102
    sheet.write(0, 8, "Risky", heading_xf)
1103
    sheet.write(0, 9, "Our Rating", heading_xf)
1104
    sheet.write(0, 10, "Our Shipping Time", heading_xf)
1105
    sheet.write(0, 11, "Our Rank", heading_xf)
1106
    sheet.write(0, 12, "Our SP", heading_xf)
1107
    sheet.write(0, 13, "Our TP", heading_xf)
1108
    sheet.write(0, 14, "Lowest Seller", heading_xf)
1109
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1110
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
1111
    sheet.write(0, 17, "Lowest Seller SP", heading_xf)
1112
    sheet.write(0, 18, "Lowest Seller TP", heading_xf)
1113
    sheet.write(0, 19, "Second Lowest Seller", heading_xf)
1114
    sheet.write(0, 20, "Second Lowest Seller Rating", heading_xf)
1115
    sheet.write(0, 21, "Second Lowest Seller Shipping Time", heading_xf)
1116
    sheet.write(0, 22, "Second Lowest Seller SP", heading_xf)
1117
    sheet.write(0, 23, "Second Lowest Seller TP", heading_xf)
1118
    sheet.write(0, 24, "Our Flipkart Inventory", heading_xf)
1119
    sheet.write(0, 25, "Our Net Availability",heading_xf)
1120
    sheet.write(0, 26, "Last Five Day Sale", heading_xf)
1121
    sheet.write(0, 27, "Average Sale", heading_xf)
1122
    sheet.write(0, 28, "Our NLC", heading_xf)
1123
    sheet.write(0, 29, "Lowest Possible SP", heading_xf)
1124
    sheet.write(0, 30, "Lowest Possible TP", heading_xf)
1125
    sheet.write(0, 31, "Target SP", heading_xf)
1126
    sheet.write(0, 32, "Target TP", heading_xf)  
1127
    sheet.write(0, 33, "Margin Increased Potential", heading_xf)
1128
    sheet.write(0, 34, "Total Seller", heading_xf)
1129
    sheet_iterator = 1
1130
    for item in buyBoxItems:
1131
        flipkartDetails = item[0]
1132
        flipkartItemInfo = item[1]
1133
        flipkartPricing = item[2]
1134
        mpItem = item[3]
1135
        sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1136
        sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1137
        sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1138
        sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1139
        sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1140
        sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1141
        sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1142
        sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1143
        sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1144
        sheet.write(sheet_iterator,9,flipkartDetails.ourScore)
1145
        ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1146
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1147
        sheet.write(sheet_iterator,10,ourShippingTime)
1148
        sheet.write(sheet_iterator,11,flipkartDetails.rank)
1149
        sheet.write(sheet_iterator,12,flipkartPricing.ourSp)
1150
        sheet.write(sheet_iterator,13,flipkartPricing.ourTp)
1151
        sheet.write(sheet_iterator,14,flipkartDetails.lowestSellerName)
1152
        sheet.write(sheet_iterator,15,flipkartDetails.lowestSellerScore)
1153
        lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
1154
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
1155
        sheet.write(sheet_iterator,16,lowestSellerShippingTime)
1156
        sheet.write(sheet_iterator,17,flipkartDetails.lowestSellerSp)
1157
        sheet.write(sheet_iterator,18,flipkartPricing.lowestTp)
1158
        sheet.write(sheet_iterator,19,flipkartDetails.secondLowestSellerName)
1159
        sheet.write(sheet_iterator,20,flipkartDetails.secondLowestSellerScore)
1160
        secondLowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitSecondLowestSeller) if flipkartDetails.shippingTimeUpperLimitSecondLowestSeller==0\
1161
        else str(flipkartDetails.shippingTimeLowerLimitSecondLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitSecondLowestSeller)
1162
        sheet.write(sheet_iterator,21,secondLowestSellerShippingTime)
1163
        sheet.write(sheet_iterator,22,flipkartDetails.secondLowestSellerSp)
1164
        sheet.write(sheet_iterator,23,flipkartPricing.secondLowestSellerTp)
1165
        sheet.write(sheet_iterator,24,flipkartItemInfo.ourFlipkartInventory)
1166
        if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1167
            sheet.write(sheet_iterator, 25, 'Info not available')
1168
        else:
1169
            sheet.write(sheet_iterator, 25, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1170
        sheet.write(sheet_iterator, 26, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1171
        sheet.write(sheet_iterator, 27, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1172
        sheet.write(sheet_iterator, 28, flipkartItemInfo.nlc)
1173
        sheet.write(sheet_iterator, 29, flipkartPricing.lowestPossibleSp)
1174
        sheet.write(sheet_iterator, 30, flipkartPricing.lowestPossibleTp)
1175
        proposed_sp = max(flipkartDetails.secondLowestSellerSp - max((20, flipkartDetails.secondLowestSellerSp*0.002)), flipkartPricing.lowestPossibleSp)
1176
        proposed_tp = getTargetTp(proposed_sp,mpItem)
1177
        target_nlc = proposed_tp - flipkartPricing.lowestPossibleTp + flipkartItemInfo.nlc
1178
        sheet.write(sheet_iterator, 31, proposed_sp)
1179
        sheet.write(sheet_iterator, 32, proposed_tp)
1180
        sheet.write(sheet_iterator, 33, proposed_tp - flipkartPricing.ourTp)
1181
        sheet.write(sheet_iterator, 34, flipkartDetails.totalAvailableSeller)
1182
        sheet_iterator+=1
1183
 
1184
 
1185
    sheet = wbk.add_sheet('PREF BUT NOT CHEAP')
1186
 
1187
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1188
 
1189
    excel_integer_format = '0'
1190
    integer_style = xlwt.XFStyle()
1191
    integer_style.num_format_str = excel_integer_format
1192
    xstr = lambda s: s or ""
1193
 
1194
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1195
 
1196
    excel_integer_format = '0'
1197
    integer_style = xlwt.XFStyle()
1198
    integer_style.num_format_str = excel_integer_format
1199
 
1200
    sheet.write(0, 0, "Item ID", heading_xf)
1201
    sheet.write(0, 1, "Category", heading_xf)
1202
    sheet.write(0, 2, "Product Group.", heading_xf)
1203
    sheet.write(0, 3, "FK Serial Number", heading_xf)
1204
    sheet.write(0, 4, "Brand", heading_xf)
1205
    sheet.write(0, 5, "Product Name", heading_xf)
1206
    sheet.write(0, 6, "Weight", heading_xf)
1207
    sheet.write(0, 7, "Courier Cost", heading_xf)
1208
    sheet.write(0, 8, "Risky", heading_xf)
1209
    sheet.write(0, 9, "Our Rank", heading_xf)
1210
    sheet.write(0, 10, "Lowest Seller", heading_xf)
1211
    sheet.write(0, 11, "Our Rating", heading_xf)
1212
    sheet.write(0, 12, "Our Shipping Time", heading_xf)
1213
    sheet.write(0, 13, "Our SP", heading_xf)
1214
    sheet.write(0, 14, "Our TP", heading_xf)
1215
    sheet.write(0, 15, "Preffered Seller", heading_xf)
1216
    sheet.write(0, 16, "Preffered Seller Rating", heading_xf)
1217
    sheet.write(0, 17, "Preffered Seller Shipping Time", heading_xf)
1218
    sheet.write(0, 18, "Preffered Seller SP", heading_xf)
1219
    sheet.write(0, 19, "Preffered Seller TP", heading_xf)
1220
    sheet.write(0, 20, "Our Flipkart Inventory", heading_xf)
1221
    sheet.write(0, 21, "Our Net Availability",heading_xf)
1222
    sheet.write(0, 22, "Last Five Day Sale", heading_xf)
1223
    sheet.write(0, 23, "Average Sale", heading_xf)
1224
    sheet.write(0, 24, "Our NLC", heading_xf)
1225
    sheet.write(0, 25, "Lowest Possible SP", heading_xf)
1226
    sheet.write(0, 26, "Lowest Possible TP", heading_xf)
1227
    sheet.write(0, 27, "Total Seller", heading_xf)
1228
    sheet_iterator = 1
1229
    for item in prefButNotCheap:
1230
        flipkartDetails = item[0]
1231
        flipkartItemInfo = item[1]
1232
        flipkartPricing = item[2]
1233
        mpItem = item[3]
1234
        sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1235
        sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1236
        sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1237
        sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1238
        sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1239
        sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1240
        sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1241
        sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1242
        sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1243
        sheet.write(sheet_iterator,9,flipkartDetails.rank)
1244
        sheet.write(sheet_iterator,10,flipkartDetails.lowestSellerName)
1245
        sheet.write(sheet_iterator,11,flipkartDetails.ourScore)
1246
        ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1247
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1248
        sheet.write(sheet_iterator,12,ourShippingTime)
1249
        sheet.write(sheet_iterator,13,flipkartPricing.ourSp)
1250
        sheet.write(sheet_iterator,14,flipkartPricing.ourTp)
1251
        sheet.write(sheet_iterator,15,flipkartDetails.prefSellerName)
1252
        sheet.write(sheet_iterator,16,flipkartDetails.prefSellerScore)
1253
        prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
1254
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
1255
        sheet.write(sheet_iterator,17,prefferedSellerShippingTime)
1256
        sheet.write(sheet_iterator,18,flipkartDetails.prefSellerSp)
1257
        sheet.write(sheet_iterator,19,flipkartPricing.prefSellerTp)
1258
        sheet.write(sheet_iterator,20,flipkartItemInfo.ourFlipkartInventory)
1259
        if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1260
            sheet.write(sheet_iterator, 21, 'Info not available')
1261
        else:
1262
            sheet.write(sheet_iterator, 21, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1263
        sheet.write(sheet_iterator, 22, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1264
        sheet.write(sheet_iterator, 23, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1265
        sheet.write(sheet_iterator, 24, flipkartItemInfo.nlc)
1266
        sheet.write(sheet_iterator, 25, flipkartPricing.lowestPossibleSp)
1267
        sheet.write(sheet_iterator, 26, flipkartPricing.lowestPossibleTp)
1268
        #proposed_sp = max(flipkartDetails.secondLowestSellerSp - max((20, flipkartDetails.secondLowestSellerSp*0.002)), flipkartPricing.lowestPossibleSp)
1269
        #proposed_tp = getTargetTp(proposed_sp,mpItem)
1270
        #target_nlc = proposed_tp - flipkartPricing.lowestPossibleTp + flipkartItemInfo.nlc
1271
        sheet.write(sheet_iterator, 27, flipkartDetails.totalAvailableSeller)
1272
        sheet_iterator+=1
1273
 
1274
    sheet = wbk.add_sheet('Cheap But Not Pref')
1275
 
1276
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1277
 
1278
    excel_integer_format = '0'
1279
    integer_style = xlwt.XFStyle()
1280
    integer_style.num_format_str = excel_integer_format
1281
    xstr = lambda s: s or ""
1282
 
1283
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1284
 
1285
    excel_integer_format = '0'
1286
    integer_style = xlwt.XFStyle()
1287
    integer_style.num_format_str = excel_integer_format
1288
 
1289
    sheet.write(0, 0, "Item ID", heading_xf)
1290
    sheet.write(0, 1, "Category", heading_xf)
1291
    sheet.write(0, 2, "Product Group.", heading_xf)
1292
    sheet.write(0, 3, "FK Serial Number", heading_xf)
1293
    sheet.write(0, 4, "Brand", heading_xf)
1294
    sheet.write(0, 5, "Product Name", heading_xf)
1295
    sheet.write(0, 6, "Weight", heading_xf)
1296
    sheet.write(0, 7, "Courier Cost", heading_xf)
1297
    sheet.write(0, 8, "Risky", heading_xf)
1298
    sheet.write(0, 9, "Our Rank", heading_xf)
1299
    sheet.write(0, 10, "Lowest Seller", heading_xf)
1300
    sheet.write(0, 11, "Our Rating", heading_xf)
1301
    sheet.write(0, 12, "Our Shipping Time", heading_xf)
1302
    sheet.write(0, 13, "Our SP", heading_xf)
1303
    sheet.write(0, 14, "Our TP", heading_xf)
1304
    sheet.write(0, 15, "Preffered Seller", heading_xf)
1305
    sheet.write(0, 16, "Preffered Seller Rating", heading_xf)
1306
    sheet.write(0, 17, "Preffered Seller Shipping Time", heading_xf)
1307
    sheet.write(0, 18, "Preffered Seller SP", heading_xf)
1308
    sheet.write(0, 19, "Preffered Seller TP", heading_xf)
1309
    sheet.write(0, 20, "Our Flipkart Inventory", heading_xf)
1310
    sheet.write(0, 21, "Our Net Availability",heading_xf)
1311
    sheet.write(0, 22, "Last Five Day Sale", heading_xf)
1312
    sheet.write(0, 23, "Average Sale", heading_xf)
1313
    sheet.write(0, 24, "Our NLC", heading_xf)
1314
    sheet.write(0, 25, "Lowest Possible SP", heading_xf)
1315
    sheet.write(0, 26, "Lowest Possible TP", heading_xf)
1316
    sheet.write(0, 27, "Total Seller", heading_xf)
1317
    sheet_iterator = 1
1318
    for item in cheapButNotPref:
1319
        flipkartDetails = item[0]
1320
        flipkartItemInfo = item[1]
1321
        flipkartPricing = item[2]
1322
        mpItem = item[3]
1323
        sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1324
        sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1325
        sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1326
        sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1327
        sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1328
        sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1329
        sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1330
        sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1331
        sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1332
        sheet.write(sheet_iterator,9,flipkartDetails.rank)
1333
        sheet.write(sheet_iterator,10,flipkartDetails.lowestSellerName)
1334
        sheet.write(sheet_iterator,11,flipkartDetails.ourScore)
1335
        ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1336
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1337
        sheet.write(sheet_iterator,12,ourShippingTime)
1338
        sheet.write(sheet_iterator,13,flipkartPricing.ourSp)
1339
        sheet.write(sheet_iterator,14,flipkartPricing.ourTp)
1340
        sheet.write(sheet_iterator,15,flipkartDetails.prefSellerName)
1341
        sheet.write(sheet_iterator,16,flipkartDetails.prefSellerScore)
1342
        prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
1343
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
1344
        sheet.write(sheet_iterator,17,prefferedSellerShippingTime)
1345
        sheet.write(sheet_iterator,18,flipkartDetails.prefSellerSp)
1346
        sheet.write(sheet_iterator,19,flipkartPricing.prefSellerTp)
1347
        sheet.write(sheet_iterator,20,flipkartItemInfo.ourFlipkartInventory)
1348
        if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1349
            sheet.write(sheet_iterator, 21, 'Info not available')
1350
        else:
1351
            sheet.write(sheet_iterator, 21, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1352
        sheet.write(sheet_iterator, 22, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1353
        sheet.write(sheet_iterator, 23, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1354
        sheet.write(sheet_iterator, 24, flipkartItemInfo.nlc)
1355
        sheet.write(sheet_iterator, 25, flipkartPricing.lowestPossibleSp)
1356
        sheet.write(sheet_iterator, 26, flipkartPricing.lowestPossibleTp)
1357
        #proposed_sp = max(flipkartDetails.secondLowestSellerSp - max((20, flipkartDetails.secondLowestSellerSp*0.002)), flipkartPricing.lowestPossibleSp)
1358
        #proposed_tp = getTargetTp(proposed_sp,mpItem)
1359
        #target_nlc = proposed_tp - flipkartPricing.lowestPossibleTp + flipkartItemInfo.nlc
1360
        sheet.write(sheet_iterator, 27, flipkartDetails.totalAvailableSeller)
1361
        sheet_iterator+=1
1362
 
1363
    sheet = wbk.add_sheet('Can Compete-With Inventory')
1364
    xstr = lambda s: s or ""
1365
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1366
 
1367
    excel_integer_format = '0'
1368
    integer_style = xlwt.XFStyle()
1369
    integer_style.num_format_str = excel_integer_format
1370
 
1371
    sheet.write(0, 0, "Item ID", heading_xf)
1372
    sheet.write(0, 1, "Category", heading_xf)
1373
    sheet.write(0, 2, "Product Group.", heading_xf)
1374
    sheet.write(0, 3, "FK Serial Number", heading_xf)
1375
    sheet.write(0, 4, "Brand", heading_xf)
1376
    sheet.write(0, 5, "Product Name", heading_xf)
1377
    sheet.write(0, 6, "Weight", heading_xf)
1378
    sheet.write(0, 7, "Courier Cost", heading_xf)
1379
    sheet.write(0, 8, "Risky", heading_xf)
1380
    sheet.write(0, 9, "Our Rating", heading_xf)
1381
    sheet.write(0, 10, "Our Shipping Time", heading_xf)
1382
    sheet.write(0, 11, "Our Rank", heading_xf)
1383
    sheet.write(0, 12, "Our SP", heading_xf)
1384
    sheet.write(0, 13, "Our TP", heading_xf)
1385
    sheet.write(0, 14, "Lowest Seller", heading_xf)
1386
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1387
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
1388
    sheet.write(0, 17, "Lowest Seller SP", heading_xf)
1389
    sheet.write(0, 18, "Lowest Seller TP", heading_xf)
1390
    sheet.write(0, 19, "Preffered Seller", heading_xf)
1391
    sheet.write(0, 20, "Preffered Seller Rating", heading_xf)
1392
    sheet.write(0, 21, "Preffered Seller Shipping Time", heading_xf)
1393
    sheet.write(0, 22, "Preffer Seller SP", heading_xf)
1394
    sheet.write(0, 23, "Preffered Seller TP", heading_xf)
1395
    sheet.write(0, 24, "Our Flipkart Inventory", heading_xf)
1396
    sheet.write(0, 25, "Our Net Availability",heading_xf)
1397
    sheet.write(0, 26, "Last Five Day Sale", heading_xf)
1398
    sheet.write(0, 27, "Average Sale", heading_xf)
1399
    sheet.write(0, 28, "Our NLC", heading_xf)
1400
    sheet.write(0, 29, "Lowest Possible SP", heading_xf)
1401
    sheet.write(0, 30, "Lowest Possible TP", heading_xf)
1402
    sheet.write(0, 31, "Target SP", heading_xf)
1403
    sheet.write(0, 32, "Target TP", heading_xf)  
1404
    sheet.write(0, 33, "Target NLC", heading_xf)
1405
    sheet.write(0, 34, "Sales Potential", heading_xf)
1406
    sheet.write(0, 35, "Total Seller", heading_xf)
1407
    sheet_iterator = 1
1408
    for item in competitive:
1409
        flipkartDetails = item[0]
1410
        flipkartItemInfo = item[1]
1411
        flipkartPricing = item[2]
1412
        mpItem = item[3]
1413
        sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1414
        sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1415
        sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1416
        sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1417
        sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1418
        sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1419
        sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1420
        sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1421
        sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1422
        sheet.write(sheet_iterator,9,flipkartDetails.ourScore)
1423
        ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1424
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1425
        sheet.write(sheet_iterator,10,ourShippingTime)
1426
        sheet.write(sheet_iterator,11,flipkartDetails.rank)
1427
        sheet.write(sheet_iterator,12,flipkartPricing.ourSp)
1428
        sheet.write(sheet_iterator,13,flipkartPricing.ourTp)
1429
        sheet.write(sheet_iterator,14,flipkartDetails.lowestSellerName)
1430
        sheet.write(sheet_iterator,15,flipkartDetails.lowestSellerScore)
1431
        lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
1432
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
1433
        sheet.write(sheet_iterator,16,lowestSellerShippingTime)
1434
        sheet.write(sheet_iterator,17,flipkartDetails.lowestSellerSp)
1435
        sheet.write(sheet_iterator,18,flipkartPricing.lowestTp)
1436
        sheet.write(sheet_iterator,19,flipkartDetails.prefSellerName)
1437
        sheet.write(sheet_iterator,20,flipkartDetails.prefSellerScore)
1438
        prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
1439
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
1440
        sheet.write(sheet_iterator,21,prefferedSellerShippingTime)
1441
        sheet.write(sheet_iterator,22,flipkartDetails.prefSellerSp)
1442
        sheet.write(sheet_iterator,23,flipkartPricing.prefSellerTp)
1443
        sheet.write(sheet_iterator,24,flipkartItemInfo.ourFlipkartInventory)
1444
        if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1445
            sheet.write(sheet_iterator, 25, 'Info not available')
1446
        else:
1447
            sheet.write(sheet_iterator, 25, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1448
        sheet.write(sheet_iterator, 26, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1449
        sheet.write(sheet_iterator, 27, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1450
        sheet.write(sheet_iterator, 28, flipkartItemInfo.nlc)
1451
        sheet.write(sheet_iterator, 29, flipkartPricing.lowestPossibleSp)
1452
        sheet.write(sheet_iterator, 30, flipkartPricing.lowestPossibleTp)
1453
        proposed_sp = max(flipkartDetails.lowestSellerSp - max((10, flipkartDetails.lowestSellerSp*0.001)), flipkartPricing.lowestPossibleSp)
1454
        proposed_tp = getTargetTp(proposed_sp,mpItem)
1455
        target_nlc = proposed_tp - flipkartPricing.lowestPossibleTp + flipkartItemInfo.nlc
1456
        sheet.write(sheet_iterator, 31, proposed_sp)
1457
        sheet.write(sheet_iterator, 32, proposed_tp)
1458
        sheet.write(sheet_iterator, 33, target_nlc)
1459
        sheet.write(sheet_iterator, 34, getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
1460
        sheet.write(sheet_iterator, 35, flipkartDetails.totalAvailableSeller)
1461
        sheet_iterator+=1
1462
 
1463
    sheet = wbk.add_sheet('Negative Margin')
1464
    xstr = lambda s: s or ""
1465
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1466
 
1467
    excel_integer_format = '0'
1468
    integer_style = xlwt.XFStyle()
1469
    integer_style.num_format_str = excel_integer_format
1470
 
1471
    sheet.write(0, 0, "Item ID", heading_xf)
1472
    sheet.write(0, 1, "Category", heading_xf)
1473
    sheet.write(0, 2, "Product Group.", heading_xf)
1474
    sheet.write(0, 3, "FK Serial Number", heading_xf)
1475
    sheet.write(0, 4, "Brand", heading_xf)
1476
    sheet.write(0, 5, "Product Name", heading_xf)
1477
    sheet.write(0, 6, "Weight", heading_xf)
1478
    sheet.write(0, 7, "Courier Cost", heading_xf)
1479
    sheet.write(0, 8, "Risky", heading_xf)
1480
    sheet.write(0, 9, "Our Rating", heading_xf)
1481
    sheet.write(0, 10, "Our Shipping Time", heading_xf)
1482
    sheet.write(0, 11, "Our Rank", heading_xf)
1483
    sheet.write(0, 12, "Our SP", heading_xf)
1484
    sheet.write(0, 13, "Our TP", heading_xf)
1485
    sheet.write(0, 14, "Lowest Seller", heading_xf)
1486
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1487
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
1488
    sheet.write(0, 17, "Lowest Seller SP", heading_xf)
1489
    sheet.write(0, 18, "Lowest Seller TP", heading_xf)
1490
    sheet.write(0, 19, "Preffered Seller", heading_xf)
1491
    sheet.write(0, 20, "Preffered Seller Rating", heading_xf)
1492
    sheet.write(0, 21, "Preffered Seller Shipping Time", heading_xf)
1493
    sheet.write(0, 22, "Preffer Seller SP", heading_xf)
1494
    sheet.write(0, 23, "Preffered Seller TP", heading_xf)
1495
    sheet.write(0, 24, "Our Flipkart Inventory", heading_xf)
1496
    sheet.write(0, 25, "Our Net Availability",heading_xf)
1497
    sheet.write(0, 26, "Last Five Day Sale", heading_xf)
1498
    sheet.write(0, 27, "Average Sale", heading_xf)
1499
    sheet.write(0, 28, "Our NLC", heading_xf)
1500
    sheet.write(0, 29, "Lowest Possible SP", heading_xf)
1501
    sheet.write(0, 30, "Lowest Possible TP", heading_xf)
1502
    sheet.write(0, 31, "Margin", heading_xf)
1503
    sheet.write(0, 32, "Total Seller", heading_xf)
1504
    sheet_iterator = 1
1505
    for item in negativeMargin:
1506
        flipkartDetails = item[0]
1507
        flipkartItemInfo = item[1]
1508
        flipkartPricing = item[2]
1509
        sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1510
        sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1511
        sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1512
        sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1513
        sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1514
        sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1515
        sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1516
        sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1517
        sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1518
        sheet.write(sheet_iterator,9,flipkartDetails.ourScore)
1519
        ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1520
        else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1521
        sheet.write(sheet_iterator,10,ourShippingTime)
1522
        sheet.write(sheet_iterator,11,flipkartDetails.rank)
1523
        sheet.write(sheet_iterator,12,flipkartPricing.ourSp)
1524
        sheet.write(sheet_iterator,13,flipkartPricing.ourTp)
1525
        sheet.write(sheet_iterator,14,flipkartDetails.lowestSellerName)
1526
        sheet.write(sheet_iterator,15,flipkartDetails.lowestSellerScore)
1527
        lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
1528
        else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
1529
        sheet.write(sheet_iterator,16,lowestSellerShippingTime)
1530
        sheet.write(sheet_iterator,17,flipkartDetails.lowestSellerSp)
1531
        sheet.write(sheet_iterator,18,flipkartPricing.lowestTp)
1532
        sheet.write(sheet_iterator,19,flipkartDetails.prefSellerName)
1533
        sheet.write(sheet_iterator,20,flipkartDetails.prefSellerScore)
1534
        prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
1535
        else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
1536
        sheet.write(sheet_iterator,21,prefferedSellerShippingTime)
1537
        sheet.write(sheet_iterator,22,flipkartDetails.prefSellerSp)
1538
        sheet.write(sheet_iterator,23,flipkartPricing.prefSellerTp)
1539
        sheet.write(sheet_iterator,24,flipkartItemInfo.ourFlipkartInventory)
1540
        if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1541
            sheet.write(sheet_iterator, 25, 'Info not available')
1542
        else:
1543
            sheet.write(sheet_iterator, 25, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1544
        sheet.write(sheet_iterator, 26, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1545
        sheet.write(sheet_iterator, 27, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1546
        sheet.write(sheet_iterator, 28, flipkartItemInfo.nlc)
1547
        sheet.write(sheet_iterator, 29, flipkartPricing.lowestPossibleSp)
1548
        sheet.write(sheet_iterator, 30, flipkartPricing.lowestPossibleTp)
1549
        sheet.write(sheet_iterator, 31, round((flipkartPricing.ourTp - flipkartPricing.lowestPossibleTp),2))
1550
        sheet.write(sheet_iterator, 32, flipkartDetails.totalAvailableSeller)
1551
        sheet_iterator+=1
1552
 
1553
    if (runType=='FULL'):    
1554
        sheet = wbk.add_sheet('Auto Favorites')
1555
 
1556
        heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1557
 
1558
        excel_integer_format = '0'
1559
        integer_style = xlwt.XFStyle()
1560
        integer_style.num_format_str = excel_integer_format
1561
        xstr = lambda s: s or ""
1562
 
1563
        sheet.write(0, 0, "Item ID", heading_xf)
1564
        sheet.write(0, 1, "Brand", heading_xf)
1565
        sheet.write(0, 2, "Product Name", heading_xf)
1566
        sheet.write(0, 3, "Auto Favourite", heading_xf)
1567
        sheet.write(0, 4, "Reason", heading_xf)
1568
 
1569
        sheet_iterator=1
1570
        for autoFav in nowAutoFav:
1571
            itemId = autoFav[0]
1572
            reason = autoFav[1]
1573
            it = Item.query.filter_by(id=itemId).one()
1574
            sheet.write(sheet_iterator, 0, itemId)
1575
            sheet.write(sheet_iterator, 1, it.brand)
1576
            sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))
1577
            sheet.write(sheet_iterator, 3, "True")
1578
            sheet.write(sheet_iterator, 4, reason)
1579
            sheet_iterator+=1
1580
        for prevFav in previousAutoFav:
1581
            it = Item.query.filter_by(id=prevFav).one()
1582
            sheet.write(sheet_iterator, 0, prevFav)
1583
            sheet.write(sheet_iterator, 1, it.brand)
1584
            sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))
1585
            sheet.write(sheet_iterator, 3, "False")
1586
            sheet_iterator+=1
1587
 
1588
    sheet = wbk.add_sheet('Exception Item List')
1589
 
1590
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1591
 
1592
    excel_integer_format = '0'
1593
    integer_style = xlwt.XFStyle()
1594
    integer_style.num_format_str = excel_integer_format
1595
    xstr = lambda s: s or ""
1596
 
1597
    sheet.write(0, 0, "Item ID", heading_xf)
1598
    sheet.write(0, 1, "FK Serial number", heading_xf)
1599
    sheet.write(0, 2, "Brand", heading_xf)
1600
    sheet.write(0, 3, "Product Name", heading_xf)
1601
    sheet.write(0, 4, "Reason", heading_xf)
1602
    sheet_iterator=1
1603
    for item in exceptionList:
1604
        sheet.write(sheet_iterator, 0, item.item_id)
1605
        sheet.write(sheet_iterator, 1, item.fkSerialNumber)
1606
        sheet.write(sheet_iterator, 2, item.brand)
1607
        sheet.write(sheet_iterator, 3, xstr(item.brand)+" "+xstr(item.model_name)+" "+xstr(item.model_number)+" "+xstr(item.color))
1608
        try:
1609
            if item.totalAvailableSeller is None:
1610
                pass
1611
        except:
1612
            sheet.write(sheet_iterator, 4, "Unable to fetch info from Flipkart")
1613
            sheet_iterator+=1
1614
            continue
1615
        sheet.write(sheet_iterator, 4, "No Seller Available")
1616
        sheet_iterator+=1
1617
 
1618
    sheet = wbk.add_sheet('Can Compete-No Inv')
1619
    xstr = lambda s: s or ""
1620
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1621
 
1622
    excel_integer_format = '0'
1623
    integer_style = xlwt.XFStyle()
1624
    integer_style.num_format_str = excel_integer_format
1625
 
1626
    sheet.write(0, 0, "Item ID", heading_xf)
1627
    sheet.write(0, 1, "Category", heading_xf)
1628
    sheet.write(0, 2, "Product Group.", heading_xf)
1629
    sheet.write(0, 3, "FK Serial Number", heading_xf)
1630
    sheet.write(0, 4, "Brand", heading_xf)
1631
    sheet.write(0, 5, "Product Name", heading_xf)
1632
    sheet.write(0, 6, "Weight", heading_xf)
1633
    sheet.write(0, 7, "Courier Cost", heading_xf)
1634
    sheet.write(0, 8, "Risky", heading_xf)
1635
    sheet.write(0, 9, "Our Rating", heading_xf)
1636
    sheet.write(0, 10, "Our Shipping Time", heading_xf)
1637
    sheet.write(0, 11, "Our Rank", heading_xf)
1638
    sheet.write(0, 12, "Our SP", heading_xf)
1639
    sheet.write(0, 13, "Our TP", heading_xf)
1640
    sheet.write(0, 14, "Lowest Seller", heading_xf)
1641
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1642
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
1643
    sheet.write(0, 17, "Lowest Seller SP", heading_xf)
1644
    sheet.write(0, 18, "Lowest Seller TP", heading_xf)
1645
    sheet.write(0, 19, "Preffered Seller", heading_xf)
1646
    sheet.write(0, 20, "Preffered Seller Rating", heading_xf)
1647
    sheet.write(0, 21, "Preffered Seller Shipping Time", heading_xf)
1648
    sheet.write(0, 22, "Preffer Seller SP", heading_xf)
1649
    sheet.write(0, 23, "Preffered Seller TP", heading_xf)
1650
    sheet.write(0, 24, "Our Flipkart Inventory", heading_xf)
1651
    sheet.write(0, 25, "Our Net Availability",heading_xf)
1652
    sheet.write(0, 26, "Last Five Day Sale", heading_xf)
1653
    sheet.write(0, 27, "Average Sale", heading_xf)
1654
    sheet.write(0, 28, "Our NLC", heading_xf)
1655
    sheet.write(0, 29, "Lowest Possible SP", heading_xf)
1656
    sheet.write(0, 30, "Lowest Possible TP", heading_xf)
1657
    sheet.write(0, 31, "Target SP", heading_xf)
1658
    sheet.write(0, 32, "Target TP", heading_xf)  
1659
    sheet.write(0, 33, "Sales Potential", heading_xf)
1660
    sheet.write(0, 34, "Total Seller", heading_xf)
1661
    sheet_iterator = 1
1662
    for item in competitiveNoInventory:
1663
        flipkartDetails = item[0]
1664
        flipkartItemInfo = item[1]
1665
        flipkartPricing = item[2]
1666
        mpItem = item[3]
1667
        if ((not inventoryMap.has_key(flipkartItemInfo.item_id)) or getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id))<=0):
1668
            sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1669
            sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1670
            sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1671
            sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1672
            sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1673
            sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1674
            sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1675
            sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1676
            sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1677
            sheet.write(sheet_iterator,9,flipkartDetails.ourScore)
1678
            ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1679
            else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1680
            sheet.write(sheet_iterator,10,ourShippingTime)
1681
            sheet.write(sheet_iterator,11,flipkartDetails.rank)
1682
            sheet.write(sheet_iterator,12,flipkartPricing.ourSp)
1683
            sheet.write(sheet_iterator,13,flipkartPricing.ourTp)
1684
            sheet.write(sheet_iterator,14,flipkartDetails.lowestSellerName)
1685
            sheet.write(sheet_iterator,15,flipkartDetails.lowestSellerScore)
1686
            lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
1687
            else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
1688
            sheet.write(sheet_iterator,16,lowestSellerShippingTime)
1689
            sheet.write(sheet_iterator,17,flipkartDetails.lowestSellerSp)
1690
            sheet.write(sheet_iterator,18,flipkartPricing.lowestTp)
1691
            sheet.write(sheet_iterator,19,flipkartDetails.prefSellerName)
1692
            sheet.write(sheet_iterator,20,flipkartDetails.prefSellerScore)
1693
            prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
1694
            else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
1695
            sheet.write(sheet_iterator,21,prefferedSellerShippingTime)
1696
            sheet.write(sheet_iterator,22,flipkartDetails.prefSellerSp)
1697
            sheet.write(sheet_iterator,23,flipkartPricing.prefSellerTp)
1698
            sheet.write(sheet_iterator,24,flipkartItemInfo.ourFlipkartInventory)
1699
            if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1700
                sheet.write(sheet_iterator, 25, 'Info not available')
1701
            else:
1702
                sheet.write(sheet_iterator, 25, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1703
            sheet.write(sheet_iterator, 26, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1704
            sheet.write(sheet_iterator, 27, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1705
            sheet.write(sheet_iterator, 28, flipkartItemInfo.nlc)
1706
            sheet.write(sheet_iterator, 29, flipkartPricing.lowestPossibleSp)
1707
            sheet.write(sheet_iterator, 30, flipkartPricing.lowestPossibleTp)
1708
            proposed_sp = max(flipkartDetails.lowestSellerSp - max((10, flipkartDetails.lowestSellerSp*0.001)), flipkartPricing.lowestPossibleSp)
1709
            proposed_tp = getTargetTp(proposed_sp,mpItem)
1710
            sheet.write(sheet_iterator, 31, proposed_sp)
1711
            sheet.write(sheet_iterator, 32, proposed_tp)
1712
            sheet.write(sheet_iterator, 33, getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
1713
            sheet.write(sheet_iterator, 34, flipkartDetails.totalAvailableSeller)
1714
            sheet_iterator+=1
1715
 
1716
    sheet = wbk.add_sheet('Can Compete-No Inv On FK')
1717
    xstr = lambda s: s or ""
1718
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1719
 
1720
    excel_integer_format = '0'
1721
    integer_style = xlwt.XFStyle()
1722
    integer_style.num_format_str = excel_integer_format
1723
 
1724
    sheet.write(0, 0, "Item ID", heading_xf)
1725
    sheet.write(0, 1, "Category", heading_xf)
1726
    sheet.write(0, 2, "Product Group.", heading_xf)
1727
    sheet.write(0, 3, "FK Serial Number", heading_xf)
1728
    sheet.write(0, 4, "Brand", heading_xf)
1729
    sheet.write(0, 5, "Product Name", heading_xf)
1730
    sheet.write(0, 6, "Weight", heading_xf)
1731
    sheet.write(0, 7, "Courier Cost", heading_xf)
1732
    sheet.write(0, 8, "Risky", heading_xf)
1733
    sheet.write(0, 9, "Our Rating", heading_xf)
1734
    sheet.write(0, 10, "Our Shipping Time", heading_xf)
1735
    sheet.write(0, 11, "Our Rank", heading_xf)
1736
    sheet.write(0, 12, "Our SP", heading_xf)
1737
    sheet.write(0, 13, "Our TP", heading_xf)
1738
    sheet.write(0, 14, "Lowest Seller", heading_xf)
1739
    sheet.write(0, 15, "Lowest Seller Rating", heading_xf)
1740
    sheet.write(0, 16, "Lowest Seller Shipping Time", heading_xf)
1741
    sheet.write(0, 17, "Lowest Seller SP", heading_xf)
1742
    sheet.write(0, 18, "Lowest Seller TP", heading_xf)
1743
    sheet.write(0, 19, "Preffered Seller", heading_xf)
1744
    sheet.write(0, 20, "Preffered Seller Rating", heading_xf)
1745
    sheet.write(0, 21, "Preffered Seller Shipping Time", heading_xf)
1746
    sheet.write(0, 22, "Preffer Seller SP", heading_xf)
1747
    sheet.write(0, 23, "Preffered Seller TP", heading_xf)
1748
    sheet.write(0, 24, "Our Flipkart Inventory", heading_xf)
1749
    sheet.write(0, 25, "Our Net Availability",heading_xf)
1750
    sheet.write(0, 26, "Last Five Day Sale", heading_xf)
1751
    sheet.write(0, 27, "Average Sale", heading_xf)
1752
    sheet.write(0, 28, "Our NLC", heading_xf)
1753
    sheet.write(0, 29, "Lowest Possible SP", heading_xf)
1754
    sheet.write(0, 30, "Lowest Possible TP", heading_xf)
1755
    sheet.write(0, 31, "Target SP", heading_xf)
1756
    sheet.write(0, 32, "Target TP", heading_xf)  
1757
    sheet.write(0, 33, "Sales Potential", heading_xf)
1758
    sheet.write(0, 34, "Total Seller", heading_xf)
1759
    sheet_iterator = 1
1760
    for item in competitiveNoInventory:
1761
        flipkartDetails = item[0]
1762
        flipkartItemInfo = item[1]
1763
        flipkartPricing = item[2]
1764
        mpItem = item[3]
1765
        if (inventoryMap.has_key(flipkartItemInfo.item_id) and getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id))>0):
1766
            sheet.write(sheet_iterator,0,flipkartItemInfo.item_id)
1767
            sheet.write(sheet_iterator,1,flipkartItemInfo.parent_category_name)
1768
            sheet.write(sheet_iterator,2,flipkartItemInfo.product_group)
1769
            sheet.write(sheet_iterator,3,flipkartItemInfo.fkSerialNumber)
1770
            sheet.write(sheet_iterator,4,flipkartItemInfo.brand)
1771
            sheet.write(sheet_iterator,5,xstr(flipkartItemInfo.brand)+" "+xstr(flipkartItemInfo.model_name)+" "+xstr(flipkartItemInfo.model_number)+" "+xstr(flipkartItemInfo.color))
1772
            sheet.write(sheet_iterator,6,flipkartItemInfo.weight)
1773
            sheet.write(sheet_iterator,7,flipkartItemInfo.courierCost)
1774
            sheet.write(sheet_iterator,8,flipkartItemInfo.risky)
1775
            sheet.write(sheet_iterator,9,flipkartDetails.ourScore)
1776
            ourShippingTime= str(flipkartDetails.shippingTimeLowerLimitOur) if flipkartDetails.shippingTimeUpperLimitOur==0\
1777
            else str(flipkartDetails.shippingTimeLowerLimitOur)+'-'+str(flipkartDetails.shippingTimeUpperLimitOur)
1778
            sheet.write(sheet_iterator,10,ourShippingTime)
1779
            sheet.write(sheet_iterator,11,flipkartDetails.rank)
1780
            sheet.write(sheet_iterator,12,flipkartPricing.ourSp)
1781
            sheet.write(sheet_iterator,13,flipkartPricing.ourTp)
1782
            sheet.write(sheet_iterator,14,flipkartDetails.lowestSellerName)
1783
            sheet.write(sheet_iterator,15,flipkartDetails.lowestSellerScore)
1784
            lowestSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitLowestSeller) if flipkartDetails.shippingTimeUpperLimitLowestSeller==0\
1785
            else str(flipkartDetails.shippingTimeLowerLimitLowestSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitLowestSeller)
1786
            sheet.write(sheet_iterator,16,lowestSellerShippingTime)
1787
            sheet.write(sheet_iterator,17,flipkartDetails.lowestSellerSp)
1788
            sheet.write(sheet_iterator,18,flipkartPricing.lowestTp)
1789
            sheet.write(sheet_iterator,19,flipkartDetails.prefSellerName)
1790
            sheet.write(sheet_iterator,20,flipkartDetails.prefSellerScore)
1791
            prefferedSellerShippingTime= str(flipkartDetails.shippingTimeLowerLimitPrefSeller) if flipkartDetails.shippingTimeUpperLimitPrefSeller==0\
1792
            else str(flipkartDetails.shippingTimeLowerLimitPrefSeller)+'-'+str(flipkartDetails.shippingTimeUpperLimitPrefSeller)
1793
            sheet.write(sheet_iterator,21,prefferedSellerShippingTime)
1794
            sheet.write(sheet_iterator,22,flipkartDetails.prefSellerSp)
1795
            sheet.write(sheet_iterator,23,flipkartPricing.prefSellerTp)
1796
            sheet.write(sheet_iterator,24,flipkartItemInfo.ourFlipkartInventory)
1797
            if (not inventoryMap.has_key(flipkartItemInfo.item_id)):
1798
                sheet.write(sheet_iterator, 25, 'Info not available')
1799
            else:
1800
                sheet.write(sheet_iterator, 25, getNetAvailability(inventoryMap.get(flipkartItemInfo.item_id)))
1801
            sheet.write(sheet_iterator, 26, getOosString((itemSaleMap.get(flipkartItemInfo.item_id))[1]))
1802
            sheet.write(sheet_iterator, 27, (itemSaleMap.get(flipkartItemInfo.item_id))[3])
1803
            sheet.write(sheet_iterator, 28, flipkartItemInfo.nlc)
1804
            sheet.write(sheet_iterator, 29, flipkartPricing.lowestPossibleSp)
1805
            sheet.write(sheet_iterator, 30, flipkartPricing.lowestPossibleTp)
1806
            proposed_sp = max(flipkartDetails.lowestSellerSp - max((10, flipkartDetails.lowestSellerSp*0.001)), flipkartPricing.lowestPossibleSp)
1807
            proposed_tp = getTargetTp(proposed_sp,mpItem)
1808
            sheet.write(sheet_iterator, 31, proposed_sp)
1809
            sheet.write(sheet_iterator, 32, proposed_tp)
1810
            sheet.write(sheet_iterator, 33, getSalesPotential(flipkartDetails.lowestSellerSp,flipkartItemInfo.nlc))
1811
            sheet.write(sheet_iterator, 34, flipkartDetails.totalAvailableSeller)
1812
            sheet_iterator+=1
1813
 
1814
    autoPricingItems = session.query(MarketPlaceHistory,Item).join((Item,MarketPlaceHistory.item_id==Item.id)).filter(MarketPlaceHistory.timestamp==timestamp).filter(MarketPlaceHistory.source==OrderSource.FLIPKART).filter(MarketPlaceHistory.decision.in_([1,2,3,4])).all()
1815
    sheet = wbk.add_sheet('Auto Inc and Dec')
1816
 
1817
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
1818
 
1819
    excel_integer_format = '0'
1820
    integer_style = xlwt.XFStyle()
1821
    integer_style.num_format_str = excel_integer_format
1822
    xstr = lambda s: s or ""
1823
 
1824
    sheet.write(0, 0, "Item ID", heading_xf)
1825
    sheet.write(0, 1, "Brand", heading_xf)
1826
    sheet.write(0, 2, "Product Name", heading_xf)
1827
    sheet.write(0, 3, "Decision", heading_xf)
1828
    sheet.write(0, 4, "Reason", heading_xf)
1829
    sheet.write(0, 5, "Old Selling Price", heading_xf)
1830
    sheet.write(0, 6, "Selling Price Updated",heading_xf)
1831
 
1832
    sheet_iterator=1
1833
    for autoPricingItem in autoPricingItems:
1834
        mpHistory = autoPricingItem[0]
1835
        item = autoPricingItem[1]
1836
        it = Item.query.filter_by(id=item.id).one()
1837
        sheet.write(sheet_iterator, 0, item.id)
1838
        sheet.write(sheet_iterator, 1, it.brand)
1839
        sheet.write(sheet_iterator, 2, xstr(it.brand)+" "+xstr(it.model_name)+" "+xstr(it.model_number)+" "+xstr(it.color))
1840
        sheet.write(sheet_iterator, 3, Decision._VALUES_TO_NAMES.get(mpHistory.decision))
1841
        sheet.write(sheet_iterator, 4, mpHistory.reason)
1842
        if Decision._VALUES_TO_NAMES.get(mpHistory.decision) == "AUTO_DECREMENT_SUCCESS":
1843
            sheet.write(sheet_iterator, 5, mpHistory.ourSellingPrice)
1844
            sheet.write(sheet_iterator, 6, math.ceil(mpHistory.proposedSellingPrice))
1845
        if Decision._VALUES_TO_NAMES.get(mpHistory.decision) == "AUTO_INCREMENT_SUCCESS":
1846
            sheet.write(sheet_iterator, 5, mpHistory.ourSellingPrice)
1847
            sheet.write(sheet_iterator, 6, math.ceil(mpHistory.ourSellingPrice+max(10,.01*mpHistory.ourSellingPrice)))
1848
        sheet_iterator+=1
1849
 
1850
    filename = "/tmp/flipkart-report-"+runType+" " + str(timestamp) + ".xls"
1851
    wbk.save(filename)
1852
    try:
11560 kshitij.so 1853
        EmailAttachmentSender.mail("build@shop2020.in", "cafe@nes", ["kshitij.sood@saholic.com"], " Flipkart Auto Pricing "+runType+" " + str(timestamp), "", [get_attachment_part(filename)], [""], [])
1854
        #EmailAttachmentSender.mail("build@shop2020.in", "cafe@nes", ["chandan.kumar@saholic.com","manoj.kumar@saholic.com","yukti.jain@saholic.com","ankush.dhingra@saholic.com","manoj.pal@saholic.com"], " Flipkart Scraping "+runType+" " + str(timestamp), "", [get_attachment_part(filename)], ["rajneesh.arora@saholic.com","anikendra.das@saholic.com","vikram.raghav@saholic.com","kshitij.sood@saholic.com","chaitnaya.vats@saholic.com","khushal.bhatia@saholic.com"], [])
11193 kshitij.so 1855
    except Exception as e:
1856
        print e
1857
        print "Unable to send report.Trying with local SMTP"
1858
        smtpServer = smtplib.SMTP('localhost')
1859
        smtpServer.set_debuglevel(1)
1860
        sender = 'support@shop2020.in'
11560 kshitij.so 1861
        recipients = ["kshitij.sood@saholic.com"]
11193 kshitij.so 1862
        msg = MIMEMultipart()
11228 kshitij.so 1863
        msg['Subject'] = "Flipkart Scraping" + ' '+runType+' - ' + str(datetime.now())
11193 kshitij.so 1864
        msg['From'] = sender
11560 kshitij.so 1865
        #recipients = ['rajneesh.arora@saholic.com','anikendra.das@saholic.com','vikram.raghav@saholic.com','kshitij.sood@saholic.com','khushal.bhatia@saholic.com','chaitnaya.vats@saholic.com','chandan.kumar@saholic.com','manoj.kumar@saholic.com','yukti.jain@saholic.com','ankush.dhingra@saholic.com','manoj.pal@saholic.com']
11193 kshitij.so 1866
        msg['To'] = ",".join(recipients)
1867
        fileMsg = email.mime.base.MIMEBase('application','vnd.ms-excel')
1868
        fileMsg.set_payload(file(filename).read())
1869
        email.encoders.encode_base64(fileMsg)
1870
        fileMsg.add_header('Content-Disposition','attachment;filename=flipkart.xls')
1871
        msg.attach(fileMsg)
1872
        try:
1873
            smtpServer.sendmail(sender, recipients, msg.as_string())
1874
            print "Successfully sent email"
1875
        except:
1876
            print "Error: unable to send email."
11560 kshitij.so 1877
 
11571 kshitij.so 1878
def populateScrapingResults(val):
1879
    global testcount
1880
    testcount+=1
1881
    print "*****testcount",testcount
1882
    print val
1883
    try:
1884
        flipkartDetails = fetchDetails(val.fkSerialNumber,scraper)
1885
        val.flipkartDetails = flipkartDetails 
1886
    except Exception as e:
1887
        print "Unable to fetch details of",val.fkSerialNumber
1888
        print e
1889
        val.flipkartDetails = None
1890
        return
1891
 
1892
    try:
1893
        request_url = "https://api.flipkart.net/sellers/skus/%s/listings"%(str(val.skuAtFlipkart))
1894
        r = requests.get(request_url, auth=('m2z93iskuj81qiid', '0c7ab6a5-98c0-4cdc-8be3-72c591e0add4'))
1895
        print "Inventory info",r.json()
1896
        stock_count = int((r.json()['attributeValues'])['stock_count'])
1897
    except:
1898
        stock_count = 0
1899
 
1900
    val.ourFlipkartInventory = stock_count
11560 kshitij.so 1901
 
1902
def threadsToSpawn(runType,itemInfo,scraper,itemPopulated):
1903
    if runType == RunType.FAVOURITE:
1904
        startOffset = itemPopulated
1905
        endOffset = startOffset
1906
        while(len(threads)< 3 and endOffset<len(itemInfo)):
1907
            endOffset = startOffset + 20
1908
            if (endOffset >= len(itemInfo)):
1909
                endOffset = len(itemInfo)
11571 kshitij.so 1910
            t = threading.Thread(target=partial(populateScrapingResults, itemInfo[startOffset:endOffset],scraper))
11560 kshitij.so 1911
            #t = threading.Thread(target=partial(test, startOffset, endOffset))
1912
            threads.append(t)
1913
            startOffset = startOffset + 20
1914
        [t.start() for t in threads]
1915
        [t.join() for t in threads] 
1916
        threads = []
1917
        return endOffset
1918
    else:
11561 kshitij.so 1919
        count = 0
11571 kshitij.so 1920
        pool = ThreadPool(5)
11581 kshitij.so 1921
        startOffset = 0
11560 kshitij.so 1922
        endOffset = startOffset
11571 kshitij.so 1923
        while(count<5 and endOffset<len(itemInfo)):
1924
            print "inside while",testcount
11581 kshitij.so 1925
            endOffset = startOffset + 20
11560 kshitij.so 1926
            if (endOffset >= len(itemInfo)):
1927
                endOffset = len(itemInfo)
11581 kshitij.so 1928
            print "pool offset start end count"+str(startOffset)+" "+str(endOffset)+" "+str(count)
1929
            time.sleep(10)
11571 kshitij.so 1930
            pool.map(populateScrapingResults,itemInfo[startOffset:endOffset])
11561 kshitij.so 1931
            #t = Process(target=decideCategory,args=(itemInfo[startOffset:endOffset], scraper))
11560 kshitij.so 1932
            #t = threading.Thread(target=partial(decideCategory, itemInfo[startOffset:endOffset], scraper))
1933
            #t = threading.Thread(target=partial(test, startOffset, endOffset))
11561 kshitij.so 1934
            #threads.append(t)
11581 kshitij.so 1935
            startOffset = startOffset + 20
11561 kshitij.so 1936
            count+=1
1937
        #[t.start() for t in threads]
1938
        #[t.join() for t in threads] 
1939
        #threads = []
11581 kshitij.so 1940
        print "terminating while"
11563 kshitij.so 1941
        pool.close()
1942
        pool.join()
11581 kshitij.so 1943
        print "joining threads"
1944
        time.sleep(5)
1945
        print "returning offset******"
11571 kshitij.so 1946
        time.sleep(5) 
11560 kshitij.so 1947
        return endOffset
1948
 
1949
 
11193 kshitij.so 1950
def main():
1951
    parser = optparse.OptionParser()
1952
    parser.add_option("-t", "--type", dest="runType",
1953
                   default="FULL", type="string",
1954
                   help="Run type FULL or FAVOURITE")
1955
    (options, args) = parser.parse_args()
1956
    if options.runType not in ('FULL','FAVOURITE'):
1957
        print "Run type argument illegal."
1958
        sys.exit(1)
1959
    timestamp = datetime.now()
11581 kshitij.so 1960
    previousProcessingTimestamp = session.query(func.max(MarketPlaceHistory.timestamp)).filter(MarketPlaceHistory.source==OrderSource.FLIPKART).one()
11193 kshitij.so 1961
    itemInfo= populateStuff(options.runType,timestamp)
11560 kshitij.so 1962
    itemsPopulated = 0
1963
    while (itemsPopulated<len(itemInfo)):
1964
        itemsPopulated = threadsToSpawn(options.runType,itemInfo,scraper,itemsPopulated)
11581 kshitij.so 1965
        cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionItems, negativeMargin, cheapButNotPref, prefButNotCheap = decideCategory(itemInfo[0:itemsPopulated])
1966
        itemInfo[0:itemsPopulated] = []
1967
        commitExceptionList(exceptionItems,timestamp)
1968
        commitCantCompete(cantCompete,timestamp)
1969
        commitBuyBox(buyBoxItems,timestamp)
1970
        commitCompetitive(competitive,timestamp)
1971
        commitCompetitiveNoInventory(competitiveNoInventory,timestamp)
1972
        commitNegativeMargin(negativeMargin,timestamp)
1973
        commitCheapButNotPref(cheapButNotPref,timestamp)
1974
        commitPrefButNotCheap(prefButNotCheap, timestamp)
1975
        cantCompete[:], buyBoxItems[:], competitive[:], competitiveNoInventory[:], exceptionItems[:], negativeMargin[:], cheapButNotPref[:], prefButNotCheap[:] =[],[],[],[],[],[],[],[]
1976
 
1977
        #getLastUpdatedOn(mailSender.start, mailSender.stop)
11193 kshitij.so 1978
    #successfulAutoDecrease = fetchItemsForAutoDecrease(timestamp)
1979
    #successfulAutoIncrease = fetchItemsForAutoIncrease(timestamp)
11581 kshitij.so 1980
#    if options.runType=='FULL':
1981
#        previousAutoFav, nowAutoFav = markAutoFavourite()
1982
#    if options.runType=='FULL':
1983
#        write_report(cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionItems, negativeMargin, cheapButNotPref, prefButNotCheap, previousAutoFav, nowAutoFav,timestamp, options.runType)
1984
#    else:
1985
#        write_report(cantCompete, buyBoxItems, competitive, competitiveNoInventory, exceptionItems, negativeMargin, cheapButNotPref, prefButNotCheap, None, None, timestamp, options.runType)
11193 kshitij.so 1986
 
1987
if __name__ == '__main__':
1988
    main()