| 94 |
ashish |
1 |
'''
|
|
|
2 |
Created on 23-Mar-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from elixir import *
|
|
|
7 |
from shop2020.model.v1.catalog.impl import DataService
|
| 122 |
ashish |
8 |
from shop2020.model.v1.catalog.impl.DataService import Item, \
|
| 103 |
ashish |
9 |
Warehouse, ItemInventoryHistory, CurrentInventorySnapshot, ItemInfo,\
|
| 635 |
rajveer |
10 |
ItemChangeLog, BestSellers, Category
|
| 122 |
ashish |
11 |
from shop2020.thriftpy.model.v1.catalog.ttypes import \
|
|
|
12 |
InventoryServiceException, status
|
| 602 |
chandransh |
13 |
from shop2020.model.v1.catalog.impl.Convertors import to_t_item
|
| 94 |
ashish |
14 |
import datetime
|
| 602 |
chandransh |
15 |
from shop2020.utils.Utils import log_entry, to_py_date
|
| 621 |
chandransh |
16 |
from sqlalchemy import desc, asc
|
| 94 |
ashish |
17 |
|
|
|
18 |
|
|
|
19 |
def initialize():
|
|
|
20 |
DataService.initialize()
|
|
|
21 |
|
|
|
22 |
|
| 103 |
ashish |
23 |
def get_all_items(is_active):
|
|
|
24 |
if is_active:
|
| 766 |
rajveer |
25 |
items = Item.query.filter_by(status= status.ACTIVE).all()
|
| 103 |
ashish |
26 |
else:
|
| 766 |
rajveer |
27 |
items = Item.query.all()
|
|
|
28 |
return items
|
|
|
29 |
|
| 103 |
ashish |
30 |
def get_all_items_by_status(status):
|
|
|
31 |
if not status:
|
|
|
32 |
#return all items
|
| 766 |
rajveer |
33 |
items = get_all_items(False)
|
| 103 |
ashish |
34 |
else:
|
| 766 |
rajveer |
35 |
items = Item.query.filter_by(status=status).all()
|
|
|
36 |
return items
|
| 103 |
ashish |
37 |
|
| 635 |
rajveer |
38 |
def get_item(item_id):
|
| 766 |
rajveer |
39 |
item = Item.get_by(id=item_id)
|
|
|
40 |
return item
|
| 94 |
ashish |
41 |
|
| 635 |
rajveer |
42 |
def get_items_by_catalog_id(catalog_id):
|
| 447 |
rajveer |
43 |
query = Item.query.filter_by(catalog_item_id=catalog_id)
|
| 437 |
rajveer |
44 |
try:
|
| 635 |
rajveer |
45 |
items = query.all()
|
|
|
46 |
return get_thrift_item_list(items)
|
| 437 |
rajveer |
47 |
except:
|
|
|
48 |
raise InventoryServiceException(109, "Item not found")
|
|
|
49 |
|
| 483 |
rajveer |
50 |
def get_item_by_vendor_id(vendor_id):
|
|
|
51 |
query = Item.query.filter_by(vendor_item_id=vendor_id)
|
| 494 |
rajveer |
52 |
#query = Item.query.filter_by(vendor_item_id='6417182037054')
|
| 483 |
rajveer |
53 |
try:
|
|
|
54 |
return query.one()
|
|
|
55 |
except:
|
|
|
56 |
raise InventoryServiceException(109, "Item not found")
|
|
|
57 |
|
| 576 |
chandransh |
58 |
def is_active(item_id):
|
|
|
59 |
try:
|
| 635 |
rajveer |
60 |
item = get_item(item_id)
|
| 576 |
chandransh |
61 |
return item.status == status.ACTIVE
|
|
|
62 |
except InventoryServiceException:
|
|
|
63 |
return False
|
| 563 |
chandransh |
64 |
|
| 94 |
ashish |
65 |
def get_Warehouse(warehouse_id):
|
|
|
66 |
return Warehouse.get_by(id=warehouse_id)
|
|
|
67 |
|
| 122 |
ashish |
68 |
def get_all_warehouses_by_status(status):
|
|
|
69 |
if not status:
|
| 766 |
rajveer |
70 |
warehouses = Warehouse.query.all()
|
| 122 |
ashish |
71 |
else:
|
| 851 |
chandransh |
72 |
warehouses = Warehouse.query.filter_by(status=status).all()
|
|
|
73 |
return warehouses
|
| 122 |
ashish |
74 |
|
| 94 |
ashish |
75 |
def get_all_warehouses_for_item(item_id):
|
| 635 |
rajveer |
76 |
item = get_item(item_id)
|
| 94 |
ashish |
77 |
if not item:
|
| 122 |
ashish |
78 |
raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
|
|
|
79 |
return item.get_all_warehouses
|
| 94 |
ashish |
80 |
|
| 122 |
ashish |
81 |
def get_all_items_for_warehouse(warehouse_id):
|
|
|
82 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
83 |
if not warehouse:
|
|
|
84 |
raise InventoryServiceException(108, "bad warehouse")
|
|
|
85 |
return warehouse.all_items
|
|
|
86 |
|
| 94 |
ashish |
87 |
def add_warehouse(warehouse):
|
| 103 |
ashish |
88 |
if not warehouse:
|
| 122 |
ashish |
89 |
raise InventoryServiceException(108, "Bad warehouse")
|
| 103 |
ashish |
90 |
if get_Warehouse(warehouse.id):
|
|
|
91 |
#warehouse is already present.
|
| 122 |
ashish |
92 |
raise InventoryServiceException(101, "Warehouse already present")
|
| 103 |
ashish |
93 |
|
| 94 |
ashish |
94 |
ds_warehouse = Warehouse()
|
|
|
95 |
ds_warehouse.id = warehouse.id
|
|
|
96 |
ds_warehouse.location = warehouse.location
|
| 122 |
ashish |
97 |
ds_warehouse.status = status.ACTIVE
|
| 103 |
ashish |
98 |
ds_warehouse.addedOn = datetime.datetime.now()
|
| 483 |
rajveer |
99 |
ds_warehouse.lastCheckedOn = datetime.datetime.now()
|
|
|
100 |
ds_warehouse.tinNumber = warehouse.tinNumber
|
|
|
101 |
ds_warehouse.pincode = warehouse.pincode
|
|
|
102 |
if warehouse.vendorString:
|
|
|
103 |
ds_warehouse.vendorString = warehouse.vendorString
|
| 94 |
ashish |
104 |
session.commit()
|
| 103 |
ashish |
105 |
return ds_warehouse.id
|
| 94 |
ashish |
106 |
|
| 122 |
ashish |
107 |
def update_item(item):
|
|
|
108 |
if not item:
|
|
|
109 |
raise InventoryServiceException(108, "Bad item in request")
|
|
|
110 |
|
|
|
111 |
if not item.id:
|
| 609 |
chandransh |
112 |
raise InventoryServiceException(101, "Missing id for update")
|
| 122 |
ashish |
113 |
|
| 635 |
rajveer |
114 |
ds_item = get_item(item.id)
|
| 122 |
ashish |
115 |
|
|
|
116 |
if not ds_item:
|
| 609 |
chandransh |
117 |
raise InventoryServiceException(101, "Item missing in our database")
|
| 122 |
ashish |
118 |
|
| 511 |
rajveer |
119 |
if item.manufacturerName:
|
|
|
120 |
ds_item.manufacturer_name = item.manufacturerName
|
|
|
121 |
if item.modelName:
|
|
|
122 |
ds_item.model_name = item.modelName
|
|
|
123 |
if item.modelNumber:
|
|
|
124 |
ds_item.model_number = item.modelNumber
|
| 609 |
chandransh |
125 |
if item.color:
|
|
|
126 |
ds_item.color = item.color
|
| 483 |
rajveer |
127 |
if item.category:
|
|
|
128 |
ds_item.category = item.category
|
|
|
129 |
if item.comments:
|
|
|
130 |
ds_item.comments = item.comments
|
| 511 |
rajveer |
131 |
|
| 122 |
ashish |
132 |
if item.catalogItemId:
|
|
|
133 |
ds_item.catalog_item_id = item.catalogItemId
|
|
|
134 |
if item.vendorItemId:
|
|
|
135 |
ds_item.vendor_item_id = item.vendorItemId
|
|
|
136 |
else:
|
| 609 |
chandransh |
137 |
raise InventoryServiceException(101, "Vendor item id cannot be missing")
|
| 483 |
rajveer |
138 |
|
|
|
139 |
if item.mrp:
|
|
|
140 |
ds_item.mrp = item.mrp
|
|
|
141 |
if item.mop:
|
|
|
142 |
ds_item.mop = item.mop
|
|
|
143 |
if item.sellingPrice:
|
|
|
144 |
ds_item.sellingPrice = item.sellingPrice
|
| 609 |
chandransh |
145 |
if item.dealerPrice:
|
|
|
146 |
ds_item.dealerPrice = item.dealerPrice
|
| 122 |
ashish |
147 |
if item.weight:
|
|
|
148 |
ds_item.weight = item.weight
|
| 511 |
rajveer |
149 |
if item.itemStatus:
|
|
|
150 |
ds_item.status = item.itemStatus
|
| 122 |
ashish |
151 |
|
| 511 |
rajveer |
152 |
if item.startDate:
|
|
|
153 |
ds_item.startDate = item.startDate
|
|
|
154 |
if item.retireDate:
|
|
|
155 |
ds_item.retireDate = item.retireDate
|
|
|
156 |
|
| 122 |
ashish |
157 |
if item.featureId:
|
|
|
158 |
ds_item.feature_id = item.featureId
|
|
|
159 |
if item.featureDescription:
|
|
|
160 |
ds_item.feature_description = item.featureDescription
|
|
|
161 |
|
| 609 |
chandransh |
162 |
if item.bestDealText:
|
|
|
163 |
ds_item.bestDealText = item.bestDealText
|
|
|
164 |
if item.bestDealValue:
|
|
|
165 |
ds_item.bestDealValue = item.bestDealValue
|
|
|
166 |
|
|
|
167 |
ds_item.updated_on = datetime.datetime.now()
|
| 122 |
ashish |
168 |
session.commit();
|
|
|
169 |
return ds_item.id
|
| 94 |
ashish |
170 |
|
| 103 |
ashish |
171 |
def add_item(item):
|
|
|
172 |
if not item:
|
| 122 |
ashish |
173 |
raise InventoryServiceException(108, "Bad item in request")
|
| 635 |
rajveer |
174 |
if get_item(item.id):
|
| 122 |
ashish |
175 |
raise InventoryServiceException(101, "Item already exists")
|
| 483 |
rajveer |
176 |
|
| 103 |
ashish |
177 |
ds_item = Item()
|
| 122 |
ashish |
178 |
if item.id:
|
|
|
179 |
ds_item.id = item.id
|
| 515 |
rajveer |
180 |
if item.manufacturerName:
|
|
|
181 |
ds_item.manufacturer_name = item.manufacturerName
|
|
|
182 |
if item.modelName:
|
|
|
183 |
ds_item.model_name = item.modelName
|
|
|
184 |
if item.modelNumber:
|
|
|
185 |
ds_item.model_number = item.modelNumber
|
| 609 |
chandransh |
186 |
if item.color:
|
|
|
187 |
ds_item.color = item.color
|
| 483 |
rajveer |
188 |
if item.category:
|
|
|
189 |
ds_item.category = item.category
|
|
|
190 |
if item.comments:
|
|
|
191 |
ds_item.comments = item.comments
|
|
|
192 |
|
| 122 |
ashish |
193 |
if item.catalogItemId:
|
|
|
194 |
ds_item.catalog_item_id = item.catalogItemId
|
|
|
195 |
if item.vendorItemId:
|
|
|
196 |
ds_item.vendor_item_id = item.vendorItemId
|
|
|
197 |
else:
|
|
|
198 |
raise InventoryServiceException(101, "vendor item id cannot be missing")
|
|
|
199 |
if item.itemStatus:
|
|
|
200 |
ds_item.status = item.itemStatus
|
|
|
201 |
else: #this is for update calls
|
|
|
202 |
ds_item.status = status.IN_PROCESS
|
| 103 |
ashish |
203 |
ds_item.addedOn = datetime.datetime.now()
|
| 609 |
chandransh |
204 |
ds_item.updatedOn = datetime.datetime.now()
|
|
|
205 |
|
| 483 |
rajveer |
206 |
if item.mrp:
|
|
|
207 |
ds_item.mrp = item.mrp
|
|
|
208 |
if item.mop:
|
|
|
209 |
ds_item.mop = item.mop
|
|
|
210 |
if item.sellingPrice:
|
|
|
211 |
ds_item.sellingPrice = item.sellingPrice
|
| 609 |
chandransh |
212 |
if item.dealerPrice:
|
|
|
213 |
ds_item.dealerPrice = item.dealerPrice
|
| 122 |
ashish |
214 |
if item.weight:
|
|
|
215 |
ds_item.weight = item.weight
|
|
|
216 |
|
|
|
217 |
if item.featureId:
|
|
|
218 |
ds_item.feature_id = item.featureId
|
|
|
219 |
if item.featureDescription:
|
|
|
220 |
ds_item.feature_description = item.featureDescription
|
|
|
221 |
|
| 103 |
ashish |
222 |
if item.otherInfo:
|
|
|
223 |
for k,v in item.otherInfo.iteritems():
|
|
|
224 |
info = ItemInfo()
|
|
|
225 |
info.key = k
|
|
|
226 |
info.value = v
|
|
|
227 |
ds_item.iteminfo.append(info)
|
|
|
228 |
#check if categories present. If yes, add them to system
|
| 122 |
ashish |
229 |
|
| 609 |
chandransh |
230 |
if item.bestDealValue:
|
|
|
231 |
ds_item.bestDealValue = item.bestDealValue
|
|
|
232 |
if item.bestDealText:
|
|
|
233 |
ds_item.bestDealText = item.bestDealText
|
|
|
234 |
|
| 103 |
ashish |
235 |
session.commit();
|
| 609 |
chandransh |
236 |
return ds_item.id
|
| 103 |
ashish |
237 |
|
| 483 |
rajveer |
238 |
def update_inventory(warehouse_id, timestamp, availability):
|
|
|
239 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
240 |
if not warehouse:
|
|
|
241 |
raise InventoryServiceException(107, "Warehouse? Where?")
|
|
|
242 |
|
|
|
243 |
time = datetime.datetime.now()
|
|
|
244 |
warehouse.lastCheckedOn = time
|
|
|
245 |
warehouse.vendorString = timestamp
|
|
|
246 |
|
| 612 |
chandransh |
247 |
for item_pk, quantity in availability.iteritems():
|
| 483 |
rajveer |
248 |
try:
|
| 612 |
chandransh |
249 |
brand, model, color = item_pk.split(';')
|
|
|
250 |
item = Item.query.filter_by(manufacturer_name=brand, model_number=model, color=color).one()
|
| 494 |
rajveer |
251 |
except:
|
|
|
252 |
continue
|
|
|
253 |
try:
|
| 483 |
rajveer |
254 |
item_inventory_history = ItemInventoryHistory()
|
|
|
255 |
item_inventory_history.warehouse = warehouse
|
|
|
256 |
item_inventory_history.item = item
|
|
|
257 |
item_inventory_history.timestamp = time
|
|
|
258 |
item_inventory_history.availibility = quantity
|
|
|
259 |
|
|
|
260 |
current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
|
|
|
261 |
if not current_inventory_snapshot:
|
|
|
262 |
current_inventory_snapshot = CurrentInventorySnapshot()
|
|
|
263 |
current_inventory_snapshot.item = item
|
|
|
264 |
current_inventory_snapshot.warehouse = warehouse
|
|
|
265 |
current_inventory_snapshot.availibility = 0
|
|
|
266 |
# added the difference in the current inventory
|
|
|
267 |
current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
|
|
|
268 |
except:
|
|
|
269 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
|
|
270 |
session.commit()
|
|
|
271 |
|
| 94 |
ashish |
272 |
def get_item_inventoy(item_id):
|
|
|
273 |
|
|
|
274 |
inventory = Item.get_by(id=item_id).currentInventory
|
|
|
275 |
if not inventory:
|
| 122 |
ashish |
276 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
| 94 |
ashish |
277 |
return inventory
|
|
|
278 |
|
| 635 |
rajveer |
279 |
def get_item_inventory_by_item_id(item_id):
|
|
|
280 |
inventory = Item.get_by(id=item_id).currentInventory
|
| 379 |
ashish |
281 |
if not inventory:
|
|
|
282 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
|
|
283 |
return inventory
|
|
|
284 |
|
| 103 |
ashish |
285 |
|
|
|
286 |
def retire_warehouse(warehouse_id):
|
|
|
287 |
if not warehouse_id:
|
| 122 |
ashish |
288 |
raise InventoryServiceException(101, "Bad warehouse id")
|
| 103 |
ashish |
289 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
290 |
if not warehouse:
|
| 122 |
ashish |
291 |
raise InventoryServiceException(108, "warehouse id not present")
|
|
|
292 |
warehouse.status = status.DELETED;
|
| 103 |
ashish |
293 |
session.commit()
|
|
|
294 |
|
|
|
295 |
def retire_item(item_id):
|
|
|
296 |
if not item_id:
|
| 122 |
ashish |
297 |
raise InventoryServiceException(101, "bad item id")
|
| 635 |
rajveer |
298 |
item = get_item(item_id)
|
| 103 |
ashish |
299 |
if not item:
|
| 122 |
ashish |
300 |
raise InventoryServiceException(108, "item id not present")
|
|
|
301 |
item.status = status.PHASED_OUT
|
|
|
302 |
item.retireDate = datetime.datetime.now()
|
| 103 |
ashish |
303 |
session.commit()
|
| 122 |
ashish |
304 |
|
|
|
305 |
#need to implement threads based solution here
|
| 103 |
ashish |
306 |
def start_item_on(item_id, timestamp):
|
|
|
307 |
if not item_id:
|
| 122 |
ashish |
308 |
raise InventoryServiceException(101, "bad item id")
|
| 635 |
rajveer |
309 |
item = get_item(item_id)
|
| 103 |
ashish |
310 |
if not item:
|
| 122 |
ashish |
311 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
312 |
|
| 122 |
ashish |
313 |
item.status = status.ACTIVE
|
|
|
314 |
item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
|
|
|
315 |
add_status_change_log(item, status.ACTIVE)
|
| 103 |
ashish |
316 |
session.commit()
|
|
|
317 |
|
| 122 |
ashish |
318 |
#need to implement threads here
|
| 103 |
ashish |
319 |
def retire_item_on(item_id, timestamp):
|
|
|
320 |
if not item_id:
|
| 122 |
ashish |
321 |
raise InventoryServiceException(101, "bad item id")
|
| 635 |
rajveer |
322 |
item = get_item(item_id)
|
| 103 |
ashish |
323 |
if not item:
|
| 122 |
ashish |
324 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
325 |
|
| 122 |
ashish |
326 |
item.status = status.PHASED_OUT
|
|
|
327 |
item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
|
|
|
328 |
add_status_change_log(item, status.PHASED_OUT)
|
| 103 |
ashish |
329 |
session.commit()
|
|
|
330 |
|
|
|
331 |
def add_status_change_log(item, new_status):
|
|
|
332 |
item_change_log = ItemChangeLog()
|
|
|
333 |
item_change_log.new_status = new_status
|
|
|
334 |
item_change_log.old_status = item.status
|
|
|
335 |
item_change_log.timestamp = datetime.datetime.now()
|
|
|
336 |
item_change_log.item = item
|
|
|
337 |
session.commit()
|
|
|
338 |
|
|
|
339 |
def change_item_status(item_id, new_status):
|
|
|
340 |
if not item_id:
|
| 122 |
ashish |
341 |
raise InventoryServiceException(101, "bad item id")
|
| 635 |
rajveer |
342 |
item = get_item(item_id)
|
| 103 |
ashish |
343 |
if not item:
|
| 122 |
ashish |
344 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
345 |
|
| 122 |
ashish |
346 |
item.status = new_status
|
|
|
347 |
add_status_change_log(item, new_status)
|
| 103 |
ashish |
348 |
session.commit()
|
|
|
349 |
|
|
|
350 |
def get_item_availability_for_warehouse(warehouse_id, item_id):
|
|
|
351 |
if not warehouse_id:
|
| 122 |
ashish |
352 |
raise InventoryServiceException(101, "bad warehouse_id")
|
| 103 |
ashish |
353 |
if not item_id:
|
| 122 |
ashish |
354 |
raise InventoryServiceException(101, "bad item_id")
|
| 103 |
ashish |
355 |
|
|
|
356 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
357 |
if not warehouse:
|
| 122 |
ashish |
358 |
raise InventoryServiceException(108, "warehouse does not exist")
|
| 635 |
rajveer |
359 |
item = get_item(item_id)
|
| 504 |
rajveer |
360 |
|
| 103 |
ashish |
361 |
if not item:
|
| 766 |
rajveer |
362 |
raise InventoryServiceException(108, "item does not exist")
|
| 122 |
ashish |
363 |
|
| 494 |
rajveer |
364 |
query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
|
| 504 |
rajveer |
365 |
query = query.filter_by(item_id = item.id)
|
| 494 |
rajveer |
366 |
try:
|
|
|
367 |
current_inventory_snapshot = query.one()
|
|
|
368 |
return current_inventory_snapshot.availibility
|
|
|
369 |
except:
|
|
|
370 |
return 0
|
|
|
371 |
"""
|
|
|
372 |
current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
|
| 103 |
ashish |
373 |
if not current_inventory_snapshot:
|
|
|
374 |
return 0
|
|
|
375 |
else:
|
|
|
376 |
return current_inventory_snapshot.availibility
|
| 494 |
rajveer |
377 |
"""
|
| 643 |
chandransh |
378 |
|
| 723 |
chandransh |
379 |
def mark_item_as_content_complete(entity_id):
|
|
|
380 |
content_complete_status = status.CONTENT_COMPLETE
|
|
|
381 |
items = Item.query.filter_by(catalog_item_id=entity_id).all()
|
|
|
382 |
current_timestamp = datetime.datetime.now()
|
|
|
383 |
for item in items:
|
|
|
384 |
item_change_log = ItemChangeLog()
|
|
|
385 |
item_change_log.old_status = item.status
|
|
|
386 |
item_change_log.new_status = content_complete_status
|
|
|
387 |
item_change_log.timestamp = current_timestamp
|
|
|
388 |
item_change_log.item = item
|
|
|
389 |
|
|
|
390 |
item.status = content_complete_status
|
|
|
391 |
item.updatedOn = current_timestamp
|
|
|
392 |
session.commit()
|
|
|
393 |
return True
|
|
|
394 |
|
| 643 |
chandransh |
395 |
def get_item_availability_for_location(warehouse_loc, item_id):
|
|
|
396 |
if warehouse_loc is None:
|
|
|
397 |
raise InventoryServiceException(101, "Bad Warehouse Location")
|
|
|
398 |
if not item_id:
|
|
|
399 |
raise InventoryServiceException(101, "Bas Item id")
|
|
|
400 |
|
|
|
401 |
warehouses = Warehouse.query.filter_by(logisticsLocation=warehouse_loc).all()
|
|
|
402 |
warehouse_ids = [warehouse.id for warehouse in warehouses]
|
|
|
403 |
warehouse_retid = -1
|
|
|
404 |
global_availability = 0
|
|
|
405 |
for warehouse_id in warehouse_ids:
|
| 785 |
rajveer |
406 |
try:
|
|
|
407 |
availability = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id).one().availibility
|
|
|
408 |
except:
|
|
|
409 |
availability = 0
|
| 643 |
chandransh |
410 |
if availability > global_availability:
|
|
|
411 |
warehouse_retid = warehouse_id
|
|
|
412 |
global_availability = availability
|
|
|
413 |
if warehouse_retid == -1:
|
| 759 |
chandransh |
414 |
# This is the case when all warehouses have exhausted their
|
|
|
415 |
# inventory of this item or no warehouse is available in this
|
|
|
416 |
# location.
|
| 643 |
chandransh |
417 |
#TODO: Change 1 to be a constant read from the Config client.
|
|
|
418 |
warehouse_retid = 1
|
|
|
419 |
return [warehouse_retid, global_availability]
|
|
|
420 |
|
| 122 |
ashish |
421 |
def get_warehouses_for_item(item_id):
|
| 643 |
chandransh |
422 |
|
| 122 |
ashish |
423 |
if not item_id:
|
|
|
424 |
raise InventoryServiceException(101, "bad item_id")
|
| 635 |
rajveer |
425 |
item = get_item(item_id)
|
| 122 |
ashish |
426 |
|
|
|
427 |
if not item:
|
|
|
428 |
raise InventoryServiceException(101, "bad item")
|
|
|
429 |
|
| 483 |
rajveer |
430 |
warehouses = item.currentInventory.warehouse
|
| 501 |
rajveer |
431 |
return warehouses
|
|
|
432 |
|
| 626 |
chandransh |
433 |
def get_best_sellers(start_index, stop_index, category=-1):
|
| 621 |
chandransh |
434 |
query = get_best_sellers_query(category)
|
|
|
435 |
best_sellers = query.order_by(asc(Item.bestSellingRank)).all()[start_index:stop_index]
|
|
|
436 |
return get_thrift_item_list(best_sellers)
|
|
|
437 |
|
|
|
438 |
def get_best_sellers_count(category):
|
|
|
439 |
query = get_best_sellers_query(category)
|
| 766 |
rajveer |
440 |
count = query.count()
|
|
|
441 |
return count
|
| 621 |
chandransh |
442 |
|
| 626 |
chandransh |
443 |
def get_best_sellers_catalog_ids(start_index, stop_index, category=-1):
|
| 621 |
chandransh |
444 |
query = get_best_sellers_query(category)
|
|
|
445 |
best_sellers = query.order_by(asc(Item.bestSellingRank)).all()[start_index:stop_index]
|
|
|
446 |
return [item.catalog_item_id for item in best_sellers]
|
| 501 |
rajveer |
447 |
|
| 621 |
chandransh |
448 |
def get_best_sellers_query(category):
|
|
|
449 |
query = Item.query.filter(Item.bestSellingRank != None)
|
| 626 |
chandransh |
450 |
if category != -1:
|
| 621 |
chandransh |
451 |
query = query.filter_by(category=category)
|
|
|
452 |
return query
|
| 609 |
chandransh |
453 |
|
| 501 |
rajveer |
454 |
def get_best_deals():
|
| 621 |
chandransh |
455 |
items = Item.query.filter(Item.bestDealValue != None).order_by(desc(Item.bestDealValue)).all()
|
| 609 |
chandransh |
456 |
return get_thrift_item_list(items)
|
|
|
457 |
|
|
|
458 |
def get_best_deals_count():
|
| 766 |
rajveer |
459 |
count = Item.query.filter(Item.bestDealValue != None).count()
|
|
|
460 |
return count
|
| 501 |
rajveer |
461 |
|
| 626 |
chandransh |
462 |
def get_best_deals_catalog_ids(start_index, stop_index, category=-1):
|
| 621 |
chandransh |
463 |
query = Item.query.filter(Item.bestDealValue != None)
|
| 626 |
chandransh |
464 |
if category != -1:
|
| 621 |
chandransh |
465 |
query = query.filter_by(category=category)
|
|
|
466 |
best_deal_items = query.order_by(desc(Item.bestDealValue)).all()[start_index:stop_index]
|
|
|
467 |
return [item.catalog_item_id for item in best_deal_items]
|
| 609 |
chandransh |
468 |
|
| 599 |
chandransh |
469 |
def get_latest_arrivals(limit):
|
|
|
470 |
items = Item.query.order_by(desc(Item.startDate)).all()[0:limit]
|
| 609 |
chandransh |
471 |
return get_thrift_item_list(items)
|
| 598 |
chandransh |
472 |
|
| 602 |
chandransh |
473 |
def get_latest_arrivals_count(limit):
|
| 609 |
chandransh |
474 |
#FIXME: This method has not been functionally tested. Have to talk to Rajveer to check where is it used.
|
| 766 |
rajveer |
475 |
count = min(Item.query.count(), limit)
|
|
|
476 |
return count
|
| 602 |
chandransh |
477 |
|
| 626 |
chandransh |
478 |
def get_latest_arrivals_catalog_ids(start_index, stop_index, category=-1):
|
| 621 |
chandransh |
479 |
query = Item.query
|
| 626 |
chandransh |
480 |
if category != -1:
|
| 621 |
chandransh |
481 |
query = query.filter_by(category=category)
|
|
|
482 |
latest_arrivals = query.order_by(desc(Item.startDate)).all()[start_index:stop_index]
|
|
|
483 |
return [item.catalog_item_id for item in latest_arrivals]
|
| 609 |
chandransh |
484 |
|
|
|
485 |
def get_thrift_item_list(items):
|
|
|
486 |
ret_items = []
|
|
|
487 |
for item in items:
|
|
|
488 |
if item:
|
|
|
489 |
ret_items.append(to_t_item(item))
|
| 635 |
rajveer |
490 |
return ret_items
|
|
|
491 |
|
|
|
492 |
def put_category_object(object):
|
|
|
493 |
category = Category.get_by(id=1)
|
|
|
494 |
if category is None:
|
|
|
495 |
category = Category()
|
|
|
496 |
category.object = object
|
|
|
497 |
session.commit()
|
|
|
498 |
return True
|
|
|
499 |
|
|
|
500 |
def get_category_object():
|
| 766 |
rajveer |
501 |
object = Category.get_by(id=1).object
|
|
|
502 |
return object
|
|
|
503 |
|
|
|
504 |
def close_session():
|
|
|
505 |
if session.is_active:
|
|
|
506 |
print "session is active. closing it."
|
|
|
507 |
session.close()
|