Delimiter //
/**
 * Author:  armen
 * Created: 12 oct. 2022
 * param : 
 *      existingUpdateMode int : masque de flag boolement (bit) indiquant le mode de mise à jour des données exitantes
 *          la recherche de l'existance ce faire sur le code / la référence de la donnée
 *          - si null ou 0, ne met rien à jour, ajout simplement des nouvelle données
 *          - existingUpdateMode & 0x01 => MAJ des messages (emails) de même DF_MESSAGES_TYPES.DF_MESSAGES_TYPES_CODE
 *          - existingUpdateMode & 0x02 => MAJ des courrier de même DF_MODELS_TYPES.DF_MODELS_TYPES_CODE
 *          - existingUpdateMode & 0x04 => MAJ des sms de même DF_SMS_TYPES.DF_SMS_TYPES_CODE
 *          - existingUpdateMode & 0x10 => MAJ des actions de même DF_TASKS.DF_TASKS_CODE
 *          - existingUpdateMode & 0x20 => MAJ des plans d'action de même DF_TASKS_PLANS.DF_TASKS_PLANS_CODE
 */
Drop Procedure If Exists BUSINESS_PROCESSES_RESTORE_BACKUP //
Create Procedure BUSINESS_PROCESSES_RESTORE_BACKUP(
    In existingUpdateMode int Unsigned
    , In useTransaction Int
)
Begin
    bprb_main: Begin
        Declare transactionStarted int default 0 ;
        DECLARE Exit HANDLER FOR SQLEXCEPTION
        Begin
                if transactionStarted  <> 0 then
                        rollback;
                        set transactionStarted  = 0;
                End if;
                resignal;
        end ;

        Set @bprb_upd_mask_MODELS       =1; -- 0x01;
        Set @bprb_upd_mask_MESSAGES     =2; -- 0x02;
        Set @bprb_upd_mask_SMS          =4; -- 0x04;
        Set @bprb_upd_mask_TASKS        =16; -- 0x10;
        Set @bprb_upd_mask_TASKSPLANS   =32; -- 0x20;

        Set @bprb_existingUpdateMode = Coalesce(existingUpdateMode, 0);
        Set @bprb_useTransaction = Coalesce(useTransaction, 1);
--         Set @bprb_existingUpdateMode = 0;
--         Set @bprb_existingUpdateMode = 0x37; -- all

        if @bprb_useTransaction Then
            start transaction;
            set transactionStarted = 1;
        End if;

        bprb_upd_bk_ids: Begin
            bprb_upd_bk_accst_ids: Begin
 
                -- 
                -- nouveau id pour les status de compte
                Set @idTbl = null;
                Update TMP_BKP_DF_ACCOUNTS_STATUS tar
                Set NEW_ID_DF_ACCOUNTS_STATUS = Coalesce(
                        (
                            Select src.ID_DF_ACCOUNTS_STATUS
                            From DF_ACCOUNTS_STATUS src
                            Where 1 = 1 
--                                 And tar.ID_DF_ACCOUNTS_STATUS = src.ID_DF_ACCOUNTS_STATUS
                                And tar.DF_ACCOUNTS_STATUS_CODE = src.DF_ACCOUNTS_STATUS_CODE
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_ACCOUNTS_STATUS
                                        From DF_ACCOUNTS_STATUS src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where tar.NEW_ID_DF_ACCOUNTS_STATUS Is Null
                ;
                -- fk des status de compte
                Update 
                    TMP_BKP_DF_ACCOUNTS_STATUS src
                    Left Join TMP_BKP_TASK_LIST tbtl
                        On src.ID_DF_ACCOUNTS_STATUS = tbtl.TSKLST_ID_DF_ACCOUNTS_STATUS
                    Left Join TMP_BKP_DF_ACCOUNTS_STATUS_LNG lng
                        On src.ID_DF_ACCOUNTS_STATUS = lng.DF_ACCOUNTS_STATUS_ID_DF_ACCOUNTS_STATUS
                Set
                    tbtl.NEW_TSKLST_ID_DF_ACCOUNTS_STATUS = src.NEW_ID_DF_ACCOUNTS_STATUS
                    , lng.NEW_DF_ACCOUNTS_STATUS_ID_DF_ACCOUNTS_STATUS = src.NEW_ID_DF_ACCOUNTS_STATUS
                Where src.NEW_ID_DF_ACCOUNTS_STATUS Is Not Null
                ;
            end;
            bprb_upd_bk_tskfam_ids: Begin
                Set @idTbl = null;
                Update TMP_BKP_DF_TASK_FAMILIES tar
                Set NEW_ID_DF_TASK_FAMILIES = Coalesce(
                        (
                            Select src.ID_DF_TASK_FAMILIES
                            From DF_TASK_FAMILIES src
                            Where  1 = 1 
--                                 And tar.ID_DF_TASK_FAMILIES = src.ID_DF_TASK_FAMILIES
                                And tar.DF_TASK_FAMILIES_CODE = src.DF_TASK_FAMILIES_CODE
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_TASK_FAMILIES
                                        From DF_TASK_FAMILIES src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where NEW_ID_DF_TASK_FAMILIES Is Null
                ;
                 -- fk des familles de taches
                Update 
                    TMP_BKP_DF_TASK_FAMILIES src
                    Left Join TMP_BKP_DF_TASKS tbtl
                        On src.ID_DF_TASK_FAMILIES = tbtl.DF_TASK_FAM_ID_DF_TASK_FAM
                    Left Join TMP_BKP_DF_TASK_FAMILIES_LNG lng
                        On src.ID_DF_TASK_FAMILIES = lng.DF_TASK_FAM_ID_DF_TASK_FAM
                Set
                    tbtl.NEW_DF_TASK_FAM_ID_DF_TASK_FAM = src.NEW_ID_DF_TASK_FAMILIES
                    , lng.NEW_DF_TASK_FAM_ID_DF_TASK_FAM = src.NEW_ID_DF_TASK_FAMILIES
                Where src.NEW_ID_DF_TASK_FAMILIES Is Not Null
                ;

            end;

            bprb_upd_bk_fct_ids: Begin
                Set @idTbl = null;
                Update TMP_BKP_DF_FUNCTIONS tar
                Set NEW_ID_DF_FUNCTIONS = Coalesce(
                        (
                            Select src.ID_DF_FUNCTIONS
                            From DF_FUNCTIONS src
                            Where  1 = 1 
--                                 And tar.ID_DF_FUNCTIONS = src.ID_DF_FUNCTIONS
                                And tar.DF_FUNCTIONS_SHORT_LABEL = src.DF_FUNCTIONS_SHORT_LABEL
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_FUNCTIONS
                                        From DF_FUNCTIONS src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where NEW_ID_DF_FUNCTIONS Is Null
                ;

                -- fk des fonctions
                Update 
                    TMP_BKP_DF_FUNCTIONS src
                    Inner Join TMP_BKP_DF_RECIPIENTS tbtl
                        On src.ID_DF_FUNCTIONS = tbtl.DF_FUNC_ID_DF_FUNC_MEMBER
                Set
                    tbtl.NEW_DF_FUNC_ID_DF_FUNC_MEMBER = src.NEW_ID_DF_FUNCTIONS
                Where src.NEW_ID_DF_FUNCTIONS Is Not Null
                ;
                Update 
                    TMP_BKP_DF_FUNCTIONS src
                    Inner Join TMP_BKP_DF_RECIPIENTS tbtl
                        On src.ID_DF_FUNCTIONS = tbtl.DF_FUNC_ID_DF_FUNC_CONTACT
                Set
                    tbtl.NEW_DF_FUNC_ID_DF_FUNC_CONTACT = src.NEW_ID_DF_FUNCTIONS
                Where src.NEW_ID_DF_FUNCTIONS Is Not Null
                ;

            end;
            bprb_upd_bk_msg_ids: Begin
                Set @idTbl = null;
                Update TMP_BKP_DF_MESSAGES_TYPES tar
                Set NEW_ID_DF_MESSAGES_TYPES = Coalesce(
                        (
                            Select src.ID_DF_MESSAGES_TYPES
                            From DF_MESSAGES_TYPES src
                            Where  1 = 1 
--                                 And tar.ID_DF_MESSAGES_TYPES = src.ID_DF_MESSAGES_TYPES
                                And tar.DF_MESSAGES_TYPES_CODE = src.DF_MESSAGES_TYPES_CODE
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_MESSAGES_TYPES
                                        From DF_MESSAGES_TYPES src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where NEW_ID_DF_MESSAGES_TYPES Is Null
                ;
                 -- fk des messages
                Update 
                    TMP_BKP_DF_MESSAGES_TYPES src
                    Left Join TMP_BKP_DF_MEDIAS tbtl
                        On src.ID_DF_MESSAGES_TYPES = tbtl.MSG_TYPES_ID_MSG_TYPES
                    Left Join TMP_BKP_DF_MESSAGES lng
                        On src.ID_DF_MESSAGES_TYPES = lng.DF_MSG_TYPES_ID_DF_MSG_TYPES
                Set
                    tbtl.NEW_MSG_TYPES_ID_MSG_TYPES = src.NEW_ID_DF_MESSAGES_TYPES
                    , lng.NEW_DF_MSG_TYPES_ID_DF_MSG_TYPES = src.NEW_ID_DF_MESSAGES_TYPES
                Where src.NEW_ID_DF_MESSAGES_TYPES Is Not Null
                ;
            end;


            bprb_upd_bk_letter_ids: Begin
                Set @idTbl = null;
                Update TMP_BKP_DF_MODELS_TYPES tar
                Set NEW_ID_DF_MODELS_TYPES = Coalesce(
                        (
                            Select src.ID_DF_MODELS_TYPES
                            From DF_MODELS_TYPES src
                            Where  1 = 1 
--                                 And tar.ID_DF_MODELS_TYPES = src.ID_DF_MODELS_TYPES
                                And tar.DF_MODELS_TYPES_CODE = src.DF_MODELS_TYPES_CODE
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_MODELS_TYPES
                                        From DF_MODELS_TYPES src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where NEW_ID_DF_MODELS_TYPES Is Null
                ;
                -- fk 
                Update 
                    TMP_BKP_DF_MODELS_TYPES src
                    Left Join TMP_BKP_DF_MEDIAS tbtl
                        On src.ID_DF_MODELS_TYPES = tbtl.MODELS_TYPES_ID_MODELS_TYPES
                    Left Join TMP_BKP_DF_MODELS lng
                        On src.ID_DF_MODELS_TYPES = lng.MODELS_TYPES_ID_MODELS_TYPES 
                Set
                    tbtl.NEW_MODELS_TYPES_ID_MODELS_TYPES = src.NEW_ID_DF_MODELS_TYPES
                    , lng.NEW_MODELS_TYPES_ID_MODELS_TYPES = src.NEW_ID_DF_MODELS_TYPES
                Where src.NEW_ID_DF_MODELS_TYPES Is Not Null
                ;
            end;

            bprb_upd_bk_sms_ids: Begin
                Set @idTbl = null;
                Update TMP_BKP_DF_SMS_TYPES tar
                Set NEW_ID_DF_SMS_TYPES = Coalesce(
                        (
                            Select src.ID_DF_SMS_TYPES
                            From DF_SMS_TYPES src
                            Where  1 = 1 
--                                 And tar.ID_DF_SMS_TYPES = src.ID_DF_SMS_TYPES
                                And tar.DF_SMS_TYPES_CODE = src.DF_SMS_TYPES_CODE
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_SMS_TYPES
                                        From DF_SMS_TYPES src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where NEW_ID_DF_SMS_TYPES Is Null
                ;
                -- fk 
                Update 
                    TMP_BKP_DF_SMS_TYPES src
                    Left Join TMP_BKP_DF_MEDIAS tbtl
                        On src.ID_DF_SMS_TYPES = tbtl.SMS_TYPES_ID_SMS_TYPES
                    Left Join TMP_BKP_DF_SMS lng
                        On src.ID_DF_SMS_TYPES = lng.DF_SMS_TYPES_ID_DF_SMS_TYPES
                Set
                    tbtl.NEW_SMS_TYPES_ID_SMS_TYPES = src.NEW_ID_DF_SMS_TYPES
                    , lng.NEW_DF_SMS_TYPES_ID_DF_SMS_TYPES = src.NEW_ID_DF_SMS_TYPES
                Where src.NEW_ID_DF_SMS_TYPES Is Not Null
                ;
            end;

            bprb_upd_bk_tasks_ids: Begin
                Set @idTbl = null;
                Update TMP_BKP_DF_TASKS tar
                Set NEW_ID_DF_TASKS = Coalesce(
                        (
                            Select src.ID_DF_TASKS
                            From DF_TASKS src
                            Where  1 = 1 
--                                 And tar.ID_DF_TASKS = src.ID_DF_TASKS
                                And tar.DF_TASKS_CODE = src.DF_TASKS_CODE
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_TASKS
                                        From DF_TASKS src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where NEW_ID_DF_TASKS Is Null
                ;
                -- fk 
                Update 
                    TMP_BKP_DF_TASKS src
                    Left Join TMP_BKP_DF_MEDIAS tbtl1
                        On src.ID_DF_TASKS = tbtl1.DF_TASKS_ID_DF_TASKS
                    Left Join TMP_BKP_DF_RECIPIENTS tbtl2
                        On src.ID_DF_TASKS = tbtl2.DF_TASKS_ID_DF_TASKS
                    Left Join TMP_BKP_DF_TASKS_PLANS_has_DF_TASKS tbtl3
                        On src.ID_DF_TASKS = tbtl3.DF_TASKS_ID_DF_TASKS
                    Left Join TMP_BKP_TASK_LIST tbtl4
                        On src.ID_DF_TASKS = tbtl4.TSKLST_ID_DF_TASKS
                Set
                    tbtl1.NEW_DF_TASKS_ID_DF_TASKS = src.NEW_ID_DF_TASKS
                    , tbtl2.NEW_DF_TASKS_ID_DF_TASKS = src.NEW_ID_DF_TASKS
                    , tbtl3.NEW_DF_TASKS_ID_DF_TASKS = src.NEW_ID_DF_TASKS
                    , tbtl4.NEW_TSKLST_ID_DF_TASKS = src.NEW_ID_DF_TASKS
                Where src.NEW_ID_DF_TASKS Is Not Null
                ;
                Update 
                    TMP_BKP_DF_TASKS src
                    Inner Join TMP_BKP_DF_TASKS tbtl1
                        On src.ID_DF_TASKS = tbtl1.DF_TASKS_ID_DF_TASKS_FALLBACK
                Set
                    tbtl1.NEW_DF_TASKS_ID_DF_TASKS_FALLBACK = src.NEW_ID_DF_TASKS
                Where src.NEW_ID_DF_TASKS Is Not Null
                ;
            end;

            bprb_upd_bk_tskplan_ids: Begin
                Set @idTbl = null;
                Update TMP_BKP_DF_TASKS_PLANS tar
                Set NEW_ID_DF_TASKS_PLANS = Coalesce(
                        (
                            Select src.ID_DF_TASKS_PLANS
                            From DF_TASKS_PLANS src
                            Where  1 = 1 
--                                 And tar.ID_DF_TASKS_PLANS = src.ID_DF_TASKS_PLANS
                                And tar.DF_TASKS_PLANS_CODE = src.DF_TASKS_PLANS_CODE
                            Limit 1
                        )
                        , (
                            @idTbl :=  (
                                 Coalesce(
                                    @idTbl
                                    , (
                                        Select src.ID_DF_TASKS_PLANS
                                        From DF_TASKS_PLANS src
                                        Order By 1 desc
                                        Limit 1
                                    )
                                    , 0
                                ) +1
                            )
                        )
                    )
                Where NEW_ID_DF_TASKS_PLANS Is Null
                ;
                -- fk 
                Update 
                    TMP_BKP_DF_TASKS_PLANS src
                    Left Join TMP_BKP_TASK_LIST tbtl1
                        On src.ID_DF_TASKS_PLANS = tbtl1.TSKLST_ID_DF_TASKS_PLANS
                    Left Join TMP_BKP_DF_TASKS_PLANS_has_DF_TASKS tbl2
                        On src.ID_DF_TASKS_PLANS = tbl2.DF_TKS_PLANS_ID_DF_TKS_PLANS
                Set
                    tbtl1.NEW_TSKLST_ID_DF_TASKS_PLANS = src.NEW_ID_DF_TASKS_PLANS
                    , tbl2.NEW_DF_TKS_PLANS_ID_DF_TKS_PLANS = src.NEW_ID_DF_TASKS_PLANS
                Where src.NEW_ID_DF_TASKS_PLANS Is Not Null
                ;
            end;

        end;

        
        savepoint bprb_upd_ids_savepoint;
-- 
        bprb_copyactions: Begin
            bprb_copy_new_fct: Begin
                Insert Into DF_FUNCTIONS (
                    ID_DF_FUNCTIONS, ADMIN_PARAMS_ID_ADMIN_PARAMS, DF_FUNCTIONS_SHORT_LABEL
                    , DF_FUNCTIONS_LABEL, DF_FUNCTIONS_TRASH, DF_FUNCTIONS_PREFERENCE
                )
                Select 
                    src.NEW_ID_DF_FUNCTIONS, src.ADMIN_PARAMS_ID_ADMIN_PARAMS, src.DF_FUNCTIONS_SHORT_LABEL
                    , src.DF_FUNCTIONS_LABEL, src.DF_FUNCTIONS_TRASH, src.DF_FUNCTIONS_PREFERENCE
                From TMP_BKP_DF_FUNCTIONS src
                    Left Join DF_FUNCTIONS tar
                        On src.NEW_ID_DF_FUNCTIONS = tar.ID_DF_FUNCTIONS
                Where tar.ID_DF_FUNCTIONS Is Null
--                     And src.NEW_ID_DF_FUNCTIONS <> src.ID_DF_FUNCTIONS
                ;
            end ;

            bprb_copy_new_tskfam: Begin
                Insert Into DF_TASK_FAMILIES (
                    ID_DF_TASK_FAMILIES, ADMIN_PARAMS_ID_ADMIN_PARAMS, DF_TASK_F_V_T_ID_DF_TASK_F_V_T
                    , DF_TASK_FAMILIES_CODE
                )
                Select 
                    src.NEW_ID_DF_TASK_FAMILIES, src.ADMIN_PARAMS_ID_ADMIN_PARAMS, src.DF_TASK_F_V_T_ID_DF_TASK_F_V_T
                    , src.DF_TASK_FAMILIES_CODE
                    
                From TMP_BKP_DF_TASK_FAMILIES src
                    Left Join DF_TASK_FAMILIES tar
                        On src.NEW_ID_DF_TASK_FAMILIES = tar.ID_DF_TASK_FAMILIES
                Where tar.ID_DF_TASK_FAMILIES is null
--                     And src.NEW_ID_DF_TASK_FAMILIES <> src.ID_DF_TASK_FAMILIES
                ;

                Insert Into DF_TASK_FAMILIES_LNG(
                    DF_TASK_FAMILIES_LNG_LABEL, DF_LANGUAGES_ID_DF_LANGUAGES
                    , DF_TASK_FAM_ID_DF_TASK_FAM
                )
                Select 
                    src.DF_TASK_FAMILIES_LNG_LABEL, src.DF_LANGUAGES_ID_DF_LANGUAGES
                    , src.NEW_DF_TASK_FAM_ID_DF_TASK_FAM
                From TMP_BKP_DF_TASK_FAMILIES_LNG src
                    Left Join DF_TASK_FAMILIES_LNG tar
                        On src.NEW_DF_TASK_FAM_ID_DF_TASK_FAM = tar.DF_TASK_FAM_ID_DF_TASK_FAM
                Where tar.DF_TASK_FAM_ID_DF_TASK_FAM is null
--                     And src.NEW_DF_TASK_FAM_ID_DF_TASK_FAM <> src.DF_TASK_FAM_ID_DF_TASK_FAM
                ;
            end ;

            bprb_copy_new_msg: Begin
                Insert Into DF_MESSAGES_TYPES(
                    ID_DF_MESSAGES_TYPES, ADMIN_PARAMS_ID_ADMIN_PARAMS, DF_MESSAGES_TYPES_LABEL
                    , DF_MESSAGES_TYPES_CODE, DF_MESSAGES_TYPES_NOTES, DF_MESSAGES_TYPES_TRASH
                )
                Select 
                    src.NEW_ID_DF_MESSAGES_TYPES, src.ADMIN_PARAMS_ID_ADMIN_PARAMS,src. DF_MESSAGES_TYPES_LABEL
                    , src.DF_MESSAGES_TYPES_CODE, src.DF_MESSAGES_TYPES_NOTES, src.DF_MESSAGES_TYPES_TRASH
                    
                From TMP_BKP_DF_MESSAGES_TYPES src
                    Left Join DF_MESSAGES_TYPES tar
                        On src.NEW_ID_DF_MESSAGES_TYPES = tar.ID_DF_MESSAGES_TYPES
                Where tar.ID_DF_MESSAGES_TYPES is null
--                    And src.NEW_ID_DF_MESSAGES_TYPES <> src.ID_DF_MESSAGES_TYPES
                ;

                Insert Into DF_MESSAGES(
                    DF_LANGUAGES_ID_DF_LANGUAGES, DF_MSG_TYPES_ID_DF_MSG_TYPES, DF_MESSAGES_MERGE_MODEL
                    , DF_MESSAGES_EDITOR_MODEL, DF_MESSAGES_MODEL_DATA, DF_MESSAGES_PREVIEW_MODEL
                )
                Select 
                    src.DF_LANGUAGES_ID_DF_LANGUAGES, src.NEW_DF_MSG_TYPES_ID_DF_MSG_TYPES, src.DF_MESSAGES_MERGE_MODEL
                    , src.DF_MESSAGES_EDITOR_MODEL, src.DF_MESSAGES_MODEL_DATA, src.DF_MESSAGES_PREVIEW_MODEL
                    
                From TMP_BKP_DF_MESSAGES src
                    Left Join DF_MESSAGES tar
                        On src.NEW_DF_MSG_TYPES_ID_DF_MSG_TYPES = tar.DF_MSG_TYPES_ID_DF_MSG_TYPES
                Where tar.DF_MSG_TYPES_ID_DF_MSG_TYPES is null
--                     And src.NEW_DF_MSG_TYPES_ID_DF_MSG_TYPES <> src.DF_MSG_TYPES_ID_DF_MSG_TYPES
                ;
            end ;

            bprb_copy_new_letter: Begin
                Insert Into DF_MODELS_TYPES(
                    ID_DF_MODELS_TYPES, ADMIN_PARAMS_ID_ADMIN_PARAMS, DF_MODELS_TYPES_CODE, DF_MODELS_TYPES_LABEL
                    ,  DF_MODELS_TYPES_NOTES, DF_MODELS_TYPES_TYPES_CATEG, DF_MODELS_TYPES_TRASH
                )
                Select 
                    src.NEW_ID_DF_MODELS_TYPES, src.ADMIN_PARAMS_ID_ADMIN_PARAMS, src.DF_MODELS_TYPES_CODE, src.DF_MODELS_TYPES_LABEL
                    , src.DF_MODELS_TYPES_NOTES, src.DF_MODELS_TYPES_TYPES_CATEG, src.DF_MODELS_TYPES_TRASH
                From TMP_BKP_DF_MODELS_TYPES src
                    Left Join DF_MODELS_TYPES tar
                        On src.NEW_ID_DF_MODELS_TYPES = tar.ID_DF_MODELS_TYPES
                Where tar.ID_DF_MODELS_TYPES is null
--                     And src.NEW_ID_DF_MODELS_TYPES <> src.ID_DF_MODELS_TYPES
                ;

                Insert Into DF_MODELS(
                    DF_LANG_ID_DF_LANG, MODELS_TYPES_ID_MODELS_TYPES, DF_MERGE_MODEL, DF_EDITOR_MODEL
                    , DF_MODELS_DATA, DF_MODELS_PREVIEW_MODEL
                )
                Select 
                    src.DF_LANG_ID_DF_LANG, src.NEW_MODELS_TYPES_ID_MODELS_TYPES, src.DF_MERGE_MODEL, src.DF_EDITOR_MODEL
                    , src.DF_MODELS_DATA, src.DF_MODELS_PREVIEW_MODEL
                From TMP_BKP_DF_MODELS src
                    Left Join DF_MODELS tar
                        On src.NEW_MODELS_TYPES_ID_MODELS_TYPES = tar.MODELS_TYPES_ID_MODELS_TYPES
                Where  tar.MODELS_TYPES_ID_MODELS_TYPES Is null
--                     And src.NEW_MODELS_TYPES_ID_MODELS_TYPES <>  src.MODELS_TYPES_ID_MODELS_TYPES
                ;
            end ;

            bprb_copy_new_sms: Begin
                Insert Into DF_SMS_TYPES(
                    ID_DF_SMS_TYPES, ADMIN_PARAMS_ID_ADMIN_PARAMS, DF_SMS_TYPES_CODE, DF_SMS_TYPES_LABEL
                    , DF_SMS_TYPES_NOTES, DF_SMS_TYPES_TRASH
                )
                Select 
                    src.NEW_ID_DF_SMS_TYPES, src.ADMIN_PARAMS_ID_ADMIN_PARAMS, src.DF_SMS_TYPES_CODE, src.DF_SMS_TYPES_LABEL
                    , src.DF_SMS_TYPES_NOTES, src.DF_SMS_TYPES_TRASH
                From TMP_BKP_DF_SMS_TYPES src
                    Left Join DF_SMS_TYPES tar
                        On src.NEW_ID_DF_SMS_TYPES = tar.ID_DF_SMS_TYPES
                Where tar.ID_DF_SMS_TYPES Is Null
--                     And src.NEW_ID_DF_SMS_TYPES <> src.ID_DF_SMS_TYPES
                ;

                Insert Into DF_SMS(
                    DF_SMS_TYPES_ID_DF_SMS_TYPES, DF_LANGUAGES_ID_DF_LANGUAGES, DF_SMS_CONTENT
                    , DF_SMS_MERGE_MODEL, DF_SMS_EDITOR_MODEL, DF_SMS_MODEL_DATA, DF_SMS_PREVIEW_MODEL
                )
                Select 
                    src.NEW_DF_SMS_TYPES_ID_DF_SMS_TYPES, src.DF_LANGUAGES_ID_DF_LANGUAGES, src.DF_SMS_CONTENT
                    , src.DF_SMS_MERGE_MODEL, src.DF_SMS_EDITOR_MODEL, src.DF_SMS_MODEL_DATA, src.DF_SMS_PREVIEW_MODEL
                From TMP_BKP_DF_SMS src
                    Left Join DF_SMS tar
                        On src.NEW_DF_SMS_TYPES_ID_DF_SMS_TYPES = tar.DF_SMS_TYPES_ID_DF_SMS_TYPES
                Where tar.DF_SMS_TYPES_ID_DF_SMS_TYPES Is Null 
--                     And src.NEW_DF_SMS_TYPES_ID_DF_SMS_TYPES <> src.DF_SMS_TYPES_ID_DF_SMS_TYPES
                ;
            end ;

            bprb_copy_new_tasks: Begin
                Insert Into DF_TASKS(
                    ID_DF_TASKS, DF_TASK_FAM_ID_DF_TASK_FAM
                    , ADMIN_PARAMS_ID_ADMIN_PARAMS, DF_PRIORITY_ID_DF_PRIORITY
                    , DF_MNGT_LVL_ID_DF_MNGT_LVL
                    , DF_TASKS_ID_DF_TASKS_FALLBACK
                    , DF_TASKS_CODE, DF_TASKS_LABEL
                    , DF_TASKS_IS_FAVORITE, DF_TASKS_COST, DF_TASKS_TRASHED, DF_TASKS_IS_VALIDATION_TASK
                    , DF_TASKS_STATUS, DF_TASKS_SEND_TO_ACCOUNT_MEMBER, DF_TASKS_SEND_TO_ACCOUNT_TEAM
                    , DF_TASKS_SEND_TO_MAIN_CONTACT, DF_TASKS_MULTIPLE_LOAD_ALLOWED
                )
                Select 
                    src.NEW_ID_DF_TASKS, src.NEW_DF_TASK_FAM_ID_DF_TASK_FAM
                    , src.ADMIN_PARAMS_ID_ADMIN_PARAMS, src.DF_PRIORITY_ID_DF_PRIORITY
                    , src.DF_MNGT_LVL_ID_DF_MNGT_LVL
                    , src.NEW_DF_TASKS_ID_DF_TASKS_FALLBACK
                    , src.DF_TASKS_CODE, src.DF_TASKS_LABEL
                    , src.DF_TASKS_IS_FAVORITE, src.DF_TASKS_COST, src.DF_TASKS_TRASHED, src.DF_TASKS_IS_VALIDATION_TASK
                    , src.DF_TASKS_STATUS, src.DF_TASKS_SEND_TO_ACCOUNT_MEMBER, src.DF_TASKS_SEND_TO_ACCOUNT_TEAM
                    , src.DF_TASKS_SEND_TO_MAIN_CONTACT, src.DF_TASKS_MULTIPLE_LOAD_ALLOWED
                From TMP_BKP_DF_TASKS src
                    Left Join DF_TASKS tar
                        On src.NEW_ID_DF_TASKS = tar.ID_DF_TASKS
                Where tar.ID_DF_TASKS is null 
--                     And src.NEW_ID_DF_TASKS <> src.ID_DF_TASKS
                ;

                Insert Into DF_RECIPIENTS(
                    DF_TASKS_ID_DF_TASKS, DF_FUNC_ID_DF_FUNC_MEMBER, DF_FUNC_ID_DF_FUNC_CONTACT
                    , TEAMS_ID_TEAMS, DF_RECIPIENTS_IS_TO, DF_RECIPIENTS_IS_CC, DF_RECIPIENTS_IS_BCC
                )
                Select 
                    src.NEW_DF_TASKS_ID_DF_TASKS, src.NEW_DF_FUNC_ID_DF_FUNC_MEMBER, src.NEW_DF_FUNC_ID_DF_FUNC_CONTACT
                    , src.TEAMS_ID_TEAMS, src.DF_RECIPIENTS_IS_TO, src.DF_RECIPIENTS_IS_CC, src.DF_RECIPIENTS_IS_BCC
                From TMP_BKP_DF_RECIPIENTS src
                    Left Join DF_RECIPIENTS tar
                        On src.NEW_DF_TASKS_ID_DF_TASKS = tar.DF_TASKS_ID_DF_TASKS
                Where tar.DF_TASKS_ID_DF_TASKS is null
--                     And (
--                         src.NEW_DF_TASKS_ID_DF_TASKS <> src.DF_TASKS_ID_DF_TASKS
--                         Or Coalesce(src.NEW_DF_FUNC_ID_DF_FUNC_MEMBER, 0) <> Coalesce(src.DF_FUNC_ID_DF_FUNC_MEMBER, 0)
--                         Or Coalesce(src.NEW_DF_FUNC_ID_DF_FUNC_CONTACT, 0) <> Coalesce(src.DF_FUNC_ID_DF_FUNC_CONTACT, 0)
--                     )
                ;

                Insert Into DF_MEDIAS(
                    DF_TASKS_ID_DF_TASKS
                    , DF_MEDIAS_FORMATS_ID_DF_MEDIAS_FORMATS
                    , MODELS_TYPES_ID_MODELS_TYPES
                    , SMS_TYPES_ID_SMS_TYPES
                    , MSG_TYPES_ID_MSG_TYPES
                    , FAX_TYPES_ID_FAX_TYPES
                )
                Select 
                    src.NEW_DF_TASKS_ID_DF_TASKS
                    , src.DF_MEDIAS_FORMATS_ID_DF_MEDIAS_FORMATS
                    , src.NEW_MODELS_TYPES_ID_MODELS_TYPES
                    , src.NEW_SMS_TYPES_ID_SMS_TYPES
                    , src.NEW_MSG_TYPES_ID_MSG_TYPES
                    , src.FAX_TYPES_ID_FAX_TYPES
                From TMP_BKP_DF_MEDIAS src
                    Left Join DF_MEDIAS tar
                        On src.NEW_DF_TASKS_ID_DF_TASKS = tar.DF_TASKS_ID_DF_TASKS
                Where tar.DF_TASKS_ID_DF_TASKS is null
--                     And (
--                         Coalesce(src.NEW_DF_TASKS_ID_DF_TASKS, 0) <> Coalesce(src.DF_TASKS_ID_DF_TASKS, 0)
--                         Or Coalesce(src.NEW_MODELS_TYPES_ID_MODELS_TYPES, 0) <> Coalesce(src.MODELS_TYPES_ID_MODELS_TYPES, 0)
--                         Or Coalesce(src.NEW_SMS_TYPES_ID_SMS_TYPES, 0) <> Coalesce(src.SMS_TYPES_ID_SMS_TYPES, 0)
--                         Or Coalesce(src.NEW_MSG_TYPES_ID_MSG_TYPES, 0) <> Coalesce(src.MSG_TYPES_ID_MSG_TYPES, 0)
--                     )
                ;

            end ;

--
            bprb_copy_new_taskplans: Begin
                Insert Into DF_TASKS_PLANS(
                    ID_DF_TASKS_PLANS, ADMIN_PARAMS_ID_ADMIN_PARAMS, DF_TASKS_PLANS_CODE, DF_TASKS_PLANS_LABEL
                    , DF_TASKS_PLANS_COMMENT, DF_TASKS_PLANS_TEMPLATE, DF_TASKS_PLANS_TRASH
                    , DF_TASKS_PLANS_PLAN_LOAD_TODAY
                )
                Select 
                    src.NEW_ID_DF_TASKS_PLANS, src.ADMIN_PARAMS_ID_ADMIN_PARAMS, src.DF_TASKS_PLANS_CODE, src.DF_TASKS_PLANS_LABEL
                    , src.DF_TASKS_PLANS_COMMENT, src.DF_TASKS_PLANS_TEMPLATE, src.DF_TASKS_PLANS_TRASH
                    , src.DF_TASKS_PLANS_PLAN_LOAD_TODAY
                From TMP_BKP_DF_TASKS_PLANS src
                    Left Join DF_TASKS_PLANS tar
                        On src.NEW_ID_DF_TASKS_PLANS = tar.ID_DF_TASKS_PLANS
                Where tar.ID_DF_TASKS_PLANS Is Null 
--                     And src.NEW_ID_DF_TASKS_PLANS <> src.ID_DF_TASKS_PLANS
                ;

                Insert Into DF_TASKS_PLANS_has_DF_TASKS(
                    DF_PRIORITY_ID_DF_PRIORITY
                    , DF_TKS_PLANS_ID_DF_TKS_PLANS, DF_TASKS_ID_DF_TASKS
                    , DF_TASKS_FREEZE, DF_TASKS_PREV_TASK_DELAY, DF_TASKS_PREV_TASK_MIN_DELAY
                    , DF_TASKS_SEQUENCE, DF_TASKS_MAX_DEFEREMENT
                )
                Select 
                    src.DF_PRIORITY_ID_DF_PRIORITY
                    , src.NEW_DF_TKS_PLANS_ID_DF_TKS_PLANS, src.NEW_DF_TASKS_ID_DF_TASKS
                    , src.DF_TASKS_FREEZE, src.DF_TASKS_PREV_TASK_DELAY, src.DF_TASKS_PREV_TASK_MIN_DELAY
                    , src.DF_TASKS_SEQUENCE, src.DF_TASKS_MAX_DEFEREMENT
                From TMP_BKP_DF_TASKS_PLANS_has_DF_TASKS src
                    Left Join DF_TASKS_PLANS_has_DF_TASKS tar
                        On src.NEW_DF_TKS_PLANS_ID_DF_TKS_PLANS = tar.DF_TKS_PLANS_ID_DF_TKS_PLANS
                Where tar.DF_TKS_PLANS_ID_DF_TKS_PLANS is null
--                     And (
--                         Coalesce(src.NEW_DF_TKS_PLANS_ID_DF_TKS_PLANS, 0) <> Coalesce(src.DF_TKS_PLANS_ID_DF_TKS_PLANS, 0)
--                         Or Coalesce(src.NEW_DF_TASKS_ID_DF_TASKS, 0) <> Coalesce(src.DF_TASKS_ID_DF_TASKS, 0)
--                     )
                ;
            end ;

        end ;

        bprb_clearprocesses: Begin

            Update ENTITIES 
            Set ENTITIES_PROCESSES_RELATION = null
                , ENTITIES_PROCESSES_RECLAIMS = null
            ;

            Delete tar From PROCESS_TREE tar;

            Delete tar From PROCESS tar;

            Delete tar From TASK_LIST tar;
            Delete tar From TASK_LIST_DESC tar;

            Delete tar From CONDITION_NODE tar;
            Delete tar From CONDITION_TREE tar;
            Delete tar From CONDITIONS tar;


            Delete tar From PROCESS_LIST tar;

            Delete tar From PROCESS_TREE_DESC tar;

            Delete tar From PROCESSES tar;

        end;

        bprb_copyprocesses: Begin

			-- ------------------
			Insert Into PROCESS (
				ID_PROCESS, PRCSS_CODE, PRCSS_EXT_ID, PRCSS_CONTENT, PRCSS_INDEX, PRCSS_TYPE
			)
			Select
				SRC.ID_PROCESS 
                , SRC.PRCSS_CODE, SRC.PRCSS_EXT_ID, SRC.PRCSS_CONTENT, SRC.PRCSS_INDEX, SRC.PRCSS_TYPE
			From TMP_BKP_PROCESS SRC
			;

			-- ------------------
			Insert Into  TASK_LIST_DESC(
				ID_TASK_LIST_DESC, TSKLSTDSC_CODE, TSKLSTDSC_EXT_ID, TSKLSTDSC_LABEL
			)
			Select
				ID_TASK_LIST_DESC, TSKLSTDSC_CODE, TSKLSTDSC_EXT_ID, TSKLSTDSC_LABEL
			From TMP_BKP_TASK_LIST_DESC
			;

			-- ------------------
			Insert Into TASK_LIST (
                ID_TASK_LIST
                , TSKLST_ID_TASK_LIST_DESC, TSKLST_INDEX, TSKLST_CODE
                , TSKLST_EXT_ID, TSKLST_EXT_REF
                , TSKLST_CONTENT, TSKLST_TASK_TYPE
                , TSKLST_ID_DF_TASKS, TSKLST_ID_DF_TASKS_PLANS, TSKLST_ID_DF_ACCOUNTS_STATUS
                , TSKLST_VARNAME, TSKLST_VARTYPE, TSKLST_VALUE
                , TSKLST_VALTYPE 
			)
			Select
                ID_TASK_LIST
                , TSKLST_ID_TASK_LIST_DESC, TSKLST_INDEX, TSKLST_CODE
                , TSKLST_EXT_ID, TSKLST_EXT_REF
                , TSKLST_CONTENT, TSKLST_TASK_TYPE
                , NEW_TSKLST_ID_DF_TASKS, NEW_TSKLST_ID_DF_TASKS_PLANS, NEW_TSKLST_ID_DF_ACCOUNTS_STATUS
                , TSKLST_VARNAME, TSKLST_VARTYPE, TSKLST_VALUE
                , TSKLST_VALTYPE 
				
			From TMP_BKP_TASK_LIST
			;

			-- ------------------
			Insert Into CONDITIONS (
                ID_CONDITIONS, CNDTNS_CODE, CNDTNS_TYPE, CNDTNS_EXPRESSION
			)
			Select
                ID_CONDITIONS, CNDTNS_CODE, CNDTNS_TYPE, CNDTNS_EXPRESSION
			From TMP_BKP_CONDITIONS
			;

			-- ------------------
			Insert Into CONDITION_TREE (
                ID_CONDITION_TREE, CNDTNTR_ID_CONDITIONS
                , CNDTNTR_BRD_LEFT, CNDTNTR_BRD_RIGHT, CNDTNTR_LEVEL
			)
			Select
                ID_CONDITION_TREE, CNDTNTR_ID_CONDITIONS
                , CNDTNTR_BRD_LEFT, CNDTNTR_BRD_RIGHT, CNDTNTR_LEVEL
			From TMP_BKP_CONDITION_TREE
			;

			-- ------------------
			Insert Into CONDITION_NODE (
                ID_CONDITION_NODE
                , CNDTNND_OPERATOR, CNDTNND_ARGUMENT_LEFT, CNDTNND_ARGUMENT_RIGHT, CNDTNND_INDEX
                , CNDTNND_EXPRESSION
                , CNDTNND_ID_CONDITION_TREE
                , CNDTNND_OPTIONS_LEFT, CNDTNND_OPTIONS_RIGHT
			)
			Select
                ID_CONDITION_NODE
                , CNDTNND_OPERATOR, CNDTNND_ARGUMENT_LEFT, CNDTNND_ARGUMENT_RIGHT, CNDTNND_INDEX
                , CNDTNND_EXPRESSION
                , CNDTNND_ID_CONDITION_TREE
                , CNDTNND_OPTIONS_LEFT, CNDTNND_OPTIONS_RIGHT
			From TMP_BKP_CONDITION_NODE
			;

			-- ------------------
			Insert Into PROCESS_TREE_DESC (
                ID_PROCESS_TREE_DESC
                , PRCSSTRDSC_CODE, PROCESS_TREE_MAX_DEPTH, PROCESS_TREE_DESC_LABEL
			)
			Select
                ID_PROCESS_TREE_DESC
                , PRCSSTRDSC_CODE, PROCESS_TREE_MAX_DEPTH, PROCESS_TREE_DESC_LABEL
			From TMP_BKP_PROCESS_TREE_DESC
			;

			-- ------------------
			Insert Into PROCESS_TREE (
                ID_PROCESS_TREE
                , PRCSSTR_ID_PROCESS, PRCSSTR_ID_TASK_LIST_DESC
                , PRCSSTR_NODE, PRCSSTR_LEVEL, PRCSSTR_BRD_LEFT, PRCSSTR_BRD_RIGHT
                , PRCSSTR_CONTENT
                , PRCSSTR_ID_CONDITION, PRCSSTR_ID_PROCESS_TREE_DESC
                , PRCSSTR_CONFIG, PRCSSTR_BRD_LEFT_MNS1, PRCSSTR_BRD_RIGHT_PLS1
			)
			Select
                ID_PROCESS_TREE
                , PRCSSTR_ID_PROCESS, PRCSSTR_ID_TASK_LIST_DESC
                , PRCSSTR_NODE, PRCSSTR_LEVEL, PRCSSTR_BRD_LEFT, PRCSSTR_BRD_RIGHT
                , PRCSSTR_CONTENT
                , PRCSSTR_ID_CONDITION, PRCSSTR_ID_PROCESS_TREE_DESC
                , PRCSSTR_CONFIG, PRCSSTR_BRD_LEFT_MNS1, PRCSSTR_BRD_RIGHT_PLS1
				
			From TMP_BKP_PROCESS_TREE
			;


            -- ------------------
			Insert Into PROCESSES(
				ID_PROCESSES, 
                ADMIN_PARAMS_ID_ADMIN_PARAMS, PROCESSES_LABEL, PROCESSES_TYPE
			)
			Select 
				SRC.ID_PROCESSES, 
                SRC.ADMIN_PARAMS_ID_ADMIN_PARAMS, SRC.PROCESSES_LABEL, SRC.PROCESSES_TYPE
			From TMP_BKP_PROCESSES SRC
			;

			-- ------------------
			Insert Into PROCESS_LIST (
                ID_PROCESS_LIST
                , PRCSSLST_ID_PROCESSES
                , PRCSSLST_INDEX
                , PRCSSLST_ID_PROCESS_TREE_DESC
                , PRCSSLST_STATUS
			)
			Select
                ID_PROCESS_LIST
                , PRCSSLST_ID_PROCESSES
                , PRCSSLST_INDEX
                , PRCSSLST_ID_PROCESS_TREE_DESC
                , PRCSSLST_STATUS
				
			From TMP_BKP_PROCESS_LIST
			;

        end;

        bprb_updateactions: Begin

            bprb_updatea_MODELS: Begin
                if @bprb_existingUpdateMode & @bprb_upd_mask_MODELS =0 Then
                    Leave  bprb_updatea_MODELS ;
                End if;
                Update TMP_BKP_DF_MODELS_TYPES src
                    Inner Join DF_MODELS_TYPES tar
                        On src.NEW_ID_DF_MODELS_TYPES = tar.ID_DF_MODELS_TYPES
                Set
                    tar.ADMIN_PARAMS_ID_ADMIN_PARAMS = src.ADMIN_PARAMS_ID_ADMIN_PARAMS
                    , tar.DF_MODELS_TYPES_LABEL = src.DF_MODELS_TYPES_LABEL
                    , tar.DF_MODELS_TYPES_NOTES = src.DF_MODELS_TYPES_NOTES
                    , tar.DF_MODELS_TYPES_TYPES_CATEG = src.DF_MODELS_TYPES_TYPES_CATEG
                    , tar.DF_MODELS_TYPES_TRASH = src.DF_MODELS_TYPES_TRASH
                ;

                Update TMP_BKP_DF_MODELS src
                Inner Join DF_MODELS tar
                    On src.NEW_MODELS_TYPES_ID_MODELS_TYPES = tar.MODELS_TYPES_ID_MODELS_TYPES
                    And src.DF_LANG_ID_DF_LANG = tar.DF_LANG_ID_DF_LANG
                Set
                    tar.DF_MERGE_MODEL = src.DF_MERGE_MODEL
                    , tar.DF_EDITOR_MODEL = src.DF_EDITOR_MODEL
                    , tar.DF_MODELS_DATA = src.DF_MODELS_DATA
                    , tar.DF_MODELS_PREVIEW_MODEL = src.DF_MODELS_PREVIEW_MODEL
                ;

                Insert Into DF_MODELS(
                    DF_LANG_ID_DF_LANG, MODELS_TYPES_ID_MODELS_TYPES, DF_MERGE_MODEL, DF_EDITOR_MODEL
                    , DF_MODELS_DATA, DF_MODELS_PREVIEW_MODEL
                )
                Select 
                    src.DF_LANG_ID_DF_LANG, src.NEW_MODELS_TYPES_ID_MODELS_TYPES, src.DF_MERGE_MODEL, src.DF_EDITOR_MODEL
                    , src.DF_MODELS_DATA, src.DF_MODELS_PREVIEW_MODEL
                From TMP_BKP_DF_MODELS src
                    Left Join DF_MODELS tar
                        On src.NEW_MODELS_TYPES_ID_MODELS_TYPES = tar.MODELS_TYPES_ID_MODELS_TYPES
                        And src.DF_LANG_ID_DF_LANG = tar.DF_LANG_ID_DF_LANG
                Where tar.DF_LANG_ID_DF_LANG Is Null
                ;
            end;
-- debug
-- ROLLBACK to savepoint bprb_upd_ids_savepoint;
-- leave bprb_main;
            bprb_updatea_MESSAGES: Begin

                if @bprb_existingUpdateMode & @bprb_upd_mask_MESSAGES =0 Then
                    Leave  bprb_updatea_MESSAGES ;
                End if;

                Update TMP_BKP_DF_MESSAGES_TYPES src
                    Inner Join DF_MESSAGES_TYPES tar
                        On tar.ID_DF_MESSAGES_TYPES = src.NEW_ID_DF_MESSAGES_TYPES
                Set
                    tar.ADMIN_PARAMS_ID_ADMIN_PARAMS = src.ADMIN_PARAMS_ID_ADMIN_PARAMS
                    , tar.DF_MESSAGES_TYPES_LABEL = src.DF_MESSAGES_TYPES_LABEL
                    , tar.DF_MESSAGES_TYPES_NOTES = src.DF_MESSAGES_TYPES_NOTES
                    , tar.DF_MESSAGES_TYPES_TRASH = src.DF_MESSAGES_TYPES_TRASH
                ;

                Update TMP_BKP_DF_MESSAGES src
                    Inner Join DF_MESSAGES tar
                        On tar.DF_MSG_TYPES_ID_DF_MSG_TYPES = NEW_DF_MSG_TYPES_ID_DF_MSG_TYPES
                        And tar.DF_LANGUAGES_ID_DF_LANGUAGES = src.DF_LANGUAGES_ID_DF_LANGUAGES
                Set
                    tar.DF_MESSAGES_MERGE_MODEL = src.DF_MESSAGES_MERGE_MODEL
                    , tar.DF_MESSAGES_EDITOR_MODEL = src.DF_MESSAGES_EDITOR_MODEL
                    , tar.DF_MESSAGES_MODEL_DATA = src.DF_MESSAGES_MODEL_DATA
                    , tar.DF_MESSAGES_PREVIEW_MODEL = src.DF_MESSAGES_PREVIEW_MODEL
                ;

                Insert Into DF_MESSAGES(
                    DF_LANGUAGES_ID_DF_LANGUAGES, DF_MSG_TYPES_ID_DF_MSG_TYPES, DF_MESSAGES_MERGE_MODEL
                    , DF_MESSAGES_EDITOR_MODEL, DF_MESSAGES_MODEL_DATA, DF_MESSAGES_PREVIEW_MODEL
                )
                Select 
                    src.DF_LANGUAGES_ID_DF_LANGUAGES, src.NEW_DF_MSG_TYPES_ID_DF_MSG_TYPES, src.DF_MESSAGES_MERGE_MODEL
                    , src.DF_MESSAGES_EDITOR_MODEL, src.DF_MESSAGES_MODEL_DATA, src.DF_MESSAGES_PREVIEW_MODEL
                    
                From TMP_BKP_DF_MESSAGES src
                    Left Join DF_MESSAGES tar
                        On src.NEW_DF_MSG_TYPES_ID_DF_MSG_TYPES = tar.DF_MSG_TYPES_ID_DF_MSG_TYPES 
                        And src.DF_LANGUAGES_ID_DF_LANGUAGES = tar.DF_LANGUAGES_ID_DF_LANGUAGES
                Where tar.DF_LANGUAGES_ID_DF_LANGUAGES Is null
                ;
            end;
-- debug
-- ROLLBACK to savepoint bprb_upd_ids_savepoint;
-- leave bprb_main;

            bprb_updatea_SMS: Begin
                if @bprb_existingUpdateMode & @bprb_upd_mask_SMS =0 Then
                    Leave  bprb_updatea_SMS ;
                End if;

                Update TMP_BKP_DF_SMS_TYPES src
                     Inner Join DF_SMS_TYPES tar  
                        On src.NEW_ID_DF_SMS_TYPES = tar.ID_DF_SMS_TYPES
                Set
                    tar.ADMIN_PARAMS_ID_ADMIN_PARAMS = src.ADMIN_PARAMS_ID_ADMIN_PARAMS
                    , tar.DF_SMS_TYPES_LABEL = src.DF_SMS_TYPES_LABEL
                    , tar.DF_SMS_TYPES_NOTES = src.DF_SMS_TYPES_NOTES
                    , tar.DF_SMS_TYPES_TRASH = src.DF_SMS_TYPES_TRASH
                ;

                Update TMP_BKP_DF_SMS src
                     Inner Join DF_SMS tar  
                        On tar.DF_SMS_TYPES_ID_DF_SMS_TYPES = src.NEW_DF_SMS_TYPES_ID_DF_SMS_TYPES
                        And tar.DF_LANGUAGES_ID_DF_LANGUAGES = src.DF_LANGUAGES_ID_DF_LANGUAGES
                Set
                    tar.DF_SMS_CONTENT = src.DF_SMS_CONTENT
                    , tar.DF_SMS_MERGE_MODEL = src.DF_SMS_MERGE_MODEL
                    , tar.DF_SMS_EDITOR_MODEL = src.DF_SMS_EDITOR_MODEL
                    , tar.DF_SMS_MODEL_DATA = src.DF_SMS_MODEL_DATA
                    , tar.DF_SMS_PREVIEW_MODEL = src.DF_SMS_PREVIEW_MODEL
                ;

                Insert Into DF_SMS(
                    DF_SMS_TYPES_ID_DF_SMS_TYPES, DF_LANGUAGES_ID_DF_LANGUAGES, DF_SMS_CONTENT
                    , DF_SMS_MERGE_MODEL, DF_SMS_EDITOR_MODEL, DF_SMS_MODEL_DATA, DF_SMS_PREVIEW_MODEL
                )
                Select 
                    src.NEW_DF_SMS_TYPES_ID_DF_SMS_TYPES, src.DF_LANGUAGES_ID_DF_LANGUAGES, src.DF_SMS_CONTENT
                    , src.DF_SMS_MERGE_MODEL, src.DF_SMS_EDITOR_MODEL, src.DF_SMS_MODEL_DATA, src.DF_SMS_PREVIEW_MODEL
                From TMP_BKP_DF_SMS src
                    Left Join DF_SMS tar  
                        On src.NEW_DF_SMS_TYPES_ID_DF_SMS_TYPES = tar.DF_SMS_TYPES_ID_DF_SMS_TYPES
                        And src.DF_LANGUAGES_ID_DF_LANGUAGES = tar.DF_LANGUAGES_ID_DF_LANGUAGES 
                Where tar.DF_LANGUAGES_ID_DF_LANGUAGES  Is Null
                ;

            end;
-- debug
-- ROLLBACK to savepoint bprb_upd_ids_savepoint;
-- leave bprb_main;

            bprb_updatea_TASKS : Begin
                if @bprb_existingUpdateMode & @bprb_upd_mask_TASKS  =0 Then
                    Leave  bprb_updatea_TASKS  ;
                End if;

                Update TMP_BKP_DF_TASKS src
                     Inner Join DF_TASKS tar  
                        On tar.ID_DF_TASKS = src.NEW_ID_DF_TASKS
                Set
                    tar.DF_TASK_FAM_ID_DF_TASK_FAM = src.NEW_DF_TASK_FAM_ID_DF_TASK_FAM
                    , tar.ADMIN_PARAMS_ID_ADMIN_PARAMS = src.ADMIN_PARAMS_ID_ADMIN_PARAMS
                    , tar.DF_PRIORITY_ID_DF_PRIORITY = src.DF_PRIORITY_ID_DF_PRIORITY
                    , tar.DF_MNGT_LVL_ID_DF_MNGT_LVL = src.DF_MNGT_LVL_ID_DF_MNGT_LVL
                    , tar.DF_TASKS_ID_DF_TASKS_FALLBACK = src.NEW_DF_TASKS_ID_DF_TASKS_FALLBACK
                    , tar.DF_TASKS_LABEL = src.DF_TASKS_LABEL
                    , tar.DF_TASKS_IS_FAVORITE = src.DF_TASKS_IS_FAVORITE
                    , tar.DF_TASKS_COST = src.DF_TASKS_COST
                    , tar.DF_TASKS_TRASHED = src.DF_TASKS_TRASHED
                    , tar.DF_TASKS_IS_VALIDATION_TASK = src.DF_TASKS_IS_VALIDATION_TASK
                    , tar.DF_TASKS_STATUS = src.DF_TASKS_STATUS
                    , tar.DF_TASKS_SEND_TO_ACCOUNT_MEMBER = src.DF_TASKS_SEND_TO_ACCOUNT_MEMBER
                    , tar.DF_TASKS_SEND_TO_ACCOUNT_TEAM = src.DF_TASKS_SEND_TO_ACCOUNT_TEAM
                    , tar.DF_TASKS_SEND_TO_MAIN_CONTACT = src.DF_TASKS_SEND_TO_MAIN_CONTACT
                    , tar.DF_TASKS_MULTIPLE_LOAD_ALLOWED = src.DF_TASKS_MULTIPLE_LOAD_ALLOWED
                ;

                Update TMP_BKP_DF_RECIPIENTS src
                     Inner Join DF_RECIPIENTS tar  
                        On tar.DF_TASKS_ID_DF_TASKS = src.NEW_DF_TASKS_ID_DF_TASKS
                Set
                    tar.DF_FUNC_ID_DF_FUNC_MEMBER = src.NEW_DF_FUNC_ID_DF_FUNC_MEMBER
                    , tar.DF_FUNC_ID_DF_FUNC_CONTACT = src.NEW_DF_FUNC_ID_DF_FUNC_CONTACT
                    , tar.TEAMS_ID_TEAMS = src.TEAMS_ID_TEAMS
                    , tar.DF_RECIPIENTS_IS_TO = src.DF_RECIPIENTS_IS_TO
                    , tar.DF_RECIPIENTS_IS_CC = src.DF_RECIPIENTS_IS_CC
                    , tar.DF_RECIPIENTS_IS_BCC = src.DF_RECIPIENTS_IS_BCC
                ;

                Insert Into DF_RECIPIENTS(
                    DF_TASKS_ID_DF_TASKS, DF_FUNC_ID_DF_FUNC_MEMBER, DF_FUNC_ID_DF_FUNC_CONTACT
                    , TEAMS_ID_TEAMS, DF_RECIPIENTS_IS_TO, DF_RECIPIENTS_IS_CC, DF_RECIPIENTS_IS_BCC
                )
                Select 
                    src.NEW_DF_TASKS_ID_DF_TASKS, src.NEW_DF_FUNC_ID_DF_FUNC_MEMBER, src.NEW_DF_FUNC_ID_DF_FUNC_CONTACT
                    , src.TEAMS_ID_TEAMS, src.DF_RECIPIENTS_IS_TO, src.DF_RECIPIENTS_IS_CC, src.DF_RECIPIENTS_IS_BCC
                From TMP_BKP_DF_RECIPIENTS src
                    Left Join DF_RECIPIENTS tar  
                        On src.NEW_DF_TASKS_ID_DF_TASKS = tar.DF_TASKS_ID_DF_TASKS 
                Where
                    tar.DF_TASKS_ID_DF_TASKS Is null
                ;
            end;
-- debug
-- ROLLBACK to savepoint bprb_upd_ids_savepoint;
-- leave bprb_main;

            bprb_updatea_TASKSPLANS: Begin
                if @bprb_existingUpdateMode & @bprb_upd_mask_TASKSPLANS =0 Then
                    Leave  bprb_updatea_TASKSPLANS ;
                End if;

                Update TMP_BKP_DF_TASKS_PLANS src
                     Inner Join DF_TASKS_PLANS tar  
                        On tar.ID_DF_TASKS_PLANS = src.NEW_ID_DF_TASKS_PLANS
                Set
                    tar.ADMIN_PARAMS_ID_ADMIN_PARAMS = src.ADMIN_PARAMS_ID_ADMIN_PARAMS
                    , tar.DF_TASKS_PLANS_LABEL = src.DF_TASKS_PLANS_LABEL
                    , tar.DF_TASKS_PLANS_COMMENT = src.DF_TASKS_PLANS_COMMENT
                    , tar.DF_TASKS_PLANS_TEMPLATE = src.DF_TASKS_PLANS_TEMPLATE
                    , tar.DF_TASKS_PLANS_TRASH = src.DF_TASKS_PLANS_TRASH
                    , tar.DF_TASKS_PLANS_PLAN_LOAD_TODAY = src.DF_TASKS_PLANS_PLAN_LOAD_TODAY
                ;

                Delete tar
                From TMP_BKP_DF_TASKS_PLANS_has_DF_TASKS src
                    Inner Join DF_TASKS_PLANS_has_DF_TASKS tar  
                        On tar.DF_TKS_PLANS_ID_DF_TKS_PLANS = src.NEW_DF_TKS_PLANS_ID_DF_TKS_PLANS
                
                ;

                Insert Into DF_TASKS_PLANS_has_DF_TASKS(
                    DF_PRIORITY_ID_DF_PRIORITY
                    , DF_TKS_PLANS_ID_DF_TKS_PLANS, DF_TASKS_ID_DF_TASKS
                    , DF_TASKS_FREEZE, DF_TASKS_PREV_TASK_DELAY, DF_TASKS_PREV_TASK_MIN_DELAY
                    , DF_TASKS_SEQUENCE, DF_TASKS_MAX_DEFEREMENT
                )
                Select 
                    src.DF_PRIORITY_ID_DF_PRIORITY
                    , src.NEW_DF_TKS_PLANS_ID_DF_TKS_PLANS, src.NEW_DF_TASKS_ID_DF_TASKS
                    , src.DF_TASKS_FREEZE, src.DF_TASKS_PREV_TASK_DELAY, src.DF_TASKS_PREV_TASK_MIN_DELAY
                    , src.DF_TASKS_SEQUENCE, src.DF_TASKS_MAX_DEFEREMENT
                From TMP_BKP_DF_TASKS_PLANS_has_DF_TASKS src
                    Left Join DF_TASKS_PLANS_has_DF_TASKS tar  
                        On tar.DF_TKS_PLANS_ID_DF_TKS_PLANS = src.NEW_DF_TKS_PLANS_ID_DF_TKS_PLANS
                Where tar.DF_TKS_PLANS_ID_DF_TKS_PLANS Is null
                ;

            end;


        end;
-- debug
-- rollback ;
-- leave bprb_main;
        if @bprb_useTransaction Then
            commit;
            set transactionStarted = 0;
        End if;
--         ROLLBACK to savepoint bprb_upd_ids_savepoint;
--         leave bprb_main;
  
    End; 

End //

Delimiter ;