diff --git a/make_post_sell/models/cart.py b/make_post_sell/models/cart.py index b3ccbe0..a751036 100644 --- a/make_post_sell/models/cart.py +++ b/make_post_sell/models/cart.py @@ -300,7 +300,6 @@ class Cart(RBase, Base): ) return self._discounted_shop_totals - @property def total_price_in_cents(self): """ @@ -456,7 +455,27 @@ class Cart(RBase, Base): elif self.handling_option == "local_shipping": self.handling_cost_in_cents = shop_location.local_shipping_rate_in_cents elif self.handling_option == "international_shipping": - self.handling_cost_in_cents = shop_location.international_shipping_rate_in_cents + self.handling_cost_in_cents = ( + shop_location.international_shipping_rate_in_cents + ) + + def update_inventory(self, shop_location): + """ + Update the inventory quantity for physical products in the cart. + """ + changed = False + for product_id, product in self.physical_products.items(): + quantity_needed = self.cart[product_id] + inventory = get_inventory_by_product_and_shop_location( + self.dbsession, product_id, shop_location.uuid_str + ) + if inventory: + inventory.quantity -= quantity_needed + self.dbsession.add(inventory) + changed = True + + if changed: + self.dbsession.flush() def get_all_carts(dbsession): diff --git a/make_post_sell/models/invoice.py b/make_post_sell/models/invoice.py index 8e13efc..9246142 100644 --- a/make_post_sell/models/invoice.py +++ b/make_post_sell/models/invoice.py @@ -202,7 +202,7 @@ class Invoice(RBase, Base): def total_in_cents(self): """Calculate the total amount in cents for the invoice, including handling fee.""" line_items_total = sum( - item.price_in_cents * item.quantity for item in self.line_items + item.price.price_in_cents * item.quantity for item in self.line_items ) return line_items_total + (self.handling_cost_in_cents or 0) diff --git a/make_post_sell/views/cart.py b/make_post_sell/views/cart.py index 17285ce..5946390 100644 --- a/make_post_sell/views/cart.py +++ b/make_post_sell/views/cart.py @@ -465,6 +465,7 @@ def cart_complete_checkout(request): for shop_id, product_quantity_tuple in cart.shop_product_dict.items(): shop = cart.shops[shop_id] invoice = Invoice(user=request.user) + invoice.shop = shop invoice.shop_id = shop.id invoice.handling_option = cart.handling_option invoice.handling_cost_in_cents = cart.handling_cost_in_cents @@ -522,6 +523,9 @@ def cart_complete_checkout(request): break if successful_checkout: + # Update inventory for physical products + cart.update_inventory(request.shop_location) + # flush changes to database because charge was successful! request.dbsession.flush() msg = ("Success, you have completed the purchase!", "success")