diff --git a/container_meals/__manifest__.py b/container_meals/__manifest__.py index b406e9a9c..ba7c5b374 100644 --- a/container_meals/__manifest__.py +++ b/container_meals/__manifest__.py @@ -14,7 +14,7 @@ "product", "sale_management", # deposit management "uom_extra_data", # mL - "website_sale", + "website_sale_delivery", ], "data": [ "data/portion_attributes.xml", diff --git a/container_meals/models/sale_order.py b/container_meals/models/sale_order.py index a47c30a59..cc37e3bd0 100644 --- a/container_meals/models/sale_order.py +++ b/container_meals/models/sale_order.py @@ -23,6 +23,7 @@ class SaleOrder(models.Model): ) def _recompute_prices(self): + self._remove_containers() # Apply product prices from pricelists and discounts result = super()._recompute_prices() # Containers should be added afterward because: @@ -160,14 +161,20 @@ def _cart_update( values = super()._cart_update(product_id, line_id, add_qty, set_qty, **kwargs) return values + def _compute_amount_total_without_delivery(self): + amount_total = super()._compute_amount_total_without_delivery() + container_cost = sum( + [ + line.price_total + for line in self.order_line + if line.is_container or line.is_container_deposit + ] + ) + return amount_total - container_cost + def _remove_containers(self): self.ensure_one() - deposit_product = self.env[ - "ir.config_parameter" - ].get_container_deposit_product_id() - lines_to_remove = self.order_line.filtered( - lambda line: line.product_id.is_container - or line.product_id == deposit_product + lambda line: line.is_container or line.is_container_deposit ) lines_to_remove.unlink() diff --git a/container_meals/models/sale_order_line.py b/container_meals/models/sale_order_line.py index c0d811185..574f9f65d 100644 --- a/container_meals/models/sale_order_line.py +++ b/container_meals/models/sale_order_line.py @@ -11,6 +11,7 @@ class SaleOrderLine(models.Model): not_returned = fields.Integer(default=0) is_container = fields.Boolean(related="product_id.is_container") + is_container_deposit = fields.Boolean(compute="_compute_is_container_deposit") @api.constrains("not_returned", "product_uom_qty") def _check_not_returned(self): @@ -23,3 +24,11 @@ def _check_not_returned(self): raise ValidationError( _("'Not Returned' may not be higher than Quantity.") ) + + @api.depends("product_id") + def _compute_is_container_deposit(self): + deposit_product = self.env[ + "ir.config_parameter" + ].get_container_deposit_product_id() + for line in self: + line.is_container_deposit = line.product_id == deposit_product diff --git a/container_meals/tests/test_sale_order.py b/container_meals/tests/test_sale_order.py index 15e7b4556..0c976e562 100644 --- a/container_meals/tests/test_sale_order.py +++ b/container_meals/tests/test_sale_order.py @@ -236,3 +236,36 @@ def test_rounding_error_biggest_container(self): ) self.assertTrue(container_line) self.assertEqual(container_line.product_uom_qty, 1) + + def test_delivery_fees(self): + """ + Test that the delivery fees don't take the price of the containers + into account. + """ + carrier = self.env["delivery.carrier"].create( + { + "name": "test delivery carrier", + "product_id": self.ref("delivery.product_product_delivery"), + "fixed_price": 5, + "free_over": True, + "amount": 15, + } + ) + cart = self.sale_order._cart_update( + product_id=self.salad_product_adult.id, line_id=None, add_qty=1, set_qty=0 + ) + self.sale_order.add_containers() + delivery = carrier.rate_shipment(self.sale_order) + # the delivery should not be free because even if the order costs 23.8 + # in total (with the containers), the price of the meal is only 13.8, + # so it does not exceed 15. + self.assertEqual(delivery["price"], 5) + cart = self.sale_order._cart_update( + product_id=self.salad_product_adult.id, + line_id=cart["line_id"], + add_qty=1, + set_qty=0, + ) + delivery = carrier.rate_shipment(self.sale_order) + # with 2 meals, the price exceeds 15, so the delivery should be free. + self.assertEqual(delivery["price"], 0)