Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][NEW] Edoc Send Email #3183

Draft
wants to merge 7 commits into
base: 14.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions l10n_br_account/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
self.move_ids.button_draft()
return result

def _edoc_subscribe(self):
if self.move_ids:
self.move_ids._edoc_subscribe()
return

Check warning on line 199 in l10n_br_account/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_account/models/document.py#L198-L199

Added lines #L198 - L199 were not covered by tests

def exec_after_SITUACAO_EDOC_DENEGADA(self, old_state, new_state):
self.ensure_one()
models_cancel_on_deny = [MODELO_FISCAL_NFE, MODELO_FISCAL_CTE]
Expand Down
12 changes: 12 additions & 0 deletions l10n_br_fiscal/demo/l10n_br_fiscal_document_email.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
model="l10n_br_fiscal.document.email"
>
<field name="issuer">company</field>
<field name="state_autorizada" eval="True" />
<field name="state_cancelada" eval="True" />
<field name="state_denegada" eval="True" />
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template"
Expand All @@ -20,6 +23,9 @@
>
<field name="document_type_id" ref="l10n_br_fiscal.document_SE" />
<field name="issuer">company</field>
<field name="state_autorizada" eval="True" />
<field name="state_cancelada" eval="True" />
<field name="state_denegada" eval="True" />
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template_nfse"
Expand All @@ -32,6 +38,9 @@
model="l10n_br_fiscal.document.email"
>
<field name="issuer">company</field>
<field name="state_autorizada" eval="True" />
<field name="state_cancelada" eval="True" />
<field name="state_denegada" eval="True" />
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template"
Expand All @@ -45,6 +54,9 @@
>
<field name="document_type_id" ref="l10n_br_fiscal.document_SE" />
<field name="issuer">company</field>
<field name="state_autorizada" eval="True" />
<field name="state_cancelada" eval="True" />
<field name="state_denegada" eval="True" />
<field
name="email_template_id"
ref="l10n_br_fiscal.fiscal_document_change_state_template_nfse"
Expand Down
76 changes: 53 additions & 23 deletions l10n_br_fiscal/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,34 +462,64 @@
# see https://github.com/OCA/l10n-brazil/pull/3272
pass

def _get_email_template(self, state):
def _get_email_template(self):
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):
domain = []
if self.state_edoc == SITUACAO_EDOC_AUTORIZADA:
domain += [
"&",
("state_autorizada", "=", self.state_edoc == SITUACAO_EDOC_AUTORIZADA),
]
if self.state_edoc == SITUACAO_EDOC_CANCELADA:
domain += [
"&",
("state_cancelada", "=", self.state_edoc == SITUACAO_EDOC_CANCELADA),
]
if self.state_edoc == SITUACAO_EDOC_DENEGADA:
domain += [
"&",
("state_denegada", "=", self.state_edoc == SITUACAO_EDOC_DENEGADA),
]

domain += [
"&",
("issuer", "=", self.issuer),
"|",
("document_type_id", "=", False),
("document_type_id", "=", self.document_type_id.id),
]
return (
self.env["l10n_br_fiscal.document.email"]
.search(domain)
.mapped("email_template_id")
)

def send_email(self):
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)
if self.state_edoc not in (
SITUACAO_EDOC_AUTORIZADA,
SITUACAO_EDOC_CANCELADA,
SITUACAO_EDOC_DENEGADA,
):
return

email_template_id = self._get_email_template()
if email_template_id:
self._edoc_subscribe()
attachment_ids = []
if self.authorization_file_id:
attachment_ids.append(self.authorization_file_id.id)
if self.file_report_id:
attachment_ids.append(self.file_report_id.id)

self.message_post_with_template(
template_id=email_template_id.id, attachment_ids=attachment_ids
)

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)
self.send_email()
return result

@api.onchange("fiscal_operation_id")
Expand Down Expand Up @@ -584,7 +614,7 @@
template message loaded by default
"""
self.ensure_one()
template = self._get_email_template(self.state)
template = self._get_email_template()

Check warning on line 617 in l10n_br_fiscal/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_fiscal/models/document.py#L617

Added line #L617 was not covered by tests
compose_form = self.env.ref("mail.email_compose_message_wizard_form", False)
lang = self.env.context.get("lang")
if template and template.lang:
Expand Down
21 changes: 12 additions & 9 deletions l10n_br_fiscal/models/document_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from odoo import api, fields, models

from ..constants.fiscal import DOCUMENT_ISSUER, DOCUMENT_ISSUER_COMPANY, SITUACAO_EDOC
from ..constants.fiscal import DOCUMENT_ISSUER, DOCUMENT_ISSUER_COMPANY


class DocumentEmail(models.Model):
Expand Down Expand Up @@ -40,11 +40,16 @@ class DocumentEmail(models.Model):
required=True,
)

state_edoc = fields.Selection(
selection=SITUACAO_EDOC,
string="Situação e-doc",
copy=False,
index=True,
state_autorizada = fields.Boolean(
string="Autorizada",
)

state_cancelada = fields.Boolean(
string="Cancelada",
)

state_denegada = fields.Boolean(
string="Denegada",
)

email_template_id = fields.Many2one(
Expand All @@ -56,14 +61,12 @@ class DocumentEmail(models.Model):
"this document state change.",
)

@api.depends("document_type_id", "state_edoc")
@api.depends("document_type_id")
def _compute_name(self):
for record in self:
document_type = record.document_type_id.name
if not document_type:
document_type = "Others Document Types"
if record.state_edoc:
record.name = document_type + " - " + record.state_edoc

_sql_constraints = [
(
Expand Down
7 changes: 7 additions & 0 deletions l10n_br_fiscal/models/document_move_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,10 @@ def _onchange_fiscal_operation_id(self):
)
self.document_subsequent_ids = subsequent_documents
return result

def _edoc_subscribe(self):
partner_ids = self.partner_id.filtered("edoc_send_email")
partner_child_ids = self.partner_id.child_ids.filtered("edoc_send_email")
all_partner_ids = partner_ids | partner_child_ids

self.message_subscribe(partner_ids=all_partner_ids.ids)
5 changes: 5 additions & 0 deletions l10n_br_fiscal/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def _default_fiscal_profile_id(self, is_company=False):
string="Anonymous Consumer",
)

edoc_send_email = fields.Boolean(
string="Edoc Send",
track_visibility="onchange",
)

def _inverse_fiscal_profile(self):
for p in self:
p._onchange_fiscal_profile_id()
Expand Down
8 changes: 6 additions & 2 deletions l10n_br_fiscal/views/document_email_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<field name="name" />
<field name="document_type_id" />
<field name="issuer" />
<field name="state_edoc" />
<field name="state_autorizada" />
<field name="state_cancelada" />
<field name="state_denegada" />
<field name="email_template_id" />
</tree>
</field>
Expand All @@ -36,7 +38,9 @@
<group>
<field name="document_type_id" />
<field name="issuer" />
<field name="state_edoc" />
<field name="state_autorizada" />
<field name="state_cancelada" />
<field name="state_denegada" />
<field name="email_template_id" />
<field name="company_id" groups="base.group_multi_company" />
</group>
Expand Down
11 changes: 11 additions & 0 deletions l10n_br_fiscal/views/res_partner_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
<record id="partner_form" model="ir.ui.view">
<field name="name">l10n_br_fiscal.partner.form</field>
<field name="model">res.partner</field>
<field name="priority">100</field>
<field name="inherit_id" ref="l10n_br_base.l10n_br_base_partner_form" />
<field name="arch" type="xml">
<field name="email" position="after">
<field name="edoc_send_email" />
<label for="edoc_send_email" string="DF-e" />
</field>
<xpath
expr="//field[@name='child_ids']/form//field[@name='email']"
position="after"
>
<field name="edoc_send_email" string="DF-e" />
</xpath>
<group name="fiscal_numbers" position="after">
<group string="Fiscal Profile" name="fiscal_profile">
<field
Expand Down
Loading