Skip to content

Commit

Permalink
[REF] l10n_br_fiscal: extract l10n_br_fiscal_notification
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi committed Aug 16, 2024
1 parent 6db0660 commit 067d480
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 109 deletions.
2 changes: 0 additions & 2 deletions l10n_br_fiscal/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"security/ir.model.access.csv",
# Data
# Some data is being loaded via post_init_hook in hook file
"data/l10n_br_fiscal_email_template.xml",
"data/l10n_br_fiscal_data.xml",
"data/uom_data.xml",
"data/uom_alternative_data.xml",
Expand Down Expand Up @@ -62,7 +61,6 @@
"views/product_genre_view.xml",
"views/document_type_view.xml",
"views/document_serie_view.xml",
"views/document_email_view.xml",
"views/simplified_tax_view.xml",
"views/simplified_tax_range_view.xml",
"views/operation_view.xml",
Expand Down
1 change: 0 additions & 1 deletion l10n_br_fiscal/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def post_init_hook(cr, registry):
"demo/fiscal_document_nfse_demo.xml",
"demo/fiscal_operation_demo.xml",
"demo/subsequent_operation_demo.xml",
"demo/l10n_br_fiscal_document_email.xml",
"demo/res_users_demo.xml",
"demo/icms_tax_definition_demo.xml",
]
Expand Down
73 changes: 0 additions & 73 deletions l10n_br_fiscal/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,36 +404,6 @@ def action_create_return(self):

return action

def _get_email_template(self, state):
self.ensure_one()
return self.document_type_id.document_email_ids.search(
[
"|",
("state_edoc", "=", False),
("state_edoc", "=", state),
("issuer", "=", self.issuer),
"|",
("document_type_id", "=", False),
("document_type_id", "=", self.document_type_id.id),
],
limit=1,
order="state_edoc, document_type_id",
).mapped("email_template_id")

def send_email(self, state):
self.ensure_one()
email_template = self._get_email_template(state)
if email_template:
email_template.with_context(
default_attachment_ids=self._get_mail_attachment()
).send_mail(self.id)

def _after_change_state(self, old_state, new_state):
self.ensure_one()
result = super()._after_change_state(old_state, new_state)
self.send_email(new_state)
return result

@api.onchange("fiscal_operation_id")
def _onchange_fiscal_operation_id(self):
result = super()._onchange_fiscal_operation_id()
Expand Down Expand Up @@ -510,46 +480,3 @@ def cancel_edoc(self):
"associated documents have already been authorized."
)
raise UserWarning(message)

def _get_mail_attachment(self):
self.ensure_one()
attachment_ids = []
if self.state_edoc == SITUACAO_EDOC_AUTORIZADA:
if self.file_report_id:
attachment_ids.append(self.file_report_id.id)
if self.authorization_file_id:
attachment_ids.append(self.authorization_file_id.id)
return attachment_ids

def action_send_email(self):
"""Open a window to compose an email, with the fiscal document_type
template message loaded by default
"""
self.ensure_one()
template = self._get_email_template(self.state)
compose_form = self.env.ref("mail.email_compose_message_wizard_form", False)
lang = self.env.context.get("lang")
if template and template.lang:
lang = template._render_template(template.lang, self._name, [self.id])
self = self.with_context(lang=lang)
ctx = dict(
default_model="l10n_br_fiscal.document",
default_res_id=self.id,
default_use_template=bool(template),
default_attachment_ids=self._get_mail_attachment(),
default_template_id=template and template.id or False,
default_composition_mode="comment",
model_description=self.document_type_id.name or self._name,
force_email=True,
)
return {
"name": _("Send Fiscal Document Email Notification"),
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "form",
"res_model": "mail.compose.message",
"views": [(compose_form.id, "form")],
"view_id": compose_form.id,
"target": "new",
"context": ctx,
}
4 changes: 2 additions & 2 deletions l10n_br_fiscal/views/document_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@
groups="l10n_br_fiscal.group_user"
attrs="{'invisible': [('state_edoc', '!=', 'autorizada')]}"
/>
<button
<!--button
name="action_send_email"
type="object"
string="Send Email"
groups="l10n_br_fiscal.group_user"
attrs="{'invisible': [('state_edoc', 'not in', ('autorizada', 'cancelada', 'denegada'))]}"
/>
/-->
<field
name="state_edoc"
widget="statusbar"
Expand Down
19 changes: 1 addition & 18 deletions l10n_br_fiscal/views/l10n_br_fiscal_action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -372,24 +372,7 @@
</field>
</record>

<!-- Fiscal Document Email -->
<record id="document_email_action" model="ir.actions.act_window">
<field name="name">Fiscal Document Email</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">l10n_br_fiscal.document.email</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="document_email_tree" />
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Add a new Fiscal Document Email Definition
</p><p>
All Document Email Definitions supported by the Brazilian
Fiscal module for Odoo.
</p>
</field>
</record>

<!-- Simplefied Tax -->
<!-- Simplified Tax -->
<record id="simplified_tax_action" model="ir.actions.act_window">
<field name="name">Simplified Tax</field>
<field name="type">ir.actions.act_window</field>
Expand Down
9 changes: 0 additions & 9 deletions l10n_br_fiscal/views/l10n_br_fiscal_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,6 @@
sequence="20"
/>

<!-- Fiscal Document Email -->
<menuitem
id="document_email_menu"
action="document_email_action"
groups="l10n_br_fiscal.group_manager"
parent="others_config_menu"
sequence="30"
/>

<!-- Comment -->
<menuitem
id="comment_menu"
Expand Down
1 change: 1 addition & 0 deletions l10n_br_fiscal_notification/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
28 changes: 28 additions & 0 deletions l10n_br_fiscal_notification/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

{
"name": "Fiscal Document Notifications",
"summary": "Define fiscal document notifications",
"category": "Localisation",
"license": "AGPL-3",
"author": "KMEE, Odoo Community Association (OCA)",
"maintainers": ["mileo"],
"website": "https://github.com/OCA/l10n-brazil",
"development_status": "Production/Stable",
"version": "14.0.1.0.0",
"depends": [
"l10n_br_fiscal",
],
"data": [
# Data
"data/l10n_br_fiscal_email_template.xml",
# Views
"views/document_email_view.xml",
# Actions
"views/l10n_br_fiscal_action.xml",
# Menus
"views/l10n_br_fiscal_menu.xml",
],
"demo": ["demo/l10n_br_fiscal_document_email.xml"],
"installable": True,
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<field name="issuer">company</field>
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template"
ref="l10n_br_fiscal_notification.fiscal_document_change_state_template"
/>
<field name="company_id" ref="l10n_br_base.empresa_lucro_presumido" />
</record>
Expand All @@ -22,7 +22,7 @@
<field name="issuer">company</field>
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template_nfse"
ref="l10n_br_fiscal_notification.fiscal_document_change_state_template_nfse"
/>
<field name="company_id" ref="l10n_br_base.empresa_lucro_presumido" />
</record>
Expand All @@ -34,7 +34,7 @@
<field name="issuer">company</field>
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template"
ref="l10n_br_fiscal_notification.fiscal_document_change_state_template"
/>
<field name="company_id" ref="l10n_br_base.empresa_simples_nacional" />
</record>
Expand All @@ -47,7 +47,7 @@
<field name="issuer">company</field>
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template_nfse"
ref="l10n_br_fiscal_notification.fiscal_document_change_state_template_nfse"
/>
<field name="company_id" ref="l10n_br_base.empresa_simples_nacional" />
</record>
Expand Down
1 change: 1 addition & 0 deletions l10n_br_fiscal_notification/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import document
81 changes: 81 additions & 0 deletions l10n_br_fiscal_notification/models/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (C) 2019 KMEE
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import models


class Document(models.Model):
_inherit = "l10n_br_fiscal.document"

def _get_email_template(self, state):
self.ensure_one()
return self.document_type_id.document_email_ids.search(
[
"|",
("state_edoc", "=", False),
("state_edoc", "=", state),
("issuer", "=", self.issuer),
"|",
("document_type_id", "=", False),
("document_type_id", "=", self.document_type_id.id),
],
limit=1,
order="state_edoc, document_type_id",
).mapped("email_template_id")

def send_email(self, state):
self.ensure_one()
email_template = self._get_email_template(state)
if email_template:
email_template.with_context(
default_attachment_ids=self._get_mail_attachment()
).send_mail(self.id)

def _after_change_state(self, old_state, new_state):
self.ensure_one()
result = super()._after_change_state(old_state, new_state)
self.send_email(new_state)
return result

def _get_mail_attachment(self):
self.ensure_one()
attachment_ids = []
if self.state_edoc == SITUACAO_EDOC_AUTORIZADA:
if self.file_report_id:
attachment_ids.append(self.file_report_id.id)
if self.authorization_file_id:
attachment_ids.append(self.authorization_file_id.id)
return attachment_ids

def action_send_email(self):
"""Open a window to compose an email, with the fiscal document_type
template message loaded by default
"""
self.ensure_one()
template = self._get_email_template(self.state)
compose_form = self.env.ref("mail.email_compose_message_wizard_form", False)
lang = self.env.context.get("lang")
if template and template.lang:
lang = template._render_template(template.lang, self._name, [self.id])
self = self.with_context(lang=lang)
ctx = dict(
default_model="l10n_br_fiscal.document",
default_res_id=self.id,
default_use_template=bool(template),
default_attachment_ids=self._get_mail_attachment(),
default_template_id=template and template.id or False,
default_composition_mode="comment",
model_description=self.document_type_id.name or self._name,
force_email=True,
)
return {
"name": _("Send Fiscal Document Email Notification"),
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "form",
"res_model": "mail.compose.message",
"views": [(compose_form.id, "form")],
"view_id": compose_form.id,
"target": "new",
"context": ctx,
}
21 changes: 21 additions & 0 deletions l10n_br_fiscal_notification/views/l10n_br_fiscal_action.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<!-- Fiscal Document Email -->
<record id="document_email_action" model="ir.actions.act_window">
<field name="name">Fiscal Document Email</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">l10n_br_fiscal.document.email</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="document_email_tree" />
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Add a new Fiscal Document Email Definition
</p><p>
All Document Email Definitions supported by the Brazilian
Fiscal module for Odoo.
</p>
</field>
</record>

</odoo>
13 changes: 13 additions & 0 deletions l10n_br_fiscal_notification/views/l10n_br_fiscal_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<!-- Fiscal Document Email -->
<menuitem
id="document_email_menu"
action="document_email_action"
groups="l10n_br_fiscal.group_manager"
parent="l10n_br_fiscal.others_config_menu"
sequence="30"
/>

</odoo>

0 comments on commit 067d480

Please sign in to comment.