| 1 |
from odoo import tools, fields, Command |
| 2 |
from odoo.tests.common import tagged |
| 3 |
from odoo.addons.l10n_hu_edi.tests.common import L10nHuEdiTestCommon |
| 4 |
|
| 5 |
from freezegun import freeze_time |
| 6 |
import base64 |
| 7 |
import zipfile |
| 8 |
import io |
| 9 |
|
| 10 |
|
| 11 |
@tagged('post_install_l10n', '-at_install', 'post_install') |
| 12 |
class L10nHuEdiTestInvoiceXml(L10nHuEdiTestCommon): |
| 13 |
@classmethod |
| 14 |
def setUpClass(cls): |
| 15 |
with freeze_time('2024-02-01'): |
| 16 |
super().setUpClass() |
| 17 |
|
| 18 |
cls.company_data['company'].write({ |
| 19 |
'bank_ids': [Command.create({ |
| 20 |
'acc_number': 'HU0123456789', |
| 21 |
})] |
| 22 |
}) |
| 23 |
cls.partner_company.write({ |
| 24 |
'bank_ids': [Command.create({ |
| 25 |
'acc_number': 'HU6666666666', |
| 26 |
})] |
| 27 |
}) |
| 28 |
cls.bank_company = cls.env['res.partner.bank'].create({ |
| 29 |
'acc_number': 'HU7357735773', |
| 30 |
'partner_id': cls.company_data['company'].partner_id.id, |
| 31 |
}) |
| 32 |
cls.bank_partner = cls.env['res.partner.bank'].create({ |
| 33 |
'acc_number': 'HU9487189480', |
| 34 |
'partner_id': cls.partner_company.id, |
| 35 |
}) |
| 36 |
|
| 37 |
def test_invoice_and_credit_note(self): |
| 38 |
with freeze_time('2024-02-01'): |
| 39 |
invoice = self.create_invoice_simple() |
| 40 |
invoice.partner_bank_id = self.bank_company |
| 41 |
invoice.action_post() |
| 42 |
invoice._l10n_hu_edi_set_chain_index() |
| 43 |
invoice_xml = invoice._l10n_hu_edi_generate_xml() |
| 44 |
|
| 45 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_simple.xml', 'rb') as expected_xml_file: |
| 46 |
self.assertXmlTreeEqual( |
| 47 |
self.get_xml_tree_from_string(invoice_xml), |
| 48 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 49 |
) |
| 50 |
|
| 51 |
# Set invoice state to 'confirmed', otherwise the credit note can't be sent |
| 52 |
invoice.write({'l10n_hu_edi_state': 'confirmed'}) |
| 53 |
|
| 54 |
credit_note = self.create_reversal(invoice) |
| 55 |
credit_note.partner_bank_id = self.bank_partner |
| 56 |
credit_note.action_post() |
| 57 |
credit_note._l10n_hu_edi_set_chain_index() |
| 58 |
credit_note_xml = credit_note._l10n_hu_edi_generate_xml() |
| 59 |
|
| 60 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/credit_note.xml', 'rb') as expected_xml_file: |
| 61 |
self.assertXmlTreeEqual( |
| 62 |
self.get_xml_tree_from_string(credit_note_xml), |
| 63 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 64 |
) |
| 65 |
|
| 66 |
def test_invoice_complex_huf(self): |
| 67 |
with freeze_time('2024-02-01'): |
| 68 |
invoice = self.create_invoice_complex_huf() |
| 69 |
invoice.action_post() |
| 70 |
invoice_xml = invoice._l10n_hu_edi_generate_xml() |
| 71 |
|
| 72 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_complex_huf.xml', 'rb') as expected_xml_file: |
| 73 |
self.assertXmlTreeEqual( |
| 74 |
self.get_xml_tree_from_string(invoice_xml), |
| 75 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 76 |
) |
| 77 |
|
| 78 |
def test_invoice_complex_eur(self): |
| 79 |
with freeze_time('2024-02-01'): |
| 80 |
invoice = self.create_invoice_complex_eur() |
| 81 |
invoice.action_post() |
| 82 |
invoice_xml = invoice._l10n_hu_edi_generate_xml() |
| 83 |
|
| 84 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_complex_eur.xml', 'rb') as expected_xml_file: |
| 85 |
self.assertXmlTreeEqual( |
| 86 |
self.get_xml_tree_from_string(invoice_xml), |
| 87 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 88 |
) |
| 89 |
|
| 90 |
def test_advance_invoice(self): |
| 91 |
# Skip if sale is not installed |
| 92 |
if 'sale_line_ids' not in self.env['account.move.line']: |
| 93 |
self.skipTest('Sale module not installed, skipping advance invoice tests.') |
| 94 |
self.env.user.group_ids += self.env.ref('sales_team.group_sale_salesman') |
| 95 |
|
| 96 |
# Issue advance invoice on 2024-01-01. |
| 97 |
with freeze_time('2024-01-01'): |
| 98 |
sale_order, advance_invoice = self.create_advance_invoice() |
| 99 |
advance_invoice.action_post() |
| 100 |
advance_invoice_xml = advance_invoice._l10n_hu_edi_generate_xml() |
| 101 |
|
| 102 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_advance.xml', 'rb') as expected_xml_file: |
| 103 |
self.assertXmlTreeEqual( |
| 104 |
self.get_xml_tree_from_string(advance_invoice_xml), |
| 105 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 106 |
) |
| 107 |
|
| 108 |
# Pay advance invoice on 2024-01-15. |
| 109 |
with freeze_time('2024-01-15'): |
| 110 |
self.env['account.payment.register'].with_context(active_ids=advance_invoice.ids, active_model='account.move').create({})._create_payments() |
| 111 |
|
| 112 |
# Issue final invoice on 2024-02-01. The XML should report 2024-01-15 as the date of advance payment. |
| 113 |
with freeze_time('2024-02-01'): |
| 114 |
final_invoice = self.create_final_invoice(sale_order) |
| 115 |
final_invoice.action_post() |
| 116 |
final_invoice_xml = final_invoice._l10n_hu_edi_generate_xml() |
| 117 |
|
| 118 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_final.xml', 'rb') as expected_xml_file: |
| 119 |
self.assertXmlTreeEqual( |
| 120 |
self.get_xml_tree_from_string(final_invoice_xml), |
| 121 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 122 |
) |
| 123 |
|
| 124 |
def test_tax_audit_export(self): |
| 125 |
with freeze_time('2024-02-01'): |
| 126 |
invoice = self.create_invoice_simple() |
| 127 |
invoice.partner_bank_id = self.bank_company |
| 128 |
invoice.action_post() |
| 129 |
|
| 130 |
tax_audit_export = self.env['l10n_hu_edi.tax_audit_export'].create({ |
| 131 |
'date_from': fields.Date.today(), |
| 132 |
'date_to': fields.Date.today(), |
| 133 |
}) |
| 134 |
tax_audit_export.action_export() |
| 135 |
|
| 136 |
export_file = base64.b64decode(tax_audit_export.export_file) |
| 137 |
with io.BytesIO(export_file) as zip_buffer: |
| 138 |
with zipfile.ZipFile(zip_buffer) as zip_file: |
| 139 |
filenames = zip_file.namelist() |
| 140 |
with zip_file.open(filenames[0]) as invoice_file: |
| 141 |
invoice_xml = invoice_file.read() |
| 142 |
|
| 143 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_simple.xml', 'rb') as expected_xml_file: |
| 144 |
self.assertXmlTreeEqual( |
| 145 |
self.get_xml_tree_from_string(invoice_xml), |
| 146 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 147 |
) |
| 148 |
|
| 149 |
def test_multi_currency_tax_sign(self): |
| 150 |
currency_eur = self.env.ref('base.EUR') |
| 151 |
|
| 152 |
out_invoice = self.create_invoice_simple(currency=currency_eur) |
| 153 |
in_invoice = self.create_bill_simple(currency=currency_eur) |
| 154 |
out_refund = self.create_credit_note_simple(currency=currency_eur) |
| 155 |
in_refund = self.create_refund_simple(currency=currency_eur) |
| 156 |
|
| 157 |
expected_value = tools.float_round(10000 * self.tax_vat.amount / (100 * currency_eur.rate), self.currency.decimal_places) |
| 158 |
|
| 159 |
self.assertEqual(out_invoice._l10n_hu_get_invoice_totals_for_report()['total_vat_amount_in_huf'], expected_value) |
| 160 |
self.assertEqual(in_invoice._l10n_hu_get_invoice_totals_for_report()['total_vat_amount_in_huf'], expected_value) |
| 161 |
self.assertEqual(out_refund._l10n_hu_get_invoice_totals_for_report()['total_vat_amount_in_huf'], -expected_value) |
| 162 |
self.assertEqual(in_refund._l10n_hu_get_invoice_totals_for_report()['total_vat_amount_in_huf'], -expected_value) |
| 163 |
|
| 164 |
def test_invoice_simple_deduction(self): |
| 165 |
with freeze_time('2024-02-01'): |
| 166 |
invoice = self.create_invoice_simple_discount() |
| 167 |
invoice.action_post() |
| 168 |
invoice_xml = invoice._l10n_hu_edi_generate_xml() |
| 169 |
|
| 170 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_simple_discount.xml', 'rb') as expected_xml_file: |
| 171 |
self.assertXmlTreeEqual( |
| 172 |
self.get_xml_tree_from_string(invoice_xml), |
| 173 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 174 |
) |
| 175 |
|
| 176 |
def test_invoice_tax_price_include(self): |
| 177 |
with freeze_time('2024-02-01'): |
| 178 |
invoice = self.create_invoice_tax_price_include() |
| 179 |
invoice.action_post() |
| 180 |
invoice_xml = invoice._l10n_hu_edi_generate_xml() |
| 181 |
|
| 182 |
with tools.file_open('l10n_hu_edi/tests/invoice_xmls/invoice_tax_price_include.xml', 'rb') as expected_xml_file: |
| 183 |
self.assertXmlTreeEqual( |
| 184 |
self.get_xml_tree_from_string(invoice_xml), |
| 185 |
self.get_xml_tree_from_string(expected_xml_file.read()), |
| 186 |
) |