636 lines
24 KiB
Python
636 lines
24 KiB
Python
"""
|
||
Ce fichier permet de traiter et generer le BPF
|
||
SPEC : Source : https://culture-rh.com/bfp-bilan-pedagogique-financier/
|
||
"""
|
||
|
||
import ast
|
||
import dateutil
|
||
import pymongo
|
||
import xlsxwriter
|
||
from flask import send_file
|
||
from pymongo import MongoClient
|
||
import json
|
||
from bson import ObjectId
|
||
import re
|
||
from datetime import datetime, timezone, date
|
||
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
|
||
from datetime import timedelta
|
||
from datetime import timedelta
|
||
import Dashbord_queries.formation_tbd_qries as formation_tbd_qries
|
||
from dateutil.relativedelta import relativedelta
|
||
|
||
def Get_Qery_Generate_BPF_From_partner_invoice_header(diction):
|
||
try:
|
||
diction = mycommon.strip_dictionary(diction)
|
||
|
||
"""
|
||
Verification des input acceptés
|
||
"""
|
||
field_list = ['token', 'periode_start_date', 'periode_end_date', 'filter_value', ]
|
||
|
||
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'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']
|
||
|
||
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
||
if (local_status is not True):
|
||
return local_status, my_partner
|
||
|
||
filt_client_id = {}
|
||
if ("filter_client_id" in diction.keys() and diction['filter_client_id']):
|
||
filt_client_id = {'order_header_client_id': str(diction['filter_client_id'])}
|
||
|
||
|
||
filt_periode_start_date = ""
|
||
if ("periode_start_date" in diction.keys() and diction['periode_start_date']):
|
||
filt_periode_start_date = str(diction['periode_start_date'])[0:10]
|
||
local_status = mycommon.CheckisDate(filt_periode_start_date)
|
||
if (local_status is False):
|
||
mycommon.myprint(str(
|
||
inspect.stack()[0][3]) + " Le filtre : 'date de debut' n'est pas au format jj/mm/aaaa.")
|
||
return False, " Le filtre : 'date de debut' n'est pas au format jj/mm/aaaa."
|
||
|
||
filt_periode_end_date = ""
|
||
if ("periode_end_date" in diction.keys() and diction['periode_end_date']):
|
||
filt_periode_end_date = str(diction['periode_end_date'])[0:10]
|
||
local_status = mycommon.CheckisDate(filt_periode_end_date)
|
||
if (local_status is False):
|
||
mycommon.myprint(str(
|
||
inspect.stack()[0][3]) + " Le filtre : 'date de fin' n'est pas au format jj/mm/aaaa.")
|
||
return False, " Le filtre : 'date de fin' n'est pas au format jj/mm/aaaa."
|
||
|
||
|
||
|
||
"""
|
||
Si la valeur de 'filter_value' est m0 ou m1, on va aller recuperer les date du mois correspondant.
|
||
On ecrase les valeur de filt_session_start_date et filt_session_end_date
|
||
"""
|
||
if ('filter_value' in diction.keys()):
|
||
# print(" filter_value = ", diction['filter_value'])
|
||
if (str(diction['filter_value']) == "m0"):
|
||
# On recupere les date du mois en cours
|
||
local_status, start_current_month_date, end_current_month_date = mycommon.Get_Current_Month_Start_End_Date()
|
||
if (local_status is False):
|
||
return local_status, start_current_month_date
|
||
|
||
filt_periode_start_date = start_current_month_date
|
||
filt_periode_end_date = end_current_month_date
|
||
|
||
# print(" ### filt_session_start_date = ", filt_session_start_date, " ### filt_session_end_date = ", filt_session_end_date)
|
||
|
||
elif (str(diction['filter_value']) == "m1"):
|
||
# On recupere les date du mois en cours
|
||
local_status, start_current_month_date, end_current_month_date = mycommon.Get_Previous_Month_Start_End_Date()
|
||
if (local_status is False):
|
||
return local_status, start_current_month_date
|
||
|
||
filt_periode_start_date = start_current_month_date
|
||
filt_periode_end_date = end_current_month_date
|
||
|
||
|
||
|
||
filt_periode_start_date_ISODATE = datetime.strptime(str(filt_periode_start_date), '%d/%m/%Y')
|
||
filt_periode_end_date_ISODATE = datetime.strptime(str(filt_periode_end_date), '%d/%m/%Y')
|
||
|
||
RetObject = []
|
||
val_tmp = 0
|
||
|
||
|
||
"""
|
||
C1 – Quelles sont vos ressources provenant directement des entreprises pour la formation de leurs salariés ?
|
||
|
||
On recherche le CA venant des client : is_client = 1, is_financeur != 1
|
||
|
||
"""
|
||
C1_qery_match = {'$and': [{"partner_owner_recid": str(my_partner['recid'])},
|
||
{"valide": '1'},
|
||
|
||
{
|
||
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
|
||
'$lte': filt_periode_end_date_ISODATE}}, ]}
|
||
|
||
C1_pipe_qry = ([
|
||
{"$addFields": {
|
||
"mysy_invoice_date": {
|
||
'$dateFromString': {
|
||
'dateString': '$invoice_date',
|
||
'format': "%d/%m/%Y"
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
{'$match': C1_qery_match},
|
||
{'$lookup': {
|
||
'from': 'partner_client',
|
||
"let": {'order_header_client_id': "$order_header_client_id",
|
||
'partner_owner_recid': '$partner_owner_recid'},
|
||
'pipeline': [
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$_id", {'$convert': {
|
||
'input': "$$order_header_client_id",
|
||
'to': "objectId",
|
||
'onError': {'error': 'true'},
|
||
'onNull': {'isnull': 'true'}
|
||
}}]},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_recid", '$$partner_owner_recid']},
|
||
{'$eq': ["is_client", "1"]},
|
||
{'$ne': ["is_financeur", "1"]},
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_client_collection'
|
||
}
|
||
},
|
||
{'$group': {
|
||
"TotalAmount": {"$sum": {'$toDouble': '$total_header_toutes_taxes'}},
|
||
"count": {"$sum": 1}
|
||
}
|
||
},
|
||
|
||
])
|
||
|
||
print(" ### Get_Qery_Generate_BPF C1_pipe_qry = ", C1_pipe_qry)
|
||
|
||
C2_pipe_qry = ([
|
||
{"$addFields": {
|
||
"mysy_invoice_date": {
|
||
'$dateFromString': {
|
||
'dateString': '$invoice_date',
|
||
'format': "%d/%m/%Y"
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
{'$match': C1_qery_match},
|
||
{'$lookup': {
|
||
'from': 'partner_client',
|
||
"let": {'order_header_client_id': "$order_header_client_id",
|
||
'partner_owner_recid': '$partner_owner_recid'},
|
||
'pipeline': [
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$_id", {'$convert': {
|
||
'input': "$$order_header_client_id",
|
||
'to': "objectId",
|
||
'onError': {'error': 'true'},
|
||
'onNull': {'isnull': 'true'}
|
||
}}]},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_recid", '$$partner_owner_recid']}
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_client_collection'
|
||
}
|
||
},
|
||
{
|
||
'$unwind': '$partner_client_collection'
|
||
},
|
||
|
||
{'$lookup': {
|
||
'from': 'partner_client_type',
|
||
"let": {'client_type_id': "$partner_client_collection.client_type_id",
|
||
'partner_recid': '$partner_recid'
|
||
},
|
||
'pipeline': [
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$_id", {'$convert': {
|
||
'input': "$$client_type_id",
|
||
'to': "objectId",
|
||
'onError': {'error': 'true'},
|
||
'onNull': {'isnull': 'true'}
|
||
}}]},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_client_collection.partner_recid",
|
||
'$$partner_recid']},
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_client_type_collection'
|
||
}
|
||
},
|
||
|
||
{'$group': {
|
||
'_id': {
|
||
"Client_type": "$partner_client_type_collection.code",
|
||
|
||
},
|
||
"TotalAmount": {"$sum": {'$toDouble': '$total_header_toutes_taxes'}},
|
||
"count": {"$sum": 1}
|
||
}
|
||
},
|
||
|
||
|
||
])
|
||
|
||
print(" ### Get_Qery_Generate_BPF C2_pipe_qry = ", C2_pipe_qry)
|
||
|
||
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 les données "
|
||
|
||
|
||
def Get_Qery_Generate_BPF_From_Inscription(diction):
|
||
try:
|
||
diction = mycommon.strip_dictionary(diction)
|
||
|
||
"""
|
||
Verification des input acceptés
|
||
"""
|
||
field_list = ['token', 'periode_start_date', 'periode_end_date', 'filter_value', ]
|
||
|
||
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'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']
|
||
|
||
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
||
if (local_status is not True):
|
||
return local_status, my_partner
|
||
|
||
filt_client_id = {}
|
||
if ("filter_client_id" in diction.keys() and diction['filter_client_id']):
|
||
filt_client_id = {'order_header_client_id': str(diction['filter_client_id'])}
|
||
|
||
filt_periode_start_date = ""
|
||
if ("periode_start_date" in diction.keys() and diction['periode_start_date']):
|
||
filt_periode_start_date = str(diction['periode_start_date'])[0:10]
|
||
local_status = mycommon.CheckisDate(filt_periode_start_date)
|
||
if (local_status is False):
|
||
mycommon.myprint(str(
|
||
inspect.stack()[0][3]) + " Le filtre : 'date de debut' n'est pas au format jj/mm/aaaa.")
|
||
return False, " Le filtre : 'date de debut' n'est pas au format jj/mm/aaaa."
|
||
|
||
filt_periode_end_date = ""
|
||
if ("periode_end_date" in diction.keys() and diction['periode_end_date']):
|
||
filt_periode_end_date = str(diction['periode_end_date'])[0:10]
|
||
local_status = mycommon.CheckisDate(filt_periode_end_date)
|
||
if (local_status is False):
|
||
mycommon.myprint(str(
|
||
inspect.stack()[0][3]) + " Le filtre : 'date de fin' n'est pas au format jj/mm/aaaa.")
|
||
return False, " Le filtre : 'date de fin' n'est pas au format jj/mm/aaaa."
|
||
|
||
"""
|
||
Si la valeur de 'filter_value' est m0 ou m1, on va aller recuperer les date du mois correspondant.
|
||
On ecrase les valeur de filt_session_start_date et filt_session_end_date
|
||
"""
|
||
if ('filter_value' in diction.keys()):
|
||
# print(" filter_value = ", diction['filter_value'])
|
||
if (str(diction['filter_value']) == "m0"):
|
||
# On recupere les date du mois en cours
|
||
local_status, start_current_month_date, end_current_month_date = mycommon.Get_Current_Month_Start_End_Date()
|
||
if (local_status is False):
|
||
return local_status, start_current_month_date
|
||
|
||
filt_periode_start_date = start_current_month_date
|
||
filt_periode_end_date = end_current_month_date
|
||
|
||
# print(" ### filt_session_start_date = ", filt_session_start_date, " ### filt_session_end_date = ", filt_session_end_date)
|
||
|
||
elif (str(diction['filter_value']) == "m1"):
|
||
# On recupere les date du mois en cours
|
||
local_status, start_current_month_date, end_current_month_date = mycommon.Get_Previous_Month_Start_End_Date()
|
||
if (local_status is False):
|
||
return local_status, start_current_month_date
|
||
|
||
filt_periode_start_date = start_current_month_date
|
||
filt_periode_end_date = end_current_month_date
|
||
|
||
filt_periode_start_date_ISODATE = datetime.strptime(str(filt_periode_start_date), '%d/%m/%Y')
|
||
filt_periode_end_date_ISODATE = datetime.strptime(str(filt_periode_end_date), '%d/%m/%Y')
|
||
|
||
RetObject = []
|
||
val_tmp = 0
|
||
|
||
"""
|
||
Voici une requete qui fait une jointure entre
|
||
- Inscription, Client, Type_Client, Facture
|
||
|
||
"""
|
||
C1_qery_match = {'$and': [{"partner_owner_recid": str(my_partner['recid'])},
|
||
{"valide": '1'},
|
||
|
||
{
|
||
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
|
||
'$lte': filt_periode_end_date_ISODATE}}, ]}
|
||
|
||
Inscription_Client_Type_Client_Facture_pipe_qry = ([
|
||
{'$match': {'invoiced':'1'} },
|
||
{'$lookup': {
|
||
'from': 'partner_client',
|
||
"let": {'facture_client_rattachement_id': "$facture_client_rattachement_id",
|
||
'partner_owner_recid': '$partner_owner_recid'},
|
||
'pipeline': [
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$_id", {'$convert': {
|
||
'input': "$$facture_client_rattachement_id",
|
||
'to': "objectId",
|
||
'onError': {'error': 'true'},
|
||
'onNull': {'isnull': 'true'}
|
||
}}]},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_recid", '$$partner_owner_recid']},
|
||
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_client_collection'
|
||
}
|
||
},
|
||
{
|
||
'$unwind': '$partner_client_collection'
|
||
},
|
||
{'$lookup': {
|
||
'from': 'partner_client_type',
|
||
"let": {'client_type_id': "$partner_client_collection.client_type_id",
|
||
'partner_recid': '$partner_recid',
|
||
|
||
},
|
||
'pipeline': [
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$_id", {'$convert': {
|
||
'input': "$$client_type_id",
|
||
'to': "objectId",
|
||
'onError': {'error': 'true'},
|
||
'onNull': {'isnull': 'true'}
|
||
}}]},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_client_collection.partner_recid", '$$partner_recid']},
|
||
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_client_type_collection'
|
||
}
|
||
},
|
||
|
||
|
||
{'$lookup': {
|
||
'from': 'partner_invoice_header',
|
||
"let": {'facture_client_rattachement_id': "$facture_client_rattachement_id",
|
||
'invoiced_ref': "$invoiced_ref",
|
||
'partner_owner_recid': '$partner_owner_recid'
|
||
|
||
},
|
||
'pipeline': [
|
||
{
|
||
"$addFields": {
|
||
"mysy_invoice_date": {
|
||
'$dateFromString': {
|
||
'dateString': '$invoice_date',
|
||
'format': "%d/%m/%Y"
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$invoice_header_ref_interne", '$$invoiced_ref']},
|
||
{'$eq': ["$order_header_client_id", '$$facture_client_rattachement_id']},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
|
||
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_invoice_header_collection'
|
||
}
|
||
},
|
||
|
||
])
|
||
print(" ### Get_Qery_Generate_BPF_From_Inscription Inscription_Client_Type_Client_Facture_pipe_qry = ", Inscription_Client_Type_Client_Facture_pipe_qry)
|
||
|
||
Inscription_Client_Type_Client_Facture_pipe_qry_Client_Financeur = ([
|
||
{'$match': {'invoiced': '1'}},
|
||
{'$lookup': {
|
||
'from': 'partner_client',
|
||
"let": {'facture_client_rattachement_id': "$facture_client_rattachement_id",
|
||
'partner_owner_recid': '$partner_owner_recid'},
|
||
'pipeline': [
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$_id", {'$convert': {
|
||
'input': "$$facture_client_rattachement_id",
|
||
'to': "objectId",
|
||
'onError': {'error': 'true'},
|
||
'onNull': {'isnull': 'true'}
|
||
}}]},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_recid", '$$partner_owner_recid']},
|
||
{'$eq': ["$is_financeur", '1']},
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_client_collection'
|
||
}
|
||
},
|
||
{
|
||
'$unwind': '$partner_client_collection'
|
||
},
|
||
{'$lookup': {
|
||
'from': 'partner_client_type',
|
||
"let": {'client_type_id': "$partner_client_collection.client_type_id",
|
||
'partner_recid': '$partner_recid',
|
||
|
||
},
|
||
'pipeline': [
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$_id", {'$convert': {
|
||
'input': "$$client_type_id",
|
||
'to': "objectId",
|
||
'onError': {'error': 'true'},
|
||
'onNull': {'isnull': 'true'}
|
||
}}]},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_client_collection.partner_recid", '$$partner_recid']},
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_client_type_collection'
|
||
}
|
||
},
|
||
|
||
{'$lookup': {
|
||
'from': 'partner_invoice_header',
|
||
"let": {'facture_client_rattachement_id': "$facture_client_rattachement_id",
|
||
'invoiced_ref': "$invoiced_ref",
|
||
'partner_owner_recid': '$partner_owner_recid'
|
||
|
||
},
|
||
'pipeline': [
|
||
{
|
||
"$addFields": {
|
||
"mysy_invoice_date": {
|
||
'$dateFromString': {
|
||
'dateString': '$invoice_date',
|
||
'format': "%d/%m/%Y"
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
{'$match':
|
||
{'$expr':
|
||
{'$and':
|
||
[
|
||
|
||
{'$eq': ["$invoice_header_ref_interne", '$$invoiced_ref']},
|
||
{'$eq': ["$order_header_client_id", '$$facture_client_rattachement_id']},
|
||
|
||
{'$eq': ["$valide", "1"]},
|
||
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
|
||
|
||
]
|
||
}
|
||
}
|
||
},
|
||
|
||
],
|
||
'as': 'partner_invoice_header_collection'
|
||
}
|
||
},
|
||
|
||
])
|
||
print(" ### Get_Qery_Generate_BPF_From_Inscription Inscription_Client_Type_Client_Facture_pipe_qry_Client_Financeur = ",
|
||
Inscription_Client_Type_Client_Facture_pipe_qry)
|
||
|
||
|
||
|
||
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 les données "
|
||
|