diff --git a/modules/purchase_trade/tests/test_module.py b/modules/purchase_trade/tests/test_module.py index ca9d190..9f5ff18 100644 --- a/modules/purchase_trade/tests/test_module.py +++ b/modules/purchase_trade/tests/test_module.py @@ -810,6 +810,66 @@ class PurchaseTradeTestCase(ModuleTestCase): with self.assertRaises(UserError): report.validate_remote_weight_report_context(shipment) + def test_weight_report_create_from_json_accepts_missing_weight_date(self): + 'weight report import keeps working when OCR returns null for weight_date' + WeightReport = Pool().get('weight.report') + party_model = Mock() + vessel_model = Mock() + location_model = Mock() + party_model.getPartyByName.side_effect = lambda name: Mock(id={ + 'SELLER': 1, + 'BUYER': 2, + 'CARRIER': 3, + }[name]) + vessel_model.search.return_value = [] + location_model.search.return_value = [] + + payload = { + 'lab': 'LAB', + 'report': { + 'reference': 'REF', + 'file_no': 'FILE', + 'date': '28 October 2025', + }, + 'contract': {}, + 'parties': { + 'seller': 'SELLER', + 'buyer': 'BUYER', + 'carrier': 'CARRIER', + }, + 'shipment': { + 'bl_no': 'BL-1', + 'weighing_place': 'PORT', + 'weighing_method': 'METHOD', + 'bales': 10, + }, + 'weights': { + 'weight_date': None, + 'gross_landed_kg': '10', + 'tare_kg': '1', + 'net_landed_kg': '9', + 'invoice_net_kg': '9', + 'gain_loss_kg': '0', + 'gain_loss_percent': '0', + } + } + + with patch( + 'trytond.modules.purchase_trade.weight_report.Pool') as PoolMock, patch.object( + WeightReport, 'create', return_value=[payload]) as create_mock: + PoolMock.return_value.get.side_effect = lambda name: { + 'party.party': party_model, + 'trade.vessel': vessel_model, + 'stock.location': location_model, + }[name] + + created = WeightReport.create_from_json(payload) + + self.assertEqual(created, payload) + values = create_mock.call_args.args[0][0] + self.assertIsNone(values['weight_date']) + self.assertEqual(values['report_date'].isoformat(), '2025-10-28') + def test_invoice_report_uses_invoice_template_from_configuration(self): 'invoice report path is resolved from purchase_trade configuration' report_class = Pool().get('account.invoice', type='report') diff --git a/modules/purchase_trade/weight_report.py b/modules/purchase_trade/weight_report.py index 2974339..62ccede 100644 --- a/modules/purchase_trade/weight_report.py +++ b/modules/purchase_trade/weight_report.py @@ -206,6 +206,12 @@ class WeightReport(ModelSQL, ModelView): def parse_date(date_str): logger.info("TRY_TO_PARSE:%s",date_str) + if not date_str: + return None + if isinstance(date_str, datetime.date): + return date_str + if not isinstance(date_str, str): + date_str = str(date_str) for fmt in ('%d %B %Y', '%d %b %Y'): try: return dt.strptime(date_str, fmt).date()