Skip to content

Commit

Permalink
[IMP] ddmrp_adjustment: delete DAD when updating parent buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidJForgeFlow committed Jun 12, 2024
1 parent 4094024 commit 6f63cea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 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
20 changes: 17 additions & 3 deletions ddmrp_adjustment/models/stock_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ class StockBuffer(models.Model):
pre_daf_adu = fields.Float(readonly=True)
daf_applied = fields.Float(default=-1, readonly=True)
parent_daf_applied = fields.Float(default=-1, readonly=True)
number_of_dad = fields.Integer(readonly=True)

@api.depends("daf_applied", "parent_daf_applied")
def _compute_daf_text(self):
for rec in self:
rec.daf_text = "DAF: *" + str(rec.daf_applied)
rec.parent_daf_text = "P. DAF: +" + str(rec.parent_daf_applied)
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 35 in ddmrp_adjustment/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

ddmrp_adjustment/models/stock_buffer.py#L34-L35

Added lines #L34 - L35 were not covered by tests

def _daf_to_apply_domain(self, current=True):
self.ensure_one()
Expand All @@ -49,6 +50,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 All @@ -73,6 +77,11 @@ def _calc_adu(self):
prev = rec.adu
increased_demand = prev * daf.value - prev
rec.explode_demand_to_components(daf, increased_demand, rec.product_uom)
rec.number_of_dad = len(
self.env["ddmrp.adjustment.demand"]
.search([("buffer_origin_id", "=", rec.id)])
.ids
)
return res

def explode_demand_to_components(self, daf, demand, uom_id):
Expand Down Expand Up @@ -127,7 +136,6 @@ 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()
res = super().cron_ddmrp_adu(automatic)
today = fields.Date.today()
for op in self.search([]).filtered("extra_demand_ids"):
Expand Down Expand Up @@ -177,6 +185,12 @@ def _compute_dlt(self):
)
return res

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

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

View check run for this annotation

Codecov / codecov/patch

ddmrp_adjustment/models/stock_buffer.py#L189

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

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

View check run for this annotation

Codecov / codecov/patch

ddmrp_adjustment/models/stock_buffer.py#L192

Added line #L192 was not covered by tests

def action_view_demand_to_components(self):
demand_ids = (
self.env["ddmrp.adjustment.demand"]
Expand Down
2 changes: 2 additions & 0 deletions ddmrp_adjustment/views/stock_buffer_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
<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="number_of_dad" 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': [('number_of_dad', '=', 0)]}"
/>
</xpath>
<button name="action_view_future_adu_indirect_demand" position="after">
Expand Down

0 comments on commit 6f63cea

Please sign in to comment.