Delimiter //
/**
 * test for LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS()
 * 
 * @author Jean-François
 */
Drop Procedure If Exists TEST_LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS//
Create Procedure TEST_LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS()
Begin


    Set 
        @facture      = (Select ID_DF_ENTRIES_TYPE From DF_ENTRIES_TYPE Where DF_ENTRIES_TYPE_CODE = 'INV')
        , @avoir        = (Select ID_DF_ENTRIES_TYPE From DF_ENTRIES_TYPE Where DF_ENTRIES_TYPE_CODE = 'CRN')
        , @reglement    = (Select ID_DF_ENTRIES_TYPE From DF_ENTRIES_TYPE Where DF_ENTRIES_TYPE_CODE = 'PAY')
        , @impaye       = (Select ID_DF_ENTRIES_TYPE From DF_ENTRIES_TYPE Where DF_ENTRIES_TYPE_CODE = 'CAN');


    -- table
    Call LOADER_TEST_MOCK_ENTRIES_CLEAN();
        -- Facture à transformer en Avoir car Débit est négatif
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(1, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@facture,', DEBIT = -500 '), 1, Null);
        -- Facture qui ne bouge pas
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(2, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@facture,', DEBIT = 400 '), 2, Null);

        -- Avoir à transformer en Facture car Crédit est négatif
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(3, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@avoir,', CREDIT = -500 '), 3, Null);
        -- Avoir qui ne bouge pas
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(4, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@avoir,', CREDIT = 400 '), 4, Null);

        -- Règlement à transformer en Règlement annulé/impayé car Crédit est négatif
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(5, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@reglement,', CREDIT = -500 '), 5, Null);
        -- Règlement qui ne bouge pas
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(6, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@reglement,', CREDIT = 400 '), 6, Null);

        -- Règlement annulé/impayé à transformer en Règlement car débit est négatif
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(7, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@impaye,', DEBIT = -500 '), 7, Null);
        -- Règlement annulé/impayé qui ne bouge pas
    Call LOADER_TEST_MOCK_ENTRIES_PUSH(8, Concat('DF_ENTR_TYPE_ID_DF_ENTR_TYPE = ',@impaye,', DEBIT = 400 '), 8, Null);


    -- tested procedure
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS(1);
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_INV_TO_CRN(1); 
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_CRN_TO_INV(1); 
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_PAY_TO_CAN(1); 
    Call LOADER_ENTRIES_MAP_NEGATIVE_AMOUNTS_CAN_TO_PAY(1); 

    -- assertions about negatives asbolutes values
    Call utAssert_eq('Negatives debits are reported as positives credits'
        , 500.0000
        , (Select ENTRIES_CREDIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 1)
    );
    Call utAssert_eq('Negatives debits are cleared after reported as positives credits'
        , 0.0000
        , (Select ENTRIES_DEBIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 1)
    );


    Call utAssert_eq('Positives debits are not reported'
        , Null
        , (Select ENTRIES_DEBIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 2)
    );

    Call utAssert_eq('Negatives credits are reported as positives debits'
        , 500.0000
        , (Select ENTRIES_DEBIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 3)
    );
    Call utAssert_eq('Negatives credits are cleared after reported as positives debits'
        , 0.0000
        , (Select ENTRIES_CREDIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 3)
    );

    Call utAssert_eq('Positives debits are not reported'
        , Null
        , (Select ENTRIES_CREDIT From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 4)
    );


    -- assertions about Entry' Type
    Call utAssert_eq('Invoice is transformed into Credit Note When Debit is negative'
        , @avoir
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 1)
    );
    Call utAssert_eq('Invoice stands an invoice When Debit is positive'
        , @facture
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 2)
    );
    Call utAssert_eq('Credit Note is transformed into Invoice When Credit is negative'
        , @facture
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 3)
    );
    Call utAssert_eq('Credit Note stands a credit Note When Credit is positive'
        , @avoir
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 4)
    );

    Call utAssert_eq('Payment is transformed into Unpaid/cancelled When CREDIT is negative'
        , @impaye
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 5)
    );
    Call utAssert_eq('Payment stands an Payment When Debit is positive'
        , @reglement
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 6)
    );
    Call utAssert_eq('Unpaid/cancelled is transformed into Payment When Débit is negative'
        , @reglement
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 7)
    );
    Call utAssert_eq('Unpaid/cancelled stands a Unpaid/cancelled When Débit is positive'
        , @impaye
        , (Select DF_ENTR_TYPE_ID_DF_ENTR_TYPE From LOADER_ENTRIES Where ID_LOADER_ENTRIES = 8)
    );


    -- cleans
    Call LOADER_TEST_MOCK_ENTRIES_CLEAN();

    

End//
Delimiter ;