"""
Ce fichier traite de tous les sujets concenant l'editique
C'est a dire :
- Quel document à imprimer
- Quand il a été imprimé ou téléchargé, et
"""
import bson
import pymongo
from pymongo import MongoClient
import json
from bson import ObjectId
import re
from datetime import datetime
import prj_common as mycommon
import secrets
import inspect
import sys, os
import csv
import pandas as pd
from pymongo import ReturnDocument
import GlobalVariable as MYSY_GV
from math import isnan
import GlobalVariable as MYSY_GV
import ela_index_bdd_classes as eibdd
import email_mgt as email
import jinja2
from flask import send_file
from xhtml2pdf import pisa
from email.message import EmailMessage
from email.mime.text import MIMEText
from email import encoders
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

"""
Cette fonction retourne la liste de documents editable par une 
- un partner
"""
def Get_Editable_Document_By_Partner(diction):
    try:
        diction = mycommon.strip_dictionary(diction)

        """
        Verification des input acceptés
        """
        field_list = ['token', ]

        incom_keys = diction.keys()
        for val in incom_keys:
            if val not in field_list and val.startswith('my_') is False:
                mycommon.myprint(str(
                    inspect.stack()[0][3]) + "  Le champ  '" + val + "' n'est pas autorisé")
                return False, " Les informations fournies sont incorrectes"

        """
        Verification des champs obligatoires
        """
        field_list_obligatoire = ['token',]

        for val in field_list_obligatoire:
            if val not in diction:
                mycommon.myprint(
                    str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
                return False, " Les informations fournies sont incorrectes"

        """
                Verification de l'identité et autorisation de l'entité qui
                appelle cette API
                """
        token = ""
        if ("token" in diction.keys()):
            if diction['token']:
                token = diction['token']

        local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
        if (local_status is not True):
            return local_status, my_partner

        RetObject = []
        val_tmp = 0

        qry_match = {"partner_owner_recid":str(my_partner['recid']), 'valide':'1', 'locked':'0'}

        pipe_qry = ([
            {'$match': qry_match},
            {'$sort': {'_id': -1}},
            {'$lookup': {
                'from': 'courrier_template',
                "let": {'courrier_template_id': "$courrier_template_id", 'partner_owner_recid': '$partner_owner_recid'},
                'pipeline': [
                    {'$match':
                        {'$expr':
                            {'$and':
                                [

                                    {'$eq': ["$_id", {'$convert': {
                                        'input': "$$courrier_template_id",
                                        'to': "objectId",
                                        'onError': {'error': 'true'},
                                        'onNull': {'isnull': 'true'}
                                    }}]},

                                    {'$eq': ["$valide", "1"]},
                                    {'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}

                                ]
                            }
                        }
                    },

                    {'$project': {'contenu_doc': 0, 'corps_mail': 0}},
                ],
                'as': 'collection_courrier_template'
            }
            },
            {
                '$unwind': '$collection_courrier_template'
            }
        ])

        print(" #### Get_Editable_Document_By_Partner pipe_qry_Get_Editable_Document_By_Partner = ", pipe_qry)

        for New_retVal in MYSY_GV.dbname['courrier_template_tracking'].aggregate(pipe_qry):
            if( "collection_courrier_template" in New_retVal.keys() ):
                user = New_retVal
                user['id'] = str(val_tmp)
                val_tmp = val_tmp + 1
                RetObject.append(mycommon.JSONEncoder().encode(user))


        return True, RetObject

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
        return False, " Impossible de récupérer la liste des documents éditables "


"""
Cette fonction retourne la liste de documents editable par une 
- un partner
- une collection (ex : session_formation).

important : ici on recuperer juste la liste des document tracké, pas l'historique de traitement
"""

def Get_Editable_Document_By_Partner_By_Collection(diction):
    try:
        diction = mycommon.strip_dictionary(diction)

        """
        Verification des input acceptés
        """
        field_list = ['token', 'related_collection', 'related_collection_recid']

        incom_keys = diction.keys()
        for val in incom_keys:
            if val not in field_list and val.startswith('my_') is False:
                mycommon.myprint(str(
                    inspect.stack()[0][3]) + "  Le champ  '" + val + "' n'est pas autorisé")
                return False, " Les informations fournies sont incorrectes"

        """
        Verification des champs obligatoires
        """
        field_list_obligatoire = ['token', 'related_collection']

        for val in field_list_obligatoire:
            if val not in diction:
                mycommon.myprint(
                    str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
                return False, " Les informations fournies sont incorrectes"

        """
                Verification de l'identité et autorisation de l'entité qui
                appelle cette API
                """
        token = ""
        if ("token" in diction.keys()):
            if diction['token']:
                token = diction['token']

        related_collection = ""
        if ("related_collection" in diction.keys()):
            if diction['related_collection']:
                related_collection = diction['related_collection']

        if( str(related_collection).strip() == ""):
            mycommon.myprint(
                str(inspect.stack()[0][3]) + " Le nom de la collection est invalide ")
            return False, " Le nom de la collection est invalide "


        local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
        if (local_status is not True):
            return local_status, my_partner

        RetObject = []
        val_tmp = 0

        qry_match = {"partner_owner_recid": str(my_partner['recid']), 'valide': '1', 'locked': '0', 'related_collection':str(related_collection)}

        pipe_qry = ([
            {'$match': qry_match},
            {'$sort': {'rang': 1}},
            {'$lookup': {
                'from': 'courrier_template_type_document',
                "let": {'courrier_template_type_document_ref_interne': '$courrier_template_type_document_ref_interne',
                        'partner_owner_recid': '$partner_owner_recid'},
                'pipeline': [
                    {'$match':
                        {'$expr':
                            {'$and':
                                [

                                    {'$eq': ["$ref_interne", '$$courrier_template_type_document_ref_interne']},

                                    {'$eq': ["$valide", "1"]},
                                    {'$eq': ["$partner_owner_recid", 'default']}

                                ]
                            }
                        }
                    },

                    {'$project': {'contenu_doc': 0, 'corps_mail': 0}},
                ],
                'as': 'collection_courrier_template'
            }
            },
            {
                '$unwind': '$collection_courrier_template'
            },
            {'$sort': {"collection_courrier_template.rang": 1}},
        ])

        #print(" #### Get_Editable_Document_By_Partner_By_Collection pipe_qry_Get_Editable_Document_By_Partner = ", pipe_qry)
        json_formatted_str = json.dumps(pipe_qry, indent=2)
        #print(" #### Get_Editable_Document_By_Partner_By_Collection pipe_qry_Get_Editable_Document_By_Partner = ", json_formatted_str)


        for New_retVal in MYSY_GV.dbname['courrier_template_tracking'].aggregate(pipe_qry):
            if ("collection_courrier_template" in New_retVal.keys()):
                user = {}
                user['_id'] = str(New_retVal['_id'])
                user['id'] = str(val_tmp)

                user['courrier_template_id'] = ""
                if('courrier_template_id' in New_retVal.keys()):
                    user['courrier_template_id'] = str(New_retVal['courrier_template_id'])

                user['rang'] = "99"
                if ('rang' in New_retVal.keys()):
                    user['rang'] = str(New_retVal['rang'])

                user['server_action_name'] = ""
                if ('server_action_name' in New_retVal.keys()):
                    user['server_action_name'] = str(New_retVal['server_action_name'])

                user['partner_owner_recid'] = ""
                if ('partner_owner_recid' in New_retVal.keys()):
                    user['partner_owner_recid'] = str(New_retVal['partner_owner_recid'])

                user['related_collection'] = ""
                if ('related_collection' in New_retVal.keys()):
                    user['related_collection'] = str(New_retVal['related_collection'])

                user['courrier_template_ref_interne'] = ""
                if ('courrier_template_ref_interne' in New_retVal.keys()):
                    user['courrier_template_ref_interne'] = str(New_retVal['courrier_template_ref_interne'])

                user['courrier_template_ref_interne'] = ""
                if ('ref_interne' in New_retVal['collection_courrier_template'].keys()):
                    user['courrier_template_ref_interne'] = str(New_retVal['collection_courrier_template']['ref_interne'])

                user['courrier_template_nom'] = ""
                if ('nom' in New_retVal['collection_courrier_template'].keys()):
                    user['courrier_template_nom'] = str(
                        New_retVal['collection_courrier_template']['nom'])

                user['courrier_template_sujet'] = ""
                if ('sujet' in New_retVal['collection_courrier_template'].keys()):
                    user['courrier_template_sujet'] = str(
                        New_retVal['collection_courrier_template']['sujet'])

                user['courrier_template_cible'] = ""
                if ('cible' in New_retVal['collection_courrier_template'].keys()):
                    user['courrier_template_cible'] = str(
                        New_retVal['collection_courrier_template']['cible'])

                user['courrier_template_type_doc'] = ""
                if ('type_doc' in New_retVal['collection_courrier_template'].keys()):
                    user['courrier_template_type_doc'] = str(
                        New_retVal['collection_courrier_template']['type_doc'])

                list_document_history_event = []
                has_history_event = "0"
                if( "related_collection_recid" in diction.keys() and diction['related_collection_recid']):
                    """
                    Verifier si document a un historic pour le related_collection_recid.
                    Cela permet de savoir si pour l'entité qui appelle cette fonction, ce document a déjà été
                    traité
                    """
                    local_qry = {'partner_owner_recid':str(my_partner['recid']),
                                 'related_collection_recid':str(diction['related_collection_recid']),
                                 'courrier_template_tracking_id':str(New_retVal['_id']),
                                 'valide':'1',
                                 'locked':'0'}

                    #print(" #### is_document_has_history_event qry = ", local_qry)

                    is_document_has_history_event = MYSY_GV.dbname['courrier_template_tracking_history'].count_documents(local_qry)
                    local_related_collection = ""
                    local_related_collection_id = ""
                    local_related_collection_name = ""

                    if( is_document_has_history_event > 0 ):
                        has_history_event = "1"
                        for val in MYSY_GV.dbname['courrier_template_tracking_history'].find(local_qry):
                            #print(" val 1 = ", val)

                            if( val and "courrier_template_id" in val.keys() and "related_collection_recid" in val.keys() ):
                                local_courrier_template_id = val['courrier_template_id']
                                if( ObjectId.is_valid(local_courrier_template_id)):
                                    courrier_template_data = MYSY_GV.dbname['courrier_template'].find_one({'_id':ObjectId(str(local_courrier_template_id))},
                                                                                                          {'contenu_doc':'0'})

                                related_collection_data = MYSY_GV.dbname[related_collection].find_one({'_id':ObjectId(str(diction['related_collection_recid']))})
                                #print(" #### SESSSION DATA = ", related_collection_data)

                                local_related_collection = "Session Formation"
                                local_related_collection_id = str(val['related_collection_recid'])
                                local_related_collection_name = str(related_collection_data['code_session'])

                            val['local_related_collection'] = local_related_collection
                            val['local_related_collection_id'] = local_related_collection_id
                            val['local_related_collection_name'] = local_related_collection_name

                            """
                            Recuperation des infos de la cible pour qui l'action a été faite.
                            """
                            local_target_collection = ""
                            local_target_collection_id = ""
                            local_target_collection_name = ""

                            if( val and "target_collection" in val.keys() and val['target_collection'] and "target_collection_recid" in val.keys() and val['target_collection_recid']):

                                if( str(val['target_collection']) == "inscription"):
                                    local_target_collection = str(val['target_collection'])
                                    local_target_collection_id = str(val['target_collection_recid'])
                                    local_target_collection_data = MYSY_GV.dbname[str(val['target_collection'])].find_one({'_id':ObjectId(str(val['target_collection_recid'])),
                                                                                                                           'partner_owner_recid':str(my_partner['recid'])})
                                    if( local_target_collection_data and "email" in local_target_collection_data.keys()):
                                        local_target_collection_name = local_target_collection_data['email']

                                elif (str(val['target_collection']) == "emargement"):
                                    local_target_collection = str(val['target_collection'])
                                    local_target_collection_id = str(val['target_collection_recid'])
                                    local_target_collection_data = MYSY_GV.dbname[str(val['target_collection'])].find_one(
                                        {'_id': ObjectId(str(val['target_collection_recid'])),
                                         'partner_owner_recid': str(my_partner['recid'])})
                                    if (local_target_collection_data and "email" in local_target_collection_data.keys()):
                                        local_target_collection_name = local_target_collection_data['email']


                            val['local_target_collection'] = local_target_collection
                            val['local_target_collection_id'] = local_target_collection_id
                            val['local_target_collection_name'] = local_target_collection_name


                            local_update_by_email = ""
                            if( "update_by" in val.keys()):
                                update_by_data = MYSY_GV.dbname['partnair_account'].find_one({'_id':ObjectId(val['update_by']),
                                                                                              'recid':my_partner['recid']})
                                if( "email" in update_by_data ):
                                    local_update_by_email = update_by_data['email']

                            val['local_update_by_email'] = local_update_by_email

                            #print(" ### VALLL = ", val)
                            list_document_history_event.append(val)

                            #print(" #### is_document_has_history_event RESULT = ", is_document_has_history_event)

                user['has_history_event'] = has_history_event
                user['list_document_history_event'] = list_document_history_event

                #print(" #### user['list_document_history_event']  = ", user['list_document_history_event'] )

                val_tmp = val_tmp + 1
                RetObject.append(mycommon.JSONEncoder().encode(user))

        return True, RetObject

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
        return False, " Impossible de récupérer la liste des documents éditables "


"""
Cette fonction retourne l'HISTORIQUE des telechargements et envois de mail 
des documents tracés, pour :
- un partner (Obligatoire)
- un related_collection_recid (Obligatoire) (ex : l'_id de la session)

"""

def Get_Editable_History_Document_By_Partner_By_Collection(diction):
    try:
        diction = mycommon.strip_dictionary(diction)

        """
        Verification des input acceptés
        """
        field_list = ['token', 'related_collection_id']

        incom_keys = diction.keys()
        for val in incom_keys:
            if val not in field_list and val.startswith('my_') is False:
                mycommon.myprint(str(
                    inspect.stack()[0][3]) + "  Le champ  '" + val + "' n'est pas autorisé")
                return False, " Les informations fournies sont incorrectes"

        """
        Verification des champs obligatoires
        """
        field_list_obligatoire = ['token', 'related_collection_id']

        for val in field_list_obligatoire:
            if val not in diction:
                mycommon.myprint(
                    str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
                return False, " Les informations fournies sont incorrectes"

        """
                Verification de l'identité et autorisation de l'entité qui
                appelle cette API
                """
        token = ""
        if ("token" in diction.keys()):
            if diction['token']:
                token = diction['token']

        related_collection_id = ""
        if ("related_collection_id" in diction.keys()):
            if diction['related_collection_id']:
                related_collection_id = diction['related_collection_id']

        if( str(related_collection_id).strip() == ""):
            mycommon.myprint(
                str(inspect.stack()[0][3]) + " L'Id de la collection est invalide ")
            return False, " L'Id de la collection est invalide "


        local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
        if (local_status is not True):
            return local_status, my_partner

        RetObject = []
        val_tmp = 0

        qry_match = {"partner_owner_recid": str(my_partner['recid']), 'valide': '1', 'locked': '0', 'related_collection_recid':str(related_collection_id)}

        pipe_qry = ([
            {'$match': qry_match},
            {'$sort': {'_id': -1}},
            {'$lookup': {
                'from': 'courrier_template_tracking',
                "let": {'courrier_template_tracking_id': "$courrier_template_tracking_id",
                        'partner_owner_recid': '$partner_owner_recid'},
                'pipeline': [
                    {'$match':
                        {'$expr':
                            {'$and':
                                [

                                    {'$eq': ["$_id", {'$convert': {
                                        'input': "$$courrier_template_tracking_id",
                                        'to': "objectId",
                                        'onError': {'error': 'true'},
                                        'onNull': {'isnull': 'true'}
                                    }}]},

                                    {'$eq': ["$valide", "1"]},
                                    {'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}

                                ]
                            }
                        }
                    },

                ],
                'as': 'courrier_template_tracking'
            }
            },

            {
                '$unwind': '$courrier_template_tracking'
            },
            {'$lookup': {
                'from': 'courrier_template',
                "let": {'courrier_template_id': "$courrier_template_tracking.courrier_template_id", 'partner_owner_recid': '$partner_owner_recid'},
                'pipeline': [
                    {'$match':
                        {'$expr':
                            {'$and':
                                [

                                    {'$eq': ["$_id", {'$convert': {
                                        'input': "$$courrier_template_id",
                                        'to': "objectId",
                                        'onError': {'error': 'true'},
                                        'onNull': {'isnull': 'true'}
                                    }}]},
                                    {'$eq': ["$valide", "1"]},
                                    {'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}

                                ]
                            }
                        }
                    },
                    {'$project': {'contenu_doc': 0, 'corps_mail': 0}},

                ],
                'as': 'courrier_template'
            }
            },
            {
                '$unwind': '$courrier_template'
            },
        ])

        print(" #### Get_Editable_Document_By_Partner pipe_qry_Get_Editable_Document_By_Partner = ", pipe_qry)

        for New_retVal in MYSY_GV.dbname['courrier_template_tracking_history'].aggregate(pipe_qry):
            if ("courrier_template_tracking" in New_retVal.keys() and "courrier_template" in New_retVal.keys()):

                user = {}
                user['_id'] = str(New_retVal['_id'])
                user['id'] = str(val_tmp)

                user['related_collection_recid'] = ""
                if ('related_collection_recid' in New_retVal.keys()):
                    user['related_collection_recid'] = str(New_retVal['related_collection_recid'])

                user['courrier_template_tracking_id'] = ""
                if ('courrier_template_tracking_id' in New_retVal.keys()):
                    user['courrier_template_tracking_id'] = str(New_retVal['courrier_template_tracking_id'])

                user['date_update'] = ""
                if ('date_update' in New_retVal.keys()):
                    user['date_update'] = str(New_retVal['date_update'])

                user['update_by'] = ""
                if ('update_by' in New_retVal.keys()):
                    user['update_by'] = str(New_retVal['update_by'])


                # Traitement de courrier_template_tracking
                user['courrier_template_id'] = ""
                if ('courrier_template_id' in New_retVal['courrier_template_tracking'].keys()):
                    user['courrier_template_id'] = str(New_retVal['courrier_template_tracking']['courrier_template_id'])

                user['related_collection'] = ""
                if ('related_collection' in New_retVal['courrier_template_tracking'].keys()):
                    user['related_collection'] = str(New_retVal['courrier_template_tracking']['related_collection'])

                user['server_action_name'] = ""
                if ('server_action_name' in New_retVal['courrier_template_tracking'].keys()):
                    user['server_action_name'] = str(New_retVal['courrier_template_tracking']['server_action_name'])

                user['courrier_template_ref_interne'] = ""
                if ('courrier_template_ref_interne' in New_retVal['courrier_template_tracking'].keys()):
                    user['courrier_template_ref_interne'] = str(New_retVal['courrier_template_tracking']['courrier_template_ref_interne'])

                user['rang'] = ""
                if ('rang' in New_retVal['courrier_template_tracking'].keys()):
                    user['rang'] = str(New_retVal['courrier_template_tracking']['rang'])

                # Traitement de courrier_template
                user['ref_interne'] = ""
                if ('ref_interne' in New_retVal['courrier_template'].keys()):
                    user['ref_interne'] = str(New_retVal['courrier_template']['ref_interne'])

                user['nom'] = ""
                if ('nom' in New_retVal['courrier_template'].keys()):
                    user['nom'] = str(New_retVal['courrier_template']['nom'])

                user['cible'] = ""
                if ('cible' in New_retVal['courrier_template'].keys()):
                    user['cible'] = str(New_retVal['courrier_template']['cible'])

                user['type_doc'] = ""
                if ('type_doc' in New_retVal['courrier_template'].keys()):
                    user['cible'] = str(New_retVal['courrier_template']['type_doc'])



                val_tmp = val_tmp + 1
                RetObject.append(mycommon.JSONEncoder().encode(user))

        return True, RetObject

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
        return False, " Impossible de récupérer la liste des documents éditables "



"""
Cette fonction a initialiser le contenu de la collection 'courrier_template_tracking' depuis les paramettres
mise dans la collection : 'courrier_template'.

Algo : 
Pour chaque element de la collection 'courrier_template' ayant le 
- tracked = 0 et
- tacking_collection[] (des elements dans ce tableau)
- partner_owner_recid : identique à l'utilisateur appelant

Le système va créer une entrée dans le collection 'courrier_template_tracking'

"""
def Init_And_Update_courrier_template_tracking(diction):
    try:
        field_list_obligatoire = ['token']

        for val in field_list_obligatoire:
            if val not in diction:
                mycommon.myprint(
                    str(inspect.stack()[0][
                            3]) + "  La valeur '" + val + "' n'est pas presente dans liste ")
                return False, "  La valeur '" + val + "' n'est pas presente dans liste"

        my_token = ""
        if ("token" in diction.keys()):
            if diction['token']:
                my_token = diction['token']

        local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
        if (local_status is not True):
            return local_status, my_partner

        warning_message = ""
        cpt = 0
        for retval in MYSY_GV.dbname['courrier_template_type_document'].find({'tracked':'0', 'valide':'1', 'locked':'0',
                                                                'partner_owner_recid':'default'}):


            tab_tacking_collection = []
            if( "tacking_collection" in retval.keys() ):
                tab_tacking_collection = retval['tacking_collection']

            for collection in tab_tacking_collection:
                new_data = {}
                new_data['courrier_template_type_document_id'] = str(retval['_id'])
                new_data['partner_owner_recid'] = str(my_partner['recid'])
                new_data['related_collection'] = str(collection)
                new_data['courrier_template_type_document_ref_interne'] = str(retval['ref_interne'])
                new_data['rang'] = str(retval['rang'])
                new_data['server_action_name'] = "a préciser"
                new_data['valide'] = "1"
                new_data['locked'] = "0"

                key_data = {}
                key_data['courrier_template_type_document_id'] = str(retval['_id'])
                key_data['partner_owner_recid'] = str(my_partner['recid'])
                key_data['related_collection'] = str(collection)
                key_data['valide'] = "1"


                new_retval_data = MYSY_GV.dbname['courrier_template_tracking'].find_one_and_update(key_data,
                                                       {"$set": new_data},
                                                       upsert=True,
                                                       return_document=ReturnDocument.AFTER
                                                       )

                if new_retval_data  is None or "_id" not in  new_retval_data.keys():
                    mycommon.myprint(
                        str(inspect.stack()[0][3]) + "  Impossible d'initialiser la traçabilité du document "+str(retval['ref_interne'])+". ")
                    warning_message = warning_message + " Impossible d'initialiser la traçabilité du document "+str(retval['ref_interne'])+". "


                cpt = cpt + 1

            """ 
            # Mise à jour du courrier_template pour mettre 'tracked':'1'
            update_data = {}
            update_data['date_update'] = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
            update_data['update_by'] = str(my_partner['_id'])
            update_data['tracked'] = "1"

            MYSY_GV.dbname['courrier_template_type_document'].update_one({'_id':ObjectId(str(retval['_id']))}, {'$set':update_data})
            """

        if( warning_message == "" ):
            return True, "Initialisation de la traçabilité de "+str(cpt)+" document(s) Ok "
        else:
            return True, warning_message

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
        return False, " Impossible de récupérer la liste des modèles de fiche"



def Init_And_Update_courrier_template_tracking_With_Partner_Data(my_partner):
    try:

        warning_message = ""
        cpt = 0
        for retval in MYSY_GV.dbname['courrier_template_type_document'].find({'tracked':'0', 'valide':'1', 'locked':'0',
                                                                'partner_owner_recid':'default'}):


            tab_tacking_collection = []
            if( "tacking_collection" in retval.keys() ):
                tab_tacking_collection = retval['tacking_collection']

            for collection in tab_tacking_collection:
                new_data = {}
                new_data['courrier_template_type_document_id'] = str(retval['_id'])
                new_data['partner_owner_recid'] = str(my_partner['recid'])
                new_data['related_collection'] = str(collection)
                new_data['courrier_template_type_document_ref_interne'] = str(retval['ref_interne'])
                new_data['rang'] = str(retval['rang'])
                new_data['server_action_name'] = "a préciser"
                new_data['valide'] = "1"
                new_data['locked'] = "0"

                key_data = {}
                key_data['courrier_template_type_document_id'] = str(retval['_id'])
                key_data['partner_owner_recid'] = str(my_partner['recid'])
                key_data['related_collection'] = str(collection)
                key_data['valide'] = "1"


                new_retval_data = MYSY_GV.dbname['courrier_template_tracking'].find_one_and_update(key_data,
                                                       {"$set": new_data},
                                                       upsert=True,
                                                       return_document=ReturnDocument.AFTER
                                                       )

                if new_retval_data  is None or "_id" not in  new_retval_data.keys():
                    mycommon.myprint(
                        str(inspect.stack()[0][3]) + "  Impossible d'initialiser la traçabilité du document "+str(retval['ref_interne'])+". ")
                    warning_message = warning_message + " Impossible d'initialiser la traçabilité du document "+str(retval['ref_interne'])+". "


                cpt = cpt + 1

            """ 
            # Mise à jour du courrier_template pour mettre 'tracked':'1'
            update_data = {}
            update_data['date_update'] = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
            update_data['update_by'] = str(my_partner['_id'])
            update_data['tracked'] = "1"

            MYSY_GV.dbname['courrier_template_type_document'].update_one({'_id':ObjectId(str(retval['_id']))}, {'$set':update_data})
            """

        if( warning_message == "" ):
            return True, "Initialisation de la traçabilité de "+str(cpt)+" document(s) Ok "
        else:
            return True, warning_message

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
        return False, " Impossible de récupérer la liste des modèles de fiche"





"""
Cette fonction log un historique d'envoie email ou de telechargement
d'un document 
/!\ : Dans certains cas on n'utilise pas de modele de courrier, comme l'accetation d'un inscription ou ,
l'envoi des demande demande d'emargement (boff a reanalyser tout meme),
on laisse la valeur "courrier_template_id" a vide
"""
def Editic_Log_History_Action(my_partner, courrier_template_data, related_collection_recid, target_collection = None, target_collection_recid = None, courrier_template_id=""):
    try:

        courrier_template_type_document_ref_interne = ""
        if( "courrier_template_type_document_ref_interne" in courrier_template_data.keys() ):
            courrier_template_type_document_ref_interne = courrier_template_data['courrier_template_type_document_ref_interne']

        courrier_template_tracking_id = ""
        courrier_template_tracking_data = MYSY_GV.dbname['courrier_template_tracking'].find_one({'partner_owner_recid':str(my_partner['recid']),
                                                                     'valide':'1',
                                                                     'locked':'0',
                                                                     'courrier_template_type_document_ref_interne':str(courrier_template_type_document_ref_interne)})
        if(courrier_template_tracking_data is None ):
            mycommon.myprint(
                "  WARNING impossible de recuperer le 'courrier_template_tracking' ")
            return True, "  WARNING impossible de récuperer le 'courrier_template_tracking' "


        courrier_template_tracking_id = str(courrier_template_tracking_data['_id'])

        #print(" ### courrier_template_data = ", courrier_template_data)

        history_data_to_log = {}
        history_data_to_log ['partner_owner_recid'] = str(my_partner['recid'])
        history_data_to_log['related_collection_recid'] = related_collection_recid
        history_data_to_log['courrier_template_tracking_id'] = courrier_template_tracking_id
        history_data_to_log['courrier_template_id'] = str(courrier_template_id)
        history_data_to_log['date_update'] = str(datetime.now())
        history_data_to_log['update_by'] = str(my_partner['_id'])
        history_data_to_log['valide'] = "1"
        history_data_to_log['locked'] = "0"

        local_target_collection = ""
        if(target_collection ):
            local_target_collection = target_collection

        local_target_collection_recid = ""
        if (target_collection_recid):
            local_target_collection_recid = target_collection_recid

        history_data_to_log['target_collection'] = local_target_collection
        history_data_to_log['target_collection_recid'] = local_target_collection_recid



        #print( "#### history_data_to_log = ", history_data_to_log)
        MYSY_GV.dbname['courrier_template_tracking_history'].insert_one(history_data_to_log)

        return True, " L'évènement Editique a été correctement enregistré "

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
        return False, " Impossible d'enregistrer de l'évènement éditique"

"""
Cette fonction log un historique tout evenement lié à un element
de la collection courrier_template_tracking (CONF_INSCRIPTION, EMARGEMENT_FORMATION, etc)

/!\ : Dans certains cas on n'utilise pas de modele de courrier, comme l'accetation d'un inscription ou ,
l'envoi des demande demande d'emargement (boff a reanalyser tout meme),
on laisse la valeur "courrier_template_id" a vide
"""
def Editic_Log_History_Action_From_courrier_template_type_document_ref_interne(my_partner, courrier_template_type_document_ref_interne,
                                                                               related_collection_recid, target_collection = None, target_collection_recid = None,
                                                                               courrier_template_id="",
                                                                               commentaire=""):
    try:

        courrier_template_tracking_data = {}
        if( courrier_template_type_document_ref_interne ):

            qry ={'partner_owner_recid':str(my_partner['recid']),
                                                                         'valide':'1',
                                                                         'locked':'0',
                                                                         'courrier_template_type_document_ref_interne':str(courrier_template_type_document_ref_interne)}

            #print(" #### qry = ,", qry)

            courrier_template_tracking_data = MYSY_GV.dbname['courrier_template_tracking'].find_one({'partner_owner_recid':str(my_partner['recid']),
                                                                         'valide':'1',
                                                                         'locked':'0',
                                                                         'courrier_template_type_document_ref_interne':str(courrier_template_type_document_ref_interne)})
            if(courrier_template_tracking_data is None ):
                mycommon.myprint(
                    "  WARNING impossible de recuperer le 'courrier_template_tracking' ")
                return True, "  WARNING impossible de récuperer le 'courrier_template_tracking' "

        else:
            mycommon.myprint(
                "  WARNING impossible de recuperer le 'courrier_template_tracking'(2) ")
            return True, "  WARNING impossible de récuperer le 'courrier_template_tracking' (2)"

        #print(" #### courrier_template_tracking_data = ", courrier_template_tracking_data)

        courrier_template_tracking_id = str(courrier_template_tracking_data['_id'])
        courrier_template_id = str(courrier_template_id)

        #print(" ### courrier_template_data = ", courrier_template_data)

        history_data_to_log = {}
        history_data_to_log ['partner_owner_recid'] = str(my_partner['recid'])
        history_data_to_log['related_collection_recid'] = related_collection_recid
        history_data_to_log['courrier_template_tracking_id'] = courrier_template_tracking_id
        history_data_to_log['courrier_template_id'] = str(courrier_template_id)
        history_data_to_log['comment'] =  str(commentaire)
        history_data_to_log['date_update'] =  str(datetime.now())
        history_data_to_log['update_by'] = str(my_partner['_id'])
        history_data_to_log['valide'] = "1"
        history_data_to_log['locked'] = "0"

        local_target_collection = ""
        if (target_collection):
            local_target_collection = target_collection

        local_target_collection_recid = ""
        if (target_collection_recid):
            local_target_collection_recid = target_collection_recid

        history_data_to_log['target_collection'] = local_target_collection
        history_data_to_log['target_collection_recid'] = local_target_collection_recid


        #print( "#### history_data_to_log = ", history_data_to_log)
        MYSY_GV.dbname['courrier_template_tracking_history'].insert_one(history_data_to_log)

        return True, " L'évènement Editique a été correctement enregistré "

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
        return False, " Impossible d'enregistrer de l'évènement éditique"



"""
Data migration du delete
"""
def Account_Migration_to_delete(diction):
    try:

        for val in MYSY_GV.dbname['partnair_account'].find({'is_partner_admin_account':'1', 'active':'1', 'locked':'0'}):

            print(" #### TRAITEMENT DU COUNT : ", val['email'])
            local_status, local_retval = Init_And_Update_courrier_template_tracking_With_Partner_Data(val)
            if (local_status is False):
                mycommon.myprint(
                    " WARNING : Impossible d'initialiser le 'courrier_template_type_document' pour le partner_account_id =" + str(
                        val['_id']))

            print(" #### Init_And_Update_courrier_template_tracking_With_Partner_Data ==> ", local_status)

            for val_tmp in MYSY_GV.dbname['base_partner_setup'].find(
                    {'partner_owner_recid': 'default', 'valide': '1', 'locked': '0'}):

                new_data = val_tmp
                del new_data['_id']
                new_data['config_value'] = ""
                new_data['date_update'] = ""
                new_data['partner_owner_recid'] = str(val['recid'])

                key_data = {}
                key_data['config_name'] = val_tmp['config_name']
                key_data['partner_owner_recid'] = str(val['recid'])

                new_retval_data = MYSY_GV.dbname['base_partner_setup'].find_one_and_update(key_data,
                                                                                           {"$set": new_data},
                                                                                           upsert=True,
                                                                                           return_document=ReturnDocument.AFTER
                                                                                           )

                if new_retval_data is None or "_id" not in new_retval_data.keys():
                    mycommon.myprint(
                        str(inspect.stack()[0][
                                3]) + "  Impossible d'initialiser le point de configuration technique " + str(
                            val_tmp['config_name']) + ". ")

                else:
                    mycommon.myprint(
                        str(inspect.stack()[0][
                                3]) + "  point de configuration technique " + str(val_tmp['config_name']) + " ==> OK ")


        return True, " Le compte a été correctement migré "

    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 migrer le compte"


