From 4f5cfbd90a67c1bb30ec1a4a390c40bf4a82802b Mon Sep 17 00:00:00 2001 From: kir-odoo <kir@odoo.com> Date: Thu, 6 Apr 2023 13:52:18 +0000 Subject: [PATCH] [FIX] delivery: exclude neg qtys from shipping weight This commit prevents inclusion of negative qty SO products from the calculation of its estimated shipping weight. Negative qtys can indicate a return, which would be a separate picking from the delivery => we shouldn't subtract their weight from the delivery. This subtraction, may have resulted in shipping rates being calculated as lower than they should have been within the SO. Additionally fixes the following use case (requires Fedex connector): - create a SO with 2 products with the same weight - set 1st product qty = 1 - set 2nd product qty = -1 - add shipping => Shipping Method = Fedex US - click on "Get Rate" An error will occur because the SO._get_estimated_weight() = 0, and you cannot have a rate for weight = 0 TaskId - 3028023 closes odoo/odoo#104658 Signed-off-by: Tiffany Chang <tic@odoo.com> --- addons/delivery/models/sale_order.py | 2 +- addons/delivery/tests/test_delivery_cost.py | 27 +++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/addons/delivery/models/sale_order.py b/addons/delivery/models/sale_order.py index a4666271d0e0..142872fda646 100644 --- a/addons/delivery/models/sale_order.py +++ b/addons/delivery/models/sale_order.py @@ -154,7 +154,7 @@ class SaleOrder(models.Model): def _get_estimated_weight(self): self.ensure_one() weight = 0.0 - for order_line in self.order_line.filtered(lambda l: l.product_id.type in ['product', 'consu'] and not l.is_delivery and not l.display_type): + for order_line in self.order_line.filtered(lambda l: l.product_id.type in ['product', 'consu'] and not l.is_delivery and not l.display_type and l.product_uom_qty > 0): weight += order_line.product_qty * order_line.product_id.weight return weight diff --git a/addons/delivery/tests/test_delivery_cost.py b/addons/delivery/tests/test_delivery_cost.py index c51954eed274..bab40ba2e7ca 100644 --- a/addons/delivery/tests/test_delivery_cost.py +++ b/addons/delivery/tests/test_delivery_cost.py @@ -17,7 +17,7 @@ class TestDeliveryCost(common.TransactionCase): self.partner_18 = self.env['res.partner'].create({'name': 'My Test Customer'}) self.pricelist = self.env.ref('product.list0') - self.product_4 = self.env['product.product'].create({'name': 'A product to deliver'}) + self.product_4 = self.env['product.product'].create({'name': 'A product to deliver', 'weight': 1.0}) self.product_uom_unit = self.env.ref('uom.product_uom_unit') self.product_delivery_normal = self.env['product.product'].create({ 'name': 'Normal Delivery Charges', @@ -39,7 +39,7 @@ class TestDeliveryCost(common.TransactionCase): self.product_uom_hour = self.env.ref('uom.product_uom_hour') self.account_data = self.env.ref('account.data_account_type_revenue') self.account_tag_operating = self.env.ref('account.account_tag_operating') - self.product_2 = self.env['product.product'].create({'name': 'Zizizaproduct'}) + self.product_2 = self.env['product.product'].create({'name': 'Zizizaproduct', 'weight': 1.0}) self.product_category = self.env.ref('product.product_category_all') self.free_delivery = self.env.ref('delivery.free_delivery_carrier') # as the tests hereunder assume all the prices in USD, we must ensure @@ -327,3 +327,26 @@ class TestDeliveryCost(common.TransactionCase): {'product_id': self.product_2.id, 'is_delivery': False, 'product_uom_qty': 1, 'qty_delivered': 1}, {'product_id': self.normal_delivery.product_id.id, 'is_delivery': True, 'product_uom_qty': 1, 'qty_delivered': 0}, ]) + + def test_estimated_weight(self): + """ + Test that negative qty SO lines are not included in the estimated weight calculation + of delivery carriers (since it's used when calculating their rates). + """ + sale_order = self.SaleOrder.create({ + 'partner_id': self.partner_18.id, + 'name': 'SO - neg qty', + 'order_line': [ + (0, 0, { + 'product_id': self.product_4.id, + 'product_uom_qty': 1, + 'product_uom': self.product_uom_unit.id, + }), + (0, 0, { + 'product_id': self.product_2.id, + 'product_uom_qty': -1, + 'product_uom': self.product_uom_unit.id, + })], + }) + shipping_weight = sale_order._get_estimated_weight() + self.assertEqual(shipping_weight, self.product_4.weight, "Only positive quantity products' weights should be included in estimated weight") -- GitLab