Skip to content

Commit

Permalink
[17.0][MIG] sale_financial_risk: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sbiosca-s73 committed May 27, 2024
1 parent 28a249d commit 3e15923
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 119 deletions.
46 changes: 23 additions & 23 deletions sale_financial_risk/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ Sale Financial Risk
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcredit--control-lightgray.png?logo=github
:target: https://github.com/OCA/credit-control/tree/16.0/sale_financial_risk
:target: https://github.com/OCA/credit-control/tree/17.0/sale_financial_risk
:alt: OCA/credit-control
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/credit-control-16-0/credit-control-16-0-sale_financial_risk
:target: https://translation.odoo-community.org/projects/credit-control-17-0/credit-control-17-0-sale_financial_risk
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/credit-control&target_branch=16.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/credit-control&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Extends Partner Financial Risk to manage sales orders.

Adds a new risk amount field in sale order line to compute risk based on the
difference between ordered quantity (or delivered in some cases) and invoiced
quantity.
Adds a new risk amount field in sale order line to compute risk based on
the difference between ordered quantity (or delivered in some cases) and
invoiced quantity.

If any limit is exceed the partner gets forbidden to confirm sale orders.
If any limit is exceed the partner gets forbidden to confirm sale
orders.

**Table of contents**

Expand All @@ -46,44 +47,43 @@ Usage

To use this module, you need to:

#. Go to *Customers > Financial Risk*
#. Set limits and choose options to compute in credit limit.
#. Go to *Sales -> Orders -> Orders* and create a new Sales Orders.
1. Go to *Customers > Financial Risk*
2. Set limits and choose options to compute in credit limit.
3. Go to *Sales -> Orders -> Orders* and create a new Sales Orders.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/credit-control/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/credit-control/issues/new?body=module:%20sale_financial_risk%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/credit-control/issues/new?body=module:%20sale_financial_risk%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~
-------

* Tecnativa

Contributors
~~~~~~~~~~~~
------------

* `Tecnativa <https://www.tecnativa.com>`_:
- `Tecnativa <https://www.tecnativa.com>`__:

* Carlos Dauden
* Pedro M. Baeza
* Ernesto Tejeda
* Stefan Ungureanu
- Carlos Dauden
- Pedro M. Baeza
- Ernesto Tejeda
- Stefan Ungureanu

* Agathe Mollé <agathe.molle@savoirfairelinux.com>

* Ugne Sinkeviciene <ugne@versada.eu>
- Agathe Mollé <agathe.molle@savoirfairelinux.com>
- Ugne Sinkeviciene <ugne@versada.eu>

Maintainers
~~~~~~~~~~~
-----------

This module is maintained by the OCA.

Expand All @@ -95,6 +95,6 @@ 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.

This module is part of the `OCA/credit-control <https://github.com/OCA/credit-control/tree/16.0/sale_financial_risk>`_ project on GitHub.
This module is part of the `OCA/credit-control <https://github.com/OCA/credit-control/tree/17.0/sale_financial_risk>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion sale_financial_risk/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Sale Financial Risk",
"summary": "Manage partner risk in sales orders",
"version": "16.0.1.1.2",
"version": "17.0.1.0.0",
"category": "Sales Management",
"license": "AGPL-3",
"author": "Tecnativa, Odoo Community Association (OCA)",
Expand Down
13 changes: 6 additions & 7 deletions sale_financial_risk/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@
logger = logging.getLogger(__name__)


def pre_init_hook(cr):
def pre_init_hook(env):
"""
The objective of this hook is to speed up the installation
of the module on an existing Odoo instance.
"""
create_risk_partner_id_column(cr)
create_risk_partner_id_column(env)


def create_risk_partner_id_column(cr):
if not sql.column_exists(cr, "sale_order_line", "risk_partner_id"):
sql.create_column(cr, "sale_order_line", "risk_partner_id", "int4")

def create_risk_partner_id_column(env):
if not sql.column_exists(env.cr, "sale_order_line", "risk_partner_id"):
sql.create_column(env.cr, "sale_order_line", "risk_partner_id", "int4")
logger.info("Computing field risk_partner_id on sale.order.line")
cr.execute(
env.cr.execute(
"""
UPDATE sale_order_line sol
SET risk_partner_id = p.commercial_partner_id
Expand Down
14 changes: 4 additions & 10 deletions sale_financial_risk/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,15 @@ def _get_risk_sale_order_domain(self):
)
def _compute_risk_sale_order(self):
self.update({"risk_sale_order": 0.0})
orders_group = self.env["sale.order.line"].read_group(
orders_group = self.env["sale.order.line"]._read_group(
domain=self._get_risk_sale_order_domain(),
fields=["risk_partner_id", "company_id", "risk_amount"],
groupby=["risk_partner_id", "company_id"],
orderby="id",
lazy=False,
aggregates=["risk_amount:sum"],
)
for group in orders_group:
partner = self.browse(group["risk_partner_id"][0])
company = self.env["res.company"].browse(
group["company_id"][0] or self.env.company.id
)
for partner, company, risk_amount in orders_group:
company_currency = company.currency_id
partner.risk_sale_order = company_currency._convert(
group["risk_amount"],
risk_amount,
partner.risk_currency_id,
company,
fields.Date.context_today(self),
Expand Down
2 changes: 1 addition & 1 deletion sale_financial_risk/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def action_confirm(self):
{
"exception_msg": exception_msg,
"partner_id": partner.id,
"origin_reference": "%s,%s" % ("sale.order", order.id),
"origin_reference": f"{order._name},{order.id}",
"continue_method": "action_confirm",
}
)
Expand Down
3 changes: 3 additions & 0 deletions sale_financial_risk/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
7 changes: 7 additions & 0 deletions sale_financial_risk/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- [Tecnativa](https://www.tecnativa.com):
- Carlos Dauden
- Pedro M. Baeza
- Ernesto Tejeda
- Stefan Ungureanu
- Agathe Mollé \<<agathe.molle@savoirfairelinux.com>\>
- Ugne Sinkeviciene \<<ugne@versada.eu>\>
10 changes: 0 additions & 10 deletions sale_financial_risk/readme/CONTRIBUTORS.rst

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Extends Partner Financial Risk to manage sales orders.

Adds a new risk amount field in sale order line to compute risk based on the
difference between ordered quantity (or delivered in some cases) and invoiced
quantity.
Adds a new risk amount field in sale order line to compute risk based on
the difference between ordered quantity (or delivered in some cases) and
invoiced quantity.

If any limit is exceed the partner gets forbidden to confirm sale orders.
If any limit is exceed the partner gets forbidden to confirm sale
orders.
5 changes: 5 additions & 0 deletions sale_financial_risk/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
To use this module, you need to:

1. Go to *Customers \> Financial Risk*
2. Set limits and choose options to compute in credit limit.
3. Go to *Sales -\> Orders -\> Orders* and create a new Sales Orders.
5 changes: 0 additions & 5 deletions sale_financial_risk/readme/USAGE.rst

This file was deleted.

15 changes: 8 additions & 7 deletions sale_financial_risk/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,13 @@ <h1 class="title">Sale Financial Risk</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ea8927c83b428d46a09ec5731ed39bf3def893cc64a8904cc994621051da9e8b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/sale_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-sale_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><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/17.0/sale_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-17-0/credit-control-17-0-sale_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=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Extends Partner Financial Risk to manage sales orders.</p>
<p>Adds a new risk amount field in sale order line to compute risk based on the
difference between ordered quantity (or delivered in some cases) and invoiced
quantity.</p>
<p>If any limit is exceed the partner gets forbidden to confirm sale orders.</p>
<p>Adds a new risk amount field in sale order line to compute risk based on
the difference between ordered quantity (or delivered in some cases) and
invoiced quantity.</p>
<p>If any limit is exceed the partner gets forbidden to confirm sale
orders.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand Down Expand Up @@ -401,7 +402,7 @@ <h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/credit-control/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/credit-control/issues/new?body=module:%20sale_financial_risk%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/credit-control/issues/new?body=module:%20sale_financial_risk%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -433,7 +434,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<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>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/credit-control/tree/16.0/sale_financial_risk">OCA/credit-control</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/credit-control/tree/17.0/sale_financial_risk">OCA/credit-control</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
37 changes: 15 additions & 22 deletions sale_financial_risk/tests/test_partner_sale_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class TestPartnerSaleRisk(TransactionCase):
@classmethod
def setUpClass(cls):
super(TestPartnerSaleRisk, cls).setUpClass()
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
cls.env.user.groups_id |= cls.env.ref(
"account_financial_risk.group_account_financial_risk_manager"
Expand Down Expand Up @@ -56,7 +56,7 @@ def create_sale_order(cls, currency, company):

def test_sale_order(self):
self.sale_order.action_confirm()
self.assertAlmostEqual(self.partner.risk_sale_order, 100.0)
self.assertAlmostEqual(self.partner.risk_sale_order, 115.0)
self.assertFalse(self.partner.risk_exception)
self.partner.risk_sale_order_limit = 99.0
self.partner.risk_sale_order_include = True
Expand All @@ -76,12 +76,10 @@ def test_sale_order(self):
self.partner.credit_limit = 100.0
wiz_dic = sale_order2.action_confirm()
wiz = self.env[wiz_dic["res_model"]].browse(wiz_dic["res_id"])
self.assertEqual(
wiz.exception_msg, "This sale order exceeds the financial risk.\n"
)
self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n")
self.assertTrue(self.partner.risk_allow_edit)
wiz.button_continue()
self.assertAlmostEqual(self.partner.risk_sale_order, 200.0)
self.assertAlmostEqual(self.partner.risk_sale_order, 230.0)

def test_sale_order_auto_done(self):
self.env["ir.config_parameter"].create(
Expand All @@ -95,7 +93,7 @@ def test_sale_order_auto_done(self):
)
self.sale_order.action_confirm()
self.partner.risk_sale_order_include = True
self.assertAlmostEqual(self.partner.risk_sale_order, 100.0)
self.assertAlmostEqual(self.partner.risk_sale_order, 115.0)
self.assertFalse(self.partner.risk_exception)
self.partner.risk_sale_order_limit = 99.0
self.assertTrue(self.partner.risk_exception)
Expand All @@ -114,17 +112,15 @@ def test_sale_order_auto_done(self):
self.partner.credit_limit = 100.0
wiz_dic = sale_order2.action_confirm()
wiz = self.env[wiz_dic["res_model"]].browse(wiz_dic["res_id"])
self.assertEqual(
wiz.exception_msg, "This sale order exceeds the financial risk.\n"
)
self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n")
self.assertTrue(self.partner.risk_allow_edit)
wiz.button_continue()
self.assertAlmostEqual(self.partner.risk_sale_order, 200.0)
self.assertAlmostEqual(self.partner.risk_sale_order, 230.0)

def test_compute_risk_amount(self):
self.sale_order.action_confirm()
# Now the amount to be invoiced must 100
self.assertEqual(self.partner.risk_sale_order, 100.0)
# Now the amount to be invoiced must 115
self.assertEqual(self.partner.risk_sale_order, 115.0)
self.assertFalse(self.partner.risk_exception)
# If we set a risk_sale_order_limit to 99, risk_exception must be True
self.partner.risk_sale_order_limit = 99.0
Expand All @@ -138,13 +134,13 @@ def test_compute_risk_amount(self):
.create({})
)
inv_wiz.create_invoices()
self.assertAlmostEqual(self.partner.risk_invoice_draft, 100.0)
self.assertAlmostEqual(self.partner.risk_invoice_draft, 115.0)
self.assertAlmostEqual(self.partner.risk_sale_order, 0)
invoice = self.sale_order.invoice_ids
invoice.with_context(bypass_risk=True).action_post()
self.assertAlmostEqual(self.partner.risk_sale_order, 0)
self.assertAlmostEqual(self.partner.risk_invoice_draft, 0.0)
self.assertAlmostEqual(self.partner.risk_invoice_open, 100.0)
self.assertAlmostEqual(self.partner.risk_invoice_open, 115.0)
self.assertFalse(self.partner.risk_exception)
# After that, if we create and validate a Credit Note from the invoice
# then the amount to be invoiced must be 100 again
Expand All @@ -158,18 +154,15 @@ def test_compute_risk_amount(self):
ref_wiz_obj = self.env["account.move.reversal"].with_context(
active_model="account.move", active_ids=[invoice.id]
)
ref_wiz = ref_wiz_obj.create(
{"reason": "testing", "refund_method": "modify", "journal_id": journal.id}
)
ref_wiz = ref_wiz_obj.create({"reason": "testing", "journal_id": journal.id})
res = ref_wiz.reverse_moves()
self.assertAlmostEqual(self.partner.risk_invoice_draft, 100.0)
self.assertAlmostEqual(self.partner.risk_sale_order, 0.0)
self.assertAlmostEqual(self.partner.risk_invoice_draft, -115.0)
self.assertAlmostEqual(self.partner.risk_sale_order, 115)
# The way to re-invoice a sale order is creating a refund with
# modify option and cancel or remove draft invoice
modify_invoice = invoice.browse(res["res_id"])
modify_invoice.unlink()
self.assertAlmostEqual(self.partner.risk_sale_order, 100.0)
self.assertTrue(self.partner.risk_exception)
self.assertAlmostEqual(self.partner.risk_sale_order, 0.0)
line = self.sale_order.order_line[:1]
line.product_uom_qty = 0.0
self.assertAlmostEqual(line.risk_amount, 0.0)
Expand Down
Loading

0 comments on commit 3e15923

Please sign in to comment.