Delimiter //
/**
 * Set the default matching code state value.
 * 
 * @param int batchnum
 * @author Benoit
 * ticket #4002
 */ 
Drop Procedure if Exists LOADER_ENTRIES_NORMALIZE_MAP_BALANCE_AMOUNT//
 
Create Procedure LOADER_ENTRIES_NORMALIZE_MAP_BALANCE_AMOUNT (
    In batchnum int
)
Begin

    Update
        LOADER_ENTRIES E
        Inner Join LOADER_STATUS S
            On S.ID_LOADER_STATUS = E.LOADER_STATUS_ID_LOADER_STATUS
        Inner Join DF_ENTRIES_TYPE DFET
            On E.DF_ENTR_TYPE_ID_DF_ENTR_TYPE = DFET.ID_DF_ENTRIES_TYPE
            
    Set
--         ENTRIES_DEBIT = Case When DFET.DF_ENTRIES_TYPE_IS_DEBIT <> 0 Then abs(E.AMOUNT) Else Null End
--         , ENTRIES_CREDIT = Case When DFET.DF_ENTRIES_TYPE_IS_CREDIT <> 0 Then abs(E.AMOUNT) Else Null End
--         ENTRIES_DEBIT = Case When E.AMOUNT >= 0 And DFET.DF_ENTRIES_TYPE_IS_DEBIT <> 0 Then E.AMOUNT Else Null End
--         , ENTRIES_CREDIT = Case When E.AMOUNT < 0 And DFET.DF_ENTRIES_TYPE_IS_CREDIT <> 0 Then abs(E.AMOUNT) Else Null End
        ENTRIES_DEBIT = Case When E.AMOUNT >= 0 Then E.AMOUNT Else 0 End
        , ENTRIES_CREDIT = Case When E.AMOUNT < 0 Then abs(E.AMOUNT) Else 0 End
    Where 1 = 1
        And S.LOADER_STATUS_BATCH_NUM = batchnum
        And DEBIT Is Null
        And CREDIT Is Null
        And AMOUNT Is Not Null
    ;
    Update
        LOADER_ENTRIES E
        Inner Join LOADER_STATUS S
            On S.ID_LOADER_STATUS = E.LOADER_STATUS_ID_LOADER_STATUS
        Inner Join DF_ENTRIES_TYPE DFET
            On E.DF_ENTR_TYPE_ID_DF_ENTR_TYPE = DFET.ID_DF_ENTRIES_TYPE
            
    Set
--         ENTRIES_ENT_DEBIT = Case When DFET.DF_ENTRIES_TYPE_IS_DEBIT <> 0 Then abs(E.ENT_AMOUNT) Else Null End
--         , ENTRIES_ENT_CREDIT = Case When DFET.DF_ENTRIES_TYPE_IS_CREDIT <> 0 Then abs(E.ENT_AMOUNT) Else Null End
--         ENTRIES_ENT_DEBIT = Case When E.ENT_AMOUNT >= 0 And DFET.DF_ENTRIES_TYPE_IS_DEBIT <> 0 Then E.ENT_AMOUNT Else Null End
--         , ENTRIES_ENT_CREDIT = Case When E.ENT_AMOUNT < 0 And DFET.DF_ENTRIES_TYPE_IS_CREDIT <> 0 Then abs(E.ENT_AMOUNT) Else Null End
        ENTRIES_ENT_DEBIT = Case When E.ENT_AMOUNT >= 0 Then E.ENT_AMOUNT Else 0 End
        , ENTRIES_ENT_CREDIT = Case When E.ENT_AMOUNT < 0 Then abs(E.ENT_AMOUNT) Else 0 End

    Where 1 = 1
        And S.LOADER_STATUS_BATCH_NUM = batchnum
        And ENT_DEBIT Is Null
        And ENT_CREDIT Is Null
        And ENT_AMOUNT Is Not Null
    ;
    Update
        LOADER_ENTRIES E
        Inner Join LOADER_STATUS S
            On S.ID_LOADER_STATUS = E.LOADER_STATUS_ID_LOADER_STATUS
        Inner Join DF_ENTRIES_TYPE DFET
            On E.DF_ENTR_TYPE_ID_DF_ENTR_TYPE = DFET.ID_DF_ENTRIES_TYPE
            
    Set
--         ENTRIES_ADMIN_DEBIT = Case When DFET.DF_ENTRIES_TYPE_IS_DEBIT <> 0 Then abs(E.ADMIN_AMOUNT) Else Null End
--         , ENTRIES_ADMIN_CREDIT = Case When DFET.DF_ENTRIES_TYPE_IS_CREDIT <> 0 Then abs(E.ADMIN_AMOUNT) Else Null End
--         ENTRIES_ADMIN_DEBIT = Case When E.ADMIN_AMOUNT >= 0 And DFET.DF_ENTRIES_TYPE_IS_DEBIT <> 0 Then abs(E.ADMIN_AMOUNT) Else Null End
--         , ENTRIES_ADMIN_CREDIT = Case When E.ADMIN_AMOUNT < 0 And DFET.DF_ENTRIES_TYPE_IS_CREDIT <> 0 Then abs(E.ADMIN_AMOUNT) Else Null End
        ENTRIES_ADMIN_DEBIT = Case When E.ADMIN_AMOUNT >= 0 Then abs(E.ADMIN_AMOUNT) Else 0 End
        , ENTRIES_ADMIN_CREDIT = Case When E.ADMIN_AMOUNT < 0 Then abs(E.ADMIN_AMOUNT) Else 0 End
    Where 1 = 1
        And S.LOADER_STATUS_BATCH_NUM = batchnum
        And ADMIN_DEBIT Is Null
        And ADMIN_CREDIT Is Null
        And ADMIN_AMOUNT Is Not Null
    ;
--     

End //
Delimiter ;