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