diff --git a/data/etl/teleicare_history/extractor.py b/data/etl/teleicare_history/extractor.py index e8c4ee5f..48fd5a57 100644 --- a/data/etl/teleicare_history/extractor.py +++ b/data/etl/teleicare_history/extractor.py @@ -10,6 +10,8 @@ def match_companies_on_siret_or_vat(): Le matching pourrait aussi être fait sur * Q(social_name__icontains=etab.etab_raison_sociale) * Q(commercial_name__icontains=etab.etab_enseigne) + * Q(email__icontains=etab.etab_courriel) + * Q(phone_number__icontains=etab.etab_telephone) Mais il serait moins précis. """ nb_vat_match = 0 diff --git a/data/models/teleicare_history/sql/company_declaration_table_creation.sql b/data/etl/teleicare_history/sql/company_declaration_table_creation.sql similarity index 100% rename from data/models/teleicare_history/sql/company_declaration_table_creation.sql rename to data/etl/teleicare_history/sql/company_declaration_table_creation.sql diff --git a/data/factories/teleicare_history/__init__.py b/data/factories/teleicare_history/__init__.py new file mode 100644 index 00000000..cec9e9fc --- /dev/null +++ b/data/factories/teleicare_history/__init__.py @@ -0,0 +1,27 @@ +import random +import string + +import factory +import faker +from phonenumber_field.phonenumber import PhoneNumber + +from data.choices import CountryChoices +from data.models.teleicare_history.etablissement import IcaEtablissement +from data.utils.string_utils import make_random_str +from data.factories.company import _make_siret, _make_vat, _make_phone_number + + +class EtablissementFactory(factory.django.DjangoModelFactory): + class Meta: + model = IcaEtablissement + + etab_ident = factory.Sequence(lambda n: n + 1) + etab_raison_sociale = factory.Faker("company", locale="FR") + etab_enseigne = factory.Faker("company", locale="FR") + etab_siret = factory.LazyFunction(_make_siret) + etab_numero_tva_intra = factory.LazyFunction(_make_siret) + pays_ident = factory.Faker("pyint", min_value=0, max_value=200) + etab_nb_compte_autorise = factory.Faker("pyint", min_value=0, max_value=5) + # contact + etab_telephone = factory.LazyFunction(_make_phone_number) + etab_courriel = factory.Faker("email", locale="FR") diff --git a/data/models/teleicare_history/etablissement.py b/data/models/teleicare_history/etablissement.py index e19c8659..676301bd 100644 --- a/data/models/teleicare_history/etablissement.py +++ b/data/models/teleicare_history/etablissement.py @@ -13,20 +13,20 @@ class IcaEtablissement(models.Model): cog_ident = models.IntegerField(blank=True, null=True) etab_ident_parent = models.IntegerField(blank=True, null=True) pays_ident = models.IntegerField() - etab_siret = models.TextField(blank=True, null=True) - etab_numero_tva_intra = models.TextField(blank=True, null=True) + etab_siret = models.CharField(blank=True, null=True) + etab_numero_tva_intra = models.CharField(blank=True, null=True) etab_raison_sociale = models.TextField() etab_enseigne = models.TextField(blank=True, null=True) - etab_adre_ville = models.TextField(blank=True, null=True) - etab_adre_cp = models.TextField(blank=True, null=True) - etab_adre_voie = models.TextField(blank=True, null=True) - etab_adre_comp = models.TextField(blank=True, null=True) - etab_adre_comp2 = models.TextField(blank=True, null=True) - etab_adre_dist = models.TextField(blank=True, null=True) - etab_telephone = models.TextField(blank=True, null=True) - etab_fax = models.TextField(blank=True, null=True) - etab_courriel = models.TextField(blank=True, null=True) - etab_site_internet = models.TextField(blank=True, null=True) + etab_adre_ville = models.CharField(blank=True, null=True) + etab_adre_cp = models.CharField(blank=True, null=True) + etab_adre_voie = models.CharField(blank=True, null=True) + etab_adre_comp = models.CharField(blank=True, null=True) + etab_adre_comp2 = models.CharField(blank=True, null=True) + etab_adre_dist = models.CharField(blank=True, null=True) + etab_telephone = models.CharField(blank=True, null=True) + etab_fax = models.CharField(blank=True, null=True) + etab_courriel = models.CharField(blank=True, null=True) + etab_site_internet = models.CharField(blank=True, null=True) etab_ica_faconnier = models.BooleanField(blank=True, null=True) etab_ica_fabricant = models.BooleanField(blank=True, null=True) etab_ica_conseil = models.BooleanField(blank=True, null=True) diff --git a/data/tests/test_teleicare_history_importer.py b/data/tests/test_teleicare_history_importer.py new file mode 100644 index 00000000..911f2732 --- /dev/null +++ b/data/tests/test_teleicare_history_importer.py @@ -0,0 +1,65 @@ +from django.db import connection +from django.test import TestCase + +from data.etl.teleicare_history.extractor import match_companies_on_siret_or_vat +from data.factories.company import CompanyFactory, _make_siret, _make_vat +from data.factories.teleicare_history import EtablissementFactory +from data.models.teleicare_history.etablissement import IcaEtablissement + + +class TeleicareHistoryImporterTestCase(TestCase): + """ + Cette classe vise à tester les fonction liées à l'import d'historique de la plateforme TeleIcare + """ + + def setUp(self): + """ + Adapted from: https://stackoverflow.com/a/49800437 + """ + super().setUp() + with connection.schema_editor() as schema_editor: + schema_editor.create_model(IcaEtablissement) + + if IcaEtablissement._meta.db_table not in connection.introspection.table_names(): + raise ValueError( + "Table `{table_name}` is missing in test database.".format( + table_name=IcaEtablissement._meta.db_table + ) + ) + + def tearDown(self): + super().tearDown() + + with connection.schema_editor() as schema_editor: + schema_editor.delete_model(IcaEtablissement) + + def test_match_companies_on_siret_or_vat(self): + """ + Une entreprise enregistrée dans Teleicare ayant le même SIRET ou n° TVA intracom + qu'une entreprise enregistrée dans Compl'Alim doit être liée par le siccrf_id + """ + # create_model_table(IcaEtablissement) + siret = _make_siret() + etablissement_with_siret = EtablissementFactory(etab_siret=siret) + company_with_siret = CompanyFactory(siret=siret) + + vat = _make_vat() + etablissement_with_vat = EtablissementFactory(etab_siret=None, etab_numero_tva_intra=vat) + company_with_vat = CompanyFactory(vat=vat) + + random_company = CompanyFactory() + random_etablissement = EtablissementFactory() + + match_companies_on_siret_or_vat() + company_with_siret.refresh_from_db() + etablissement_with_siret.refresh_from_db() + company_with_vat.refresh_from_db() + etablissement_with_vat.refresh_from_db() + random_company.refresh_from_db() + random_etablissement.refresh_from_db() + + # self.assertEqual(company_with_siret.siccrf_id, etablissement_with_siret.etab_ident) + self.assertEqual(company_with_vat.siccrf_id, etablissement_with_vat.etab_ident) + self.assertNotEqual(random_company.siccrf_id, random_etablissement.etab_ident) + self.assertEqual(random_company.siccrf_id, None) + # delete_model_table(IcaEtablissement)