"""Test generating self-billing invoices.""" from datetime import datetime from decimal import Decimal from os import path from unittest.mock import call from unittest.mock import patch import pytest import config from src.documents import self_billing_invoice from src.documents.utils import format_address from src.utils.error_handling import LambdaException @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_contract') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account') @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.generate_pdf_file') @patch('src.documents.self_billing_invoice.OwsMoneyhub') @patch('src.documents.self_billing_invoice.OwsRoyalties') @patch('src.documents.self_billing_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, mock_get_bank_details, account_fixture, contract_fixture, signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[0] vat_categories = ['gross_revenue', 'closing_balance', 'custom_payment'] expected_path = path.join(config.FILE_OUTPUT_PATH, '1_1_self_billing_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 mock_get_reference_payment_entity.return_value = {'country_of_tax_reporting': 'TBD'} result = self_billing_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] == 'self_billing_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'], ['vat_number'] ) 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, ) assert mock_get_account_payment_term.call_args_list == [ call(statement_attachment['account_id']) ] assert mock_get_reference_payment_entity.call_args_list == [ call(account_payment_term_fixture['payment_entity_id']) ] assert not mock_get_bank_details.called @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_contract') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account') @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.generate_pdf_file_playwright') @patch('src.documents.self_billing_invoice.OwsMoneyhub') @patch('src.documents.self_billing_invoice.OwsRoyalties') @patch('src.documents.self_billing_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, mock_get_bank_details, account_fixture, contract_fixture, signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[0] expected_path = path.join(config.FILE_OUTPUT_PATH, '1_1_self_billing_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 mock_get_reference_payment_entity.return_value = {'country_of_tax_reporting': 'TBD'} result = self_billing_invoice.build_document(statement_attachment) assert result == expected_path mock_generate_pdf_file_playwright.assert_called_once_with( expected_path, 'self_billing_invoice/body.html', 'self_billing_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'], 'customer_name': tax_details_fixture['business_name'], 'customer_address': format_address(tax_details_fixture['address']), 'account_id': account_fixture['account_id'], 'account_name': account_fixture['account_name'], 'contract_name': contract_fixture['contract_name'], 'contract_id': contract_fixture['contract_id'], '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'), 'footer_info': 'The VAT shown is your output tax due to HMRC.', 'has_vat': True, 'has_wht': False, 'payee_total_vat': Decimal('200.00'), 'subtotal_amount': Decimal('213012.44'), 'total_amount': Decimal('213212.44'), 'total_wht_amount_payee_currency': Decimal('0'), 'total_wht_amount_vat_currency': Decimal('0'), '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.75in', ) @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_contract') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account') @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.generate_pdf_file') @patch('src.documents.self_billing_invoice.OwsMoneyhub') @patch('src.documents.self_billing_invoice.OwsRoyalties') @patch('src.documents.self_billing_invoice.is_feature_enabled') def test_build_document_spanish_entity_no_vat_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, mock_get_bank_details, account_fixture, contract_fixture, signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture, bank_details_fixture, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[0] vat_categories = ['gross_revenue', 'closing_balance', 'custom_payment'] expected_path = path.join(config.FILE_OUTPUT_PATH, '1_1_self_billing_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, 'vat_number': None, 'address': None} mock_get_account_payment_term.return_value = account_payment_term_fixture mock_get_reference_payment_entity.return_value = { 'country_of_tax_reporting': config.Countries.SPAIN } mock_get_bank_details.return_value = bank_details_fixture result = self_billing_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] == 'self_billing_invoice/body.html' assert ( mock_generate_pdf_file.call_args.args[2]['customer_tax_number'] == tax_details_fixture['local_tax_id'] ) 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'], ['local_tax_id'] ) 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, ) assert mock_get_account_payment_term.call_args_list == [ call(statement_attachment['account_id']) ] assert mock_get_reference_payment_entity.call_args_list == [ call(account_payment_term_fixture['payment_entity_id']) ] assert mock_get_bank_details.call_args_list == [ call(account_fixture['account_payee_id'], ['address']) ] @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.self_billing_invoice.extract_wht_info') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_contract') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account') @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.generate_pdf_file') @patch('src.documents.self_billing_invoice.OwsMoneyhub') @patch('src.documents.self_billing_invoice.OwsRoyalties') @patch('src.documents.self_billing_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, mock_get_bank_details, account_fixture, contract_fixture, altafonte_networks_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_wht_fixture, statement_period_fixture, account_payment_term_fixture, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[0] 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 mock_get_reference_payment_entity.return_value = {'country_of_tax_reporting': 'TBD'} self_billing_invoice.build_document(statement_attachment) mock_extract_wht_info.assert_called_once() assert mock_get_account_payment_term.call_args_list == [ call(statement_attachment['account_id']) ] assert mock_get_reference_payment_entity.call_args_list == [ call(account_payment_term_fixture['payment_entity_id']) ] @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_contract') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account') @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.generate_pdf_file') @patch('src.documents.self_billing_invoice.OwsMoneyhub') @patch('src.documents.self_billing_invoice.OwsRoyalties') @patch('src.documents.self_billing_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, mock_get_bank_details, 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, ): """Test generating a document when there is no data inside ledger vat.""" statement_attachment = statement_attachments_fixture[0] 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_get_account_payment_term.return_value = account_payment_term_fixture mock_get_reference_payment_entity.return_value = {'country_of_tax_reporting': 'TBD'} with pytest.raises(LambdaException) as err: self_billing_invoice.build_document(statement_attachment) assert str( err.value.message ) == 'There is no data inside ledger_vat_summary for {account_id} {contract_id}'.format( account_id=statement_attachment['account_id'], contract_id=statement_attachment['contract_id'], ) assert mock_get_account_payment_term.call_args_list == [ call(statement_attachment['account_id']) ] assert mock_get_reference_payment_entity.call_args_list == [ call(account_payment_term_fixture['payment_entity_id']) ] mock_generate_pdf_file.assert_not_called() @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_signing_entity') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_contract') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account') @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.generate_pdf_file') @patch('src.documents.self_billing_invoice.OwsMoneyhub') @patch('src.documents.self_billing_invoice.OwsRoyalties') @patch('src.documents.self_billing_invoice.is_feature_enabled') def test_build_document_orchard_norway( 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, mock_get_reference_signing_entity, mock_get_bank_details, account_fixture, contract_fixture, orch_norway_signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture_norway, ): """Test generating a document for norway.""" statement_attachment = statement_attachments_fixture[0] vat_categories = ['gross_revenue', 'closing_balance', 'custom_payment'] expected_path = path.join(config.FILE_OUTPUT_PATH, '1_1_self_billing_invoice.pdf') mock_is_feature_enabled.return_value = False mock_get_account.return_value = account_fixture mock_get_contract.return_value = contract_fixture mock_get_reference_signing_entity.get_reference_signing_entity.return_value = ( orch_norway_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_norway mock_get_reference_payment_entity.return_value = {'country_of_tax_reporting': 'TBD'} result = self_billing_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] == 'self_billing_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_get_reference_signing_entity.assert_called_once_with( config.ORCHARD_SIGNING_ENTITY_MAPPING[config.ORCHARD_NORWAY_PAYMENT_ENTITY] ) mock_get_tax_details.assert_called_once_with( account_fixture['account_payee_id'], ['vat_number'] ) 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, ) assert mock_get_account_payment_term.call_args_list == [ call(statement_attachment['account_id']) ] assert mock_get_reference_payment_entity.call_args_list == [ call(account_payment_term_fixture_norway['payment_entity_id']) ] assert not mock_get_bank_details.called @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_signing_entity') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_reference_payment_entity') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account_payment_term') @patch('src.documents.self_billing_invoice.OwsRoyalties.get_contract') @patch('src.documents.self_billing_invoice.OwsAbacusAccount.get_account') @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.generate_pdf_file') @patch('src.documents.self_billing_invoice.OwsMoneyhub') @patch('src.documents.self_billing_invoice.OwsRoyalties') @patch('src.documents.self_billing_invoice.is_feature_enabled') def test_build_document_orchard_uk( 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, mock_get_reference_signing_entity, mock_get_bank_details, account_fixture, contract_fixture, orch_uk_signing_entity_fixture, tax_details_fixture, statement_attachments_fixture, ledger_vat_summary_fixture, statement_period_fixture, account_payment_term_fixture_uk, ): """Test generating a document.""" statement_attachment = statement_attachments_fixture[0] vat_categories = ['gross_revenue', 'closing_balance', 'custom_payment'] expected_path = path.join(config.FILE_OUTPUT_PATH, '1_1_self_billing_invoice.pdf') mock_is_feature_enabled.return_value = False mock_get_account.return_value = account_fixture mock_get_contract.return_value = contract_fixture mock_get_reference_signing_entity.get_reference_signing_entity.return_value = ( orch_uk_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': 'TBD'} result = self_billing_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] == 'self_billing_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_get_reference_signing_entity.assert_called_once_with( config.ORCHARD_SIGNING_ENTITY_MAPPING[config.ORCHARD_UK_PAYMENT_ENTITY] ) mock_get_tax_details.assert_called_once_with( account_fixture['account_payee_id'], ['vat_number'] ) 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, ) assert mock_get_account_payment_term.call_args_list == [ call(statement_attachment['account_id']) ] assert mock_get_reference_payment_entity.call_args_list == [ call(account_payment_term_fixture_uk['payment_entity_id']) ] assert not mock_get_bank_details.called @pytest.mark.parametrize( 'is_spanish_entity, tax_details, bank_details, expected_tax_details', [ ( True, {'local_tax_id': 232}, { 'address': { 'address_1': 'TV HENRIQUE CARDOSO Nº37 - 2º DRTº', 'address_2': '', 'city': 'LISBOA', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, 'contact': {'first_name': 'John', 'last_name': 'Doe'}, 'type': 'INDIVIDUAL', }, { 'business_name': 'John Doe', 'tax_number': 232, 'address': { 'address_1': 'TV HENRIQUE CARDOSO Nº37 - 2º DRTº', 'address_2': '', 'city': 'LISBOA', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, }, ), ( True, {'local_tax_id': 233, 'business_name': 'fake-biz'}, { 'address': { 'address_1': '100 fake street', 'address_2': '', 'city': 'Madrid', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, 'company': {'name': 'fake-bizness'}, 'type': 'COMPANY', }, { 'business_name': 'fake-biz', 'tax_number': 233, 'address': { 'address_1': '100 fake street', 'address_2': '', 'city': 'Madrid', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, }, ), ( False, {'vat_number': 235, 'business_name': 'Mckinsey & Company'}, { 'address': { 'address_1': '100 Wall St', 'address_2': '', 'city': 'New York', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, 'company': {'name': 'Mckinsey & Company backup'}, 'type': 'COMPANY', }, { 'business_name': 'Mckinsey & Company', 'tax_number': 235, 'address': { 'address_1': '100 Wall St', 'address_2': '', 'city': 'New York', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, }, ), ( False, { 'vat_number': 235, 'business_name': 'Mckinsey & Company', 'address': { 'address_1': '100 Wall St', 'address_2': '', 'city': 'New York', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, }, { 'address': { 'address_1': '100 Wall St', 'address_2': '', 'city': 'New York', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, 'company': {'name': 'Mckinsey & Company backup'}, 'type': 'COMPANY', }, { 'business_name': 'Mckinsey & Company', 'tax_number': 235, 'address': { 'address_1': '100 Wall St', 'address_2': '', 'city': 'New York', 'country_code': 'PT', 'province': None, 'zip': '1700-227', }, }, ), ( True, { 'account_payee_id': 81698, 'vat_number': None, 'local_tax_id': 'taxtaxtax', 'business_name': 'BRODAMUSICLDA', 'business_number': None, 'country_of_tax_residency_code': 'PRT', 'address': { 'country_code': None, 'address_1': None, 'address_2': '', 'city': None, 'province': None, 'zip': None, }, }, { 'account_payee_id': '80683', 'address': { 'address_1': 'MEDELLIN273INT202COL.ROMASUR', 'address_2': '', 'city': 'CUAUHTEMOC', 'country_code': 'MX', 'province': None, 'zip': '06760', }, 'company': {'name': 'DRAGORAGRUPOCOMERCIALSADECV'}, 'contact': {'email': None, 'phone': None, 'phone_country': None}, 'modified_at': '2025-07-15T18: 37: 29.884414+0000', 'payoneer_client_reference_id': '80683', 'payout_method': { 'bank_account_type': 'COMPANY', 'bank_field_details': [], 'country': 'MX', 'currency': 'MXN', }, 'type': 'COMPANY', }, { 'business_name': 'BRODAMUSICLDA', 'tax_number': 'taxtaxtax', 'address': { 'address_1': 'MEDELLIN273INT202COL.ROMASUR', 'address_2': '', 'city': 'CUAUHTEMOC', 'country_code': 'MX', 'province': None, 'zip': '06760', }, }, ), ], ) @patch('src.documents.self_billing_invoice.OwsPayee.get_tax_details') @patch('src.documents.self_billing_invoice.OwsPayee.get_bank_details') def test_get_tax_details( mock_bank_details, mock_tax_details, is_spanish_entity, tax_details, bank_details, expected_tax_details, ): """Test getting tax details for self-billing invoice.""" mock_tax_details.return_value = tax_details mock_bank_details.return_value = bank_details account = {'account_payee_id': 123} address = tax_details.get('address') invalid_address = not address or all(not v for v in address.values()) has_invalid_details = not tax_details.get('business_name') or invalid_address result = self_billing_invoice._get_tax_details(is_spanish_entity, account) assert result == expected_tax_details assert has_invalid_details == mock_bank_details.called