Add single python script for translating third party information into SQL

This commit is contained in:
nobohan 2024-02-16 09:48:07 +01:00
parent 543a4569fd
commit 9c90e5ac07
2 changed files with 132 additions and 0 deletions

14
third_party/README.md vendored Normal file
View File

@ -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.

118
third_party/import_third_party_to_sql.py vendored Normal file
View File

@ -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