From c18064d717312287f5acba2fabeb77b2e1c01fb9 Mon Sep 17 00:00:00 2001
From: "Touati Djamel (otd)" <otd@odoo.com>
Date: Wed, 30 Nov 2022 09:04:45 +0000
Subject: [PATCH] [FIX] delivery: raise a UserError if the total weight is 0
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Steps to reproduce the bug:
- Install `delivery_dhl`
- Create a storable product “P1”:
    - make sure the weight of the product is equal to 0
- create a transfert:
    - Type: delivery orders
    - Add the product “P1”
    - Go to “Additional info” tab and select “DHL” in the carrier field
    - Mark as Todo
    - Validate

Problem:
A traceback is triggered, because in order to set the shipment details,
a package should be created, so the `_get_packages_from_picking`
function is called:
https://github.com/odoo/enterprise/blob/aed802ee17dba5ebe12b42594503732a2662be68/delivery_dhl/models/dhl_request.py#L166

But as the total weight of the products is equal to 0, the package is
not created:

https://github.com/odoo/odoo/blob/6b0ab28791f4a29254d294f8a116545d4c124e8b/addons/delivery/models/delivery_carrier.py#L324-L325

then, the result is used without checking if the package has been
created:
https://github.com/odoo/enterprise/blob/aed802ee17dba5ebe12b42594503732a2662be68/delivery_dhl/models/dhl_request.py#L185

Solution:
If the total products weight is equal to 0, raise a UserError

opw-3076826
opw-3075562

closes odoo/odoo#106809

Signed-off-by: William Henrotin (whe) <whe@odoo.com>
---
 addons/delivery/i18n/delivery.pot          | 9 +++++++++
 addons/delivery/models/delivery_carrier.py | 3 +++
 2 files changed, 12 insertions(+)

diff --git a/addons/delivery/i18n/delivery.pot b/addons/delivery/i18n/delivery.pot
index efb527e4daa3..0b54cf4894af 100644
--- a/addons/delivery/i18n/delivery.pot
+++ b/addons/delivery/i18n/delivery.pot
@@ -982,6 +982,15 @@ msgstr ""
 msgid "The Poste"
 msgstr ""
 
+#. module: delivery
+#. odoo-python
+#: 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"
+" picking is 0.0 %s"
+msgstr ""
+
 #. module: delivery
 #: model:ir.model.fields,help:delivery.field_delivery_carrier__get_return_label_from_portal
 msgid ""
diff --git a/addons/delivery/models/delivery_carrier.py b/addons/delivery/models/delivery_carrier.py
index 0f4e5b5f2afe..6309a2b254c0 100644
--- a/addons/delivery/models/delivery_carrier.py
+++ b/addons/delivery/models/delivery_carrier.py
@@ -5,6 +5,7 @@ import psycopg2
 
 from odoo import api, fields, models, registry, SUPERUSER_ID, _
 from odoo.tools.float_utils import float_round
+from odoo.exceptions import UserError
 
 from .delivery_request_objects import DeliveryCommodity, DeliveryPackage
 
@@ -361,6 +362,8 @@ class DeliveryCarrier(models.Model):
             for move_line in picking.move_line_ids:
                 package_total_cost += self._product_price_to_company_currency(move_line.qty_done, move_line.product_id, picking.company_id)
             packages.append(DeliveryPackage(commodities, picking.weight_bulk, default_package_type, name='Bulk Content', total_cost=package_total_cost, currency=picking.company_id.currency_id, picking=picking))
+        elif not packages:
+            raise UserError(_("The package cannot be created because the total weight of the products in the picking is 0.0 %s") % (picking.weight_uom_name))
 
         return packages
 
-- 
GitLab