Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] ddmrp_adjustment: visualize parent DAFs #423

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ddmrp_adjustment/models/ddmrp_adjustment_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class DdmrpAdjustmentDemand(models.Model):
buffer_origin_id = fields.Many2one(
comodel_name="stock.buffer",
string="Originated from",
required=True,
ondelete="cascade",
)
product_origin_id = fields.Many2one(
related="buffer_origin_id.product_id",
Expand Down
62 changes: 49 additions & 13 deletions ddmrp_adjustment/models/stock_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from datetime import timedelta as td

from odoo import _, api, fields, models
from odoo import api, fields, models

from ..models.ddmrp_adjustment import DAF_string, LTAF_string

Expand All @@ -21,8 +21,28 @@
help="Demand associated to Demand Adjustment Factors applied to "
"parent buffers.",
)
daf_text = fields.Char(compute="_compute_daf_text")
parent_daf_text = fields.Char(compute="_compute_daf_text")
pre_daf_adu = fields.Float(readonly=True)
daf_applied = fields.Float(default=-1, readonly=True)
parent_daf_applied = fields.Float(default=-1, readonly=True)
count_ddmrp_adjustment_demand = fields.Integer(
compute="_compute_count_ddmrp_adjustment_demand"
)

def _compute_count_ddmrp_adjustment_demand(self):
for rec in self:
rec.count_ddmrp_adjustment_demand = len(

Check warning on line 35 in ddmrp_adjustment/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

ddmrp_adjustment/models/stock_buffer.py#L35

Added line #L35 was not covered by tests
self.env["ddmrp.adjustment.demand"].search(
[("buffer_origin_id", "=", rec.id)]
)
)

@api.depends("daf_applied", "parent_daf_applied")
def _compute_daf_text(self):
for rec in self:
rec.daf_text = "DAF: *" + str(round(rec.daf_applied, 2))
rec.parent_daf_text = "P. DAF: +" + str(round(rec.parent_daf_applied, 2))

Check warning on line 45 in ddmrp_adjustment/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

ddmrp_adjustment/models/stock_buffer.py#L44-L45

Added lines #L44 - L45 were not covered by tests

def _daf_to_apply_domain(self, current=True):
self.ensure_one()
Expand All @@ -40,6 +60,9 @@
# Apply DAFs if existing for the buffer.
res = super()._calc_adu()
for rec in self:
self.env["ddmrp.adjustment.demand"].search(
[("buffer_origin_id", "=", rec.id)]
).unlink()
dafs_to_apply = self.env["ddmrp.adjustment"].search(
rec._daf_to_apply_domain()
)
Expand Down Expand Up @@ -118,20 +141,21 @@
def cron_ddmrp_adu(self, automatic=False):
"""Apply extra demand originated by Demand Adjustment Factors to
components after the cron update of all the buffers."""
self.env["ddmrp.adjustment.demand"].search([]).unlink()
res = super().cron_ddmrp_adu(automatic)
today = fields.Date.today()
for op in self.search([]).filtered("extra_demand_ids"):
to_add = sum(
op.parent_daf_applied = -1
daf_parent = sum(
op.extra_demand_ids.filtered(
lambda r: r.date_start <= today <= r.date_end
).mapped("extra_demand")
)
if to_add:
op.adu += to_add
if daf_parent:
op.parent_daf_applied = daf_parent
op.adu += op.parent_daf_applied
_logger.debug(
"DAFs-originated demand applied. {}: ADU += {}".format(
op.name, to_add
op.name, op.parent_daf_applied
)
)
return res
Expand Down Expand Up @@ -166,19 +190,23 @@
)
return res

def action_archive(self):
self.env["ddmrp.adjustment.demand"].search(

Check warning on line 194 in ddmrp_adjustment/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

ddmrp_adjustment/models/stock_buffer.py#L194

Added line #L194 was not covered by tests
[("buffer_origin_id", "in", self.ids)]
).unlink()
return super().action_archive()

Check warning on line 197 in ddmrp_adjustment/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

ddmrp_adjustment/models/stock_buffer.py#L197

Added line #L197 was not covered by tests

def action_view_demand_to_components(self):
demand_ids = (
self.env["ddmrp.adjustment.demand"]
.search([("buffer_origin_id", "=", self.id)])
.ids
)
return {
"name": _("Demand Allocated to Components"),
"type": "ir.actions.act_window",
"res_model": "ddmrp.adjustment.demand",
"view_mode": "tree",
"domain": [("id", "in", demand_ids)],
}
action = self.env["ir.actions.act_window"]._for_xml_id(
"ddmrp_adjustment.ddmrp_adjustment_demand_action"
)
action["domain"] = [("id", "in", demand_ids)]
return action

def action_view_affecting_adu(self):
demand_ids = (
Expand All @@ -192,3 +220,11 @@
action["domain"] = [("id", "in", demand_ids)]
action["context"] = {"search_default_current": 1}
return action

def action_view_parent_affecting_adu(self):
demand_ids = self.extra_demand_ids.ids
action = self.env["ir.actions.act_window"]._for_xml_id(
"ddmrp_adjustment.ddmrp_adjustment_demand_action"
)
action["domain"] = [("id", "in", demand_ids)]
return action
1 change: 1 addition & 0 deletions ddmrp_adjustment/tests/test_adu_dlt_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ def test_dummy(self):
# Run actions
self.assertTrue(self.buffer.action_view_demand_to_components())
self.assertTrue(self.buffer.action_view_affecting_adu())
self.assertTrue(self.buffer.action_view_parent_affecting_adu())
6 changes: 6 additions & 0 deletions ddmrp_adjustment/views/ddmrp_adjustment_demand_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
</tree>
</field>
</record>
<record id="ddmrp_adjustment_demand_action" model="ir.actions.act_window">
<field name="name">Buffer Adjustments Demand</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">ddmrp.adjustment.demand</field>
<field name="view_mode">tree</field>
</record>
</odoo>
19 changes: 14 additions & 5 deletions ddmrp_adjustment/views/stock_buffer_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@
<field name="inherit_id" ref="ddmrp.stock_buffer_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']" position="inside">
<field name="count_ddmrp_adjustment_demand" invisible="1" />
<button
string="DAF Demand Allocation"
name="action_view_demand_to_components"
class="oe_stat_button"
icon="fa-caret-square-o-down"
type="object"
attrs="{'invisible': [('count_ddmrp_adjustment_demand', '=', 0)]}"
/>
</xpath>
<button name="action_view_future_adu_indirect_demand" position="after">
<field name="daf_applied" invisible="1" />
<field name="parent_daf_applied" invisible="1" />
<button
title="View DAF Affecting Actual ADU"
title="View DAFs Affecting Actual ADU"
name="action_view_affecting_adu"
type="object"
attrs="{'invisible': [('daf_applied', '=', -1)]}"
>
<div name="daf_applied" class="o_row">
DAF: *
<field name="daf_applied" readonly="1" />
</div>
<field name="daf_text" readonly="1" />
</button>
<button
title="View Demand from parent DAFs Affecting Actual ADU"
name="action_view_parent_affecting_adu"
type="object"
attrs="{'invisible': [('parent_daf_applied', '=', -1)]}"
>
<field name="parent_daf_text" readonly="1" />
</button>
</button>
</field>
Expand Down
Loading