1064 lines
41 KiB
Python
1064 lines
41 KiB
Python
"""
|
|
Ce fichier permet de gerer tout qui est lié aux client d'un partenaire.
|
|
|
|
Cas d'utilisation :
|
|
Un partenaire de mysy, souhaite créer (CRUD) ses clients
|
|
Emettre des devis, bons de commandes, des factures, etc....
|
|
"""
|
|
|
|
import pymongo
|
|
from pymongo import MongoClient
|
|
import json
|
|
from bson import ObjectId
|
|
import re
|
|
from datetime import datetime
|
|
import prj_common as mycommon
|
|
import secrets
|
|
import inspect
|
|
import sys, os
|
|
import csv
|
|
import pandas as pd
|
|
from pymongo import ReturnDocument
|
|
import GlobalVariable as MYSY_GV
|
|
from math import isnan
|
|
import GlobalVariable as MYSY_GV
|
|
import ela_index_bdd_classes as eibdd
|
|
import email_mgt as email
|
|
import ast
|
|
|
|
def Add_Partner_Client(diction):
|
|
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', "raison_sociale", "nom", "siret", "tva", "email",
|
|
"telephone", "website", "comment", "address", "invoice_address", "list_contact",
|
|
'adr_adresse', 'adr_code_postal', 'adr_ville', 'adr_pays',
|
|
'invoice_email', 'invoice_nom', 'invoice_siret',
|
|
'invoice_tva', 'invoice_adresse', 'invoice_ville',
|
|
'invoice_code_postal', 'invoice_pays'
|
|
]
|
|
|
|
print(" diction = ", diction)
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', "raison_sociale", "nom", "email", "telephone", ]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
# Verifier la validité du token
|
|
retval = mycommon.check_partner_token_validity("", token)
|
|
|
|
if retval is False:
|
|
return "Err_Connexion", " La session de connexion n'est pas valide"
|
|
|
|
partner_recid = mycommon.get_parnter_recid_from_token(token)
|
|
if (partner_recid is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - partner_recid est KO. Les données de connexion sont incorrectes ")
|
|
return False, " Vous n'etes pas autorisé à utiliser cette API "
|
|
|
|
# Recuperation des données du partenaire
|
|
local_status, my_partner = mycommon.get_partner_data_from_recid(partner_recid)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire")
|
|
return False, str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire. "
|
|
|
|
|
|
"""
|
|
Verification s'il n'existe pas un cient du partenaire qui perte le meme
|
|
nom, ou la meme adresse email
|
|
"""
|
|
|
|
qry = {'nom': str(diction['nom']), 'valide': '1', 'partner_recid': str(my_partner['recid'])}
|
|
|
|
tmp_count = MYSY_GV.dbname['partner_client'].count_documents(qry)
|
|
if (tmp_count > 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - Il existe déjà un client qui porte le même nom = "+str(diction['nom']))
|
|
|
|
return False, " - Vous déjà un client qui porte le même nom "
|
|
|
|
tmp_count = MYSY_GV.dbname['partner_client'].count_documents({'email': str(diction['email']),
|
|
'valide': '1', 'partner_recid': my_partner['recid']})
|
|
if (tmp_count > 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Il existe déjà un client qui porte le même email principal = " +str(diction['email']))
|
|
|
|
return False, " - Vous avez déjà un client qui a le même email principal "
|
|
|
|
|
|
|
|
"""
|
|
Recuperation des données fournies en entrée
|
|
"""
|
|
data = {}
|
|
data['partner_recid'] = my_partner['recid']
|
|
|
|
raison_sociale = ""
|
|
if ("raison_sociale" in diction.keys()):
|
|
if diction['raison_sociale']:
|
|
raison_sociale = diction['raison_sociale']
|
|
data['raison_sociale'] = diction['raison_sociale']
|
|
|
|
nom = ""
|
|
if ("nom" in diction.keys()):
|
|
if diction['nom']:
|
|
nom = diction['nom']
|
|
data['nom'] = diction['nom']
|
|
|
|
siret = ""
|
|
if ("siret" in diction.keys()):
|
|
if diction['siret']:
|
|
siret = diction['siret']
|
|
data['siret'] = diction['siret']
|
|
|
|
adr_adresse = ""
|
|
if ("adr_adresse" in diction.keys()):
|
|
if diction['adr_adresse']:
|
|
adr_adresse = diction['adr_adresse']
|
|
data['adr_adresse'] = diction['adr_adresse']
|
|
|
|
adr_code_postal = ""
|
|
if ("adr_code_postal" in diction.keys()):
|
|
if diction['adr_code_postal']:
|
|
adr_code_postal = diction['adr_code_postal']
|
|
data['adr_code_postal'] = diction['adr_code_postal']
|
|
|
|
adr_ville = ""
|
|
if ("adr_ville" in diction.keys()):
|
|
if diction['adr_ville']:
|
|
adr_ville = diction['adr_ville']
|
|
data['adr_ville'] = diction['adr_ville']
|
|
|
|
adr_pays = ""
|
|
if ("adr_pays" in diction.keys()):
|
|
if diction['adr_pays']:
|
|
adr_pays = diction['siret']
|
|
data['adr_pays'] = diction['adr_pays']
|
|
|
|
tva = ""
|
|
if ("tva" in diction.keys()):
|
|
if diction['tva']:
|
|
tva = diction['tva']
|
|
data['tva'] = diction['tva']
|
|
|
|
email = ""
|
|
if ("email" in diction.keys()):
|
|
if diction['email']:
|
|
email = diction['email']
|
|
data['email'] = diction['email']
|
|
if( mycommon.isEmailValide(email) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - L'adresse email "+str(email)+" n'est pas valide")
|
|
|
|
return False, " - L'adresse email "+str(email)+" n'est pas valide "
|
|
|
|
|
|
telephone = ""
|
|
if ("telephone" in diction.keys()):
|
|
if diction['telephone']:
|
|
telephone = diction['telephone']
|
|
data['telephone'] = diction['telephone']
|
|
|
|
|
|
website = ""
|
|
if ("website" in diction.keys()):
|
|
if diction['website']:
|
|
website = diction['website']
|
|
data['website'] = diction['website']
|
|
|
|
comment = ""
|
|
if ("comment" in diction.keys()):
|
|
if diction['comment']:
|
|
comment = diction['comment']
|
|
data['comment'] = diction['comment']
|
|
|
|
invoice_email = ""
|
|
if ("invoice_email" in diction.keys()):
|
|
if diction['invoice_email']:
|
|
invoice_email = diction['invoice_email']
|
|
data['invoice_email'] = diction['invoice_email']
|
|
if (mycommon.isEmailValide(invoice_email) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - L'adresse email de facturation '" + str(invoice_email) + "' n'est pas valide")
|
|
|
|
return False, " - L'adresse email de facturation '" + str(invoice_email) + "' n'est pas valide "
|
|
|
|
invoice_nom = ""
|
|
if ("invoice_nom" in diction.keys()):
|
|
if diction['invoice_nom']:
|
|
invoice_nom = diction['invoice_nom']
|
|
data['invoice_nom'] = diction['invoice_nom']
|
|
|
|
invoice_siret = ""
|
|
if ("invoice_siret" in diction.keys()):
|
|
if diction['invoice_siret']:
|
|
invoice_siret = diction['invoice_siret']
|
|
data['invoice_siret'] = diction['invoice_siret']
|
|
|
|
invoice_tva = ""
|
|
if ("invoice_tva" in diction.keys()):
|
|
if diction['invoice_tva']:
|
|
invoice_tva = diction['invoice_tva']
|
|
data['invoice_tva'] = diction['invoice_tva']
|
|
|
|
invoice_adresse = ""
|
|
if ("invoice_adresse" in diction.keys()):
|
|
if diction['invoice_adresse']:
|
|
invoice_adresse = diction['invoice_adresse']
|
|
data['invoice_adresse'] = diction['invoice_adresse']
|
|
|
|
invoice_ville = ""
|
|
if ("invoice_ville" in diction.keys()):
|
|
if diction['invoice_ville']:
|
|
invoice_ville = diction['invoice_ville']
|
|
data['invoice_ville'] = diction['invoice_ville']
|
|
|
|
invoice_code_postal = ""
|
|
if ("invoice_code_postal" in diction.keys()):
|
|
if diction['invoice_code_postal']:
|
|
invoice_code_postal = diction['invoice_code_postal']
|
|
data['invoice_code_postal'] = diction['invoice_code_postal']
|
|
|
|
invoice_pays = ""
|
|
if ("invoice_pays" in diction.keys()):
|
|
if diction['invoice_pays']:
|
|
invoice_pays = diction['invoice_pays']
|
|
data['invoice_pays'] = diction['invoice_pays']
|
|
|
|
list_adress = []
|
|
line = 0
|
|
if ("address" in diction.keys()):
|
|
if diction['address']:
|
|
list_adress = ast.literal_eval(diction['address'])
|
|
data['address'] = list_adress
|
|
for adress_val in list_adress :
|
|
print(" ### adress_val = ", adress_val)
|
|
address = adress_val
|
|
data['address'][line]['recid'] = mycommon.create_user_recid()
|
|
line = line + 1
|
|
|
|
"""
|
|
/!\ : l'adresse etant directement enregistrée sur le client, alors verification que le ligne "adresse" contient bien les champs :
|
|
- adresse, code postal, ville, pays
|
|
"""
|
|
|
|
adresse_field_list_obligatoire = ['adresse', "code_postal", "ville", "pays", ]
|
|
for val in adresse_field_list_obligatoire:
|
|
if val not in address.keys():
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - Le champ '" + val + "' est obligatoire dans l'adresse")
|
|
return False, " Le champ '" + val + "' est obligatoire dans l'adresse",
|
|
|
|
|
|
|
|
invoice_address = {}
|
|
if ("invoice_address" in diction.keys()):
|
|
if diction['invoice_address']:
|
|
invoice_address = ast.literal_eval(diction['invoice_address'])
|
|
data['invoice_address'] = ast.literal_eval(diction['invoice_address'])
|
|
|
|
""""
|
|
verification que le ligne "adresse de facturation" contient bien les champs :
|
|
- adresse, code postal, ville, pays
|
|
"""
|
|
|
|
adresse_field_list_obligatoire = ['adresse', "code_postal", "ville", "pays", ]
|
|
for val in adresse_field_list_obligatoire:
|
|
if val not in invoice_address.keys():
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - Le champ '" + val + "' est obligatoire dans l'adresse de facturation")
|
|
return False, " Le champ '" + val + "' est obligatoire dans l'adresse de facturation",
|
|
|
|
list_contact = ""
|
|
if ("list_contact" in diction.keys()):
|
|
if diction['list_contact']:
|
|
list_contact = diction['list_contact']
|
|
data['list_contact'] = diction['list_contact']
|
|
|
|
data['valide'] = '1'
|
|
data['locked'] = '0'
|
|
data['date_update'] = str(datetime.now())
|
|
|
|
# Creation du RecId
|
|
data['recid'] = mycommon.create_user_recid()
|
|
|
|
|
|
inserted_id = ""
|
|
inserted_id = MYSY_GV.dbname['partner_client'].insert_one(data).inserted_id
|
|
if (not inserted_id):
|
|
mycommon.myprint(
|
|
" Impossible de créer le client du partner ")
|
|
return False, " Impossible de créer le client "
|
|
|
|
|
|
return True, " Le client a été correctement créé"
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible d'ajouter le client "
|
|
|
|
|
|
|
|
"""
|
|
Mise à jour contact d'un client de partner
|
|
"""
|
|
def Update_Partner_Client_Contact(diction):
|
|
try:
|
|
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', "partner_client_contact_recid", "adresse", "ville", "code_postal", "pays", "email", "telephone" ]
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
# Verifier la validité du token
|
|
retval = mycommon.check_partner_token_validity("", token)
|
|
|
|
if retval is False:
|
|
return "Err_Connexion", " La session de connexion n'est pas valide"
|
|
|
|
partner_recid = mycommon.get_parnter_recid_from_token(token)
|
|
if (partner_recid is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - partner_recid est KO. Les données de connexion sont incorrectes ")
|
|
return False, " Vous n'etes pas autorisé à utiliser cette API "
|
|
|
|
# Recuperation des données du partenaire
|
|
local_status, my_partner = mycommon.get_partner_data_from_recid(partner_recid)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire")
|
|
return False, str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire. "
|
|
|
|
data_update = {}
|
|
|
|
partner_client_contact_recid = ""
|
|
if ("partner_client_contact_recid" in diction.keys()):
|
|
partner_client_contact_recid = diction['partner_client_contact_recid']
|
|
|
|
adresse = ""
|
|
if ("adresse" in diction.keys()):
|
|
adresse = diction['adresse']
|
|
data_update['partner_recid'] = my_partner['recid']
|
|
data_update["address.$[elem].adresse"] = adresse
|
|
|
|
ville = ""
|
|
if ("ville" in diction.keys()):
|
|
ville = diction['ville']
|
|
data_update["address.$[elem].ville"] = ville
|
|
|
|
code_postal = ""
|
|
if ("code_postal" in diction.keys()):
|
|
code_postal = diction['code_postal']
|
|
data_update["address.$[elem].code_postal"] = code_postal
|
|
|
|
pays = ""
|
|
if ("pays" in diction.keys()):
|
|
pays = diction['pays']
|
|
data_update["address.$[elem].pays"] = pays
|
|
|
|
email = ""
|
|
if ("email" in diction.keys()):
|
|
email = diction['email']
|
|
data_update["address.$[elem].email"] = email
|
|
if (mycommon.isEmailValide(email) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - L'adresse email " + str(email) + " n'est pas valide")
|
|
|
|
return False, " -L'adresse email " + str(email) + " n'est pas valide "
|
|
|
|
telephone = ""
|
|
if ("telephone" in diction.keys()):
|
|
telephone = diction['telephone']
|
|
data_update["address.$[elem].telephone"] = telephone
|
|
|
|
|
|
print(" #### data_update = ", data_update)
|
|
print(" #### partner_recid = ", str(partner_recid))
|
|
print(" #### elem.recid = ", str(partner_client_contact_recid))
|
|
|
|
result = MYSY_GV.dbname['partner_client'].update_one(
|
|
{'partner_recid':str(partner_recid)},
|
|
{ "$set": data_update},
|
|
upsert=True,
|
|
array_filters=[{"elem.recid": str(partner_client_contact_recid) }],
|
|
)
|
|
|
|
|
|
print("raw:", result.raw_result)
|
|
print("acknowledged:", result.acknowledged)
|
|
print("matched_count:", result.matched_count)
|
|
|
|
|
|
return True, " Le contact du client a été correctement mis à jour"
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible d'ajouter le client "
|
|
|
|
"""
|
|
Mise à jour d'un client.
|
|
La clé de mise à jour est : l'email et ou le nom
|
|
|
|
Donc pas de modification possible du mail et du nom
|
|
"""
|
|
def Update_Partner_Client(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', "raison_sociale", "nom", "siret", "tva", "email",
|
|
"telephone", "website", "comment", "address", "invoice_address", "list_contact",
|
|
'adr_adresse', 'adr_code_postal', 'adr_ville', 'adr_pays',
|
|
'invoice_email', 'invoice_nom', 'invoice_siret',
|
|
'invoice_tva', 'invoice_adresse', 'invoice_ville',
|
|
'invoice_code_postal', 'invoice_pays']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', "nom", "email", ]
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
# Verifier la validité du token
|
|
retval = mycommon.check_partner_token_validity("", token)
|
|
|
|
if retval is False:
|
|
return "Err_Connexion", " La session de connexion n'est pas valide"
|
|
|
|
partner_recid = mycommon.get_parnter_recid_from_token(token)
|
|
if (partner_recid is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - partner_recid est KO. Les données de connexion sont incorrectes ")
|
|
return False, " Vous n'etes pas autorisé à utiliser cette API "
|
|
|
|
# Recuperation des données du partenaire
|
|
local_status, my_partner = mycommon.get_partner_data_from_recid(partner_recid)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire")
|
|
return False, str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire. "
|
|
|
|
"""
|
|
Verification s'il n'existe pas un cient du partenaire qui perte le meme
|
|
nom, ou la meme adresse email
|
|
"""
|
|
|
|
qry_update = { "$or":[{'nom': str(diction['nom']), 'email': str(diction['email']),
|
|
'valide': '1', 'partner_recid': str(my_partner['recid'])},
|
|
|
|
{'email': str(diction['email']),
|
|
'valide': '1', 'partner_recid': str(my_partner['recid'])}
|
|
]}
|
|
|
|
|
|
print(" ### qry_update = ", qry_update)
|
|
tmp_count = MYSY_GV.dbname['partner_client'].count_documents(qry_update)
|
|
if (tmp_count <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Ce client n'existe pas: nom = "+str(diction['nom']))
|
|
|
|
return False, " - Ce client n'existe pas, mise à jour impossible "
|
|
|
|
|
|
|
|
"""
|
|
Recuperation des données fournies en entrée
|
|
"""
|
|
|
|
data_update = {}
|
|
"""
|
|
Recuperation des données fournies en entrée
|
|
"""
|
|
|
|
raison_sociale = ""
|
|
if ("raison_sociale" in diction.keys()):
|
|
raison_sociale = diction['raison_sociale']
|
|
data_update['raison_sociale'] = diction['raison_sociale']
|
|
|
|
nom = ""
|
|
if ("nom" in diction.keys()):
|
|
nom = diction['nom']
|
|
# data_update['nom'] = diction['nom'] - on ne modifie pas le nom
|
|
|
|
siret = ""
|
|
if ("siret" in diction.keys()):
|
|
siret = diction['siret']
|
|
data_update['siret'] = diction['siret']
|
|
|
|
adr_adresse = ""
|
|
if ("adr_adresse" in diction.keys()):
|
|
adr_adresse = diction['adr_adresse']
|
|
data_update['adr_adresse'] = diction['adr_adresse']
|
|
|
|
adr_code_postal = ""
|
|
if ("adr_code_postal" in diction.keys()):
|
|
adr_code_postal = diction['adr_code_postal']
|
|
data_update['adr_code_postal'] = diction['adr_code_postal']
|
|
|
|
adr_ville = ""
|
|
if ("adr_ville" in diction.keys()):
|
|
adr_ville = diction['adr_ville']
|
|
data_update['adr_ville'] = diction['adr_ville']
|
|
|
|
adr_pays = ""
|
|
if ("adr_pays" in diction.keys()):
|
|
adr_pays = diction['adr_pays']
|
|
data_update['adr_pays'] = diction['adr_pays']
|
|
|
|
|
|
|
|
tva = ""
|
|
if ("tva" in diction.keys()):
|
|
tva = diction['tva']
|
|
data_update['tva'] = diction['tva']
|
|
|
|
telephone = ""
|
|
if ("telephone" in diction.keys()):
|
|
telephone = diction['telephone']
|
|
data_update['telephone'] = diction['telephone']
|
|
|
|
email = ""
|
|
if ("email" in diction.keys()):
|
|
email = diction['email']
|
|
# data_update['email'] = diction['email'] - on ne modifie pas le mail
|
|
|
|
invoice_email = ""
|
|
if ("invoice_email" in diction.keys()):
|
|
invoice_email = diction['invoice_email']
|
|
data_update['invoice_email'] = diction['invoice_email']
|
|
if (mycommon.isEmailValide(invoice_email) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - L'adresse email de facturation " + str(invoice_email) + " n'est pas valide")
|
|
|
|
return False, " - L'adresse email de facturation " + str(invoice_email) + " n'est pas valide "
|
|
|
|
invoice_nom = ""
|
|
if ("invoice_nom" in diction.keys()):
|
|
invoice_nom = diction['invoice_nom']
|
|
data_update['invoice_nom'] = diction['invoice_nom']
|
|
|
|
invoice_siret = ""
|
|
if ("invoice_siret" in diction.keys()):
|
|
invoice_siret = diction['invoice_siret']
|
|
data_update['invoice_siret'] = diction['invoice_siret']
|
|
|
|
invoice_tva = ""
|
|
if ("invoice_tva" in diction.keys()):
|
|
invoice_tva = diction['invoice_tva']
|
|
data_update['invoice_tva'] = diction['invoice_tva']
|
|
|
|
invoice_adresse = ""
|
|
if ("invoice_adresse" in diction.keys()):
|
|
invoice_adresse = diction['invoice_adresse']
|
|
data_update['invoice_adresse'] = diction['invoice_adresse']
|
|
|
|
invoice_ville = ""
|
|
if ("invoice_ville" in diction.keys()):
|
|
invoice_ville = diction['invoice_ville']
|
|
data_update['invoice_ville'] = diction['invoice_ville']
|
|
|
|
invoice_code_postal = ""
|
|
if ("invoice_code_postal" in diction.keys()):
|
|
invoice_code_postal = diction['invoice_code_postal']
|
|
data_update['invoice_code_postal'] = diction['invoice_code_postal']
|
|
|
|
invoice_pays = ""
|
|
if ("invoice_pays" in diction.keys()):
|
|
invoice_pays = diction['invoice_pays']
|
|
data_update['invoice_pays'] = diction['invoice_pays']
|
|
|
|
|
|
website = ""
|
|
if ("website" in diction.keys()):
|
|
website = diction['website']
|
|
data_update['website'] = diction['website']
|
|
|
|
comment = ""
|
|
if ("comment" in diction.keys()):
|
|
comment = diction['comment']
|
|
data_update['comment'] = diction['comment']
|
|
|
|
address = {}
|
|
if ("address" in diction.keys()):
|
|
address = ast.literal_eval(diction['address'])
|
|
data_update['address'] = ast.literal_eval(diction['address'])
|
|
""""
|
|
/!\ : l'adresse etant directement enregistrée sur le client, alors verification que le ligne "adresse" contient bien les champs :
|
|
- adresse, code postal, ville, pays
|
|
"""
|
|
|
|
adresse_field_list_obligatoire = ['adresse', "code_postal", "ville", "pays", ]
|
|
for val in adresse_field_list_obligatoire:
|
|
if val not in address.keys():
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - Le champ '" + val + "' est obligatoire dans l'adresse")
|
|
return False, " Le champ '" + val + "' est obligatoire dans l'adresse",
|
|
|
|
invoice_address = {}
|
|
if ("invoice_address" in diction.keys()):
|
|
invoice_address = ast.literal_eval(diction['invoice_address'])
|
|
data_update['invoice_address'] = ast.literal_eval(diction['invoice_address'])
|
|
|
|
""""
|
|
verification que le ligne "adresse de facturation" contient bien les champs :
|
|
- adresse, code postal, ville, pays
|
|
"""
|
|
|
|
adresse_field_list_obligatoire = ['adresse', "code_postal", "ville", "pays", ]
|
|
for val in adresse_field_list_obligatoire:
|
|
if val not in invoice_address.keys():
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Le champ '" + val + "' est obligatoire dans l'adresse de facturation")
|
|
return False, " Le champ '" + val + "' est obligatoire dans l'adresse de facturation",
|
|
|
|
list_contact = ""
|
|
if ("list_contact" in diction.keys()):
|
|
list_contact = diction['list_contact']
|
|
data_update['list_contact'] = diction['list_contact']
|
|
|
|
data_update['valide'] = '1'
|
|
data_update['locked'] = '0'
|
|
data_update['date_update'] = str(datetime.now())
|
|
|
|
"""
|
|
Clés de mise à jour
|
|
"""
|
|
data_cle = {}
|
|
data_cle['partner_recid'] = str(my_partner['recid'])
|
|
data_cle['email'] = str(diction['email'])
|
|
data_cle['nom'] = str(diction['nom'])
|
|
data_cle['valide'] = "1"
|
|
data_cle['locked'] = "0"
|
|
|
|
inserted_id = ""
|
|
result = MYSY_GV.dbname['partner_client'].find_one_and_update(
|
|
data_cle,
|
|
{"$set": data_update},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
if ("_id" not in result.keys()):
|
|
mycommon.myprint(
|
|
" Impossible de mettre à jour le client : email = " + str(diction['email']))
|
|
return False, " Impossible de mettre à jour le client "
|
|
|
|
return True, " Le client a été correctement mise à jour"
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de mettre à jour le client "
|
|
|
|
|
|
"""
|
|
Recuperation d'une liste de client d'un partenaire
|
|
"""
|
|
|
|
def Get_Partner_List_Partner_Client(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token',]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token',]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
# Verifier la validité du token
|
|
retval = mycommon.check_partner_token_validity("", token)
|
|
|
|
if retval is False:
|
|
return "Err_Connexion", " La session de connexion n'est pas valide"
|
|
|
|
# Recuperation des données du partenaire
|
|
local_status, my_partner = mycommon.get_partner_data_from_token(token)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - impossible de récupérer les données de l'utilisateur ")
|
|
return False, str(inspect.stack()[0][3]) + " - impossible de récupérer les données de l'utilisateur. "
|
|
|
|
"""
|
|
Clés de mise à jour
|
|
"""
|
|
data_cle = {}
|
|
data_cle['partner_recid'] = str(my_partner['recid'])
|
|
data_cle['valide'] = "1"
|
|
data_cle['locked'] = "0"
|
|
|
|
print(" ### Get_Partner_List_Partner_Client data_cle = ", data_cle)
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
for retval in MYSY_GV.dbname['partner_client'].find(data_cle):
|
|
user = retval
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer la liste des clients "
|
|
|
|
|
|
"""
|
|
Recuperation d'un client donnée
|
|
"""
|
|
def Get_Given_Partner_Client(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'email']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token','email' ]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
# Verifier la validité du token
|
|
retval = mycommon.check_partner_token_validity("", token)
|
|
|
|
if retval is False:
|
|
return "Err_Connexion", " La session de connexion n'est pas valide"
|
|
|
|
# Recuperation des données du partenaire
|
|
local_status, my_partner = mycommon.get_partner_data_from_token(token)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - impossible de récupérer les données de l'utilisateur ")
|
|
return False, str(inspect.stack()[0][3]) + " - impossible de récupérer les données de l'utilisateur. "
|
|
|
|
"""
|
|
Clés de mise à jour
|
|
"""
|
|
data_cle = {}
|
|
data_cle['partner_recid'] = str(my_partner['recid'])
|
|
data_cle['email'] = str(diction['email'])
|
|
data_cle['valide'] = "1"
|
|
data_cle['locked'] = "0"
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
print(" ### data_cle = ", data_cle)
|
|
for retval in MYSY_GV.dbname['partner_client'].find(data_cle):
|
|
user = retval
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
#print(" ### RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer le client "
|
|
|
|
|
|
|
|
""""
|
|
Cette fonction permet d'importer des clients de partner en masse avec un fichier excel
|
|
"""
|
|
def Add_Partner_Client_mass(file=None, Folder=None, diction=None):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', ]
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas, Creation participants annulée")
|
|
return False, " Verifier votre API"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['token', ]
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Verifier votre API, Toutes les informations techniques ne sont pas fournies"
|
|
|
|
my_token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
my_token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
status, saved_file = mycommon.Upload_Save_CSV_File(file, Folder)
|
|
if (status is False):
|
|
mycommon.myprint("Impossible de récupérer correctement le fichier à importer")
|
|
return False, "Impossible de récupérer correctement le fichier à importer"
|
|
|
|
|
|
df = pd.read_csv(saved_file, encoding='utf8', on_bad_lines='skip', sep=';', encoding_errors='ignore')
|
|
df = df.fillna('')
|
|
# Dictionnaire des champs utilisables
|
|
'''
|
|
# Verification que les noms des colonne sont bien corrects"
|
|
'''
|
|
field_list = ['nom', 'raison_sociale', 'email', 'adresse', 'code_postal', 'ville', 'pays', 'siret', 'telephone', 'tva', 'website', 'invoice_adresse',
|
|
'invoice_code_postal', 'invoice_email', 'invoice_nom', 'invoice_siret', 'invoice_tva', 'invoice_ville', 'invoice_pays']
|
|
|
|
# Controle du nombre de lignes dans le fichier.
|
|
total_rows = len(df)
|
|
if (total_rows > MYSY_GV.MAX_PARTNER_CLIENT_BY_CSV):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Le fichier comporte plus de " + str(
|
|
MYSY_GV.MAX_PARTNER_CLIENT_BY_CSV) + " lignes.")
|
|
return False, " Le fichier comporte plus de " + str(MYSY_GV.MAX_PARTNER_CLIENT_BY_CSV) + " lignes."
|
|
|
|
for val in df.columns:
|
|
if str(val).lower() not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " : entete du fichier csv. '" + val + "' n'est pas acceptée")
|
|
return False, " Entete du fichier csv. '" + val + "' n'est pas acceptée"
|
|
|
|
# Verification des champs obligatoires dans le fichier
|
|
field_list_obligatoire_file = ['nom', 'email', 'adresse', 'code_postal', 'ville', 'pays', 'telephone', ]
|
|
|
|
for val in field_list_obligatoire_file:
|
|
if val not in df.columns:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " : Le champ '" + val + "' n'est pas présent dans le fichier. Il est obligatoire")
|
|
return False, " Le champ '" + val + "' n'est pas présent dans le fichier. Il est obligatoire "
|
|
|
|
# Traitement des données du fichier
|
|
|
|
warning_msg = ""
|
|
nb_warning_msg = 0
|
|
x = range(0, total_rows)
|
|
for n in x:
|
|
mydata = {}
|
|
mydata['nom'] = str(df['nom'].values[n])
|
|
mydata['email'] = str(df['email'].values[n])
|
|
mydata['adr_adresse'] = str(df['adresse'].values[n])
|
|
mydata['adr_code_postal'] = str(df['code_postal'].values[n])
|
|
mydata['adr_ville'] = str(df['ville'].values[n])
|
|
mydata['adr_pays'] = str(df['pays'].values[n])
|
|
mydata['telephone'] = str(df['telephone'].values[n])
|
|
mydata['token'] = str(my_token)
|
|
|
|
raison_sociale = ""
|
|
if ("raison_sociale" in df.keys()):
|
|
if (str(df['raison_sociale'].values[n])):
|
|
raison_sociale = str(df['raison_sociale'].values[n])
|
|
mydata['raison_sociale'] = raison_sociale
|
|
|
|
siret = ""
|
|
if ("siret" in df.keys()):
|
|
if (str(df['siret'].values[n])):
|
|
siret = str(df['siret'].values[n])
|
|
mydata['siret'] = siret
|
|
|
|
tva = ""
|
|
if ("tva" in df.keys()):
|
|
if (str(df['tva'].values[n])):
|
|
tva = str(df['tva'].values[n])
|
|
mydata['tva'] = tva
|
|
|
|
website = ""
|
|
if ("website" in df.keys()):
|
|
if (str(df['website'].values[n])):
|
|
website = str(df['website'].values[n])
|
|
mydata['website'] = website
|
|
|
|
invoice_adresse = ""
|
|
if ("invoice_adresse" in df.keys()):
|
|
if (str(df['invoice_adresse'].values[n])):
|
|
invoice_adresse = str(df['invoice_adresse'].values[n])
|
|
mydata['invoice_adresse'] = invoice_adresse
|
|
|
|
invoice_code_postal = ""
|
|
if ("invoice_code_postal" in df.keys()):
|
|
if (str(df['invoice_code_postal'].values[n])):
|
|
invoice_code_postal = str(df['invoice_code_postal'].values[n])
|
|
mydata['invoice_code_postal'] = invoice_code_postal
|
|
|
|
invoice_email = ""
|
|
if ("invoice_email" in df.keys()):
|
|
if (str(df['invoice_email'].values[n])):
|
|
invoice_email = str(df['invoice_email'].values[n])
|
|
mydata['invoice_email'] = invoice_email
|
|
|
|
invoice_nom = ""
|
|
if ("invoice_nom" in df.keys()):
|
|
if (str(df['invoice_nom'].values[n])):
|
|
invoice_nom = str(df['invoice_nom'].values[n])
|
|
mydata['invoice_nom'] = invoice_nom
|
|
|
|
invoice_siret = ""
|
|
if ("invoice_siret" in df.keys()):
|
|
if (str(df['invoice_siret'].values[n])):
|
|
invoice_siret = str(df['invoice_siret'].values[n])
|
|
mydata['invoice_siret'] = invoice_siret
|
|
|
|
invoice_tva = ""
|
|
if ("invoice_tva" in df.keys()):
|
|
if (str(df['invoice_tva'].values[n])):
|
|
invoice_tva = str(df['invoice_tva'].values[n])
|
|
mydata['invoice_tva'] = invoice_tva
|
|
|
|
invoice_ville = ""
|
|
if ("invoice_ville" in df.keys()):
|
|
if (str(df['invoice_ville'].values[n])):
|
|
invoice_ville = str(df['invoice_ville'].values[n])
|
|
mydata['invoice_ville'] = invoice_ville
|
|
|
|
invoice_pays = ""
|
|
if ("invoice_pays" in df.keys()):
|
|
if (str(df['invoice_pays'].values[n])):
|
|
invoice_pays = str(df['invoice_pays'].values[n])
|
|
mydata['invoice_pays'] = invoice_pays
|
|
|
|
clean_dict = {k: mydata[k] for k in mydata if (str(mydata[k]) != "nan")}
|
|
|
|
print("#### clean_dict ", clean_dict)
|
|
status, retval = Add_Partner_Client( clean_dict)
|
|
|
|
if (status is False):
|
|
warning_msg = warning_msg + " Line "+str(n+1)+" : "+str(retval)
|
|
nb_warning_msg = nb_warning_msg + 1
|
|
|
|
|
|
if( nb_warning_msg > 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + "Toutes les lignes ont été correctement traités, sauf : "+str(warning_msg))
|
|
return True, "Toutes les lignes ont été correctement traités, sauf : "+str(warning_msg)
|
|
|
|
|
|
return True, " Toutes les lignes ont été correctement traitées"
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible d'importer les client en masse " |