Delimiter //
/**
 * We can import amounts 
 * 
 * @author Jean-François
 * @ticket #2143
 * @ticket #2144
 * @ticket #2145
 * @ticket #2146
 * @ticket #3756.
 */
Drop Procedure If Exists LOADER_ENTRIES_MAP_POSITIVE_AMOUNTS//
Create Procedure LOADER_ENTRIES_MAP_POSITIVE_AMOUNTS(
    In batchnum Int
)
Begin

    Update
        LOADER_ENTRIES E
        Inner Join
            LOADER_STATUS S
            On S.ID_LOADER_STATUS = E.LOADER_STATUS_ID_LOADER_STATUS
    Set
        -- Amounts
          ENTRIES_DEBIT = If(Coalesce(DEBIT, 0) > 0, DEBIT, Null)
        , ENTRIES_CREDIT = If(Coalesce(CREDIT, 0) > 0, CREDIT, Null)
        , ENTRIES_ENT_DEBIT = If(Coalesce(ENT_DEBIT, 0) > 0, ENT_DEBIT, Null)
        , ENTRIES_ENT_CREDIT = If(Coalesce(ENT_CREDIT, 0) > 0, ENT_CREDIT, Null)
        , ENTRIES_ADMIN_DEBIT = If(Coalesce(ADMIN_DEBIT, 0) > 0, ADMIN_DEBIT, Null)
        , ENTRIES_ADMIN_CREDIT = If(Coalesce(ADMIN_CREDIT, 0) > 0, ADMIN_CREDIT, Null)
    Where 
        S.LOADER_STATUS_BATCH_NUM = batchnum
        And 
            (   Coalesce(ENT_DEBIT, 0) > 0
                Or Coalesce(ENT_CREDIT, 0) > 0
                Or Coalesce(DEBIT, 0) > 0
                Or Coalesce(CREDIT, 0) > 0
                Or Coalesce(ADMIN_DEBIT, 0) > 0
                Or Coalesce(ADMIN_CREDIT, 0) > 0
            )
    ;

End//
Delimiter ;