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

[3758][IMP] stock_owner_restriction, mrp_stock_owner_restriction #83

Merged
merged 4 commits into from
Oct 10, 2023
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
1 change: 1 addition & 0 deletions mrp_stock_owner_restriction/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import mrp_production
from . import mrp_unbuild
from . import stock_move_line
from . import stock_move
26 changes: 26 additions & 0 deletions mrp_stock_owner_restriction/models/mrp_unbuild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class MrpUnbuild(models.Model):
_inherit = "mrp.unbuild"

# FIXME handle unbuild cases without MO
# owner_id = fields.Many2one(
# "res.partner",
# "Assign Owner",
# readonly=True,
# states={"draft": [("readonly", False)], "confirmed": [("readonly", False)]},
# check_company=True,
# help="Produced products will be assigned to this owner.",
# )
# owner_restriction = fields.Selection(related="picking_type_id.owner_restriction")

def action_validate(self):
owner_restriction = self.mo_id.picking_type_id.owner_restriction
owner = self.mo_id.owner_id
if owner and owner_restriction != "standard_behavior":
self = self.with_context(force_restricted_owner_id=owner)
return super().action_validate()
14 changes: 14 additions & 0 deletions mrp_stock_owner_restriction/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
class StockMove(models.Model):
_inherit = "stock.move"

def _get_mo_to_unbuild(self):
self.ensure_one()
return self.consume_unbuild_id.mo_id or self.unbuild_id.mo_id

def _get_owner_for_assign(self):
self.ensure_one()
if self.raw_material_production_id:
Expand All @@ -20,4 +24,14 @@ def _get_owner_for_assign(self):
production = self.move_dest_ids.raw_material_production_id
if production:
return production.owner_id
mo_to_unbuild = self._get_mo_to_unbuild()
if mo_to_unbuild:
return mo_to_unbuild.owner_id
return super()._get_owner_for_assign()

def _get_owner_restriction(self):
self.ensure_one()
mo_to_unbuild = self._get_mo_to_unbuild()
if mo_to_unbuild:
return mo_to_unbuild.picking_type_id.owner_restriction
return super()._get_owner_restriction()
4 changes: 3 additions & 1 deletion mrp_stock_owner_restriction/models/stock_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class StockMoveLine(models.Model):

def _action_done(self):
for line in self:
owner = line.move_id.production_id.owner_id
owner = line.move_id.production_id.owner_id or self.env.context.get(
"force_restricted_owner_id", None
)
if owner:
line.move_id.write({"restrict_partner_id": owner.id})
line.write({"owner_id": owner.id})
Expand Down
8 changes: 4 additions & 4 deletions stock_owner_restriction/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def _compute_quantities_dict(
return super()._compute_quantities_dict(
lot_id, owner_id, package_id, from_date=from_date, to_date=to_date
)
restricted_owner_id = self.env.context.get("force_restricted_owner_id", None)
if owner_id is None and restricted_owner_id is None:
restricted_owner = self.env.context.get("force_restricted_owner_id", None)
if owner_id is None and restricted_owner is None:
# Force owner to False if is None
owner_id = False
elif restricted_owner_id:
owner_id = restricted_owner_id
elif restricted_owner:
owner_id = restricted_owner.id
return super()._compute_quantities_dict(
lot_id, owner_id, package_id, from_date=from_date, to_date=to_date
)
Expand Down
13 changes: 11 additions & 2 deletions stock_owner_restriction/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2020 Carlos Dauden - Tecnativa
# Copyright 2020 Sergio Teruel - Tecnativa
# Copyright 2023 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from collections import defaultdict

Expand All @@ -19,9 +20,16 @@ def _get_moves_to_assign_with_standard_behavior(self):
lambda m: m.picking_type_id.owner_restriction == "standard_behavior"
)

def _get_owner_restriction(self):
"""This method is expected to be extended as necessary. e.g. different logic
needs to be applied to moves in unbuild orders.
"""
self.ensure_one()
return self.picking_type_id.owner_restriction

def _get_owner_for_assign(self):
"""This method is expected to be extended as necessary. e.g. different logic
needs to be applied for moves in manufacturing orders.
needs to be applied to moves in manufacturing orders.
"""
self.ensure_one()
partner = self.move_dest_ids.picking_id.owner_id
Expand All @@ -36,7 +44,8 @@ def _action_assign(self, force_qty=False):
res = super(StockMove, moves)._action_assign(force_qty=force_qty)
dict_key = defaultdict(lambda: self.env["stock.move"])
for move in self - moves:
if move.picking_type_id.owner_restriction == "unassigned_owner":
owner_restriction = move._get_owner_restriction()
if owner_restriction == "unassigned_owner":
dict_key[False] |= move
else:
partner = move._get_owner_for_assign()
Expand Down
80 changes: 71 additions & 9 deletions stock_owner_restriction/models/stock_quant.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2020 Carlos Dauden - Tecnativa
# Copyright 2020 Sergio Teruel - Tecnativa
# Copyright 2023 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, models
from odoo.osv import expression
Expand All @@ -25,20 +26,33 @@ def _gather(
owner_id=owner_id,
strict=strict,
)
restricted_owner_id = self.env.context.get("force_restricted_owner_id", None)
if owner_id is None or restricted_owner_id is None:
restricted_owner = self.env.context.get("force_restricted_owner_id", None)
if owner_id is None or restricted_owner is None:
return records
return records.filtered(
lambda q: q.owner_id == (restricted_owner_id or self.env["res.partner"])
lambda q: q.owner_id == (restricted_owner or self.env["res.partner"])
)

@api.model
def read_group(
self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True
):
restricted_owner_id = self.env.context.get("force_restricted_owner_id", None)
if restricted_owner_id is not None:
domain = expression.AND([domain, [("owner_id", "=", restricted_owner_id)]])
owner_in_domain = any(t[0] == "owner_id" for t in domain)
if not owner_in_domain:
restricted_owner = self.env.context.get("force_restricted_owner_id", None)
if restricted_owner is not None:
domain = expression.AND(
[
domain,
[
(
"owner_id",
"=",
restricted_owner and restricted_owner.id or False,
)
],
]
)
return super(StockQuant, self).read_group(
domain,
fields,
Expand All @@ -60,9 +74,9 @@ def _get_available_quantity(
strict=False,
allow_negative=False,
):
restricted_owner_id = self.env.context.get("force_restricted_owner_id", None)
if not owner_id and restricted_owner_id is not None:
owner_id = restricted_owner_id
restricted_owner = self.env.context.get("force_restricted_owner_id", None)
if not owner_id and restricted_owner is not None:
owner_id = restricted_owner
return super()._get_available_quantity(
product_id,
location_id,
Expand All @@ -72,3 +86,51 @@ def _get_available_quantity(
strict=strict,
allow_negative=allow_negative,
)

@api.model
def _update_available_quantity(
self,
product_id,
location_id,
quantity,
lot_id=None,
package_id=None,
owner_id=None,
in_date=None,
):
restricted_owner = self.env.context.get("force_restricted_owner_id", None)
if not owner_id and restricted_owner is not None:
owner_id = restricted_owner
return super()._update_available_quantity(
product_id,
location_id,
quantity,
lot_id=lot_id,
package_id=package_id,
owner_id=owner_id,
in_date=in_date,
)

@api.model
def _update_reserved_quantity(
self,
product_id,
location_id,
quantity,
lot_id=None,
package_id=None,
owner_id=None,
strict=False,
):
restricted_owner = self.env.context.get("force_restricted_owner_id", None)
if not owner_id and restricted_owner is not None:
owner_id = restricted_owner
return super()._update_reserved_quantity(
product_id,
location_id,
quantity,
lot_id=lot_id,
package_id=package_id,
owner_id=owner_id,
strict=strict,
)
Loading