Skip to content

Commit

Permalink
[FIX] delivery_multi_destination: consider no carrier scenario
Browse files Browse the repository at this point in the history
When there is no carrier, destination_type is false and can cause error.
In the case, `one` destination_type should be used.

delivery_multi_destination 14.0.1.0.1
  • Loading branch information
hailangvn authored and victoralmau committed Nov 4, 2022
1 parent 726f1bd commit 0e28544
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion delivery_multi_destination/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{
"name": "Multiple destinations for the same delivery method",
"version": "14.0.1.0.0",
"version": "14.0.1.0.1",
"category": "Delivery",
"website": "https://github.com/OCA/delivery-carrier",
"author": "Tecnativa, " "Odoo Community Association (OCA)",
Expand Down
60 changes: 32 additions & 28 deletions delivery_multi_destination/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
# Copyright 2017 Luis M. Ontalba <luis.martinez@tecnativa.com>
# Copyright 2021 Gianmarco Conte <gconte@dinamicheaziendali.it>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

_logger = logging.getLogger(__name__)


class DeliveryCarrier(models.Model):
_inherit = "delivery.carrier"
Expand Down Expand Up @@ -77,33 +80,34 @@ def send_shipping(self, pickings):
"""We have to override this method for redirecting the result to the
proper "child" carrier.
"""
if self.destination_type == "one":
# falsy type is considered as one destination
if not self.destination_type or self.destination_type == "one":
return super().send_shipping(pickings)
carrier = self.with_context(show_children_carriers=True)
res = []
for p in pickings:
picking_res = False
for subcarrier in carrier.child_ids:
picking_res = subcarrier._send_shipping_next(p, picking_res)
if not picking_res:
raise ValidationError(_("There is no matching delivery rule."))
res += picking_res
return res

def _send_shipping_next(self, picking, picking_res):
if self.delivery_type == "fixed":
if self._match_address(picking.partner_id):
picking_res = [
{
"exact_price": self.fixed_price,
"tracking_number": False,
}
]
# TODO: verify if not match address, previous picking_res (passed
# in method's argument) can be used.
else:
carrier = self.with_context(show_children_carriers=True)
res = []
for p in pickings:
picking_res = False
for subcarrier in carrier.child_ids:
if subcarrier.delivery_type == "fixed":
if subcarrier._match_address(p.partner_id):
picking_res = [
{
"exact_price": subcarrier.fixed_price,
"tracking_number": False,
}
]
break
else:
try:
picking_res = super(
DeliveryCarrier,
subcarrier,
).send_shipping(pickings)
break
except Exception:
pass
if not picking_res:
raise ValidationError(_("There is no matching delivery rule."))
res += picking_res
return res
try:
picking_res = super().send_shipping(picking)
except Exception as err:
_logger.warning("%s: %s", "_send_shipping_next", str(err))
return picking_res

0 comments on commit 0e28544

Please sign in to comment.