"""Functional tests for statement attachment invoices.""" from datetime import datetime from unittest.mock import patch from moneyhub.constants.constants import NumberFormat from moneyhub.constants.constants import StatementAttachmentFileType from moneyhub.constants.constants import StatementAttachmentStatus from moneyhub.constants.constants import StatementAttachmentType from moneyhub.constants.constants import SYSTEM_TIMEZONE from moneyhub.constants.constants import VatCategory from moneyhub.models import StatementAttachment from tests.functional.conftest import insert_mock_data def test_get_invoice_statement_attachments(fixture_client): """Test getting invoice attachments.""" payment_entity_id = 1 statement_period_id = 123 insert_mock_data({ 'account': {'account_id': 24601, 'account_name': 'Test'}, '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': payment_entity_id }, 'statement_attachment': { 'account_id': 24601, 'subaccount_id': None, 'contract_id': 1001, 'file_type': StatementAttachmentFileType.PDF, 'number_format': NumberFormat.US, 'statement_period_id': statement_period_id, 'statement_attachment_status': StatementAttachmentStatus.COMPLETE, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE, 'created_at': '2010-09-08 07:06:05', 'created_by': 'me', }, }) result = fixture_client.get( f'/invoices/statement-period/{statement_period_id}/?payment_entity_id={payment_entity_id}') result_json = result.json() del result_json[0]['statement_attachment_id'] # avoid autoincrement assert result.status_code == 200 assert result_json == [ { 'account_id': 24601, 'subaccount_id': None, 'account_name': 'Test', '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.COMPLETE.value, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'statement_period_id': statement_period_id, 'statement_period_ids': None, } ] @patch('moneyhub.logic.statement_attachment_invoice.sqs') def test_create_invoice_statement_attachments(mock_sqs, fixture_client): """Test creating invoice attachments.""" payment_entity_id = 1 statement_period_id = 123 activity_statement_period_id = 122 expected_invoice_number = '1234_{}_0000000001'.format( (datetime.now(SYSTEM_TIMEZONE)).year) 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': activity_statement_period_id}, {'statement_period_id': statement_period_id}, ], 'account_payment_term': { 'account_id': 24601, 'payment_entity_id': 1 }, 'account_contract': { 'account_id': 24601, 'contract_id': 1001 }, 'abacus_event': {'abacus_event_id': 1}, 'ledger_vat_summary': { 'statement_period_id': statement_period_id, 'activity_statement_period_id': activity_statement_period_id, 'abacus_event_id': 1, 'account_id': 24601, 'contract_id': 1001, 'vat_category': VatCategory.DISTRIBUTION_FEE, 'payee_currency_code': 'GBP', 'vat_currency_code': 'GBP', 'base_amount_payee_currency': 100.0, 'vat_rate': 20.0, 'vat_amount_payee_currency': 20.0, 'vat_amount_vat_currency': 20.0, 'net_amount_payee_currency': 80.0, }, }) result = fixture_client.post( f'/invoices/payment-entity/{payment_entity_id}/statement-period/{activity_statement_period_id}') # noqa: E501 assert result.status_code == 201 data = result.json() for item in data: del item['created_at'] # avoid having to match exact date del item['statement_attachment_id'] # avoid autoincrement assert data == [ { 'account_id': 24601, 'subaccount_id': None, 'contract_id': 1001, 'created_by': 'me', 'file_type': StatementAttachmentFileType.PDF, 'number_format': NumberFormat.US, 'failure_reason': None, 'file_location': None, 'displayed_file_name': None, 'invoice_number': expected_invoice_number, 'statement_attachment_status': StatementAttachmentStatus.IN_PROGRESS, 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'statement_period_id': activity_statement_period_id, 'statement_period_ids': None, }, ] @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, 'number_format': NumberFormat.US, '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 result_json = result.json() del result_json[0]['statement_attachment_id'] assert result.status_code == 201 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, 'number_format': NumberFormat.US, '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, } ] def test_get_latest_statement_attachments_invoices(): """Test to get latest statement attachment invoices.""" insert_mock_data({ 'account': [ {'account_id': 1}, {'account_id': 2}, {'account_id': 3}, {'account_id': 4}, ], 'statement_period': {'statement_period_id': 1, 'statement_period_status': 'closed'}, 'statement_attachment': [ { 'account_id': 1, 'subaccount_id': None, 'statement_period_id': 1, 'invoice_number': '4921_2021_0000000001', 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'created_at': datetime.now() }, { 'account_id': 2, 'subaccount_id': None, 'statement_period_id': 1, 'invoice_number': '4921_2021_0000000002', 'statement_attachment_type': StatementAttachmentType.DISTRIBUTION_FEE_INVOICE.value, 'created_at': datetime.now() }, { 'account_id': 3, 'subaccount_id': None, 'statement_period_id': 1, 'invoice_number': '1978_2021_0000000001', 'statement_attachment_type': StatementAttachmentType.SELF_BILLING_INVOICE, 'created_at': datetime.now() }, { 'account_id': 4, 'subaccount_id': None, 'statement_period_id': 1, 'invoice_number': '1978_2021_0000000002', 'statement_attachment_type': StatementAttachmentType.SELF_BILLING_INVOICE, 'created_at': datetime.now() } ] }) current_year = datetime.now().year statement_attachments = \ StatementAttachment.get_latest_statement_attachments_invoices(current_year) assert len(statement_attachments) == 2 assert statement_attachments[0].invoice_number == '4921_2021_0000000002' assert statement_attachments[1].invoice_number == '1978_2021_0000000002'