Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion container_meals/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"product",
"sale_management", # deposit management
"uom_extra_data", # mL
"website_sale",
"website_sale_delivery",
],
"data": [
"data/portion_attributes.xml",
Expand Down
19 changes: 13 additions & 6 deletions container_meals/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
9 changes: 9 additions & 0 deletions container_meals/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
33 changes: 33 additions & 0 deletions container_meals/tests/test_sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading