Skip to content

Commit

Permalink
[FIX/I18N] l10n_es_ticketbai: fix invoice with VAT not subject to.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljsalvatierra-binovo authored and ao-landoo committed Sep 20, 2021
1 parent 55f609d commit 38c92e0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
Expand Up @@ -18,7 +18,7 @@
id="l10n_es.fp_not_subject_tai"
model="account.fiscal.position.template"
>
<field name="tbai_vat_regime_key" ref="tbai_vat_regime_01" />
<field name="tbai_vat_regime_key" ref="tbai_vat_regime_08" />
</record>
<record id="l10n_es.fp_recargo" model="account.fiscal.position.template">
<field name="tbai_vat_regime_key" ref="tbai_vat_regime_51" />
Expand Down
13 changes: 13 additions & 0 deletions l10n_es_ticketbai/migrations/11.0.0.2.0/post-migration.py
@@ -0,0 +1,13 @@
# Copyright 2021 Binovo IT Human Project SL
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, SUPERUSER_ID


def migrate(cr, version):
env = api.Environment(cr, SUPERUSER_ID, {})
companies = env['res.company'].search([])
fps = env['account.fiscal.position']
for company in companies:
fps |= company.get_fps_from_templates(env.ref("l10n_es.fp_not_subject_tai"))
fps.write({
'tbai_vat_regime_key': env.ref("l10n_es_ticketbai.tbai_vat_regime_08").id})
26 changes: 26 additions & 0 deletions l10n_es_ticketbai/models/res_company.py
Expand Up @@ -2,6 +2,7 @@
# Copyright 2021 Landoo Sistemas de Informacion SL
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo.tools import ormcache


class ResCompany(models.Model):
Expand Down Expand Up @@ -37,6 +38,31 @@ def tbai_certificate_get_private_key(self):
else:
return None

@ormcache('fp_template', 'company')
def _get_fp_id_from_fp_template(self, fp_template, company):
"""Low level cached search for a fiscal position given its template and
company.
"""
xmlids = self.env['ir.model.data'].search_read([
('model', '=', 'account.fiscal.position.template'),
('res_id', '=', fp_template.id)
], ['name', 'module'])
return xmlids and self.env['ir.model.data'].search([
('model', '=', 'account.fiscal.position'),
('module', '=', xmlids[0]['module']),
('name', '=', '{}_{}'.format(company.id, xmlids[0]['name']))
]).res_id or False

def get_fps_from_templates(self, fp_templates):
"""Return company fiscal positions that match the given templates."""
self.ensure_one()
fp_ids = []
for tmpl in fp_templates:
fp_id = self._get_fp_id_from_fp_template(tmpl, self)
if fp_id:
fp_ids.append(fp_id)
return self.env['account.fiscal.position'].browse(fp_ids)

def write(self, vals):
super().write(vals)
if vals.get("tbai_enabled", False):
Expand Down
2 changes: 2 additions & 0 deletions l10n_es_ticketbai/tests/common.py
Expand Up @@ -394,3 +394,5 @@ def setUp(self):
],
}
)
self.fiscal_position_ipsi_igic = self.main_company.get_fps_from_templates(
self.env.ref("l10n_es.fp_not_subject_tai"))
18 changes: 18 additions & 0 deletions l10n_es_ticketbai/tests/test_l10n_es_ticketbai_customer_invoice.py
Expand Up @@ -105,6 +105,24 @@ def test_cancel_and_recreate(self):
self.assertEqual(invoices_with_errors[1].state, "cancel")
self.assertEqual(invoice3.tbai_invoice_id.state, "pending")

def test_invoice_ipsi_igic(self):
invoice = self.create_draft_invoice(
self.account_billing.id, self.fiscal_position_ipsi_igic)
for line in invoice.invoice_line_ids:
if line.product_id != self.product_service:
line.unlink()
invoice.onchange_fiscal_position_id_tbai_vat_regime_key()
invoice.compute_taxes()
self.assertEqual(1, len(invoice.invoice_line_ids))
self.assertEqual('RL', invoice.tax_line_ids.tbai_get_value_causa())
invoice.action_invoice_open()
self.assertEqual(invoice.state, 'open')
self.assertEqual(1, len(invoice.tbai_invoice_ids))
root, signature_value = \
invoice.sudo().tbai_invoice_ids.get_tbai_xml_signed_and_signature_value()
res = XMLSchema.xml_is_valid(self.test_xml_invoice_schema_doc, root)
self.assertTrue(res)

def test_invoice_foreign_customer_extracommunity(self):
invoice = self.create_draft_invoice(
self.account_billing.id,
Expand Down

0 comments on commit 38c92e0

Please sign in to comment.