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