Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
*/

Drop Procedure if exists LOADER_MATCHINGCODES_COPY//

Create Procedure LOADER_MATCHINGCODES_COPY (
    In batchnum int
)
Begin
	Set @lmc_batchnum = batchnum;
    cleanQuerys: Begin
        call LOADER_MATCHINGCODES_ENTRIES_DELETE(@lmc_batchnum);
        call LOADER_MATCHINGCODES_DELETE(@lmc_batchnum);
    end ;

	initVars: Begin
		Set @lmc_idEntities = GET_LOADER_INFO('ID_ENTITIES', @lmc_batchnum);
		Set @lmc_idTemplateLoader = GET_LOADER_INFO('ID_TEMPLATE_LOADER', @lmc_batchnum);

		-- obtention du mode fast du schema utilisé
	    -- , si non paramtré utilisation du mode fast du schema par défaut de l'entitié
		-- sinon pas d'utilisation du mode fast par défaut
		set @lmc_fastStatusModeCode = 'MTCHNG-FAST-STT' ;
		set @lmc_avgStatusModeCode = 'MTCHNG-AVG-STT' ;

		set @lmc_fastStatusMode = Coalesce(
					(
						Select
-- 							Case When Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') = @lmc_fastStatusModeCode Then 1 Else 0 End
							Case Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') 
								When @lmc_fastStatusModeCode Then 1 
								When @lmc_avgStatusModeCode Then 2 
								Else 0 
							End
						From
						
							TEMPLATE_LOADER TMPLDR_FSLT
							Inner Join TEMPL_LOADER_has_TEMPL_OPT TMPLDR_FSLTHASOPT
								On TMPLDR_FSLT.ID_TEMPLATE_LOADER = TMPLDR_FSLTHASOPT.TEMPL_LOADER_ID_TEMPL_LOADER
							Inner Join TEMPLATE_OPTIONS TMPLOPT_FSLT
								On TMPLDR_FSLTHASOPT.TEMPL_OPT_ID_TEMPL_OPT = TMPLOPT_FSLT.ID_TEMPLATE_OPTIONS
-- 								And TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE = @lmc_fastStatusModeCode
								And TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE In (@lmc_fastStatusModeCode, @lmc_avgStatusModeCode)
						where 1 = 1
							And TMPLDR_FSLT.ID_TEMPLATE_LOADER = @lmc_idTemplateLoader
					)
					, (
						Select
-- 							Case When Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') = @lmc_fastStatusModeCode Then 1 Else 0 End
							Case Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') 
								When @lmc_fastStatusModeCode Then 1 
								When @lmc_avgStatusModeCode Then 2 
								Else 0 
							End
						From
							ENTITIES ENTT

							Left Join (
									TEMPLATE_LOADER TMPLDR_FSLT
									Inner Join TEMPL_LOADER_has_TEMPL_OPT TMPLDR_FSLTHASOPT
										On TMPLDR_FSLT.ID_TEMPLATE_LOADER = TMPLDR_FSLTHASOPT.TEMPL_LOADER_ID_TEMPL_LOADER
									Inner Join TEMPLATE_OPTIONS TMPLOPT_FSLT
										On TMPLDR_FSLTHASOPT.TEMPL_OPT_ID_TEMPL_OPT = TMPLOPT_FSLT.ID_TEMPLATE_OPTIONS
-- 										And TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE = @lmc_fastStatusModeCode
										And TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE In (@lmc_fastStatusModeCode, @lmc_avgStatusModeCode)
								) On ENTT.TEMPL_LOADER_ID_TEMPL_LOADER = TMPLDR_FSLT.ID_TEMPLATE_LOADER
						where 1 = 1
							And ENTT.ID_ENTITIES = @lmc_idEntities 
					)
					, 0
				)
			;
	end ;

    insertQuerys: Begin
        call CASH_AUTOINCREMENT_VALUE('MATCHINGCODES', @lmc_lastid);

        call LOADER_MATCHINGCODES_INSERT(@lmc_batchnum, @lmc_lastid);
        call LOADER_MATCHINGCODES_ENTRIES_INSERT(@lmc_batchnum, @lmc_lastid, @lmc_fastStatusMode);
        
    end ;



    -- @lmc_temp : à déplacer dans business rules du batch
    Set @lmc_max_batch = GET_LOADER_INFO('MAX_BATCH', @lmc_batchnum);

    if @lmc_batchnum = @lmc_max_batch Then

		call BUSINESS_COMPUTE_ENTRIES_DATA_ENTITIES_MATCHINGCODES_LAST(@lmc_idEntities, null, @lmc_fastStatusMode);

		-- determination du mode de lettrage inter / intra compte
		set @lmc_letteringModeAccounts = 'MTCHNG-IN-ACCNT';
		set @lmc_letteringModeEntities = 'MTCHNG-IN-ENTTS';

-- 		set @lmc_letteringMode = Coalesce(
-- 					(
-- 						Select
-- 							TMPLOPT_MDLT.TEMPLATE_OPTIONS_CODE
-- 						From
-- 						
-- 							TEMPLATE_LOADER TMPLDR_MDLT
-- 							Inner Join TEMPL_LOADER_has_TEMPL_OPT TMPLDR_MDLTHASOPT
-- 								On TMPLDR_MDLT.ID_TEMPLATE_LOADER = TMPLDR_MDLTHASOPT.TEMPL_LOADER_ID_TEMPL_LOADER
-- 							Inner Join TEMPLATE_OPTIONS TMPLOPT_MDLT
-- 								On TMPLDR_MDLTHASOPT.TEMPL_OPT_ID_TEMPL_OPT = TMPLOPT_MDLT.ID_TEMPLATE_OPTIONS
-- 								And TMPLOPT_MDLT.TEMPLATE_OPTIONS_CODE = @lmc_letteringModeEntities
-- 						where 1 = 1
-- 							And TMPLDR_MDLT.ID_TEMPLATE_LOADER = @lmc_idTemplateLoader
-- 							And TMPLOPT_MDLT.TEMPLATE_OPTIONS_CODE = @lmc_letteringModeEntities
-- 					)
-- 					, @lmc_letteringModeAccounts
-- 				)
-- 			;
		call LOADER_MATCHINGCODES_UPDATE(@lmc_batchnum);

        

-- 		if @lmc_letteringMode = @lmc_letteringModeEntities Then
-- 			-- mode de letrage inter-compte (intra entitié)
-- 			Call BUSINESS_CALCULATE_ENTITIES_MATCHING_CODE_STATE(@lmc_idEntities, @lmc_fastStatusMode, null);
-- 		Else 
-- 			-- mode de lettrage intra-compte (defaut)
-- 			Call BUSINESS_CALCULATE_ENTRIES_MATCHING_CODE_STATE(null, @lmc_fastStatusMode, null);
-- 		End if;

		Call BUSINESS_CALCULATE_MATCHING_CODE_STATE(null, @lmc_idEntities, null); -- doit être lancé apres BUSINESS_COMPUTE_ENTRIES_DATA_MATCHINGCODES_LAST

		Call LOADER_MATCHINGCODES_RECLAIMS_DETAILS_UPDATE(@lmc_batchnum); -- doit être lancer apres call BUSINESS_CALCULATE_MATCHING_CODE_STATE;
		Call LOADER_MATCHINGCODES_RECLAIMS_DETAILS_INSERT(@lmc_batchnum); -- doit être lancer apres LOADER_MATCHINGCODES_RECLAIMS_DETAILS_UPDATE
		Call LOADER_MATCHINGCODES_RECLAIMS_SUMMARIES_INSERT	(@lmc_batchnum); -- doit être lancer apres LOADER_MATCHINGCODES_RECLAIMS_DETAILS_INSERT
    End if;


End //

Delimiter ;