diff --git a/addons/delivery/models/sale_order.py b/addons/delivery/models/sale_order.py index b3588f05dcd..53723782bad 100644 --- a/addons/delivery/models/sale_order.py +++ b/addons/delivery/models/sale_order.py @@ -147,6 +147,6 @@ class SaleOrder(models.Model): if self.delivery_set: return self.shipping_weight 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 76b377e6207..40924783e61 100644 --- a/addons/delivery/tests/test_delivery_cost.py +++ b/addons/delivery/tests/test_delivery_cost.py @@ -16,7 +16,7 @@ class TestDeliveryCost(common.TransactionCase): self.Product = self.env['product.product'] self.partner_18 = self.env['res.partner'].create({'name': 'My Test Customer'}) - 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', @@ -37,7 +37,7 @@ class TestDeliveryCost(common.TransactionCase): }) self.product_uom_hour = self.env.ref('uom.product_uom_hour') 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 @@ -296,3 +296,26 @@ class TestDeliveryCost(common.TransactionCase): ]) self.assertRecordValues(line, [{'price_subtotal': 9.09, 'price_total': 10.45}]) + + 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")