"""Statement period model tests.""" import pytest from moneyhub.constants.constants import OrderDirection from moneyhub.models.statement_period import StatementPeriod from tests.unit.conftest import insert_mock_data from tests.unit.conftest import insert_mock_statement_period ACCOUNT_ID = 24601 PAYMENT_ENTITY_ID = 2 def test_get_for_account_activity(): """Test getting statement periods for an account.""" insert_mock_data({ 'account': {'account_id': ACCOUNT_ID}, 'reference_payment_entity': {'reference_payment_entity_id': PAYMENT_ENTITY_ID}, 'reference_sap_profit_center': { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK1234', 'company_code': '1234', 'business_group': 'ORC' }, 'reference_signing_entity': { 'reference_signing_entity_id': 1, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': { 'contract_id': 1001, 'reference_signing_entity_id': 1, }, 'statement_period': [ {'statement_period_id': 11, 'statement_period_status': 'closed'}, {'statement_period_id': 12, 'statement_period_status': 'closed'}, {'statement_period_id': 13, 'statement_period_status': 'closed'}, {'statement_period_id': 14, 'statement_period_status': 'current'}, {'statement_period_id': 15, 'statement_period_status': 'open'}, ], 'account_payment_term': { 'account_payment_term_id': 1, 'account_id': ACCOUNT_ID, 'currency_code': 'USD', 'payment_entity_id': PAYMENT_ENTITY_ID, 'payment_schedule': '30_days_after_month_end' }, 'statement_period_payment_entity': { 'statement_period_payment_entity_id': 1, 'statement_period_id': 14, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'is_visible_to_customer': 1, }, 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 12}, {'abacus_event_id': 2, 'statement_period_id': 14}, ], 'ledger_account_contract': [ { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 1, }, { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 2, }, ], }) first_statement_period_id = 12 items, total = StatementPeriod.get_for_account_activity(ACCOUNT_ID, first_statement_period_id) assert [r.statement_period_id for r in items] == [12, 13, 14] assert total == 3 def test_get_for_account_activity_filtering_first_statement_period(): """Test getting statement periods for an account.""" insert_mock_data({ 'account': {'account_id': ACCOUNT_ID}, 'reference_payment_entity': {'reference_payment_entity_id': PAYMENT_ENTITY_ID}, 'reference_sap_profit_center': { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK1234', 'company_code': '1234', 'business_group': 'ORC' }, 'reference_signing_entity': { 'reference_signing_entity_id': 1, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': { 'contract_id': 1001, 'reference_signing_entity_id': 1, }, 'statement_period': [ {'statement_period_id': 11, 'statement_period_status': 'closed'}, {'statement_period_id': 12, 'statement_period_status': 'closed'}, {'statement_period_id': 13, 'statement_period_status': 'closed'}, {'statement_period_id': 14, 'statement_period_status': 'current'}, {'statement_period_id': 15, 'statement_period_status': 'open'}, ], 'account_payment_term': { 'account_payment_term_id': 1, 'account_id': ACCOUNT_ID, 'currency_code': 'USD', 'payment_entity_id': PAYMENT_ENTITY_ID, 'payment_schedule': '30_days_after_month_end' }, 'statement_period_payment_entity': { 'statement_period_payment_entity_id': 1, 'statement_period_id': 14, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'is_visible_to_customer': 1, }, 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 12}, {'abacus_event_id': 2, 'statement_period_id': 13}, {'abacus_event_id': 3, 'statement_period_id': 14}, ], 'ledger_account_contract': [ { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 1, }, { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 2, }, ], }) first_statement_period_id = 13 items, total = StatementPeriod.get_for_account_activity(ACCOUNT_ID, first_statement_period_id) assert [r.statement_period_id for r in items] == [13, 14] assert total == 2 @pytest.mark.parametrize( 'order_dir,expected_ids', [ (OrderDirection.ASC, [12, 13, 14]), (OrderDirection.DESC, [14, 13, 12]), ], ) def test_get_for_account_activity_sorted_results(order_dir, expected_ids): """Test getting statement periods for an account with specified result order.""" insert_mock_data({ 'account': {'account_id': ACCOUNT_ID}, 'reference_payment_entity': {'reference_payment_entity_id': PAYMENT_ENTITY_ID}, 'reference_sap_profit_center': { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK1234', 'company_code': '1234', 'business_group': 'ORC' }, 'reference_signing_entity': { 'reference_signing_entity_id': 1, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': { 'contract_id': 1001, 'reference_signing_entity_id': 1, }, 'statement_period': [ {'statement_period_id': 11, 'statement_period_status': 'closed'}, {'statement_period_id': 12, 'statement_period_status': 'closed'}, {'statement_period_id': 13, 'statement_period_status': 'closed'}, {'statement_period_id': 14, 'statement_period_status': 'current'}, {'statement_period_id': 15, 'statement_period_status': 'open'}, ], 'account_payment_term': { 'account_payment_term_id': 1, 'account_id': ACCOUNT_ID, 'currency_code': 'USD', 'payment_entity_id': PAYMENT_ENTITY_ID, 'payment_schedule': '30_days_after_month_end' }, 'statement_period_payment_entity': { 'statement_period_payment_entity_id': 1, 'statement_period_id': 14, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'is_visible_to_customer': 1, }, 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 12}, {'abacus_event_id': 2, 'statement_period_id': 14}, ], 'ledger_account_contract': [ { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 1, }, { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 2, }, ], }) first_statement_period_id = 12 items, total = StatementPeriod.get_for_account_activity( ACCOUNT_ID, first_statement_period_id, order_dir=order_dir) assert [r.statement_period_id for r in items] == expected_ids assert total == 3 def test_get_for_account_activity_not_shown(): """Test getting statement periods when current period is not visible.""" insert_mock_data({ 'account': {'account_id': ACCOUNT_ID}, 'reference_payment_entity': {'reference_payment_entity_id': PAYMENT_ENTITY_ID}, 'reference_sap_profit_center': { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK1234', 'company_code': '1234', 'business_group': 'ORC' }, 'reference_signing_entity': { 'reference_signing_entity_id': 1, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': { 'contract_id': 1001, 'reference_signing_entity_id': 1, }, 'statement_period': [ {'statement_period_id': 11, 'statement_period_status': 'closed'}, {'statement_period_id': 12, 'statement_period_status': 'closed'}, {'statement_period_id': 13, 'statement_period_status': 'closed'}, {'statement_period_id': 14, 'statement_period_status': 'current'}, {'statement_period_id': 15, 'statement_period_status': 'open'}, ], 'account_payment_term': { 'account_payment_term_id': 1, 'account_id': ACCOUNT_ID, 'currency_code': 'USD', 'payment_entity_id': PAYMENT_ENTITY_ID, 'payment_schedule': '30_days_after_month_end' }, 'statement_period_payment_entity': { 'statement_period_payment_entity_id': 1, 'statement_period_id': 14, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'is_visible_to_customer': 0, }, 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 12}, {'abacus_event_id': 2, 'statement_period_id': 14}, ], 'ledger_account_contract': [ { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 1, }, { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 2, }, ], }) first_statement_period_id = 12 items, total = StatementPeriod.get_for_account_activity(ACCOUNT_ID, first_statement_period_id) assert [r.statement_period_id for r in items] == [12, 13] assert total == 2 def test_get_for_account_activity_with_contract(): """Test getting statement periods for an account/contract combo.""" account_id = 24601 contract_id = 1002 insert_mock_data({ 'account': {'account_id': account_id}, 'reference_payment_entity': {'reference_payment_entity_id': PAYMENT_ENTITY_ID}, 'reference_sap_profit_center': { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK1234', 'company_code': '1234', 'business_group': 'ORC' }, 'reference_signing_entity': { 'reference_signing_entity_id': 1, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': [ { 'contract_id': 1001, 'reference_signing_entity_id': 1, }, { 'contract_id': contract_id, 'reference_signing_entity_id': 1, }, ], 'statement_period': [ {'statement_period_id': 11, 'statement_period_status': 'closed'}, {'statement_period_id': 12, 'statement_period_status': 'closed'}, {'statement_period_id': 13, 'statement_period_status': 'closed'}, {'statement_period_id': 14, 'statement_period_status': 'current'}, {'statement_period_id': 15, 'statement_period_status': 'open'}, ], 'statement_period_payment_entity': { 'statement_period_payment_entity_id': 1, 'statement_period_id': 14, 'reference_payment_entity_id': PAYMENT_ENTITY_ID, 'is_visible_to_customer': 1, }, 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 11}, {'abacus_event_id': 2, 'statement_period_id': 12}, {'abacus_event_id': 3, 'statement_period_id': 13}, {'abacus_event_id': 4, 'statement_period_id': 13}, {'abacus_event_id': 5, 'statement_period_id': 14}, {'abacus_event_id': 6, 'statement_period_id': 14}, ], 'ledger_account_contract': [ { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 1, }, { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 2, }, { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 3, }, { 'account_id': ACCOUNT_ID, 'contract_id': contract_id, 'abacus_event_id': 4, }, { 'account_id': ACCOUNT_ID, 'contract_id': 1001, 'abacus_event_id': 5, }, { 'account_id': ACCOUNT_ID, 'contract_id': contract_id, 'abacus_event_id': 6, }, ], }) first_statement_period_id = 13 items, total = StatementPeriod.get_for_account_activity( ACCOUNT_ID, first_statement_period_id, contract_id) assert [r.statement_period_id for r in items] == [13] assert total == 1 @pytest.mark.parametrize('input_ids', [ [51], [51, 52, 54], [] ]) def test_init(input_ids): """Test getting objects by IDs.""" insert_mock_statement_period(51) insert_mock_statement_period(52) insert_mock_statement_period(53) insert_mock_statement_period(54) insert_mock_statement_period(55) result = StatementPeriod.get_by_ids(input_ids) assert len(result) == len(input_ids) for i in range(len(input_ids)): assert result[i].statement_period_id == input_ids[i]