From 76a99a4a1eb5b7386a390ef29a479dbfcbe37603 Mon Sep 17 00:00:00 2001 From: Aleksandr Meshchriakov Date: Wed, 20 May 2026 12:45:06 +0200 Subject: [PATCH] fix: keep organization identity types exclusive --- src/organizations/source_identity.py | 10 ++-- src/organizations/source_ingestion.py | 10 ++-- .../organizations/test_source_ingestion.py | 46 +++++++++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/organizations/source_identity.py b/src/organizations/source_identity.py index c5d7a81..e9439a9 100644 --- a/src/organizations/source_identity.py +++ b/src/organizations/source_identity.py @@ -27,24 +27,26 @@ def normalize_identity_fields( kpp_digits = digits(kpp) ogrn_digits = digits(ogrn) ogrip_digits = digits(ogrip) + has_legal_entity_inn = len(inn_digits) == 10 + has_entrepreneur_inn = len(inn_digits) == 12 normalized_ogrip = "" - if len(ogrip_digits) == VALID_OGRIP_LENGTH: + if len(ogrip_digits) == VALID_OGRIP_LENGTH and not has_legal_entity_inn: normalized_ogrip = ogrip_digits - elif len(ogrn_digits) == VALID_OGRIP_LENGTH: + elif len(ogrn_digits) == VALID_OGRIP_LENGTH and not has_legal_entity_inn: normalized_ogrip = ogrn_digits normalized_inn = inn_digits if len(inn_digits) in VALID_INN_LENGTHS else "" normalized_kpp = ( "" - if normalized_ogrip + if normalized_ogrip or has_entrepreneur_inn else kpp_digits if len(kpp_digits) == VALID_KPP_LENGTH else "" ) normalized_ogrn = ( "" - if normalized_ogrip + if normalized_ogrip or has_entrepreneur_inn else ogrn_digits if len(ogrn_digits) == VALID_OGRN_LENGTH else "" diff --git a/src/organizations/source_ingestion.py b/src/organizations/source_ingestion.py index 5920723..0143599 100644 --- a/src/organizations/source_ingestion.py +++ b/src/organizations/source_ingestion.py @@ -519,13 +519,17 @@ class OrganizationSourceIngestionService: if not organization.inn and safe_inn == record.inn: organization.inn = record.inn changed = True - if not organization.kpp and record.kpp: + has_entrepreneur_identity = bool(organization.ogrip or record.ogrip) + has_legal_identity = bool( + organization.kpp or organization.ogrn or record.kpp or record.ogrn + ) + if not organization.kpp and record.kpp and not has_entrepreneur_identity: organization.kpp = record.kpp changed = True - if not organization.ogrn and record.ogrn: + if not organization.ogrn and record.ogrn and not has_entrepreneur_identity: organization.ogrn = record.ogrn changed = True - if not organization.ogrip and record.ogrip: + if not organization.ogrip and record.ogrip and not has_legal_identity: organization.ogrip = record.ogrip changed = True if cls._should_replace_placeholder_name(organization, record.organization_name): diff --git a/tests/apps/organizations/test_source_ingestion.py b/tests/apps/organizations/test_source_ingestion.py index 2778e29..2e1631c 100644 --- a/tests/apps/organizations/test_source_ingestion.py +++ b/tests/apps/organizations/test_source_ingestion.py @@ -154,6 +154,52 @@ class OrganizationSourceIngestionServiceTest(TestCase): record = OrganizationSourceRecord.objects.get(external_id="fstec-5067") self.assertEqual(record.extension.organization, partial) + def test_save_records_ignores_ogrip_like_ogrn_for_legal_entity_inn(self): + result = OrganizationSourceIngestionService.save_records( + source=ParserLoadLog.Source.INDUSTRIAL, + load_batch=47, + records=[ + SourceRecordInput( + external_id="cert-legal-entity-bad-ogrn", + title="cert-legal-entity-bad-ogrn", + organization_name='ООО "Металл-Завод"', + inn="7720525156", + ogrn="105774446645395", + ) + ], + ) + + self.assertEqual(result.created_records, 1) + organization = Organization.objects.get(inn="7720525156") + self.assertEqual(organization.ogrn, "") + self.assertEqual(organization.ogrip, "") + + def test_save_records_does_not_add_ogrip_to_existing_legal_entity(self): + organization = Organization.objects.create( + name='ООО "Металл-Завод"', + inn="7720525156", + ogrn="1057746645395", + ) + + result = OrganizationSourceIngestionService.save_records( + source=ParserLoadLog.Source.INDUSTRIAL, + load_batch=48, + records=[ + SourceRecordInput( + external_id="cert-legal-entity-existing-bad-ogrn", + title="cert-legal-entity-existing-bad-ogrn", + organization_name='ООО "Металл-Завод"', + inn="7720525156", + ogrn="105774446645395", + ) + ], + ) + + self.assertEqual(result.created_records, 1) + organization.refresh_from_db() + self.assertEqual(organization.ogrn, "1057746645395") + self.assertEqual(organization.ogrip, "") + def test_save_financial_report_writes_financial_lines_without_legacy_report(self): result = OrganizationSourceIngestionService.save_records( source=ParserLoadLog.Source.FNS_REPORTS,