This commit is contained in:
2026-04-09 21:23:27 +02:00
parent b39607d987
commit 9c8d7f11ae
3 changed files with 276 additions and 25 deletions

View File

@@ -789,6 +789,106 @@ class PurchaseTradeTestCase(ModuleTestCase):
'USC 70.2500 PER POUND (SEVENTY USC AND TWENTY FIVE CENTS) ON ICE Cotton #2 MAY 2026',
])
def test_sale_report_converts_mixed_units_for_total_and_words(self):
'sale report totals prefer the virtual lot unit as common unit'
Sale = Pool().get('sale.sale')
mt = Mock(id=1, rec_name='MT')
kg = Mock(id=2, rec_name='KILOGRAM')
line_mt = Mock()
line_mt.type = 'line'
line_mt.quantity = Decimal('1000')
line_mt.unit = mt
line_mt.del_period = Mock(description='MARCH 2026')
line_mt.lots = []
virtual = Mock(lot_type='virtual', lot_unit_line=kg)
virtual.get_hist_quantity.return_value = (
Decimal('1000000'),
Decimal('1000000'),
)
line_kg = Mock()
line_kg.type = 'line'
line_kg.quantity = Decimal('1000')
line_kg.unit = mt
line_kg.del_period = Mock(description='MAY 2026')
line_kg.lots = [virtual]
sale = Sale()
sale.lines = [line_mt, line_kg]
uom_model = Mock()
uom_model.compute_qty.side_effect = (
lambda from_unit, qty, to_unit: (
qty * 1000
if getattr(from_unit, 'rec_name', None) == 'MT'
and getattr(to_unit, 'rec_name', None) == 'KILOGRAM'
else (
qty / 1000
if getattr(from_unit, 'rec_name', None) == 'KILOGRAM'
and getattr(to_unit, 'rec_name', None) == 'MT'
else qty
)
))
with patch('trytond.modules.purchase_trade.sale.Pool') as PoolMock:
PoolMock.return_value.get.return_value = uom_model
self.assertEqual(sale.report_total_quantity, '2000000.0')
self.assertEqual(sale.report_quantity_unit_upper, 'KILOGRAM')
self.assertEqual(sale.report_qt, 'TWO MILLION KILOGRAMS')
self.assertEqual(
sale.report_quantity_lines.splitlines(),
[
'1000.0 MT (ONE THOUSAND METRIC TONS) - MARCH 2026',
'1000000.0 KILOGRAM (ONE MILLION KILOGRAMS) - MAY 2026',
])
def test_sale_report_total_unit_falls_back_when_multiple_virtual_lots(self):
'sale report common unit uses virtual only when there is a single one'
Sale = Pool().get('sale.sale')
mt = Mock(id=1, rec_name='MT')
kg = Mock(id=2, rec_name='KILOGRAM')
line_mt = Mock(type='line', quantity=Decimal('1000'), unit=mt)
line_mt.del_period = Mock(description='MARCH 2026')
line_mt.lots = []
virtual_a = Mock(lot_type='virtual', lot_unit_line=kg)
virtual_a.get_hist_quantity.return_value = (
Decimal('1000000'),
Decimal('1000000'),
)
virtual_b = Mock(lot_type='virtual', lot_unit_line=kg)
virtual_b.get_hist_quantity.return_value = (
Decimal('1000000'),
Decimal('1000000'),
)
line_kg = Mock(type='line', quantity=Decimal('1000'), unit=mt)
line_kg.del_period = Mock(description='MAY 2026')
line_kg.lots = [virtual_a, virtual_b]
sale = Sale()
sale.lines = [line_mt, line_kg]
uom_model = Mock()
uom_model.compute_qty.side_effect = (
lambda from_unit, qty, to_unit: (
qty / 1000
if getattr(from_unit, 'rec_name', None) == 'KILOGRAM'
and getattr(to_unit, 'rec_name', None) == 'MT'
else qty
))
with patch('trytond.modules.purchase_trade.sale.Pool') as PoolMock:
PoolMock.return_value.get.return_value = uom_model
self.assertEqual(sale.report_quantity_unit_upper, 'MT')
self.assertEqual(sale.report_total_quantity, '2000.0')
self.assertEqual(sale.report_qt, 'TWO THOUSAND METRIC TONS')
def test_report_product_fields_expose_name_and_description(self):
'sale and invoice templates use stable product name/description helpers'
Sale = Pool().get('sale.sale')
@@ -1015,7 +1115,7 @@ class PurchaseTradeTestCase(ModuleTestCase):
'invoice net and gross weights come from the current lot hist entry'
Invoice = Pool().get('account.invoice')
unit = Mock(rec_name='LBS')
unit = Mock(rec_name='LBS', symbol='LBS')
lot = Mock(lot_unit_line=unit)
lot.get_hist_quantity.return_value = (
Decimal('950'),
@@ -1030,7 +1130,36 @@ class PurchaseTradeTestCase(ModuleTestCase):
self.assertEqual(invoice.report_weight_unit_upper, 'LBS')
self.assertEqual(
invoice.report_quantity_lines,
'950.0 LBS (2094389.00 LBS)')
'950.0 LBS (950.00 LBS)')
def test_invoice_report_lbs_converts_kilogram_to_lbs(self):
'invoice lbs helper converts kilogram quantities with the proper uom ratio'
Invoice = Pool().get('account.invoice')
kg = Mock(id=1, rec_name='KILOGRAM', symbol='KG')
lbs = Mock(id=2, rec_name='LBS', symbol='LBS')
lot = Mock(lot_unit_line=kg)
lot.get_hist_quantity.return_value = (
Decimal('999995'),
Decimal('999995'),
)
line = Mock(type='line', quantity=Decimal('999995'), lot=lot, unit=kg)
invoice = Invoice()
invoice.lines = [line]
uom_model = Mock()
uom_model.search.return_value = [lbs]
uom_model.compute_qty.side_effect = (
lambda from_unit, qty, to_unit: qty * 2.20462
)
with patch('trytond.modules.purchase_trade.invoice.Pool') as PoolMock:
PoolMock.return_value.get.return_value = uom_model
self.assertEqual(invoice.report_lbs, Decimal('2204608.98'))
self.assertEqual(
invoice.report_quantity_lines,
'999995.0 KILOGRAM (2204608.98 LBS)')
def test_invoice_report_weights_keep_line_sign_with_lot_hist_values(self):
'invoice lot hist values keep the invoice line sign for final notes'