Delimiter //
/**
 * Author:  armen
 * corrige le mappage du code lettrage (MATCHINGCODES_CODE, MATCHINGCODES_ACCOUNTS_CODE)
 * par rapport à l'import d'import :
 *  - lettrage inter-compte, intra entité (MTCHNG-IN-ENTTS) 
 *  - lettrage intra-compte (MTCHNG-IN-ACCNT) 
 */

Drop Procedure if Exists LOADER_MATCHINGCODE_NORMALIZE_MAP_FIX_MATCHINGCODE_FIELD//
Create Procedure LOADER_MATCHINGCODE_NORMALIZE_MAP_FIX_MATCHINGCODE_FIELD(
)
Begin
    lmcnmfmcf_main: Begin

        Set @lmcnmfmcf_tmplid = GET_LOADER_INFO('ID_TEMPLATE_LOADER', 1);

        Set @lmcnmfmcf_optmtacc = 'MTCHNG-IN-ACCNT';
        Set @lmcnmfmcf_optmtent = 'MTCHNG-IN-ENTTS';

        Set @lmcnmfmcf_optmtchin = Coalesce(
                    (
                        Select
                            tplo.TEMPLATE_OPTIONS_CODE
                        from TEMPLATE_LOADER tl
                            Inner Join TEMPL_LOADER_has_TEMPL_OPT tlhto
                                On tl.ID_TEMPLATE_LOADER = tlhto.TEMPL_LOADER_ID_TEMPL_LOADER
                            Inner Join TEMPLATE_OPTIONS tplo
                                On tlhto.TEMPL_OPT_ID_TEMPL_OPT = tplo.ID_TEMPLATE_OPTIONS
                        where
                            tl.ID_TEMPLATE_LOADER = @lmcnmfmcf_tmplid
                            And tplo.TEMPLATE_OPTIONS_CODE In (@lmcnmfmcf_optmtacc, @lmcnmfmcf_optmtent)
                    )
                    , @lmcnmfmcf_optmtacc 
                )
            ;
    
        if @lmcnmfmcf_optmtchin = @lmcnmfmcf_optmtacc Then
            -- correction mappage MATCHINGCODES_CODE => MATCHINGCODES_ACCOUNTS_CODE
            Update 
                LOADER_MATCHINGCODES lm  
                Inner Join LOADER_STATUS ls
                    On lm.LOADER_STATUS_ID_LOADER_STATUS = ls.ID_LOADER_STATUS
            Set 
                lm.MATCHINGCODES_ACCOUNTS_CODE = lm.MATCHINGCODES_CODE
            Where
                ls.LOADER_STATUS_ERRCOUNT = 0
                And Coalesce(lm.MATCHINGCODES_ACCOUNTS_CODE, '') = '' 
            ;
            -- purge champ MATCHINGCODES_CODE
            Update 
                LOADER_MATCHINGCODES lm  
                Inner Join LOADER_STATUS ls
                    On lm.LOADER_STATUS_ID_LOADER_STATUS = ls.ID_LOADER_STATUS
            Set 
                lm.MATCHINGCODES_CODE = null
            Where
                ls.LOADER_STATUS_ERRCOUNT = 0
            ;

        Else
            -- correction mappage MATCHINGCODES_ACCOUNTS_CODE => MATCHINGCODES_CODE
            Update 
                LOADER_MATCHINGCODES lm  
                Inner Join LOADER_STATUS ls
                    On lm.LOADER_STATUS_ID_LOADER_STATUS = ls.ID_LOADER_STATUS
            Set 
                lm.MATCHINGCODES_CODE = lm.MATCHINGCODES_ACCOUNTS_CODE
            Where
                ls.LOADER_STATUS_ERRCOUNT = 0
                And Coalesce(lm.MATCHINGCODES_CODE, '') = '' 
            ;
            -- purge champ MATCHINGCODES_ACCOUNTS_CODE
            Update 
                LOADER_MATCHINGCODES lm  
                Inner Join LOADER_STATUS ls
                    On lm.LOADER_STATUS_ID_LOADER_STATUS = ls.ID_LOADER_STATUS
            Set 
                lm.MATCHINGCODES_ACCOUNTS_CODE = null
            Where
                ls.LOADER_STATUS_ERRCOUNT = 0
            ;
        End If;
    End ;
End //
Delimiter ;