402 lines
15 KiB
Python
402 lines
15 KiB
Python
"""
|
|
Ce fichier permet de gerer les caracterisques financières
|
|
d'une formation, d'une session ou toutes autre object metier.
|
|
|
|
Par exemple il permettra de gere sur une session :
|
|
- les frais d'inscription
|
|
- les frais de scolarité
|
|
- les couts fixe (utiliser pour calculer la rentrabilité, etc).
|
|
|
|
IMPORTANT : Il ne s'agit aucunement de faire de la comptabilité mais gerer les aspect administratif finacier
|
|
"""
|
|
import bson
|
|
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 jinja2
|
|
from flask import send_file
|
|
from xhtml2pdf import pisa
|
|
from email.message import EmailMessage
|
|
from email.mime.text import MIMEText
|
|
from email import encoders
|
|
import smtplib
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
from email.mime.base import MIMEBase
|
|
from email import encoders
|
|
"""
|
|
Fonction d'ajout d'une caracteristique financière
|
|
"""
|
|
def Add_Financial_Caracteristique(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'code' , 'description', 'commentaire', 'montant',
|
|
'related_collection', 'related_collection_id', 'type_cout',
|
|
'inclus_calcul']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes"
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'code', 'related_collection', 'related_collection_id']
|
|
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 la liste des arguments ")
|
|
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']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
"""
|
|
Verifier que ce code de la cara fin n'est pas déjà utilisé
|
|
"""
|
|
is_competence_code_used = MYSY_GV.dbname['financial_caracteristic'].count_documents({'code':str(diction['code']),
|
|
'valide':'1',
|
|
'partner_owner_recid':str(my_partner['recid'])})
|
|
|
|
if( is_competence_code_used != 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " Le code "+str(diction['code'])+" est déjà utilisé ")
|
|
return False, " Le code "+str(diction['code'])+" est déjà utilisé "
|
|
|
|
if( "montant" in diction.keys() and diction['montant']):
|
|
local_status, valeur = mycommon.IsFloat(str(diction['montant']))
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " Le montant est incorrecte. la veleur attendue est un entier ou un decimal.")
|
|
return False, " Le montant est incorrecte. la veleur attendue est un entier ou un decimal. "
|
|
|
|
|
|
"""
|
|
Verifier la validité de la related_collection et related_collection_id
|
|
"""
|
|
is_related_collection_id_valide = MYSY_GV.dbname[str(diction['related_collection'])].count_documents({'_id':ObjectId(str(diction['related_collection_id'])),
|
|
'valide':'1',
|
|
'partner_owner_recid':str(my_partner['recid'])})
|
|
|
|
if( is_related_collection_id_valide != 1 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " L'identifiant de la related_collection_id est invalide ")
|
|
return False, " L'identifiant de la related_collection_id est invalide "
|
|
|
|
|
|
mydata = {}
|
|
|
|
mydata = diction
|
|
del mydata['token']
|
|
|
|
# Initialisation des champs non envoyés à vide
|
|
for val in field_list:
|
|
if val not in diction.keys():
|
|
mydata[str(val)] = ""
|
|
|
|
mydata['creation_date'] = str(datetime.now())
|
|
mydata['creation_by'] = str(my_partner['_id'])
|
|
mydata['partner_owner_recid'] = str(my_partner['recid'])
|
|
mydata['valide'] = "1"
|
|
mydata['locked'] = "0"
|
|
|
|
|
|
|
|
inserted_id = MYSY_GV.dbname['financial_caracteristic'].insert_one(mydata).inserted_id
|
|
|
|
if (not inserted_id):
|
|
mycommon.myprint(" Impossible de créer la caractéristique financière (2) ")
|
|
return False, " Impossible de créer la caractéristique financière (2) "
|
|
|
|
return True, " La caractéristique financière a été correctement ajoutée "
|
|
|
|
|
|
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 créer la caractéristique financière "
|
|
|
|
|
|
"""
|
|
Fonction de mise à jour d'une Financial_Caracteristique
|
|
"""
|
|
def Update_Financial_Caracteristique(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id', 'code' , 'description', 'commentaire', 'montant', 'type_cout',
|
|
'related_collection', 'related_collection_id', 'inclus_calcul'
|
|
]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes"
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id', 'code']
|
|
|
|
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 la liste des arguments ")
|
|
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']
|
|
|
|
fin_ca_id = ""
|
|
if ("_id" in diction.keys()):
|
|
if diction['_id']:
|
|
fin_ca_id = diction['_id']
|
|
|
|
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
"""
|
|
Verifier que le code de compétence n'est pas utilisée par une autre compétence autre que celle ci
|
|
"""
|
|
|
|
is_fiance_ca_code_used = MYSY_GV.dbname['financial_caracteristic'].count_documents({'code': str(diction['code']),
|
|
'valide': '1',
|
|
'_id':{'$ne':ObjectId(str(diction['_id']))},
|
|
'partner_owner_recid': str(
|
|
my_partner['recid'])})
|
|
|
|
if (is_fiance_ca_code_used != 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " Le code " + str(diction['code']) + " est déjà utilisé ")
|
|
return False, " Le code " + str(diction['code']) + " est déjà utilisé "
|
|
|
|
|
|
# Verifier que la caracteristique existe et est valide
|
|
is_valide_fin_ca = MYSY_GV.dbname['financial_caracteristic'].count_documents({'_id':ObjectId(str(fin_ca_id)),
|
|
'valide':'1',
|
|
'locked':'0',
|
|
'partner_owner_recid':str(my_partner['recid'])})
|
|
|
|
|
|
if( is_valide_fin_ca != 1):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de la caractéristique financière est invalide ")
|
|
return False, " L'identifiant de la caractéristique financière est invalide "
|
|
|
|
|
|
mydata = {}
|
|
mydata = diction
|
|
del mydata['token']
|
|
del mydata['_id']
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
mydata['update_by'] = str(my_partner['_id'])
|
|
|
|
result = MYSY_GV.dbname['financial_caracteristic'].find_one_and_update(
|
|
{'_id':ObjectId(str(fin_ca_id)),
|
|
'partner_owner_recid':str(my_partner['recid'])},
|
|
{"$set": mydata},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
if ("_id" not in result.keys()):
|
|
mycommon.myprint(
|
|
" Impossible de mettre à jour la caractéristique financière (2) ")
|
|
return False, " Impossible de mettre à jour la caractéristique financière (2) "
|
|
|
|
|
|
|
|
return True, " La caractéristique financière 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 la caractéristique financière "
|
|
|
|
|
|
"""
|
|
Recuperer la liste des Financial_Caracteristique d'un partner, sans filtres
|
|
"""
|
|
def Get_Financial_Caracteristique_no_filter(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'related_collection' , 'related_collection_id']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes"
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'related_collection' , 'related_collection_id']
|
|
|
|
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 la liste des arguments ")
|
|
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']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
|
|
RetObject = []
|
|
val_tmp = 0
|
|
|
|
qry = {"partner_owner_recid":str(my_partner['recid']), 'valide':'1', 'locked':'0',
|
|
'related_collection':str(diction['related_collection']), 'related_collection_id':str(diction['related_collection_id'])}
|
|
|
|
print(" ### Get_Financial_Caracteristique_no_filter QRY = ", qry)
|
|
|
|
for New_retVal in MYSY_GV.dbname['financial_caracteristic'].find(qry).sort([("_id", pymongo.DESCENDING), ]):
|
|
user = New_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 la liste des caractéristiques financières "
|
|
|
|
|
|
"""
|
|
Suppression d'une liste de Financial_Caracteristique
|
|
"""
|
|
def Delete_List_Financial_Caracteristique(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'tab_id', ]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes"
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'tab_id', ]
|
|
|
|
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 la liste des arguments ")
|
|
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']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
|
|
local_tab_ids = str(diction['tab_id']).split(",")
|
|
tab_ids_ObjectID = []
|
|
for tmp in local_tab_ids:
|
|
if( tmp ):
|
|
tab_ids_ObjectID.append(ObjectId(str(tmp)))
|
|
|
|
|
|
qry = {'_id':{'$in':tab_ids_ObjectID}, 'partner_owner_recid':str(my_partner['recid'])}
|
|
|
|
ret_del_ca_fin = MYSY_GV.dbname['financial_caracteristic'].delete_many(qry)
|
|
|
|
return True, "Caractéristique financière supprimée(s)"
|
|
|
|
|
|
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 supprimer la compétence "
|
|
|