"""Worksheet Adjustment model tests.""" from decimal import Decimal from moneyhub.constants.constants import VAT_ADJUSTMENT_TYPE_ID from moneyhub.models.worksheet_adjustment import WorksheetAdjustment from tests.unit.conftest import insert_mock_data def _insert_mock_data(): """Insert mock data for the tests.""" insert_mock_data({ 'account': {'account_id': 1235}, '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': 4343, 'reference_signing_entity_id': 1, }, { 'contract_id': 999, 'reference_signing_entity_id': 1, }, ], 'statement_period': [ {'statement_period_id': 12, 'statement_period_status': 'closed'}, {'statement_period_id': 14, 'statement_period_status': 'current'}, ], 'reference_adjustment_type': [ { 'reference_adjustment_type_id': 1, 'type_name': 'Label Earnings' }, { 'reference_adjustment_type_id': 2, 'type_name': 'Publisher Earnings' }, { 'reference_adjustment_type_id': VAT_ADJUSTMENT_TYPE_ID, 'type_name': 'VAT Tax', } ], 'abacus_event': [ {'abacus_event_id': 1}, {'abacus_event_id': 2}, {'abacus_event_id': 3}, {'abacus_event_id': 4}, {'abacus_event_id': 5}, ], 'statement_period_adjustment_file': { 'statement_period_adjustment_file_id': 1, 'statement_period_id': 14, }, 'worksheet_adjustment': [ { 'worksheet_adjustment_id': 1, 'abacus_event_id': 1, 'statement_period_adjustment_file_id': 1, 'account_id': 1235, 'contract_id': 4343, 'activity_statement_period_id': 12, 'apply_to_statement_period_id': 14, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 2, 'abacus_event_id': 2, 'statement_period_adjustment_file_id': 1, 'account_id': 1235, 'contract_id': 999, 'activity_statement_period_id': 12, 'apply_to_statement_period_id': 14, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 3, 'abacus_event_id': 3, 'statement_period_adjustment_file_id': 1, 'account_id': 1235, 'contract_id': 4343, 'activity_statement_period_id': 12, 'apply_to_statement_period_id': 14, 'reference_adjustment_type_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 4, 'abacus_event_id': 3, 'statement_period_adjustment_file_id': 1, 'account_id': 1235, 'contract_id': 4343, 'activity_statement_period_id': 12, 'apply_to_statement_period_id': 14, 'reference_adjustment_type_id': 2, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, { 'worksheet_adjustment_id': 5, 'abacus_event_id': 3, 'statement_period_adjustment_file_id': 1, 'account_id': 1235, 'contract_id': 4343, 'activity_statement_period_id': 12, 'apply_to_statement_period_id': 14, 'reference_adjustment_type_id': VAT_ADJUSTMENT_TYPE_ID, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', }, ], 'ledger_adjustment_applied': [ { 'ledger_adjustment_applied_id': 1, 'abacus_event_id': 1, 'account_id': 1235, 'contract_id': 4343, 'statement_period_id': 14, 'worksheet_adjustment_id': 1, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 2, 'abacus_event_id': 2, 'account_id': 1235, 'contract_id': 999, 'statement_period_id': 14, 'worksheet_adjustment_id': 2, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, { 'ledger_adjustment_applied_id': 3, 'abacus_event_id': 3, 'account_id': 1235, 'contract_id': 4343, 'statement_period_id': 14, 'worksheet_adjustment_id': 4, 'adjustment_amount': 1000, 'adjustment_currency_code': 'USD', 'adjustment_amount_payee_currency': 800, 'adjustment_payee_currency_code': 'GBP', }, ], }) def test_get_by_account_id(): """Test to get ledger adjustments for a specified account.""" account_id = 1235 statement_period_id = 14 _insert_mock_data() res = WorksheetAdjustment.get_by_account_id( account_id, None, statement_period_id, None, None) assert len(res) == 3 assert all(entry.account_id == account_id for entry in res) def test_get_by_account_and_contract_id(): """Test to get ledger adjustments for a specified account and contract.""" account_id = 1235 contract_id = 4343 _insert_mock_data() res = WorksheetAdjustment.get_by_account_id( account_id, contract_id, None, None, None) assert len(res) == 2 assert all(entry.account_id == account_id for entry in res) assert all(entry.contract_id == contract_id for entry in res) def test_get_by_account_and_contract_id_and_statement_period_range(): """Test to get ledger adjustments for a specified account and contract. Adds filtering by statement period. """ account_id = 1235 contract_id = 4343 statement_period_id_start = 13 statement_period_id_end = 14 _insert_mock_data() res = WorksheetAdjustment.get_by_account_id( account_id, contract_id, statement_period_id_start, statement_period_id_end, None) assert len(res) == 2 assert all(entry.account_id == account_id for entry in res) assert all(entry.contract_id == contract_id for entry in res) def test_get_by_account_id_and_adjustment_type(): """Test to get ledger adjustments for a specified account filtering by adjustment type.""" account_id = 1235 adjustment_type_id = 2 _insert_mock_data() res = WorksheetAdjustment.get_by_account_id( account_id, None, None, None, adjustment_type_id) assert len(res) == 1 assert all(entry.account_id == account_id for entry in res) def test_get_vat_adjustments(): """Test getting VAT adjustments.""" statement_period_id = 14 _insert_mock_data() result = WorksheetAdjustment.get_vat_adjustments(statement_period_id) assert [item.to_dict() for item in result] == [ { 'account_id': 1235, 'contract_id': 4343, 'abacus_event_id': 3, 'worksheet_adjustment_id': 5, 'activity_statement_period_id': 12, 'adjustment_amount': Decimal('1000.00'), 'adjustment_currency_code': 'USD', 'apply_to_statement_period_id': 14, 'reference_adjustment_type_id': VAT_ADJUSTMENT_TYPE_ID, 'statement_period_adjustment_file_id': 1, 'internal_note': None, 'created_by': None, 'note': None } ] def test_get_account_adjustments_activity(): """Test getting adjustments activity.""" account_id = 1235 _insert_mock_data() result = WorksheetAdjustment.get_account_adjustments_activity(account_id) assert bool(result) is True def test_get_account_adjustments_activity_no_activity(): """Test getting adjustments activity no activity.""" account_id = 1235 insert_mock_data({ 'account': {'account_id': account_id}, }) result = WorksheetAdjustment.get_account_adjustments_activity(account_id) assert bool(result) is False