Skip to content

Commit

Permalink
[IMP] l10n_it_sdi_channel: Allow user to be notified of new e-bills o…
Browse files Browse the repository at this point in the history
…r e-invoice notifications
  • Loading branch information
SirTakobi committed Jul 21, 2022
1 parent 01f02a2 commit 3ad7f21
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 6 deletions.
1 change: 1 addition & 0 deletions l10n_it_sdi_channel/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"data": [
"security/ir.model.access.csv",
"security/security.xml",
"data/mail_message_subtype_data.xml",
"views/sdi_view.xml",
"views/company_view.xml",
"views/fatturapa_attachment_views.xml",
Expand Down
128 changes: 128 additions & 0 deletions l10n_it_sdi_channel/data/mail_message_subtype_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright 2022 Simone Rubino - TAKOBI
~ License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>
<!-- Subtypes for e-invoice -->
<record id="e_invoice_ready" model="mail.message.subtype">
<field name="name">Ready to Send</field>
<field name="res_model">fatturapa.attachment.out</field>
<field name="default" eval="False"/>
<field name="description">E-invoice Ready to Send</field>
</record>

<record id="e_invoice_sent" model="mail.message.subtype">
<field name="name">Sent</field>
<field name="res_model">fatturapa.attachment.out</field>
<field name="default" eval="False"/>
<field name="description">E-invoice Sent</field>
</record>

<record id="e_invoice_sender_error" model="mail.message.subtype">
<field name="name">Sender Error</field>
<field name="res_model">fatturapa.attachment.out</field>
<field name="default" eval="True"/>
<field name="description">E-invoice Sender Error</field>
</record>

<record id="e_invoice_recipient_error" model="mail.message.subtype">
<field name="name">Not delivered</field>
<field name="res_model">fatturapa.attachment.out</field>
<field name="default" eval="False"/>
<field name="description">E-invoice Not Delivered</field>
</record>

<record id="e_invoice_rejected" model="mail.message.subtype">
<field name="name">Rejected (PA)</field>
<field name="res_model">fatturapa.attachment.out</field>
<field name="default" eval="False"/>
<field name="description">E-invoice Rejected (PA)</field>
</record>

<record id="e_invoice_validated" model="mail.message.subtype">
<field name="name">Delivered</field>
<field name="res_model">fatturapa.attachment.out</field>
<field name="default" eval="False"/>
<field name="description">E-invoice Delivered</field>
</record>

<record id="e_invoice_accepted" model="mail.message.subtype">
<field name="name">Accepted</field>
<field name="res_model">fatturapa.attachment.out</field>
<field name="default" eval="False"/>
<field name="description">E-invoice Accepted</field>
</record>

<!-- Subtypes for e-bill -->
<record id="e_bill_received" model="mail.message.subtype">
<field name="name">E-bill received</field>
<field name="res_model">fatturapa.attachment.in</field>
<field name="default" eval="True"/>
<field name="description">E-bill received</field>
</record>

<!-- Subtypes for SdI channel -->
<record id="sdi_channel_e_invoice_ready" model="mail.message.subtype">
<field name="name">E-invoice Ready to Send</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_invoice_ready')"/>
<field name="relation_field">channel_id</field>
<field name="default" eval="False"/>
</record>

<record id="sdi_channel_e_invoice_sent" model="mail.message.subtype">
<field name="name">E-invoice Sent</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_invoice_sent')"/>
<field name="relation_field">channel_id</field>
<field name="default" eval="False"/>
</record>

<record id="sdi_channel_e_invoice_sender_error" model="mail.message.subtype">
<field name="name">E-invoice Sender Error</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_invoice_sender_error')"/>
<field name="relation_field">channel_id</field>
<field name="default" eval="True"/>
</record>

<record id="sdi_channel_e_invoice_recipient_error" model="mail.message.subtype">
<field name="name">E-invoice Not Delivered</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_invoice_recipient_error')"/>
<field name="relation_field">channel_id</field>
<field name="default" eval="False"/>
</record>

<record id="sdi_channel_e_invoice_rejected" model="mail.message.subtype">
<field name="name">E-invoice Rejected (PA)</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_invoice_rejected')"/>
<field name="relation_field">channel_id</field>
<field name="default" eval="False"/>
</record>

<record id="sdi_channel_e_invoice_validated" model="mail.message.subtype">
<field name="name">E-invoice Delivered</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_invoice_validated')"/>
<field name="relation_field">channel_id</field>
<field name="default" eval="False"/>
</record>

<record id="sdi_channel_e_invoice_accepted" model="mail.message.subtype">
<field name="name">E-invoice Accepted</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_invoice_accepted')"/>
<field name="relation_field">channel_id</field>
<field name="default" eval="False"/>
</record>

<record id="sdi_channel_e_bill_received" model="mail.message.subtype">
<field name="name">E-bill received</field>
<field name="res_model">sdi.channel</field>
<field name="parent_id" eval="ref('e_bill_received')"/>
<field name="relation_field">channel_id</field>
</record>
</odoo>
3 changes: 2 additions & 1 deletion l10n_it_sdi_channel/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import fatturapa_attachment_out
from . import company
from . import fatturapa_attachment
from . import fatturapa_attachment_out
from . import sdi
12 changes: 12 additions & 0 deletions l10n_it_sdi_channel/models/fatturapa_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2022 Simone Rubino - TAKOBI
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class FatturaPAAttachment (models.AbstractModel):
_inherit = 'fatturapa.attachment'

channel_id = fields.Many2one(
comodel_name='sdi.channel',
)
35 changes: 35 additions & 0 deletions l10n_it_sdi_channel/models/fatturapa_attachment_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
from odoo import _, api, fields, models
from odoo.exceptions import UserError

STATE_SUBTYPE_MAPPING = {
'ready': 'l10n_it_sdi_channel.e_invoice_ready',
'sent': 'l10n_it_sdi_channel.e_invoice_sent',
'sender_error': 'l10n_it_sdi_channel.e_invoice_sender_error',
'recipient_error': 'l10n_it_sdi_channel.e_invoice_recipient_error',
'rejected': 'l10n_it_sdi_channel.e_invoice_rejected',
'validated': 'l10n_it_sdi_channel.e_invoice_validated',
'accepted': 'l10n_it_sdi_channel.e_invoice_accepted',
}
"""
Map each state of fatturapa.attachment.out to
the mail.message.subtype that should be used to notify the user.
"""


class FatturaPAAttachmentOut (models.Model):
_inherit = 'fatturapa.attachment.out'
Expand All @@ -26,3 +40,24 @@ def send_to_sdi(self):
sdi_channel = company.sdi_channel_id
send_result = sdi_channel.send(self)
return send_result

@api.multi
def _track_subtype(self, init_values):
self.ensure_one()
if 'state' in init_values:
state_subtype = STATE_SUBTYPE_MAPPING.get(self.state)
if state_subtype:
return state_subtype
return super()._track_subtype(init_values)

@api.model_create_multi
def create(self, vals_list):
attachments = super().create(vals_list)
sdi_channel = self.env.user.company_id.sdi_channel_id
if sdi_channel:
# Link the attachments to the channel
# to inherit channel's followers
attachments.update({
'channel_id': sdi_channel.id,
})
return attachments
24 changes: 19 additions & 5 deletions l10n_it_sdi_channel/models/sdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from lxml import etree

from odoo import api, models, fields
from odoo import _, api, models, fields
from odoo.fields import first

FATTURAPA_IN_REGEX = '^(IT[a-zA-Z0-9]{11,16}|'\
Expand All @@ -25,6 +25,7 @@

class SdiChannel(models.Model):
_name = "sdi.channel"
_inherit = "mail.thread"
_description = "ES channel"

name = fields.Char(string='Name', required=True)
Expand Down Expand Up @@ -57,7 +58,6 @@ def send(self, attachment_out_ids):
send_method = getattr(self, send_method_name)
return send_method(attachment_out_ids)

@api.model
def _prepare_attachment_in_values(
self,
file_name,
Expand Down Expand Up @@ -94,9 +94,15 @@ def _prepare_attachment_in_values(
'datas_fname': file_name,
'datas': base64.encodebytes(file_content),
})
if len(self) == 1:
# Link the attachment to the channel
# to inherit channel's followers
attachment_values.update({
'channel_id': self.id,
})

return attachment_values

@api.model
def _process_single_fe(
self,
file_name,
Expand Down Expand Up @@ -137,7 +143,6 @@ def _process_single_fe(

return attachments_values

@api.model
def receive_fe(
self,
file_name_content_dict,
Expand Down Expand Up @@ -179,7 +184,16 @@ def receive_fe(
e_invoice_user = company.e_invoice_user_id
if e_invoice_user:
attachment_model = attachment_model.sudo(e_invoice_user.id)
return attachment_model.create(all_attachments_values)

attachments = attachment_model.create(all_attachments_values)
for attachment in attachments:
attachment.message_post(
subject=_('Received new e-bill: {att_name}').format(
att_name=attachment.att_name,
),
subtype='l10n_it_sdi_channel.e_bill_received',
)
return attachments

@api.model
def _search_attachment_out_by_notification(
Expand Down
6 changes: 6 additions & 0 deletions l10n_it_sdi_channel/views/sdi_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<newline/>
</group>
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids"
widget="mail_followers"
help="Follow this channel to automatically be notified of the events associated to attachments and notifications received by this channel."
/>
</div>
</form>
</field>
</record>
Expand Down

0 comments on commit 3ad7f21

Please sign in to comment.