
from odoo import api, models


class FiscalDocument(models.Model):
    _inherit = 'l10n_br_fiscal.document'

    @api.multi
    def unlink(self):
        invoices = self.env['account.invoice'].search(
            [('fiscal_document_id', 'in', self.ids)])
        invoices.unlink()
        return super().unlink()


from odoo import _, api, models
from odoo.exceptions import UserError

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
    SITUACAO_EDOC_EM_DIGITACAO,
)


class FiscalDocument(models.Model):
    _inherit = 'l10n_br_fiscal.document'

    @api.multi
    def unlink(self):
        draft_documents = self.filtered(
            lambda d: d.state == SITUACAO_EDOC_EM_DIGITACAO)

        if draft_documents:
            UserError(_("You cannot delete a fiscal document "
                        "which is not draft state."))

        invoices = self.env['account.invoice'].search(
            [('fiscal_document_id', 'in', self.ids)])
        invoices.unlink()
        return super().unlink()

Allow delete only fiscal documents with draft state
[REF] Allow delete only fiscal documents with draft state

  
- from odoo import api, models
+ from odoo import _, api, models
+ from odoo.exceptions import UserError
+ 
+ from odoo.addons.l10n_br_fiscal.constants.fiscal import (
+     SITUACAO_EDOC_EM_DIGITACAO,
+ )
  
  
  class FiscalDocument(models.Model):
      _inherit = 'l10n_br_fiscal.document'
  
      @api.multi
      def unlink(self):
+         draft_documents = self.filtered(
+             lambda d: d.state == SITUACAO_EDOC_EM_DIGITACAO)
+ 
+         if draft_documents:
+             UserError(_("You cannot delete a fiscal document "
+                         "which is not draft state."))
+ 
          invoices = self.env['account.invoice'].search(
              [('fiscal_document_id', 'in', self.ids)])
          invoices.unlink()
          return super().unlink()
  
Allow delete only fiscal documents with draft state
## Code Before:

from odoo import api, models


class FiscalDocument(models.Model):
    _inherit = 'l10n_br_fiscal.document'

    @api.multi
    def unlink(self):
        invoices = self.env['account.invoice'].search(
            [('fiscal_document_id', 'in', self.ids)])
        invoices.unlink()
        return super().unlink()

## Instruction:
Allow delete only fiscal documents with draft state
## Code After:

from odoo import _, api, models
from odoo.exceptions import UserError

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
    SITUACAO_EDOC_EM_DIGITACAO,
)


class FiscalDocument(models.Model):
    _inherit = 'l10n_br_fiscal.document'

    @api.multi
    def unlink(self):
        draft_documents = self.filtered(
            lambda d: d.state == SITUACAO_EDOC_EM_DIGITACAO)

        if draft_documents:
            UserError(_("You cannot delete a fiscal document "
                        "which is not draft state."))

        invoices = self.env['account.invoice'].search(
            [('fiscal_document_id', 'in', self.ids)])
        invoices.unlink()
        return super().unlink()
