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

[IMP] account_financial_risk: Send email when risk confirmed #393

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions account_financial_risk/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ To configure this module, you need to:
#. In the *Customer Payments* section, fill *Maturity Margin* for setting the
number of days to last after the due date to consider an invoice as unpaid.

If you want an email to be sent when risk is exceeded in invoices, go to *Invoicing/Accounting > Configuration > Settings > Financial Risk* and fill the field `Email template sent when confirming invoice risk`.

Usage
=====

Expand Down Expand Up @@ -105,6 +107,10 @@ Contributors

* Ilyas <irazor147@gmail.com>

* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <simone.rubino@aion-tech.it>

Maintainers
~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions account_financial_risk/models/res_company.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2016-2018 Tecnativa - Carlos Dauden
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models
Expand All @@ -19,3 +20,9 @@ class ResCompany(models.Model):
"Useful when the flow comes from sales orders and the over-risk "
"has already been allowed when confirming these.",
)
account_move_confirm_risk_template_id = fields.Many2one(
comodel_name="mail.template",
string="Email template sent when confirming invoice risk",
help="This email template is sent when "
"the 'Partner risk exceeded wizard' for an invoice is confirmed.",
)
5 changes: 5 additions & 0 deletions account_financial_risk/models/res_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2016-2018 Tecnativa - Carlos Dauden
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models
Expand All @@ -13,3 +14,7 @@ class AccountConfigSettings(models.TransientModel):
allow_overrisk_invoice_validation = fields.Boolean(
related="company_id.allow_overrisk_invoice_validation", readonly=False
)
account_move_confirm_risk_template_id = fields.Many2one(
readonly=False,
related="company_id.account_move_confirm_risk_template_id",
)
2 changes: 2 additions & 0 deletions account_financial_risk/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2016-2021 Tecnativa - Carlos Dauden
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import datetime
Expand Down Expand Up @@ -157,6 +158,7 @@ class ResPartner(models.Model):
store=True,
readonly=False,
string="Last Credit Limit Date",
tracking=True,
)
risk_remaining_value = fields.Monetary(
compute="_compute_risk_remaining",
Expand Down
2 changes: 2 additions & 0 deletions account_financial_risk/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ To configure this module, you need to:
#. Go to *Invoicing/Accounting > Configuration > Settings > Accounting*
#. In the *Customer Payments* section, fill *Maturity Margin* for setting the
number of days to last after the due date to consider an invoice as unpaid.

If you want an email to be sent when risk is exceeded in invoices, go to *Invoicing/Accounting > Configuration > Settings > Financial Risk* and fill the field `Email template sent when confirming invoice risk`.
4 changes: 4 additions & 0 deletions account_financial_risk/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
* `Ooops404 <https://www.ooops404.com>`__:

* Ilyas <irazor147@gmail.com>

* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <simone.rubino@aion-tech.it>
5 changes: 5 additions & 0 deletions account_financial_risk/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ <h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<li>In the <em>Customer Payments</em> section, fill <em>Maturity Margin</em> for setting the
number of days to last after the due date to consider an invoice as unpaid.</li>
</ol>
<p>If you want an email to be sent when risk is exceeded in invoices, go to <em>Invoicing/Accounting &gt; Configuration &gt; Settings &gt; Financial Risk</em> and fill the field <cite>Email template sent when confirming invoice risk</cite>.</p>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
Expand Down Expand Up @@ -450,6 +451,10 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Ilyas &lt;<a class="reference external" href="mailto:irazor147&#64;gmail.com">irazor147&#64;gmail.com</a>&gt;</li>
</ul>
</li>
<li><a class="reference external" href="https://aiontech.company/">Aion Tech</a>:<ul>
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;aion-tech.it">simone.rubino&#64;aion-tech.it</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
32 changes: 32 additions & 0 deletions account_financial_risk/tests/test_account_financial_risk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2016-2019 Tecnativa - Carlos Dauden
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -290,3 +291,34 @@ def test_invoice_risk_draft_different_currency(self):
self.partner.risk_amount_exceeded,
self.partner.risk_total - self.partner.credit_limit,
)

def test_confirm_risk_send_email(self):
"""When the risk is confirmed by the user, an email is sent."""
# Arrange
invoice = self.invoice
template_subject = "Risk exceeded"
template = self.env["mail.template"].create(
{
"name": "Test Risk exceeded template",
"model_id": self.env.ref("account.model_account_move").id,
"subject": template_subject,
}
)
invoice.company_id.account_move_confirm_risk_template_id = template
partner = invoice.partner_id
partner.risk_invoice_draft_include = True
partner.credit_limit = invoice.amount_total - 1
# pre-condition
self.assertTrue(partner.risk_exception)
existing_emails = self.env["mail.mail"].search([])

# Act
risk_wizard_action = invoice.action_post()
risk_wizard = self.env[risk_wizard_action["res_model"]].browse(
risk_wizard_action["res_id"]
)
risk_wizard.button_continue()

# Assert
new_emails = self.env["mail.mail"].search([]) - existing_emails
self.assertIn(template_subject, new_emails.mapped("subject"))
24 changes: 24 additions & 0 deletions account_financial_risk/views/res_config_view.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2016-2018 Tecnativa - Carlos Dauden
Copyright 2024 Simone Rubino - Aion Tech
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
Expand Down Expand Up @@ -45,6 +46,29 @@
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane" />
<div class="o_setting_right_pane">
<label for="account_move_confirm_risk_template_id" />
<span
class="fa fa-lg fa-building-o"
title="Values set here are company-specific."
role="img"
aria-label="Values set here are company-specific."
groups="base.group_multi_company"
/>
<div class="text-muted">
This email template is sent when the 'Partner risk exceeded wizard' for an invoice is confirmed.
</div>
<div class="content-group">
<div class="row mt16 ml4">
<field
name="account_move_confirm_risk_template_id"
/>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
Expand Down
14 changes: 12 additions & 2 deletions account_financial_risk/wizards/parner_risk_exceeded.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2016-2018 Tecnativa - Carlos Dauden
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, fields, models
Expand Down Expand Up @@ -31,8 +32,17 @@ def action_show(self):
"target": "new",
}

def _post_account_move_confirm_risk(self, origin_record):
template = origin_record.company_id.account_move_confirm_risk_template_id
if template:
template.send_mail(origin_record.id)

def button_continue(self):
self.ensure_one()
return getattr(
self.origin_reference.with_context(bypass_risk=True), self.continue_method
origin_record = self.origin_reference
continue_result = getattr(
origin_record.with_context(bypass_risk=True), self.continue_method
)()
if origin_record._name == "account.move":
self._post_account_move_confirm_risk(origin_record)
return continue_result
Loading