diff --git a/l10n_mx_ir_attachment_facturae/models/__init__.py b/l10n_mx_ir_attachment_facturae/models/__init__.py index 4b789da44577913421e615f81f9671c1b1747edb..26b237258503e54768d36b55c87bc30a95f00508 100644 --- a/l10n_mx_ir_attachment_facturae/models/__init__.py +++ b/l10n_mx_ir_attachment_facturae/models/__init__.py @@ -9,3 +9,4 @@ from . import ir_attachment from . import ir_attachment_facturae from . import ir_attachment_facturae_config from . import res_company_facturae_fiel +from . import cfdi_related diff --git a/l10n_mx_ir_attachment_facturae/models/base_cfdi_mixin.py b/l10n_mx_ir_attachment_facturae/models/base_cfdi_mixin.py index 5f15ccced33c1510f9ebc81f7ea44cd720c8c1bf..a210978bd9993eb24271b3753b32c5e9769843a7 100644 --- a/l10n_mx_ir_attachment_facturae/models/base_cfdi_mixin.py +++ b/l10n_mx_ir_attachment_facturae/models/base_cfdi_mixin.py @@ -3,6 +3,8 @@ import logging from openerp import api, fields, models +from openerp.exceptions import ValidationError +from openerp.tools.translate import _ _logger = logging.getLogger(__name__) @@ -83,6 +85,7 @@ class BaseCfdi(models.AbstractModel): "related_cfdi_id", "original_cfdi_id", "Refund invoices", + compute="_compute_related_cfdi_ids", readonly=True, copy=False, help="Original CFDI to which this CFDI is referred to", @@ -95,6 +98,17 @@ class BaseCfdi(models.AbstractModel): help="Select addendum node to use on this CFDI." ) + @api.multi + def _compute_related_cfdi_ids(self): + for record in self: + cfdi_related = self.env["cfdi.related"].search( + [ + ("related_cfdi_id", "=", record.id), + ("type_attachment", "=", record._name), + ] + ) + record.update({"related_cfdi_ids": cfdi_related.original_cfdi_id}) + @api.one def create_cfdi(self): """Creates a new cfdi object related with current record""" @@ -134,3 +148,33 @@ class BaseCfdi(models.AbstractModel): """Consult the status cancel of the cfdi related""" if self.cfdi_id and self.cfdi_id.state in ["waiting", "cancel"]: return self.cfdi_id.action_consult_cancellation_status() + + @api.multi + def _cancel_cfdi_related_document(self): + raise ValidationError(_("Cfdi was not canceled due to an internal problem")) + + @api.multi + def _cancel_cfdi(self): + """General steps needed for cancel a CFDI related to a record""" + # self._origin_cfdi_search() + self.ensure_one() + # Before cancel original voucher set relation to current related CFDI + self.write( + {"cfdi_relation_type": self.env.ref("l10n_mx.cfdi_relation_type_04").id} + ) + self.env["cfdi.related"].search( + [("related_cfdi_id", "=", self.id), ("type_attachment", "=", self._name)] + ).unlink() + self.env["cfdi.related"].create( + { + "related_cfdi_id": self.id, + "original_cfdi_id": self.cfdi_id.id, + "type_attachment": self._name, + } + ) + # We also remove relation for attachments to avoid confusing situations + self.cfdi_id.file_xml_sign.res_id = None + if self.cfdi_id.file_pdf: + self.cfdi_id.file_pdf.res_id = None + # Actually cancel de related document + self._cancel_cfdi_related_document() diff --git a/l10n_mx_ir_attachment_facturae/models/cfdi_related.py b/l10n_mx_ir_attachment_facturae/models/cfdi_related.py new file mode 100644 index 0000000000000000000000000000000000000000..0cda1cd791c0ded1c24b79f0ee77ad6a2a3d7836 --- /dev/null +++ b/l10n_mx_ir_attachment_facturae/models/cfdi_related.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 OpenPyme +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import fields, models + + +class CfdiRelated(models.Model): + + _name = "cfdi.related" + _description = "Cfdi Related" + _table = "cfdis_related_rel" + + related_cfdi_id = fields.Integer(help="Document related to cfdi") + original_cfdi_id = fields.Many2one( + "ir.attachment.facturae.mx", help="Original Cfdi replaced" + ) + type_attachment = fields.Char(help="Type of document related")