"""Tests for ledger accounting run balance model.""" from decimal import Decimal from moneyhub.models.ledger_accounting_run_balance import LedgerAccountingRunBalance from tests.unit.conftest import insert_mock_data def test_get_for_account(): """Test getting the accounting run balance for an account.""" account_id = 24601 contract_id = 10001 insert_mock_data({ 'account': [ {'account_id': account_id}, {'account_id': account_id + 1}, ], 'reference_payment_entity': {'reference_payment_entity_id': 1}, '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': 1, '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': contract_id, 'reference_signing_entity_id': 1, }, { 'contract_id': contract_id + 1, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': account_id, 'contract_id': contract_id}, {'account_id': account_id + 1, 'contract_id': contract_id + 1}, ], 'statement_period': [ {'statement_period_id': 10}, {'statement_period_id': 11}, {'statement_period_id': 12}, ], 'accounting_period': [ {'accounting_period_id': 10, 'statement_period_id': 10}, {'accounting_period_id': 11, 'statement_period_id': 11}, {'accounting_period_id': 12, 'statement_period_id': 12}, ], 'run_controller': {'run_controller_id': 5}, 'accounting_run': [ { 'accounting_run_id': 1, 'accounting_period_id': 10, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 2, 'accounting_period_id': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 3, 'accounting_period_id': 12, 'run_controller_id': 5, 'run_status': 'Committed' }, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 10}, {'abacus_event_id': 2, 'statement_period_id': 11}, {'abacus_event_id': 3, 'statement_period_id': 11}, {'abacus_event_id': 4, 'statement_period_id': 12}, ], 'ledger_accounting_run_balance': [ { 'contract_id': contract_id + 1, 'accounting_run_id': 1, 'abacus_event_id': 1, 'currency_code': 'NOK', 'total_gross_revenue_amount': 10, 'total_net_revenue_amount': 8, 'distribution_fee': 2, 'mechanical_deduction_total': 3, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 5, }, { 'contract_id': contract_id + 1, 'accounting_run_id': 2, 'abacus_event_id': 2, 'currency_code': 'NOK', 'total_gross_revenue_amount': 1234, 'total_net_revenue_amount': 1035, 'distribution_fee': 200, 'mechanical_deduction_total': 34, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 1000, }, { 'contract_id': contract_id, 'accounting_run_id': 2, 'abacus_event_id': 3, 'currency_code': 'NOK', 'total_gross_revenue_amount': 123, 'total_net_revenue_amount': 103, 'distribution_fee': 20, 'mechanical_deduction_total': 3, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 100, }, { 'contract_id': contract_id, 'accounting_run_id': 3, 'abacus_event_id': 4, 'currency_code': 'NOK', 'total_gross_revenue_amount': 321, 'total_net_revenue_amount': 320, 'distribution_fee': 1, 'mechanical_deduction_total': 20, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 300, }, ], }) result = LedgerAccountingRunBalance.get_for_account(account_id) assert [item._asdict() for item in result] == [ { 'currency_code': 'NOK', 'statement_period_id': 11, 'total_gross_revenue_amount': Decimal('123.00'), 'total_net_revenue_amount': Decimal('103.00'), 'distribution_fee': Decimal('20.00'), 'mechanical_deduction_total': Decimal('3.00'), 'mechanical_deduction_admin_fee_total': Decimal('0.00'), 'adjusted_net_revenue': Decimal('100.00'), }, { 'currency_code': 'NOK', 'statement_period_id': 12, 'total_gross_revenue_amount': Decimal('321.00'), 'total_net_revenue_amount': Decimal('320.00'), 'distribution_fee': Decimal('1.00'), 'mechanical_deduction_total': Decimal('20.00'), 'mechanical_deduction_admin_fee_total': Decimal('0.00'), 'adjusted_net_revenue': Decimal('300.00'), }, ] def test_get_for_account_multiple_runs(): """Test accounting run balance when multiple runs made for the same statement period.""" account_id = 24601 contract_id = 10001 statement_period_id = 111 insert_mock_data({ 'account': [ {'account_id': account_id}, ], 'reference_payment_entity': {'reference_payment_entity_id': 1}, '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': 1, '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': contract_id, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': account_id, 'contract_id': contract_id}, ], 'statement_period': [ {'statement_period_id': statement_period_id}, ], 'accounting_period': [ {'accounting_period_id': 11, 'statement_period_id': statement_period_id}, ], 'run_controller': {'run_controller_id': 5}, 'accounting_run': [ { 'accounting_run_id': 1, 'accounting_period_id': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 2, 'accounting_period_id': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': statement_period_id}, {'abacus_event_id': 2, 'statement_period_id': statement_period_id}, {'abacus_event_id': 3, 'statement_period_id': statement_period_id}, {'abacus_event_id': 4, 'statement_period_id': statement_period_id}, ], 'ledger_accounting_run_balance': [ { 'contract_id': contract_id, 'accounting_run_id': 1, 'abacus_event_id': 1, 'currency_code': 'NOK', 'total_gross_revenue_amount': 123, 'total_net_revenue_amount': 103, 'distribution_fee': 20, 'mechanical_deduction_total': 3, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 100, }, { 'contract_id': contract_id, 'accounting_run_id': 2, 'abacus_event_id': 3, 'currency_code': 'NOK', 'total_gross_revenue_amount': 321, 'total_net_revenue_amount': 320, 'distribution_fee': 1, 'mechanical_deduction_total': 20, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 300, }, ], }) result = LedgerAccountingRunBalance.get_for_account(account_id) assert [item._asdict() for item in result] == [ { 'currency_code': 'NOK', 'statement_period_id': statement_period_id, 'total_gross_revenue_amount': Decimal('123.00'), 'total_net_revenue_amount': Decimal('103.00'), 'distribution_fee': Decimal('20.00'), 'mechanical_deduction_total': Decimal('3.00'), 'mechanical_deduction_admin_fee_total': Decimal('0.00'), 'adjusted_net_revenue': Decimal('100.00'), }, { 'currency_code': 'NOK', 'statement_period_id': statement_period_id, 'total_gross_revenue_amount': Decimal('321.00'), 'total_net_revenue_amount': Decimal('320.00'), 'distribution_fee': Decimal('1.00'), 'mechanical_deduction_total': Decimal('20.00'), 'mechanical_deduction_admin_fee_total': Decimal('0.00'), 'adjusted_net_revenue': Decimal('300.00'), }, ] def test_get_for_account_filter_contract(): """Test getting the accounting run balance for an account filtered by contract.""" account_id = 24601 contract_id = 10001 insert_mock_data({ 'account': [ {'account_id': account_id}, ], 'reference_payment_entity': {'reference_payment_entity_id': 1}, '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': 1, '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': contract_id, 'reference_signing_entity_id': 1, }, { 'contract_id': contract_id + 1, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': account_id, 'contract_id': contract_id}, {'account_id': account_id, 'contract_id': contract_id + 1}, ], 'statement_period': [ {'statement_period_id': 10}, {'statement_period_id': 11}, {'statement_period_id': 12}, ], 'accounting_period': [ {'accounting_period_id': 10, 'statement_period_id': 10}, {'accounting_period_id': 11, 'statement_period_id': 11}, {'accounting_period_id': 12, 'statement_period_id': 12}, ], 'run_controller': {'run_controller_id': 5}, 'accounting_run': [ { 'accounting_run_id': 1, 'accounting_period_id': 10, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 2, 'accounting_period_id': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 3, 'accounting_period_id': 12, 'run_controller_id': 5, 'run_status': 'Committed' }, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 10}, {'abacus_event_id': 2, 'statement_period_id': 11}, {'abacus_event_id': 3, 'statement_period_id': 11}, {'abacus_event_id': 4, 'statement_period_id': 12}, ], 'ledger_accounting_run_balance': [ { 'contract_id': contract_id, 'accounting_run_id': 1, 'abacus_event_id': 1, 'currency_code': 'NOK', 'total_gross_revenue_amount': 10, 'total_net_revenue_amount': 8, 'distribution_fee': 2, 'mechanical_deduction_total': 3, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 5, }, { 'contract_id': contract_id + 1, 'accounting_run_id': 2, 'abacus_event_id': 2, 'currency_code': 'NOK', 'total_gross_revenue_amount': 1234, 'total_net_revenue_amount': 1035, 'distribution_fee': 200, 'mechanical_deduction_total': 34, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 1000, }, { 'contract_id': contract_id, 'accounting_run_id': 2, 'abacus_event_id': 3, 'currency_code': 'NOK', 'total_gross_revenue_amount': 123, 'total_net_revenue_amount': 103, 'distribution_fee': 20, 'mechanical_deduction_total': 3, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 100, }, { 'contract_id': contract_id + 1, 'accounting_run_id': 3, 'abacus_event_id': 4, 'currency_code': 'NOK', 'total_gross_revenue_amount': 321, 'total_net_revenue_amount': 320, 'distribution_fee': 1, 'mechanical_deduction_total': 20, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 300, }, ], }) result = LedgerAccountingRunBalance.get_for_account(account_id, contract_id) assert [item._asdict() for item in result] == [ { 'currency_code': 'NOK', 'statement_period_id': 10, 'total_gross_revenue_amount': Decimal('10.00'), 'total_net_revenue_amount': Decimal('8.00'), 'distribution_fee': Decimal('2.00'), 'mechanical_deduction_total': Decimal('3.00'), 'mechanical_deduction_admin_fee_total': Decimal('0.00'), 'adjusted_net_revenue': Decimal('5.00'), }, { 'currency_code': 'NOK', 'statement_period_id': 11, 'total_gross_revenue_amount': Decimal('123.00'), 'total_net_revenue_amount': Decimal('103.00'), 'distribution_fee': Decimal('20.00'), 'mechanical_deduction_total': Decimal('3.00'), 'mechanical_deduction_admin_fee_total': Decimal('0.00'), 'adjusted_net_revenue': Decimal('100.00'), }, ] def test_get_revenue_total_for_account_statement_periods(): """Test getting the total revenue for an account.""" account_id = 24601 contract_id = 10001 insert_mock_data({ 'account': [ {'account_id': account_id}, ], 'account_payment_term': { 'account_payment_term_id': 1234, 'account_id': account_id, 'currency_code': 'NOK', 'payment_minimum': 42.00, 'payment_entity_id': 1, 'payment_schedule': None, }, 'reference_payment_entity': {'reference_payment_entity_id': 1}, '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': 1, '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': contract_id, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': account_id, 'contract_id': contract_id}, ], 'statement_period': [ {'statement_period_id': 10}, {'statement_period_id': 11}, {'statement_period_id': 12}, ], 'accounting_period': [ {'accounting_period_id': 10, 'statement_period_id': 10}, {'accounting_period_id': 11, 'statement_period_id': 11}, {'accounting_period_id': 12, 'statement_period_id': 12}, ], 'run_controller': {'run_controller_id': 5}, 'accounting_run': [ { 'accounting_run_id': 1, 'accounting_period_id': 10, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 2, 'accounting_period_id': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 3, 'accounting_period_id': 12, 'run_controller_id': 5, 'run_status': 'Committed' }, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 10}, {'abacus_event_id': 2, 'statement_period_id': 11}, {'abacus_event_id': 3, 'statement_period_id': 11}, {'abacus_event_id': 4, 'statement_period_id': 12}, ], 'ledger_accounting_run_balance': [ { 'contract_id': contract_id, 'accounting_run_id': 2, 'abacus_event_id': 3, 'currency_code': 'NOK', 'total_gross_revenue_amount': 123, 'total_net_revenue_amount': 103, 'distribution_fee': 20, 'mechanical_deduction_total': 3, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 100, }, { 'contract_id': contract_id, 'accounting_run_id': 3, 'abacus_event_id': 4, 'currency_code': 'NOK', 'total_gross_revenue_amount': 321, 'total_net_revenue_amount': 320, 'distribution_fee': 1, 'mechanical_deduction_total': 20, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 300, }, ], }) result = LedgerAccountingRunBalance.get_revenue_total_for_account_statement_periods( account_id, [10, 11, 12]) assert result._asdict() == { 'account_id': account_id, 'currency_code': 'NOK', 'gross_revenue_payee_currency': Decimal('444.00'), 'net_revenue_payee_currency': Decimal('423.00'), } def test_get_revenue_total_for_account_statement_periods_no_data(): """Test getting the total revenue for an account that has no data.""" account_id = 24601 insert_mock_data({ 'account': {'account_id': account_id} }) result = LedgerAccountingRunBalance.get_revenue_total_for_account_statement_periods( account_id, [10, 11, 12]) assert result._asdict() == { 'account_id': None, 'currency_code': None, 'gross_revenue_payee_currency': None, 'net_revenue_payee_currency': None, } def test_get_account_revenue_activity(): """Test get account revenue activity.""" account_id = 24601 contract_id = 10001 insert_mock_data({ 'account': [ {'account_id': account_id}, ], 'reference_payment_entity': {'reference_payment_entity_id': 1}, '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': 1, 'reference_sap_profit_center_id': 1, 'company_code': '1234', 'tax_entity_company_code': '1234', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', }, 'contract': [ { 'contract_id': contract_id, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': account_id, 'contract_id': contract_id}, ], 'statement_period': [ {'statement_period_id': 10}, {'statement_period_id': 11}, {'statement_period_id': 12}, ], 'accounting_period': [ {'accounting_period_id': 10, 'statement_period_id': 10}, {'accounting_period_id': 11, 'statement_period_id': 11}, {'accounting_period_id': 12, 'statement_period_id': 12}, ], 'run_controller': {'run_controller_id': 5}, 'accounting_run': [ { 'accounting_run_id': 1, 'accounting_period_id': 10, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 2, 'accounting_period_id': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 3, 'accounting_period_id': 12, 'run_controller_id': 5, 'run_status': 'Committed' }, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 10}, {'abacus_event_id': 2, 'statement_period_id': 11}, {'abacus_event_id': 3, 'statement_period_id': 11}, {'abacus_event_id': 4, 'statement_period_id': 12}, ], 'ledger_accounting_run_balance': [ { 'contract_id': contract_id, 'accounting_run_id': 2, 'abacus_event_id': 3, 'currency_code': 'NOK', 'total_gross_revenue_amount': 123, 'total_net_revenue_amount': 103, 'distribution_fee': 20, 'mechanical_deduction_total': 3, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 100, }, { 'contract_id': contract_id, 'accounting_run_id': 3, 'abacus_event_id': 4, 'currency_code': 'NOK', 'total_gross_revenue_amount': 321, 'total_net_revenue_amount': 320, 'distribution_fee': 1, 'mechanical_deduction_total': 20, 'mechanical_deduction_admin_fee_total': 0, 'adjusted_net_revenue': 300, }, ], }) result = LedgerAccountingRunBalance.get_account_revenue_activity(account_id) assert bool(result) is True def test_get_account_revenue_activity_no_activity(): """Test get account revenue activity no activity.""" account_id = 24601 insert_mock_data({ 'account': {'account_id': account_id}, }) result = LedgerAccountingRunBalance.get_account_revenue_activity(account_id) assert bool(result) is False