Delimiter //
/**
 * 
 * @author armen
 */
Drop Procedure If Exists LOADER_BALANCECONTROL_NORMALIZE_MAP_GET_MODE//
Create Procedure LOADER_BALANCECONTROL_NORMALIZE_MAP_GET_MODE(
    In batchnum Int
	, out boundMode int
)
Begin
   lbcnmgm_initVars: Begin
		Set @lbcnmgm_batchnum = batchnum;
		Set @lbcnmgm_idEntities = GET_LOADER_INFO('ID_ENTITIES', @lbcnmgm_batchnum);
		Set @lbcnmgm_idTemplateLoader = GET_LOADER_INFO('ID_TEMPLATE_LOADER', @lbcnmgm_batchnum);

		Set @lbcnmgm_CodeBalCtrlWithLnkAcc = 'BLCTR-CHK-LNKAC';
		Set @lbcnmgm_codeBalCtrlNoLnkAcc = 'BLCTR-CHK-MNACC';

		set @lbcnmgm_boundMode = Coalesce(
					(
						Select
							Case Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, @lbcnmgm_codeBalCtrlNoLnkAcc) 
								When @lbcnmgm_CodeBalCtrlWithLnkAcc Then 1 
-- 								When @lbcnmgm_codeBalCtrlNoLnkAcc Then 0 
								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 In (@lbcnmgm_CodeBalCtrlWithLnkAcc, @lbcnmgm_codeBalCtrlNoLnkAcc)
						where 1 = 1
							And TMPLDR_FSLT.ID_TEMPLATE_LOADER = @lbcnmgm_idTemplateLoader
					)
					, (
						Select
							Case Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, @lbcnmgm_codeBalCtrlNoLnkAcc) 
								When @lbcnmgm_CodeBalCtrlWithLnkAcc Then 1 
-- 								When @lbcnmgm_codeBalCtrlNoLnkAcc Then 0 
								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 In (@lbcnmgm_CodeBalCtrlWithLnkAcc, @lbcnmgm_codeBalCtrlNoLnkAcc)
								) On ENTT.TEMPL_LOADER_ID_TEMPL_LOADER = TMPLDR_FSLT.ID_TEMPLATE_LOADER
						where 1 = 1
							And ENTT.ID_ENTITIES = @lbcnmgm_idEntities 
					)
					, 0
				)
			;
	end ;
	Set boundMode = @lbcnmgm_boundMode;
End//
Delimiter ;