Skip to content

Commit

Permalink
[MIG] stock_request: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
celm1990 committed Apr 12, 2024
1 parent 1f5c4bf commit 159123c
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 299 deletions.
2 changes: 1 addition & 1 deletion stock_request/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Stock Request",
"summary": "Internal request for stock",
"version": "15.0.1.7.0",
"version": "17.0.1.0.0",
"license": "LGPL-3",
"website": "https://github.com/OCA/stock-logistics-request",
"author": "ForgeFlow, Odoo Community Association (OCA)",
Expand Down
14 changes: 0 additions & 14 deletions stock_request/migrations/15.0.1.4.0/noupdate_changes.xml

This file was deleted.

10 changes: 0 additions & 10 deletions stock_request/migrations/15.0.1.4.0/post-migration.py

This file was deleted.

2 changes: 1 addition & 1 deletion stock_request/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
from . import res_config_settings
from . import stock_warehouse
from . import stock_location
from . import stock_location_route
from . import stock_route
from . import res_company
4 changes: 2 additions & 2 deletions stock_request/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _compute_stock_request_ids(self):
rec.stock_request_ids = rec.allocation_ids.mapped("stock_request_id")

def _merge_moves_fields(self):
res = super(StockMove, self)._merge_moves_fields()
res = super()._merge_moves_fields()
res["allocation_ids"] = [(4, m.id) for m in self.mapped("allocation_ids")]
return res

Expand Down Expand Up @@ -65,7 +65,7 @@ def copy_data(self, default=None):
},
)
)
return super(StockMove, self).copy_data(default)
return super().copy_data(default)

def _action_cancel(self):
"""Apply sudo to prevent requests ACL errors if the user does not have
Expand Down
8 changes: 4 additions & 4 deletions stock_request/models/stock_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ def _prepare_message_data(self, ml, request, allocated_qty):
return {
"request_name": request.name,
"picking_name": ml.picking_id.name,
"product_name": ml.product_id.name_get()[0][1],
"product_name": ml.product_id.display_name,
"product_qty": allocated_qty,
"product_uom": ml.product_uom_id.name,
"location_name": ml.location_dest_id.name_get()[0][1],
"location_name": ml.location_dest_id.display_name,
}

def _action_done(self):
res = super(StockMoveLine, self)._action_done()
res = super()._action_done()
for ml in self.filtered(lambda m: m.exists() and m.move_id.allocation_ids):
qty_done = ml.product_uom_id._compute_quantity(
ml.qty_done, ml.product_id.uom_id
ml.quantity, ml.product_id.uom_id
)

# We do sudo because potentially the user that completes the move
Expand Down
4 changes: 2 additions & 2 deletions stock_request/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class StockPicking(models.Model):
"Stock Request #", compute="_compute_stock_request_ids"
)

@api.depends("move_lines")
@api.depends("move_ids")
def _compute_stock_request_ids(self):
for rec in self:
rec.stock_request_ids = rec.move_lines.mapped("stock_request_ids")
rec.stock_request_ids = rec.move_ids.mapped("stock_request_ids")
rec.stock_request_count = len(rec.stock_request_ids)

def action_view_stock_request(self):
Expand Down
127 changes: 73 additions & 54 deletions stock_request/models/stock_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _get_default_requested_by(self):
def _get_expected_date():
return fields.Datetime.now()

name = fields.Char(states={"draft": [("readonly", False)]})
name = fields.Char()
state = fields.Selection(
selection=[
("draft", "Draft"),
Expand All @@ -43,8 +43,6 @@ def _get_expected_date():
expected_date = fields.Datetime(
index=True,
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
help="Date when you expect to receive the goods.",
)
picking_policy = fields.Selection(
Expand All @@ -54,8 +52,6 @@ def _get_expected_date():
],
string="Shipping Policy",
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
default="direct",
)
move_ids = fields.One2many(
Expand Down Expand Up @@ -102,24 +98,14 @@ def _get_expected_date():
string="Stock Request Allocation",
)
order_id = fields.Many2one("stock.request.order", readonly=True)
warehouse_id = fields.Many2one(
states={"draft": [("readonly", False)]}, readonly=True
)
location_id = fields.Many2one(
states={"draft": [("readonly", False)]}, readonly=True
)
product_id = fields.Many2one(states={"draft": [("readonly", False)]}, readonly=True)
product_uom_id = fields.Many2one(
states={"draft": [("readonly", False)]}, readonly=True
)
product_uom_qty = fields.Float(
states={"draft": [("readonly", False)]}, readonly=True
)
procurement_group_id = fields.Many2one(
states={"draft": [("readonly", False)]}, readonly=True
)
company_id = fields.Many2one(states={"draft": [("readonly", False)]}, readonly=True)
route_id = fields.Many2one(states={"draft": [("readonly", False)]}, readonly=True)
warehouse_id = fields.Many2one()
location_id = fields.Many2one()
product_id = fields.Many2one()
product_uom_id = fields.Many2one()
product_uom_qty = fields.Float()
procurement_group_id = fields.Many2one()
company_id = fields.Many2one()
route_id = fields.Many2one()

_sql_constraints = [
("name_uniq", "unique(name, company_id)", "Stock Request name must be unique")
Expand Down Expand Up @@ -148,7 +134,7 @@ def _compute_picking_ids(self):
"allocation_ids",
"allocation_ids.stock_move_id.state",
"allocation_ids.stock_move_id.move_line_ids",
"allocation_ids.stock_move_id.move_line_ids.qty_done",
"allocation_ids.stock_move_id.move_line_ids.quantity",
)
def _compute_qty(self):
for request in self:
Expand Down Expand Up @@ -180,41 +166,70 @@ def _compute_qty(self):

@api.constrains("order_id", "requested_by")
def check_order_requested_by(self):
if self.order_id and self.order_id.requested_by != self.requested_by:
raise ValidationError(_("Requested by must be equal to the order"))
for stock_request in self:
if (
stock_request.order_id
and stock_request.order_id.requested_by != stock_request.requested_by
):
raise ValidationError(_("Requested by must be equal to the order"))

@api.constrains("order_id", "warehouse_id")
def check_order_warehouse_id(self):
if self.order_id and self.order_id.warehouse_id != self.warehouse_id:
raise ValidationError(_("Warehouse must be equal to the order"))
for stock_request in self:
if (
stock_request.order_id
and stock_request.order_id.warehouse_id != stock_request.warehouse_id
):
raise ValidationError(_("Warehouse must be equal to the order"))

@api.constrains("order_id", "location_id")
def check_order_location(self):
if self.order_id and self.order_id.location_id != self.location_id:
raise ValidationError(_("Location must be equal to the order"))
for stock_request in self:
if (
stock_request.order_id
and stock_request.order_id.location_id != stock_request.location_id
):
raise ValidationError(_("Location must be equal to the order"))

@api.constrains("order_id", "procurement_group_id")
def check_order_procurement_group(self):
if (
self.order_id
and self.order_id.procurement_group_id != self.procurement_group_id
):
raise ValidationError(_("Procurement group must be equal to the order"))
for stock_request in self:
if (
stock_request.order_id
and stock_request.order_id.procurement_group_id
!= stock_request.procurement_group_id
):
raise ValidationError(_("Procurement group must be equal to the order"))

@api.constrains("order_id", "company_id")
def check_order_company(self):
if self.order_id and self.order_id.company_id != self.company_id:
raise ValidationError(_("Company must be equal to the order"))
for stock_request in self:
if (
stock_request.order_id
and stock_request.order_id.company_id != stock_request.company_id
):
raise ValidationError(_("Company must be equal to the order"))

@api.constrains("order_id", "expected_date")
def check_order_expected_date(self):
if self.order_id and self.order_id.expected_date != self.expected_date:
raise ValidationError(_("Expected date must be equal to the order"))
for stock_request in self:
if (
stock_request.order_id
and stock_request.order_id.expected_date != stock_request.expected_date
):
raise ValidationError(_("Expected date must be equal to the order"))

@api.constrains("order_id", "picking_policy")
def check_order_picking_policy(self):
if self.order_id and self.order_id.picking_policy != self.picking_policy:
raise ValidationError(_("The picking policy must be equal to the order"))
for stock_request in self:
if (
stock_request.order_id
and stock_request.order_id.picking_policy
!= stock_request.picking_policy
):
raise ValidationError(
_("The picking policy must be equal to the order")
)

def _action_confirm(self):
self._action_launch_procurement_rule()
Expand Down Expand Up @@ -329,7 +344,8 @@ def _action_use_stock_available(self):
pending_qty -= qty_move
# Create allocation + done move
allocation_model.create(self._prepare_stock_request_allocation(move))
move.quantity_done = move.product_uom_qty
move.quantity = move.product_uom_qty
move.picked = True
move._action_done()

def _action_launch_procurement_rule(self):
Expand Down Expand Up @@ -405,19 +421,22 @@ def action_view_transfer(self):
action["res_id"] = pickings.id
return action

@api.model
def create(self, vals):
upd_vals = vals.copy()
if upd_vals.get("name", "/") == "/":
upd_vals["name"] = self.env["ir.sequence"].next_by_code("stock.request")
if "order_id" in upd_vals:
order_id = self.env["stock.request.order"].browse(upd_vals["order_id"])
upd_vals["expected_date"] = order_id.expected_date
else:
upd_vals["expected_date"] = self._get_expected_date()
return super().create(upd_vals)
@api.model_create_multi
def create(self, vals_list):
vals_list_upd = []
for vals in vals_list:
upd_vals = vals.copy()
if upd_vals.get("name", "/") == "/":
upd_vals["name"] = self.env["ir.sequence"].next_by_code("stock.request")
if "order_id" in upd_vals:
order_id = self.env["stock.request.order"].browse(upd_vals["order_id"])
upd_vals["expected_date"] = order_id.expected_date
else:
upd_vals["expected_date"] = self._get_expected_date()
vals_list_upd.append(upd_vals)
return super().create(vals_list_upd)

def unlink(self):
if self.filtered(lambda r: r.state != "draft"):
raise UserError(_("Only requests on draft state can be unlinked"))
return super(StockRequest, self).unlink()
return super().unlink()
15 changes: 7 additions & 8 deletions stock_request/models/stock_request_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StockRequest(models.AbstractModel):

@api.model
def default_get(self, fields):
res = super(StockRequest, self).default_get(fields)
res = super().default_get(fields)
warehouse = None
if "warehouse_id" not in res and res.get("company_id"):
warehouse = self.env["stock.warehouse"].search(
Expand Down Expand Up @@ -94,14 +94,14 @@ def _compute_product_qty(self):
"res.company", "Company", required=True, default=lambda self: self.env.company
)
route_id = fields.Many2one(
"stock.location.route",
"stock.route",
string="Route",
domain="[('id', 'in', route_ids)]",
ondelete="restrict",
)

route_ids = fields.Many2many(
"stock.location.route",
"stock.route",
string="Routes",
compute="_compute_route_ids",
readonly=True,
Expand All @@ -113,16 +113,14 @@ def _compute_product_qty(self):

@api.depends("product_id", "warehouse_id", "location_id")
def _compute_route_ids(self):
route_obj = self.env["stock.location.route"]
route_obj = self.env["stock.route"]
routes = route_obj.search(
[("warehouse_ids", "in", self.mapped("warehouse_id").ids)]
)
routes_by_warehouse = {}
for route in routes:
for warehouse in route.warehouse_ids:
routes_by_warehouse.setdefault(
warehouse.id, self.env["stock.location.route"]
)
routes_by_warehouse.setdefault(warehouse.id, self.env["stock.route"])
routes_by_warehouse[warehouse.id] |= route
for record in self:
routes = route_obj
Expand All @@ -133,8 +131,9 @@ def _compute_route_ids(self):
if record.warehouse_id and routes_by_warehouse.get(record.warehouse_id.id):
routes |= routes_by_warehouse[record.warehouse_id.id]
parents = record.get_parents().ids
# ruff: noqa: B023
record.route_ids = routes.filtered(
lambda r: any(p.location_id.id in parents for p in r.rule_ids)
lambda r: any(p.location_dest_id.id in parents for p in r.rule_ids)
)

def get_parents(self):
Expand Down
Loading

0 comments on commit 159123c

Please sign in to comment.