Delimiter //
/**
 * Test of BUSINESS_CALCULATE_ENTRIES_MATCHING_CODE_STATE()
 * @test
 */ 
Drop Procedure if Exists TEST_CALCULATE_ENTRIES_MATCHING_CODE_STATE//
Create Procedure TEST_CALCULATE_ENTRIES_MATCHING_CODE_STATE()
Begin

    -- Context
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('MATCHINGCODES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_MATCHCOD');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_has_ACCOUNTS');

    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTITIES', 
            'ID_ENTITIES = 1
            , DF_CURRENCIES_ID_DF_CURR = 1
            , ADMIN_ID_ADMIN = 1
            , CONT_BUSINESS_ID_CONT_BUSINESS = 1
            , ENTITIES_LOCK_STATE = 0
            , ENTITIES_CODE = 1'
    );

    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ACCOUNTS', 
            'ID_ACCOUNTS = 1
            , ENTITIES_ID_ENTITIES = 1
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ACC_CATEG_ID_DF_ACC_CATEG = 3
            , DF_PRIORITY_ID_DF_PRIORITY = 2
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ACCOUNTS_TYPE = 1
            , CONTACTS_ID_CONTACTS = 1
            , ACCOUNTS_REF = ''ACCNT-REF-1''
            , ACCOUNTS_CREATED = Now()
            , ACCOUNTS_LAST_EVENT_UPDATE = Now()
            , ACCOUNTS_PROCESSUS_SCAN_TYPE = 1
            , ACCOUNTS_ACC_MNGT_LAST_LVL = 0'
    );
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ACCOUNTS', 
            'ID_ACCOUNTS = 2
            , ENTITIES_ID_ENTITIES = 1
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ACC_CATEG_ID_DF_ACC_CATEG = 3
            , DF_PRIORITY_ID_DF_PRIORITY = 2
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ACCOUNTS_TYPE = 1
            , CONTACTS_ID_CONTACTS = 1
            , ACCOUNTS_REF = ''ACCNT-REF-2''
            , ACCOUNTS_CREATED = Now()
            , ACCOUNTS_LAST_EVENT_UPDATE = Now()
            , ACCOUNTS_PROCESSUS_SCAN_TYPE = 1
            , ACCOUNTS_ACC_MNGT_LAST_LVL = 0'
    );

    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES', '
            ID_ENTRIES = 1
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1
            , CONTACTS_ID_CONTACTS = 0
            , DF_PAY_TYPE_ID_DF_PAY_TYPE = 1
            , DF_MATCH_STAT_ID_DF_MATCH_STAT = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ID_DF_PAY_SCHED_STATUS = 1
            , ENTRIES_DATE = Now()
            , ENTRIES_DUE_DATE = Now()
            , ENTRIES_DEBIT = 50
            , ENTRIES_CREDIT = 0
    ');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES', '
            ID_ENTRIES = 2
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1
            , CONTACTS_ID_CONTACTS = 0
            , DF_PAY_TYPE_ID_DF_PAY_TYPE = 1
            , DF_MATCH_STAT_ID_DF_MATCH_STAT = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ID_DF_PAY_SCHED_STATUS = 1
            , ENTRIES_DATE = Now()
            , ENTRIES_DUE_DATE = Now()
            , ENTRIES_DEBIT = 0
            , ENTRIES_CREDIT = 50
    ');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES', '
            ID_ENTRIES = 3
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1
            , CONTACTS_ID_CONTACTS = 0
            , DF_PAY_TYPE_ID_DF_PAY_TYPE = 1
            , DF_MATCH_STAT_ID_DF_MATCH_STAT = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ID_DF_PAY_SCHED_STATUS = 1
            , ENTRIES_DATE = Now()
            , ENTRIES_DUE_DATE = Now()
            , ENTRIES_DEBIT = 50
            , ENTRIES_CREDIT = 0
    ');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES', '
            ID_ENTRIES = 4
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1
            , CONTACTS_ID_CONTACTS = 0
            , DF_PAY_TYPE_ID_DF_PAY_TYPE = 1
            , DF_MATCH_STAT_ID_DF_MATCH_STAT = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ID_DF_PAY_SCHED_STATUS = 1
            , ENTRIES_DATE = Now()
            , ENTRIES_DUE_DATE = Now()
            , ENTRIES_DEBIT = 0
            , ENTRIES_CREDIT = 25
    ');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES', '
            ID_ENTRIES = 5
            , DF_CURRENCIES_ID_DF_CURRENCIES = 1
            , DF_ENTR_TYPE_ID_DF_ENTR_TYPE = 1
            , CONTACTS_ID_CONTACTS = 0
            , DF_PAY_TYPE_ID_DF_PAY_TYPE = 1
            , DF_MATCH_STAT_ID_DF_MATCH_STAT = 1
            , DF_SAND_STATE_ID_DF_SAND_STATE = 2
            , ID_DF_PAY_SCHED_STATUS = 1
            , ENTRIES_DATE = Now()
            , ENTRIES_DUE_DATE = Now()
            , ENTRIES_DEBIT = 0
            , ENTRIES_CREDIT = 50
    ');
    
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('MATCHINGCODES', 'ID_MATCHINGCODES = 1, MATCHINGCODES_CODE = ''AAA'', MATCHINGCODES_DATE = ''2012-07-03''');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('MATCHINGCODES', 'ID_MATCHINGCODES = 2, MATCHINGCODES_CODE = ''BBB'', MATCHINGCODES_DATE = ''2012-07-03''');

    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_MATCHCOD', 'ID_ENTRIES_MATCHCOD = 1, ENTRIES_ID_ENTRIES = 1, MATCHCOD_ID_MATCHCOD = 1');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_MATCHCOD', 'ID_ENTRIES_MATCHCOD = 2, ENTRIES_ID_ENTRIES = 2, MATCHCOD_ID_MATCHCOD = 1');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_MATCHCOD', 'ID_ENTRIES_MATCHCOD = 3, ENTRIES_ID_ENTRIES = 3, MATCHCOD_ID_MATCHCOD = 2');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_MATCHCOD', 'ID_ENTRIES_MATCHCOD = 4, ENTRIES_ID_ENTRIES = 4, MATCHCOD_ID_MATCHCOD = 2');

    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_has_ACCOUNTS', 'ID_ENTRIES_has_ACCOUNTS = 1, ENTRIES_ID_ENTRIES = 1, ACCOUNTS_ID_ACCOUNTS = 1, ENTRIES_ACC_EXT_REF=''ENTACCEXT-REF-1''');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_has_ACCOUNTS', 'ID_ENTRIES_has_ACCOUNTS = 2, ENTRIES_ID_ENTRIES = 2, ACCOUNTS_ID_ACCOUNTS = 1, ENTRIES_ACC_EXT_REF=''ENTACCEXT-REF-2''');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_has_ACCOUNTS', 'ID_ENTRIES_has_ACCOUNTS = 3, ENTRIES_ID_ENTRIES = 3, ACCOUNTS_ID_ACCOUNTS = 1, ENTRIES_ACC_EXT_REF=''ENTACCEXT-REF-3''');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_has_ACCOUNTS', 'ID_ENTRIES_has_ACCOUNTS = 4, ENTRIES_ID_ENTRIES = 4, ACCOUNTS_ID_ACCOUNTS = 1, ENTRIES_ACC_EXT_REF=''ENTACCEXT-REF-4''');
    Call LOADER_TEST_MOCK_BUSINESS_PUSH('ENTRIES_has_ACCOUNTS', 'ID_ENTRIES_has_ACCOUNTS = 5, ENTRIES_ID_ENTRIES = 5, ACCOUNTS_ID_ACCOUNTS = 2, ENTRIES_ACC_EXT_REF=''ENTACCEXT-REF-5''');

    -- Tested procedure
    Call BUSINESS_CALCULATE_ENTRIES_MATCHING_CODE_STATE(null, null, null);

    -- Get matching codes state ids
    Set @OpenEntry = (Select ID_DF_MATCHINGCODE_STATE From DF_MATCHINGCODE_STATE Where DF_MATCHINGCODE_STATE_CODE = 'OPEN ENTRY');
    Set @Partial = (Select ID_DF_MATCHINGCODE_STATE From DF_MATCHINGCODE_STATE Where DF_MATCHINGCODE_STATE_CODE = 'PARTIAL MATCHING CODE');
    Set @ClosedEntry = (Select ID_DF_MATCHINGCODE_STATE From DF_MATCHINGCODE_STATE Where DF_MATCHINGCODE_STATE_CODE = 'CLOSED ENTRY');

    -- Assertions
    Call utAssert_eq('A group of entries from an account with the same matching code and a balance equal to zero get the balance state ''CLOSED ENTRY'''
        , @ClosedEntry
        , (Select DF_MATCH_STAT_ID_DF_MATCH_STAT From ENTRIES Where ID_ENTRIES = 1)
    );
    Call utAssert_eq('A group of entries from an account with the same matching code and a balance equal to zero get the balance state ''CLOSED ENTRY'''
        , @ClosedEntry
        , (Select DF_MATCH_STAT_ID_DF_MATCH_STAT From ENTRIES Where ID_ENTRIES = 2)
    );
    Call utAssert_eq('A group of entries from an account with the same matching code and a balance not equal to zero get the balance state ''PARTIAL MATCHING CODE'''
        , @Partial
        , (Select DF_MATCH_STAT_ID_DF_MATCH_STAT From ENTRIES Where ID_ENTRIES = 3)
    );
    Call utAssert_eq('A group of entries from an account with the same matching code and a balance not equal to zero get the balance state ''PARTIAL MATCHING CODE'''
        , @Partial
        , (Select DF_MATCH_STAT_ID_DF_MATCH_STAT From ENTRIES Where ID_ENTRIES = 4)
    );
    Call utAssert_eq('An entry with no matching code have the balance state ''OPEN ENTRY'''
        , @OpenEntry
        , (Select DF_MATCH_STAT_ID_DF_MATCH_STAT From ENTRIES Where ID_ENTRIES = 5)
    );

    -- @debug
--     SELECT
--         ENTRIES_has_ACCOUNTS.ACCOUNTS_ID_ACCOUNTS as ID_ACCOUNTS
--         , ENTRIES.ID_ENTRIES
--         , ENTRIES.DF_MATCH_STAT_ID_DF_MATCH_STAT
--     FROM ENTRIES_has_ACCOUNTS
--         INNER JOIN ENTRIES
--             ON ENTRIES_has_ACCOUNTS.ENTRIES_ID_ENTRIES = ENTRIES.ID_ENTRIES
--     ;
    
    -- Clean
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_has_ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES_MATCHCOD');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('MATCHINGCODES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTRIES');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ACCOUNTS');
    Call LOADER_TEST_MOCK_BUSINESS_CLEAN('ENTITIES');

End //
Delimiter ;