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