| 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,\
|
| 122 |
ashish |
10 |
ItemChangeLog, Price
|
|
|
11 |
from shop2020.thriftpy.model.v1.catalog.ttypes import \
|
|
|
12 |
InventoryServiceException, status
|
| 94 |
ashish |
13 |
from shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItem
|
| 122 |
ashish |
14 |
|
| 94 |
ashish |
15 |
from shop2020.thriftpy.model.v1.catalog.ttypes import Warehouse as TWarehouse
|
|
|
16 |
import datetime
|
| 122 |
ashish |
17 |
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
|
| 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:
|
| 122 |
ashish |
26 |
return Item.query.filter_by(status= status.ACTIVE).all()
|
| 103 |
ashish |
27 |
else:
|
|
|
28 |
return Item.query.all()
|
|
|
29 |
|
|
|
30 |
def get_all_items_by_status(status):
|
|
|
31 |
if not status:
|
|
|
32 |
#return all items
|
|
|
33 |
return get_all_items(False)
|
|
|
34 |
else:
|
| 122 |
ashish |
35 |
return Item.query.filter_by(status=status).all()
|
| 103 |
ashish |
36 |
|
| 94 |
ashish |
37 |
def get_Item(item_id):
|
|
|
38 |
return Item.get_by(id=item_id)
|
|
|
39 |
|
| 379 |
ashish |
40 |
def get_item_by_catalog_id(catalog_id):
|
| 447 |
rajveer |
41 |
query = Item.query.filter_by(catalog_item_id=catalog_id)
|
| 437 |
rajveer |
42 |
try:
|
|
|
43 |
return query.one()
|
|
|
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)
|
|
|
49 |
try:
|
|
|
50 |
return query.one()
|
|
|
51 |
except:
|
|
|
52 |
raise InventoryServiceException(109, "Item not found")
|
|
|
53 |
|
| 94 |
ashish |
54 |
def get_Warehouse(warehouse_id):
|
|
|
55 |
return Warehouse.get_by(id=warehouse_id)
|
|
|
56 |
|
| 122 |
ashish |
57 |
def get_all_warehouses_by_status(status):
|
|
|
58 |
if not status:
|
|
|
59 |
return Warehouse.query.all()
|
|
|
60 |
else:
|
|
|
61 |
return Warehouse.query.filter_by(status=status)
|
|
|
62 |
|
| 94 |
ashish |
63 |
def get_all_warehouses_for_item(item_id):
|
|
|
64 |
item = get_Item(item_id)
|
|
|
65 |
if not item:
|
| 122 |
ashish |
66 |
raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
|
|
|
67 |
return item.get_all_warehouses
|
| 94 |
ashish |
68 |
|
| 122 |
ashish |
69 |
def get_all_items_for_warehouse(warehouse_id):
|
|
|
70 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
71 |
if not warehouse:
|
|
|
72 |
raise InventoryServiceException(108, "bad warehouse")
|
|
|
73 |
return warehouse.all_items
|
|
|
74 |
|
| 94 |
ashish |
75 |
def add_warehouse(warehouse):
|
| 103 |
ashish |
76 |
if not warehouse:
|
| 122 |
ashish |
77 |
raise InventoryServiceException(108, "Bad warehouse")
|
| 103 |
ashish |
78 |
if get_Warehouse(warehouse.id):
|
|
|
79 |
#warehouse is already present.
|
| 122 |
ashish |
80 |
raise InventoryServiceException(101, "Warehouse already present")
|
| 103 |
ashish |
81 |
|
| 94 |
ashish |
82 |
ds_warehouse = Warehouse()
|
|
|
83 |
ds_warehouse.id = warehouse.id
|
|
|
84 |
ds_warehouse.location = warehouse.location
|
| 122 |
ashish |
85 |
ds_warehouse.status = status.ACTIVE
|
| 103 |
ashish |
86 |
ds_warehouse.addedOn = datetime.datetime.now()
|
| 483 |
rajveer |
87 |
ds_warehouse.lastCheckedOn = datetime.datetime.now()
|
|
|
88 |
ds_warehouse.tinNumber = warehouse.tinNumber
|
|
|
89 |
ds_warehouse.pincode = warehouse.pincode
|
|
|
90 |
if warehouse.vendorString:
|
|
|
91 |
ds_warehouse.vendorString = warehouse.vendorString
|
| 94 |
ashish |
92 |
session.commit()
|
| 103 |
ashish |
93 |
return ds_warehouse.id
|
| 94 |
ashish |
94 |
|
| 122 |
ashish |
95 |
def update_item(item):
|
|
|
96 |
if not item:
|
|
|
97 |
raise InventoryServiceException(108, "Bad item in request")
|
|
|
98 |
|
|
|
99 |
if not item.id:
|
|
|
100 |
raise InventoryServiceException(101, "non existent item")
|
|
|
101 |
|
|
|
102 |
ds_item = get_Item(item.id)
|
|
|
103 |
|
|
|
104 |
if not ds_item:
|
|
|
105 |
raise InventoryServiceException(101, "Item already exists")
|
|
|
106 |
|
| 483 |
rajveer |
107 |
if item.manufacturer_name:
|
|
|
108 |
ds_item.manufacturer_name = item.manufacturer_name
|
|
|
109 |
if item.model_name:
|
|
|
110 |
ds_item.model_name = item.model_name
|
|
|
111 |
if item.model_number:
|
|
|
112 |
ds_item.model_number = item.model_number
|
|
|
113 |
if item.category:
|
|
|
114 |
ds_item.category = item.category
|
|
|
115 |
if item.comments:
|
|
|
116 |
ds_item.comments = item.comments
|
|
|
117 |
|
| 122 |
ashish |
118 |
if item.catalogItemId:
|
|
|
119 |
ds_item.catalog_item_id = item.catalogItemId
|
|
|
120 |
if item.vendorItemId:
|
|
|
121 |
ds_item.vendor_item_id = item.vendorItemId
|
|
|
122 |
else:
|
|
|
123 |
raise InventoryServiceException(101, "vendor item id cannot be missing")
|
| 483 |
rajveer |
124 |
|
|
|
125 |
if item.mrp:
|
|
|
126 |
ds_item.mrp = item.mrp
|
|
|
127 |
if item.mop:
|
|
|
128 |
ds_item.mop = item.mop
|
|
|
129 |
if item.sellingPrice:
|
|
|
130 |
ds_item.sellingPrice = item.sellingPrice
|
| 122 |
ashish |
131 |
if item.weight:
|
|
|
132 |
ds_item.weight = item.weight
|
| 483 |
rajveer |
133 |
if item.weight:
|
|
|
134 |
ds_item.weight = item.weight
|
| 122 |
ashish |
135 |
|
|
|
136 |
if item.featureId:
|
|
|
137 |
ds_item.feature_id = item.featureId
|
|
|
138 |
if item.featureDescription:
|
|
|
139 |
ds_item.feature_description = item.featureDescription
|
|
|
140 |
|
|
|
141 |
session.commit();
|
|
|
142 |
return ds_item.id
|
| 94 |
ashish |
143 |
|
| 103 |
ashish |
144 |
def add_item(item):
|
|
|
145 |
if not item:
|
| 122 |
ashish |
146 |
raise InventoryServiceException(108, "Bad item in request")
|
| 103 |
ashish |
147 |
if get_Item(item.id):
|
| 122 |
ashish |
148 |
raise InventoryServiceException(101, "Item already exists")
|
| 483 |
rajveer |
149 |
|
| 103 |
ashish |
150 |
ds_item = Item()
|
| 122 |
ashish |
151 |
if item.id:
|
|
|
152 |
ds_item.id = item.id
|
| 483 |
rajveer |
153 |
if item.manufacturer_name:
|
|
|
154 |
ds_item.manufacturer_name = item.manufacturer_name
|
|
|
155 |
if item.model_name:
|
|
|
156 |
ds_item.model_name = item.model_name
|
|
|
157 |
if item.model_number:
|
|
|
158 |
ds_item.model_number = item.model_number
|
|
|
159 |
if item.category:
|
|
|
160 |
ds_item.category = item.category
|
|
|
161 |
if item.comments:
|
|
|
162 |
ds_item.comments = item.comments
|
|
|
163 |
|
| 122 |
ashish |
164 |
if item.catalogItemId:
|
|
|
165 |
ds_item.catalog_item_id = item.catalogItemId
|
|
|
166 |
if item.vendorItemId:
|
|
|
167 |
ds_item.vendor_item_id = item.vendorItemId
|
|
|
168 |
else:
|
|
|
169 |
raise InventoryServiceException(101, "vendor item id cannot be missing")
|
|
|
170 |
if item.itemStatus:
|
|
|
171 |
ds_item.status = item.itemStatus
|
|
|
172 |
else: #this is for update calls
|
|
|
173 |
ds_item.status = status.IN_PROCESS
|
| 103 |
ashish |
174 |
ds_item.addedOn = datetime.datetime.now()
|
| 483 |
rajveer |
175 |
if item.mrp:
|
|
|
176 |
ds_item.mrp = item.mrp
|
|
|
177 |
if item.mop:
|
|
|
178 |
ds_item.mop = item.mop
|
|
|
179 |
if item.sellingPrice:
|
|
|
180 |
ds_item.sellingPrice = item.sellingPrice
|
| 122 |
ashish |
181 |
if item.weight:
|
|
|
182 |
ds_item.weight = item.weight
|
|
|
183 |
|
|
|
184 |
if item.featureId:
|
|
|
185 |
ds_item.feature_id = item.featureId
|
|
|
186 |
if item.featureDescription:
|
|
|
187 |
ds_item.feature_description = item.featureDescription
|
|
|
188 |
|
| 103 |
ashish |
189 |
if item.otherInfo:
|
|
|
190 |
for k,v in item.otherInfo.iteritems():
|
|
|
191 |
info = ItemInfo()
|
|
|
192 |
info.key = k
|
|
|
193 |
info.value = v
|
|
|
194 |
ds_item.iteminfo.append(info)
|
|
|
195 |
#check if categories present. If yes, add them to system
|
| 122 |
ashish |
196 |
|
| 103 |
ashish |
197 |
session.commit();
|
|
|
198 |
return ds_item.id
|
|
|
199 |
|
| 483 |
rajveer |
200 |
#this is no more used
|
| 122 |
ashish |
201 |
def update_price(item, ds_item):
|
|
|
202 |
log_entry("update_price", "updating price for item")
|
|
|
203 |
for k,v in item.price.iteritems():
|
|
|
204 |
price = Price()
|
|
|
205 |
price.warehouse_id = k
|
|
|
206 |
price.price = v
|
| 386 |
ashish |
207 |
price.item = ds_item
|
|
|
208 |
session.commit()
|
| 122 |
ashish |
209 |
|
| 483 |
rajveer |
210 |
#this is no more used
|
| 122 |
ashish |
211 |
def update_price_for_warehouse(item_id, warehouse_id, price):
|
|
|
212 |
log_entry("update_price", "updating price for item and warehouse")
|
|
|
213 |
price = Price.query.filter_by(warehouse_id=warehouse_id, item_id=item_id).one()
|
|
|
214 |
if not price:
|
|
|
215 |
price = Price()
|
|
|
216 |
item = get_Item(item_id)
|
|
|
217 |
price.warehouse_id = warehouse_id
|
|
|
218 |
price.item = item
|
|
|
219 |
price.price = price
|
|
|
220 |
session.commit()
|
| 94 |
ashish |
221 |
|
|
|
222 |
|
| 483 |
rajveer |
223 |
def update_inventory(warehouse_id, timestamp, availability):
|
|
|
224 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
225 |
if not warehouse:
|
|
|
226 |
raise InventoryServiceException(107, "Warehouse? Where?")
|
|
|
227 |
|
|
|
228 |
time = datetime.datetime.now()
|
|
|
229 |
warehouse.lastCheckedOn = time
|
|
|
230 |
warehouse.vendorString = timestamp
|
|
|
231 |
|
|
|
232 |
for k,v in availability.iteritems():
|
|
|
233 |
vendor_id = k
|
|
|
234 |
quantity = v
|
|
|
235 |
item = get_item_by_vendor_id(vendor_id)
|
|
|
236 |
if not item:
|
|
|
237 |
raise InventoryServiceException(106, "Item not found, how did you get this?")
|
|
|
238 |
try:
|
|
|
239 |
item_inventory_history = ItemInventoryHistory()
|
|
|
240 |
item_inventory_history.warehouse = warehouse
|
|
|
241 |
item_inventory_history.item = item
|
|
|
242 |
item_inventory_history.timestamp = time
|
|
|
243 |
item_inventory_history.availibility = quantity
|
|
|
244 |
|
|
|
245 |
current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
|
|
|
246 |
if not current_inventory_snapshot:
|
|
|
247 |
current_inventory_snapshot = CurrentInventorySnapshot()
|
|
|
248 |
current_inventory_snapshot.item = item
|
|
|
249 |
current_inventory_snapshot.warehouse = warehouse
|
|
|
250 |
current_inventory_snapshot.availibility = 0
|
|
|
251 |
# added the difference in the current inventory
|
|
|
252 |
current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
|
|
|
253 |
except:
|
|
|
254 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
|
|
255 |
session.commit()
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
#this is no more used
|
|
|
260 |
def update_inventory_old(item_id, warehouse_id, quantity, timestamp):
|
| 94 |
ashish |
261 |
item = get_Item(item_id)
|
|
|
262 |
warehouse = get_Warehouse(warehouse_id)
|
| 122 |
ashish |
263 |
time = to_py_date(timestamp)
|
| 94 |
ashish |
264 |
|
|
|
265 |
if not item:
|
| 122 |
ashish |
266 |
raise InventoryServiceException(106, "Item not found, how did you get this?")
|
| 94 |
ashish |
267 |
if not warehouse:
|
| 122 |
ashish |
268 |
raise InventoryServiceException(107, "Warehouse? Where?")
|
| 94 |
ashish |
269 |
try:
|
|
|
270 |
item_inventory_history = ItemInventoryHistory()
|
|
|
271 |
item_inventory_history.warehouse = warehouse
|
|
|
272 |
item_inventory_history.item = item
|
|
|
273 |
item_inventory_history.timestamp = time
|
| 122 |
ashish |
274 |
item_inventory_history.availibility = quantity
|
| 94 |
ashish |
275 |
|
|
|
276 |
current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
|
| 122 |
ashish |
277 |
if not current_inventory_snapshot:
|
|
|
278 |
current_inventory_snapshot = CurrentInventorySnapshot()
|
|
|
279 |
current_inventory_snapshot.item = item
|
|
|
280 |
current_inventory_snapshot.warehouse = warehouse
|
|
|
281 |
|
| 94 |
ashish |
282 |
current_inventory_snapshot.availibility = quantity
|
|
|
283 |
current_inventory_snapshot.checkedOn = time
|
|
|
284 |
session.commit()
|
|
|
285 |
except:
|
| 122 |
ashish |
286 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
| 94 |
ashish |
287 |
|
|
|
288 |
def get_item_inventoy(item_id):
|
|
|
289 |
|
|
|
290 |
inventory = Item.get_by(id=item_id).currentInventory
|
|
|
291 |
if not inventory:
|
| 122 |
ashish |
292 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
| 94 |
ashish |
293 |
return inventory
|
|
|
294 |
|
| 383 |
ashish |
295 |
def get_item_inventory_by_catalog_id(item_id):
|
| 379 |
ashish |
296 |
inventory = Item.get_by(catalog_item_id=item_id).currentInventory
|
|
|
297 |
if not inventory:
|
|
|
298 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
|
|
299 |
return inventory
|
|
|
300 |
|
| 103 |
ashish |
301 |
|
|
|
302 |
def retire_warehouse(warehouse_id):
|
|
|
303 |
if not warehouse_id:
|
| 122 |
ashish |
304 |
raise InventoryServiceException(101, "Bad warehouse id")
|
| 103 |
ashish |
305 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
306 |
if not warehouse:
|
| 122 |
ashish |
307 |
raise InventoryServiceException(108, "warehouse id not present")
|
|
|
308 |
warehouse.status = status.DELETED;
|
| 103 |
ashish |
309 |
session.commit()
|
|
|
310 |
|
|
|
311 |
def retire_item(item_id):
|
|
|
312 |
if not item_id:
|
| 122 |
ashish |
313 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
314 |
item = get_Item(item_id)
|
|
|
315 |
if not item:
|
| 122 |
ashish |
316 |
raise InventoryServiceException(108, "item id not present")
|
|
|
317 |
item.status = status.PHASED_OUT
|
|
|
318 |
item.retireDate = datetime.datetime.now()
|
| 103 |
ashish |
319 |
session.commit()
|
| 122 |
ashish |
320 |
|
|
|
321 |
#need to implement threads based solution here
|
| 103 |
ashish |
322 |
def start_item_on(item_id, timestamp):
|
|
|
323 |
if not item_id:
|
| 122 |
ashish |
324 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
325 |
item = get_Item(item_id)
|
|
|
326 |
if not item:
|
| 122 |
ashish |
327 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
328 |
|
| 122 |
ashish |
329 |
item.status = status.ACTIVE
|
|
|
330 |
item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
|
|
|
331 |
add_status_change_log(item, status.ACTIVE)
|
| 103 |
ashish |
332 |
session.commit()
|
|
|
333 |
|
| 122 |
ashish |
334 |
#need to implement threads here
|
| 103 |
ashish |
335 |
def retire_item_on(item_id, timestamp):
|
|
|
336 |
if not item_id:
|
| 122 |
ashish |
337 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
338 |
item = get_Item(item_id)
|
|
|
339 |
if not item:
|
| 122 |
ashish |
340 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
341 |
|
| 122 |
ashish |
342 |
item.status = status.PHASED_OUT
|
|
|
343 |
item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
|
|
|
344 |
add_status_change_log(item, status.PHASED_OUT)
|
| 103 |
ashish |
345 |
session.commit()
|
|
|
346 |
|
|
|
347 |
def add_status_change_log(item, new_status):
|
|
|
348 |
item_change_log = ItemChangeLog()
|
|
|
349 |
item_change_log.new_status = new_status
|
|
|
350 |
item_change_log.old_status = item.status
|
|
|
351 |
item_change_log.timestamp = datetime.datetime.now()
|
|
|
352 |
item_change_log.item = item
|
|
|
353 |
session.commit()
|
|
|
354 |
|
|
|
355 |
def change_item_status(item_id, new_status):
|
|
|
356 |
if not item_id:
|
| 122 |
ashish |
357 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
358 |
item = get_Item(item_id)
|
|
|
359 |
if not item:
|
| 122 |
ashish |
360 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
361 |
|
| 122 |
ashish |
362 |
item.status = new_status
|
|
|
363 |
add_status_change_log(item, new_status)
|
| 103 |
ashish |
364 |
session.commit()
|
|
|
365 |
|
|
|
366 |
def get_item_availability_for_warehouse(warehouse_id, item_id):
|
|
|
367 |
if not warehouse_id:
|
| 122 |
ashish |
368 |
raise InventoryServiceException(101, "bad warehouse_id")
|
| 103 |
ashish |
369 |
if not item_id:
|
| 122 |
ashish |
370 |
raise InventoryServiceException(101, "bad item_id")
|
| 103 |
ashish |
371 |
|
|
|
372 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
373 |
if not warehouse:
|
| 122 |
ashish |
374 |
raise InventoryServiceException(108, "warehouse does not exist")
|
| 103 |
ashish |
375 |
item = get_Item(item_id)
|
|
|
376 |
|
|
|
377 |
if not item:
|
| 122 |
ashish |
378 |
raise InventoryServiceException(108, "item does not exist")
|
|
|
379 |
|
| 103 |
ashish |
380 |
current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse.id == warehouse_id, CurrentInventorySnapshot.item.id == item_id).one()
|
|
|
381 |
if not current_inventory_snapshot:
|
|
|
382 |
return 0
|
|
|
383 |
else:
|
|
|
384 |
return current_inventory_snapshot.availibility
|
|
|
385 |
|
| 122 |
ashish |
386 |
def get_warehouses_for_item(item_id):
|
|
|
387 |
if not item_id:
|
|
|
388 |
raise InventoryServiceException(101, "bad item_id")
|
|
|
389 |
item = get_Item(item_id)
|
|
|
390 |
|
|
|
391 |
if not item:
|
|
|
392 |
raise InventoryServiceException(101, "bad item")
|
|
|
393 |
|
| 483 |
rajveer |
394 |
warehouses = item.currentInventory.warehouse
|
|
|
395 |
return warehouses
|