Subversion Repositories SmartDukaan

Rev

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