Skip to content

Commit

Permalink
[IMP] account_financial_risk: Send email when risk confirmed
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Aug 1, 2024
1 parent 43b11fa commit 1d25890
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 11 deletions.
8 changes: 7 additions & 1 deletion account_financial_risk/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Account Financial Risk
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:36fb23d517cb43fb97f16428457e3fb48f93d01c936b62f423671e96c61ccb48
!! source digest: sha256:31cac0ba9b752cfb1819ddbd398f68ab68a57ac51e304e053fc2ea7b2a7056e3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down 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/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>
18 changes: 10 additions & 8 deletions account_financial_risk/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -367,7 +366,7 @@ <h1 class="title">Account Financial Risk</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:36fb23d517cb43fb97f16428457e3fb48f93d01c936b62f423671e96c61ccb48
!! source digest: sha256:31cac0ba9b752cfb1819ddbd398f68ab68a57ac51e304e053fc2ea7b2a7056e3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/credit-control/tree/16.0/account_financial_risk"><img alt="OCA/credit-control" src="https://img.shields.io/badge/github-OCA%2Fcredit--control-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/credit-control-16-0/credit-control-16-0-account_financial_risk"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/credit-control&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Adds a new page in partner to manage its <em>Financial Risk</em>.
Expand Down Expand Up @@ -403,6 +402,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,14 +450,16 @@ <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">
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
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"))
17 changes: 17 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,22 @@
</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" />
<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

0 comments on commit 1d25890

Please sign in to comment.