From 1c3dde9f0663d39b16d3238ca1645e0bd8bd168c Mon Sep 17 00:00:00 2001 From: Ben Coronel Date: Tue, 12 Nov 2024 17:08:24 +0100 Subject: [PATCH] [FIX] delivery_deliverea: Send correct package data --- delivery_deliverea/__manifest__.py | 1 + .../data/product_packaging_data.xml | 3 +- delivery_deliverea/models/delivery_carrier.py | 70 +++++++++++++------ .../tests/test_delivery_deliverea.py | 51 ++++++++++++++ 4 files changed, 103 insertions(+), 22 deletions(-) diff --git a/delivery_deliverea/__manifest__.py b/delivery_deliverea/__manifest__.py index 2fb6a2f959..a8016e43f4 100644 --- a/delivery_deliverea/__manifest__.py +++ b/delivery_deliverea/__manifest__.py @@ -13,6 +13,7 @@ "depends": [ "delivery_state", "delivery_package_number", + "stock_quant_package_dimension", ], "external_dependencies": {"python": ["unidecode"]}, "data": [ diff --git a/delivery_deliverea/data/product_packaging_data.xml b/delivery_deliverea/data/product_packaging_data.xml index 633c082dae..7dd455beaf 100644 --- a/delivery_deliverea/data/product_packaging_data.xml +++ b/delivery_deliverea/data/product_packaging_data.xml @@ -7,6 +7,7 @@ 15 10 20 - 2 + 0 + 2 diff --git a/delivery_deliverea/models/delivery_carrier.py b/delivery_deliverea/models/delivery_carrier.py index aefb9be9a6..4a5df12d10 100644 --- a/delivery_deliverea/models/delivery_carrier.py +++ b/delivery_deliverea/models/delivery_carrier.py @@ -310,37 +310,65 @@ def _get_deliverea_parcel_items(self, picking): ) return items - def _get_deliverea_parcel_info(self, carrier, picking): - parcels = [] - num_packages = picking.number_of_packages or 1 - default_package = carrier.deliverea_default_packaging_id - if ( - not default_package.max_weight - or not default_package.height - or not default_package.width - or not default_package.packaging_length + def _get_package_weight(self, package): + return package.weight_uom_id._compute_quantity( + package.pack_weight, self.env.ref("uom.product_uom_kgm") + ) + + def _get_package_length(self, package, field): + return package.length_uom_id._compute_quantity( + getattr(package, field), self.env.ref("uom.product_uom_cm") + ) + + def assert_default_values(self, default_package): + if not all( + ( + default_package.base_weight, + default_package.height, + default_package.width, + default_package.packaging_length, + ) ): raise UserError( _( - "The max_weight, height, width and length values must be defined" + "The base_weight, height, width and length values must be defined" " in the package associated with the carrier." ) ) - for i in range(num_packages): + + def _get_deliverea_parcel_info(self, carrier, picking): + parcels = [] + num_packages = picking.number_of_packages or 1 + default_package = carrier.deliverea_default_packaging_id + warehouse_country_id = ( + picking.picking_type_id.warehouse_id.partner_id.country_id + ) + self.assert_default_values(default_package) + for package in picking.package_ids: parcels.append( { - "weight": default_package.max_weight, - "height": default_package.height, - "width": default_package.width, - "length": default_package.packaging_length, + "weight": self._get_package_weight(package) + or default_package.base_weight, + "height": self._get_package_length(package, "height") + or default_package.height, + "width": self._get_package_length(package, "width") + or default_package.width, + "length": self._get_package_length(package, "pack_length") + or default_package.packaging_length, } ) - if ( - i == 0 - and picking.picking_type_id.warehouse_id.partner_id.country_id.code - != picking.partner_id.country_id.code - ): - parcels[0].update({"items": self._get_deliverea_parcel_items(picking)}) + if not picking.package_ids: + for __ in range(num_packages): + parcels.append( + { + "weight": default_package.base_weight, + "height": default_package.height, + "width": default_package.width, + "length": default_package.packaging_length, + } + ) + if warehouse_country_id.code != picking.partner_id.country_id.code: + parcels[0].update({"items": self._get_deliverea_parcel_items(picking)}) return parcels def _get_service_attributes(self, carrier, service): diff --git a/delivery_deliverea/tests/test_delivery_deliverea.py b/delivery_deliverea/tests/test_delivery_deliverea.py index 4f2c77e576..af6b0c793c 100644 --- a/delivery_deliverea/tests/test_delivery_deliverea.py +++ b/delivery_deliverea/tests/test_delivery_deliverea.py @@ -457,3 +457,54 @@ def test_13_get_return_label(self): ] ) self.assertTrue(attachment) + + def test_14_get_default_packages(self): + default_package = self.carrier.deliverea_default_packaging_id + packages = self.carrier._get_deliverea_parcel_info(self.carrier, self.picking) + self.assertEqual(len(packages), 2) + self.assertTrue(packages[0].get("items")) + for package in packages: + self.assertEqual(package.get("weight"), default_package.base_weight) + self.assertEqual(package.get("width"), default_package.width) + self.assertEqual(package.get("height"), default_package.height) + self.assertEqual(package.get("length"), default_package.packaging_length) + + def test_15_get_data_packages(self): + self.picking.package_ids = [ + ( + 0, + 0, + { + "name": "Test Package", + "pack_weight": 1, + "width": 1, + "height": 1, + "pack_length": 1, + }, + ), + ( + 0, + 0, + { + "name": "Test Package 2", + "pack_weight": 0, + "width": 0, + "height": 0, + "pack_length": 0, + }, + ), + ] + # Package length uom is mm, weight uom is kg + packages = self.carrier._get_deliverea_parcel_info(self.carrier, self.picking) + self.assertEqual(len(packages), 2) + self.assertEqual(packages[0].get("weight"), 1.0) + self.assertEqual(packages[0].get("height"), 0.1) + self.assertEqual(packages[0].get("width"), 0.1) + self.assertEqual(packages[0].get("length"), 0.1) + self.assertTrue(packages[0].get("items")) + # Check default values + default_package = self.carrier.deliverea_default_packaging_id + self.assertEqual(packages[1].get("weight"), default_package.base_weight) + self.assertEqual(packages[1].get("width"), default_package.width) + self.assertEqual(packages[1].get("height"), default_package.height) + self.assertEqual(packages[1].get("length"), default_package.packaging_length)