Delimiter $$
/**
 * compute BUSINESS_COMPUTE_ENTRIES_DATA_MATCHINGCODES_LAST
 * dernier lettrage par mouvements
 * rempli la table ENTRIES_COMPUTED_DATA, champ ENTRIES_COMPUTED_DATA_EXTID, clé LASTMATCH
 */ 
Drop Procedure if Exists BUSINESS_COMPUTE_ENTRIES_DATA_MATCHINGCODES_LAST $$
Create Procedure BUSINESS_COMPUTE_ENTRIES_DATA_MATCHINGCODES_LAST(In refdate DateTime, In idaccounts Int unsigned, In identities Int unsigned)
Begin
    entr_computed_data_lastmtchcde: begin

		set @bcedml_idacounts = ifnull(idaccounts, 0);
		

        if refdate is null Then
            set refdate = AMO_BAL_REFDATE();
        End if ;

		-- prise en compte du monde de calcul optimisé
		-- on ne traite pas les écriture lettré totalement
		-- on doit prendre en compte l'option d'import pour chaque entité
		set @bcedml_fastStatusModeCode = 'MTCHNG-FAST-STT' ;
		set @bcedml_avgStatusModeCode = 'MTCHNG-AVG-STT' ;

		Set @bcedml_idEntities = Coalesce(identities, (select ENTITIES_ID_ENTITIES From ACCOUNTS where ID_ACCOUNTS = @bcedml_idacounts )
							)
						;

        if @bcedml_idEntities Is null Then
            Set @bcedml_enttMinVal = 0;
            Set @bcedml_enttMaxVal = 1000;
        Else
            Set @bcedml_enttMinVal = identities;
            Set @bcedml_enttMaxVal = identities;
        End if;
		
		
		Set @bcedml_OpenEntry = (Select ID_DF_MATCHINGCODE_STATE From DF_MATCHINGCODE_STATE Where DF_MATCHINGCODE_STATE_CODE = 'OPEN ENTRY');
		Set @bcedml_Partial = (Select ID_DF_MATCHINGCODE_STATE From DF_MATCHINGCODE_STATE Where DF_MATCHINGCODE_STATE_CODE = 'PARTIAL MATCHING CODE');
		Set @bcedml_ClosedEntry = (Select ID_DF_MATCHINGCODE_STATE From DF_MATCHINGCODE_STATE Where DF_MATCHINGCODE_STATE_CODE = 'CLOSED ENTRY');

		entrcptdtlmt_build: Begin
			Declare targetIdEntities Int unsigned;
			Declare fastStatusMode Int;
			-- obtention des entités à traiter et de leur mode de calcul de status de lettrage respectif

			Declare entrcptdtlmt_cursor Cursor For
					Select
						ENTT.ID_ENTITIES
-- 						, Case When Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') = @bcedml_fastStatusModeCode Then 1 Else 0 End
						, Case Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') 
								When @bcedml_fastStatusModeCode Then 1 
								When @bcedml_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 = @bcedml_fastStatusModeCode
									And TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE In (@bcedml_fastStatusModeCode, @bcedml_avgStatusModeCode)
							) On ENTT.TEMPL_LOADER_ID_TEMPL_LOADER = TMPLDR_FSLT.ID_TEMPLATE_LOADER
					where 1 = 1
# 						And ENTT.ID_ENTITIES Like @bcedml_idEntities
						And ENTT.ID_ENTITIES BETWEEN @bcedml_enttMinVal And @bcedml_enttMaxVal
				;
			Open entrcptdtlmt_cursor;
			entrcptdtlmt_read: Begin
				Declare Exit Handler for NOT FOUND 
                Begin
                    Close entrcptdtlmt_cursor;
                End ;
				entrcptdtlmt_loop: Loop
					-- parcour des entités
					Fetch entrcptdtlmt_cursor Into targetIdEntities, fastStatusMode;
					set @bcedml_fastStatusMode = Coalesce(fastStatusMode, 0);
					set @bcedml_targetIdEntities = targetIdEntities;
					call BUSINESS_COMPUTE_ENTRIES_DATA_ENTITIES_MATCHINGCODES_LAST(@bcedml_targetIdEntities, @bcedml_idacounts, @bcedml_fastStatusMode);
					
				End Loop entrcptdtlmt_loop;
			End ;
		End ;
						
    end;
end $$
Delimiter ;