From 15f791bd92453433b9ec7dd750d1c5ea1babc2fb Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Thu, 2 Apr 2026 16:46:50 +0200 Subject: [PATCH] 02.04.26 --- modules/purchase_trade/invoice.py | 30 ++++++++++++++++----- modules/purchase_trade/tests/test_module.py | 28 +++++-------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/modules/purchase_trade/invoice.py b/modules/purchase_trade/invoice.py index 3ef1d8d..630e8a4 100644 --- a/modules/purchase_trade/invoice.py +++ b/modules/purchase_trade/invoice.py @@ -375,6 +375,26 @@ class Invoice(metaclass=PoolMeta): @property def report_nb_bale(self): + total_packages = Decimal(0) + package_unit = None + has_invoice_line_packages = False + for line in self._get_report_invoice_lines(): + lot = getattr(line, 'lot', None) + if not lot or getattr(lot, 'lot_qt', None) in (None, ''): + continue + has_invoice_line_packages = True + if not package_unit and getattr(lot, 'lot_unit', None): + package_unit = lot.lot_unit + sign = Decimal(1) + if Decimal(str(getattr(line, 'quantity', 0) or 0)) < 0: + sign = Decimal(-1) + total_packages += ( + Decimal(str(lot.lot_qt or 0)).quantize( + Decimal('1'), rounding=ROUND_HALF_UP) * sign) + if has_invoice_line_packages: + label = self._format_report_package_label(package_unit) + return f"NB {label}: {int(total_packages)}" + lots = self._get_report_invoice_lots() if lots: total_packages = Decimal(0) @@ -384,12 +404,10 @@ class Invoice(metaclass=PoolMeta): total_packages += Decimal(str(lot.lot_qt or 0)) if not package_unit and getattr(lot, 'lot_unit', None): package_unit = lot.lot_unit - if total_packages: - package_qty = total_packages.quantize( - Decimal('1'), rounding=ROUND_HALF_UP) - if package_qty: - label = self._format_report_package_label(package_unit) - return f"NB {label}: {int(package_qty)}" + package_qty = total_packages.quantize( + Decimal('1'), rounding=ROUND_HALF_UP) + label = self._format_report_package_label(package_unit) + return f"NB {label}: {int(package_qty)}" sale = self._get_report_sale() if sale and sale.report_nb_bale: return sale.report_nb_bale diff --git a/modules/purchase_trade/tests/test_module.py b/modules/purchase_trade/tests/test_module.py index 075a9c5..34ce163 100644 --- a/modules/purchase_trade/tests/test_module.py +++ b/modules/purchase_trade/tests/test_module.py @@ -333,31 +333,17 @@ class PurchaseTradeTestCase(ModuleTestCase): self.assertEqual(invoice.report_net, Decimal('800')) - def test_invoice_report_nb_bale_uses_linked_lot_quantity_only(self): - 'invoice reports packaging from linked physical lots using lot_qt only' + def test_invoice_report_nb_bale_sums_signed_line_lot_quantities(self): + 'invoice reports packaging from the signed sum of line lot_qt values' Invoice = Pool().get('account.invoice') - line = Mock(type='line', quantity=Decimal('-15')) - line.unit = Mock(rec_name='MT') - sale_line = Mock() - lot = Mock( - lot_type='physic', - lot_qt=Decimal('700'), - lot_unit=Mock(symbol='bale'), - lot_quantity=Decimal('2000'), - lot_unit_line=line.unit, - sale_invoice_line=line, - sale_invoice_line_prov=None, - invoice_line=None, - invoice_line_prov=None, - ) - sale_line.lots = [lot] - sale = Mock(lines=[sale_line]) + lot = Mock(lot_qt=Decimal('350'), lot_unit=Mock(symbol='bale')) + negative = Mock(type='line', quantity=Decimal('-1000'), lot=lot) + positive = Mock(type='line', quantity=Decimal('1000'), lot=lot) invoice = Invoice() - invoice.sales = [sale] - invoice.lines = [line] + invoice.lines = [negative, positive] - self.assertEqual(invoice.report_nb_bale, 'NB BALES: 700') + self.assertEqual(invoice.report_nb_bale, 'NB BALES: 0') def test_invoice_report_positive_rate_lines_keep_positive_components(self): 'invoice final note pricing section keeps only positive component lines'