Skip to content

Commit

Permalink
[MIG] stock_request_analytic: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Sep 19, 2024
1 parent b267d68 commit e495aae
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 371 deletions.
7 changes: 6 additions & 1 deletion stock_request_analytic/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ accounts to stock requests.
Usage
=====

Assign the analytic account to the stock request.
To create stock moves with a specific analytic distribution, assign an
analytic distribution to each stock request or set an analytic
distribution on the stock request order as a default value for new stock
requests on that order.

Known issues / Roadmap
======================
Expand Down Expand Up @@ -77,6 +80,8 @@ Contributors

- João Marques

- Stefan Rijnhart <stefan@opener.amsterdam>

Maintainers
-----------

Expand Down
3 changes: 1 addition & 2 deletions stock_request_analytic/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Stock Request Analytic",
"summary": "Internal request for stock",
"version": "15.0.1.1.0",
"version": "17.0.1.0.0",
"license": "AGPL-3",
"website": "https://github.com/OCA/stock-logistics-request",
"author": "ForgeFlow, Odoo Community Association (OCA)",
Expand All @@ -14,7 +14,6 @@
"security/ir.model.access.csv",
"views/stock_request_views.xml",
"views/stock_request_order_views.xml",
"views/analytic_views.xml",
],
"installable": True,
}
32 changes: 32 additions & 0 deletions stock_request_analytic/migrations/16.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from openupgradelib.openupgrade_160 import fill_analytic_distribution

from odoo import SUPERUSER_ID, api
from odoo.tools.sql import table_exists


def migrate(cr, version):
env = api.Environment(cr, SUPERUSER_ID, {})
# Migrate stock_request's analytic account and tags
if table_exists(cr, "account_analytic_distribution"): # 16.0
fill_analytic_distribution(
env,
"stock_request",
"account_analytic_tag_stock_request_rel",
"stock_request_id",
)
else: # 17.0, analytic account only
cr.execute(
"""
update stock_request set analytic_distribution =
json_build_object(analytic_account_id::varchar, 100.0)
where analytic_account_id is not null;
"""
)
# Migrate stock_request_order's analytic account
cr.execute(
"""
update stock_request_order set analytic_distribution =
json_build_object(default_analytic_account_id::varchar, 100.0)
where default_analytic_account_id is not null;
"""
)
2 changes: 0 additions & 2 deletions stock_request_analytic/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from . import analytic
from . import stock_rule
from . import stock_request
from . import stock_request_order
29 changes: 0 additions & 29 deletions stock_request_analytic/models/analytic.py

This file was deleted.

35 changes: 10 additions & 25 deletions stock_request_analytic/models/stock_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,23 @@


class StockRequest(models.Model):
_inherit = "stock.request"
_check_company_auto = True
_name = "stock.request"
_inherit = ["analytic.mixin", "stock.request"]

analytic_account_id = fields.Many2one(
comodel_name="account.analytic.account",
string="Analytic Account",
compute="_compute_analytic_id",
store=True,
readonly=False,
check_company=True,
compute_sudo=True,
)
analytic_tag_ids = fields.Many2many(
comodel_name="account.analytic.tag",
string="Analytic Tags",
check_company=True,
)
analytic_distribution = fields.Json(compute="_compute_analytic_distribution")

@api.depends("order_id")
def _compute_analytic_id(self):
"""
Set default analytic account on lines from order if defined.
"""
def _compute_analytic_distribution(self):
"""Set default analytic distribution on lines from order if defined"""
for req in self:
if req.order_id and req.order_id.default_analytic_account_id:
req.analytic_account_id = req.order_id.default_analytic_account_id
if req.order_id.analytic_distribution:
req.analytic_distribution = req.order_id.analytic_distribution

def _prepare_procurement_values(self, group_id=False):
"""
Add analytic account to procurement values
Add analytic distribution to procurement values
"""
res = super()._prepare_procurement_values(group_id=group_id)
if self.analytic_account_id:
res.update({"analytic_account_id": self.analytic_account_id.id})
if self.analytic_distribution:
res.update({"analytic_distribution": self.analytic_distribution})
return res
78 changes: 3 additions & 75 deletions stock_request_analytic/models/stock_request_order.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,9 @@
# Copyright 2018 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

MAP_ACTIONS = {
"analytic_account": "analytic.action_account_analytic_account_form",
"analytic_tag": "analytic.account_analytic_tag_action",
}
MAP_FIELDS = {
"analytic_account": "analytic_account_ids",
"analytic_tag": "analytic_tag_ids",
}
MAP_VIEWS = {
"analytic_account": "analytic.view_account_analytic_account_form",
"analytic_tag": "analytic.account_analytic_tag_form_view",
}
from odoo import models


class StockRequestOrder(models.Model):
_inherit = "stock.request.order"

analytic_count = fields.Integer(
compute="_compute_analytic_ids",
readonly=True,
compute_sudo=True,
)
analytic_tag_count = fields.Integer(
compute="_compute_analytic_ids",
readonly=True,
compute_sudo=True,
)
analytic_account_ids = fields.One2many(
comodel_name="account.analytic.account",
compute="_compute_analytic_ids",
string="Analytic Accounts",
readonly=True,
compute_sudo=True,
)
analytic_tag_ids = fields.One2many(
comodel_name="account.analytic.tag",
compute="_compute_analytic_ids",
string="Analytic Tags",
readonly=True,
compute_sudo=True,
)
default_analytic_account_id = fields.Many2one(
comodel_name="account.analytic.account",
string="Default Analytic Account",
help="Set this if you want to define a default analytic account on requests",
)

@api.depends("stock_request_ids")
def _compute_analytic_ids(self):
for req in self:
req.analytic_account_ids = req.stock_request_ids.mapped(
"analytic_account_id"
)
req.analytic_tag_ids = req.stock_request_ids.mapped("analytic_tag_ids")
req.analytic_count = len(req.analytic_account_ids)
req.analytic_tag_count = len(req.analytic_tag_ids)

def action_view_analytic(self):
self.ensure_one()
analytic_type = self.env.context.get("analytic_type")
if not analytic_type:
raise ValidationError(
_("Analytic type (analytic_type) not present in the context")
)
xmlid = MAP_ACTIONS[analytic_type]
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
records = self.mapped(MAP_FIELDS[analytic_type])
if len(records) > 1:
action["domain"] = [("id", "in", records.ids)]
elif records:
action["views"] = [
(self.env.ref(MAP_VIEWS[self._context["analytic_type"]]).id, "form")
]
action["res_id"] = records.id
return action
_name = "stock.request.order"
_inherit = ["analytic.mixin", "stock.request.order"]
39 changes: 0 additions & 39 deletions stock_request_analytic/models/stock_rule.py

This file was deleted.

1 change: 1 addition & 0 deletions stock_request_analytic/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Denis Roussel \<<denis.roussel@acsone.eu>\>
- [Tecnativa](https://www.tecnativa.com):
- João Marques
- Stefan Rijnhart \<<stefan@opener.amsterdam>\>
4 changes: 3 additions & 1 deletion stock_request_analytic/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Assign the analytic account to the stock request.
To create stock moves with a specific analytic distribution, assign an analytic
distribution to each stock request or set an analytic distribution on the stock
request order as a default value for new stock requests on that order.
6 changes: 5 additions & 1 deletion stock_request_analytic/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@ <h1 class="title">Stock Request Analytic</h1>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>Assign the analytic account to the stock request.</p>
<p>To create stock moves with a specific analytic distribution, assign an
analytic distribution to each stock request or set an analytic
distribution on the stock request order as a default value for new stock
requests on that order.</p>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h1>
Expand Down Expand Up @@ -425,6 +428,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>João Marques</li>
</ul>
</li>
<li>Stefan Rijnhart &lt;<a class="reference external" href="mailto:stefan&#64;opener.amsterdam">stefan&#64;opener.amsterdam</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
Loading

0 comments on commit e495aae

Please sign in to comment.