Skip to content

Commit dbab84b

Browse files
committed
[IMP] define proper delivery.carrier methods
* define .base_on_destination_rate_shipment() and .base_on_destination_send_shipping() instead of overriding .rate_shipment() and .send_shipping() and filtering on destination_type. * always return a value in .base_on_destination_rate_shipment() to fail nicely if no sub-carrier matches.
1 parent 9c70acf commit dbab84b

File tree

1 file changed

+50
-56
lines changed

1 file changed

+50
-56
lines changed

delivery_multi_destination/models/delivery_carrier.py

+50-56
Original file line numberDiff line numberDiff line change
@@ -103,60 +103,54 @@ def available_carriers(self, partner):
103103
available |= carrier
104104
return available
105105

106-
def rate_shipment(self, order):
107-
"""We have to override this method for getting the proper price
108-
according destination on sales orders.
109-
"""
110-
if self.destination_type == "one":
111-
return super().rate_shipment(order)
112-
else:
113-
carrier = self.with_context(show_children_carriers=True)
114-
for subcarrier in carrier.child_ids:
115-
if subcarrier._match_address(order.partner_shipping_id):
116-
return super(
117-
DeliveryCarrier,
118-
subcarrier,
119-
).rate_shipment(order)
106+
def base_on_destination_rate_shipment(self, order):
107+
carrier = self.with_context(show_children_carriers=True)
108+
for subcarrier in carrier.child_ids:
109+
if subcarrier._match_address(order.partner_shipping_id):
110+
return subcarrier.rate_shipment(order)
111+
# this should normally not happen, because the delivery carrier should
112+
# be filtered out before, but in case it is somehow selected anyway,
113+
# at least it will fail nicely.
114+
return {
115+
"success": False,
116+
"price": 0.0,
117+
"error_message": _(
118+
"Error: this delivery method is not available for this address."
119+
),
120+
"warning_message": False,
121+
}
120122

121-
def send_shipping(self, pickings):
122-
"""We have to override this method for redirecting the result to the
123-
proper "child" carrier.
124-
"""
125-
if self.destination_type == "one" or not self:
126-
return super().send_shipping(pickings)
127-
else:
128-
carrier = self.with_context(show_children_carriers=True)
129-
res = []
130-
for p in pickings:
131-
picking_res = False
132-
for subcarrier in carrier.child_ids.filtered(
133-
lambda x: not x.company_id or x.company_id == p.company_id
134-
):
135-
if subcarrier.delivery_type == "fixed":
136-
if subcarrier._match_address(p.partner_id):
137-
picking_res = [
138-
{
139-
"exact_price": subcarrier.fixed_price,
140-
"tracking_number": False,
141-
}
142-
]
143-
break
144-
else:
145-
try:
146-
# on base_on_rule_send_shipping, the method
147-
# _get_price_available is called using p.carrier_id,
148-
# ignoring the self arg, so we need to temporarily replace
149-
# it with the subcarrier
150-
p.carrier_id = subcarrier.id
151-
picking_res = super(
152-
DeliveryCarrier, subcarrier
153-
).send_shipping(p)
154-
break
155-
except Exception: # pylint: disable=except-pass
156-
pass
157-
finally:
158-
p.carrier_id = carrier
159-
if not picking_res:
160-
raise ValidationError(_("There is no matching delivery rule."))
161-
res += picking_res
162-
return res
123+
def base_on_destination_send_shipping(self, pickings):
124+
carrier = self.with_context(show_children_carriers=True)
125+
res = []
126+
for p in pickings:
127+
picking_res = False
128+
for subcarrier in carrier.child_ids.filtered(
129+
lambda x: not x.company_id or x.company_id == p.company_id
130+
):
131+
if subcarrier.delivery_type == "fixed":
132+
if subcarrier._match_address(p.partner_id):
133+
picking_res = [
134+
{
135+
"exact_price": subcarrier.fixed_price,
136+
"tracking_number": False,
137+
}
138+
]
139+
break
140+
else:
141+
try:
142+
# on base_on_rule_send_shipping, the method
143+
# _get_price_available is called using p.carrier_id,
144+
# ignoring the self arg, so we need to temporarily replace
145+
# it with the subcarrier
146+
p.carrier_id = subcarrier.id
147+
picking_res = subcarrier.send_shipping(p)
148+
break
149+
except Exception: # pylint: disable=except-pass
150+
pass
151+
finally:
152+
p.carrier_id = carrier
153+
if not picking_res:
154+
raise ValidationError(_("There is no matching delivery rule."))
155+
res += picking_res
156+
return res

0 commit comments

Comments
 (0)