Skip to content

Commit

Permalink
[IMP] ddmrp: Autocalculate buffer in Manufacturing Orders
Browse files Browse the repository at this point in the history
Perform a calculation by level in which the location level is first searched and if not found it is searched at the warehouse level.
  • Loading branch information
BernatPForgeFlow committed Aug 28, 2023
1 parent 16e18ff commit 300750b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions ddmrp/models/mrp_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,51 @@ class MrpProduction(models.Model):
@api.model
def create(self, vals):
record = super(MrpProduction, self).create(vals)
record._find_buffer_link()
record._calc_execution_priority()
return record

def write(self, vals):
res = super().write(vals)
if any(
f in vals
for f in ("product_id", "picking_type_id", "location_dest_id", "company_id")
):
self._find_buffer_link()
return res

def _get_domain_buffer_link(self, warehouse_level=False):
self.ensure_one()
if not warehouse_level:
locations = self.env["stock.location"].search(
[("id", "child_of", [self.location_dest_id.id])]
)
return [
("product_id", "=", self.product_id.id),
("company_id", "=", self.company_id.id),
("buffer_profile_id.item_type", "=", "manufactured"),
("location_id", "in", locations.ids),
]
else:
return [
("product_id", "=", self.product_id.id),
("company_id", "=", self.company_id.id),
("buffer_profile_id.item_type", "=", "manufactured"),
("warehouse_id", "=", self.picking_type_id.warehouse_id.id),
]

def _find_buffer_link(self):
buffer_model = self.env["stock.buffer"]
for rec in self:
domain = rec._get_domain_buffer_link()
buffer = buffer_model.search(domain, limit=1)
if not buffer:
domain = rec._get_domain_buffer_link(warehouse_level=True)
buffer = buffer_model.search(domain, limit=1)
rec.buffer_id = buffer
if buffer:
rec._calc_execution_priority()

def _calc_execution_priority(self):
"""Technical note: this method cannot be decorated with api.depends,
otherwise it would generate a infinite recursion."""
Expand Down

0 comments on commit 300750b

Please sign in to comment.