[FIX]l10n_it_edi: update to new account_edi

edi_document_ids was ir.attachment but is now account.edi.document.

See https://github.com/odoo/odoo/pull/52407

closes odoo/odoo#59540

X-original-commit: 6a5c19d8011a5b480cbd3bccee3d2ab409807258
Signed-off-by: Laurent Smet <smetl@users.noreply.github.com>
Signed-off-by: bfr-o <bfr-o@users.noreply.github.com>
This commit is contained in:
Benjamin Frantzen (bfr)
2020-10-08 15:46:28 +00:00
parent fb17680703
commit a900f96619
3 changed files with 73 additions and 69 deletions
@@ -20,16 +20,54 @@ DEFAULT_FACTUR_ITALIAN_DATE_FORMAT = '%Y-%m-%d'
class AccountEdiFormat(models.Model):
_inherit = 'account.edi.format'
# -------------------------------------------------------------------------
# Export
# -------------------------------------------------------------------------
def _is_embedding_to_invoice_pdf_needed(self):
# OVERRIDE
self.ensure_one()
return True if self.code == 'fattura_pa' else super()._is_embedding_to_invoice_pdf_needed()
def _is_compatible_with_journal(self, journal):
# OVERRIDE
self.ensure_one()
if self.code != 'fattura_pa':
return super()._is_compatible_with_journal(journal)
return False # edi does not support generic export
return journal.type == 'sale' and journal.country_code == 'IT'
def _is_required_for_invoice(self, invoice):
# OVERRIDE
self.ensure_one()
if self.code != 'fattura_pa':
return super()._is_required_for_invoice(invoice)
# Determine on which invoices the Mexican CFDI must be generated.
return invoice.is_sale_document() and invoice.l10n_it_send_state not in ['sent', 'delivered', 'delivered_accepted'] and invoice.country_code == 'IT'
def _post_invoice_edi(self, invoices, test_mode=False):
# OVERRIDE
self.ensure_one()
edi_result = super()._post_invoice_edi(invoices, test_mode=test_mode)
if self.code != 'fattura_pa':
return edi_result
invoice = invoices # no batching ensure that we only have one invoice
invoice.l10n_it_send_state = 'other'
invoice._check_before_xml_exporting()
res = invoice.invoice_generate_xml()
if len(invoice.commercial_partner_id.l10n_it_pa_index or '') == 6:
invoice.message_post(
body=(_("Invoices for PA are not managed by Odoo, you can download the document and send it on your own."))
)
else:
invoice.l10n_it_send_state = 'to_send'
invoice.send_pec_mail()
return {invoice: res}
# -------------------------------------------------------------------------
# Import
# -------------------------------------------------------------------------
def _check_filename_is_fattura_pa(self, filename):
return re.search("([A-Z]{2}[A-Za-z0-9]{2,28}_[A-Za-z0-9]{0,5}.(xml.p7m|xml))", filename)
+34 -64
View File
@@ -47,46 +47,16 @@ class AccountMove(models.Model):
l10n_it_einvoice_id = fields.Many2one('ir.attachment', string="Electronic invoice", compute='_compute_l10n_it_einvoice')
@api.depends('edi_document_ids')
@api.depends('edi_document_ids', 'edi_document_ids.attachment_id')
def _compute_l10n_it_einvoice(self):
fattura_pa = self.env.ref('l10n_it_edi.edi_fatturaPA')
for invoice in self:
einvoice = invoice.edi_document_ids.filtered(lambda d: d.edi_format_id == fattura_pa)
invoice.l10n_it_einvoice_id = einvoice
invoice.l10n_it_einvoice_name = einvoice.name
def _post(self, soft=True):
# OVERRIDE
posted = super()._post(soft)
# Retrieve invoices to generate the xml.
invoices_to_export = posted.filtered(lambda move:
move.company_id.country_id == self.env.ref('base.it') and
move.is_sale_document() and
move.l10n_it_send_state not in ['sent', 'delivered', 'delivered_accepted'])
invoices_to_export.write({'l10n_it_send_state': 'other'})
invoices_to_send = self.env['account.move']
invoices_other = self.env['account.move']
for invoice in invoices_to_export:
invoice._check_before_xml_exporting()
invoice.invoice_generate_xml()
if len(invoice.commercial_partner_id.l10n_it_pa_index or '') == 6:
invoice.message_post(
body=(_("Invoices for PA are not managed by Odoo, you can download the document and send it on your own."))
)
invoices_other += invoice
else:
invoices_to_send += invoice
invoices_other.write({'l10n_it_send_state': 'other'})
invoices_to_send.write({'l10n_it_send_state': 'to_send'})
for invoice in invoices_to_send:
invoice.send_pec_mail()
return posted
invoice.l10n_it_einvoice_id = einvoice.attachment_id
invoice.l10n_it_einvoice_name = einvoice.attachment_id.name
def _check_before_xml_exporting(self):
self.ensure_one()
seller = self.company_id
buyer = self.commercial_partner_id
@@ -149,40 +119,40 @@ class AccountMove(models.Model):
raise ValidationError(_("%s has an amount of 0.0, you must indicate the kind of exoneration.", tax_line.name))
def invoice_generate_xml(self):
for invoice in self:
if invoice.l10n_it_einvoice_id and invoice.l10n_it_send_state not in ['invalid', 'to_send']:
raise UserError(_("You can't regenerate an E-Invoice when the first one is sent and there are no errors"))
if invoice.l10n_it_einvoice_id:
invoice.l10n_it_einvoice_id.unlink()
self.ensure_one()
if self.l10n_it_einvoice_id and self.l10n_it_send_state not in ['invalid', 'to_send']:
return {'error': _("You can't regenerate an E-Invoice when the first one is sent and there are no errors")}
if self.l10n_it_einvoice_id:
self.l10n_it_einvoice_id.unlink()
a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
n = invoice.id
progressive_number = ""
while n:
(n,m) = divmod(n,len(a))
progressive_number = a[m] + progressive_number
a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
n = self.id
progressive_number = ""
while n:
(n,m) = divmod(n,len(a))
progressive_number = a[m] + progressive_number
report_name = '%(country_code)s%(codice)s_%(progressive_number)s.xml' % {
'country_code': invoice.company_id.country_id.code,
'codice': invoice.company_id.l10n_it_codice_fiscale,
'progressive_number': progressive_number.zfill(5),
}
report_name = '%(country_code)s%(codice)s_%(progressive_number)s.xml' % {
'country_code': self.company_id.country_id.code,
'codice': self.company_id.l10n_it_codice_fiscale,
'progressive_number': progressive_number.zfill(5),
}
data = b"<?xml version='1.0' encoding='UTF-8'?>" + invoice._export_as_xml()
description = _('Italian invoice: %s', invoice.move_type)
invoice.edi_document_ids = self.env['ir.attachment'].create({
'name': report_name,
'res_id': invoice.id,
'res_model': invoice._name,
'datas': base64.encodebytes(data),
'description': description,
'type': 'binary',
'edi_format_id': self.env.ref('l10n_it_edi.edi_fatturaPA').id
})
data = b"<?xml version='1.0' encoding='UTF-8'?>" + self._export_as_xml()
description = _('Italian invoice: %s', self.move_type)
attachment = self.env['ir.attachment'].create({
'name': report_name,
'res_id': self.id,
'res_model': self._name,
'datas': base64.encodebytes(data),
'description': description,
'type': 'binary',
})
invoice.message_post(
body=(_("E-Invoice is generated on %s by %s") % (fields.Datetime.now(), self.env.user.display_name))
)
self.message_post(
body=(_("E-Invoice is generated on %s by %s") % (fields.Datetime.now(), self.env.user.display_name))
)
return {'attachment': attachment}
def _export_as_xml(self):
''' Create the xml file content.
@@ -151,10 +151,6 @@
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<data>
<xpath expr="//button[@name='preview_invoice']" position="after">
<button name="post" type="object" icon="fa-envelope-o" class="btn btn-primary" string="Resend"
attrs="{'invisible': ['|', ('move_type', 'not in', ('out_invoice', 'out_refund')), ('l10n_it_send_state', '!=', 'invalid')]}"/>
</xpath>
<xpath expr="//field[@name='move_type']" position="before">
<div class="alert alert-success" role="alert"
attrs="{'invisible': ['|', ('move_type', 'not in', ('out_invoice', 'out_refund')), ('l10n_it_send_state', 'not in', ['delivered', 'delivered_accepted', 'delivered_refused', 'delivered_expired', 'failed_delivery'])]}">