From 7ae9901b9f45a3c26bd5cb0319a8d0a2addc965c Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Wed, 31 Jul 2024 17:43:45 +0200 Subject: [PATCH 1/2] [IMP] account_financial_risk: Send email when risk confirmed --- account_financial_risk/README.rst | 6 ++++ account_financial_risk/models/res_company.py | 7 ++++ account_financial_risk/models/res_config.py | 5 +++ account_financial_risk/readme/CONFIGURE.rst | 2 ++ .../readme/CONTRIBUTORS.rst | 4 +++ .../static/description/index.html | 5 +++ .../tests/test_account_financial_risk.py | 32 +++++++++++++++++++ .../views/res_config_view.xml | 24 ++++++++++++++ .../wizards/parner_risk_exceeded.py | 19 +++++++++-- 9 files changed, 102 insertions(+), 2 deletions(-) diff --git a/account_financial_risk/README.rst b/account_financial_risk/README.rst index eaf401414..3f8449844 100644 --- a/account_financial_risk/README.rst +++ b/account_financial_risk/README.rst @@ -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 ===== @@ -105,6 +107,10 @@ Contributors * Ilyas +* `Aion Tech `_: + + * Simone Rubino + Maintainers ~~~~~~~~~~~ diff --git a/account_financial_risk/models/res_company.py b/account_financial_risk/models/res_company.py index 738b019d6..e9cb6cfbf 100644 --- a/account_financial_risk/models/res_company.py +++ b/account_financial_risk/models/res_company.py @@ -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 @@ -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.", + ) diff --git a/account_financial_risk/models/res_config.py b/account_financial_risk/models/res_config.py index 176f3fbe4..5bf682294 100644 --- a/account_financial_risk/models/res_config.py +++ b/account_financial_risk/models/res_config.py @@ -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 @@ -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", + ) diff --git a/account_financial_risk/readme/CONFIGURE.rst b/account_financial_risk/readme/CONFIGURE.rst index 159c01264..f6dfe4312 100644 --- a/account_financial_risk/readme/CONFIGURE.rst +++ b/account_financial_risk/readme/CONFIGURE.rst @@ -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`. diff --git a/account_financial_risk/readme/CONTRIBUTORS.rst b/account_financial_risk/readme/CONTRIBUTORS.rst index c6275b002..6517030c9 100644 --- a/account_financial_risk/readme/CONTRIBUTORS.rst +++ b/account_financial_risk/readme/CONTRIBUTORS.rst @@ -11,3 +11,7 @@ * `Ooops404 `__: * Ilyas + +* `Aion Tech `_: + + * Simone Rubino diff --git a/account_financial_risk/static/description/index.html b/account_financial_risk/static/description/index.html index 51d783d6d..469b94de9 100644 --- a/account_financial_risk/static/description/index.html +++ b/account_financial_risk/static/description/index.html @@ -403,6 +403,7 @@

Configuration

  • 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

    @@ -450,6 +451,10 @@

    Contributors

  • Ilyas <irazor147@gmail.com>
  • +
  • Aion Tech: +
  • diff --git a/account_financial_risk/tests/test_account_financial_risk.py b/account_financial_risk/tests/test_account_financial_risk.py index 45840cc45..64884ab53 100644 --- a/account_financial_risk/tests/test_account_financial_risk.py +++ b/account_financial_risk/tests/test_account_financial_risk.py @@ -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 @@ -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")) diff --git a/account_financial_risk/views/res_config_view.xml b/account_financial_risk/views/res_config_view.xml index 9f085226f..1d9e0a9d3 100644 --- a/account_financial_risk/views/res_config_view.xml +++ b/account_financial_risk/views/res_config_view.xml @@ -1,5 +1,6 @@ @@ -45,6 +46,29 @@
    +
    +
    +
    +
    +
    diff --git a/account_financial_risk/wizards/parner_risk_exceeded.py b/account_financial_risk/wizards/parner_risk_exceeded.py index 984c2ee01..e8f9664f9 100644 --- a/account_financial_risk/wizards/parner_risk_exceeded.py +++ b/account_financial_risk/wizards/parner_risk_exceeded.py @@ -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 @@ -31,8 +32,22 @@ def action_show(self): "target": "new", } + def _post_account_move_confirm_risk(self, account_move): + """Perform actions when risk is confirmed for the Journal Entry `account_move`. + + By default, this sends the email template configured in the company. + Override as needed. + """ + template = account_move.company_id.account_move_confirm_risk_template_id + if template: + template.send_mail(account_move.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 From 449b2bda4cdf43454ff98c6a21839d3988b94256 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Thu, 1 Aug 2024 12:42:03 +0200 Subject: [PATCH 2/2] [IMP] account_financial_risk: Track credit limit date as amount --- account_financial_risk/models/res_partner.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/account_financial_risk/models/res_partner.py b/account_financial_risk/models/res_partner.py index a3b4c9fb9..1cc525e7c 100644 --- a/account_financial_risk/models/res_partner.py +++ b/account_financial_risk/models/res_partner.py @@ -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 @@ -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",