"""Ledger Accounting Run Balance model tests.""" from decimal import Decimal from unittest.mock import patch from ledger.constants.constants import VAT_CATEGORIES_OLD from ledger.models.ledger_accounting_run_balance import LedgerAccountingRunBalance from tests.utils.factories import LedgerAccountingRunBalanceFactory def test_create_ledger_accounting_run_balance(mock_event_fixtures): """Create a Ledger Accounting Run Balance.""" LedgerAccountingRunBalance.create( accounting_run_id=1, abacus_event_id=1, contract_id=1, currency_code='USD', total_gross_revenue_amount=Decimal('710.03'), total_net_revenue_amount=Decimal('659.31'), mechanical_deduction_total=Decimal('23.00'), mechanical_deduction_admin_fee_total=Decimal('27.22'), adjusted_net_revenue=Decimal('609.09'), distribution_fee=Decimal('50.72'), ) records = LedgerAccountingRunBalance.query.all() assert len(records) == 1 record = records[0] assert record.accounting_run_id == 1 assert record.abacus_event_id == 1 assert record.contract_id == 1 assert record.currency_code == 'USD' assert record.total_gross_revenue_amount == Decimal('710.03') assert record.total_net_revenue_amount == Decimal('659.31') assert record.mechanical_deduction_total == Decimal('23.00') assert record.mechanical_deduction_admin_fee_total == Decimal('27.22') assert record.adjusted_net_revenue == Decimal('609.09') assert record.distribution_fee == Decimal('50.72') def test_find_by_accounting_run_id(mock_event_fixtures): """Test getting Ledger Accounting Run Balance entries by accounting_run_id.""" accounting_run_id = 1 entry = LedgerAccountingRunBalanceFactory.create( accounting_run_id=accounting_run_id ) params = {'accounting_run_id': accounting_run_id, 'limit': 10, 'offset': 0} items, total_count = LedgerAccountingRunBalance.find_by_accounting_run_id(**params) assert ( items[0].ledger_accounting_run_balance_id == entry.ledger_accounting_run_balance_id ) assert items[0].accounting_run_id == entry.accounting_run_id assert total_count def test_get_by_accounting_period_and_vat_category_count(mock_event_fixtures): """Test to get Ledger Accounting Run Balance entries count.""" LedgerAccountingRunBalanceFactory.create(accounting_run_id=1, contract_id=1) vat_exempt_params = {'accounting_period_id': 1, 'vat_category': 'vat_exempt'} vat_exempt_ledger_balance_count = ( LedgerAccountingRunBalance.get_by_accounting_period_and_vat_category_count( **vat_exempt_params ) ) assert vat_exempt_ledger_balance_count == 1 def test_get_by_accounting_period_and_vat_category_run_status(mock_event_fixtures): """Test get ledger_accounting_run_balances returns records for committed runs.""" LedgerAccountingRunBalanceFactory.create(accounting_run_id=3, contract_id=1) LedgerAccountingRunBalanceFactory.create(accounting_run_id=4, contract_id=2) vat_exempt_params = { 'accounting_period_id': 1, 'vat_category': VAT_CATEGORIES_OLD.VAT_EXEMPT, 'limit': 10, 'offset': 0, } vat_applied_params = { 'accounting_period_id': 1, 'vat_category': VAT_CATEGORIES_OLD.VAT_APPLIED, 'limit': 10, 'offset': 0, } vat_exempt_ledger_balance_records = ( LedgerAccountingRunBalance.get_by_accounting_period_and_vat_category( **vat_exempt_params ) ) assert len(vat_exempt_ledger_balance_records) == 0 vat_applied_ledger_balance_records = ( LedgerAccountingRunBalance.get_by_accounting_period_and_vat_category( **vat_applied_params ) ) assert len(vat_applied_ledger_balance_records) == 0 def test_get_by_accounting_period_and_vat_category(mock_event_fixtures): """Test to get Ledger Accounting Run Balance entries by accounting_period_id.""" entry_1 = LedgerAccountingRunBalanceFactory.create( accounting_run_id=1, contract_id=1 ) entry_2 = LedgerAccountingRunBalanceFactory.create( accounting_run_id=2, contract_id=4 ) vat_exempt_params = { 'accounting_period_id': 1, 'vat_category': VAT_CATEGORIES_OLD.VAT_EXEMPT, 'limit': 10, 'offset': 0, } vat_applied_params = { 'accounting_period_id': 1, 'vat_category': VAT_CATEGORIES_OLD.VAT_APPLIED, 'limit': 10, 'offset': 0, } vat_exempt_ledger_balance_records = ( LedgerAccountingRunBalance.get_by_accounting_period_and_vat_category( **vat_exempt_params ) ) assert len(vat_exempt_ledger_balance_records) == 1 assert ( vat_exempt_ledger_balance_records[0].accounting_run_id == entry_1.accounting_run_id ) assert vat_exempt_ledger_balance_records[0].payment_entity_id == 1 assert vat_exempt_ledger_balance_records[0].is_vat_exempt == 1 vat_applied_ledger_balance_records = ( LedgerAccountingRunBalance.get_by_accounting_period_and_vat_category( **vat_applied_params ) ) assert len(vat_applied_ledger_balance_records) == 1 assert ( vat_applied_ledger_balance_records[0].accounting_run_id == entry_2.accounting_run_id ) assert vat_applied_ledger_balance_records[0].payment_entity_id == 1 def test_find_contract_count_by_accounting_run_ids_returns_correct_counts( mock_event_fixtures, ): """Test to get contract count by accounting_period_id.""" LedgerAccountingRunBalanceFactory.create(accounting_run_id=1, contract_id=1) LedgerAccountingRunBalanceFactory.create(accounting_run_id=2, contract_id=4) result = LedgerAccountingRunBalance.find_contract_count_by_accounting_run_ids( [1, 2] ) assert result == [ {'accounting_run_id': 1, 'contract_count': 1}, {'accounting_run_id': 2, 'contract_count': 1}, ] def test_find_contract_count_by_accounting_run_ids_returns_empty_for_missing_ids( mock_event_fixtures, ): """Test to get contract count by accounting_period_id returns empty.""" result = LedgerAccountingRunBalance.find_contract_count_by_accounting_run_ids([999]) assert result == [] def test_find_contract_count_by_accounting_run_ids_returns_zero_if_no_contracts( mock_event_fixtures, ): """Test to get contract count by accounting_period_id returns zero.""" LedgerAccountingRunBalanceFactory.create(accounting_run_id=1, contract_id=1) result = LedgerAccountingRunBalance.find_contract_count_by_accounting_run_ids([2]) assert result == [{'accounting_run_id': 2, 'contract_count': 0}]