From e67cda7febe46ae433937923154453ce6276d8d6 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Mon, 8 Jul 2024 17:41:28 +0200 Subject: [PATCH] [MIG] purchase_request_analytic: Migration to 17.0 --- purchase_request_analytic/README.rst | 19 ++--- purchase_request_analytic/__manifest__.py | 2 +- .../migrations/16.0.1.0.0/pre-migration.py | 15 ++++ .../models/purchase_request.py | 49 +++++------- .../readme/DESCRIPTION.md | 3 +- purchase_request_analytic/readme/USAGE.md | 3 +- .../static/description/index.html | 26 ++++--- .../tests/test_purchase_request_analytic.py | 77 ++++++++++--------- .../views/purchase_request_views.xml | 33 +++++--- 9 files changed, 127 insertions(+), 100 deletions(-) create mode 100644 purchase_request_analytic/migrations/16.0.1.0.0/pre-migration.py diff --git a/purchase_request_analytic/README.rst b/purchase_request_analytic/README.rst index 1b42c43dcd..d1d7db745f 100644 --- a/purchase_request_analytic/README.rst +++ b/purchase_request_analytic/README.rst @@ -7,7 +7,7 @@ Purchase Request Analytic !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:091f04d8f6a4d3c43d2c4472b26a68d8b4de7c34d58bc3e45bb3cb22b41b137d + !! source digest: sha256:e5d542b4f36801b7d89139c82816d3daaeff1caff8abf8e095ff1f74f576d5d3 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -17,18 +17,19 @@ Purchase Request Analytic :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--analytic-lightgray.png?logo=github - :target: https://github.com/OCA/account-analytic/tree/17.0/purchase_request_analytic + :target: https://github.com/OCA/account-analytic/tree/17.-/purchase_request_analytic :alt: OCA/account-analytic .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/account-analytic-17-0/account-analytic-17-0-purchase_request_analytic + :target: https://translation.odoo-community.org/projects/account-analytic-17--/account-analytic-17---purchase_request_analytic :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/account-analytic&target_branch=17.0 + :target: https://runboat.odoo-community.org/builds?repo=OCA/account-analytic&target_branch=17.- :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| -This module adds the analytic account field to Purchase Requests. +This module adds an analytic distribution field to Purchase Requests. +The field serves as a default for new lines on a request. **Table of contents** @@ -38,8 +39,8 @@ This module adds the analytic account field to Purchase Requests. Usage ===== -- Assign an analytic account to the purchase request or to purchase - request lines. +- Assign an analytic distribution to the purchase request or to + purchase request lines. Changelog ========= @@ -70,7 +71,7 @@ Bug Tracker Bugs are tracked on `GitHub 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 `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -102,6 +103,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/account-analytic `_ project on GitHub. +This module is part of the `OCA/account-analytic `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/purchase_request_analytic/__manifest__.py b/purchase_request_analytic/__manifest__.py index fdeeb41f87..ea6faaa06d 100644 --- a/purchase_request_analytic/__manifest__.py +++ b/purchase_request_analytic/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Purchase Request Analytic", - "version": "15.0.1.0.1", + "version": "17.0.1.0.0", "author": "ForgeFlow, Odoo Community Association (OCA)", "category": "Purchase Management", "website": "https://github.com/OCA/account-analytic", diff --git a/purchase_request_analytic/migrations/16.0.1.0.0/pre-migration.py b/purchase_request_analytic/migrations/16.0.1.0.0/pre-migration.py new file mode 100644 index 0000000000..da5d59d081 --- /dev/null +++ b/purchase_request_analytic/migrations/16.0.1.0.0/pre-migration.py @@ -0,0 +1,15 @@ +from odoo.tools.sql import column_exists + + +def migrate(cr, version): + """Populate analytic distribution values from the old analytic account""" + if column_exists(cr, "purchase_request", "analytic_distribution"): + return + cr.execute( + """ + ALTER TABLE purchase_request ADD COLUMN analytic_distribution jsonb; + update purchase_request set analytic_distribution = + json_build_object(analytic_account_id::varchar, 100.0) + where analytic_account_id is not null; + """ + ) diff --git a/purchase_request_analytic/models/purchase_request.py b/purchase_request_analytic/models/purchase_request.py index d2184bb37f..22b913cb0c 100644 --- a/purchase_request_analytic/models/purchase_request.py +++ b/purchase_request_analytic/models/purchase_request.py @@ -5,48 +5,37 @@ class PurchaseRequest(models.Model): - _inherit = "purchase.request" + _name = "purchase.request" + _inherit = ["analytic.mixin", "purchase.request"] - analytic_account_id = fields.Many2one( - compute="_compute_analytic_account_id", - inverse="_inverse_analytic_account_id", - comodel_name="account.analytic.account", - string="Analytic Account", - readonly=True, - states={"draft": [("readonly", False)]}, - store=True, - help="The analytic account related to a sales order.", + analytic_distribution = fields.Json( + inverse="_inverse_analytic_distribution", + help="The default distribution for new lines on this request", ) - @api.depends("line_ids.analytic_account_id") - def _compute_analytic_account_id(self): - """If all purchase request lines have same analytic account set - analytic_account_id - """ + @api.depends("line_ids.analytic_distribution") + def _compute_analytic_distribution(self): + """Take the distribution that is already set on all of our lines.""" for pr in self: - al = pr.analytic_account_id + al = pr.analytic_distribution if pr.line_ids: - first_line_analytic_account_id = pr.line_ids[0].analytic_account_id + first_line_analytic_distribution = pr.line_ids[0].analytic_distribution all_lines_same = all( - prl.analytic_account_id == first_line_analytic_account_id + prl.analytic_distribution == first_line_analytic_distribution for prl in pr.line_ids ) - # If all lines share the same analytic_account_id, + # If all lines share the same analytic_distribution, # set it to the purchase request. if all_lines_same: - pr.analytic_account_id = first_line_analytic_account_id + pr.analytic_distribution = first_line_analytic_distribution continue for prl in pr.line_ids: - if prl.analytic_account_id != al: + if prl.analytic_distribution != al: al = False break - pr.analytic_account_id = al + pr.analytic_distribution = al - def _inverse_analytic_account_id(self): - """If analytic_account is set on PR, propagate it to all purchase - request lines - """ - for pr in self: - if pr.analytic_account_id: - for line in pr.line_ids: - line.analytic_account_id = pr.analytic_account_id.id + def _inverse_analytic_distribution(self): + """Set this requests's distribution on all of its lines.""" + for pr in self.filtered("analytic_distribution"): + pr.line_ids.analytic_distribution = pr.analytic_distribution diff --git a/purchase_request_analytic/readme/DESCRIPTION.md b/purchase_request_analytic/readme/DESCRIPTION.md index 7910de960d..fbb8c0a704 100644 --- a/purchase_request_analytic/readme/DESCRIPTION.md +++ b/purchase_request_analytic/readme/DESCRIPTION.md @@ -1 +1,2 @@ -This module adds the analytic account field to Purchase Requests. +This module adds an analytic distribution field to Purchase Requests. The field +serves as a default for new lines on a request. diff --git a/purchase_request_analytic/readme/USAGE.md b/purchase_request_analytic/readme/USAGE.md index aa6df84621..6b31ada888 100644 --- a/purchase_request_analytic/readme/USAGE.md +++ b/purchase_request_analytic/readme/USAGE.md @@ -1,2 +1,3 @@ -- Assign an analytic account to the purchase request or to purchase +- Assign an analytic distribution to the purchase request or to purchase request lines. + diff --git a/purchase_request_analytic/static/description/index.html b/purchase_request_analytic/static/description/index.html index 1229d58467..17445befe0 100644 --- a/purchase_request_analytic/static/description/index.html +++ b/purchase_request_analytic/static/description/index.html @@ -8,10 +8,11 @@ /* :Author: David Goodger (goodger@python.org) -:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $ +:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z 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. @@ -274,7 +275,7 @@ margin-left: 2em ; margin-right: 2em } -pre.code .ln { color: grey; } /* line numbers */ +pre.code .ln { color: gray; } /* 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 } @@ -300,7 +301,7 @@ span.pre { white-space: pre } -span.problematic { +span.problematic, pre.problematic { color: red } span.section-subtitle { @@ -366,10 +367,11 @@

Purchase Request Analytic

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:091f04d8f6a4d3c43d2c4472b26a68d8b4de7c34d58bc3e45bb3cb22b41b137d +!! source digest: sha256:e5d542b4f36801b7d89139c82816d3daaeff1caff8abf8e095ff1f74f576d5d3 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> -

Beta License: AGPL-3 OCA/account-analytic Translate me on Weblate Try me on Runboat

-

This module adds the analytic account field to Purchase Requests.

+

Beta License: AGPL-3 OCA/account-analytic Translate me on Weblate Try me on Runboat

+

This module adds an analytic distribution field to Purchase Requests. +The field serves as a default for new lines on a request.

Table of contents

    @@ -393,8 +395,8 @@

    Purchase Request Analytic

    Usage

      -
    • Assign an analytic account to the purchase request or to purchase -request lines.
    • +
    • Assign an analytic distribution to the purchase request or to +purchase request lines.
    @@ -421,7 +423,7 @@

    Bug Tracker

    Bugs are tracked on GitHub 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.

    +feedback.

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

    @@ -443,11 +445,13 @@

    Contributors

    Maintainers

    This module is maintained by the OCA.

    -Odoo Community Association + +Odoo Community Association +

    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/account-analytic project on GitHub.

    +

    This module is part of the OCA/account-analytic project on GitHub.

    You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

    diff --git a/purchase_request_analytic/tests/test_purchase_request_analytic.py b/purchase_request_analytic/tests/test_purchase_request_analytic.py index 194c8c854d..de9f1b98da 100644 --- a/purchase_request_analytic/tests/test_purchase_request_analytic.py +++ b/purchase_request_analytic/tests/test_purchase_request_analytic.py @@ -5,20 +5,31 @@ class TestPurchaseRequestAnalytic(TransactionCase): - def setUp(self): - super(TestPurchaseRequestAnalytic, self).setUp() - self.anal = self.env["account.analytic.account"].create( - {"name": "Account Analytic for Tests"} + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.analytic_plan = cls.env["account.analytic.plan"].create( + { + "name": "Default", + }, + ) + cls.analytic_account = cls.env["account.analytic.account"].create( + { + "name": "Account Analytic for Tests", + "plan_id": cls.analytic_plan.id, + } ) - self.anal2 = self.env["account.analytic.account"].create( - {"name": "Account Analytic for Tests 2"} + cls.analytic_account2 = cls.env["account.analytic.account"].create( + { + "name": "Account Analytic for Tests 2", + "plan_id": cls.analytic_plan.id, + } ) + cls.analytic_distribution = {str(cls.analytic_account.id): 100} + cls.analytic_distribution2 = {str(cls.analytic_account2.id): 100} - def test_analytic_account(self): - """Create a purchase order with line - Set analytic account on purchase - Check analytic account on line is set - """ + def test_analytic_distribution(self): + """The analytic distribution on the request is propagated to its lines""" product_id = self.env.ref("product.product_product_9") pr = self.env["purchase.request"].create( { @@ -38,25 +49,12 @@ def test_analytic_account(self): } ) - pr.analytic_account_id = self.anal.id - self.assertEqual(pr.analytic_account_id.id, self.anal.id) - self.assertEqual(pr.line_ids.analytic_account_id.id, self.anal.id) - - def test_analytic(self): - """Create a purchase order without line - Set analytic account on purchase - Check analytic account is on purchase - """ - pr = self.env["purchase.request"].new( - {"requested_by": self.env.user.id, "analytic_account_id": self.anal.id} - ) - self.assertEqual(pr.analytic_account_id.id, self.anal.id) + pr.analytic_distribution = self.analytic_distribution + self.assertEqual(pr.analytic_distribution, self.analytic_distribution) + self.assertEqual(pr.line_ids.analytic_distribution, self.analytic_distribution) def test_all_analytic_accounts(self): - """Create a purchase request with same analytic account - in the lines and check the purchase request has the - same analytic account - """ + """The distribution assigned to all the lines is assigned to the request""" product_id = self.env.ref("product.product_product_9") pr = self.env["purchase.request"].create( { @@ -70,7 +68,7 @@ def test_all_analytic_accounts(self): "product_id": product_id.id, "product_qty": 1.0, "product_uom_id": self.env.ref("uom.product_uom_unit").id, - "analytic_account_id": self.anal.id, + "analytic_distribution": self.analytic_distribution, }, ), ( @@ -81,23 +79,28 @@ def test_all_analytic_accounts(self): "product_id": product_id.id, "product_qty": 1.0, "product_uom_id": self.env.ref("uom.product_uom_unit").id, - "analytic_account_id": self.anal.id, + "analytic_distribution": self.analytic_distribution, }, ), ], } ) - self.assertEqual(pr.analytic_account_id, self.anal) + self.assertEqual(pr.analytic_distribution, self.analytic_distribution) def test_not_all_analytic_accounts(self): - """Create a purchase request with diff analytic account - in the lines and check the purchase request has the - same analytic account + """The distribution assigned to some lines is not assigned to the request. + + If any distribution was set on the request, it is unset. """ product_id = self.env.ref("product.product_product_9") pr = self.env["purchase.request"].create( { "requested_by": self.env.user.id, + "analytic_distribution": self.analytic_distribution, + } + ) + pr.write( + { "line_ids": [ ( 0, @@ -107,7 +110,7 @@ def test_not_all_analytic_accounts(self): "product_id": product_id.id, "product_qty": 1.0, "product_uom_id": self.env.ref("uom.product_uom_unit").id, - "analytic_account_id": self.anal.id, + "analytic_distribution": self.analytic_distribution, }, ), ( @@ -118,10 +121,10 @@ def test_not_all_analytic_accounts(self): "product_id": product_id.id, "product_qty": 1.0, "product_uom_id": self.env.ref("uom.product_uom_unit").id, - "analytic_account_id": self.anal2.id, + "analytic_distribution": self.analytic_distribution2, }, ), ], } ) - self.assertNotEqual(pr.analytic_account_id, self.anal) + self.assertFalse(pr.analytic_distribution) diff --git a/purchase_request_analytic/views/purchase_request_views.xml b/purchase_request_analytic/views/purchase_request_views.xml index 1deafa7164..deae27028f 100644 --- a/purchase_request_analytic/views/purchase_request_views.xml +++ b/purchase_request_analytic/views/purchase_request_views.xml @@ -7,8 +7,10 @@ @@ -21,15 +23,23 @@ + {'default_analytic_account_id': analytic_account_id} + >{'default_analytic_distribution': analytic_distribution} @@ -39,17 +49,20 @@ purchase.request + + + - - -