"""Functional tests for account statements handlers.""" from contextlib import contextmanager from decimal import Decimal from moneyhub.connectors.snowflake import db from moneyhub.models import CombinedAdvances from moneyhub.models import CombinedPayments from moneyhub.models import Expenses from tests.functional.conftest import insert_mock_data @contextmanager def _using_mock_data(mock_data: dict): """Create the necessary mock tables and fills them with the data. Args: mock_data (dict): Data to enter. """ CombinedAdvances.__table__.drop(db.engine, checkfirst=True) Expenses.__table__.drop(db.engine, checkfirst=True) CombinedPayments.__table__.drop(db.engine, checkfirst=True) db.session.commit() CombinedAdvances.__table__.create(db.engine) Expenses.__table__.create(db.engine) CombinedPayments.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() CombinedAdvances.__table__.drop(db.engine) Expenses.__table__.drop(db.engine) CombinedPayments.__table__.drop(db.engine) db.session.close() def test_load_vat_by_account_and_statement_periods(fixture_client): """Test loading VAT per statement period for an account.""" account_id = 24601 statement_period_ids = [123, 234] 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': 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': 100, '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': 1000, 'gross_vat_rate': 20, 'distribution_fee': 10, 'distribution_vat_rate': 20, 'gross_vat': 20, 'distribution_vat': 4, 'net_revenue': 800, 'adjusted_net_revenue': 800, }, { 'contract_id': 99999, 'currency_code': 'NOK', 'accounting_run_id': 2, 'abacus_event_id': 3, 'gross_revenue': 100, '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': 100, '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, }, ], }) payload = { 'contract_id': contract_id, 'statement_period_ids': statement_period_ids } res = fixture_client.post( f'account-statements/dataloader/account/{account_id}/vat', json=payload ) assert res.status_code == 200 assert res.json() == [ { 'statement_period_id': statement_period_ids[0], 'account_id': account_id, 'payee_currency_code': 'NOK', 'base_amount_payee_currency': Decimal('100.0'), 'net_amount_payee_currency': Decimal('80.0'), 'vat_amount_payee_currency': Decimal('20.0'), 'currency_code': 'NOK', 'gross_revenue': Decimal('100.0'), 'net_revenue': Decimal('80.0'), 'distribution_fee': Decimal('10.0'), 'gross_vat_rate': Decimal('20.0'), 'distribution_vat_rate': Decimal('20.0'), 'gross_vat': Decimal('20.0'), 'distribution_vat': Decimal('4.0'), 'adjusted_net_revenue': Decimal('80.0'), }, { 'statement_period_id': statement_period_ids[1], 'account_id': account_id, 'payee_currency_code': 'NOK', 'base_amount_payee_currency': Decimal('1000.0'), 'net_amount_payee_currency': Decimal('800.0'), 'vat_amount_payee_currency': Decimal('20.0'), 'currency_code': 'NOK', 'gross_revenue': Decimal('1000.0'), 'net_revenue': Decimal('800.0'), 'distribution_fee': Decimal('10.0'), 'gross_vat_rate': Decimal('20.0'), 'distribution_vat_rate': Decimal('20.0'), 'gross_vat': Decimal('20.0'), 'distribution_vat': Decimal('4.0'), 'adjusted_net_revenue': Decimal('800.0'), }, ] def test_load_payments_for_account_and_statement_period(fixture_client): """Test to loading payments using unified data.""" account_id = 10 payments_mock = { 'combined_payments_dbt': [ { 'account_id': account_id, 'contract_id': 1, 'currency_code': 'USD', 'currency_amount': -5000.00, 'created_at': '2022-01-01 11:55:55', 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': 3, 'withholding_tax_currency_code': 'USD', 'withholding_tax_currency_amount': -1000.00, 'withholding_tax_created_at': '2022-01-01', 'unique_key': '1', }, { 'account_id': account_id, 'contract_id': 2, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': None, 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None, 'unique_key': '2', }, ], } payload = { 'contract_id': None, 'statement_period_ids': [20, 30] } with _using_mock_data(payments_mock): res = fixture_client.post( f'/account-statements/dataloader/account/{account_id}/payments', json=payload) assert res.status_code == 200 assert res.json() == [ { 'ledger_account_id': '1', 'statement_period_id': 20, 'account_id': account_id, 'contract_id': 1, 'action_status': 'complete', 'currency_code': 'USD', 'currency_amount': -5000.00, 'created_at': '2022-01-01', 'event_name': 'send_payments', 'withholding_tax_ledger_account_id': 3, 'withholding_tax_currency_code': 'USD', 'withholding_tax_currency_amount': -1000.00, 'withholding_tax_created_at': '2022-01-01' }, { 'ledger_account_id': '2', 'account_id': account_id, 'contract_id': 2, 'currency_code': 'USD', 'currency_amount': -2000.00, 'created_at': None, 'statement_period_id': 20, 'event_name': 'send_payments', 'action_status': 'complete', 'withholding_tax_ledger_account_id': None, 'withholding_tax_currency_code': None, 'withholding_tax_currency_amount': None, 'withholding_tax_created_at': None }, ] def test_load_payment_allocations(fixture_client): """Test to loading payment allocations.""" account_id = 10 payments_mock = { 'reference_adjustment_type': [ { 'reference_adjustment_type_id': 1, 'type_name': 'Fake Flowthrough Adjustment', }, { 'reference_adjustment_type_id': 2, 'type_name': 'Vat', }, ], 'worksheet_adjustment': [ { 'worksheet_adjustment_id': 1, 'contract_id': 1, 'activity_statement_period_id': 11, 'adjustment_amount': 361.30, 'adjustment_currency_code': 'GBP', 'apply_to_statement_period_id': 11, 'reference_adjustment_type_id': 1, 'abacus_event_id': 1, 'statement_period_adjustment_file_id': 1, 'account_id': account_id, }, { 'worksheet_adjustment_id': 2, 'contract_id': 1, 'activity_statement_period_id': 11, 'adjustment_amount': 36.13, 'adjustment_currency_code': 'GBP', 'apply_to_statement_period_id': 11, 'reference_adjustment_type_id': 2, 'abacus_event_id': 1, 'statement_period_adjustment_file_id': 1, 'account_id': account_id, } ], 'ledger_adjustment_applied': [ { 'ledger_adjustment_applied_id': 1, 'ledger_adjustment_id': 1, 'worksheet_adjustment_id': 1, 'statement_period_id': 11, 'adjustment_amount': 361.30, 'adjustment_amount_payee_currency': 361.30, 'adjustment_currency_code': 'GBP', 'adjustment_payee_currency_code': 'GBP', 'abacus_event_id': 1, 'account_id': account_id, }, { 'ledger_adjustment_applied_id': 2, 'ledger_adjustment_id': 2, 'worksheet_adjustment_id': 2, 'statement_period_id': 11, 'adjustment_amount': 36.13, 'adjustment_amount_payee_currency': 36.13, 'adjustment_currency_code': 'GBP', 'adjustment_payee_currency_code': 'GBP', 'abacus_event_id': 1, 'account_id': account_id, }, ], 'payment_allocation': [ { 'payment_allocation_id': 1, 'contract_id': 1, 'statement_period_id': 11, 'payment_allocation_type': 'flowthrough', } ], 'payment_allocation_ledger_adjustment': [ { 'payment_allocation_ledger_adjustment_id': 1, 'payment_allocation_id': 1, 'ledger_adjustment_applied_id': 1, }, { 'payment_allocation_ledger_adjustment_id': 2, 'payment_allocation_id': 1, 'ledger_adjustment_applied_id': 2, }, ], } payload = { 'contract_id': 1, 'statement_period_ids': [11] } insert_mock_data(payments_mock) res = fixture_client.post( f'/account-statements/dataloader/account/{account_id}/payment-allocations', json=payload) assert res.status_code == 200 assert res.json() == [ { 'reference_adjustment_type_id': 1, 'apply_to_statement_period_id': 11, 'reference_adjustment_type': 'Fake Flowthrough Adjustment', 'adjustment_amount': 361.30, 'currency_code': 'GBP', }, { 'reference_adjustment_type_id': 2, 'apply_to_statement_period_id': 11, 'reference_adjustment_type': 'Vat', 'adjustment_amount': 36.13, 'currency_code': 'GBP', }, ] def test_load_flowthrough_balances(fixture_client): """Test loading flowthrough balances.""" account_id = 24601 contract_id = 10001 insert_mock_data({ 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 123}, {'abacus_event_id': 2, 'statement_period_id': 123}, {'abacus_event_id': 3, 'statement_period_id': 124}, {'abacus_event_id': 4, 'statement_period_id': 125}, ], 'ledger_contract_flowthrough': [ { 'abacus_event_id': 1, 'account_id': account_id, 'contract_id': contract_id, 'currency_code': 'GBP', 'current_balance': 100.00, }, { 'abacus_event_id': 2, 'account_id': account_id, 'contract_id': contract_id, 'currency_code': 'GBP', 'current_balance': 200.00, }, { 'abacus_event_id': 3, 'account_id': account_id, 'contract_id': contract_id, 'currency_code': 'GBP', 'current_balance': 300.00, }, { 'abacus_event_id': 4, 'account_id': account_id, 'contract_id': contract_id, 'currency_code': 'GBP', 'current_balance': 400.00, }, ], }) result = fixture_client.post( f'/account-statements/dataloader/account/{account_id}/flowthrough-balances', json={ 'contract_id': contract_id, 'statement_period_ids': [123, 124], }, ) assert result.status_code == 200 assert result.json() == [ { 'amount': 200.00, 'currency_code': 'GBP', 'statement_period_id': 123, }, { 'amount': 300.00, 'currency_code': 'GBP', 'statement_period_id': 124, }, ] def test_load_advances_by_account_and_statement_periods(fixture_client): """Test loading advances by account and statement period.""" account_id = 24601 statement_period_id = 123 contract_id = 10001 mock_data = { 'combined_advances_dbt': { 'account_id': account_id, 'contract_id': contract_id, 'statement_period_id': statement_period_id, 'advance_amount': 123.45, 'advance_currency_code': 'NOK', 'advance_amount_payee_currency': 123.45, 'advance_payee_currency_code': 'NOK', 'advance_description': 'Something', 'unique_key': '1234-abcd', } } payload = { 'contract_id': contract_id, 'statement_period_ids': [statement_period_id] } with _using_mock_data(mock_data): result = fixture_client.post( f'account-statements/dataloader/account/{account_id}/applied-advances', json=payload ) assert result.status_code == 200 assert result.json() == [ { 'advance_id': '1234-abcd', 'account_id': account_id, 'contract_id': contract_id, 'statement_period_id': statement_period_id, 'advance_amount': 123.45, 'advance_currency_code': 'NOK', 'advance_amount_payee_currency': 123.45, 'advance_payee_currency_code': 'NOK', 'advance_description': 'Something', } ] def test_load_expenses_by_account_and_statement_periods_ff_enabled( fixture_client ): """Test loading expenses by account and statement period.""" account_id = 999 statement_period_id = 123 contract_id = 1001 mock_data = { 'combined_expenses_dbt': [ { 'worksheet_adjustment_detail_id': 1, 'account_id': 999, 'contract_id': 1001, 'activity_statement_period_id': 123, 'apply_to_statement_period_id': 123, 'distribution_type': 'digital', 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'note': 'mock adjustment', 'upc': '87654321', 'artist_id': 12341231, 'artist_name': 'Pharoah Sanders', 'subaccount_id': 7116, 'subaccount_name': 'Subaccount Name', 'adjustment_amount_payee_currency': 800.00, 'adjustment_payee_currency_code': 'GBP', }, { 'worksheet_adjustment_detail_id': 2, 'account_id': 999, 'contract_id': 1001, 'activity_statement_period_id': 124, 'apply_to_statement_period_id': 124, 'distribution_type': 'digital', 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', 'note': 'mock adjustment', 'upc': '12341234', 'artist_id': None, 'artist_name': None, 'subaccount_id': 7111, 'subaccount_name': 'Another Subaccount Name', 'adjustment_amount_payee_currency': 800.00, 'adjustment_payee_currency_code': 'GBP', }, ] } payload = { 'contract_id': contract_id, 'statement_period_ids': [statement_period_id] } with _using_mock_data(mock_data): result = fixture_client.post( f'account-statements/dataloader/account/{account_id}/expenses', json=payload ) assert result.status_code == 200 assert result.json() == [ { 'amount': 800.00, 'breakdown_items': [ { 'adjustment_payee_currency_code': 'GBP', 'adjustment_total_payee_currency': 800.00, 'reference_adjustment_type_id': 1, 'reference_adjustment_type_name': 'Label Earnings', }, ], 'currency_code': 'GBP', 'statement_period_id': 123 } ]