# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime
from odoo.addons.account_edi.tests.common import AccountEdiTestCommon
from odoo.tests import tagged
from odoo import Command
from pytz import timezone
from freezegun import freeze_time
@tagged('post_install_l10n', 'post_install', '-at_install')
class TestEcEdiCommon(AccountEdiTestCommon):
@classmethod
def setUpClass(cls, chart_template_ref='ec', edi_format_ref='l10n_ec_edi.ecuadorian_edi_format'):
super().setUpClass(chart_template_ref=chart_template_ref, edi_format_ref=edi_format_ref)
cls.frozen_today = datetime(year=2022, month=1, day=25, 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': "EC Test Company",
'vat': "1792366836001",
'street': "Avenida Machala 42",
'zip': "090514",
'city': "Guayaquil",
'country_id': cls.env.ref('base.ec').id,
'l10n_ec_legal_name': "EC Test Company (official)",
})
cls.env['account.chart.template']._l10n_ec_configure_ecuadorian_journals(cls.env.company)
for journal in (cls.company_data['default_journal_sale'], cls.company_data['default_journal_purchase']):
# Needs to be set before assigning authorization number
journal.write({
'l10n_latam_use_documents': True, # For tests the value is not set automatically
'l10n_ec_entity': '001',
'l10n_ec_emission': '001',
'l10n_ec_emission_address_id': cls.company_data['company'].partner_id,
})
# ==== Business ====
partner_vals = {
'name': "EC Test Partner AàÁ³$£€èêÈÊöÔÇç¡⅛&@™", # special characters should be escaped appropriately
'street': "Av. Libertador Simón Bolívar 1155",
'zip': "170209",
'city': "Quito",
'country_id': cls.env.ref('base.ec').id,
}
cls.partner_a.write({
**partner_vals,
'vat': "0453661050152",
'l10n_latam_identification_type_id': cls.env.ref('l10n_ec.ec_ruc').id,
})
cls.partner_b.write({
**partner_vals,
'vat': "0453661050",
'l10n_latam_identification_type_id': cls.env.ref('l10n_ec.ec_dni').id,
})
cls.product_withhold = cls.env['product.product'].create({
'name': 'Test Service Product',
'detailed_type': 'service',
'lst_price': 100.0,
'standard_price': 80.0,
'property_account_income_id': cls.company_data['default_account_revenue'].id,
'property_account_expense_id': cls.company_data['default_account_expense'].id,
'taxes_id': [(Command.set(cls.tax_sale_a.ids))],
'supplier_taxes_id': [Command.set(cls.tax_purchase_a.ids)],
'l10n_ec_withhold_tax_id': cls._get_tax_by_xml_id('tax_withhold_profit_312').id,
})
# ===== HELPER METHODS =====
@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'account.{cls.env.company.id}_{trailing_xml_id}')
def get_invoice_line_vals(self, vat_tax_xmlid='tax_vat_510_sup_01'):
"""Default values for invoice line creation"""
return [Command.create({
'product_id': self.product_a.id,
'price_unit': 100.0,
'quantity': 5,
'discount': 20,
'tax_ids': [Command.set(self._get_tax_by_xml_id(vat_tax_xmlid).ids)],
})]
def get_invoice_vals(self, invoice_line_args):
"""Default values for invoice creation"""
return {
'name': 'INV/01',
'invoice_date': self.frozen_today,
'date': self.frozen_today,
'invoice_line_ids': invoice_line_args,
'l10n_ec_sri_payment_id': self.env['l10n_ec.sri.payment'].search([('code', '=', 16)], limit=1).id, # Debit card (see l10n_ec.sri.payment.csv)
'l10n_latam_document_number': '001-001-000000001',
}
def get_invoice(self, invoice_args, invoice_line_args=None):
if invoice_line_args is None:
invoice_line_args = self.get_invoice_line_vals()
invoice_vals = self.get_invoice_vals(invoice_line_args)
invoice_vals.update(invoice_args)
invoice = self.env['account.move'].create({
**invoice_vals,
})
invoice.l10n_latam_document_number = invoice.l10n_latam_document_number or '001-001-000000001'
return invoice
def get_custom_purchase_invoice_line_vals(self):
product_ids_vals = [
(self.product_a, 'tax_vat_510_sup_01', 100.0, 5.0, 20.0),
(self.product_b, 'tax_vat_512_sup_04', 100.0, 5.0, 0.0),
(self.product_withhold, 'tax_vat_517_sup_15', 50.0, 1.0, 0.0),
(self.product_withhold, 'tax_vat_517_sup_15', 30.0, 10.0, 0.0)
]
line_vals = []
for product, tax, price_unit, qty, discount in product_ids_vals:
line_vals.append(Command.create({
'product_id': product.id,
'price_unit': price_unit,
'quantity': qty,
'tax_ids': [Command.set(self._get_tax_by_xml_id(tax).ids)],
'discount': discount,
}))
return line_vals
def get_wizard_and_purchase_invoice(self):
purchase_journal = self.env['account.journal'].search([
('company_id', '=', self.company_data['company'].id),
('code', '=', 'BILL')])
with freeze_time(self.frozen_today):
purchase_invoice = self.get_invoice({
'move_type': 'in_invoice',
'partner_id': self.partner_a.id,
'journal_id': purchase_journal.id,
'l10n_ec_sri_payment_id': self.env.ref('l10n_ec.P1').id,
'invoice_line_ids': self.get_custom_purchase_invoice_line_vals(),
})
purchase_invoice.action_post()
with freeze_time(self.frozen_today):
wizard = self.env['l10n_ec.wizard.account.withhold'].with_context(active_ids=[purchase_invoice.id], active_model='account.move').create({})
wizard.document_number = '001-001-000000001'
return wizard, purchase_invoice
def set_custom_taxpayer_type_on_partner_a(self):
# Setting a contributor type to partner
self.partner_a.l10n_ec_taxpayer_type_id = self.env.ref('l10n_ec_edi.l10n_ec_taxpayer_type_01')
self.partner_a.l10n_ec_taxpayer_type_id.profit_withhold_tax_id = self._get_tax_by_xml_id('tax_withhold_profit_303')
self.partner_a.l10n_ec_taxpayer_type_id.vat_goods_withhold_tax_id = self._get_tax_by_xml_id('tax_withhold_vat_10')
self.partner_a.l10n_ec_taxpayer_type_id.vat_services_withhold_tax_id = self._get_tax_by_xml_id('tax_withhold_vat_20')
# ===== HARD-CODED XMLS =====
L10N_EC_EDI_XML_OUT_INV = """
1
1
PRUEBAS SERVICIO DE RENTAS INTERNAS
PRUEBAS SERVICIO DE RENTAS INTERNAS
1792366836001
2501202201179236683600110010010000000013121521410
01
001
001
000000001
Avenida Machala 42
25/01/2022
Avenida Machala 42
SI
04
EC Test Partner AàÁ³$£€èêÈÊöÔÇç¡⅛&@™
0453661050152
Av. Libertador Simón Bolívar 1155
400.000000
100.00
2
2
400.000000
12.000000
48.00
448.00
DOLAR
16
448.00
0
dias
N/A
product_a
5.000000
100.000000
100.00
400.00
2
2
12.000000
400.000000
48.00
Fact 001-001-000000001
Because I am accountman!
accountman@test.com
""".encode()
L10N_EC_EDI_XML_DEBIT_NOTE = """
1
1
PRUEBAS SERVICIO DE RENTAS INTERNAS
PRUEBAS SERVICIO DE RENTAS INTERNAS
1792366836001
2501202205179236683600110010010000000013121521416
05
001
001
000000001
Avenida Machala 42
25/01/2022
Avenida Machala 42
04
EC Test Partner AàÁ³$£€èêÈÊöÔÇç¡⅛&@™
0453661050152
SI
01
001-001-000000001
25/01/2022
400.000000
2
2
12.000000
400.000000
48.00
448.00
16
448.00
0
dias
product_a
400.000000
NotDb 001-001-000000001
Because I am accountman!
accountman@test.com
""".encode()
L10N_EC_EDI_XML_CREDIT_NOTE = """
1
1
PRUEBAS SERVICIO DE RENTAS INTERNAS
PRUEBAS SERVICIO DE RENTAS INTERNAS
1792366836001
2501202204179236683600110010010000000013121521411
04
001
001
000000001
Avenida Machala 42
25/01/2022
Avenida Machala 42
04
EC Test Partner AàÁ³$£€èêÈÊöÔÇç¡⅛&@™
0453661050152
SI
01
001-001-000000001
25/01/2022
400.000000
448.00
DOLAR
2
2
400.000000
48.00
NotCr 001-001-000000001
N/A
product_a
5.000000
100.000000
100.00
400.00
2
2
12.000000
400.000000
48.00
NotCr 001-001-000000001
Because I am accountman!
accountman@test.com
""".encode()
L10N_EC_EDI_XML_PURCHASE_LIQ = """
1
1
PRUEBAS SERVICIO DE RENTAS INTERNAS
PRUEBAS SERVICIO DE RENTAS INTERNAS
1792366836001
2501202203179236683600110010010000000013121521413
03
001
001
000000001
Avenida Machala 42
25/01/2022
Avenida Machala 42
SI
05
EC Test Partner AàÁ³$£€èêÈÊöÔÇç¡⅛&@™
0453661050
Av. Libertador Simón Bolívar 1155
400.000000
100.00
2
2
400.000000
48.00
448.00
DOLAR
16
134.40
0
dias
16
313.60
34
dias
N/A
product_a
5.000000
100.000000
100.00
400.00
2
2
12.000000
400.000000
48.00
LiqCo 001-001-000000001
""".encode()
L10N_EC_EDI_XML_IN_WTH = """
1
1
PRUEBAS SERVICIO DE RENTAS INTERNAS
PRUEBAS SERVICIO DE RENTAS INTERNAS
1792366836001
2501202207179236683600110010010000000013121521419
07
001
001
000000001
Avenida Machala 42
25/01/2022
Avenida Machala 42
SI
04
NO
EC Test Partner AàÁ³$£€èêÈÊöÔÇç¡⅛&@™
0453661050152
01/2022
01
01
001001000000001
25/01/2022
01
400.00
448.00
2
2
400.00
12.00
48.00
1
312
400.00
1.75
7.00
01
448.00
Av. Libertador Simón Bolívar 1155
""".encode()
L10N_EC_EDI_XML_PURCHASE_LIQ_WTH = """
1
1
PRUEBAS SERVICIO DE RENTAS INTERNAS
PRUEBAS SERVICIO DE RENTAS INTERNAS
1792366836001
2501202207179236683600110010010000000013121521419
07
001
001
000000001
Avenida Machala 42
25/01/2022
Avenida Machala 42
SI
05
NO
EC Test Partner AàÁ³$£€èêÈÊöÔÇç¡⅛&@™
0453661050
01/2022
01
03
001001000000001
25/01/2022
01
100.00
112.00
2
2
100.00
12.00
12.00
2
3
12.00
100.00
12.00
16
112.00
04
03
001001000000001
25/01/2022
01
200.00
224.00
2
2
200.00
12.00
24.00
2
3
23.99
100.00
23.99
16
224.00
Av. Libertador Simón Bolívar 1155
""".encode()
L10N_EC_EDI_XPATH_INVOICE_IN = """
01
01
001001000000001
25/01/2022
01
400.00
448.00
2
2
400.00
12.00
48.00
1
312
400.00
1.75
7.00
01
448.00
04
01
001001000000001
25/01/2022
01
500.00
560.00
2
2
500.00
12.00
60.00
1
312
500.00
1.75
8.75
01
560.00
15
01
001001000000001
25/01/2022
01
350.00
350.00
2
0
350.00
0.00
0.00
1
312
350.00
1.75
6.13
01
350.00
"""
L10N_EC_EDI_XPATH_INVOICE_IN_CUSTOM_TAXPAYER = """
1
303
400.00
10.00
40.00
2
9
48.00
10.00
4.80
1
303
500.00
10.00
50.00
2
9
60.00
10.00
6.00
1
303
350.00
10.00
35.00
"""