fix: keep organization identity types exclusive
This commit is contained in:
@@ -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 ""
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user