Delimiter //
/**
 * If the currency is not provided, use the Admin Param currency by default
 * 
 * @param int batchnum
 * @author Benoit
 * @ticket #3895
 */ 
Drop Procedure if Exists LOADER_ACCOUNTS_NORMALIZE_MAP_CURRENCY_NOT_PROVIDED//
 
Create Procedure LOADER_ACCOUNTS_NORMALIZE_MAP_CURRENCY_NOT_PROVIDED (
    In batchnum int
)
Begin
 
    Set @idAdminParams = GET_LOADER_INFO('ID_ADMIN_PARAMS', batchnum);

    UPDATE
        LOADER_ACCOUNTS A
        INNER JOIN LOADER_STATUS S
            ON S.ID_LOADER_STATUS = A.LOADER_STATUS_ID_LOADER_STATUS
        INNER JOIN ADMIN_PARAMS P
            ON P.ID_ADMIN_PARAMS = @idAdminParams
    SET
        A.DF_CURRENCIES_ID_DF_CURRENCIES = P.DF_CURRENCIES_ID_DF_CURRENCIES
    WHERE
        S.LOADER_STATUS_BATCH_NUM = batchnum
        AND A.ID_ACCOUNTS_NEW IS NOT NULL
        AND Coalesce(A.DF_CURRENCIES_CODE, '') = ''
   ;
    
End //
Delimiter ;
