Delimiter //
/**
 * Accounts normalization
 * 
 * @author Jean-François
 */
Drop Procedure If Exists LOADER_ACCOUNTS_NORMALIZE//
Create Procedure LOADER_ACCOUNTS_NORMALIZE (
    In batchnum Int
)
MAIN_ACCOUNTS_NORMALIZE: Begin

    -- initialise dependents tables on init batch (0)
    if batchnum = 0 Then
        call LOADER_CREATE_TABLE_ACCOUNTS_REPLICATION();

        -- pour éviter un qu'un précédant import de membre lie les contacts aux membres
        call LOADER_CREATE_TABLE_MEMBERS();

        leave MAIN_ACCOUNTS_NORMALIZE;
    End if;

    -- Mandatory checks
    Call LOADER_ACCOUNTS_NORMALIZE_ACCOUNT_REF_NOT_NULL(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_ACCOUNT_REF_IS_UNIQUE(batchnum);

    -- Create or update ?
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_ACCOUNT_CREATE_OR_UPDATE(batchnum);

    -- Mapping and linking
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_CATEG_NOT_PROVIDED(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_CATEG_PROVIDED(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_CURRENCY_NOT_PROVIDED(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_CURRENCY_PROVIDED(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_DATES_PROVIDED_OR_NOT(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_MANAGMENT_LEVEL_SET(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_PRIORITY_SET(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_SAND_STATE_SET(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_SCAN_TYPE_SET(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_TYPE_NOT_PROVIDED(batchnum);
    Call LOADER_ACCOUNTS_NORMALIZE_MAP_TYPE_PROVIDED(batchnum);
	call LOADER_ACCOUNTS_NORMALIZE_MAP_AGENCY_CODE_PROVIDED(batchnum);
	call LOADER_ACCOUNTS_NORMALIZE_MAP_RATE_CODE_PROVIDED(batchnum);
	call LOADER_ACCOUNTS_NORMALIZE_MAP_DEBTS_TYPES_CODE_PROVIDED(batchnum);

    call LOADER_ACCOUNTS_NORMALIZE_MAP_PENALTIES_RULES_CODE_PROVIDED(batchnum, 'FR.441-6', 'FR4416');
    call LOADER_ACCOUNTS_NORMALIZE_MAP_PENALTIES_RULES_CODE_PROVIDED(batchnum, 'FR.LATE_PENAL_CAUSE', 'FRLTPNCSE');

	call LOADER_ACCOUNTS_NORMALIZE_MAP_INERNALSCORE_PROVIDED(batchnum);
	call LOADER_ACCOUNTS_NORMALIZE_MAP_CASH_ASSSSMNT_PROVIDED(batchnum);
	call LOADER_ACCOUNTS_NORMALIZE_MAP_EXERNALSCORE_PROVIDED(batchnum);

	call LOADER_ACCOUNTS_NORMALIZE_MAP_ACCOUNTSTATUS_PROVIDED(batchnum);

    Call LOADER_POPULATE_TABLE_ACCOUNTS_REPLICATION(batchnum);

End//
Delimiter ;