From 0801d1a92ec1a36b8df54c79b54f2a18df7aaf9a Mon Sep 17 00:00:00 2001 From: Achraf Date: Wed, 7 Dec 2022 11:50:54 +0000 Subject: [PATCH] [FIX] delivery: Fix ZeroDivisionError in shipping cost calculation A `ZeroDivisionError` traceback that occurs in `delivery/DeliveryCarrier:_get_packages_from_order` was caught by Sentry Because in this part of the function https://github.com/odoo/odoo/blob/c18064d717312287f5acba2fabeb77b2e1c01fb9/addons/delivery/models/delivery_carrier.py#L326-L335 if `total_weight === 0.0` then `total_full_packages` and `last_package_weight` will be null too. And that will cause a `ZeroDivisionError` because of `total / len([])` opw-3086602 closes odoo/odoo#107820 X-original-commit: a362e14ee62a5d9d2bcb08c4431994a96796ce1e Signed-off-by: William Henrotin (whe) --- addons/delivery/i18n/delivery.pot | 1 + addons/delivery/models/delivery_carrier.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/addons/delivery/i18n/delivery.pot b/addons/delivery/i18n/delivery.pot index 049548b3a06..e4241b6022b 100644 --- a/addons/delivery/i18n/delivery.pot +++ b/addons/delivery/i18n/delivery.pot @@ -994,6 +994,7 @@ msgstr "" #. module: delivery #. odoo-python #: code:addons/delivery/models/delivery_carrier.py:0 +#: code:addons/delivery/models/delivery_carrier.py:0 #, python-format msgid "" "The package cannot be created because the total weight of the products in the" diff --git a/addons/delivery/models/delivery_carrier.py b/addons/delivery/models/delivery_carrier.py index da3ee5ad523..3fc79df1fbc 100644 --- a/addons/delivery/models/delivery_carrier.py +++ b/addons/delivery/models/delivery_carrier.py @@ -328,6 +328,9 @@ class DeliveryCarrier(models.Model): total_cost += self._product_price_to_company_currency(line.product_qty, line.product_id, order.company_id) total_weight = order._get_estimated_weight() + default_package_type.base_weight + if total_weight == 0.0: + weight_uom_name = self.env['product.template']._get_weight_uom_name_from_ir_config_parameter() + raise UserError(_("The package cannot be created because the total weight of the products in the picking is 0.0 %s") % (weight_uom_name)) # If max weight == 0 => division by 0. If this happens, we want to have # more in the max weight than in the total weight, so that it only # creates ONE package with everything.