From 9c90e5ac07ae44a9e3a587d079e2ad9dddece991 Mon Sep 17 00:00:00 2001 From: nobohan Date: Fri, 16 Feb 2024 09:48:07 +0100 Subject: [PATCH] Add single python script for translating third party information into SQL --- third_party/README.md | 14 +++ third_party/import_third_party_to_sql.py | 118 +++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 third_party/README.md create mode 100644 third_party/import_third_party_to_sql.py diff --git a/third_party/README.md b/third_party/README.md new file mode 100644 index 0000000..5b1f41e --- /dev/null +++ b/third_party/README.md @@ -0,0 +1,14 @@ +Import Third parties into Chill from the canvas +=============================================== + +This folder contains a single Python script that reads a csv file with Third party and print to the console some SQL to execute on the database. + +The csv file should be simply exported from the sheet "Tiers" from the xls canvas. + +Then, you can run the python script with adapting the following: + +- name of the csv file to open +- change the mapping of third party categories, civilities and third party kinds according to your Chill database + + +So far, the addresses are not treated. \ No newline at end of file diff --git a/third_party/import_third_party_to_sql.py b/third_party/import_third_party_to_sql.py new file mode 100644 index 0000000..d3304ca --- /dev/null +++ b/third_party/import_third_party_to_sql.py @@ -0,0 +1,118 @@ +import csv + +print_to_screen = "INSERT INTO chill_3party.third_party (id, civility_id, name, firstname, name_company, acronym, kind, parent_id, profession, email, telephone, comment, contact_data_anonymous, active, created_at) VALUES" + + +def get_parent_id(arg): + return int(arg) + 1000 if arg else "NULL" + + +def get_third_party_civility(arg): + if arg == "Madame": + return 1 + elif arg == "Monsieur": + return 2 + elif arg == "Docteur": + return 3 + else: + return "NULL" + + +def get_third_party_kind(arg): + if arg == "Tiers institutionnel": + return "company" + elif arg == "Personne de contact": + return "child" + elif arg == "Personne morale": + return "contact" + else: + return "company" + + +def make_bool_with_default_false(arg): + if arg == "oui": + return True + else: + return False + + +def make_telephone(arg): + if arg: + return f"'+32{arg}'" + else: + return "NULL" + + +with open("HalleDeHan_tiers.csv", newline="") as csv_file: + csv_reader = csv.reader(csv_file) + + for skip in range(7): + next(csv_file) + + for row in csv_reader: + third_party_id = int(row[0]) + 1000 + civility = get_third_party_civility(row[1]) + name = row[2] + firstname = row[3] + name_company = row[4] + acronym = row[5] + kind = get_third_party_kind(row[6]) + parent_id = get_parent_id(row[7]) + # row[8] is the name of the parent in the csv + profession = row[9] + # category = row[10] # category -> see below + email = row[11] + telephone = make_telephone(row[12]) + # street = row[13] # TODO address + # extra = row[14] + # streetnumber = row[15] + # postcode = row[16] + # country = row[17] + # validfrom = row[18] + comment = row[19] + contact_data_anonymous = make_bool_with_default_false(row[20]) + print_to_screen += f"({third_party_id}, {civility}, '{name}', '{firstname}', '{name_company}', '{acronym}', '{kind}', {parent_id}, '{profession}', '{email}', {telephone}, '{comment}', {contact_data_anonymous}, True, NOW())," + +print_to_screen = print_to_screen[:-1] + +print(print_to_screen) + +## categories + +print_to_screen_cat = ( + "INSERT INTO chill_3party.thirdparty_category (thirdparty_id, category_id) VALUES" +) + + +def get_third_party_category(arg): + if arg == "Stage vente": + return 2 + elif arg == "Stage reassort": + return 8 + elif arg == "Stage social / educ": + return 9 + elif arg == "Stage logistique": + return 10 + elif arg == "Stage bureautique": + return 11 + else: + return None + + +with open("HalleDeHan_tiers.csv", newline="") as csv_file: + csv_reader = csv.reader(csv_file) + + for skip in range(7): + next(csv_file) + + for row in csv_reader: + third_party_id = int(row[0]) + 1000 + category_id = get_third_party_category(row[10]) + if category_id: + print_to_screen_cat += f"({third_party_id}, {category_id})," + +print_to_screen_cat = print_to_screen_cat[:-1] + +print(print_to_screen_cat) + +# TODO addresses