"""Test generating self-billing invoices.""" from datetime import datetime from decimal import Decimal from os import path from unittest.mock import patch import config from src.documents import distribution_fee_invoice from src.documents.utils import format_address @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_contract') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account') @patch('src.documents.distribution_fee_invoice.OwsPayee.get_tax_details') @patch('src.documents.distribution_fee_invoice.generate_pdf_file') @patch('src.documents.distribution_fee_invoice.OwsMoneyhub') @patch('src.documents.distribution_fee_invoice.OwsRoyalties') @patch('src.documents.distribution_fee_invoice.is_feature_enabled') def test_build_document( mock_is_feature_enabled, mock_ows_royalties, mock_ows_moneyhub, mock_generate_pdf_file, mock_get_tax_details, mock_get_account, mock_get_contract, mock_get_account_payment_term, mock_get_reference_payment_entity, contract_fixture, account_fixture, signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture_uk, reference_payment_entity_fixture, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[1] vat_categories = ['distribution_fee', 'commission'] expected_path = path.join(config.FILE_OUTPUT_PATH, '2_2_distribution_fee_invoice.pdf') mock_is_feature_enabled.return_value = False mock_get_account.return_value = account_fixture mock_get_contract.return_value = contract_fixture mock_ows_moneyhub.get_reference_signing_entity.return_value = signing_entity_fixture mock_ows_moneyhub.get_ledger_vat_summary.return_value = ledger_vat_summary_fixture mock_ows_royalties.get_statement_period.return_value = statement_period_fixture mock_get_tax_details.return_value = tax_details_fixture mock_get_account_payment_term.return_value = account_payment_term_fixture_uk mock_get_reference_payment_entity.return_value = reference_payment_entity_fixture result = distribution_fee_invoice.build_document(statement_attachment) assert result == expected_path mock_generate_pdf_file.assert_called_once() assert mock_generate_pdf_file.call_args.args[0] == expected_path assert mock_generate_pdf_file.call_args.args[1] == 'distribution_fee_invoice/body.html' mock_get_account.assert_called_once_with(statement_attachment['account_id']) mock_get_contract.assert_called_once_with(statement_attachment['contract_id']) mock_ows_moneyhub.get_reference_signing_entity.assert_called_once_with( statement_attachment['contract_id'] ) mock_get_tax_details.assert_called_once_with(account_fixture['account_payee_id'], ['address']) mock_ows_royalties.get_statement_period.assert_called_once_with( statement_attachment['statement_period_id'] ) mock_ows_moneyhub.get_ledger_vat_summary.assert_called_once_with( statement_attachment['account_id'], statement_attachment['contract_id'], statement_attachment['statement_period_id'], vat_categories, ) @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_contract') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account') @patch('src.documents.distribution_fee_invoice.OwsPayee.get_tax_details') @patch('src.documents.distribution_fee_invoice.generate_pdf_file_playwright') @patch('src.documents.distribution_fee_invoice.OwsMoneyhub') @patch('src.documents.distribution_fee_invoice.OwsRoyalties') @patch('src.documents.distribution_fee_invoice.is_feature_enabled') def test_build_document_new_pdf_library( mock_is_feature_enabled, mock_ows_royalties, mock_ows_moneyhub, mock_generate_pdf_file_playwright, mock_get_tax_details, mock_get_account, mock_get_contract, mock_get_account_payment_term, mock_get_reference_payment_entity, contract_fixture, account_fixture, signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture_uk, reference_payment_entity_fixture, ): """Test generating a document with the new PDF library.""" statement_attachment = statement_attachments_fixture[1] expected_path = path.join(config.FILE_OUTPUT_PATH, '2_2_distribution_fee_invoice.pdf') mock_is_feature_enabled.return_value = True mock_get_account.return_value = account_fixture mock_get_contract.return_value = contract_fixture mock_ows_moneyhub.get_reference_signing_entity.return_value = signing_entity_fixture mock_ows_moneyhub.get_ledger_vat_summary.return_value = ledger_vat_summary_fixture mock_ows_royalties.get_statement_period.return_value = statement_period_fixture mock_get_tax_details.return_value = tax_details_fixture mock_get_account_payment_term.return_value = account_payment_term_fixture_uk mock_get_reference_payment_entity.return_value = reference_payment_entity_fixture result = distribution_fee_invoice.build_document(statement_attachment) assert result == expected_path mock_generate_pdf_file_playwright.assert_called_once_with( expected_path, 'distribution_fee_invoice/body.html', 'distribution_fee_invoice/footer_playwright.html', { 'se_name': signing_entity_fixture['legal_name'], 'se_address': signing_entity_fixture['address'].replace('\n', ' '), 'se_vat_number': signing_entity_fixture['vat_number'], 'customer_tax_number': signing_entity_fixture['vat_number'], 'account_name': account_fixture['account_name'], 'account_id': account_fixture['account_id'], 'contract_name': contract_fixture['contract_name'], 'contract_id': contract_fixture['contract_id'], 'customer_name': tax_details_fixture['business_name'], 'customer_address': format_address(tax_details_fixture['address']), 'invoice_number': statement_attachment['invoice_number'], 'invoice_date': datetime.fromisoformat(statement_attachment['created_at']).date(), 'invoice_currency': ledger_vat_summary_fixture[0]['payee_currency_code'], 'base_amount_total': Decimal('-213012.44'), 'has_vat': True, 'has_wht': False, 'subtotal_amount': Decimal('-213012.44'), 'total_amount': Decimal('-213212.44'), 'total_wht_amount_payee_currency': Decimal('0'), 'total_wht_amount_vat_currency': Decimal('0'), 'logo_path': 'https://cdn.theorchard.io/assets/awal/icons/brand-text.png', 'payee_total_vat': Decimal('-200.00'), 'transactions': [ { 'amount': Decimal('-106506.22'), 'amount_ex_tax': Decimal('-106506.22'), 'description': 'test ledger 1', 'payee_currency_code': 'USD', 'quantity': 1, 'vat_rate': Decimal('20.00'), 'wht_rate': Decimal('0'), }, { 'amount': Decimal('-106506.22'), 'amount_ex_tax': Decimal('-106506.22'), 'description': 'test ledger 2', 'payee_currency_code': 'USD', 'quantity': 1, 'vat_rate': Decimal('20.00'), 'wht_rate': Decimal('0'), }, ], 'vat_items': [ { 'base_amount_payee_currency': Decimal('-213012.44'), 'payee_currency_code': 'USD', 'vat_amount_payee_currency': Decimal('-200.00'), 'vat_amount_vat_currency': Decimal('-200.00'), 'vat_currency_code': 'GBP', 'vat_rate': Decimal('20.00'), }, ], 'vat_total_vat': Decimal('-200.00'), 'wht_items': [ { 'base_amount_payee_currency': Decimal('-213012.44'), 'payee_currency_code': 'USD', 'vat_currency_code': 'GBP', 'wht_amount_payee_currency': Decimal('0'), 'wht_amount_vat_currency': Decimal('0'), 'wht_rate': Decimal('0'), }, ], }, '2.15in', ) @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_contract') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account') @patch('src.documents.distribution_fee_invoice.OwsPayee.get_tax_details') @patch('src.documents.distribution_fee_invoice.generate_pdf_file') @patch('src.documents.distribution_fee_invoice.OwsMoneyhub') @patch('src.documents.distribution_fee_invoice.OwsRoyalties') @patch('src.documents.distribution_fee_invoice.is_feature_enabled') def test_build_document_spanish( mock_is_feature_enabled, mock_ows_royalties, mock_ows_moneyhub, mock_generate_pdf_file, mock_get_tax_details, mock_get_account, mock_get_contract, mock_get_account_payment_term, mock_get_reference_payment_entity, contract_fixture, account_fixture, signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture_uk, reference_payment_entity_fixture, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[1] expected_path = path.join(config.FILE_OUTPUT_PATH, '2_2_distribution_fee_invoice.pdf') mock_is_feature_enabled.return_value = False mock_get_account.return_value = account_fixture mock_get_contract.return_value = contract_fixture mock_ows_moneyhub.get_reference_signing_entity.return_value = signing_entity_fixture mock_ows_moneyhub.get_ledger_vat_summary.return_value = ledger_vat_summary_fixture mock_ows_royalties.get_statement_period.return_value = statement_period_fixture mock_get_tax_details.return_value = tax_details_fixture mock_get_account_payment_term.return_value = account_payment_term_fixture_uk mock_get_reference_payment_entity.return_value = { 'country_of_tax_reporting': config.Countries.SPAIN } result = distribution_fee_invoice.build_document(statement_attachment) assert result == expected_path mock_generate_pdf_file.assert_called_once() mock_get_tax_details.assert_called_once_with( account_fixture['account_payee_id'], ['local_tax_id'] ) @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.distribution_fee_invoice.extract_wht_info') @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_contract') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account') @patch('src.documents.distribution_fee_invoice.OwsPayee.get_tax_details') @patch('src.documents.distribution_fee_invoice.generate_pdf_file') @patch('src.documents.distribution_fee_invoice.OwsMoneyhub') @patch('src.documents.distribution_fee_invoice.OwsRoyalties') @patch('src.documents.distribution_fee_invoice.is_feature_enabled') def test_build_document_with_wht( mock_is_feature_enabled, mock_ows_royalties, mock_ows_moneyhub, mock_generate_pdf_file, mock_get_tax_details, mock_get_account, mock_get_contract, mock_extract_wht_info, mock_get_account_payment_term, mock_get_reference_payment_entity, contract_fixture, account_fixture, altafonte_networks_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_wht_fixture, statement_period_fixture, account_payment_term_fixture_uk, reference_payment_entity_fixture, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[1] mock_is_feature_enabled.return_value = False mock_get_account.return_value = account_fixture mock_get_contract.return_value = contract_fixture mock_ows_moneyhub.get_reference_signing_entity.return_value = altafonte_networks_fixture mock_ows_moneyhub.get_ledger_vat_summary.return_value = ledger_vat_summary_wht_fixture mock_ows_royalties.get_statement_period.return_value = statement_period_fixture mock_get_tax_details.return_value = tax_details_fixture mock_get_account_payment_term.return_value = account_payment_term_fixture_uk mock_get_reference_payment_entity.return_value = reference_payment_entity_fixture distribution_fee_invoice.build_document(statement_attachment) mock_extract_wht_info.assert_called_once() @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.distribution_fee_invoice.OwsRoyalties.get_contract') @patch('src.documents.distribution_fee_invoice.OwsAbacusAccount.get_account') @patch('src.documents.distribution_fee_invoice.OwsPayee.get_tax_details') @patch('src.documents.distribution_fee_invoice.generate_pdf_file') @patch('src.documents.distribution_fee_invoice.OwsMoneyhub') @patch('src.documents.distribution_fee_invoice.OwsRoyalties') @patch('src.documents.distribution_fee_invoice.is_feature_enabled') def test_build_document_no_ledger_vat_summary_data( mock_is_feature_enabled, mock_ows_royalties, mock_ows_moneyhub, mock_generate_pdf_file, mock_get_tax_details, mock_get_account, mock_get_contract, mock_get_account_payment_term, mock_get_reference_payment_entity, contract_fixture, statement_attachments_fixture, account_statement_period_vat_fixture, account_fixture, signing_entity_fixture, tax_details_fixture, statement_period_fixture, account_payment_term_fixture_uk, reference_payment_entity_fixture, ): """Test generating a document with old logic when there is no data inside ledger vat.""" statement_attachment = statement_attachments_fixture[1] expected_path = path.join(config.FILE_OUTPUT_PATH, '2_2_distribution_fee_invoice.pdf') mock_is_feature_enabled.return_value = False mock_ows_moneyhub.get_ledger_vat_summary.return_value = None mock_get_account.return_value = account_fixture mock_get_contract.return_value = contract_fixture mock_ows_moneyhub.get_reference_signing_entity.return_value = signing_entity_fixture mock_ows_royalties.get_statement_period.return_value = statement_period_fixture mock_get_tax_details.return_value = tax_details_fixture mock_ows_moneyhub.get_account_statement_period_vat.return_value = ( account_statement_period_vat_fixture ) mock_get_account_payment_term.return_value = account_payment_term_fixture_uk mock_get_reference_payment_entity.return_value = reference_payment_entity_fixture result = distribution_fee_invoice.build_document(statement_attachment) assert result == expected_path mock_generate_pdf_file.assert_called_once() assert mock_generate_pdf_file.call_args.args[0] == expected_path assert mock_generate_pdf_file.call_args.args[1] == 'distribution_fee_invoice_old.html'