# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
from datetime import datetime
from odoo.addons.account_edi.tests.common import AccountEdiTestCommon
from odoo.tools import misc
from pytz import timezone
class TestEsEdiTbaiCommon(AccountEdiTestCommon):
@classmethod
def setUpClass(cls, chart_template_ref='l10n_es.account_chart_template_full', edi_format_ref='l10n_es_edi_tbai.edi_es_tbai'):
super().setUpClass(chart_template_ref=chart_template_ref, edi_format_ref=edi_format_ref)
cls.frozen_today = datetime(year=2022, month=1, day=1, hour=0, minute=0, second=0, tzinfo=timezone('utc'))
# Allow to see the full result of AssertionError.
cls.maxDiff = None
# ==== Config ====
cls.company_data['company'].write({
'name': 'EUS Company',
'country_id': cls.env.ref('base.es').id,
'state_id': cls.env.ref('base.state_es_ss').id,
'vat': 'ES09760433S',
'l10n_es_edi_test_env': True,
})
cls.certificate = None
cls._set_tax_agency('gipuzkoa')
# ==== Business ====
cls.partner_a.write({
'name': "&@àÁ$£€èêÈÊöÔÇç¡⅛™³", # special characters should be escaped appropriately
'vat': 'BE0477472701',
'country_id': cls.env.ref('base.be').id,
'street': 'Rue Sans Souci 1',
'zip': 93071,
})
cls.partner_b.write({
'vat': 'ESF35999705',
})
cls.product_t = cls.env["product.product"].create(
{"name": "Test product"})
cls.partner_t = cls.env["res.partner"].create({"name": "Test partner", "vat": "ESF35999705"})
@classmethod
def _set_tax_agency(cls, agency):
if agency == "araba":
cert_name = 'araba_1234.p12'
cert_password = '1234'
elif agency == 'bizkaia':
cert_name = 'Bizkaia-IZDesa2021.p12'
cert_password = 'IZDesa2021'
elif agency == 'gipuzkoa':
cert_name = 'gipuzkoa_IZDesa2021.p12'
cert_password = 'IZDesa2021'
else:
raise ValueError("Unknown tax agency: " + agency)
cls.certificate = cls.env['l10n_es_edi.certificate'].sudo().create({
'content': base64.encodebytes(
misc.file_open("l10n_es_edi_tbai/demo/certificates/" + cert_name, 'rb').read()),
'password': cert_password,
})
cls.company_data['company'].sudo().write({
'l10n_es_tbai_tax_agency': agency,
'l10n_es_edi_certificate_id': cls.certificate.id,
})
@classmethod
def _get_tax_by_xml_id(cls, trailing_xml_id):
""" Helper to retrieve a tax easily.
:param trailing_xml_id: The trailing tax's xml id.
:return: An account.tax record
"""
return cls.env.ref(f'l10n_es.{cls.env.company.id}_account_tax_template_{trailing_xml_id}')
@classmethod
def create_invoice(cls, **kwargs):
return cls.env['account.move'].with_context(edi_test_mode=True).create({
'move_type': 'out_invoice',
'partner_id': cls.partner_a.id,
'invoice_date': '2022-01-01',
'date': '2022-01-01',
**kwargs,
'invoice_line_ids': [(0, 0, {
'product_id': cls.product_a.id,
'price_unit': 1000.0,
**line_vals,
}) for line_vals in kwargs.get('invoice_line_ids', [])],
})
L10N_ES_TBAI_SAMPLE_XML_POST = """
1.2
___ignore___
EUS Company
02
BE0477472701
&@àÁ$£€èêÈÊöÔÇç¡⅛™³
___ignore___
___ignore___
N
N
INVTEST
01
01-01-2022
___ignore___
N
manual
producta
5.00
1000.00
1000.00
4840.00
4840.00
01
S1
4000.00
21.00
840.00
N
___ignore___
___ignore___
___ignore___
___ignore___
___ignore___
""".encode("utf-8")
L10N_ES_TBAI_SAMPLE_XML_CANCEL = """
1.2
09760433S
EUS Company
INVTEST
01
01-01-2022
___ignore___
___ignore___
___ignore___
___ignore___
___ignore___
""".encode("utf-8")
L10N_ES_TBAI_SAMPLE_XML_POST_IN = """
240
2
A00
1.0
2022
09760433S
EUS Company
02
BE0477472701
&@àÁ$£€èêÈÊöÔÇç¡⅛™³
TEST
INV/5234
01-01-2022
01-01-2022
F1
INV/5234
01
4840.00
C
N
4000.00
21.0
840.00
840.00
"""
L10N_ES_TBAI_SAMPLE_XML_POST_IN_ND = """
240
2
A00
1.0
2022
09760433S
EUS Company
02
BE0477472701
&@àÁ$£€èêÈÊöÔÇç¡⅛™³
TEST
INV/5234
01-01-2022
01-01-2022
F1
INV/5234
01
1100.00
C
N
1000.00
10.0
100.00
0.00
"""
L10N_ES_TBAI_SAMPLE_XML_POST_IN_IC = """
240
2
A00
1.0
2022
09760433S
EUS Company
F35999705
partner_b
TEST
INV/5234
01-01-2022
01-01-2022
F1
INV/5234
09
12000.00
C
N
4000.00
21.0
840.00
840.00
G
N
8000.00
21.0
1680.00
1680.00
"""