"""Tests for the ledger account model.""" from decimal import Decimal import pytest from moneyhub.models.ledger_accounting_run_vat import LedgerAccountingRunVat from tests.unit.conftest import insert_mock_data @pytest.mark.parametrize('statement_period_id, expected_ids', [ (101, [1]), (102, [2, 3]), (103, [4, 5]), ]) def test_get_by_statement_period(statement_period_id, expected_ids): """Test getting contracts by statement period.""" insert_mock_data({ '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': 1001, 'reference_signing_entity_id': 1, }, { 'contract_id': 1002, 'reference_signing_entity_id': 1, }, { 'contract_id': 1003, 'reference_signing_entity_id': 1, }, { 'contract_id': 1004, 'reference_signing_entity_id': 1, }, ], 'statement_period': [ {'statement_period_id': 101}, {'statement_period_id': 102}, {'statement_period_id': 103}, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 101}, {'abacus_event_id': 2, 'statement_period_id': 102}, {'abacus_event_id': 3, 'statement_period_id': 102}, {'abacus_event_id': 4, 'statement_period_id': 103}, {'abacus_event_id': 5, 'statement_period_id': 103}, ], 'run_controller': {'run_controller_id': 5}, 'accounting_period': [ {'accounting_period_id': 10, 'statement_period_id': 101}, {'accounting_period_id': 11, 'statement_period_id': 102}, {'accounting_period_id': 12, 'statement_period_id': 103}, ], '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' }, ], 'ledger_accounting_run_vat': [ { 'ledger_accounting_run_vat_id': 1, 'accounting_run_id': 1, 'contract_id': 1001, 'abacus_event_id': 1, 'distribution_fee': 123, }, { 'ledger_accounting_run_vat_id': 2, 'accounting_run_id': 2, 'contract_id': 1001, 'abacus_event_id': 2, 'distribution_fee': 321, }, { 'ledger_accounting_run_vat_id': 3, 'accounting_run_id': 2, 'contract_id': 1002, 'abacus_event_id': 3, 'distribution_fee': 111, }, { 'ledger_accounting_run_vat_id': 4, 'accounting_run_id': 3, 'contract_id': 1003, 'abacus_event_id': 4, 'distribution_fee': 222, }, { 'ledger_accounting_run_vat_id': 5, 'accounting_run_id': 3, 'contract_id': 1004, 'abacus_event_id': 5, 'distribution_fee': None, 'exempt_reason': 'Some legit reason' }, ], }) result = LedgerAccountingRunVat.get_by_statement_period(statement_period_id) assert [item.ledger_accounting_run_vat_id for item in result] == expected_ids def test_get_committed_for_account(): """Test getting committed entries for a given account.""" 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, }, 'account_contract': {'account_id': account_id, 'contract_id': contract_id}, 'statement_period': [ {'statement_period_id': 11}, {'statement_period_id': 12}, ], 'accounting_period': [ {'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': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 2, 'accounting_period_id': 12, 'run_controller_id': 5, 'run_status': 'Committed' }, ], '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': 12}, ], 'ledger_accounting_run_vat': [ { 'contract_id': contract_id, 'currency_code': 'NOK', 'accounting_run_id': 1, 'abacus_event_id': 1, 'gross_revenue': 10, 'gross_vat_rate': 20, 'distribution_fee': 10, 'distribution_vat_rate': 20, 'gross_vat': 20, 'distribution_vat': 4, 'net_revenue': 80, 'adjusted_net_revenue': 80, }, { 'contract_id': contract_id, 'currency_code': 'NOK', 'accounting_run_id': 2, 'abacus_event_id': 2, 'gross_revenue': 10, 'gross_vat_rate': 20, 'distribution_fee': 10, 'distribution_vat_rate': 20, 'gross_vat': 60, 'distribution_vat': 0.2, 'net_revenue': 240, 'adjusted_net_revenue': 240, }, { 'contract_id': contract_id, 'currency_code': 'NOK', 'accounting_run_id': 2, 'abacus_event_id': 3, 'gross_revenue': 10, 'gross_vat_rate': -10, 'distribution_fee': 10, 'distribution_vat_rate': -10, 'gross_vat': -30, 'distribution_vat': -0.1, 'net_revenue': -120, 'adjusted_net_revenue': -120, }, ], }) result = LedgerAccountingRunVat.get_committed_for_account(account_id) assert [item._asdict() for item in result] == [ { 'ledger_accounting_run_vat_id': 1, 'account_id': account_id, 'contract_id': contract_id, 'statement_period_id': 11, 'currency_code': 'NOK', 'gross_revenue': Decimal('10.00'), 'gross_vat_rate': Decimal('20.00'), 'distribution_fee': Decimal('10.00'), 'distribution_vat_rate': Decimal('20.00'), 'gross_vat': Decimal('20.00'), 'distribution_vat': Decimal('4.00'), 'net_revenue': Decimal('80.00'), 'adjusted_net_revenue': Decimal('80.00'), }, { 'ledger_accounting_run_vat_id': 2, 'account_id': account_id, 'contract_id': contract_id, 'statement_period_id': 12, 'currency_code': 'NOK', 'gross_revenue': Decimal('10.00'), 'gross_vat_rate': Decimal('20.00'), 'distribution_fee': Decimal('10.00'), 'distribution_vat_rate': Decimal('20.00'), 'gross_vat': Decimal('60.00'), 'distribution_vat': Decimal('0.20'), 'net_revenue': Decimal('240.00'), 'adjusted_net_revenue': Decimal('240.00'), }, { 'ledger_accounting_run_vat_id': 3, 'account_id': account_id, 'contract_id': contract_id, 'statement_period_id': 12, 'currency_code': 'NOK', 'gross_revenue': Decimal('10.00'), 'gross_vat_rate': Decimal('-10.00'), 'distribution_fee': Decimal('10.00'), 'distribution_vat_rate': Decimal('-10.00'), 'gross_vat': Decimal('-30.00'), 'distribution_vat': Decimal('-0.10'), 'net_revenue': Decimal('-120.00'), 'adjusted_net_revenue': Decimal('-120.00'), }, ] def test_get_committed_for_account_contract_statement_periods(): """Test getting committed entries for a given account, contract, and statement periods.""" account_id = 24601 contract_id = 10001 statement_period_ids = [123, 124] 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': 99999, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': account_id, 'contract_id': contract_id}, {'account_id': account_id, 'contract_id': 99999}, ], 'statement_period': [ {'statement_period_id': statement_period_ids[0]}, {'statement_period_id': statement_period_ids[1]}, {'statement_period_id': 999}, ], 'accounting_period': [ {'accounting_period_id': 11, 'statement_period_id': statement_period_ids[0]}, {'accounting_period_id': 12, 'statement_period_id': statement_period_ids[1]}, {'accounting_period_id': 13, 'statement_period_id': 999}, ], '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': 12, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 3, 'accounting_period_id': 13, 'run_controller_id': 5, 'run_status': 'Committed' }, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': statement_period_ids[0]}, {'abacus_event_id': 2, 'statement_period_id': statement_period_ids[1]}, {'abacus_event_id': 3, 'statement_period_id': 999}, {'abacus_event_id': 4, 'statement_period_id': 999}, ], 'ledger_accounting_run_vat': [ { 'contract_id': contract_id, 'currency_code': 'NOK', 'accounting_run_id': 1, 'abacus_event_id': 1, 'gross_revenue': 10, 'gross_vat_rate': 20, 'distribution_fee': 10, 'distribution_vat_rate': 20, 'gross_vat': 20, 'distribution_vat': 4, 'net_revenue': 80, 'adjusted_net_revenue': 80, }, { 'contract_id': contract_id, 'currency_code': 'NOK', 'accounting_run_id': 2, 'abacus_event_id': 2, 'gross_revenue': 10, 'gross_vat_rate': 20, 'distribution_fee': 10, 'distribution_vat_rate': 20, 'gross_vat': 20, 'distribution_vat': 4, 'net_revenue': 80, 'adjusted_net_revenue': 80, }, { 'contract_id': 99999, 'currency_code': 'NOK', 'accounting_run_id': 2, 'abacus_event_id': 3, 'gross_revenue': 10, 'gross_vat_rate': 20, 'distribution_fee': 10, 'distribution_vat_rate': 20, 'gross_vat': 60, 'distribution_vat': 0.2, 'net_revenue': 240, 'adjusted_net_revenue': 240, }, { 'contract_id': contract_id, 'currency_code': 'NOK', 'accounting_run_id': 3, 'abacus_event_id': 4, 'gross_revenue': 10, 'gross_vat_rate': -10, 'distribution_fee': 10, 'distribution_vat_rate': -10, 'gross_vat': -30, 'distribution_vat': -0.1, 'net_revenue': -120, 'adjusted_net_revenue': -120, }, ], }) result = LedgerAccountingRunVat.get_committed_for_account( account_id, contract_id, statement_period_ids) assert [item._asdict() for item in result] == [ { 'ledger_accounting_run_vat_id': 1, 'account_id': account_id, 'contract_id': contract_id, 'statement_period_id': statement_period_ids[0], 'currency_code': 'NOK', 'gross_revenue': Decimal('10.00'), 'gross_vat_rate': Decimal('20.00'), 'distribution_fee': Decimal('10.00'), 'distribution_vat_rate': Decimal('20.00'), 'gross_vat': Decimal('20.00'), 'distribution_vat': Decimal('4.00'), 'net_revenue': Decimal('80.00'), 'adjusted_net_revenue': Decimal('80.00'), }, { 'ledger_accounting_run_vat_id': 2, 'account_id': account_id, 'contract_id': contract_id, 'statement_period_id': statement_period_ids[1], 'currency_code': 'NOK', 'gross_revenue': Decimal('10.00'), 'gross_vat_rate': Decimal('20.00'), 'distribution_fee': Decimal('10.00'), 'distribution_vat_rate': Decimal('20.00'), 'gross_vat': Decimal('20.00'), 'distribution_vat': Decimal('4.00'), 'net_revenue': Decimal('80.00'), 'adjusted_net_revenue': Decimal('80.00'), }, ] def test_get_by_payment_entity_and_statement_period(): """Test getting entries for a given payment entity and statement period.""" payment_entity_id = 2 statement_period_id = 11 insert_mock_data({ 'account': {'account_id': 1}, 'reference_payment_entity': {'reference_payment_entity_id': 2}, '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': 2, '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': 1, 'reference_signing_entity_id': 1, }, 'statement_period': [ {'statement_period_id': 11}, {'statement_period_id': 12}, ], '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': 12}, ], 'ledger_account_contract': {'account_id': 1, 'contract_id': 1, 'abacus_event_id': 1}, 'accounting_period': [ {'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': 11, 'run_controller_id': 5, 'run_status': 'Committed' }, { 'accounting_run_id': 2, 'accounting_period_id': 12, 'run_controller_id': 5, 'run_status': 'Committed' }, ], 'account_payment_term': [ { 'account_payment_term_id': 1, 'account_id': 1, 'currency_code': 'GBP', 'payment_minimum': 42.00, 'payment_entity_id': 2, 'payment_schedule': None, } ], 'ledger_accounting_run_vat': [ { 'contract_id': 1, 'currency_code': 'NOK', 'accounting_run_id': 1, 'abacus_event_id': 1, 'gross_revenue': 10, 'gross_vat_rate': 20, 'distribution_fee': 10, 'distribution_vat_rate': 20, 'gross_vat': 20, 'distribution_vat': 4, 'net_revenue': 80, 'adjusted_net_revenue': 80, } ], }) result = LedgerAccountingRunVat.get_by_payment_entity_and_statement_period( payment_entity_id, statement_period_id) assert result.ledger_accounting_run_vat_id == 1