Skip to content

Commit

Permalink
[MIG] procurement_mto_analytic: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AungKoKoLin1997 committed Mar 27, 2024
1 parent 3b7d974 commit 9ffe1c1
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 49 deletions.
12 changes: 6 additions & 6 deletions procurement_mto_analytic/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Purchase Analytic (MTO)
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:5422c8951e5b5e292bcb68881aac0c7296bd9abe57ed26b8eecf69b555fef59c
!! source digest: sha256:541299b9d83e8dca12c223d23e8d78357360045ca1f7272edd4f4120da984de7
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -17,13 +17,13 @@ Purchase Analytic (MTO)
: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/15.0/procurement_mto_analytic
:target: https://github.com/OCA/account-analytic/tree/16.0/procurement_mto_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-15-0/account-analytic-15-0-procurement_mto_analytic
:target: https://translation.odoo-community.org/projects/account-analytic-16-0/account-analytic-16-0-procurement_mto_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=15.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/account-analytic&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -74,7 +74,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-analytic/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/account-analytic/issues/new?body=module:%20procurement_mto_analytic%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/account-analytic/issues/new?body=module:%20procurement_mto_analytic%0Aversion:%2016.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.

Expand Down Expand Up @@ -125,6 +125,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 <https://github.com/OCA/account-analytic/tree/15.0/procurement_mto_analytic>`_ project on GitHub.
This module is part of the `OCA/account-analytic <https://github.com/OCA/account-analytic/tree/16.0/procurement_mto_analytic>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
8 changes: 4 additions & 4 deletions procurement_mto_analytic/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

{
"name": "Purchase Analytic (MTO)",
"summary": "This module sets analytic account in purchase order line from "
"sale order analytic account",
"version": "15.0.1.0.0",
"summary": "This module sets analytic distribution in purchase order line from "
"sale order line analytic distribution",
"version": "16.0.1.0.0",
"category": "Analytic",
"license": "AGPL-3",
"author": "Tecnativa, VentorTech, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-analytic",
"depends": ["sale_stock", "purchase_stock"],
"depends": ["purchase_stock", "sale_stock"],
"installable": True,
}
2 changes: 1 addition & 1 deletion procurement_mto_analytic/models/purchase_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ def _prepare_purchase_order_line_from_procurement(
)._prepare_purchase_order_line_from_procurement(
product_id, product_qty, product_uom, company_id, values, po
)
res["account_analytic_id"] = values.get("account_analytic_id", False)
res["analytic_distribution"] = values.get("analytic_distribution", False)
return res
4 changes: 2 additions & 2 deletions procurement_mto_analytic/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class SaleOrderLine(models.Model):

def _prepare_procurement_values(self, group_id=False):
res = super()._prepare_procurement_values(group_id)
res.update({"account_analytic_id": self.order_id.analytic_account_id.id})
res.update({"analytic_distribution": self.analytic_distribution})
return res

def _purchase_service_prepare_line_values(self, purchase_order, quantity=False):
res = super()._purchase_service_prepare_line_values(
purchase_order=purchase_order, quantity=quantity
)
res.update({"account_analytic_id": self.order_id.analytic_account_id.id})
res.update({"analytic_distribution": self.analytic_distribution})
return res
10 changes: 7 additions & 3 deletions procurement_mto_analytic/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class StockMove(models.Model):

def _prepare_procurement_values(self):
res = super()._prepare_procurement_values()
res.update(
{"account_analytic_id": self.group_id.sale_id.analytic_account_id.id}
)
analytic_distribution = self.sale_line_id.analytic_distribution
# In case of using stock_analytic and mrp_stock_analytic
if not analytic_distribution and hasattr(self, "analytic_distribution"):
analytic_distribution = self.analytic_distribution

Check warning on line 14 in procurement_mto_analytic/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

procurement_mto_analytic/models/stock_move.py#L14

Added line #L14 was not covered by tests
# Update the procurement values with analytic_distribution if it exists
if analytic_distribution:
res.update({"analytic_distribution": analytic_distribution})
return res
39 changes: 30 additions & 9 deletions procurement_mto_analytic/models/stock_rule.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import json

from odoo import models


class StockRule(models.Model):
_inherit = "stock.rule"

def _find_matching_analytic_distribution_record(self, analytic_distribution):
# Prepare the JSONB structure as a string for the SQL query
analytic_distribution_str = json.dumps(analytic_distribution)
# Use a parameterized query for safety
query = """
SELECT id FROM purchase_order_line
WHERE analytic_distribution::jsonb = %s::jsonb;
"""
self.env.cr.execute(query, (analytic_distribution_str,))
result = self.env.cr.fetchall()
# Extract IDs from the result
matching_po_line = [res[0] for res in result]
return matching_po_line

def _make_po_get_domain(self, company_id, values, partner):
res = super()._make_po_get_domain(company_id, values, partner)
res += (
(
"order_line.account_analytic_id",
"=",
values.get("account_analytic_id", False),
),
)
return res
domain = super()._make_po_get_domain(company_id, values, partner)
if values.get("analytic_distribution"):
# Fetch matching record IDs based on dynamic analytic_distribution
matching_po_line = self._find_matching_analytic_distribution_record(
values["analytic_distribution"]
)
if matching_po_line:
domain += (("order_line", "in", tuple(matching_po_line)),)

Check warning on line 33 in procurement_mto_analytic/models/stock_rule.py

View check run for this annotation

Codecov / codecov/patch

procurement_mto_analytic/models/stock_rule.py#L33

Added line #L33 was not covered by tests
else:
# To create new PO
domain += (("order_line", "=", 0000000),)
else:
domain += (("order_line.analytic_distribution", "=", False),)

Check warning on line 38 in procurement_mto_analytic/models/stock_rule.py

View check run for this annotation

Codecov / codecov/patch

procurement_mto_analytic/models/stock_rule.py#L38

Added line #L38 was not covered by tests
return domain
8 changes: 4 additions & 4 deletions procurement_mto_analytic/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ <h1 class="title">Purchase Analytic (MTO)</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:5422c8951e5b5e292bcb68881aac0c7296bd9abe57ed26b8eecf69b555fef59c
!! source digest: sha256:541299b9d83e8dca12c223d23e8d78357360045ca1f7272edd4f4120da984de7
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/account-analytic/tree/15.0/procurement_mto_analytic"><img alt="OCA/account-analytic" src="https://img.shields.io/badge/github-OCA%2Faccount--analytic-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/account-analytic-15-0/account-analytic-15-0-procurement_mto_analytic"><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/account-analytic&amp;target_branch=15.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/account-analytic/tree/16.0/procurement_mto_analytic"><img alt="OCA/account-analytic" src="https://img.shields.io/badge/github-OCA%2Faccount--analytic-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/account-analytic-16-0/account-analytic-16-0-procurement_mto_analytic"><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/account-analytic&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>This module takes account analytic value from sale order to the created
purchase order line.</p>
<p><strong>Table of contents</strong></p>
Expand Down Expand Up @@ -426,7 +426,7 @@ <h1><a class="toc-backref" href="#toc-entry-7">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/account-analytic/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/account-analytic/issues/new?body=module:%20procurement_mto_analytic%0Aversion:%2015.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/account-analytic/issues/new?body=module:%20procurement_mto_analytic%0Aversion:%2016.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 @@ -472,7 +472,7 @@ <h2><a class="toc-backref" href="#toc-entry-11">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/account-analytic/tree/15.0/procurement_mto_analytic">OCA/account-analytic</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/account-analytic/tree/16.0/procurement_mto_analytic">OCA/account-analytic</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
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class TestPurchaseProcurementAnalytic(common.TransactionCase):
@classmethod
def setUpClass(cls):
super(TestPurchaseProcurementAnalytic, cls).setUpClass()
vendor = cls.env["res.partner"].create({"name": "Partner #2"})
supplierinfo = cls.env["product.supplierinfo"].create({"name": vendor.id})
cls.vendor = cls.env["res.partner"].create({"name": "Partner #2"})
supplierinfo = cls.env["product.supplierinfo"].create(
{"partner_id": cls.vendor.id}
)
mto = cls.env.ref("stock.route_warehouse0_mto")
mto.write({"active": True})
buy = cls.env.ref("purchase_stock.route_warehouse0_buy")
Expand All @@ -22,7 +24,7 @@ def setUpClass(cls):
}
)
supplierinfo_service = cls.env["product.supplierinfo"].create(
{"name": vendor.id}
{"partner_id": cls.vendor.id}
)
cls.service_product = cls.env["product.product"].create(
{
Expand All @@ -33,15 +35,14 @@ def setUpClass(cls):
}
)
cls.partner = cls.env["res.partner"].create({"name": "Partner #1"})
cls.analytic_distribution = dict(
{str(cls.env.ref("analytic.analytic_agrolait").id): 100.0}
)

def test_sale_to_procurement(self):
analytic_account = self.env["account.analytic.account"].create(
{"name": "Test Analytic Account"}
)
sale_order = self.env["sale.order"].create(
{
"partner_id": self.partner.id,
"analytic_account_id": analytic_account.id,
"order_line": [
(
0,
Expand All @@ -51,36 +52,36 @@ def test_sale_to_procurement(self):
"product_uom_qty": 1,
"price_unit": self.product.list_price,
"name": self.product.name,
"analytic_distribution": self.analytic_distribution,
},
)
],
"picking_policy": "direct",
}
)
sale_order.with_context(test_enabled=True).action_confirm()

purchase_order = self.env["purchase.order.line"].search(
[("account_analytic_id", "=", analytic_account.id)]
purchase_order_line = self.env["purchase.order.line"].search(
[("partner_id", "=", self.vendor.id)]
)
self.assertTrue(purchase_order_line)
self.assertEqual(
purchase_order_line.analytic_distribution, self.analytic_distribution
)
self.assertTrue(purchase_order)

def test_sale_service_product(self):
analytic_account = self.env["account.analytic.account"].create(
{"name": "Test Service Analytic Account"}
)
sale_order = self.env["sale.order"].create(
{
"partner_id": self.partner.id,
"analytic_account_id": analytic_account.id,
"order_line": [
(
0,
0,
{
"product_id": self.service_product.id,
"product_uom_qty": 1,
"price_unit": self.product.list_price,
"name": self.product.name,
"price_unit": self.service_product.list_price,
"name": self.service_product.name,
"analytic_distribution": self.analytic_distribution,
},
)
],
Expand All @@ -89,7 +90,10 @@ def test_sale_service_product(self):
)
sale_order.with_context(test_enabled=True).action_confirm()

purchase_order = self.env["purchase.order.line"].search(
[("account_analytic_id", "=", analytic_account.id)]
purchase_order_line = self.env["purchase.order.line"].search(
[("partner_id", "=", self.vendor.id)]
)
self.assertTrue(purchase_order_line)
self.assertTrue(
purchase_order_line.analytic_distribution, self.analytic_distribution
)
self.assertTrue(purchase_order)

0 comments on commit 9ffe1c1

Please sign in to comment.