Delimiter //
/**
 * control of balance  normalization
 * 
 * @author Jean-François
 */
Drop Procedure If Exists LOADER_BALANCECONTROL_NORMALIZE//
Create Procedure LOADER_BALANCECONTROL_NORMALIZE(
    In batchnum Int
)
MAIN_BALANCECONTROL_NORMALIZE: Begin

	Set @lbcn_batchnum = batchnum;

--     -- initialise dependents tables on init batch (0)
--     if @lbcn_batchnum = 0 Then
--         call LOADER_CREATE_TABLE_BALANCECONTROL_ACCOUNTS_STATUS;
--         leave MAIN_BALANCECONTROL_NORMALIZE;
--     End if;
	-- Linking
    Call LOADER_BALANCECONTROL_NORMALIZE_MAP_ACCOUNT(@lbcn_batchnum); -- doit être lancé avant LOADER_BALANCECONTROL_NORMALIZE_CHECK_ACCOUNT_EXISTS

    -- Mandatory check
    Call LOADER_BALANCECONTROL_NORMALIZE_CHECK_ACCOUNT_EXISTS(@lbcn_batchnum); -- doit être lancé aprés LOADER_BALANCECONTROL_NORMALIZE_MAP_ACCOUNT
    
	-- obtention du mode de controle (avec ou sans les compte rattachées)
	Call LOADER_BALANCECONTROL_NORMALIZE_MAP_GET_MODE(@lbcn_batchnum, @lbcn_boundMode);
    -- Checks
    Call LOADER_BALANCECONTROL_NORMALIZE_CHECK_GIVEN_BALANCE(@lbcn_batchnum, @lbcn_boundMode);

    -- Last actions
    Set @max_batch = GET_LOADER_INFO('MAX_BATCH', @lbcn_batchnum);
    if @lbcn_batchnum = @max_batch Then
        call LOADER_CREATE_TABLE_BALANCECONTROL_ACCOUNTS_STATUS;
        Call LOADER_BALANCECONTROL_NORMALIZE_CHECK_APPLICATION_BALANCE(@lbcn_boundMode);
    End if;

End//
Delimiter ;