Skip to content

Commit

Permalink
IMP l10n_it_sdi_channel interface
Browse files Browse the repository at this point in the history
ADD mechanism to check SDI PEC address after first sending
ADD check valid PEC incoming server
  • Loading branch information
eLBati committed Dec 5, 2018
1 parent d7bd83a commit c168f04
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 6 deletions.
14 changes: 14 additions & 0 deletions l10n_it_fatturapa_pec/models/fatturapa_attachment_out.py
Expand Up @@ -46,8 +46,20 @@ def reset_to_ready(self):
raise UserError(_("Yo can only reset 'sender error' files"))
att.state = 'ready'

@api.model
def _check_fetchmail(self):
server = self.env['fetchmail.server'].search([
('is_fatturapa_pec', '=', True),
('state', '=', 'done')
])
if not server:
raise UserError(_(
"No incoming PEC server found. Please configure it."))

@api.multi
def send_via_pec(self):
self._check_fetchmail()
self.env.user.company_id.sdi_channel_id.check_first_pec_sending()
states = self.mapped('state')
if set(states) != set(['ready']):
raise UserError(_("You can only send 'ready to send' files"))
Expand Down Expand Up @@ -94,6 +106,8 @@ def send_via_pec(self):
att.state = 'sent'
att.sending_date = fields.Datetime.now()
att.sending_user = self.env.user.id
self.env.user.company_id.sdi_channel_id.\
update_after_first_pec_sending()
except MailDeliveryException as e:
att.state = 'sender_error'
mail.body = str(e)
Expand Down
1 change: 1 addition & 0 deletions l10n_it_sdi_channel/__manifest__.py
Expand Up @@ -26,5 +26,6 @@
"views/company_view.xml",
'views/fetchmail_server.xml',
'views/ir_mail_server.xml',
"data/config_parameter.xml"
],
}
9 changes: 9 additions & 0 deletions l10n_it_sdi_channel/data/config_parameter.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="sdi_pec_first_address" model="ir.config_parameter">
<field name="key">sdi.pec.first.address</field>
<field name="value">sdi01@pec.fatturapa.it</field>
</record>
</data>
</odoo>
13 changes: 12 additions & 1 deletion l10n_it_sdi_channel/models/ir_mail_server.py
@@ -1,7 +1,8 @@
# Copyright 2018 Sergio Corato (https://efatto.it)
# Copyright 2018 Lorenzo Battistini
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models, fields
from odoo import models, fields, api


class IrMailServer(models.Model):
Expand All @@ -10,3 +11,13 @@ class IrMailServer(models.Model):
is_fatturapa_pec = fields.Boolean("FatturaPA PEC server")
email_from_for_fatturaPA = fields.Char(
"Sender Email Address")

@api.multi
def test_smtp_connection(self):
self.ensure_one()
if self.is_fatturapa_pec:
# self.env.user.email is used to test SMTP connection
self.env.user.email = self.email_from_for_fatturaPA
# no need to revert to correct email: UserError is always raised and
# rollback done
return super(IrMailServer, self).test_smtp_connection()
39 changes: 36 additions & 3 deletions l10n_it_sdi_channel/models/sdi.py
Expand Up @@ -15,7 +15,7 @@ class SdiChannel(models.Model):
_name = "sdi.channel"
_description = "SdI channel"

name = fields.Char(string='Name', required=True)
name = fields.Char(string='Name', required=True, default=_("PEC"))
company_id = fields.Many2one(
'res.company', string='Company', required=True,
default=lambda self:
Expand All @@ -28,11 +28,21 @@ class SdiChannelPEC(models.Model):
channel_type = fields.Selection(
string='SdI channel type', selection=SDI_CHANNELS, required=True,
help='PEC is the only implemented channel in this module. Other '
'channels (Web, Sftp) could be provided by external modules.')
'channels (Web, Sftp) could be provided by external modules.',
default='pec')
pec_server_id = fields.Many2one(
'ir.mail_server', string='Pec mail server', required=False,
domain=[('is_fatturapa_pec', '=', True)])
email_exchange_system = fields.Char("Exchange System Email Address")
email_exchange_system = fields.Char(
"Exchange System Email Address",
help="The first time you send a PEC to SDI, you must use the address "
"sdi01@pec.fatturapa.it . The system, with the first response "
"or notification, communicates the PEC address to be used for "
"future messages",
default=lambda self: self.env['ir.config_parameter'].get_param(
'sdi.pec.first.address')
)
first_invoice_sent = fields.Boolean("First invoice sent", readonly=True)

@api.constrains('pec_server_id')
def check_pec_server_id(self):
Expand All @@ -46,12 +56,35 @@ def check_pec_server_id(self):

@api.constrains('email_exchange_system')
def check_email_validity(self):
if self.env.context.get('skip_check_email_validity'):
return
for channel in self:
if not extract_rfc2822_addresses(channel.email_exchange_system):
raise exceptions.ValidationError(
_("Email %s is not valid")
% channel.email_exchange_system)

def check_first_pec_sending(self):
sdi_address = self.env['ir.config_parameter'].get_param(
'sdi.pec.first.address')
if not self.first_invoice_sent:
if self.email_exchange_system != sdi_address:
raise exceptions.UserError(_(
"This is a first sending but SDI address is different "
"from %s"
) % sdi_address)
else:
if not self.email_exchange_system:
raise exceptions.UserError(_(
"SDI PEC address not set. Please update it with the "
"address indicated by SDI after the first sending"))

def update_after_first_pec_sending(self):
if not self.first_invoice_sent:
self.first_invoice_sent = True
self.with_context(
skip_check_email_validity=True).email_exchange_system = False


class SdiChannelWEB(models.Model):
_inherit = "sdi.channel"
Expand Down
11 changes: 11 additions & 0 deletions l10n_it_sdi_channel/views/company_view.xml
Expand Up @@ -34,4 +34,15 @@
</xpath>
</field>
</record>

<record id="view_company_form_e_invoice_sdi" model="ir.ui.view">
<field name="name">view_company_form_e_invoice_sdi</field>
<field name="model">res.company</field>
<field name="inherit_id" ref="l10n_it_fatturapa.view_company_form_e_invoice"/>
<field name="arch" type="xml">
<field name="fatturapa_fiscal_position_id" position="after">
<field name="sdi_channel_id" required="1"/>
</field>
</field>
</record>
</odoo>
6 changes: 4 additions & 2 deletions l10n_it_sdi_channel/views/sdi_view.xml
Expand Up @@ -30,8 +30,10 @@
</group>
<newline/>
<group attrs="{'invisible': [('channel_type', '!=', 'pec')]}">
<field name="pec_server_id" context="{'default_is_fatturapa_pec': True}"/>
<field name="email_exchange_system"/>
<field name="pec_server_id" context="{'default_is_fatturapa_pec': True}"
attrs="{'required': [('channel_type', '=', 'pec')]}"/>
<field name="email_exchange_system"
attrs="{'required': [('channel_type', '=', 'pec')]}"/>
</group>
<!--<group attrs="{'invisible': [('channel_type', '!=', 'web')]}">-->
<!--<field name="web_server_address"/>-->
Expand Down

0 comments on commit c168f04

Please sign in to comment.