Skip to content

Commit

Permalink
IMP l10n_it_fatturapa_pec allowing user to configure contacts to be n…
Browse files Browse the repository at this point in the history
…otified when a non e-invoice PEC is received by fetchmail (OCA#733)
  • Loading branch information
eLBati authored and TheMule71 committed Jul 2, 2021
1 parent 831a07d commit 408b978
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
10 changes: 10 additions & 0 deletions l10n_it_fatturapa_pec/models/fetchmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@

class Fetchmail(models.Model):
_inherit = 'fetchmail.server'

def _default_e_inv_notify_partner_ids(self):
return [(6, 0, [self.env.user.partner_id.id])]

last_pec_error_message = fields.Text(
"Last PEC Error Message", readonly=True)
e_inv_notify_partner_ids = fields.Many2many(
"res.partner", string="Contacts to notify",
help="Contacts to notify when PEC message can't be processed",
domain=[('email', '!=', False)],
default=_default_e_inv_notify_partner_ids
)

@api.multi
def fetch_mail(self):
Expand Down
48 changes: 42 additions & 6 deletions l10n_it_fatturapa_pec/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import zipfile
import io

from odoo import api, models
from odoo import api, models, _

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,21 +97,57 @@ def message_route(self, message, message_dict, model=None, thread_id=None,
return []

elif self._context.get('fetchmail_server_id', False):
fetchmail_server_id = self.env['fetchmail.server'].browse(
# This is not an email coming from SDI
fetchmail_server = self.env['fetchmail.server'].browse(
self._context['fetchmail_server_id'])
if fetchmail_server_id.is_fatturapa_pec:
if fetchmail_server.is_fatturapa_pec:
att = self.find_attachment_by_subject(message_dict['subject'])
if att:
# This a PEC response (CONSEGNA o ACCETTAZIONE)
# related to a message sent to SDI by us
message_dict['model'] = 'fatturapa.attachment.out'
message_dict['res_id'] = att.id
self.clean_message_dict(message_dict)
self.env['mail.message'].with_context(
message_create_from_mail_mail=True).create(
message_dict)
else:
# todo send email for non-routable pec mail
_logger.error('Can\'t route PEC E-Mail with Message-Id: {}'
.format(message.get('Message-Id')))
_logger.info(
'Can\'t route PEC E-Mail with Message-Id: {}'.format(
message.get('Message-Id'))
)
if fetchmail_server.e_inv_notify_partner_ids:
self.env['mail.mail'].create({
'subject': _(
"PEC message [%s] not processed"
) % message.get('Subject'),
'body_html': _(
"<p>"
"PEC message with Message-Id %s has been read "
"but not processed, as not related to an "
"e-invoice.</p>"
"<p>Please check PEC mailbox %s, at server %s,"
" with user %s</p>"
) % (
message.get('Message-Id'),
fetchmail_server.name, fetchmail_server.server,
fetchmail_server.user
),
'recipient_ids': [(
6, 0,
fetchmail_server.e_inv_notify_partner_ids.ids
)]
})
_logger.info(
'Notifying partners %s about message with '
'Message-Id: %s' % (
fetchmail_server.e_inv_notify_partner_ids.ids,
message.get('Message-Id')))
else:
_logger.error(
'Can\'t notify anyone about not processed '
'PEC E-Mail with Message-Id: {}'.format(
message.get('Message-Id')))
return []

return super(MailThread, self).message_route(
Expand Down
4 changes: 4 additions & 0 deletions l10n_it_fatturapa_pec/views/fetchmail_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<!-- Hiding, because it would not be considered. See 'fetch_mail' override -->
<attribute name="attrs">{'invisible': [('is_fatturapa_pec', '=', True)]}</attribute>
</xpath>
<field name="active" position="after">
<field name="e_inv_notify_partner_ids" widget="many2many_tags"
attrs="{'invisible': [('is_fatturapa_pec', '=', False)]}"/>
</field>
</field>
</record>
</odoo>

0 comments on commit 408b978

Please sign in to comment.