Skip to content

Commit

Permalink
Merge PR #463 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by LoisRForgeFlow
  • Loading branch information
OCA-git-bot committed Jun 14, 2024
2 parents 83e065e + 194af98 commit e8a4616
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 deletions.
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
63 changes: 49 additions & 14 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 @@ class StockBuffer(models.Model):
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(
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))

def _daf_to_apply_domain(self, current=True):
self.ensure_one()
Expand All @@ -40,6 +60,9 @@ def _calc_adu(self):
# 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 _create_demand(bom, factor=1, level=0, clt=0):
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()
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
)
)

Expand Down Expand Up @@ -165,20 +189,23 @@ def _compute_dlt(self):
)
return res

def action_archive(self):
self.env["ddmrp.adjustment.demand"].search(
[("buffer_origin_id", "in", self.ids)]
).unlink()
return super().action_archive()

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_type": "form",
"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 +219,11 @@ def action_view_affecting_adu(self):
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>
24 changes: 15 additions & 9 deletions ddmrp_adjustment/views/stock_buffer_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,35 @@
<field name="model">stock.buffer</field>
<field name="inherit_id" ref="ddmrp.stock_buffer_view_form" />
<field name="arch" type="xml">
<xpath
expr="//div[@name='button_box']//button[@name='refresh_buffer']"
position="before"
>
<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

0 comments on commit e8a4616

Please sign in to comment.