| 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 |
|
| 94 |
ashish |
47 |
def get_Warehouse(warehouse_id):
|
|
|
48 |
return Warehouse.get_by(id=warehouse_id)
|
|
|
49 |
|
| 122 |
ashish |
50 |
def get_all_warehouses_by_status(status):
|
|
|
51 |
if not status:
|
|
|
52 |
return Warehouse.query.all()
|
|
|
53 |
else:
|
|
|
54 |
return Warehouse.query.filter_by(status=status)
|
|
|
55 |
|
| 94 |
ashish |
56 |
def get_all_warehouses_for_item(item_id):
|
|
|
57 |
item = get_Item(item_id)
|
|
|
58 |
if not item:
|
| 122 |
ashish |
59 |
raise InventoryServiceException(108, "Some unforeseen error while obtaining item")
|
|
|
60 |
return item.get_all_warehouses
|
| 94 |
ashish |
61 |
|
| 122 |
ashish |
62 |
def get_all_items_for_warehouse(warehouse_id):
|
|
|
63 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
64 |
if not warehouse:
|
|
|
65 |
raise InventoryServiceException(108, "bad warehouse")
|
|
|
66 |
return warehouse.all_items
|
|
|
67 |
|
| 94 |
ashish |
68 |
def add_warehouse(warehouse):
|
| 103 |
ashish |
69 |
if not warehouse:
|
| 122 |
ashish |
70 |
raise InventoryServiceException(108, "Bad warehouse")
|
| 103 |
ashish |
71 |
if get_Warehouse(warehouse.id):
|
|
|
72 |
#warehouse is already present.
|
| 122 |
ashish |
73 |
raise InventoryServiceException(101, "Warehouse already present")
|
| 103 |
ashish |
74 |
|
| 94 |
ashish |
75 |
ds_warehouse = Warehouse()
|
|
|
76 |
ds_warehouse.id = warehouse.id
|
|
|
77 |
ds_warehouse.location = warehouse.location
|
| 122 |
ashish |
78 |
ds_warehouse.status = status.ACTIVE
|
| 103 |
ashish |
79 |
ds_warehouse.addedOn = datetime.datetime.now()
|
| 94 |
ashish |
80 |
session.commit()
|
| 103 |
ashish |
81 |
return ds_warehouse.id
|
| 94 |
ashish |
82 |
|
| 122 |
ashish |
83 |
def update_item(item):
|
|
|
84 |
if not item:
|
|
|
85 |
raise InventoryServiceException(108, "Bad item in request")
|
|
|
86 |
|
|
|
87 |
if not item.id:
|
|
|
88 |
raise InventoryServiceException(101, "non existent item")
|
|
|
89 |
|
|
|
90 |
ds_item = get_Item(item.id)
|
|
|
91 |
|
|
|
92 |
if not ds_item:
|
|
|
93 |
raise InventoryServiceException(101, "Item already exists")
|
|
|
94 |
|
|
|
95 |
if item.catalogItemId:
|
|
|
96 |
ds_item.catalog_item_id = item.catalogItemId
|
|
|
97 |
if item.vendorItemId:
|
|
|
98 |
ds_item.vendor_item_id = item.vendorItemId
|
|
|
99 |
else:
|
|
|
100 |
raise InventoryServiceException(101, "vendor item id cannot be missing")
|
|
|
101 |
|
|
|
102 |
if item.weight:
|
|
|
103 |
ds_item.weight = item.weight
|
|
|
104 |
|
|
|
105 |
if item.featureId:
|
|
|
106 |
ds_item.feature_id = item.featureId
|
|
|
107 |
if item.featureDescription:
|
|
|
108 |
ds_item.feature_description = item.featureDescription
|
|
|
109 |
|
|
|
110 |
session.commit();
|
|
|
111 |
return ds_item.id
|
| 94 |
ashish |
112 |
|
| 103 |
ashish |
113 |
def add_item(item):
|
|
|
114 |
if not item:
|
| 122 |
ashish |
115 |
raise InventoryServiceException(108, "Bad item in request")
|
| 103 |
ashish |
116 |
if get_Item(item.id):
|
| 122 |
ashish |
117 |
raise InventoryServiceException(101, "Item already exists")
|
| 103 |
ashish |
118 |
|
|
|
119 |
ds_item = Item()
|
| 122 |
ashish |
120 |
if item.id:
|
|
|
121 |
ds_item.id = item.id
|
|
|
122 |
if item.catalogItemId:
|
|
|
123 |
ds_item.catalog_item_id = item.catalogItemId
|
|
|
124 |
if item.vendorItemId:
|
|
|
125 |
ds_item.vendor_item_id = item.vendorItemId
|
|
|
126 |
else:
|
|
|
127 |
raise InventoryServiceException(101, "vendor item id cannot be missing")
|
|
|
128 |
if item.itemStatus:
|
|
|
129 |
ds_item.status = item.itemStatus
|
|
|
130 |
else: #this is for update calls
|
|
|
131 |
ds_item.status = status.IN_PROCESS
|
| 103 |
ashish |
132 |
ds_item.addedOn = datetime.datetime.now()
|
| 122 |
ashish |
133 |
if item.price:
|
|
|
134 |
update_price(item, ds_item)
|
|
|
135 |
if item.weight:
|
|
|
136 |
ds_item.weight = item.weight
|
|
|
137 |
|
|
|
138 |
if item.featureId:
|
|
|
139 |
ds_item.feature_id = item.featureId
|
|
|
140 |
if item.featureDescription:
|
|
|
141 |
ds_item.feature_description = item.featureDescription
|
|
|
142 |
|
| 103 |
ashish |
143 |
if item.otherInfo:
|
|
|
144 |
for k,v in item.otherInfo.iteritems():
|
|
|
145 |
info = ItemInfo()
|
|
|
146 |
info.key = k
|
|
|
147 |
info.value = v
|
|
|
148 |
ds_item.iteminfo.append(info)
|
|
|
149 |
#check if categories present. If yes, add them to system
|
| 122 |
ashish |
150 |
|
| 103 |
ashish |
151 |
session.commit();
|
|
|
152 |
return ds_item.id
|
|
|
153 |
|
| 122 |
ashish |
154 |
def update_price(item, ds_item):
|
|
|
155 |
log_entry("update_price", "updating price for item")
|
|
|
156 |
for k,v in item.price.iteritems():
|
|
|
157 |
price = Price()
|
|
|
158 |
price.warehouse_id = k
|
|
|
159 |
price.price = v
|
| 386 |
ashish |
160 |
price.item = ds_item
|
|
|
161 |
session.commit()
|
| 122 |
ashish |
162 |
|
|
|
163 |
def update_price_for_warehouse(item_id, warehouse_id, price):
|
|
|
164 |
log_entry("update_price", "updating price for item and warehouse")
|
|
|
165 |
price = Price.query.filter_by(warehouse_id=warehouse_id, item_id=item_id).one()
|
|
|
166 |
if not price:
|
|
|
167 |
price = Price()
|
|
|
168 |
item = get_Item(item_id)
|
|
|
169 |
price.warehouse_id = warehouse_id
|
|
|
170 |
price.item = item
|
|
|
171 |
price.price = price
|
|
|
172 |
session.commit()
|
| 94 |
ashish |
173 |
|
|
|
174 |
|
|
|
175 |
def update_inventory(item_id, warehouse_id, quantity, timestamp):
|
|
|
176 |
item = get_Item(item_id)
|
|
|
177 |
warehouse = get_Warehouse(warehouse_id)
|
| 122 |
ashish |
178 |
time = to_py_date(timestamp)
|
| 94 |
ashish |
179 |
|
|
|
180 |
if not item:
|
| 122 |
ashish |
181 |
raise InventoryServiceException(106, "Item not found, how did you get this?")
|
| 94 |
ashish |
182 |
if not warehouse:
|
| 122 |
ashish |
183 |
raise InventoryServiceException(107, "Warehouse? Where?")
|
| 94 |
ashish |
184 |
try:
|
|
|
185 |
item_inventory_history = ItemInventoryHistory()
|
|
|
186 |
item_inventory_history.warehouse = warehouse
|
|
|
187 |
item_inventory_history.item = item
|
|
|
188 |
item_inventory_history.timestamp = time
|
| 122 |
ashish |
189 |
item_inventory_history.availibility = quantity
|
| 94 |
ashish |
190 |
|
|
|
191 |
current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
|
| 122 |
ashish |
192 |
if not current_inventory_snapshot:
|
|
|
193 |
current_inventory_snapshot = CurrentInventorySnapshot()
|
|
|
194 |
current_inventory_snapshot.item = item
|
|
|
195 |
current_inventory_snapshot.warehouse = warehouse
|
|
|
196 |
|
| 94 |
ashish |
197 |
current_inventory_snapshot.availibility = quantity
|
|
|
198 |
current_inventory_snapshot.checkedOn = time
|
|
|
199 |
session.commit()
|
|
|
200 |
except:
|
| 122 |
ashish |
201 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
| 94 |
ashish |
202 |
|
|
|
203 |
def get_item_inventoy(item_id):
|
|
|
204 |
|
|
|
205 |
inventory = Item.get_by(id=item_id).currentInventory
|
|
|
206 |
if not inventory:
|
| 122 |
ashish |
207 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
| 94 |
ashish |
208 |
return inventory
|
|
|
209 |
|
| 383 |
ashish |
210 |
def get_item_inventory_by_catalog_id(item_id):
|
| 379 |
ashish |
211 |
inventory = Item.get_by(catalog_item_id=item_id).currentInventory
|
|
|
212 |
if not inventory:
|
|
|
213 |
raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
|
|
|
214 |
return inventory
|
|
|
215 |
|
| 103 |
ashish |
216 |
|
|
|
217 |
def retire_warehouse(warehouse_id):
|
|
|
218 |
if not warehouse_id:
|
| 122 |
ashish |
219 |
raise InventoryServiceException(101, "Bad warehouse id")
|
| 103 |
ashish |
220 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
221 |
if not warehouse:
|
| 122 |
ashish |
222 |
raise InventoryServiceException(108, "warehouse id not present")
|
|
|
223 |
warehouse.status = status.DELETED;
|
| 103 |
ashish |
224 |
session.commit()
|
|
|
225 |
|
|
|
226 |
def retire_item(item_id):
|
|
|
227 |
if not item_id:
|
| 122 |
ashish |
228 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
229 |
item = get_Item(item_id)
|
|
|
230 |
if not item:
|
| 122 |
ashish |
231 |
raise InventoryServiceException(108, "item id not present")
|
|
|
232 |
item.status = status.PHASED_OUT
|
|
|
233 |
item.retireDate = datetime.datetime.now()
|
| 103 |
ashish |
234 |
session.commit()
|
| 122 |
ashish |
235 |
|
|
|
236 |
#need to implement threads based solution here
|
| 103 |
ashish |
237 |
def start_item_on(item_id, timestamp):
|
|
|
238 |
if not item_id:
|
| 122 |
ashish |
239 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
240 |
item = get_Item(item_id)
|
|
|
241 |
if not item:
|
| 122 |
ashish |
242 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
243 |
|
| 122 |
ashish |
244 |
item.status = status.ACTIVE
|
|
|
245 |
item.startDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
|
|
|
246 |
add_status_change_log(item, status.ACTIVE)
|
| 103 |
ashish |
247 |
session.commit()
|
|
|
248 |
|
| 122 |
ashish |
249 |
#need to implement threads here
|
| 103 |
ashish |
250 |
def retire_item_on(item_id, timestamp):
|
|
|
251 |
if not item_id:
|
| 122 |
ashish |
252 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
253 |
item = get_Item(item_id)
|
|
|
254 |
if not item:
|
| 122 |
ashish |
255 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
256 |
|
| 122 |
ashish |
257 |
item.status = status.PHASED_OUT
|
|
|
258 |
item.retireDate = datetime.datetime.fromtimestamp(to_py_date(timestamp))
|
|
|
259 |
add_status_change_log(item, status.PHASED_OUT)
|
| 103 |
ashish |
260 |
session.commit()
|
|
|
261 |
|
|
|
262 |
def add_status_change_log(item, new_status):
|
|
|
263 |
item_change_log = ItemChangeLog()
|
|
|
264 |
item_change_log.new_status = new_status
|
|
|
265 |
item_change_log.old_status = item.status
|
|
|
266 |
item_change_log.timestamp = datetime.datetime.now()
|
|
|
267 |
item_change_log.item = item
|
|
|
268 |
session.commit()
|
|
|
269 |
|
|
|
270 |
def change_item_status(item_id, new_status):
|
|
|
271 |
if not item_id:
|
| 122 |
ashish |
272 |
raise InventoryServiceException(101, "bad item id")
|
| 103 |
ashish |
273 |
item = get_Item(item_id)
|
|
|
274 |
if not item:
|
| 122 |
ashish |
275 |
raise InventoryServiceException(108, "item id not present")
|
| 103 |
ashish |
276 |
|
| 122 |
ashish |
277 |
item.status = new_status
|
|
|
278 |
add_status_change_log(item, new_status)
|
| 103 |
ashish |
279 |
session.commit()
|
|
|
280 |
|
|
|
281 |
def get_item_availability_for_warehouse(warehouse_id, item_id):
|
|
|
282 |
if not warehouse_id:
|
| 122 |
ashish |
283 |
raise InventoryServiceException(101, "bad warehouse_id")
|
| 103 |
ashish |
284 |
if not item_id:
|
| 122 |
ashish |
285 |
raise InventoryServiceException(101, "bad item_id")
|
| 103 |
ashish |
286 |
|
|
|
287 |
warehouse = get_Warehouse(warehouse_id)
|
|
|
288 |
if not warehouse:
|
| 122 |
ashish |
289 |
raise InventoryServiceException(108, "warehouse does not exist")
|
| 103 |
ashish |
290 |
item = get_Item(item_id)
|
|
|
291 |
|
|
|
292 |
if not item:
|
| 122 |
ashish |
293 |
raise InventoryServiceException(108, "item does not exist")
|
|
|
294 |
|
| 103 |
ashish |
295 |
current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse.id == warehouse_id, CurrentInventorySnapshot.item.id == item_id).one()
|
|
|
296 |
if not current_inventory_snapshot:
|
|
|
297 |
return 0
|
|
|
298 |
else:
|
|
|
299 |
return current_inventory_snapshot.availibility
|
|
|
300 |
|
| 122 |
ashish |
301 |
def get_warehouses_for_item(item_id):
|
|
|
302 |
if not item_id:
|
|
|
303 |
raise InventoryServiceException(101, "bad item_id")
|
|
|
304 |
item = get_Item(item_id)
|
|
|
305 |
|
|
|
306 |
if not item:
|
|
|
307 |
raise InventoryServiceException(101, "bad item")
|
|
|
308 |
|
|
|
309 |
warehouses = item.currentInventory.warehouse
|