...
 
Commits (14)
......@@ -124,10 +124,6 @@ class AccountMove(models.Model):
"cfdi.use", "CFDI use", readonly=True, states={"draft": [("readonly", False)]}
)
cfdi_relation_type = fields.Many2one("cfdi.relation.type", "CFDI Relation type")
state = fields.Selection(
selection_add=[("waiting", _("To cancel"))],
ondelete={'waiting': 'cascade'}
)
l10n_mx_edi_to_cancel = fields.Char(
#compute="_compute_l10n_mx_edi_to_cancel",
#search="_search_l10n_mx_edi_to_cancel",
......@@ -316,10 +312,9 @@ class AccountMove(models.Model):
# Create new CFDI object for this invoice
account_invoice.create_cfdi()
def action_cancel(self):
"""Extend `AccountMove.action_cancel()`; Cancels the CFDI related to the
invoice
"""
def l10n_mx_action_cancel(self):
"""Cancels the CFDI related to the invoice"""
# Get only invoices with related cfdi to cancel cfdi before cancel invoice
cfdis = self.filtered(
lambda i:
......@@ -327,38 +322,46 @@ class AccountMove(models.Model):
and i.cfdi_id
and i.cfdi_id.state not in ["draft", "cancel"]
)
for invoice in cfdis:
# Ensure we can cancel this invoice
invoice.check_if_can_cancel()
# If l10n_mx_edi_original_invoice is set save uuid to send info to PAC
# while cancel invoice
invoice.cfdi_id.substitute_cfdi_uuid = (
invoice.l10n_mx_edi_original_invoice.cfdi_id.uuid
invoice.message_post(
body=_("Cancellation request sent")
)
cancelacion = invoice.cancel_cfdi()[0]
cancelacion = invoice.cancel_cfdi()
if cancelacion:
# CFDI cancelled (cancelacion == True) must cancel invoice too
super(AccountMove, invoice).action_cancel()
invoice.button_draft()
invoice.button_cancel()
elif cancelacion is None:
# CFDI set to approval (cancelacion == None) must set invoice
# to waiting too
invoice.write({"state": "waiting"})
invoice.write({"state": "posted"})
invoice.message_post(
body=_("Awaiting cancellation")
)
elif cancelacion is False:
# CFDI cancel denied (cancelacion == False) must get back invoice
# to open state
self.undo_waiting_state()
# Call super only with invoices without CFDI
invoices = self - cfdis
return super(AccountMove, invoices).action_cancel()
invoice.message_post(
body=_("Denied cancellation")
)
def undo_waiting_state(self):
"""When cancel is negate revert invoice to open and post account_move"""
for record in self:
to_update = record.filtered(lambda i: i.state == "waiting")
to_update.write({"state": "open"})
to_update = record.filtered(lambda i: i.cfdi_state == "waiting")
to_update.write({"state": "posted"})
to_update.mapped("move_id").post()
@api.depends("state", "cfdi_state")
def _compute_show_reset_to_draft_button(self):
super()._compute_show_reset_to_draft_button()
for move in self:
if move.state in ("posted", "cancel") and move.cfdi_state in ("signed", "done", "waiting", "cancel"):
move.show_reset_to_draft_button = False
def action_consult_cancellation_status(self):
"""Verify cancellation status"""
# TODO: Is this really needed? Maybe we can reuse the action_cancel
......@@ -520,7 +523,6 @@ class AccountMove(models.Model):
return res
class AccountMoveLine(models.Model):
_inherit = "account.move.line"
......
......@@ -31,6 +31,15 @@
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<button name="button_cancel" position="before">
<button name="l10n_mx_action_cancel"
string="Cancel CFDI"
type="object"
class="btn-primary"
attrs="{'invisible': [
'&amp;',
('state', '!=', 'posted'),
('cfdi_state', '!=', 'done')
]}"/>
<field name="is_cfdi_candidate" invisible="1" />
</button>
<xpath expr="//header" position="after">
......