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