Skip to content

Commit

Permalink
[FIX] delivery_deliverea: Send correct package data
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Coronel committed Nov 12, 2024
1 parent 2e65847 commit 9aa9928
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 22 deletions.
1 change: 1 addition & 0 deletions delivery_deliverea/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"depends": [
"delivery_state",
"delivery_package_number",
"stock_quant_package_dimension",
],
"external_dependencies": {"python": ["unidecode"]},
"data": [
Expand Down
3 changes: 2 additions & 1 deletion delivery_deliverea/data/product_packaging_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<field name="height">15</field>
<field name="width">10</field>
<field name="packaging_length">20</field>
<field name="max_weight">2</field>
<field name="max_weight">0</field>
<field name="base_weight">2</field>
</record>
</odoo>
70 changes: 49 additions & 21 deletions delivery_deliverea/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
51 changes: 51 additions & 0 deletions delivery_deliverea/tests/test_delivery_deliverea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9aa9928

Please sign in to comment.