Delimiter //
/**
 * Entries normalization
 * 
 * @author Jean-François
 */
Drop Procedure If Exists LOADER_ENTRIES_NORMALIZE//
Create Procedure LOADER_ENTRIES_NORMALIZE(
    In batchnum Int
)
 MAIN_ENTRIES_NORMALIZE: Begin
    
    -- initialise dependents tables on init batch (0)
    if batchnum = 0 Then
        call LOADER_CREATE_TABLE_ENTRIES_ENTRIES_HAS_ACCOUNTS;
        leave MAIN_ENTRIES_NORMALIZE;
    End if;

    -- Mandatory checks
    Call LOADER_ENTRIES_NORMALIZE_ACCOUNT_REF_NOT_NULL(batchnum);
    Call LOADER_ENTRIES_NORMALIZE_ENTRY_ACCOUNT_EXT_REF_NOT_NULL(batchnum);

    -- Other checks
    Call LOADER_ENTRIES_NORMALIZE_ENTRY_REF_NOT_NULL(batchnum);

    Call LOADER_ENTRIES_NORMALIZE_REF_IS_UNIQUE_PER_ACCOUNTS(batchnum);

    -- Mandatory linking
    Call LOADER_ENTRIES_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED(batchnum); -- doit être AVANT LOADER_ENTRIES_NORMALIZE_MAP_ENTRY_CREATE_OR_UPDATE

    -- Create or update ?
    Call LOADER_ENTRIES_NORMALIZE_MAP_ENTRY_CREATE_OR_UPDATE(batchnum); -- doit être après LOADER_ENTRIES_NORMALIZE_MAP_ACCOUNT_REF_PROVIDED

    -- Mapping and linking
    Call LOADER_ENTRIES_MAP_CURRENCY_NOTPROVIDED(batchnum);
    Call LOADER_ENTRIES_MAP_CURRENCY_PROVIDED(batchnum);
    Call LOADER_ENTRIES_NORMALIZE_MAP_DATE_NOTPROVIDED(batchnum); -- doit être AVANT LOADER_ENTRIES_NORMALIZE_MAP_DUEDATE_NOTPROVIDED
    Call LOADER_ENTRIES_NORMALIZE_MAP_DUEDATE_NOTPROVIDED(batchnum); -- doit être après LOADER_ENTRIES_NORMALIZE_MAP_DATE_NOTPROVIDED

    Call LOADER_ENTRIES_NORMALIZE_MAP_ENTRIES_PROMISE_DATE(batchnum);

    Call LOADER_ENTRIES_NORMALIZE_MAP_CONTACT_REF_PROVIDED(batchnum);

    Call LOADER_ENTRIES_MAP_PAYMENTTYPE_NOT_PROVIDED(batchnum);
    Call LOADER_ENTRIES_MAP_PAYMENTTYPE_PROVIDED(batchnum);

    Call LOADER_ENTRIES_MAP_POSITIVE_AMOUNTS(batchnum);

    Call LOADER_ENTRIES_NORMALIZE_MAP_MATCHING_CODE_STATE_SET(batchnum);
    Call LOADER_ENTRIES_NORMALIZE_MAP_FORCED_MATCH_STAT(batchnum);

    Call LOADER_ENTRIES_NORMALIZE_MAP_PAYMENT_SCHEDULE_STATUS_SET(batchnum);
    Call LOADER_ENTRIES_NORMALIZE_MAP_SAND_STATE_SET(batchnum);

    Call LOADER_ENTRIES_NORMALIZE_MAP_STATUS_PROVIDED(batchnum); -- doit être avant LOADER_ENTRIES_NORMALIZE_MAP_STATUS_NOTPROVIDED
    Call LOADER_ENTRIES_NORMALIZE_MAP_STATUS_NOTPROVIDED(batchnum); -- doit être paret LOADER_ENTRIES_NORMALIZE_MAP_STATUS_PROVIDED

    Call LOADER_ENTRIES_MAP_TYPE_PROVIDED(batchnum); -- doit être AVANT LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS et LOADER_ENTRIES_NORMALIZE_MAP_BALANCE_AMOUNT
    Call LOADER_ENTRIES_NORMALIZE_MAP_BALANCE_AMOUNT(batchnum); -- doit être après LOADER_ENTRIES_MAP_TYPE_PROVIDED et avant LOADER_ENTRIES_MAP_TYPE_PROVIDED
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS(batchnum); -- doit être après LOADER_ENTRIES_MAP_TYPE_PROVIDED et LOADER_ENTRIES_NORMALIZE_MAP_BALANCE_AMOUNT

    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_INV_TO_CRN(batchnum); -- doit être après LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_CRN_TO_INV(batchnum); -- doit être après LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_PAY_TO_CAN(batchnum); -- doit être après LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_CAN_TO_PAY(batchnum); -- doit être après LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS

    Call LOADER_ENTRIES_MAP_CALCULATE_BALANCE(batchnum); -- après tous les traitements sur les montants
    Call LOADER_ENTRIES_MAP_EMPTY_TRANSACTION_AMOUNTS(batchnum); -- après tous les traitements sur les montants, LOADER_ENTRIES_MAP_CALCULATE_BALANCE  et LOADER_ENTRIES_NORMALIZE_MAP_ENTRY_CREATE_OR_UPDATE



--     -- Last actions
--     Set @max_batch = GET_LOADER_INFO('MAX_BATCH', batchnum);
--     if batchnum = @max_batch Then
--         -- Put your code here
--     End if;

End//
Delimiter ;