diff --git a/modules/purchase_trade/service.py b/modules/purchase_trade/service.py index ef7cbdc..ef736e1 100644 --- a/modules/purchase_trade/service.py +++ b/modules/purchase_trade/service.py @@ -49,7 +49,7 @@ class ContractFactory: parts = c.currency_unit.split("_") contract.currency = int(parts[0]) or 1 contract.party = c.party - contract.crop = c.crop + contract.crop = cls._get_crop(c, base_contract) contract.tol_min = cls._get_tolerance(c, 'tol_min') contract.tol_max = cls._get_tolerance(c, 'tol_max') contract.payment_term = c.payment_term @@ -197,6 +197,13 @@ class ContractFactory: value = getattr(party, field, None) return value if value is not None else Decimal(0) + @staticmethod + def _get_crop(contract_detail, base_contract): + crop = getattr(contract_detail, 'crop', None) + if crop: + return crop + return getattr(base_contract, 'crop', None) + @staticmethod def _normalize_quantity(quantity): return abs(Decimal(str(quantity or 0))).quantize(Decimal('0.00001')) diff --git a/modules/purchase_trade/tests/test_module.py b/modules/purchase_trade/tests/test_module.py index 6587ecb..ef693d6 100644 --- a/modules/purchase_trade/tests/test_module.py +++ b/modules/purchase_trade/tests/test_module.py @@ -2776,6 +2776,16 @@ class PurchaseTradeTestCase(ModuleTestCase): ContractFactory._get_tolerance(contract_detail, 'tol_max'), Decimal('5')) + def test_contract_factory_uses_base_crop_when_line_is_empty(self): + 'hidden crop falls back to the source contract crop' + crop = Mock() + contract_detail = Mock(crop=None) + base_contract = Mock(crop=crop) + + self.assertEqual( + ContractFactory._get_crop(contract_detail, base_contract), + crop) + def test_contract_factory_delivery_dates_fallback_to_period(self): 'hidden delivery dates are still derived from delivery period' period = Mock(beg_date='2026-05-01', end_date='2026-05-31')