fix inventory management

modified:   make_post_sell/views/product.py
This commit is contained in:
Russell Ballestrini 2024-06-08 19:26:04 -04:00
parent 0b7e643976
commit f218e30ad9

View file

@ -340,7 +340,7 @@ def product_edit(request):
.all()
)
inventories = {
inv.shop_location_id: inv.quantity for inv in product.inventories
inv.shop_location.id: inv.quantity for inv in product.inventories
}
return {
@ -364,19 +364,33 @@ def product_inventory_edit(request):
request.session.flash(("This product is not physical.", "error"))
return HTTPFound(get_referer_or_home(request))
for inventory in product.inventories:
quantity = request.params.get(f"inventory_{inventory.shop_location.id}", None)
# Get all shop locations for the product's shop
shop_locations = request.shop.shop_locations
updated = False
for location in shop_locations:
quantity = request.params.get(f"inventory_{location.id}", None)
if quantity is not None:
try:
quantity = int(quantity)
inventory.update_quantity(quantity)
inventory = next((inv for inv in product.inventories if inv.shop_location_id == location.id), None)
if inventory:
if inventory.quantity != quantity:
inventory.update_quantity(quantity)
request.dbsession.add(inventory)
updated = True
else:
inventory = Inventory(shop_location=location, product=product, quantity=quantity)
request.dbsession.add(inventory)
updated = True
except ValueError:
request.session.flash(
(f"Invalid quantity for location {inventory.shop_location.name}.", "error")
)
request.session.flash((f"Invalid quantity for location {location.name}.", "error"))
if updated:
request.dbsession.flush()
request.session.flash(("Inventory updated successfully.", "success"))
request.dbsession.flush()
request.session.flash(("Inventory updated successfully.", "success"))
return HTTPFound(get_referer_or_home(request))