import csv import json import os from datetime import datetime from class_invoices import * if __name__ == '__main__': print("Script conversion JSON to Winbiz csv, by Resk-U") print("Lecture des fichiers .json dans le dossier ./File_input") Elements = [] export_csv = ""; con_csv = ""; mode = 2; def parseFile(data,filename): lines = [] for ele in data["invoices"]: data = cls_Invoice() data.parse_item(ele) for article in data.Articles: csv_col = cls_Col(True) ##Donnée globales csv_col.data[0] = data.data["id"] # N° document csv_col.data[129] = data.Intervention["type"] # Priorité csv_col.data[1] = 20 # Type of document 20 = facture débiteur csv_col.data[2] = datetime.strptime(data.data["date"], "%Y-%m-%d").strftime("%d.%m.%Y") # Date du document csv_col.data[4] = data.Patient["fip_number"] # Référence csv_col.data[10] = "" # Compte collectif du tiers = if data.Patient["gender"] == "Féminin": csv_col.data[12] = "F" # Langue du document, utilisé comme sexe elif data.Patient["gender"] == "Masculin": csv_col.data[12] = "M" # Langue du document, utilisé comme sexe csv_col.data[19] = data.Debtor["code"] # Code adresse à récupérer dans recherche d'adresse automatisée ## Donnée Patients csv_col.data[22] = data.Patient["lastname"] # Nom de famille du patient csv_col.data[23] = data.Patient["firstname"] # Prénom du patient csv_col.data[24] = data.Patient["street"] # Rue du patient csv_col.data[25] = data.Patient["complement"] # Complément rue csv_col.data[26] = data.Patient["postal_code"] # NPA du patient csv_col.data[27] = data.Patient["city"] # Localité du patient csv_col.data[107] = data.Patient["birthdate"] # Date de naissance du patient csv_col.data[108] = "" # Profession csv_col.data[109] = data.Patient["insurance_name"] # Assurance csv_col.data[110] = "Filiation" # Filiation csv_col.data[112] = data.Intervention["site_street"] # Rue PEC csv_col.data[113] = data.Intervention["site_street_number"] # Rue PEC csv_col.data[114] = data.Intervention["site_postal_code"] + " " + data.Intervention["site_city"] # Rue PEC if "destination_name" not in data.Intervention.keys(): csv_col.data[115] = "Sans transport" else: csv_col.data[115] = data.Intervention["destination_name"] csv_col.data[111] = datetime.strptime(data.Intervention["start_time"], "%Y-%m-%dT%H:%M:%S%z").strftime( "%d.%m.%Y") # Date PEC csv_col.data[118] = datetime.strptime(data.Intervention["start_time"], "%Y-%m-%dT%H:%M:%S%z").strftime( "%H:%M") # Heure PEC csv_col.data[119] = datetime.strptime(data.Intervention["end_time"], "%Y-%m-%dT%H:%M:%S%z").strftime( "%H:%M") # Heure PEC # Donnée débiteur csv_col.data[120] = data.Debtor["lastname"] # Débiteur nom csv_col.data[121] = data.Debtor["firstname"] # Débiteur prénom if data.Debtor["street"] is not None: csv_col.data[122] = data.Debtor["street"] + " " + data.Debtor["street_number"] # Débiteur nom csv_col.data[127] = data.Debtor["street"] + " " + data.Debtor["street_number"] # Débiteur nom csv_col.data[123] = data.Debtor["postal_code"] + " " + data.Debtor["city"] # Débiteur csv_col.data[128] = data.Debtor["postal_code"] + " " + data.Debtor["city"] # Débiteur csv_col.data[124] = "11111111" # Débiteur nom if data.Debtor["gender"] == "Féminin": csv_col.data[125] = "Madame" # Langue du document, utilisé comme sexe elif data.Debtor["gender"] == "Masculin": csv_col.data[125] = "Monsieur" # Langue du document, utilisé comme sexe else: csv_col.data[125] = "" # Langue du document, utilisé comme sexe if data.Debtor["type"] == "Personne": csv_col.data[126] = data.Debtor["firstname"] + " " + data.Debtor["lastname"] # Débiteur nom elif data.Debtor["type"] == "Établissement": temp_data = [] temp_data.append(data.Debtor["name"]) temp_data.append(data.Debtor["firstname"]) temp_data.append(data.Debtor["lastname"]) csv_col.data[126] = ' '.join(filter(None,temp_data)) # Débiteur nom ## Données prestations if "code" in article.keys(): csv_col.data[49] = article["code"] # Code de prestations csv_col.data[50] = article["line_1"] if article["line_2"] != None: csv_col.data[50] = article["line_1"] + " / " + article["line_2"] # Descriptions prestations csv_col.data[52] = article["quantity"] # Quantité csv_col.data[53] = article["unit_price"] # prix unitaire csv_col.data[54] = article["unit"] # unité csv_col.data[56] = article["price"] # prix total csv_col.data[59] = "" # Compte chiffre d'affaire = else: csv_col.data[49] = "txt" csv_col.data[50] = article["line_1"] if article["line_2"] != None: csv_col.data[50] = article["line_1"] + " / " + article["line_2"] # Descriptions prestations csv_col.data[52] = 0 csv_col.data[53] = 0 csv_col.data[56] = 0 # prix total csv_col.data[59] = "" # Compte chiffre d'affaire = lines.append(csv_col.data) csv_col = None with open("./File_input/bizexdoc_"+filename.replace(".json","")+".csv","w", newline='') as csv_file: wr = csv.writer(csv_file, delimiter=';') for cdr in lines: wr.writerow(cdr) def convertFile(data,filename): lines = [] for ele in data["invoices"]: data = cls_Invoice() data.parse_item(ele) for article in data.Articles: csv_col = cls_Col(False) ##Donnée globales csv_col.data.append(data.data["id"]) # Doc ID csv_col.data.append(data.data["date"]) # Date du document csv_col.data.append(data.data["total_price"]) # Prix Total csv_col.data.append(data.data["comments"]) # comments factures whitout_transport = False for elem in data.Intervention: if elem == "without_transportation": whitout_transport = True break csv_col.data.append(data.Intervention[elem]) if whitout_transport: for x in range(8): csv_col.data.append("") csv_col.data.append(whitout_transport) for elem in data.Patient: csv_col.data.append(data.Patient[elem]) for elem in data.Debtor: if elem != "name": csv_col.data.append(data.Debtor[elem]) if "name" in data.Debtor.keys(): csv_col.data.append(data.Debtor["name"]) else: csv_col.data.append("") #csv_col.data[126] = ' '.join(filter(None,temp_data)) # Débiteur nom ## Données prestations if "code" in article.keys(): csv_col.data.append(article["code"]) else: csv_col.data.append("") csv_col.data.append(article["line_1"]) csv_col.data.append(article["line_2"]) if "quantity" in article.keys(): csv_col.data.append(article["quantity"]) else: csv_col.data.append("") if "unit_price" in article.keys(): csv_col.data.append(article["unit_price"]) else: csv_col.data.append("") if "unit" in article.keys(): csv_col.data.append(article["unit"]) else: csv_col.data.append("") if "price" in article.keys(): csv_col.data.append(article["price"]) else: csv_col.data.append("") lines.append(csv_col.data) csv_col = None with open("./File_input/bizexdoc_"+filename.replace(".json","")+".csv","w", newline='') as csv_file: wr = csv.writer(csv_file, delimiter=';') for cdr in lines: wr.writerow(cdr) dir = "./File_input" for filename in os.listdir(dir): if ".json" in filename: print(filename) with open(dir + "/" + filename, encoding="utf-8") as f: data = json.load(f) if mode == 1: parseFile(data,filename) elif mode == 2: convertFile(data,filename) #os.remove(dir+"/"+filename)