Skip to content

Commit

Permalink
[FIX] delivery_auto_refresh: don't allow to pick a not allowed carrier
Browse files Browse the repository at this point in the history
When choosing the sale carrier from the header of the order form we
should filter those carriers not available for the given shipping
address the in the same way the wizard does it.

TT35200
  • Loading branch information
chienandalu committed Mar 24, 2022
1 parent ac74a20 commit 27ffc44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
21 changes: 20 additions & 1 deletion delivery_auto_refresh/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,33 @@ class SaleOrder(models.Model):
carrier_id = fields.Many2one(
compute="_compute_carrier_id", store=True, readonly=False
)
available_carrier_ids = fields.Many2many(
comodel_name="delivery.carrier", compute="_compute_available_carrier_ids",
)

@api.depends("partner_id", "partner_shipping_id")
def _compute_carrier_id(self):
if hasattr(super(), "_compute_carrier_id"):
super()._compute_carrier_id()
for order in self:
action = order.action_open_delivery_wizard()
order.carrier_id = action["context"]["default_carrier_id"]
carrier_id = self.env["delivery.carrier"].browse(
action["context"]["default_carrier_id"]
)
# If the carrier isn't allowed for the current shipping address, we wont
# default to it. In that case we'd try to fallback to the former carrier.
order.carrier_id = fields.first(
(carrier_id | order.carrier_id).filtered(
lambda x: x in order.available_carrier_ids._origin
)
)

@api.depends("partner_shipping_id")
def _compute_available_carrier_ids(self):
"""We want to apply the same carriers filter in the header as in the wizard"""
for sale in self:
wizard = self.env["choose.delivery.carrier"].new({"order_id": sale.id})
sale.available_carrier_ids = wizard.available_carrier_ids._origin

def _get_param_auto_add_delivery_line(self):
get_param = self.env["ir.config_parameter"].sudo().get_param
Expand Down
6 changes: 5 additions & 1 deletion delivery_auto_refresh/views/sale_order_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='payment_term_id']" position='after'>
<field name="carrier_id" />
<field name="available_carrier_ids" invisible="1" />
<field
name="carrier_id"
domain="[('id', 'in', available_carrier_ids)]"
/>
</xpath>
</data>
</field>
Expand Down

0 comments on commit 27ffc44

Please sign in to comment.