Ela_Back/Dashbord_queries/BPF.py

3609 lines
149 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""
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 pandas.io.formats.style import jinja2
from pymongo import MongoClient
import json
from bson import ObjectId
import re
from datetime import datetime, timezone, date
from xhtml2pdf import pisa
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 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
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': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
'order_header_is_company':'1',
'order_header_is_client':'1',
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
"""
TRAITEMENT DU PAVE CCCC
"""
total_c1_C9 = 0
bpf_c1_entreprise = []
total_bpf_c1_entreprise = 0
C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1 = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C1_qery_match
},
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$group': {
'_id': { },
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
print(
" ### C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1 = ",
C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1)
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(
C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1):
print(" ### retval bpf_c1_entreprise = ", retval)
node = {}
node['bpf_c1_entreprise'] = retval['TotalAmount_HT']
total_bpf_c1_entreprise = total_bpf_c1_entreprise + mycommon.tryFloat(retval['TotalAmount_HT'])
bpf_c1_entreprise.append(node)
RetObject['bpf_c1_entreprise'] = bpf_c1_entreprise
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c1_entreprise)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### bpf_c1_entreprise = ", total_bpf_c1_entreprise)
C2_qery_match = {'$and': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
'order_header_is_financeur':'1',
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C2_qery_match },
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$lookup': {
'from': 'type_organisme_financement',
"let": {'type_financeur_id': "$order_header_type_financeur_id",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$type_financeur_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
]
}
}
},
],
'as': 'type_organisme_financement_collection'
}
},
{
'$unwind': '$type_organisme_financement_collection'
},
{'$lookup': {
'from': 'type_apprenant',
"let": {'type_apprenant_code': "$collection_partner_invoice_line.order_line_inscription_type_apprenant",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$code", '$$type_apprenant_code']},
]
}
}
},
],
'as': 'type_apprenante_collection'
}
},
{
'$unwind': '$type_apprenante_collection'
},
{'$group': {
'_id': {
"Type_Apprenant": "$collection_partner_invoice_line.order_line_inscription_type_apprenant",
"Type_Apprenant_Description": "$type_apprenante_collection.description",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
print(" ### Get_Qery_Generate_BPF C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant = ", C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant)
bpf_c2_type_apprenant = []
total_bpf_c_type_apprenant = 0
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant):
print(" ### retval = ", retval)
node = {}
node['Type_Apprenant_Code'] = retval['_id']['Type_Apprenant']
node['Type_Apprenant_Desc'] = retval['_id']['Type_Apprenant_Description']
node['TotalAmount_HT'] = retval['TotalAmount_HT']
total_bpf_c_type_apprenant = total_bpf_c_type_apprenant + mycommon.tryFloat(retval['TotalAmount_HT'])
bpf_c2_type_apprenant.append(node)
RetObject['bpf_c2_type_apprenant'] = bpf_c2_type_apprenant
RetObject['total_bpf_c_type_apprenant'] = round(total_bpf_c_type_apprenant, 2)
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c_type_apprenant)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### total_bpf_c_type_apprenant = ", total_bpf_c_type_apprenant)
C3_qery_match = {'$and': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
C3_C8_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_pouvoir_public = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C3_qery_match
},
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$lookup': {
'from': 'type_pouvoir_public',
"let": {'order_header_type_pouvoir_public_id': "$order_header_type_pouvoir_public_id",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$order_header_type_pouvoir_public_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
]
}
}
},
],
'as': 'type_pouvoir_public_collection'
}
},
{
'$unwind': '$type_pouvoir_public_collection'
},
{'$group': {
'_id': {
"Type_pouvoir_public_desc": "$type_pouvoir_public_collection.description",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
bpf_c3_c8_type_pouvoir_public = []
total_bpf_c3_c8_type_pouvoir_public = 0
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(
C3_C8_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_pouvoir_public):
print(" bpf_c3_c8_type_pouvoir_public ### retval = ", retval)
node = {}
node['Type_pouvoir_public_desc'] = retval['_id']['Type_pouvoir_public_desc']
node['TotalAmount_HT'] = retval['TotalAmount_HT']
total_bpf_c3_c8_type_pouvoir_public = total_bpf_c3_c8_type_pouvoir_public + mycommon.tryFloat(retval['TotalAmount_HT'])
bpf_c3_c8_type_pouvoir_public.append(node)
RetObject['bpf_c3_c8_type_pouvoir_public'] = bpf_c3_c8_type_pouvoir_public
RetObject['total_bpf_c3_c8_type_pouvoir_public'] = round(total_bpf_c3_c8_type_pouvoir_public, 2)
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c3_c8_type_pouvoir_public)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### bpf_c3_c8_type_pouvoir_public = ", total_bpf_c3_c8_type_pouvoir_public)
C9_qery_match = {'$and': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
'order_header_is_company':'0',
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
C9_pipe_qry_invoice_header_partner__invoice_line_detail__is_company_0 = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C9_qery_match},
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$group': {
'_id': {
"Type_Client": "$order_header_is_company",
"Type_Client_Desc": "Client Type Particulier",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
bpf_c9_is_company_particulier = []
total_bpf_c9_is_company_particulier = 0
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(C9_pipe_qry_invoice_header_partner__invoice_line_detail__is_company_0):
print(" ### retval is_company_particulier = ", retval)
node = {}
node['Type_Client_Desc'] = retval['_id']['Type_Client_Desc']
node['TotalAmount_HT'] = retval['TotalAmount_HT']
total_bpf_c9_is_company_particulier = total_bpf_c9_is_company_particulier + mycommon.tryFloat(retval['TotalAmount_HT'])
bpf_c9_is_company_particulier.append(node)
RetObject['bpf_c9_is_company_particulier'] = bpf_c9_is_company_particulier
RetObject['total_bpf_c9_is_company_particulier'] = round(total_bpf_c9_is_company_particulier, 2)
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c9_is_company_particulier)
RetObject['total_bpf_c1_c9'] = str(total_c1_C9)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### total_bpf_c9_is_company_particulier = ", total_bpf_c9_is_company_particulier)
"""
FIN TRAITEMENT DU PAVE CCCC
"""
"""
TRAITEMENT DU PAVE EEE
"""
E_pipe_qry_ressource_humaine__ressource_humaine_contrat = ([
{
"$project": {
"_id": 1, "partner_recid": 1, "nom": 1, "prenom": 1, "fonction": 1, "email": 1, "email": 1,
"email": 1, "email": 1, "email": 1,
}
},
{'$lookup': {
'from': 'ressource_humaine_contrat',
'let': {'rh_id': {'$toString': '$_id'}, 'rh_partner_owner_recid': '$partner_recid'},
'pipeline': [
{"$addFields": {
"mysy_contrat_date_debut": {
'$dateFromString': {
'dateString': '$date_debut',
'format': "%d/%m/%Y"
}
},
"mysy_contrat_date_fin": {
'$dateFromString': {
'dateString': '$date_fin',
'format': "%d/%m/%Y"
}
},
}
},
{'$match':
{'$expr':
{'$and':
[ {'$eq': ['$valide', '1']},
{'$eq': ['$rh_id', '$$rh_id']},
{'$eq': ['$partner_owner_recid', '$$rh_partner_owner_recid']},
{"$and":
[
{"$gt": [
{
"$toDate": "$mysy_contrat_date_fin"
},filt_periode_start_date_ISODATE
]},
{"$lt": [
{
"$toDate": "$mysy_contrat_date_debut"
},filt_periode_end_date_ISODATE
]},
],
},
]
}
}
},
{
"$project": {
"_id": 1, "rh_id": 1, "date_debut": 1, "date_fin": 1, "type_contrat": 1, "type_employe": 1,
"cout": 1, "periodicite": 1, "mysy_contrat_date_debut": 1, "mysy_contrat_date_fin": 1
}
},
],
'as': 'collection_ressource_humaine_contrat'
}
},
{
'$unwind': '$collection_ressource_humaine_contrat'
},
{'$lookup': {
'from': 'agenda',
"let": {"rh_id": {'$toString': "$_id"}, 'rh_partner_owner_recid': '$partner_recid'},
'pipeline': [
{
"$addFields": {
"agenda_event_date_debut": {
"$dateFromString": {
'dateString': {"$substr": ["$event_start", 0, 10]},
"format": "%Y-%m-%d"
}
},
"agenda_event_date_fin": {
"$dateFromString": {
'dateString': {"$substr": ["$event_end", 0, 10]},
"format": "%Y-%m-%d"
}
},
}
},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$related_collection_recid", '$$rh_id']},
{'$eq': ["$partner_owner_recid", '$$rh_partner_owner_recid']},
{'$eq': ["$event_type", 'planning']},
{'$gte': [{'$toDate': "$agenda_event_date_debut"},filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$agenda_event_date_fin"}, filt_periode_end_date_ISODATE]}
]
}
}
},
],
'as': 'collection_agenda'
}
},
])
tab_formateur_rh_id_interne = []
tab_formateur_rh_interne_nom_prenom_mail = []
tab_formateur_rh_interne_total_heure = 0
tab_formateur_rh_id_sous_traitant = []
tab_formateur_rh_sous_traitant_nom_prenom_mail = []
tab_formateur_rh_sous_traitant_total_heure = 0
print(" ### E_pipe_qry_ressource_humaine__ressource_humaine_contrat = ", E_pipe_qry_ressource_humaine__ressource_humaine_contrat)
for retval in MYSY_GV.dbname['ressource_humaine'].aggregate(
E_pipe_qry_ressource_humaine__ressource_humaine_contrat):
#print(" ### retval (employé ayant un contrat valide) = ", retval)
formateur_id = str(retval['_id'])
formateur_data = str(retval['nom']) + " " + str(retval['prenom']) + " - " + str(retval['email']) + " - " + str(retval['_id'])
formateur_contrat_type = ""
if( "collection_ressource_humaine_contrat" in retval.keys() and "type_contrat" in retval['collection_ressource_humaine_contrat'].keys()):
formateur_contrat_type = retval['collection_ressource_humaine_contrat']["type_contrat"]
total_duration = 0
for agenda_retval in retval['collection_agenda']:
#print(" ### pour formateur_data = ", formateur_data)
#print(" ### AGENDA : ", agenda_retval)
event_duration_second = datetime.strptime( str(agenda_retval['event_end'])[0:16], '%Y-%m-%dT%H:%M') - datetime.strptime(str(agenda_retval['event_start'])[0:16],'%Y-%m-%dT%H:%M')
event_duration_second = event_duration_second.total_seconds()
event_duration_hour = round(divmod(event_duration_second, 3600)[0] + divmod(event_duration_second, 3600)[1] / 3600, 2)
"""
Gerer les eventuels cas d'erreur sur une date de fin event < debut event
"""
if( event_duration_hour < 0 ):
event_duration_hour = 0
total_duration = total_duration + event_duration_hour
formateur_data = formateur_data+" - Nb_heure = "+str(total_duration)+" heure "
if( formateur_contrat_type and formateur_contrat_type in ['vacataire', 'vacataire_qty']):
if( formateur_id not in tab_formateur_rh_id_sous_traitant ):
tab_formateur_rh_id_sous_traitant.append(str(retval['_id']))
tab_formateur_rh_sous_traitant_nom_prenom_mail.append(str(formateur_data))
tab_formateur_rh_sous_traitant_total_heure = tab_formateur_rh_sous_traitant_total_heure + total_duration
elif( formateur_contrat_type and formateur_contrat_type not in ['vacataire', 'vacataire_qty']):
if (formateur_id not in tab_formateur_rh_id_interne):
tab_formateur_rh_id_interne.append(str(retval['_id']))
tab_formateur_rh_interne_nom_prenom_mail.append(str(formateur_data))
tab_formateur_rh_interne_total_heure = tab_formateur_rh_interne_total_heure + total_duration
"""
RetObject['tab_formateur_rh_id_interne'] = tab_formateur_rh_id_interne
RetObject['tab_formateur_rh_interne_nom_prenom_mail'] = tab_formateur_rh_interne_nom_prenom_mail
RetObject['tab_formateur_rh_interne_total_heure'] = tab_formateur_rh_interne_total_heure
"""
RetObject['bpf_e1_nb_formateurs_internee'] = str(len(tab_formateur_rh_id_interne))
RetObject['bpf_e1_nb_heures_formateurs_internes'] = tab_formateur_rh_interne_total_heure
"""
RetObject['tab_formateur_rh_id_sous_traitant'] = tab_formateur_rh_id_sous_traitant
RetObject['tab_formateur_rh_sous_traitant_nom_prenom_mail'] = tab_formateur_rh_sous_traitant_nom_prenom_mail
RetObject['tab_formateur_rh_sous_traitant_total_heure'] = tab_formateur_rh_sous_traitant_total_heure
"""
RetObject['bpf_e2_nb_formateurs_externes'] = str(len(tab_formateur_rh_id_sous_traitant))
RetObject['bpf_e2_nb_heures_formateurs_externes'] = tab_formateur_rh_sous_traitant_total_heure
"""
FIN TRAITEMENT DU PAVE EEEE
"""
"""
TRAITEMENT DU PAVE FFFF
"""
F3_qery_match = {'$and': [{"partner_owner_recid": str(my_partner['recid'])}, {"valide": '1', 'status':'1'},
]}
F3_pipe_qry_stagiaire_par_class_level = ([
{'$project': {'_id': 1, 'email': 1, 'session_id': 1, 'nom': 1, 'prenom': 1, 'partner_owner_recid': 1,
'valide': 1, 'type_apprenant': 1, 'status':1}},
{'$match': F3_qery_match},
{'$lookup': {
'from': 'session_formation',
'let': {'session_id': "$session_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [
{
"$addFields": {
"mysy_date_debut_session": {
"$dateFromString": {
"dateString": "$date_debut",
"format": "%d/%m/%Y"
}
}
}
},
{'$project': {'_id': 1, 'code_session': 1, 'class_internal_url': 1, 'date_debut': 1, 'date_fin': 1,
'nb_participant': 1, 'partner_owner_recid': 1, 'valide': 1, 'mysy_date_debut_session':1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$session_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
{'$gte': [{'$toDate': "$mysy_date_debut_session"}, filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$mysy_date_debut_session"}, filt_periode_end_date_ISODATE]},
]
}
}
},
],
'as': 'session_formation_collection'
}
},
{
'$unwind': '$session_formation_collection'
},
{
"$lookup": {
'from': 'myclass',
'localField': 'session_formation_collection.class_internal_url',
'foreignField': 'internal_url',
"pipeline": [
{'$project': {'title': 1, 'internal_url': 1, 'external_code': 1, 'published': 1,
'duration': 1, 'duration_unit': 1, 'class_level':1, 'partner_owner_recid':1}},
{'$match': {'partner_owner_recid': str(my_partner['recid'])}},
],
"as": "myclass_collection"
}
},
{
'$unwind': '$myclass_collection'
},
{'$lookup': {
'from': 'class_niveau_formation',
"let": {'class_level_code': "$myclass_collection.class_level",
},
'pipeline': [
{'$project': {'_id': 1, 'code': 1, 'description': 1, 'valide': 1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$code", '$$class_level_code']},
]
}
}
},
],
'as': 'class_niveau_formation_collection'
}
},
{
'$unwind': '$class_niveau_formation_collection'
},
{'$group': {
'_id': {
"Class_Level_Code": "$class_niveau_formation_collection.code",
"Class_Level_Desc": "$class_niveau_formation_collection.description",
},
"TotalAmount_HT": {
"$sum": 1},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
print(" ### F3_pipe_qry_stagiaire_par_class_level = ", F3_pipe_qry_stagiaire_par_class_level)
bpf_f3_class_niveau_formation = []
total_bpf_f3_class_niveau_formation = 0
"""
Recuperer la configuration de conversion des semaines, mois, années en nombre d'heures
"""
local_conversion_heure_status, local_nb_heure_par_jour, local_nb_heure_par_sem, local_nb_heure_par_mois, local_nb_heure_par_annee = mycommon.Get_Config_Conversion_Heures(str(my_partner['recid']))
for retval in MYSY_GV.dbname['inscription'].aggregate(F3_pipe_qry_stagiaire_par_class_level):
#print(" ### retval = ", retval)
node = {}
node['Class_Level_Desc'] = retval['_id']['Class_Level_Desc']
node['Nb_Apprenant_By_Class_Level'] = retval['count']
"""
Aller chercher le nombre d'heures de formation des formations concernées par niveau
"""
pipe_concerned_class = ([
{'$project': {'_id': 1, 'email': 1, 'session_id': 1, 'nom': 1, 'prenom': 1, 'partner_owner_recid': 1,
'valide': 1, 'type_apprenant': 1, 'status':1}},
{'$match': F3_qery_match},
{'$lookup': {
'from': 'session_formation',
'let': {'session_id': "$session_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [
{
"$addFields": {
"mysy_date_debut_session": {
"$dateFromString": {
"dateString": "$date_debut",
"format": "%d/%m/%Y"
}
}
}
},
{'$project': {'_id': 1, 'code_session': 1, 'class_internal_url': 1, 'date_debut': 1,
'date_fin': 1,
'nb_participant': 1, 'partner_owner_recid': 1, 'valide': 1, 'mysy_date_debut_session':1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$session_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
{'$gte': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_end_date_ISODATE]},
]
}
}
},
],
'as': 'session_formation_collection'
}
},
{
'$unwind': '$session_formation_collection'
},
{
"$lookup": {
'from': 'myclass',
'localField': 'session_formation_collection.class_internal_url',
'foreignField': 'internal_url',
"pipeline": [
{'$project': {'_id':1, 'title': 1, 'internal_url': 1, 'external_code': 1, 'published': 1,
'duration': 1, 'duration_unit': 1, 'class_level': 1, 'partner_owner_recid':1}},
{'$match': {'class_level': str(retval['_id']['Class_Level_Code']) , 'partner_owner_recid': str(my_partner['recid'])}},
],
"as": "myclass_collection"
}
},
{
'$unwind': '$myclass_collection'
},
])
#print(" ### pipe_concerned_class = ", pipe_concerned_class)
tab_class_info = []
tab_class_id = []
total_nb_heure_formation_by_level = 0
if( local_conversion_heure_status is True ):
for retval_pipe_concerned_class in MYSY_GV.dbname['inscription'].aggregate(pipe_concerned_class):
#print(" ### retval_pipe_concerned_class = ", retval_pipe_concerned_class)
if( "myclass_collection" in retval_pipe_concerned_class.keys() and
"duration" in retval_pipe_concerned_class['myclass_collection'].keys() and retval_pipe_concerned_class['myclass_collection']['duration'] and
"duration_unit" in retval_pipe_concerned_class['myclass_collection'].keys() and retval_pipe_concerned_class['myclass_collection']['duration_unit'] ):
local_duration = mycommon.tryFloat(str(retval_pipe_concerned_class['myclass_collection']['duration']))
local_duration_unit = str(retval_pipe_concerned_class['myclass_collection']['duration_unit'])
#print(" ### local_duration = ", str(local_duration), " duration_unit = ", str(local_duration_unit) )
if( str(retval_pipe_concerned_class['myclass_collection']['_id']) not in tab_class_id ):
tab_class_id.append((str(retval_pipe_concerned_class['myclass_collection']['_id'])))
local_node = {}
local_node['class_id'] = str(retval_pipe_concerned_class['myclass_collection']['_id'])
local_node['class_duration'] = str(local_duration)
local_node['class_duration_unit'] = local_duration_unit
tab_class_info.append(local_node)
for local_retval in tab_class_info :
#print( " ### local_retval = ",local_retval)
local_duration = mycommon.tryFloat(str(local_retval['class_duration']))
local_duration_unit = str(local_retval['class_duration_unit'])
if( str(local_duration_unit) == "heure"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + local_duration
elif( str(local_duration_unit) == "jour"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + ( local_duration * local_nb_heure_par_jour)
elif (str(local_duration_unit) == "semaine"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + (local_duration * local_nb_heure_par_sem)
elif (str(local_duration_unit) == "mois"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + (local_duration * local_nb_heure_par_mois)
elif (str(local_duration_unit) == "annee"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + (local_duration * local_nb_heure_par_annee)
node['total_nb_heure_formation_by_level'] = str(total_nb_heure_formation_by_level)
total_bpf_f3_class_niveau_formation = total_bpf_f3_class_niveau_formation + mycommon.tryFloat(retval['count'])
bpf_f3_class_niveau_formation.append(node)
RetObject['bpf_f3_class_niveau_formation'] = bpf_f3_class_niveau_formation
RetObject['total_bpf_f3_class_niveau_formation'] = round(total_bpf_f3_class_niveau_formation, 2)
""""
F1 Les différents types de stagiaires au sein de votre organisme
"""
"""
Recuperation des types d'apprenant
"""
bpf_f1_type_apprenant = []
nb_personne_a_distance = 0
nb_personne = 0
nb_heure_formation = 0
for type_apprenant_data in MYSY_GV.dbname['type_apprenant'].find({'valide':'1', 'locked':'0'}):
F1_qery_match = {'$and': [{"partner_owner_recid": str(my_partner['recid'])}, {"valide": '1', 'status': '1',
'type_apprenant':str(type_apprenant_data['code'])},
]}
F1_pipe_qry_stagiaire_type = ([
{'$project': {'_id': 1, 'email': 1, 'session_id': 1, 'nom': 1, 'prenom': 1, 'partner_owner_recid': 1,
'valide': 1, 'type_apprenant': 1, 'status': 1, }},
{'$match': F1_qery_match},
{'$lookup': {
'from': 'session_formation',
'let': {'session_id': "$session_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [
{
"$addFields": {
"mysy_date_debut_session": {
"$dateFromString": {
"dateString": "$date_debut",
"format": "%d/%m/%Y"
}
}
}
},
{'$project': {'_id': 1, 'code_session': 1, 'class_internal_url': 1, 'date_debut': 1, 'date_fin': 1,
'nb_participant': 1, 'partner_owner_recid': 1, 'valide': 1,
'mysy_date_debut_session': 1, 'distantiel':1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$session_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
{'$gte': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$mysy_date_debut_session"}, filt_periode_end_date_ISODATE]},
]
}
}
},
],
'as': 'session_formation_collection'
}
},
{
'$unwind': '$session_formation_collection'
},
{
"$lookup": {
'from': 'myclass',
'localField': 'session_formation_collection.class_internal_url',
'foreignField': 'internal_url',
"pipeline": [
{'$project': {'title': 1, 'internal_url': 1, 'external_code': 1, 'published': 1,
'duration': 1, 'duration_unit': 1, 'class_level': 1, 'partner_owner_recid': 1}},
{'$match': {'partner_owner_recid': str(my_partner['recid'])}},
],
"as": "myclass_collection"
}
},
{
'$unwind': '$myclass_collection'
},
{
"$group": {
"_id": {
"class_id": "$myclass_collection._id",
"class_external_code": "$myclass_collection.external_code",
"class_duration": "$myclass_collection.duration",
"class_duration_unit": "$myclass_collection.duration_unit",
"session_distance": "$session_formation_collection.distantiel"
},
"count": {
"$sum": 1
}
}
},
])
print(" ### F1_pipe_qry_stagiaire_type pour : "+str(type_apprenant_data['description'])+" = ", F1_pipe_qry_stagiaire_type)
total_bpf_f1_type_apprenant = 0
total_bpf_f1_nb_heure_formation = 0
node = {}
node['Type_Apprenant'] = str(type_apprenant_data['code'])
node['Type_Apprenant_Desc'] = str(type_apprenant_data['description'])
local_f1_nb_distance = 0
for f1_local_retval in MYSY_GV.dbname['inscription'].aggregate(F1_pipe_qry_stagiaire_type):
#print(" ### retval F1_pipe_qry_stagiaire_type = ", f1_local_retval)
total_bpf_f1_type_apprenant = total_bpf_f1_type_apprenant + mycommon.tryFloat( f1_local_retval['count'])
nb_personne = nb_personne + + mycommon.tryFloat( f1_local_retval['count'])
if( str(f1_local_retval['_id']['session_distance']) == "1"):
nb_personne_a_distance = nb_personne_a_distance + mycommon.tryFloat( f1_local_retval['count'])
local_f1_nb_distance = local_f1_nb_distance + mycommon.tryFloat( f1_local_retval['count'])
print(" ### nb_personne_a_distance= ", str(nb_personne_a_distance))
print(" ### local_f1_nb_distance= ", str(local_f1_nb_distance))
"""
traitement du nombre d'heure de formation
"""
local_duration = mycommon.tryFloat(str(f1_local_retval['_id']['class_duration']))
local_duration_unit = str(f1_local_retval['_id']['class_duration_unit'])
if (str(local_duration_unit) == "heure"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + local_duration
elif (str(local_duration_unit) == "jour"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_jour)
elif (str(local_duration_unit) == "semaine"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_sem)
elif (str(local_duration_unit) == "mois"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_mois)
elif (str(local_duration_unit) == "annee"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_annee)
node['total_bpf_f1_type_apprenant'] = str(total_bpf_f1_type_apprenant)
node['total_bpf_f1_type_apprenant_distance'] = str(local_f1_nb_distance)
node['total_bpf_f1_nb_heure_formation'] = str(total_bpf_f1_nb_heure_formation)
nb_heure_formation = nb_heure_formation + mycommon.tryFloat( total_bpf_f1_nb_heure_formation)
bpf_f1_type_apprenant.append(node)
RetObject['total_f1_apprenant_a_distance'] = str(nb_personne_a_distance)
RetObject['total_f1_apprenant'] = str(nb_personne)
RetObject['total_f1_nb_heure'] = str(nb_heure_formation)
RetObject['bpf_f1_type_apprenant'] = bpf_f1_type_apprenant
print(" ### RetObject BPF = ", RetObject)
RetObject = mycommon.JSONEncoder().encode(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 les données "
"""
Cette fonction permert d'imprimer le BPF au format PDF
"""
def Prepare_and_Print_BPF_PDF(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 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
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': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
'order_header_is_company': '1',
'order_header_is_client': '1',
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
"""
TRAITEMENT DU PAVE CCCC
"""
total_c1_C9 = 0
bpf_c1_entreprise = []
total_bpf_c1_entreprise = 0
C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1 = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C1_qery_match
},
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$group': {
'_id': {},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
print(
" ### C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1 = ",
C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1)
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(
C1_pipe_qry_invoice_header_partner__invoice_line_detail__is_client_1_is_financeur_0_is_company_1):
print(" ### retval bpf_c1_entreprise = ", retval)
node = {}
node['bpf_c1_entreprise'] = retval['TotalAmount_HT']
total_bpf_c1_entreprise = total_bpf_c1_entreprise + mycommon.tryFloat(retval['TotalAmount_HT'])
bpf_c1_entreprise.append(node)
RetObject['bpf_c1_entreprise'] = bpf_c1_entreprise
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c1_entreprise)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### bpf_c1_entreprise = ", total_bpf_c1_entreprise)
C2_qery_match = {'$and': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
'order_header_is_financeur': '1',
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C2_qery_match},
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$lookup': {
'from': 'type_organisme_financement',
"let": {'type_financeur_id': "$order_header_type_financeur_id",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$type_financeur_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
]
}
}
},
],
'as': 'type_organisme_financement_collection'
}
},
{
'$unwind': '$type_organisme_financement_collection'
},
{'$lookup': {
'from': 'type_apprenant',
"let": {'type_apprenant_code': "$collection_partner_invoice_line.order_line_inscription_type_apprenant",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$code", '$$type_apprenant_code']},
]
}
}
},
],
'as': 'type_apprenante_collection'
}
},
{
'$unwind': '$type_apprenante_collection'
},
{'$group': {
'_id': {
"Type_Apprenant": "$collection_partner_invoice_line.order_line_inscription_type_apprenant",
"Type_Apprenant_Description": "$type_apprenante_collection.description",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
print(
" ### Get_Qery_Generate_BPF C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant = ",
C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant)
bpf_c2_type_apprenant = []
total_bpf_c_type_apprenant = 0
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(
C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant):
print(" ### retval = ", retval)
node = {}
node['Type_Apprenant_Code'] = retval['_id']['Type_Apprenant']
node['Type_Apprenant_Desc'] = retval['_id']['Type_Apprenant_Description']
node['TotalAmount_HT'] = retval['TotalAmount_HT']
total_bpf_c_type_apprenant = total_bpf_c_type_apprenant + mycommon.tryFloat(retval['TotalAmount_HT'])
bpf_c2_type_apprenant.append(node)
RetObject['bpf_c2_type_apprenant'] = bpf_c2_type_apprenant
RetObject['total_bpf_c_type_apprenant'] = round(total_bpf_c_type_apprenant, 2)
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c_type_apprenant)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### total_bpf_c_type_apprenant = ", total_bpf_c_type_apprenant)
C3_qery_match = {'$and': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
C3_C8_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_pouvoir_public = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C3_qery_match
},
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$lookup': {
'from': 'type_pouvoir_public',
"let": {'order_header_type_pouvoir_public_id': "$order_header_type_pouvoir_public_id",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$order_header_type_pouvoir_public_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
]
}
}
},
],
'as': 'type_pouvoir_public_collection'
}
},
{
'$unwind': '$type_pouvoir_public_collection'
},
{'$group': {
'_id': {
"Type_pouvoir_public_desc": "$type_pouvoir_public_collection.description",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
bpf_c3_c8_type_pouvoir_public = []
total_bpf_c3_c8_type_pouvoir_public = 0
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(
C3_C8_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_pouvoir_public):
print(" bpf_c3_c8_type_pouvoir_public ### retval = ", retval)
node = {}
node['Type_pouvoir_public_desc'] = retval['_id']['Type_pouvoir_public_desc']
node['TotalAmount_HT'] = retval['TotalAmount_HT']
total_bpf_c3_c8_type_pouvoir_public = total_bpf_c3_c8_type_pouvoir_public + mycommon.tryFloat(
retval['TotalAmount_HT'])
bpf_c3_c8_type_pouvoir_public.append(node)
RetObject['bpf_c3_c8_type_pouvoir_public'] = bpf_c3_c8_type_pouvoir_public
RetObject['total_bpf_c3_c8_type_pouvoir_public'] = round(total_bpf_c3_c8_type_pouvoir_public, 2)
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c3_c8_type_pouvoir_public)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### bpf_c3_c8_type_pouvoir_public = ",
total_bpf_c3_c8_type_pouvoir_public)
C9_qery_match = {'$and': [
{"valide": '1', 'invoice_header_type': 'facture',
'credit_note_ref': {'$exists': False},
'order_header_is_company': '0',
"partner_owner_recid": str(my_partner['recid'])
},
{
'mysy_invoice_date': {'$gte': filt_periode_start_date_ISODATE,
'$lte': filt_periode_end_date_ISODATE}}, ]}
C9_pipe_qry_invoice_header_partner__invoice_line_detail__is_company_0 = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': C9_qery_match},
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",
'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$group': {
'_id': {
"Type_Client": "$order_header_is_company",
"Type_Client_Desc": "Client Type Particulier",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
bpf_c9_is_company_particulier = []
total_bpf_c9_is_company_particulier = 0
for retval in MYSY_GV.dbname['partner_invoice_header'].aggregate(
C9_pipe_qry_invoice_header_partner__invoice_line_detail__is_company_0):
print(" ### retval is_company_particulier = ", retval)
node = {}
node['Type_Client_Desc'] = retval['_id']['Type_Client_Desc']
node['TotalAmount_HT'] = retval['TotalAmount_HT']
total_bpf_c9_is_company_particulier = total_bpf_c9_is_company_particulier + mycommon.tryFloat(
retval['TotalAmount_HT'])
bpf_c9_is_company_particulier.append(node)
RetObject['bpf_c9_is_company_particulier'] = bpf_c9_is_company_particulier
RetObject['total_bpf_c9_is_company_particulier'] = round(total_bpf_c9_is_company_particulier, 2)
total_c1_C9 = total_c1_C9 + mycommon.tryFloat(total_bpf_c9_is_company_particulier)
RetObject['total_bpf_c1_c9'] = str(total_c1_C9)
print(" ### total_c1_C9 = ", total_c1_C9, ", ### total_bpf_c9_is_company_particulier = ",
total_bpf_c9_is_company_particulier)
"""
FIN TRAITEMENT DU PAVE CCCC
"""
"""
TRAITEMENT DU PAVE EEE
"""
E_pipe_qry_ressource_humaine__ressource_humaine_contrat = ([
{
"$project": {
"_id": 1, "partner_recid": 1, "nom": 1, "prenom": 1, "fonction": 1, "email": 1, "email": 1,
"email": 1, "email": 1, "email": 1,
}
},
{'$lookup': {
'from': 'ressource_humaine_contrat',
'let': {'rh_id': {'$toString': '$_id'}, 'rh_partner_owner_recid': '$partner_recid'},
'pipeline': [
{"$addFields": {
"mysy_contrat_date_debut": {
'$dateFromString': {
'dateString': '$date_debut',
'format': "%d/%m/%Y"
}
},
"mysy_contrat_date_fin": {
'$dateFromString': {
'dateString': '$date_fin',
'format': "%d/%m/%Y"
}
},
}
},
{'$match':
{'$expr':
{'$and':
[{'$eq': ['$valide', '1']},
{'$eq': ['$rh_id', '$$rh_id']},
{'$eq': ['$partner_owner_recid', '$$rh_partner_owner_recid']},
{"$and":
[
{"$gt": [
{
"$toDate": "$mysy_contrat_date_fin"
}, filt_periode_start_date_ISODATE
]},
{"$lt": [
{
"$toDate": "$mysy_contrat_date_debut"
}, filt_periode_end_date_ISODATE
]},
],
},
]
}
}
},
{
"$project": {
"_id": 1, "rh_id": 1, "date_debut": 1, "date_fin": 1, "type_contrat": 1, "type_employe": 1,
"cout": 1, "periodicite": 1, "mysy_contrat_date_debut": 1, "mysy_contrat_date_fin": 1
}
},
],
'as': 'collection_ressource_humaine_contrat'
}
},
{
'$unwind': '$collection_ressource_humaine_contrat'
},
{'$lookup': {
'from': 'agenda',
"let": {"rh_id": {'$toString': "$_id"}, 'rh_partner_owner_recid': '$partner_recid'},
'pipeline': [
{
"$addFields": {
"agenda_event_date_debut": {
"$dateFromString": {
'dateString': {"$substr": ["$event_start", 0, 10]},
"format": "%Y-%m-%d"
}
},
"agenda_event_date_fin": {
"$dateFromString": {
'dateString': {"$substr": ["$event_end", 0, 10]},
"format": "%Y-%m-%d"
}
},
}
},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$related_collection_recid", '$$rh_id']},
{'$eq': ["$partner_owner_recid", '$$rh_partner_owner_recid']},
{'$eq': ["$event_type", 'planning']},
{'$gte': [{'$toDate': "$agenda_event_date_debut"},
filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$agenda_event_date_fin"}, filt_periode_end_date_ISODATE]}
]
}
}
},
],
'as': 'collection_agenda'
}
},
])
tab_formateur_rh_id_interne = []
tab_formateur_rh_interne_nom_prenom_mail = []
tab_formateur_rh_interne_total_heure = 0
tab_formateur_rh_id_sous_traitant = []
tab_formateur_rh_sous_traitant_nom_prenom_mail = []
tab_formateur_rh_sous_traitant_total_heure = 0
print(" ### E_pipe_qry_ressource_humaine__ressource_humaine_contrat = ",
E_pipe_qry_ressource_humaine__ressource_humaine_contrat)
for retval in MYSY_GV.dbname['ressource_humaine'].aggregate(
E_pipe_qry_ressource_humaine__ressource_humaine_contrat):
# print(" ### retval (employé ayant un contrat valide) = ", retval)
formateur_id = str(retval['_id'])
formateur_data = str(retval['nom']) + " " + str(retval['prenom']) + " - " + str(
retval['email']) + " - " + str(retval['_id'])
formateur_contrat_type = ""
if ("collection_ressource_humaine_contrat" in retval.keys() and "type_contrat" in retval[
'collection_ressource_humaine_contrat'].keys()):
formateur_contrat_type = retval['collection_ressource_humaine_contrat']["type_contrat"]
total_duration = 0
for agenda_retval in retval['collection_agenda']:
# print(" ### pour formateur_data = ", formateur_data)
# print(" ### AGENDA : ", agenda_retval)
event_duration_second = datetime.strptime(str(agenda_retval['event_end'])[0:16],
'%Y-%m-%dT%H:%M') - datetime.strptime(
str(agenda_retval['event_start'])[0:16], '%Y-%m-%dT%H:%M')
event_duration_second = event_duration_second.total_seconds()
event_duration_hour = round(
divmod(event_duration_second, 3600)[0] + divmod(event_duration_second, 3600)[1] / 3600, 2)
"""
Gerer les eventuels cas d'erreur sur une date de fin event < debut event
"""
if (event_duration_hour < 0):
event_duration_hour = 0
total_duration = total_duration + event_duration_hour
formateur_data = formateur_data + " - Nb_heure = " + str(total_duration) + " heure "
if (formateur_contrat_type and formateur_contrat_type in ['vacataire', 'vacataire_qty']):
if (formateur_id not in tab_formateur_rh_id_sous_traitant):
tab_formateur_rh_id_sous_traitant.append(str(retval['_id']))
tab_formateur_rh_sous_traitant_nom_prenom_mail.append(str(formateur_data))
tab_formateur_rh_sous_traitant_total_heure = tab_formateur_rh_sous_traitant_total_heure + total_duration
elif (formateur_contrat_type and formateur_contrat_type not in ['vacataire', 'vacataire_qty']):
if (formateur_id not in tab_formateur_rh_id_interne):
tab_formateur_rh_id_interne.append(str(retval['_id']))
tab_formateur_rh_interne_nom_prenom_mail.append(str(formateur_data))
tab_formateur_rh_interne_total_heure = tab_formateur_rh_interne_total_heure + total_duration
"""
RetObject['tab_formateur_rh_id_interne'] = tab_formateur_rh_id_interne
RetObject['tab_formateur_rh_interne_nom_prenom_mail'] = tab_formateur_rh_interne_nom_prenom_mail
RetObject['tab_formateur_rh_interne_total_heure'] = tab_formateur_rh_interne_total_heure
"""
RetObject['bpf_e1_nb_formateurs_internee'] = str(len(tab_formateur_rh_id_interne))
RetObject['bpf_e1_nb_heures_formateurs_internes'] = tab_formateur_rh_interne_total_heure
"""
RetObject['tab_formateur_rh_id_sous_traitant'] = tab_formateur_rh_id_sous_traitant
RetObject['tab_formateur_rh_sous_traitant_nom_prenom_mail'] = tab_formateur_rh_sous_traitant_nom_prenom_mail
RetObject['tab_formateur_rh_sous_traitant_total_heure'] = tab_formateur_rh_sous_traitant_total_heure
"""
RetObject['bpf_e2_nb_formateurs_externes'] = str(len(tab_formateur_rh_id_sous_traitant))
RetObject['bpf_e2_nb_heures_formateurs_externes'] = tab_formateur_rh_sous_traitant_total_heure
"""
FIN TRAITEMENT DU PAVE EEEE
"""
"""
TRAITEMENT DU PAVE FFFF
"""
F3_qery_match = {'$and': [{"partner_owner_recid": str(my_partner['recid'])}, {"valide": '1', 'status': '1'},
]}
F3_pipe_qry_stagiaire_par_class_level = ([
{'$project': {'_id': 1, 'email': 1, 'session_id': 1, 'nom': 1, 'prenom': 1, 'partner_owner_recid': 1,
'valide': 1, 'type_apprenant': 1, 'status': 1}},
{'$match': F3_qery_match},
{'$lookup': {
'from': 'session_formation',
'let': {'session_id': "$session_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [
{
"$addFields": {
"mysy_date_debut_session": {
"$dateFromString": {
"dateString": "$date_debut",
"format": "%d/%m/%Y"
}
}
}
},
{'$project': {'_id': 1, 'code_session': 1, 'class_internal_url': 1, 'date_debut': 1, 'date_fin': 1,
'nb_participant': 1, 'partner_owner_recid': 1, 'valide': 1,
'mysy_date_debut_session': 1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$session_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
{'$gte': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$mysy_date_debut_session"}, filt_periode_end_date_ISODATE]},
]
}
}
},
],
'as': 'session_formation_collection'
}
},
{
'$unwind': '$session_formation_collection'
},
{
"$lookup": {
'from': 'myclass',
'localField': 'session_formation_collection.class_internal_url',
'foreignField': 'internal_url',
"pipeline": [
{'$project': {'title': 1, 'internal_url': 1, 'external_code': 1, 'published': 1,
'duration': 1, 'duration_unit': 1, 'class_level': 1, 'partner_owner_recid': 1}},
{'$match': {'partner_owner_recid': str(my_partner['recid'])}},
],
"as": "myclass_collection"
}
},
{
'$unwind': '$myclass_collection'
},
{'$lookup': {
'from': 'class_niveau_formation',
"let": {'class_level_code': "$myclass_collection.class_level",
},
'pipeline': [
{'$project': {'_id': 1, 'code': 1, 'description': 1, 'valide': 1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$code", '$$class_level_code']},
]
}
}
},
],
'as': 'class_niveau_formation_collection'
}
},
{
'$unwind': '$class_niveau_formation_collection'
},
{'$group': {
'_id': {
"Class_Level_Code": "$class_niveau_formation_collection.code",
"Class_Level_Desc": "$class_niveau_formation_collection.description",
},
"TotalAmount_HT": {
"$sum": 1},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
print(" ### F3_pipe_qry_stagiaire_par_class_level = ", F3_pipe_qry_stagiaire_par_class_level)
bpf_f3_class_niveau_formation = []
total_bpf_f3_class_niveau_formation = 0
"""
Recuperer la configuration de conversion des semaines, mois, années en nombre d'heures
"""
local_conversion_heure_status, local_nb_heure_par_jour, local_nb_heure_par_sem, local_nb_heure_par_mois, local_nb_heure_par_annee = mycommon.Get_Config_Conversion_Heures(
str(my_partner['recid']))
for retval in MYSY_GV.dbname['inscription'].aggregate(F3_pipe_qry_stagiaire_par_class_level):
# print(" ### retval = ", retval)
node = {}
node['Class_Level_Desc'] = retval['_id']['Class_Level_Desc']
node['Nb_Apprenant_By_Class_Level'] = retval['count']
"""
Aller chercher le nombre d'heures de formation des formations concernées par niveau
"""
pipe_concerned_class = ([
{'$project': {'_id': 1, 'email': 1, 'session_id': 1, 'nom': 1, 'prenom': 1, 'partner_owner_recid': 1,
'valide': 1, 'type_apprenant': 1, 'status': 1}},
{'$match': F3_qery_match},
{'$lookup': {
'from': 'session_formation',
'let': {'session_id': "$session_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [
{
"$addFields": {
"mysy_date_debut_session": {
"$dateFromString": {
"dateString": "$date_debut",
"format": "%d/%m/%Y"
}
}
}
},
{'$project': {'_id': 1, 'code_session': 1, 'class_internal_url': 1, 'date_debut': 1,
'date_fin': 1,
'nb_participant': 1, 'partner_owner_recid': 1, 'valide': 1,
'mysy_date_debut_session': 1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$session_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
{'$gte': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_end_date_ISODATE]},
]
}
}
},
],
'as': 'session_formation_collection'
}
},
{
'$unwind': '$session_formation_collection'
},
{
"$lookup": {
'from': 'myclass',
'localField': 'session_formation_collection.class_internal_url',
'foreignField': 'internal_url',
"pipeline": [
{'$project': {'_id': 1, 'title': 1, 'internal_url': 1, 'external_code': 1, 'published': 1,
'duration': 1, 'duration_unit': 1, 'class_level': 1,
'partner_owner_recid': 1}},
{'$match': {'class_level': str(retval['_id']['Class_Level_Code']),
'partner_owner_recid': str(my_partner['recid'])}},
],
"as": "myclass_collection"
}
},
{
'$unwind': '$myclass_collection'
},
])
# print(" ### pipe_concerned_class = ", pipe_concerned_class)
tab_class_info = []
tab_class_id = []
total_nb_heure_formation_by_level = 0
if (local_conversion_heure_status is True):
for retval_pipe_concerned_class in MYSY_GV.dbname['inscription'].aggregate(pipe_concerned_class):
# print(" ### retval_pipe_concerned_class = ", retval_pipe_concerned_class)
if ("myclass_collection" in retval_pipe_concerned_class.keys() and
"duration" in retval_pipe_concerned_class['myclass_collection'].keys() and
retval_pipe_concerned_class['myclass_collection']['duration'] and
"duration_unit" in retval_pipe_concerned_class['myclass_collection'].keys() and
retval_pipe_concerned_class['myclass_collection']['duration_unit']):
local_duration = mycommon.tryFloat(
str(retval_pipe_concerned_class['myclass_collection']['duration']))
local_duration_unit = str(retval_pipe_concerned_class['myclass_collection']['duration_unit'])
# print(" ### local_duration = ", str(local_duration), " duration_unit = ", str(local_duration_unit) )
if (str(retval_pipe_concerned_class['myclass_collection']['_id']) not in tab_class_id):
tab_class_id.append((str(retval_pipe_concerned_class['myclass_collection']['_id'])))
local_node = {}
local_node['class_id'] = str(retval_pipe_concerned_class['myclass_collection']['_id'])
local_node['class_duration'] = str(local_duration)
local_node['class_duration_unit'] = local_duration_unit
tab_class_info.append(local_node)
for local_retval in tab_class_info:
# print( " ### local_retval = ",local_retval)
local_duration = mycommon.tryFloat(str(local_retval['class_duration']))
local_duration_unit = str(local_retval['class_duration_unit'])
if (str(local_duration_unit) == "heure"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + local_duration
elif (str(local_duration_unit) == "jour"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + (
local_duration * local_nb_heure_par_jour)
elif (str(local_duration_unit) == "semaine"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + (
local_duration * local_nb_heure_par_sem)
elif (str(local_duration_unit) == "mois"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + (
local_duration * local_nb_heure_par_mois)
elif (str(local_duration_unit) == "annee"):
total_nb_heure_formation_by_level = total_nb_heure_formation_by_level + (
local_duration * local_nb_heure_par_annee)
node['total_nb_heure_formation_by_level'] = str(total_nb_heure_formation_by_level)
total_bpf_f3_class_niveau_formation = total_bpf_f3_class_niveau_formation + mycommon.tryFloat(
retval['count'])
bpf_f3_class_niveau_formation.append(node)
RetObject['bpf_f3_class_niveau_formation'] = bpf_f3_class_niveau_formation
RetObject['total_bpf_f3_class_niveau_formation'] = round(total_bpf_f3_class_niveau_formation, 2)
""""
F1 Les différents types de stagiaires au sein de votre organisme
"""
"""
Recuperation des types d'apprenant
"""
bpf_f1_type_apprenant = []
nb_personne_a_distance = 0
nb_personne = 0
nb_heure_formation = 0
for type_apprenant_data in MYSY_GV.dbname['type_apprenant'].find({'valide': '1', 'locked': '0'}):
F1_qery_match = {'$and': [{"partner_owner_recid": str(my_partner['recid'])}, {"valide": '1', 'status': '1',
'type_apprenant': str(
type_apprenant_data[
'code'])},
]}
F1_pipe_qry_stagiaire_type = ([
{'$project': {'_id': 1, 'email': 1, 'session_id': 1, 'nom': 1, 'prenom': 1, 'partner_owner_recid': 1,
'valide': 1, 'type_apprenant': 1, 'status': 1, }},
{'$match': F1_qery_match},
{'$lookup': {
'from': 'session_formation',
'let': {'session_id': "$session_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [
{
"$addFields": {
"mysy_date_debut_session": {
"$dateFromString": {
"dateString": "$date_debut",
"format": "%d/%m/%Y"
}
}
}
},
{'$project': {'_id': 1, 'code_session': 1, 'class_internal_url': 1, 'date_debut': 1,
'date_fin': 1,
'nb_participant': 1, 'partner_owner_recid': 1, 'valide': 1,
'mysy_date_debut_session': 1, 'distantiel': 1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$session_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']},
{'$gte': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_start_date_ISODATE]},
{'$lt': [{'$toDate': "$mysy_date_debut_session"},
filt_periode_end_date_ISODATE]},
]
}
}
},
],
'as': 'session_formation_collection'
}
},
{
'$unwind': '$session_formation_collection'
},
{
"$lookup": {
'from': 'myclass',
'localField': 'session_formation_collection.class_internal_url',
'foreignField': 'internal_url',
"pipeline": [
{'$project': {'title': 1, 'internal_url': 1, 'external_code': 1, 'published': 1,
'duration': 1, 'duration_unit': 1, 'class_level': 1,
'partner_owner_recid': 1}},
{'$match': {'partner_owner_recid': str(my_partner['recid'])}},
],
"as": "myclass_collection"
}
},
{
'$unwind': '$myclass_collection'
},
{
"$group": {
"_id": {
"class_id": "$myclass_collection._id",
"class_external_code": "$myclass_collection.external_code",
"class_duration": "$myclass_collection.duration",
"class_duration_unit": "$myclass_collection.duration_unit",
"session_distance": "$session_formation_collection.distantiel"
},
"count": {
"$sum": 1
}
}
},
])
print(" ### F1_pipe_qry_stagiaire_type pour : " + str(type_apprenant_data['description']) + " = ",
F1_pipe_qry_stagiaire_type)
total_bpf_f1_type_apprenant = 0
total_bpf_f1_nb_heure_formation = 0
node = {}
node['Type_Apprenant'] = str(type_apprenant_data['code'])
node['Type_Apprenant_Desc'] = str(type_apprenant_data['description'])
local_f1_nb_distance = 0
for f1_local_retval in MYSY_GV.dbname['inscription'].aggregate(F1_pipe_qry_stagiaire_type):
# print(" ### retval F1_pipe_qry_stagiaire_type = ", f1_local_retval)
total_bpf_f1_type_apprenant = total_bpf_f1_type_apprenant + mycommon.tryFloat(f1_local_retval['count'])
nb_personne = nb_personne + + mycommon.tryFloat(f1_local_retval['count'])
if (str(f1_local_retval['_id']['session_distance']) == "1"):
nb_personne_a_distance = nb_personne_a_distance + mycommon.tryFloat(f1_local_retval['count'])
local_f1_nb_distance = local_f1_nb_distance + mycommon.tryFloat(f1_local_retval['count'])
print(" ### nb_personne_a_distance= ", str(nb_personne_a_distance))
print(" ### local_f1_nb_distance= ", str(local_f1_nb_distance))
"""
traitement du nombre d'heure de formation
"""
local_duration = mycommon.tryFloat(str(f1_local_retval['_id']['class_duration']))
local_duration_unit = str(f1_local_retval['_id']['class_duration_unit'])
if (str(local_duration_unit) == "heure"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + local_duration
elif (str(local_duration_unit) == "jour"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_jour)
elif (str(local_duration_unit) == "semaine"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_sem)
elif (str(local_duration_unit) == "mois"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_mois)
elif (str(local_duration_unit) == "annee"):
total_bpf_f1_nb_heure_formation = total_bpf_f1_nb_heure_formation + (
local_duration * local_nb_heure_par_annee)
node['total_bpf_f1_type_apprenant'] = str(total_bpf_f1_type_apprenant)
node['total_bpf_f1_type_apprenant_distance'] = str(local_f1_nb_distance)
node['total_bpf_f1_nb_heure_formation'] = str(total_bpf_f1_nb_heure_formation)
nb_heure_formation = nb_heure_formation + mycommon.tryFloat(total_bpf_f1_nb_heure_formation)
bpf_f1_type_apprenant.append(node)
RetObject['total_f1_apprenant_a_distance'] = str(nb_personne_a_distance)
RetObject['total_f1_apprenant'] = str(nb_personne)
RetObject['total_f1_nb_heure'] = str(nb_heure_formation)
RetObject['filt_periode_start_date'] = str(filt_periode_start_date)
RetObject['filt_periode_end_date'] = str(filt_periode_end_date)
RetObject['bpf_f1_type_apprenant'] = bpf_f1_type_apprenant
print(" ### RetObject BPF = ", RetObject)
# Creation du dictionnaire d'information à utiliser pour la creation du doc
convention_dictionnary_data = {}
new_diction = {}
new_diction['token'] = diction['token']
new_diction['list_stagiaire_id'] = []
new_diction['list_session_id'] = []
new_diction['list_class_id'] = []
new_diction['list_client_id'] = []
new_diction['list_apprenant_id'] = []
local_status, local_retval = mycommon.Get_Dictionnary_data_For_Template(new_diction)
if (local_status is False):
return local_status, local_retval
convention_dictionnary_data = local_retval
convention_dictionnary_data['bpf_data'] = RetObject
body = {
"params": convention_dictionnary_data,
}
#json_formatted_str = json.dumps(body, indent=2)
#print(" ### AFFICHAGE dU data dictionnary -- body body ")
#print(json_formatted_str)
"""
Recuperer le modèle de document
"""
local_diction = {}
local_diction['ref_interne'] = "BPF_MODEL"
local_diction['type_doc'] = "pdf"
local_diction['partner_owner_recid'] = str(my_partner['recid'])
courrier_data_status, courrier_data_retval = mycommon.Get_Courrier_Template_Include_Default_Data(local_diction)
if (courrier_data_status is False):
return courrier_data_status, courrier_data_retval
if ("contenu_doc" not in courrier_data_retval.keys() or str(courrier_data_retval['contenu_doc']) == ""):
mycommon.myprint(
str(inspect.stack()[0][
3]) + " Le modèle de courrier 'BPF_MODEL' n'est pas correctement configuré ")
return False, " Le modèle de courrier 'BPF_MODEL' n'est pas correctement configuré "
"""
Creation du fichier PDF
"""
contenu_doc_Template = jinja2.Template(str(courrier_data_retval['contenu_doc']))
sourceHtml = contenu_doc_Template.render(params=body["params"])
todays_date = str(date.today().strftime("%d/%m/%Y"))
ts = datetime.now().timestamp()
ts = str(ts).replace(".", "").replace(",", "")[-3:]
tmp_file_name = "BPF_"+str(my_partner['nom'])
if (len(str(tmp_file_name)) > 30):
tmp_file_name = str(tmp_file_name)[0:30]
orig_file_name = str(tmp_file_name) + "_" + str(ts) + ".pdf"
outputFilename = str(MYSY_GV.TEMPORARY_DIRECTORY) + "/" + str(orig_file_name)
# open output file for writing (truncated binary)
resultFile = open(outputFilename, "w+b")
# convert HTML to PDF
pisaStatus = pisa.CreatePDF(
src=sourceHtml, # the HTML to convert
dest=resultFile) # file handle to receive result
# close output file
resultFile.close()
# print(" ### outputFilename = "+str(outputFilename))
if os.path.exists(outputFilename):
# print(" ### ok os.path.exists(outputFilename) "+str(outputFilename))
return True, send_file(outputFilename, as_attachment=True)
# return True on success and False on errors
print(pisaStatus.err, type(pisaStatus.err))
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_partner_invoice_header_work(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 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
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)
C2_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_organisme_financement__type_apprenant = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': {'invoice_header_type':'facture',
'credit_note_ref': {'$exists': False},
'invoice_header_ref_interne':{'$in':['Invoice_567', 'Invoice_568', 'Invoice_569', 'Invoice_570', 'Invoice_571', 'Invoice_572', 'Invoice_573' ]} } },
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$lookup': {
'from': 'type_organisme_financement',
"let": {'type_financeur_id': "$order_header_type_financeur_id",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$type_financeur_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
]
}
}
},
],
'as': 'type_organisme_financement_collection'
}
},
{
'$unwind': '$type_organisme_financement_collection'
},
{'$lookup': {
'from': 'type_apprenant',
"let": {'type_apprenant_code': "$collection_partner_invoice_line.order_line_inscription_type_apprenant",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$code",'$$type_apprenant_code']},
]
}
}
},
],
'as': 'type_apprenante_collection'
}
},
{
'$unwind': '$type_apprenante_collection'
},
{'$group': {
'_id': {
"Type Apprenant": "$collection_partner_invoice_line.order_line_inscription_type_apprenant",
"Type Apprenant Description": "$type_apprenante_collection.description",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
C3_C8_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_pouvoir_public = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': {'invoice_header_type':'facture',
'credit_note_ref': {'$exists': False},
'invoice_header_ref_interne':{'$in':['Invoice_588', 'Invoice_589', 'Invoice_590', 'Invoice_591', 'Invoice_592', 'Invoice_593', 'Invoice_594', 'Invoice_595' ]} } },
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$lookup': {
'from': 'type_pouvoir_public',
"let": {'order_header_type_pouvoir_public_id': "$order_header_type_pouvoir_public_id",
},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$order_header_type_pouvoir_public_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
]
}
}
},
],
'as': 'type_pouvoir_public_collection'
}
},
{
'$unwind': '$type_pouvoir_public_collection'
},
{'$group': {
'_id': {
"Type pouvoir public": "$type_pouvoir_public_idt",
"Type pouvoir public": "$type_pouvoir_public_collection.description",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
C9_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_pouvoir_public = ([
{"$addFields": {
"mysy_invoice_date": {
'$dateFromString': {
'dateString': '$invoice_date',
'format': "%d/%m/%Y"
}
}
}
},
{'$match': {'invoice_header_type':'facture',
'credit_note_ref': {'$exists': False},
'order_header_is_company':'0',
'invoice_header_ref_interne':{'$in':['Invoice_588', 'Invoice_589', 'Invoice_590', 'Invoice_591', 'Invoice_592', 'Invoice_593', 'Invoice_594', 'Invoice_595' ]} } },
{'$lookup': {
'from': 'partner_invoice_line_detail',
"let": {"invoice_header_id": {'$toString': "$_id"},
'partner_invoice_line_partner_owner_recid': '$partner_owner_recid',
'partner_invoice_line_invoice_header_ref_interne': '$invoice_header_ref_interne'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$order_line_is_include_bpf", "1"]},
{'$eq': ["$invoice_header_ref_interne",'$$partner_invoice_line_invoice_header_ref_interne']},
{'$eq': ["$partner_owner_recid", '$$partner_invoice_line_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_partner_invoice_line'
}
},
{
'$unwind': '$collection_partner_invoice_line'
},
{'$group': {
'_id': {
"Type Client": "$order_header_is_company",
"Type Client public": "Client Type Particulier",
},
"TotalAmount_HT": {
"$sum": {'$toDouble': '$collection_partner_invoice_line.order_line_invoiced_amount'}},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
E_pipe_qry_ressource_humaine__ressource_humaine_contrat__partner_agenda = ([
{'$project': {'list_competence': 0, }},
{'$lookup': {
'from': 'ressource_humaine_contrat',
'let': {'rh_id': {'$toString': '$_id'}, 'rh_partner_owner_recid': '$partner_recid'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ['$valide', '1']},
{'$eq': ['$rh_id', '$$rh_id']},
{'$eq': ['$partner_owner_recid', '$$rh_partner_owner_recid']}
]
}
}
},
{"$project": {"_id": 0, }}
],
'as': 'collection_ressource_humaine_contrat'
}
},
{
'$unwind': '$collection_ressource_humaine_contrat'
},
{'$lookup': {
'from': 'agenda',
"let": {"rh_id": {'$toString': "$_id"}, 'rh_partner_owner_recid':'$partner_recid'},
'pipeline': [
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$related_collection_recid", '$$rh_id']},
{'$eq': ["$partner_owner_recid", '$$rh_partner_owner_recid']},
]
}
}
},
],
'as': 'collection_agenda'
}
},
])
F1_pipe_qry_invoice_header_partner__invoice_line_detail__partner_type_pouvoir_public = ([
{"$addFields": {
"mysy_date_debut_session": {
'$dateFromString': {
'dateString': '$date_du',
'format': "%d/%m/%Y"
}
}
}
},
{'$project': {'_id': 1, 'email': 1, 'session_id': 1, 'nom': 1, 'prenom':1, 'partner_owner_recid':1, 'valide':1, 'type_apprenant':1}},
{'$match': {'session_id':'66cdeb5d7e8061b7b6f9b066'} },
{'$lookup': {
'from': 'session_formation',
'let': {'session_id': "$session_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [
{'$project': {'_id': 1, 'code_session': 1, 'class_internal_url': 1, 'date_debut': 1, 'date_fin':1, 'nb_participant':1, 'partner_owner_recid':1, 'valide':1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$_id", {'$convert': {
'input': "$$session_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
{'$eq': ["$valide", "1"]},
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}
]
}
}
},
],
'as': 'session_formation_collection'
}
},
{
'$unwind': '$session_formation_collection'
},
{
"$lookup": {
'from': 'myclass',
'localField': 'session_formation_collection.class_internal_url',
'foreignField': 'internal_url',
"pipeline": [{'$project': {'title': 1, 'internal_url': 1, 'external_code': 1, 'published': 1, 'duration':1, 'duration_unit':1}}
],
"as": "myclass_collection"
}
},
{
'$unwind': '$myclass_collection'
},
{'$lookup': {
'from': 'type_apprenant',
"let": {'type_apprenant_code': "$type_apprenant",
},
'pipeline': [
{'$project': {'_id': 1, 'code': 1, 'description': 1, 'valide':1}},
{'$match':
{'$expr':
{'$and':
[
{'$eq': ["$valide", "1"]},
{'$eq': ["$code",'$$type_apprenant_code']},
]
}
}
},
],
'as': 'type_apprenante_collection'
}
},
{
'$unwind': '$type_apprenante_collection'
},
{'$group': {
'_id': {
"Type Apprenant": "$type_apprenant",
"Type Apprenant Description": "$type_apprenante_collection.description",
},
"TotalAmount_HT": {
"$sum": 1},
"count": {"$sum": 1}
}
},
{
'$sort': {'count': -1}
},
])
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 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
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 "