Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][FIX] delivery_auto_refresh: dynamic carrier line #796

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 18 additions & 6 deletions delivery_auto_refresh/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,22 @@ def _get_param_auto_add_delivery_line(self):
def _auto_refresh_delivery(self):
self.ensure_one()
# Make sure that if you have removed the carrier, the line is gone
if self.state in {"draft", "sent"}:
if self.state in {"draft", "sent"} and not self.env.context.get(
"deleting_delivery_line"
):
# Context added to avoid the recursive calls and save the new
# value of carrier_id
self.with_context(auto_refresh_delivery=True)._remove_delivery_line()
if self._get_param_auto_add_delivery_line() and self.carrier_id:
if self.state in {"draft", "sent"}:
price_unit = self.carrier_id.rate_shipment(self)["price"]
if not self.is_all_service:
self._create_delivery_line(self.carrier_id, price_unit)
self.with_context(auto_refresh_delivery=True).write(
{"recompute_delivery_price": False}
)
self.with_context(skip_validation_check=True)._create_delivery_line(
self.carrier_id, price_unit
)
self.with_context(
auto_refresh_delivery=True, skip_validation_check=True
).write({"recompute_delivery_price": False})

@api.model
def create(self, vals):
Expand All @@ -80,7 +84,15 @@ def create(self, vals):
return order

def write(self, vals):
"""Create or refresh delivery line after saving."""
"""Create or refresh the delivery line after saving."""
# Check if it's already deleting a delivery line to not
# delete it again inside `_auto_refresh_delivery()`
deleting_delivery_line = vals.get("order_line") and any(
i[0] == 2 and self.env["sale.order.line"].browse(i[1]).is_delivery
for i in vals["order_line"]
)
if deleting_delivery_line:
self = self.with_context(deleting_delivery_line=deleting_delivery_line)
res = super().write(vals)
if self._get_param_auto_add_delivery_line() and not self.env.context.get(
"auto_refresh_delivery"
Expand Down
11 changes: 9 additions & 2 deletions delivery_auto_refresh/tests/test_delivery_auto_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ def test_auto_refresh_so_and_unlink_line(self):
self.assertFalse(delivery_line.exists())

def test_auto_add_delivery_line_add_service(self):
"""Delivery line should not be created because
there are only service products in SO"""
self.env["ir.config_parameter"].sudo().set_param(self.auto_add_delivery_line, 1)
service = self.env["product.product"].create(
{"name": "Service Test", "type": "service"}
)
Expand All @@ -318,3 +317,11 @@ def test_auto_add_delivery_line_add_service(self):
order = order_form.save()
delivery_line = order.order_line.filtered("is_delivery")
self.assertFalse(delivery_line.exists())

def test_auto_refresh_so_and_manually_unlink_delivery_line(self):
"""Test that we are able to manually remove the delivery line"""
self._test_autorefresh_unlink_line()
sale_form = Form(self.order)
# Deleting the delivery line
sale_form.order_line.remove(1)
sale_form.save()
Loading