09/08/22 - 14h30

master
ChérifBALDE 2022-08-09 13:42:07 +02:00 committed by cherif
parent 5ffbb1b45a
commit 026fbd6551
3 changed files with 459 additions and 6 deletions

View File

@ -1,5 +1,5 @@
"""
Ce fichier permet de gerer les factures client
Ce fichier permet de gerer les factures client et les methodes de payement
"""
import pymongo
from pymongo import MongoClient
@ -32,7 +32,7 @@ def get_invoice_by_customer(diction):
for val in incom_keys:
if val not in field_list:
mycommon.myprint(str(inspect.stack()[0][
3]) + " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
3]) + " - Le champ '" + val + "' n'existe pas, Creation formation annulée")
return False, " Impossible de recuperer les factures"
'''
@ -47,8 +47,7 @@ def get_invoice_by_customer(diction):
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
return False, " Impossible de recuperer les factures"
# recuperation des paramettre
# recuperation des paramettre
my_token = ""
user_recid = ""
@ -94,5 +93,416 @@ def get_invoice_by_customer(diction):
'''
Cette API recupere les infos de facturation d'un client
'''
Cette API enregistre la methode de payement.
/!\ un client a une seule mode de payement
ceci est enregistré dans la collection : payement_mode
'''
def add_payement_mode(diction):
try:
field_list = ['token', 'pwd', 'secret', 'nom_carte', 'num_carte', 'date_exp_carte', 'cvv_carte', 'nom_compte',
'iban', 'bic','type' ]
incom_keys = diction.keys()
for val in incom_keys:
if val not in field_list:
mycommon.myprint(str(inspect.stack()[0][
3]) + " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
return False, "Impossible d'ajouter le mode de payement"
'''
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', 'pwd', 'secret', 'type']
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, "Impossible d'ajouter le mode de payement"
# recuperation des paramettre
my_token = ""
user_recid = ""
my_pwd = ""
my_secret = ""
if ("pwd" in diction.keys()):
if diction['pwd']:
my_pwd = diction['pwd']
if ("secret" in diction.keys()):
if diction['secret']:
my_secret = diction['secret']
if ("token" in diction.keys()):
if diction['token']:
my_token = diction['token']
# Verification de la validité du pwd et du secret
coll_tmp = MYSY_GV.dbname['partnair_account']
tmp_count = coll_tmp.count_documents({'pwd': str(my_pwd), 'active': '1', 'token':str(my_secret)})
if (tmp_count <= 0):
return False, "Les identifiants sont incorrectes"
user_recid = "None"
# Verification de la validité du token/mail dans le cas des user en mode connecté
if (len(str(my_token)) > 0):
retval = mycommon.check_partner_token_validity("", my_token)
if retval is False:
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide")
return False, "Impossible d'ajouter le mode de payement"
# Recuperation du recid de l'utilisateur
user_recid = mycommon.get_parnter_recid_from_token(my_token)
if user_recid is False:
mycommon.myprint(
str(inspect.stack()[0][3]) + " - Impossible de recuperer le token de l'utilisateur")
return False, "Impossible d'ajouter le mode de payement"
if (len(str(my_token)) <= 0):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide")
return False, "Impossible d'ajouter le mode de payement"
coll_name = MYSY_GV.dbname['payement_mode']
new_data = {}
new_data['valide'] = "1"
new_data['date_update'] = str(datetime.now())
new_data['client_recid'] = user_recid
# initialisation pr nettoyer l'existant (faire comme un delete)
new_data['nom_carte'] = ""
new_data['num_carte'] = ""
new_data['date_exp_carte'] = ""
new_data['cvv_carte'] = ""
new_data['nom_compte'] = ""
new_data['iban'] = ""
new_data['bic'] = ""
if ("nom_carte" in diction.keys()):
if diction['nom_carte']:
new_data['nom_carte'] = diction['nom_carte']
if ("num_carte" in diction.keys()):
if diction['num_carte']:
new_data['num_carte'] = diction['num_carte']
if ("type" in diction.keys()):
if diction['type']:
new_data['type'] = diction['type']
if ("date_exp_carte" in diction.keys()):
if diction['date_exp_carte']:
new_data['date_exp_carte'] = diction['date_exp_carte']
if ("cvv_carte" in diction.keys()):
if diction['cvv_carte']:
new_data['cvv_carte'] = diction['cvv_carte']
if ("nom_compte" in diction.keys()):
if diction['nom_compte']:
new_data['nom_compte'] = diction['nom_compte']
if ("iban" in diction.keys()):
if diction['iban']:
new_data['iban'] = diction['iban']
if ("nom_compte" in diction.keys()):
if diction['nom_compte']:
new_data['nom_compte'] = diction['nom_compte']
if ("bic" in diction.keys()):
if diction['bic']:
new_data['bic'] = diction['bic']
print("mode de payement data :"+str(new_data))
ret_val = coll_name.find_one_and_update(
{'client_recid': user_recid, 'valide': '1'},
{"$set": new_data},
upsert=True,
return_document=ReturnDocument.AFTER
)
if (ret_val['_id'] is False):
return False, "Impossible d'ajouter le mode de payement"
return True, " Le mode de payement a bien ete ajouté"
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
return False, "Impossible d'ajouter le mode de payement"
'''
Cette fonction recuperer le mode de payement par defaut d'un client
'''
def get_payement_mode(diction):
try:
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]) + " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
return False, " Impossible de recuperer les mode de payement"
'''
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, " Impossible de recuperer les mode de payement"
# recuperation des paramettre
my_token = ""
user_recid = ""
if ("token" in diction.keys()):
if diction['token']:
my_token = diction['token']
user_recid = "None"
# Verification de la validité du token/mail dans le cas des user en mode connecté
if (len(str(my_token)) > 0):
retval = mycommon.check_partner_token_validity("", my_token)
if retval is False:
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide")
return False, " Impossible de recuperer les mode de payement"
# Recuperation du recid de l'utilisateur
user_recid = mycommon.get_parnter_recid_from_token(my_token)
if user_recid is False:
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le token de l'utilisateur")
return False, " Impossible de recuperer les mode de payement"
if (len(str(my_token)) <= 0):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide")
return False, " Impossible de recuperer les mode de payement"
RetObject = []
coll_facture = MYSY_GV.dbname['payement_mode']
for retVal in coll_facture.find({'client_recid': user_recid, 'valide': '1'}):
user = retVal
RetObject.append(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) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
return False, " Impossible de recuperer les mode de payement"
"""
Cette fonction crée une facture dans la collecion : factures.
"""
def createOrder(diction):
try:
order_id = ""
nb_line = 0
num_facture = ""
'''field_list = ['token', 'nb_product']
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 formation annulée")
return False, " Impossible de créer la facture"
'''
'''
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', 'nb_product', 'periodicite', 'prix_ttc', 'prix_ht']
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, " Impossible de créer la facture"
# recuperation des paramettre
my_token = ""
user_recid = ""
if ("token" in diction.keys()):
if diction['token']:
my_token = diction['token']
user_recid = "None"
# Verification de la validité du token/mail dans le cas des user en mode connecté
if (len(str(my_token)) > 0):
retval = mycommon.check_partner_token_validity("", my_token)
if retval is False:
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide")
return False, " Impossible de créer la facture"
# Recuperation du recid de l'utilisateur
user_recid = mycommon.get_parnter_recid_from_token(my_token)
if user_recid is False:
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le token de l'utilisateur")
return False," Impossible de créer la facture"
if (len(str(my_token)) <= 0):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide")
return False, " Impossible de créer la facture"
new_data = {}
if ("nb_product" in diction.keys()):
if diction['nb_product']:
nb_line = mycommon.tryInt(diction['nb_product'])
if ("periodicite" in diction.keys()):
if diction['periodicite']:
new_data['periodicite'] = diction['periodicite']
if ("prix_ttc" in diction.keys()):
if diction['prix_ttc']:
new_data['prix_ttc'] = diction['prix_ttc']
if ("prix_ht" in diction.keys()):
if diction['prix_ht']:
new_data['prix_ht'] = diction['prix_ht']
# Recuperation des données client
new_data['client_recid'] = user_recid
coll_part_account = MYSY_GV.dbname['partnair_account']
part_account = coll_part_account.find({'recid':user_recid, 'active':'1'})
if ("invoice_nom" in part_account[0].keys()):
if part_account[0]['invoice_nom']:
new_data['invoice_nom'] = part_account[0]['invoice_nom']
if ("invoice_adr_city" in part_account[0].keys()):
if part_account[0]['invoice_adr_city']:
new_data['invoice_adr_city'] = part_account[0]['invoice_adr_city']
if ("invoice_adr_country" in part_account[0].keys()):
if part_account[0]['invoice_adr_country']:
new_data['invoice_adr_country'] = part_account[0]['invoice_adr_country']
if ("invoice_adr_street" in part_account[0].keys()):
if part_account[0]['invoice_adr_street']:
new_data['invoice_adr_street'] = part_account[0]['invoice_adr_street']
if ("invoice_adr_zip" in part_account[0].keys()):
if part_account[0]['invoice_adr_zip']:
new_data['invoice_adr_zip'] = part_account[0]['invoice_adr_zip']
if ("invoice_adr_street" in part_account[0].keys()):
if part_account[0]['invoice_adr_street']:
new_data['invoice_adr_street'] = part_account[0]['invoice_adr_street']
if ("invoice_email" in part_account[0].keys()):
if part_account[0]['invoice_email']:
new_data['invoice_email'] = part_account[0]['invoice_email']
if ("invoice_vat_num" in part_account[0].keys()):
if part_account[0]['invoice_vat_num']:
new_data['invoice_vat_num'] = part_account[0]['invoice_vat_num']
if ("invoice_telephone" in part_account[0].keys()):
if part_account[0]['invoice_telephone']:
new_data['invoice_telephone'] = part_account[0]['invoice_telephone']
# Recuperation ds données de payement
coll_part_payment = MYSY_GV.dbname['payement_mode']
part_account = coll_part_payment.find({'client_recid': user_recid, 'valide': '1'})
if ("type" in part_account[0].keys()):
if part_account[0]['type']:
new_data['type_payment'] = part_account[0]['type']
if ("bic" in part_account[0].keys()):
if part_account[0]['bic']:
new_data['bic_payment'] = part_account[0]['bic']
if ("cvv_carte" in part_account[0].keys()):
if part_account[0]['cvv_carte']:
new_data['cvv_carte_payment'] = part_account[0]['cvv_carte']
if ("date_exp_carte" in part_account[0].keys()):
if part_account[0]['date_exp_carte']:
new_data['date_exp_carte_payment'] = part_account[0]['date_exp_carte']
if ("iban" in part_account[0].keys()):
if part_account[0]['iban']:
new_data['iban_payment'] = part_account[0]['iban']
if ("nom_carte" in part_account[0].keys()):
if part_account[0]['nom_carte']:
new_data['nom_carte_payment'] = part_account[0]['nom_carte']
if ("nom_compte" in part_account[0].keys()):
if part_account[0]['nom_compte']:
new_data['nom_compte_payment'] = part_account[0]['nom_compte']
if ("num_carte" in part_account[0].keys()):
if part_account[0]['num_carte']:
new_data['num_carte_payment'] = part_account[0]['num_carte']
i = 0
while (i < nb_line):
print("PRODUIT N° " + str((i + 1)) + " : " )
line_dict = json.loads(diction[str(i)])
print(" line = "+str(line_dict)+" -- le type est : "+str(type(line_dict)))
print(" code = "+str( line_dict['code'] ))
print(" prix = " + str(line_dict['prix']))
new_data_item = {}
new_data_item['code'] = line_dict['code']
new_data_item['prix'] = line_dict['prix']
row = "item_"+str(i)
new_data[str(row)] = new_data_item
i = i + 1
new_data['valide'] = "1"
new_data['date_update'] = str(datetime.now())
new_data['order_id'] = "MySy_00"+str(mycommon.create_order_id())
print(" la line à facturer est ::::: "+str(new_data))
# Enregistrement de la commande dans la systeme
coll_order = MYSY_GV.dbname['sales_order']
ret_val = coll_order.insert_one(new_data)
if ret_val and ret_val.inserted_id:
return True, "la commande été correctement créee", str(new_data['order_id'])
else:
mycommon.myprint(
str(inspect.stack()[0][3]) + "Impossible de créer la commande ")
return False, "Impossible de créer la commande ", None
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
return False, " Impossible de créer la facture"

37
main.py
View File

@ -1232,6 +1232,43 @@ def get_product_service():
return jsonify(status=status, message=result)
"""
Cette API rengristre un mode depayment (CB ou SEPA)
"""
@app.route('/myclass/api/add_payement_mode/', methods=['GET','POST'])
@crossdomain(origin='*')
def add_payement_mode():
# On recupere le corps (payload) de la requete
payload = request.form.to_dict()
print(" ### payload = ", payload)
status, result = invoice.add_payement_mode(payload)
return jsonify(status=status, message=result)
"""
Cette API recupere le mode de payement par defaut d'un client
"""
@app.route('/myclass/api/get_payement_mode/', methods=['GET','POST'])
@crossdomain(origin='*')
def get_payement_mode():
# On recupere le corps (payload) de la requete
payload = request.form.to_dict()
print(" ### payload = ", payload)
status, result = invoice.get_payement_mode(payload)
return jsonify(status=status, message=result)
"""
Cette API recupere les données et crée la commande
"""
@app.route('/myclass/api/createOrder/', methods=['GET','POST'])
@crossdomain(origin='*')
def createOrder():
# On recupere le corps (payload) de la requete
payload = request.form.to_dict()
print(" ### payload = ", str(payload))
status, message, order_id = invoice.createOrder(payload)
return jsonify(status=status, message=message, order_id=order_id)
if __name__ == '__main__':
print(" debut api")

View File

@ -43,6 +43,12 @@ def myprint(message = ""):
print(str(datetime.now()) + " : " + str(message))
def create_order_id():
return secrets.token_urlsafe(3)
def create_token_urlsafe():
return secrets.token_urlsafe(MYSY_GV.TOKEN_SIZE)