diff --git a/addons/delivery/models/sale_order.py b/addons/delivery/models/sale_order.py index a4666271d0e04d816119c393808770834811cdc8..142872fda6469eae657da9af7d93a41d17370ca2 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 c51954eed2749aa4722944e50e9f54f779268b4f..bab40ba2e7caeb3545deacd6ee2b2b13b794fde4 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")