<commit_before>from odoo import models, fields, api, _
import ftplib
import io
import base64


class AccountInvoice(models.Model):

    _inherit = 'account.invoice'

    @api.multi
    def send_via_edi(self):
        for invoice in self:
            edi_msg = self.env['edif.message'].generate_invoice(invoice)
            filename = 'VT-{}.EDI'.format(invoice.number.replace("/", "-"))

            fileb = io.BytesIO()
            fileb.write(edi_msg.encode('latin-1'))
            fileb.seek(0)  # This allows to reads from the start of the stream

            ftp_dir = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_dir')
            ftp_user = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_user')
            ftp_pass = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_pass')
            ftp_folder_out = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_folder_out')

            ftp = ftplib.FTP(ftp_dir)
            ftp.login(user=ftp_user, passwd=ftp_<PASSWORD>)
            ftp.cwd(ftp_folder_out)

            response = ftp.storbinary("STOR %s" % filename, fileb)

            if not response.startswith('226'):
                self.send_edi_error_mail()

            ftp.quit()

            ctx = {}
            self.env['ir.attachment'].with_context(ctx).create({
                'name': filename,
                'res_id': invoice.id,
                'res_model': str(invoice._name),
                'datas': base64.b64encode(fileb.getvalue()),
                'datas_fname': filename,
                'type': 'binary',
            })

    @api.multi
    def invoice_validate(self):
        res = super(AccountInvoice, self).invoice_validate()
        for invoice in self.filtered(lambda i: i.partner_id.commercial_partner_id.edi_enable):
            invoice.send_via_edi()

        return res

    @api.multi
    def send_edi_error_mail(self):
        mail_pool = self.env['mail.mail']
        context = self._context.copy()
        context.pop('default_state', False)

        template_id = self.env.ref('edi_edifact.email_template_edi_send_error')

        if template_id:
            mail_id = template_id.with_context(context).send_mail(self.id)
            if mail_id:
                mail_id_check = mail_pool.browse(mail_id)
                mail_id_check.with_context(context).send()

        return True
<commit_msg>[FIX]edi_bricomart: añadida excepción de permisos<commit_after>from odoo import models, fields, api, _
import ftplib
import io
import base64


class AccountInvoice(models.Model):

    _inherit = 'account.invoice'

    @api.multi
    def send_via_edi(self):
        for invoice in self:
            edi_msg = self.env['edif.message'].generate_invoice(invoice)
            filename = 'VT-{}.EDI'.format(invoice.number.replace("/", "-"))

            fileb = io.BytesIO()
            fileb.write(edi_msg.encode('latin-1'))
            fileb.seek(0)  # This allows to reads from the start of the stream

            ftp_dir = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_dir')
            ftp_user = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_user')
            ftp_pass = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_pass')
            ftp_folder_out = self.env['ir.config_parameter'].sudo().get_param('ftp_edi_folder_out')

            ftp = ftplib.FTP(ftp_dir)
            ftp.login(user=ftp_user, passwd=ftp_pass)
            ftp.cwd(ftp_folder_out)
            try:
                ftp.storbinary("STOR %s" % filename, fileb)
            except ftplib.error_perm:
                self.send_edi_error_mail()

            ftp.quit()

            ctx = {}
            self.env['ir.attachment'].with_context(ctx).create({
                'name': filename,
                'res_id': invoice.id,
                'res_model': str(invoice._name),
                'datas': base64.b64encode(fileb.getvalue()),
                'datas_fname': filename,
                'type': 'binary',
            })

    @api.multi
    def invoice_validate(self):
        res = super(AccountInvoice, self).invoice_validate()
        for invoice in self.filtered(lambda i: i.partner_id.commercial_partner_id.edi_enable):
            invoice.send_via_edi()

        return res

    @api.multi
    def send_edi_error_mail(self):
        mail_pool = self.env['mail.mail']
        context = self._context.copy()
        context.pop('default_state', False)

        template_id = self.env.ref('edi_edifact.email_template_edi_send_error')

        if template_id:
            mail_id = template_id.with_context(context).send_mail(self.id)
            if mail_id:
                mail_id_check = mail_pool.browse(mail_id)
                mail_id_check.with_context(context).send()

        return True

{'fraction_non_alphanumeric': 0.1208515967438948, 'fraction_numerical': 0.003965769150490503, 'mean_word_length': 2.240027045300879, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757839', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1460, 'n_words': 233}