Delimiter //
/**
 * Currency is provided
 * checks if provided currency exist
 *  -> yes : apply it
 *  -> no  : add an error
 * 
 * @author Jean-François
 */
Drop Procedure If Exists LOADER_ENTRIES_MAP_CURRENCY_PROVIDED//
Create Procedure LOADER_ENTRIES_MAP_CURRENCY_PROVIDED(
    In batchnum Int
)
Begin
    
    Update
        LOADER_ENTRIES E
        Inner Join LOADER_STATUS S
            On S.ID_LOADER_STATUS = E.LOADER_STATUS_ID_LOADER_STATUS
        Left Join
            DF_CURRENCIES DC
            On DC.DF_CURRENCIES_CODE = E.CURRENCY
    Set
        E.DF_CURRENCIES_ID_DF_CURRENCIES = DC.ID_DF_CURRENCIES

        , S.LOADER_STATUS_ERRCOUNT = 
            Case When DC.ID_DF_CURRENCIES Is Null 
                Then S.LOADER_STATUS_ERRCOUNT + 1
                Else S.LOADER_STATUS_ERRCOUNT
            End

        , S.LOADER_STATUS_ERRMSGS = 
            Case When DC.ID_DF_CURRENCIES Is Null  
                Then LOADER_CONCAT_ERR(S.LOADER_STATUS_ERRMSGS, CONCAT('Devise inconnue : ', E.CURRENCY) )
                Else S.LOADER_STATUS_ERRMSGS
            End
    Where
        S.LOADER_STATUS_BATCH_NUM = batchnum
        And Coalesce(E.CURRENCY, '') != ''
    ;

End//
Delimiter ;