diff --git a/addons/l10n_it_edi/models/account_edi_format.py b/addons/l10n_it_edi/models/account_edi_format.py index 2b53e09b322..380b4d0014e 100644 --- a/addons/l10n_it_edi/models/account_edi_format.py +++ b/addons/l10n_it_edi/models/account_edi_format.py @@ -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) diff --git a/addons/l10n_it_edi/models/account_invoice.py b/addons/l10n_it_edi/models/account_invoice.py index 6ad986994a9..b84b5f67747 100644 --- a/addons/l10n_it_edi/models/account_invoice.py +++ b/addons/l10n_it_edi/models/account_invoice.py @@ -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"" + 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"" + 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. diff --git a/addons/l10n_it_edi/views/l10n_it_view.xml b/addons/l10n_it_edi/views/l10n_it_view.xml index e002a945479..95e78acea4b 100644 --- a/addons/l10n_it_edi/views/l10n_it_view.xml +++ b/addons/l10n_it_edi/views/l10n_it_view.xml @@ -151,10 +151,6 @@ - -