"""Functional tests for statement attachment.""" from contextlib import contextmanager from datetime import date from datetime import datetime from unittest.mock import patch import httpx from moto import mock_aws from moneyhub.connectors.mysql import db from moneyhub.constants.constants import ContractType from moneyhub.constants.constants import KNR_SAP_IDS from moneyhub.constants.constants import NumberFormat from moneyhub.constants.constants import RevenueVariant from moneyhub.constants.constants import StatementAttachmentFileType from moneyhub.constants.constants import StatementAttachmentStatus from moneyhub.constants.constants import StatementAttachmentType from moneyhub.constants.constants import StatementPeriodStatus from moneyhub.constants.error import FORBIDDEN_INVOICE_URL_ACCESS from moneyhub.models import StatementAttachment from moneyhub.models.account_statement_periods import AccountStatementPeriods from tests.functional.conftest import insert_mock_account from tests.functional.conftest import insert_mock_contracts from tests.functional.conftest import insert_mock_data from tests.functional.conftest import insert_mock_reference_payment_entity from tests.functional.conftest import insert_mock_reference_sap_profit_center from tests.functional.conftest import insert_mock_reference_signing_entity from tests.functional.conftest import insert_mock_statement_attachment from tests.functional.conftest import insert_mock_statement_period @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. """ AccountStatementPeriods.__table__.drop(db.engine, checkfirst=True) db.session.commit() AccountStatementPeriods.__table__.create(db.engine) insert_mock_data(mock_data, database='snowflake') yield db.session.commit() AccountStatementPeriods.__table__.drop(db.engine) db.session.close() def test_delete_statement_attachment(fixture_client): """Test deleting a statement attachment.""" statement_attachment_id = 123 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, }, 'account': {'account_id': 24601}, 'contract': { 'contract_id': 10001, 'reference_signing_entity_id': 1, }, 'statement_period': {'statement_period_id': 123}, 'statement_attachment': { 'statement_attachment_id': statement_attachment_id, 'subaccount_id': None, 'account_id': 24601, 'contract_id': 10001, 'statement_period_id': 123, } }) endpoint_url = f'/statement-attachment/{statement_attachment_id}' result = fixture_client.delete(endpoint_url) assert result.status_code == 204 def test_get_statement_attachment(fixture_client, ows_client_mock): """Test getting a statement attachments by ID.""" account_id = 24601 contract_id = 10001 statement_period_id = 123 statement_attachment_id = 1234 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, }, 'account': {'account_id': account_id}, 'contract': { 'contract_id': contract_id, 'reference_signing_entity_id': 1, }, 'statement_period': {'statement_period_id': statement_period_id}, 'statement_attachment': { 'statement_attachment_id': statement_attachment_id, 'subaccount_id': None, 'account_id': account_id, 'contract_id': contract_id, 'failure_reason': None, 'file_location': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': None, 'number_format': NumberFormat.US, 'statement_period_id': statement_period_id, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, } }) ows_client_mock.get( 'ows-permissions', '/admin/profile-type/MoneyhubProfile/profile/1111/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Vendor', 'vendorId': account_id}], 'total_count': 1}, ) ) endpoint_url = f'/statement-attachment/{statement_attachment_id}' result = fixture_client.get(endpoint_url) assert result.status_code == 200 assert result.json() == { 'statement_attachment_id': statement_attachment_id, 'subaccount_id': None, 'account_id': account_id, 'contract_id': contract_id, 'failure_reason': None, 'displayed_file_name': None, 'file_location': None, 'file_type': StatementAttachmentFileType.PDF, 'invoice_number': None, 'number_format': NumberFormat.US, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, } def test_get_statement_attachment_error(fixture_client): """Test getting a statement attachments by ID when it doesn't exist.""" statement_attachment_id = 1234 endpoint_url = f'/statement-attachment/{statement_attachment_id}' result = fixture_client.get(endpoint_url) assert result.status_code == 404 def test_get_statement_attachments(fixture_client): """Test to Get statement attachments by an account_id and statement_period_id.""" account_id = 2 contract_id = 2 statement_period_id = 2 invoice_number = 'f23989245' insert_mock_account(account_id) insert_mock_reference_payment_entity() insert_mock_reference_sap_profit_center() insert_mock_reference_signing_entity() insert_mock_contracts() insert_mock_statement_period(statement_period_id) insert_mock_statement_attachment( account_id=account_id, contract_id=contract_id, statement_period_id=statement_period_id, invoice_number=invoice_number) endpoint_url = f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}' # noqa: E501 result = fixture_client.get(endpoint_url) result_json = result.json() del result_json[0]['statement_attachment_id'] # avoid autoincrement assert result.status_code == 200 assert result_json == [{ 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'invoice_number': invoice_number, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'failure_reason': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US, 'account_id': account_id, 'subaccount_id': None, 'contract_id': contract_id, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, }] def test_get_statement_attachments_subaccount(fixture_client): """Test to get subaccount statement attachments by an account_id and statement_period_id.""" account_id = 2 contract_id = 2 subaccount_id = 2 statement_period_id = 2 invoice_number = 'f23989245' insert_mock_account(account_id) insert_mock_reference_payment_entity() insert_mock_reference_sap_profit_center() insert_mock_reference_signing_entity() insert_mock_contracts() insert_mock_statement_period(statement_period_id) insert_mock_statement_attachment( account_id=account_id, contract_id=contract_id, statement_period_id=statement_period_id, invoice_number=invoice_number, subaccount_id=subaccount_id) endpoint_url = f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}?subaccount_id={subaccount_id}' # noqa: E501 result = fixture_client.get(endpoint_url) result_json = result.json() del result_json[0]['statement_attachment_id'] # avoid autoincrement assert result.status_code == 200 assert result_json == [{ 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'invoice_number': invoice_number, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'failure_reason': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US, 'account_id': account_id, 'subaccount_id': subaccount_id, 'contract_id': contract_id, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'filters': None, }] def test_create_statement_attachments(fixture_client, ows_client_mock): """Test to Create statement attachments for a specified statement_period_id.""" insert_mock_data({ 'account': {'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': '4321', 'legal_name': 'Company', 'vat_number': '12341234', 'company_registration_number': '12341234', 'address': None, }, 'contract': [ { 'contract_id': 1, 'reference_signing_entity_id': 1, }, { 'contract_id': 2, 'reference_signing_entity_id': 1, }, { 'contract_id': 3, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': 1, 'contract_id': 1}, {'account_id': 1, 'contract_id': 2}, {'account_id': 1, 'contract_id': 3}, ], 'statement_period': { 'statement_period_id': 1, 'statement_period_status': StatementPeriodStatus.CLOSED }, 'run_controller': {'run_controller_id': 555}, 'accounting_period': {'accounting_period_id': 1, 'statement_period_id': 1}, 'accounting_run': { 'accounting_run_id': 1, 'accounting_period_id': 1, 'run_controller_id': 555, 'run_status': 'Committed' }, 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 1}, {'abacus_event_id': 2, 'statement_period_id': 1}, {'abacus_event_id': 3, 'statement_period_id': 1}, ], 'ledger_accounting_run_vat': [ { 'ledger_accounting_run_vat_id': 1, 'accounting_run_id': 1, 'contract_id': 1, 'abacus_event_id': 1, 'distribution_fee': 123, 'gross_vat_rate': 10, }, { 'ledger_accounting_run_vat_id': 2, 'accounting_run_id': 1, 'contract_id': 2, 'abacus_event_id': 2, 'distribution_fee': 123, 'gross_vat_rate': 10, }, { 'ledger_accounting_run_vat_id': 3, 'accounting_run_id': 1, 'contract_id': 3, 'abacus_event_id': 3, 'distribution_fee': 123, 'gross_vat_rate': 10, }, ], 'ledger_account_contract': [ {'account_id': 1, 'contract_id': 1, 'abacus_event_id': 1}, {'account_id': 1, 'contract_id': 2, 'abacus_event_id': 2}, {'account_id': 1, 'contract_id': 3, 'abacus_event_id': 3}, ], 'account_payment_term': [ { 'account_payment_term_id': 1, 'account_id': 1, 'payment_entity_id': 1 }, ], 'statement_period_payment_entity': [ { 'statement_period_payment_entity_id': 1, 'statement_period_id': 1, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1 }, ] }) endpoint_url = '/statement-attachment/statement-period/1' result = fixture_client.post(endpoint_url) current_year = date.today().year result_json = result.json() for item in result_json: del item['created_at'] # avoid having to match exact date del item['statement_attachment_id'] # avoid autoincrement assert result.status_code == 201 assert result_json == [ { 'account_id': 1, 'subaccount_id': None, 'contract_id': 1, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'invoice_number': f'4321_{current_year}_0000000001', 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_by': 'me', 'filters': None }, # TEMPORARILY DISABLED (SEE WAR-503) # { # 'account_id': 1, # 'contract_id': 1, # 'file_location': None, # 'invoice_number': f'4321_{current_year}_SB00000001', # 'statement_attachment_id': 2, # 'statement_attachment_status': 'in_progress', # 'statement_attachment_type': 'self_billing_invoice', # 'statement_period_id': 1, # 'created_by': 'default_user_id', # }, { 'account_id': 1, 'subaccount_id': None, 'contract_id': 2, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'invoice_number': f'4321_{current_year}_0000000002', 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_by': 'me', 'filters': None, }, # TEMPORARILY DISABLED (SEE WAR-503) # { # 'account_id': 1, # 'contract_id': 2, # 'file_location': None, # 'invoice_number': f'4321_{current_year}_SB00000002', # 'statement_attachment_id': 4, # 'statement_attachment_status': 'in_progress', # 'statement_attachment_type': 'self_billing_invoice', # 'statement_period_id': 1, # 'created_by': 'default_user_id', # }, { 'account_id': 1, 'subaccount_id': None, 'contract_id': 3, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'invoice_number': f'4321_{current_year}_0000000003', 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_by': 'me', 'filters': None }, # TEMPORARILY DISABLED (SEE WAR-503) # { # 'account_id': 1, # 'contract_id': 3, # 'file_location': None, # 'invoice_number': f'4321_{current_year}_SB00000003', # 'statement_attachment_id': 6, # 'statement_attachment_status': 'in_progress', # 'statement_attachment_type': 'self_billing_invoice', # 'statement_period_id': 1, # 'created_by': 'default_user_id', # }, ] def test_create_statement_attachments_vat_summary(fixture_client, ows_client_mock): """Test creating statement attachments for a period based off VAT summary entries.""" insert_mock_data({ 'account': {'account_id': 24601}, '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': 10001, 'reference_signing_entity_id': 1, }, ], 'account_contract': {'account_id': 24601, 'contract_id': 10001}, 'statement_period': [ { 'statement_period_id': 1, 'statement_period_status': StatementPeriodStatus.CLOSED }, { 'statement_period_id': 2, 'statement_period_status': StatementPeriodStatus.CLOSED }, ], 'abacus_event': [ {'abacus_event_id': 1, 'statement_period_id': 1}, {'abacus_event_id': 2, 'statement_period_id': 2}, ], 'ledger_vat_summary': [ { 'ledger_vat_summary_id': 1, 'statement_period_id': 1, 'activity_statement_period_id': 1, 'abacus_event_id': 1, 'account_id': 24601, 'contract_id': 10001, 'vat_category': 'distribution_fee', 'payee_currency_code': 'GBP', 'vat_currency_code': 'GBP', 'base_amount_payee_currency': 12.34, 'vat_rate': 20.0, 'vat_amount_payee_currency': 2.46, 'vat_amount_vat_currency': 2.46, 'net_amount_payee_currency': 9.88, 'is_reporting_only': False, 'created_by': 'me', 'created_at': '2010-09-08T07:06:05', }, { 'ledger_vat_summary_id': 2, 'statement_period_id': 2, 'activity_statement_period_id': 1, 'abacus_event_id': 2, 'account_id': 24601, 'contract_id': 10001, 'vat_category': 'gross_revenue', 'payee_currency_code': 'GBP', 'vat_currency_code': 'GBP', 'base_amount_payee_currency': 12.34, 'vat_rate': 20.0, 'vat_amount_payee_currency': 2.46, 'vat_amount_vat_currency': 2.46, 'net_amount_payee_currency': 9.88, 'is_reporting_only': False, 'created_by': 'me', 'created_at': '2010-09-08T07:06:05', }, ], 'ledger_account_contract': [ {'account_id': 24601, 'contract_id': 10001, 'abacus_event_id': 1}, ], 'account_payment_term': [ { 'account_payment_term_id': 1, 'account_id': 24601, 'payment_entity_id': 1 }, ], 'statement_period_payment_entity': [ { 'statement_period_payment_entity_id': 1, 'statement_period_id': 1, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1 }, ] }) endpoint_url = '/statement-attachment/statement-period/1' result = fixture_client.post(endpoint_url) assert result.status_code == 201 current_year = date.today().year result_json = result.json() for item in result_json: del item['created_at'] # avoid having to match exact date del item['statement_attachment_id'] # avoid autoincrement assert result.status_code == 201 assert result_json == [ { 'account_id': 24601, 'subaccount_id': None, 'contract_id': 10001, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'invoice_number': f'1234_{current_year}_0000000001', 'number_format': 'us', 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'distribution_fee_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_by': 'me', 'filters': None, }, { 'account_id': 24601, 'subaccount_id': None, 'contract_id': 10001, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'invoice_number': f'1234_{current_year}_SB00000001', 'number_format': 'us', 'statement_attachment_status': 'in_progress', 'statement_attachment_type': 'self_billing_invoice', 'failure_reason': None, 'statement_period_id': 1, 'statement_period_ids': None, 'created_by': 'me', 'filters': None, }, ] @patch('moneyhub.logic.statement_attachment.sqs') def test_regenerate_statement_attachments(mock_sqs, fixture_client): """Test to regenerate attachments for a specified statement period.""" account_id = 24601 statement_period_id = 123 insert_mock_data({ 'account': [ {'account_id': account_id}, {'account_id': 99999}, ], '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': 10001, 'reference_signing_entity_id': 1, }, 'statement_period': {'statement_period_id': 123, 'statement_period_status': 'closed'}, 'statement_attachment': [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 10001, 'statement_attachment_status': StatementAttachmentStatus.COMPLETE, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'statement_period_id': statement_period_id, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', }, { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 10001, 'statement_attachment_status': StatementAttachmentStatus.ERROR, 'statement_attachment_type': StatementAttachmentType.SELF_BILLING_INVOICE, 'statement_period_id': statement_period_id, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', }, { 'account_id': 99999, 'subaccount_id': None, 'contract_id': 10001, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.REVENUE_DETAIL.value, 'statement_period_id': statement_period_id, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', }, ] }) endpoint_url = f'/statement-attachment/statement-period/{statement_period_id}/regenerate' result = fixture_client.post(endpoint_url, json={'account_id': account_id}) assert result.status_code == 200 result_json = result.json() for item in result_json: del item['statement_attachment_id'] # avoid autoincrement assert result_json == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 10001, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', }, { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 10001, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.SELF_BILLING_INVOICE, 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', }, ] def test_update_statement_attachment(fixture_client): """Test to update statement attachment details by statement_attachment_id.""" account_id = 11 contract_id = 2 statement_period_id = 11 invoice_number = '11_11_01' insert_mock_account(account_id) insert_mock_reference_payment_entity() insert_mock_reference_sap_profit_center() insert_mock_reference_signing_entity() insert_mock_contracts() insert_mock_statement_period(statement_period_id) StatementAttachment.__table__.drop(db.engine) StatementAttachment.__table__.create(db.engine) insert_mock_statement_attachment( account_id=account_id, contract_id=contract_id, invoice_number=invoice_number, statement_period_id=statement_period_id ) put_body = {'statement_attachment_status': StatementAttachmentStatus.COMPLETE} endpoint_url = '/statement-attachment/1' result = fixture_client.put(endpoint_url, json=put_body) assert result.status_code == 200 result_json = result.json() del result_json['statement_attachment_id'] # avoid autoincrement assert result_json == { 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'statement_attachment_status': StatementAttachmentStatus.COMPLETE, 'invoice_number': invoice_number, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'failure_reason': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'account_id': account_id, 'subaccount_id': None, 'contract_id': contract_id, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', } @mock_aws def test_get_invoice_presigned_url(fixture_client, ows_client_mock): """Test to get invoice presigned url to download.""" account_id = 12 contract_id = 3 profile_id = '98787' profile_type = 'MoneyhubProfile' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Vendor', 'vendorId': account_id}], 'total_count': 1}, ) ) statement_period_id = 12 url = 'https://invoice-files.s3.amazonaws.com/invoice_1.pdf' insert_mock_account(account_id) insert_mock_reference_payment_entity() insert_mock_reference_sap_profit_center() insert_mock_reference_signing_entity() insert_mock_contracts() insert_mock_statement_period(statement_period_id) StatementAttachment.__table__.drop(db.engine) StatementAttachment.__table__.create(db.engine) insert_mock_statement_attachment( account_id=account_id, contract_id=contract_id, invoice_number='12_12s_01', statement_period_id=statement_period_id, file_location=url ) endpoint_url = '/statement-attachment/1/download' result = fixture_client.get( endpoint_url, headers={'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type} ) assert result.status_code == 200 assert result.json()['url'].startswith('https://invoice-files.s3.amazonaws.com/') @mock_aws def test_get_invoice_presigned_url_subaccount(fixture_client, ows_client_mock): """Test to get invoice presigned URL for subaccount access.""" account_id = 12 subaccount_id = 54321 contract_id = 3 profile_id = '98787' profile_type = 'MoneyhubProfile' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Subaccount', 'id': subaccount_id}], 'total_count': 1}, ) ) statement_period_id = 12 url = 'https://invoice-files.s3.amazonaws.com/invoice_1.pdf' insert_mock_account(account_id) insert_mock_reference_payment_entity() insert_mock_reference_sap_profit_center() insert_mock_reference_signing_entity() insert_mock_contracts() insert_mock_statement_period(statement_period_id) StatementAttachment.__table__.drop(db.engine) StatementAttachment.__table__.create(db.engine) insert_mock_statement_attachment( account_id=account_id, subaccount_id=subaccount_id, contract_id=contract_id, invoice_number='12_12s_01', statement_period_id=statement_period_id, file_location=url ) endpoint_url = '/statement-attachment/1/download' result = fixture_client.get( endpoint_url, headers={'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type} ) assert result.status_code == 200 assert result.json()['url'].startswith('https://invoice-files.s3.amazonaws.com/') def test_get_invoice_presigned_url_forbidden_access(fixture_client, ows_client_mock): """Test to get invoice presigned url failed. When profile doesn't have access to invoice url. """ account_id = 13 contract_id = 4 profile_id = '98787' profile_type = 'MoneyhubProfile' ows_client_mock.get( 'ows-permissions', f'/admin/profile-type/{profile_type}/profile/{profile_id}/resource/all', ).mock( return_value=httpx.Response( 200, json={'items': [{'type': 'Vendor', 'vendorId': 1}], 'total_count': 1}, ) ) statement_period_id = 13 url = 'https://invoice-files.s3.amazonaws.com/invoice_1.pdf' insert_mock_account(account_id) insert_mock_reference_payment_entity() insert_mock_reference_sap_profit_center() insert_mock_reference_signing_entity() insert_mock_contracts() insert_mock_statement_period(statement_period_id) StatementAttachment.__table__.drop(db.engine) StatementAttachment.__table__.create(db.engine) insert_mock_statement_attachment( account_id=account_id, contract_id=contract_id, invoice_number='13_13s_01', statement_period_id=statement_period_id, file_location=url ) endpoint_url = '/statement-attachment/1/download' result = fixture_client.get( endpoint_url, headers={'orchard-profile-id': profile_id, 'orchard-profile-type': profile_type} ) assert result.status_code == 403 assert result.json() == {'detail': FORBIDDEN_INVOICE_URL_ACCESS} @patch('moneyhub.logic.statement_attachment.sqs') def test_create_revenue_detail_reports( mock_sqs, fixture_client, ows_client_mock): """Test creating revenue detail reports.""" account_id = 24601 contract_id = 1001 statement_period_id = 123 ows_client_mock.get( 'ows-royalties', f'/contracts/account/{account_id}', ).mock( return_value=httpx.Response( 200, json={ 'items': [ {'contract_id': contract_id, 'contract_type': ContractType.NEIGHBOURING_RIGHTS}, ], 'total_count': 1 }, ) ) 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, }, 'statement_period': { 'statement_period_id': statement_period_id, 'statement_period_status': StatementPeriodStatus.CLOSED, }, 'account_payment_term': {'account_id': account_id, 'payment_entity_id': 1}, 'statement_period_payment_entity': { 'statement_period_id': statement_period_id, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1, }, }) with _using_mock_data({ 'account_statement_periods_dbt': { 'account_id': account_id, 'contract_id': 1111, 'currency_code': 'USD', 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' } }): result = fixture_client.post( f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}/revenue-detail') # noqa: E501 assert result.status_code == 200 data = result.json() del data[0]['created_at'] del data[0]['statement_attachment_id'] assert data == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': contract_id, 'created_by': 'me', 'failure_reason': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.CSV.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.NEIGHBOURING_RIGHTS_PERFORMER_REVENUE.value, 'statement_period_id': statement_period_id, 'statement_period_ids': None, } ] @patch('moneyhub.logic.statement_attachment.sqs') def test_create_revenue_detail_reports_subaccount( mock_sqs, fixture_client, ows_client_mock): """Test creating revenue detail reports for a subaccount.""" account_id = 24601 subaccount_id = 12 contract_id = 1001 statement_period_id = 123 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, }, 'statement_period': { 'statement_period_id': statement_period_id, 'statement_period_status': StatementPeriodStatus.CLOSED, }, 'account_payment_term': {'account_id': account_id, 'payment_entity_id': 1}, 'statement_period_payment_entity': { 'statement_period_id': statement_period_id, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1, }, }) with _using_mock_data({ 'account_statement_periods_dbt': { 'account_id': account_id, 'contract_id': 1111, 'currency_code': 'USD', 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' } }): result = fixture_client.post( f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}/revenue-detail?subaccount_id={subaccount_id}') # noqa: E501 assert result.status_code == 200 data = result.json() del data[0]['created_at'] del data[0]['statement_attachment_id'] assert data == [ { 'account_id': account_id, 'subaccount_id': subaccount_id, 'contract_id': None, 'created_by': 'me', 'failure_reason': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.CSV.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.REVENUE_DETAIL.value, 'statement_period_id': statement_period_id, 'statement_period_ids': None, } ] @patch('moneyhub.logic.statement_attachment.sqs') def test_generate_statement_attachments(mock_sqs, fixture_client): """Test generating statement attachments.""" statement_period_id = 123 insert_mock_data({ 'account': [ {'account_id': 24601}, {'account_id': 12345}, ], '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, }, 'statement_period': { 'statement_period_id': statement_period_id, 'statement_period_status': StatementPeriodStatus.CLOSED, }, 'statement_attachment': [ { 'account_id': 24601, 'subaccount_id': None, 'contract_id': 1001, 'file_type': StatementAttachmentFileType.PDF.value, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'statement_period_id': statement_period_id, }, { 'account_id': 12345, 'subaccount_id': None, 'contract_id': 1001, 'file_type': StatementAttachmentFileType.PDF.value, 'statement_attachment_status': StatementAttachmentStatus.ERROR, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'statement_period_id': statement_period_id, }, ], }) result = fixture_client.post( f'/statement-attachment/statement-period/{statement_period_id}/generate') assert result.status_code == 201 assert result.json() == { 'status': 'OK', 'messages_sent': 2, } @patch('moneyhub.logic.statement_attachment_invoice.sqs') def test_retry_invoice_statement_attachments( mock_sqs, fixture_client): """Test retrying invoice attachments.""" payment_entity_id = 1 statement_period_id = 123 insert_mock_data({ 'account': {'account_id': 24601}, '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, }, 'statement_period': {'statement_period_id': statement_period_id}, 'account_payment_term': { 'account_id': 24601, 'payment_entity_id': 1 }, 'statement_attachment': { 'account_id': 24601, 'subaccount_id': None, 'contract_id': 1001, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'statement_period_id': statement_period_id, 'statement_attachment_status': StatementAttachmentStatus.ERROR, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'created_at': '2010-09-08 07:06:05', 'created_by': 'me', }, }) result = fixture_client.post( f'/invoices/payment-entity/{payment_entity_id}/statement-period/{statement_period_id}/retry') # noqa: E501 assert result.status_code == 201 result_json = result.json() for item in result_json: del item['statement_attachment_id'] # avoid autoincrement assert result_json == [ { 'account_id': 24601, 'subaccount_id': None, 'contract_id': 1001, 'created_at': '2010-09-08T07:06:05', 'created_by': 'me', 'failure_reason': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.RETRY.value, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'statement_period_id': statement_period_id, 'statement_period_ids': None, } ] @patch('moneyhub.logic.statement_attachment.sqs') def test_create_collection_summary_documents( mock_sqs, fixture_client, ows_client_mock): """Test creating statement period collection summary.""" account_id = 24601 statement_period_id = 123 mock_contracts = [ {'contract_id': 10001, 'contract_type': ContractType.NEIGHBOURING_RIGHTS}, {'contract_id': 10002, 'contract_type': ContractType.NEIGHBOURING_RIGHTS}, ] insert_mock_data({ 'account': {'account_id': 24601}, 'statement_period': {'statement_period_id': 123, 'statement_period_status': 'closed'}, 'reference_payment_entity': {'reference_payment_entity_id': 1}, 'reference_sap_profit_center': [ { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK5', 'company_code': KNR_SAP_IDS[1] }, { 'reference_sap_profit_center_id': 2, 'profit_center': 'UK6', 'company_code': KNR_SAP_IDS[0]}, ], 'reference_signing_entity': [ { 'reference_signing_entity_id': 1, 'company_code': KNR_SAP_IDS[1], 'tax_entity_company_code': KNR_SAP_IDS[1], 'reference_payment_entity_id': 1, 'reference_sap_profit_center_id': 1, }, { 'reference_signing_entity_id': 2, 'company_code': KNR_SAP_IDS[0], 'tax_entity_company_code': KNR_SAP_IDS[0], 'reference_payment_entity_id': 1, 'reference_sap_profit_center_id': 2, }, ], 'contract': [ { 'contract_id': 10001, 'reference_signing_entity_id': 1, }, { 'contract_id': 10002, 'reference_signing_entity_id': 2, }, ] }) ows_client_mock.get( 'ows-royalties', f'/contracts/account/{account_id}', ).mock( return_value=httpx.Response( 200, json={'items': mock_contracts, 'total_count': 1}, ) ) endpoint_url = f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}/collection-summary' # noqa: E501 result = fixture_client.post(endpoint_url) result_body = result.json() for item in result_body: item['created_at'] = 'now' del item['statement_attachment_id'] assert result.status_code == 201 assert result_body == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 10001, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.COLLECTION_SUMMARY_PERFORMER.value, 'number_format': NumberFormat.US.value, 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': 'now', 'created_by': 'me', }, { 'account_id': account_id, 'subaccount_id': None, 'contract_id': 10002, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.COLLECTION_SUMMARY_PERFORMER.value, 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': 'now', 'created_by': 'me', }, ] def test_create_internal_attachment(fixture_client): """Test creating internal attachment.""" account_id = 24601 contract_id = 10001 statement_period_id = 123 insert_mock_data({ 'account': {'account_id': 24601}, 'statement_period': {'statement_period_id': 123, 'statement_period_status': 'closed'}, 'reference_payment_entity': {'reference_payment_entity_id': 1}, 'reference_sap_profit_center': [ { 'reference_sap_profit_center_id': 1, 'profit_center': 'UK5', 'company_code': KNR_SAP_IDS[1] }, { 'reference_sap_profit_center_id': 2, 'profit_center': 'UK6', 'company_code': KNR_SAP_IDS[0]}, ], 'reference_signing_entity': [ { 'reference_signing_entity_id': 1, 'company_code': KNR_SAP_IDS[1], 'tax_entity_company_code': KNR_SAP_IDS[1], 'reference_payment_entity_id': 1, 'reference_sap_profit_center_id': 1, }, ], 'contract': [ { 'contract_id': contract_id, 'reference_signing_entity_id': 1, }, ] }) body = { 'file_type': 'pdf', 'file_location': 's3://fake-bucket/prod-per-label-statement-attachments/L33687' '/L33687_276_publishing_statement_December_2021.xlsx', 'upload_date': datetime(1, 1, 1).isoformat(), 'contract_id': contract_id } endpoint_url = f'/statement-attachment/account/{account_id}/statement-period/{statement_period_id}/internal-attachment' # noqa: E501 result = fixture_client.post(endpoint_url, json=body) result_body = result.json() del result_body['statement_attachment_id'] assert result.status_code == 201 assert result_body == { 'account_id': account_id, 'subaccount_id': None, 'contract_id': contract_id, 'file_location': body.get('file_location'), 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.PDF.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.COMPLETE.value, 'statement_attachment_type': StatementAttachmentType.PUBLISHING_DETAIL.value, 'number_format': NumberFormat.US.value, 'failure_reason': None, 'statement_period_id': statement_period_id, 'statement_period_ids': None, 'created_at': body.get('upload_date'), 'created_by': 'me', } @patch('moneyhub.logic.statement_attachment.sqs') def test_create_legacy_revenue_reports_without_statement_period_ids(mock_sqs, fixture_client): """Test creating legacy revenue reports.""" account_id = 1 statement_period_id = 1 report_type = StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL file_type = StatementAttachmentFileType.XLS number_format = NumberFormat.US insert_mock_data({ 'account': {'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': 1, 'reference_signing_entity_id': 1, }, { 'contract_id': 2, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': 1, 'contract_id': 1}, {'account_id': 1, 'contract_id': 2}, ], 'statement_period': { 'statement_period_id': 1, 'statement_period_status': StatementPeriodStatus.CLOSED }, 'account_payment_term': [ { 'account_payment_term_id': 1, 'account_id': 1, 'payment_entity_id': 1 }, ], 'statement_period_payment_entity': [ { 'statement_period_payment_entity_id': 1, 'statement_period_id': 1, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1 }, ] }) params = { 'file_type': file_type.value, 'number_format': number_format.value, 'report_type': report_type.value } endpoint_url = ( f'/statement-attachment/account/{account_id}/statement-period/' f'{statement_period_id}/legacy-revenue-report') with _using_mock_data({ 'account_statement_periods_dbt': { 'account_id': account_id, 'contract_id': 1111, 'currency_code': 'USD', 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' } }): result = fixture_client.post(endpoint_url, params=params) result_body = result.json() for item in result_body: item['created_at'] = 'now' del item['statement_attachment_id'] assert result.status_code == 201 assert result_body == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.XLS.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL.value, # noqa: E501 'failure_reason': None, 'filters': None, 'statement_period_id': statement_period_id, 'statement_period_ids': str(statement_period_id), 'created_at': 'now', 'created_by': 'me', }, ] @patch('moneyhub.logic.statement_attachment.sqs') def test_create_legacy_revenue_reports_subaccount(mock_sqs, fixture_client): """Test creating legacy revenue reports for a subaccount.""" account_id = 1 statement_period_id = 1 statement_period_ids = '1' subaccount_id = 1 report_type = StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL file_type = StatementAttachmentFileType.XLS number_format = NumberFormat.US insert_mock_data({ 'account': {'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': 1, 'reference_signing_entity_id': 1, }, { 'contract_id': 2, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': 1, 'contract_id': 1}, {'account_id': 1, 'contract_id': 2}, ], 'statement_period': { 'statement_period_id': 1, 'statement_period_status': StatementPeriodStatus.CLOSED }, 'account_payment_term': [ { 'account_payment_term_id': 1, 'account_id': 1, 'payment_entity_id': 1 }, ], 'statement_period_payment_entity': [ { 'statement_period_payment_entity_id': 1, 'statement_period_id': 1, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1 }, ] }) endpoint_url = ( f'/statement-attachment/account/{account_id}/statement-period/' f'{statement_period_id}/legacy-revenue-report?file_type={file_type.value}&report_type' f'={report_type.value}&number_format={number_format.value}' f'&subaccount_id={subaccount_id}&statement_period_ids={statement_period_ids}') with _using_mock_data({ 'account_statement_periods_dbt': { 'account_id': account_id, 'contract_id': 1111, 'currency_code': 'USD', 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' } }): result = fixture_client.post(endpoint_url) result_body = result.json() for item in result_body: item['created_at'] = 'now' del item['statement_attachment_id'] assert result.status_code == 201 assert result_body == [ { 'account_id': account_id, 'subaccount_id': subaccount_id, 'contract_id': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.XLS.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL.value, # noqa: E501 'failure_reason': None, 'filters': None, 'statement_period_id': statement_period_id, 'statement_period_ids': statement_period_ids, 'created_at': 'now', 'created_by': 'me', }, ] @patch('moneyhub.logic.statement_attachment.sqs') def test_create_legacy_revenue_reports_with_filters(mock_sqs, fixture_client): """Test creating legacy revenue reports with filters.""" account_id = 1 statement_period_id = 1 report_type = StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL file_type = StatementAttachmentFileType.XLS number_format = NumberFormat.US filters = { 'transaction_type_ids': [1, 2, 3], 'transaction_type_group_ids': [4, 5, 6], 'exclude_transaction_type_group_ids': None, 'exclude_transaction_type_ids': None, 'variant': RevenueVariant.PHYSICAL.value, } insert_mock_data({ 'account': {'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': 1, 'reference_signing_entity_id': 1, }, { 'contract_id': 2, 'reference_signing_entity_id': 1, }, ], 'account_contract': [ {'account_id': 1, 'contract_id': 1}, {'account_id': 1, 'contract_id': 2}, ], 'statement_period': { 'statement_period_id': 1, 'statement_period_status': StatementPeriodStatus.CLOSED }, 'account_payment_term': [ { 'account_payment_term_id': 1, 'account_id': 1, 'payment_entity_id': 1 }, ], 'statement_period_payment_entity': [ { 'statement_period_payment_entity_id': 1, 'statement_period_id': 1, 'reference_payment_entity_id': 1, 'is_visible_to_customer': 1 }, ] }) params = { 'file_type': file_type.value, 'number_format': number_format.value, 'report_type': report_type.value } endpoint_url = ( f'/statement-attachment/account/{account_id}/statement-period/' f'{statement_period_id}/legacy-revenue-report') with _using_mock_data({ 'account_statement_periods_dbt': { 'account_id': account_id, 'contract_id': 1111, 'currency_code': 'USD', 'statement_period_id': statement_period_id, 'statement_period_status': 'closed' } }): result = fixture_client.post(endpoint_url, params=params, json=filters) result_body = result.json() for item in result_body: item['created_at'] = 'now' del item['statement_attachment_id'] assert result.status_code == 201 assert result_body == [ { 'account_id': account_id, 'subaccount_id': None, 'contract_id': None, 'file_location': None, 'displayed_file_name': None, 'file_type': StatementAttachmentFileType.XLS.value, 'number_format': NumberFormat.US.value, 'invoice_number': None, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS.value, 'statement_attachment_type': StatementAttachmentType.LEGACY_REVENUE_DETAIL_PHYSICAL.value, # noqa: E501 'failure_reason': None, 'filters': filters, 'statement_period_id': statement_period_id, 'statement_period_ids': str(statement_period_id), 'created_at': 'now', 'created_by': 'me', }, ]