Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
9334 kshitij.so 1
#!/usr/bin/python
2
# coding: ascii
3
from elixir import *
4
from shop2020.model.v1.catalog.impl import DataService
5
from shop2020.model.v1.catalog.impl.DataService import Item, Category,\
6
    SnapdealItem
9359 kshitij.so 7
from shop2020.thriftpy.model.v1.inventory import ttypes
8
from shop2020.thriftpy.model.v1.inventory.ttypes import WarehouseType, InventoryType
9334 kshitij.so 9
import csv
10
import datetime
11
import optparse
12
import xlrd
13
from shop2020.clients.InventoryClient import InventoryClient
14
import xlwt
15
import urllib2
16
import time
17
import simplejson as json
9365 kshitij.so 18
from shop2020.utils import EmailAttachmentSender
19
from shop2020.utils.EmailAttachmentSender import get_attachment_part
9334 kshitij.so 20
 
21
 
9365 kshitij.so 22
 
9334 kshitij.so 23
if __name__ == '__main__' and __package__ is None:
24
    import sys
25
    import os
26
    sys.path.insert(0, os.getcwd())
27
 
28
 
29
class _SnapdealItemInfo:
30
 
9360 kshitij.so 31
    def __init__(self, item_id, product_group, category_name, our_nlc, brand, model_name, model_number, color, weight, parent_category, risky):
9334 kshitij.so 32
        self.item_id = item_id
33
        self.product_group = product_group
34
        self.category_name = category_name
35
        self.our_nlc = our_nlc
36
        self.brand = brand
37
        self.model_name = model_name
38
        self.model_number = model_number
39
        self.color = color
40
        self.weight = weight
41
        self.parent_category = parent_category
9359 kshitij.so 42
        self.risky = risky
9334 kshitij.so 43
 
44
 
45
class SnapdealDetails:
9359 kshitij.so 46
    def __init__(self, supc, ourSp, offerPrice, ourInventory, otherInventory, rank, lowestSellerName,lowestSp,secondLowestSellerName,secondLowestSellerSp,secondLowestSellerInventory,lowestOfferPrice,secondLowestSellerOfferPrice,ourOfferPrice):
9334 kshitij.so 47
        self.supc = supc
48
        self.ourSp = ourSp
49
        self.offerPrice = offerPrice
50
        self.ourInventory = ourInventory
51
        self.otherInventory = otherInventory
52
        self.rank = rank
53
        self.lowestSellerName = lowestSellerName
54
        self.lowestSp = lowestSp
55
        self.secondLowestSellerName = secondLowestSellerName
56
        self.secondLowestSellerSp = secondLowestSellerSp
57
        self.secondLowestSellerInventory = secondLowestSellerInventory
9359 kshitij.so 58
        self.lowestOfferPrice = lowestOfferPrice
59
        self.secondLowestSellerOfferPrice = secondLowestSellerOfferPrice
60
        self.ourOfferPrice = ourOfferPrice
9334 kshitij.so 61
 
62
 
63
def fetchDetails(supc_code):
64
    url="http://www.snapdeal.com/json/gvbps?supc=%s&catId=91"%(supc_code)
65
    print url
66
    time.sleep(2)
67
    req = urllib2.Request(url)
68
    response = urllib2.urlopen(req)
69
    print response
70
    json_input = response.read()
71
    vendorInfo = json.loads(json_input)
72
    rank ,otherInventory ,ourInventory, offerPrice, ourSp,iterator = 0, 0, 0, 0, 0, 0
9337 kshitij.so 73
    secondLowestSellerName=''
74
    secondLowestSellerSp=0
75
    secondLowestSellerInventory=0
9359 kshitij.so 76
    lowestOfferPrice = 0
77
    secondLowestSellerOfferPrice = 0
78
    ourOfferPrice = 0
9334 kshitij.so 79
    for vendor in vendorInfo:
80
        if iterator == 0:
81
            lowestSellerName = vendor['vendorDisplayName']
9362 kshitij.so 82
            try:
83
                lowestSp = vendor['sellingPriceBefIntCashBack']
84
            except:
85
                lowestSp = vendor['sellingPrice']
9359 kshitij.so 86
            lowestOfferPrice = vendor['sellingPrice']
9334 kshitij.so 87
 
88
        if iterator ==1:
89
            secondLowestSellerName = vendor['vendorDisplayName']
9362 kshitij.so 90
            try:
91
                secondLowestSellerSp = vendor['sellingPriceBefIntCashBack']
92
            except:
93
                secondLowestSellerSp = vendor['sellingPrice'] 
9359 kshitij.so 94
            secondLowestSellerOfferPrice = vendor['sellingPrice'] 
9334 kshitij.so 95
            secondLowestSellerInventory = vendor['buyableInventory']
96
 
97
        if vendor['vendorDisplayName'] == 'MobilesnMore':
98
            ourInventory = vendor['buyableInventory']
9362 kshitij.so 99
            try:
100
                ourSp = vendor['sellingPriceBefIntCashBack']
101
            except:
102
                ourSp = vendor['sellingPrice']
9359 kshitij.so 103
            ourOfferPrice = vendor['sellingPrice']
9334 kshitij.so 104
            rank = iterator +1
105
        else:
106
            if rank==0:
107
                otherInventory = otherInventory +vendor['buyableInventory']
108
 
109
        iterator+=1
9359 kshitij.so 110
    snapdealDetails = SnapdealDetails(supc_code,ourSp,offerPrice,ourInventory,otherInventory,rank,lowestSellerName,lowestSp,secondLowestSellerName,secondLowestSellerSp,secondLowestSellerInventory,lowestOfferPrice,secondLowestSellerOfferPrice,ourOfferPrice)
9334 kshitij.so 111
    return snapdealDetails
112
 
113
 
114
def read_data(filename):
115
    all_lines = []
116
    all_supc=[]
117
    iclient = InventoryClient().get_client()
9337 kshitij.so 118
    DataService.initialize('catalog','192.168.166.135')
9334 kshitij.so 119
    workbook = xlrd.open_workbook(filename)
120
    sheet = workbook.sheet_by_index(0)
121
    num_rows = sheet.nrows
122
    for rownum in range(1, num_rows):
123
        print sheet.row_values(rownum)
124
        item_id = int(sheet.row_values(rownum)[0])
125
        supc = sheet.row_values(rownum)[1]
126
        item = Item.query.filter_by(id=item_id).one()
127
        category = Category.query.filter_by(id=item.category).one()
128
        snapdeal_item = SnapdealItem.query.filter_by(item_id=item_id).one()
129
        warehouse = iclient.getWarehouse(snapdeal_item.warehouseId)
130
        item_pricing = iclient.getItemPricing(item_id, warehouse.vendor.id)
9359 kshitij.so 131
        #TO BE USED LATER
132
        #inventory_snapshot = iclient.getInventorySnapshot(0)
133
        #warehouses_ours = iclient.getWarehouses(WarehouseType._NAMES_TO_VALUES.get("OURS"), InventoryType._NAMES_TO_VALUES.get("GOOD"), 0,0,0)
134
        #warehouses_third_party = iclient.getWarehouses(WarehouseType._NAMES_TO_VALUES.get("THIRD_PARTY "), InventoryType._NAMES_TO_VALUES.get("GOOD"), 0,0,0)
135
        one_line = _SnapdealItemInfo(item.id, item.product_group, category.label, item_pricing.nlc, item.brand, item.model_name, item.model_number, item.color, item.weight, category.parent_category_id, item.risky)
9334 kshitij.so 136
        all_supc.append(supc)
137
        all_lines.append(one_line)
138
    write_report("/tmp/snapdeal_running.xls", all_lines, all_supc)
139
 
140
 
141
 
142
def write_report(filename, all_lines, all_supc):
143
 
144
    buyBoxItems = []
145
 
146
    wbk = xlwt.Workbook()
147
    sheet = wbk.add_sheet('main')
148
 
149
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
150
 
151
    excel_integer_format = '0'
152
    integer_style = xlwt.XFStyle()
153
    integer_style.num_format_str = excel_integer_format
154
 
155
    sheet.write(0, 0, "Item ID", heading_xf)
156
    sheet.write(0, 1, "Category", heading_xf)
157
    sheet.write(0, 2, "Product Group.", heading_xf)
158
    sheet.write(0, 3, "SUPC", heading_xf)
159
    sheet.write(0, 4, "Brand", heading_xf)
160
    sheet.write(0, 5, "Model Name", heading_xf)
161
    sheet.write(0, 6, "Model Number", heading_xf)
162
    sheet.write(0, 7, "Color", heading_xf)
9359 kshitij.so 163
    sheet.write(0, 8, "Weight", heading_xf)
164
    sheet.write(0, 9, "Risky", heading_xf)
165
    sheet.write(0, 10, "Our SP", heading_xf)
166
    sheet.write(0, 11, "Our TP", heading_xf)
167
    sheet.write(0, 12, "Offer Price", heading_xf)
168
    sheet.write(0, 13, "Our Rank", heading_xf)
169
    sheet.write(0, 14, "Lowest Seller", heading_xf)
170
    sheet.write(0, 15, "Lowest SP", heading_xf)
171
    sheet.write(0, 16, "Lowest TP", heading_xf)
172
    sheet.write(0, 17, "Lowest Offer Price", heading_xf)
173
    sheet.write(0, 18, "Inventory of Top Vendors", heading_xf)
174
    sheet.write(0, 19, "Our Inventory", heading_xf)
175
    sheet.write(0, 20, "Our NLC", heading_xf)
176
    sheet.write(0, 21, "Lowest Possible TP", heading_xf)
177
    sheet.write(0, 22, "Can Compete", heading_xf)
178
    sheet.write(0, 23, "Proposed TP", heading_xf)
179
    sheet.write(0, 24, "Proposed SP", heading_xf)    
9334 kshitij.so 180
 
9337 kshitij.so 181
    i, sheet_iterator=1,1
9334 kshitij.so 182
    for one_line in all_lines:
183
        supc = all_supc[i-1]
184
        supc_data = fetchDetails(supc)
185
 
186
        courierCost = 45
187
 
188
        if one_line.weight:
9364 kshitij.so 189
            slab = int(((one_line.weight+.1) - .001)/.5)
9334 kshitij.so 190
        for x in range (0,slab):
191
            courierCost = courierCost + 35
192
 
193
        courierCost = courierCost * 1.1236
194
 
195
        if supc_data.rank==1:
196
            temp = []
197
            temp.append(supc_data)
198
            temp.append(one_line)
199
            buyBoxItems.append(temp)
200
            i+=1
201
            continue
202
 
9337 kshitij.so 203
        sheet.write(sheet_iterator, 0, one_line.item_id)
204
        sheet.write(sheet_iterator, 1, one_line.category_name)
205
        sheet.write(sheet_iterator, 2, one_line.product_group)
206
        sheet.write(sheet_iterator, 3, supc)
207
        sheet.write(sheet_iterator, 4, one_line.brand)
208
        sheet.write(sheet_iterator, 5, one_line.model_name)
209
        sheet.write(sheet_iterator, 6, one_line.model_number)
210
        sheet.write(sheet_iterator, 7, one_line.color)
9359 kshitij.so 211
        sheet.write(sheet_iterator, 8, one_line.weight)
212
        sheet.write(sheet_iterator, 9, one_line.risky)
213
        sheet.write(sheet_iterator, 10, supc_data.ourSp)
214
        sheet.write(sheet_iterator, 11, round(supc_data.ourSp*0.9597-courierCost))
215
        sheet.write(sheet_iterator, 12, supc_data.offerPrice)
216
        sheet.write(sheet_iterator, 13, supc_data.rank)
217
        sheet.write(sheet_iterator, 14, supc_data.lowestSellerName)
218
        sheet.write(sheet_iterator, 15, supc_data.lowestSp)
9334 kshitij.so 219
        if one_line.parent_category ==10011:
220
            lowestTp = (supc_data.lowestSp*(1-.0803))-courierCost
221
        else:
222
            lowestTp = supc_data.lowestSp*0.9497-courierCost
9359 kshitij.so 223
        sheet.write(sheet_iterator, 16, round(lowestTp))
224
        sheet.write(sheet_iterator, 17, )
225
        sheet.write(sheet_iterator, 18, supc_data.otherInventory)
226
        sheet.write(sheet_iterator, 19, supc_data.ourInventory)
227
        sheet.write(sheet_iterator, 20, one_line.our_nlc)
9334 kshitij.so 228
        if supc_data.rank==1:
229
            i+=1
9337 kshitij.so 230
            sheet_iterator+=1
9334 kshitij.so 231
            continue
232
        lowest_possible_tp = one_line.our_nlc/0.988+15+6
9359 kshitij.so 233
        sheet.write(sheet_iterator, 21, round(lowest_possible_tp))
9334 kshitij.so 234
        proposed_tp = 0
235
        if lowestTp > lowest_possible_tp:
9359 kshitij.so 236
            sheet.write(sheet_iterator, 22, "Yes")
9337 kshitij.so 237
            proposed_tp  = max(lowestTp - max((10, lowestTp*0.001)), lowest_possible_tp)
9359 kshitij.so 238
            sheet.write(sheet_iterator, 23, round(proposed_tp))
239
            sheet.write(sheet_iterator, 24, round((proposed_tp+courierCost)/0.9597)) 
9334 kshitij.so 240
        else:
9359 kshitij.so 241
            sheet.write(sheet_iterator, 22, "No")
9334 kshitij.so 242
        i= i+1
9337 kshitij.so 243
        sheet_iterator+=1
244
 
9334 kshitij.so 245
    createSheetForBuyBoxItems(buyBoxItems,wbk)
246
    wbk.save(filename)
9365 kshitij.so 247
 
248
    EmailAttachmentSender.mail("build@shop2020.in", "cafe@nes", ["kshitij.sood@shop2020.in"], "Insurance Details for date " , "Please find attached insurance details for today.", [get_attachment_part(filename)], ["kshitij.sood@shop2020.in"], [])
9334 kshitij.so 249
 
9365 kshitij.so 250
 
251
 
9334 kshitij.so 252
def createSheetForBuyBoxItems(buyBoxItems,wbk):
253
    sheet = wbk.add_sheet('BuyBoxItems')
254
 
255
    heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
256
 
257
    excel_integer_format = '0'
258
    integer_style = xlwt.XFStyle()
259
    integer_style.num_format_str = excel_integer_format
260
 
261
    sheet.write(0, 0, "Item ID", heading_xf)
262
    sheet.write(0, 1, "Category", heading_xf)
263
    sheet.write(0, 2, "Product Group.", heading_xf)
264
    sheet.write(0, 3, "SUPC", heading_xf)
265
    sheet.write(0, 4, "Brand", heading_xf)
266
    sheet.write(0, 5, "Model Name", heading_xf)
267
    sheet.write(0, 6, "Model Number", heading_xf)
268
    sheet.write(0, 7, "Color", heading_xf)
9359 kshitij.so 269
    sheet.write(0, 8, "Weight", heading_xf)
270
    sheet.write(0, 9, "Risky", heading_xf)
271
    sheet.write(0, 10, "Our SP", heading_xf)
272
    sheet.write(0, 11, "Our TP", heading_xf)
273
    sheet.write(0, 12, "Offer Price", heading_xf)
274
    sheet.write(0, 13, "Our Rank", heading_xf)
275
    sheet.write(0, 14, "Lowest Seller", heading_xf)
276
    sheet.write(0, 15, "Lowest Offer Price", heading_xf)
277
    sheet.write(0, 16, "Second Lowest Seller", heading_xf)
278
    sheet.write(0, 17, "Second Lowest Price", heading_xf)
279
    sheet.write(0, 18, "Our Inventory", heading_xf)
280
    sheet.write(0, 19, "Second Lowest Seller Inventory", heading_xf)
281
    sheet.write(0, 20, "Our NLC", heading_xf)
282
    sheet.write(0, 21, "Second Lowest Seller TP", heading_xf)
9334 kshitij.so 283
 
284
    i=1
285
 
286
    for data in buyBoxItems:
287
        supc_data =data[0]
288
        one_line = data[1]
289
 
290
        courierCost = 45
291
 
292
        if one_line.weight:
9364 kshitij.so 293
            slab = int(((one_line.weight+.1) - .001)/.5)
9334 kshitij.so 294
        for x in range (0,slab):
295
            courierCost = courierCost + 35
296
 
297
        courierCost = courierCost * 1.1236
298
 
299
        sheet.write(i, 0, one_line.item_id)
300
        sheet.write(i, 1, one_line.category_name)
301
        sheet.write(i, 2, one_line.product_group)
302
        sheet.write(i, 3, supc_data.supc)
303
        sheet.write(i, 4, one_line.brand)
304
        sheet.write(i, 5, one_line.model_name)
305
        sheet.write(i, 6, one_line.model_number)
306
        sheet.write(i, 7, one_line.color)
9359 kshitij.so 307
        sheet.write(i, 8, one_line.weight)
308
        sheet.write(i, 9, one_line.risky)
309
        sheet.write(i, 10, supc_data.ourSp)
310
        sheet.write(i, 11, round(supc_data.ourSp*0.9597-courierCost))
311
        sheet.write(i, 12, supc_data.offerPrice)
312
        sheet.write(i, 13, supc_data.rank)
313
        sheet.write(i, 14, supc_data.lowestSellerName)
314
        sheet.write(i, 15, )
315
        sheet.write(i, 16, supc_data.secondLowestSellerName)
316
        sheet.write(i, 17, supc_data.secondLowestSellerSp)
317
        sheet.write(i, 18, supc_data.ourInventory)
318
        sheet.write(i, 19, supc_data.secondLowestSellerInventory)
319
        sheet.write(i, 20, one_line.our_nlc)
9334 kshitij.so 320
        if one_line.parent_category ==10011:
321
            lowestTp = (supc_data.secondLowestSellerSp*(1-.0803))-courierCost
322
        else:
323
            lowestTp = supc_data.secondLowestSellerSp*0.9497-courierCost
9359 kshitij.so 324
        sheet.write(i, 21, round(lowestTp))
9334 kshitij.so 325
        i+=1
326
 
327
 
328
def main():
329
    parser = optparse.OptionParser()
330
    parser.add_option("-f", "--file", dest="filename",
331
                   default="ItemList.xls", type="string",
332
                   help="Read the item list from FILE",
333
                   metavar="FILE")
334
    (options, args) = parser.parse_args()
335
    if len(args) != 0:
336
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
337
    filename = options.filename
338
    read_data(filename)
339
 
340
if __name__ == '__main__':
341
    main()
342