mirror of
https://github.com/bringout/oca-technical.git
synced 2026-04-21 05:52:08 +02:00
Initial commit: OCA Technical packages (595 packages)
This commit is contained in:
commit
2cc02aac6e
24950 changed files with 2318079 additions and 0 deletions
|
|
@ -0,0 +1,14 @@
|
|||
from . import procurement_group
|
||||
from . import stock_request_abstract
|
||||
from . import stock_request
|
||||
from . import stock_request_allocation
|
||||
from . import stock_request_order
|
||||
from . import stock_move
|
||||
from . import stock_picking
|
||||
from . import stock_rule
|
||||
from . import stock_move_line
|
||||
from . import res_config_settings
|
||||
from . import stock_warehouse
|
||||
from . import stock_location
|
||||
from . import stock_route
|
||||
from . import res_company
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
# Copyright (C) 2019 Open Source Integrators
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
|
||||
class ProcurementGroup(models.Model):
|
||||
_inherit = "procurement.group"
|
||||
|
||||
@api.model
|
||||
def run(self, procurements, raise_user_error=True):
|
||||
indexes_to_pop = []
|
||||
new_procs = []
|
||||
for i, procurement in enumerate(procurements):
|
||||
if "stock_request_id" in procurement.values and procurement.values.get(
|
||||
"stock_request_id"
|
||||
):
|
||||
req = self.env["stock.request"].browse(
|
||||
procurement.values.get("stock_request_id")
|
||||
)
|
||||
if req.order_id:
|
||||
new_procs.append(procurement._replace(origin=req.order_id.name))
|
||||
indexes_to_pop.append(i)
|
||||
if new_procs:
|
||||
indexes_to_pop.reverse()
|
||||
for index in indexes_to_pop:
|
||||
procurements.pop(index)
|
||||
procurements.extend(new_procs)
|
||||
return super().run(procurements, raise_user_error=raise_user_error)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
# Copyright 2018 ForgeFlow S.L.
|
||||
# (http://www.forgeflow.com)
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ResCompany(models.Model):
|
||||
_inherit = "res.company"
|
||||
|
||||
stock_request_allow_virtual_loc = fields.Boolean(
|
||||
string="Allow Virtual locations on Stock Requests"
|
||||
)
|
||||
stock_request_check_available_first = fields.Boolean(
|
||||
string="Check available stock first"
|
||||
)
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
# Copyright 2018 Creu Blanca
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class ResConfigSettings(models.TransientModel):
|
||||
_inherit = "res.config.settings"
|
||||
|
||||
group_stock_request_order = fields.Boolean(
|
||||
implied_group="stock_request.group_stock_request_order"
|
||||
)
|
||||
|
||||
module_stock_request_purchase = fields.Boolean(
|
||||
string="Stock Requests for Purchases"
|
||||
)
|
||||
|
||||
module_stock_request_kanban = fields.Boolean(
|
||||
string="Stock Requests Kanban integration"
|
||||
)
|
||||
|
||||
stock_request_check_available_first = fields.Boolean(
|
||||
related="company_id.stock_request_check_available_first", readonly=False
|
||||
)
|
||||
stock_request_allow_virtual_loc = fields.Boolean(
|
||||
related="company_id.stock_request_allow_virtual_loc", readonly=False
|
||||
)
|
||||
|
||||
module_stock_request_analytic = fields.Boolean(
|
||||
string="Stock Requests Analytic integration"
|
||||
)
|
||||
|
||||
module_stock_request_submit = fields.Boolean(
|
||||
string="Submitted state in Stock Requests"
|
||||
)
|
||||
|
||||
module_stock_request_mrp = fields.Boolean(string="Stock Request for Manufacturing")
|
||||
|
||||
# Dependencies
|
||||
@api.onchange("stock_request_allow_virtual_loc")
|
||||
def _onchange_stock_request_allow_virtual_loc(self):
|
||||
if self.stock_request_allow_virtual_loc:
|
||||
self.group_stock_multi_locations = True
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
# Copyright 2018 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import _, api, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class StockLocation(models.Model):
|
||||
_inherit = "stock.location"
|
||||
|
||||
@api.constrains("company_id")
|
||||
def _check_company_stock_request(self):
|
||||
if any(
|
||||
rec.company_id
|
||||
and self.env["stock.request"].search(
|
||||
[("company_id", "!=", rec.company_id.id), ("location_id", "=", rec.id)],
|
||||
limit=1,
|
||||
)
|
||||
for rec in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You cannot change the company of the location, as it is "
|
||||
"already assigned to stock requests that belong to "
|
||||
"another company."
|
||||
)
|
||||
)
|
||||
if any(
|
||||
rec.company_id
|
||||
and self.env["stock.request.order"].search(
|
||||
[
|
||||
("company_id", "!=", rec.company_id.id),
|
||||
("warehouse_id", "=", rec.id),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
for rec in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You cannot change the company of the location, as it is "
|
||||
"already assigned to stock request orders that belong to "
|
||||
"another company."
|
||||
)
|
||||
)
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
# Copyright 2017-2020 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class StockMove(models.Model):
|
||||
_inherit = "stock.move"
|
||||
|
||||
allocation_ids = fields.One2many(
|
||||
comodel_name="stock.request.allocation",
|
||||
inverse_name="stock_move_id",
|
||||
string="Stock Request Allocation",
|
||||
)
|
||||
|
||||
stock_request_ids = fields.One2many(
|
||||
comodel_name="stock.request",
|
||||
string="Stock Requests",
|
||||
compute="_compute_stock_request_ids",
|
||||
)
|
||||
|
||||
@api.depends("allocation_ids")
|
||||
def _compute_stock_request_ids(self):
|
||||
for rec in 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["allocation_ids"] = [(4, m.id) for m in self.mapped("allocation_ids")]
|
||||
return res
|
||||
|
||||
@api.constrains("company_id")
|
||||
def _check_company_stock_request(self):
|
||||
if any(
|
||||
self.env["stock.request.allocation"].search(
|
||||
[
|
||||
("company_id", "!=", rec.company_id.id),
|
||||
("stock_move_id", "=", rec.id),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
for rec in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"The company of the stock request must match with "
|
||||
"that of the location."
|
||||
)
|
||||
)
|
||||
|
||||
def copy_data(self, default=None):
|
||||
if not default:
|
||||
default = {}
|
||||
if "allocation_ids" not in default:
|
||||
default["allocation_ids"] = []
|
||||
for alloc in self.allocation_ids:
|
||||
default["allocation_ids"].append(
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"stock_request_id": alloc.stock_request_id.id,
|
||||
"requested_product_uom_qty": alloc.requested_product_uom_qty,
|
||||
},
|
||||
)
|
||||
)
|
||||
return super(StockMove, self).copy_data(default)
|
||||
|
||||
def _action_cancel(self):
|
||||
"""Apply sudo to prevent requests ACL errors if the user does not have
|
||||
permissions (example: productions)."""
|
||||
res = super()._action_cancel()
|
||||
self.mapped("allocation_ids.stock_request_id").sudo().check_cancel()
|
||||
return res
|
||||
|
||||
def _action_done(self, cancel_backorder=False):
|
||||
"""Apply sudo to prevent requests ACL errors if the user does not have
|
||||
permissions (example: productions)."""
|
||||
res = super()._action_done(cancel_backorder=cancel_backorder)
|
||||
self.mapped("allocation_ids.stock_request_id").sudo().check_done()
|
||||
return res
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
# Copyright 2017 ForgeFlow S.L.
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).
|
||||
|
||||
from odoo import _, api, models
|
||||
|
||||
|
||||
class StockMoveLine(models.Model):
|
||||
_inherit = "stock.move.line"
|
||||
|
||||
@api.model
|
||||
def _stock_request_confirm_done_message_content(self, message_data):
|
||||
title = (
|
||||
_("Receipt confirmation %(picking_name)s for your Request %(request_name)s")
|
||||
% message_data
|
||||
)
|
||||
message = "<h3>%s</h3>" % title
|
||||
message += (
|
||||
_(
|
||||
"The following requested items from Stock Request %(request_name)s "
|
||||
"have now been received in %(location_name)s using Picking %(picking_name)s:"
|
||||
)
|
||||
% message_data
|
||||
)
|
||||
message += "<ul>"
|
||||
message += (
|
||||
_(
|
||||
"<li><b>%(product_name)s</b>: Transferred quantity %(product_qty)s"
|
||||
"%(product_uom)s</li>"
|
||||
)
|
||||
% message_data
|
||||
)
|
||||
message += "</ul>"
|
||||
return message
|
||||
|
||||
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_qty": allocated_qty,
|
||||
"product_uom": ml.product_uom_id.name,
|
||||
"location_name": ml.location_dest_id.name_get()[0][1],
|
||||
}
|
||||
|
||||
def _action_done(self):
|
||||
res = super(StockMoveLine, self)._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
|
||||
)
|
||||
|
||||
# We do sudo because potentially the user that completes the move
|
||||
# may not have permissions for stock.request.
|
||||
to_allocate_qty = qty_done
|
||||
for allocation in ml.move_id.allocation_ids.sudo():
|
||||
allocated_qty = 0.0
|
||||
if allocation.open_product_qty:
|
||||
allocated_qty = min(allocation.open_product_qty, to_allocate_qty)
|
||||
allocation.allocated_product_qty += allocated_qty
|
||||
to_allocate_qty -= allocated_qty
|
||||
if allocated_qty:
|
||||
request = allocation.stock_request_id
|
||||
message_data = self._prepare_message_data(
|
||||
ml, request, allocated_qty
|
||||
)
|
||||
message = self._stock_request_confirm_done_message_content(
|
||||
message_data
|
||||
)
|
||||
request.message_post(body=message, subtype_xmlid="mail.mt_comment")
|
||||
request.check_done()
|
||||
return res
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
# Copyright 2017-2020 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class StockPicking(models.Model):
|
||||
_inherit = "stock.picking"
|
||||
|
||||
stock_request_ids = fields.One2many(
|
||||
comodel_name="stock.request",
|
||||
string="Stock Requests",
|
||||
compute="_compute_stock_request_ids",
|
||||
)
|
||||
stock_request_count = fields.Integer(
|
||||
"Stock Request #", compute="_compute_stock_request_ids"
|
||||
)
|
||||
|
||||
@api.depends("move_ids")
|
||||
def _compute_stock_request_ids(self):
|
||||
for rec in self:
|
||||
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):
|
||||
"""
|
||||
:return dict: dictionary value for created view
|
||||
"""
|
||||
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||
"stock_request.action_stock_request_form"
|
||||
)
|
||||
|
||||
requests = self.mapped("stock_request_ids")
|
||||
if len(requests) > 1:
|
||||
action["domain"] = [("id", "in", requests.ids)]
|
||||
elif requests:
|
||||
action["views"] = [
|
||||
(self.env.ref("stock_request.view_stock_request_form").id, "form")
|
||||
]
|
||||
action["res_id"] = requests.id
|
||||
return action
|
||||
|
|
@ -0,0 +1,465 @@
|
|||
# Copyright 2017-2020 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError, ValidationError
|
||||
from odoo.tools import float_compare
|
||||
|
||||
|
||||
class StockRequest(models.Model):
|
||||
_name = "stock.request"
|
||||
_description = "Stock Request"
|
||||
_inherit = "stock.request.abstract"
|
||||
_order = "id desc"
|
||||
|
||||
def _get_default_requested_by(self):
|
||||
return self.env["res.users"].browse(self.env.uid)
|
||||
|
||||
@staticmethod
|
||||
def _get_expected_date():
|
||||
return fields.Datetime.now()
|
||||
|
||||
name = fields.Char(states={"draft": [("readonly", False)]})
|
||||
state = fields.Selection(
|
||||
selection=[
|
||||
("draft", "Draft"),
|
||||
("open", "In progress"),
|
||||
("done", "Done"),
|
||||
("cancel", "Cancelled"),
|
||||
],
|
||||
string="Status",
|
||||
copy=False,
|
||||
default="draft",
|
||||
index=True,
|
||||
readonly=True,
|
||||
tracking=True,
|
||||
)
|
||||
requested_by = fields.Many2one(
|
||||
"res.users",
|
||||
required=True,
|
||||
tracking=True,
|
||||
default=lambda s: s._get_default_requested_by(),
|
||||
)
|
||||
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(
|
||||
[
|
||||
("direct", "Receive each product when available"),
|
||||
("one", "Receive all products at once"),
|
||||
],
|
||||
string="Shipping Policy",
|
||||
required=True,
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
default="direct",
|
||||
)
|
||||
move_ids = fields.One2many(
|
||||
comodel_name="stock.move",
|
||||
compute="_compute_move_ids",
|
||||
string="Stock Moves",
|
||||
readonly=True,
|
||||
)
|
||||
picking_ids = fields.One2many(
|
||||
"stock.picking",
|
||||
compute="_compute_picking_ids",
|
||||
string="Pickings",
|
||||
readonly=True,
|
||||
)
|
||||
qty_in_progress = fields.Float(
|
||||
digits="Product Unit of Measure",
|
||||
readonly=True,
|
||||
compute="_compute_qty",
|
||||
store=True,
|
||||
help="Quantity in progress.",
|
||||
)
|
||||
qty_done = fields.Float(
|
||||
digits="Product Unit of Measure",
|
||||
readonly=True,
|
||||
compute="_compute_qty",
|
||||
store=True,
|
||||
help="Quantity completed",
|
||||
)
|
||||
qty_cancelled = fields.Float(
|
||||
digits="Product Unit of Measure",
|
||||
readonly=True,
|
||||
compute="_compute_qty",
|
||||
store=True,
|
||||
help="Quantity cancelled",
|
||||
)
|
||||
picking_count = fields.Integer(
|
||||
string="Delivery Orders",
|
||||
compute="_compute_picking_ids",
|
||||
readonly=True,
|
||||
)
|
||||
allocation_ids = fields.One2many(
|
||||
comodel_name="stock.request.allocation",
|
||||
inverse_name="stock_request_id",
|
||||
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)
|
||||
|
||||
_sql_constraints = [
|
||||
("name_uniq", "unique(name, company_id)", "Stock Request name must be unique")
|
||||
]
|
||||
|
||||
@api.constrains("state", "product_qty")
|
||||
def _check_qty(self):
|
||||
for rec in self:
|
||||
if rec.state == "draft" and rec.product_qty <= 0:
|
||||
raise ValidationError(
|
||||
_("Stock Request product quantity has to be strictly positive.")
|
||||
)
|
||||
elif rec.state != "draft" and rec.product_qty < 0:
|
||||
raise ValidationError(
|
||||
_("Stock Request product quantity cannot be negative.")
|
||||
)
|
||||
|
||||
def _get_all_origin_moves(self, move):
|
||||
all_moves = move
|
||||
if move.move_orig_ids:
|
||||
for orig_move in move.move_orig_ids:
|
||||
all_moves |= self._get_all_origin_moves(orig_move)
|
||||
return all_moves
|
||||
|
||||
@api.depends("allocation_ids", "allocation_ids.stock_move_id")
|
||||
def _compute_move_ids(self):
|
||||
for request in self:
|
||||
move_ids = request.allocation_ids.mapped("stock_move_id")
|
||||
all_moves = self.env["stock.move"]
|
||||
for move in move_ids:
|
||||
all_moves |= self._get_all_origin_moves(move)
|
||||
request.move_ids = all_moves
|
||||
|
||||
@api.depends(
|
||||
"allocation_ids",
|
||||
"allocation_ids.stock_move_id",
|
||||
"allocation_ids.stock_move_id.picking_id",
|
||||
)
|
||||
def _compute_picking_ids(self):
|
||||
for request in self:
|
||||
request.picking_count = 0
|
||||
request.picking_ids = self.env["stock.picking"]
|
||||
request.picking_ids = request.move_ids.filtered(
|
||||
lambda m: m.state != "cancel"
|
||||
).mapped("picking_id")
|
||||
request.picking_count = len(request.picking_ids)
|
||||
|
||||
@api.depends(
|
||||
"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",
|
||||
)
|
||||
def _compute_qty(self):
|
||||
for request in self:
|
||||
incoming_qty = 0.0
|
||||
other_qty = 0.0
|
||||
for allocation in request.allocation_ids:
|
||||
if allocation.stock_move_id.picking_code == "incoming":
|
||||
incoming_qty += allocation.allocated_product_qty
|
||||
else:
|
||||
other_qty += allocation.allocated_product_qty
|
||||
done_qty = abs(other_qty - incoming_qty)
|
||||
open_qty = sum(request.allocation_ids.mapped("open_product_qty"))
|
||||
uom = request.product_id.uom_id
|
||||
request.qty_done = uom._compute_quantity(
|
||||
done_qty,
|
||||
request.product_uom_id,
|
||||
rounding_method="HALF-UP",
|
||||
)
|
||||
request.qty_in_progress = uom._compute_quantity(
|
||||
open_qty,
|
||||
request.product_uom_id,
|
||||
rounding_method="HALF-UP",
|
||||
)
|
||||
request.qty_cancelled = (
|
||||
max(
|
||||
0,
|
||||
uom._compute_quantity(
|
||||
request.product_qty - done_qty - open_qty,
|
||||
request.product_uom_id,
|
||||
rounding_method="HALF-UP",
|
||||
),
|
||||
)
|
||||
if request.allocation_ids
|
||||
else 0
|
||||
)
|
||||
|
||||
@api.constrains("order_id", "requested_by")
|
||||
def check_order_requested_by(self):
|
||||
for rec in self:
|
||||
if rec.order_id and rec.order_id.requested_by != rec.requested_by:
|
||||
raise ValidationError(_("Requested by must be equal to the order"))
|
||||
|
||||
@api.constrains("order_id", "warehouse_id")
|
||||
def check_order_warehouse_id(self):
|
||||
for rec in self:
|
||||
if rec.order_id and rec.order_id.warehouse_id != rec.warehouse_id:
|
||||
raise ValidationError(_("Warehouse must be equal to the order"))
|
||||
|
||||
@api.constrains("order_id", "location_id")
|
||||
def check_order_location(self):
|
||||
for rec in self:
|
||||
if rec.order_id and rec.order_id.location_id != rec.location_id:
|
||||
raise ValidationError(_("Location must be equal to the order"))
|
||||
|
||||
@api.constrains("order_id", "procurement_group_id")
|
||||
def check_order_procurement_group(self):
|
||||
for rec in self:
|
||||
if (
|
||||
rec.order_id
|
||||
and rec.order_id.procurement_group_id != rec.procurement_group_id
|
||||
):
|
||||
raise ValidationError(_("Procurement group must be equal to the order"))
|
||||
|
||||
@api.constrains("order_id", "company_id")
|
||||
def check_order_company(self):
|
||||
for rec in self:
|
||||
if rec.order_id and rec.order_id.company_id != rec.company_id:
|
||||
raise ValidationError(_("Company must be equal to the order"))
|
||||
|
||||
@api.constrains("order_id", "expected_date")
|
||||
def check_order_expected_date(self):
|
||||
for rec in self:
|
||||
if rec.order_id and rec.order_id.expected_date != rec.expected_date:
|
||||
raise ValidationError(_("Expected date must be equal to the order"))
|
||||
|
||||
@api.constrains("order_id", "picking_policy")
|
||||
def check_order_picking_policy(self):
|
||||
for rec in self:
|
||||
if rec.order_id and rec.order_id.picking_policy != rec.picking_policy:
|
||||
raise ValidationError(
|
||||
_("The picking policy must be equal to the order")
|
||||
)
|
||||
|
||||
def _action_confirm(self):
|
||||
self._action_launch_procurement_rule()
|
||||
self.filtered(lambda x: x.state != "done").write({"state": "open"})
|
||||
|
||||
def action_confirm(self):
|
||||
self._action_confirm()
|
||||
return True
|
||||
|
||||
def action_draft(self):
|
||||
self.write({"state": "draft"})
|
||||
return True
|
||||
|
||||
def action_cancel(self):
|
||||
self.sudo().mapped("move_ids")._action_cancel()
|
||||
self.write({"state": "cancel"})
|
||||
return True
|
||||
|
||||
def action_done(self):
|
||||
self.write({"state": "done"})
|
||||
return True
|
||||
|
||||
def check_cancel(self):
|
||||
for request in self:
|
||||
if request._check_cancel_allocation():
|
||||
request.write({"state": "cancel"})
|
||||
|
||||
def check_done(self):
|
||||
precision = self.env["decimal.precision"].precision_get(
|
||||
"Product Unit of Measure"
|
||||
)
|
||||
for request in self:
|
||||
allocated_qty = sum(request.allocation_ids.mapped("allocated_product_qty"))
|
||||
qty_done = request.product_id.uom_id._compute_quantity(
|
||||
allocated_qty, request.product_uom_id
|
||||
)
|
||||
if (
|
||||
float_compare(
|
||||
qty_done, request.product_uom_qty, precision_digits=precision
|
||||
)
|
||||
>= 0
|
||||
):
|
||||
request.action_done()
|
||||
elif request._check_cancel_allocation():
|
||||
# If qty_done=0 and qty_cancelled>0 it's cancelled
|
||||
request.write({"state": "cancel"})
|
||||
return True
|
||||
|
||||
def _check_cancel_allocation(self):
|
||||
precision = self.env["decimal.precision"].precision_get(
|
||||
"Product Unit of Measure"
|
||||
)
|
||||
self.ensure_one()
|
||||
return (
|
||||
self.allocation_ids
|
||||
and float_compare(self.qty_cancelled, 0, precision_digits=precision) > 0
|
||||
)
|
||||
|
||||
def _prepare_procurement_values(self, group_id=False):
|
||||
"""Prepare specific key for moves or other components that
|
||||
will be created from a procurement rule
|
||||
coming from a stock request. This method could be override
|
||||
in order to add other custom key that could be used in
|
||||
move/po creation.
|
||||
"""
|
||||
return {
|
||||
"date_planned": self.expected_date,
|
||||
"warehouse_id": self.warehouse_id,
|
||||
"stock_request_allocation_ids": self.id,
|
||||
"group_id": group_id or self.procurement_group_id.id or False,
|
||||
"route_ids": self.route_id,
|
||||
"stock_request_id": self.id,
|
||||
}
|
||||
|
||||
def _skip_procurement(self):
|
||||
return self.state != "draft" or self.product_id.type not in ("consu", "product")
|
||||
|
||||
def _prepare_stock_move(self, qty):
|
||||
return {
|
||||
"name": self.product_id.display_name,
|
||||
"company_id": self.company_id.id,
|
||||
"product_id": self.product_id.id,
|
||||
"product_uom_qty": qty,
|
||||
"product_uom": self.product_id.uom_id.id,
|
||||
"location_id": self.location_id.id,
|
||||
"location_dest_id": self.location_id.id,
|
||||
"state": "draft",
|
||||
"reference": self.name,
|
||||
}
|
||||
|
||||
def _prepare_stock_request_allocation(self, move):
|
||||
return {
|
||||
"stock_request_id": self.id,
|
||||
"stock_move_id": move.id,
|
||||
"requested_product_uom_qty": move.product_uom_qty,
|
||||
}
|
||||
|
||||
def _action_use_stock_available(self):
|
||||
"""Create a stock move with the necessary data and mark it as done."""
|
||||
allocation_model = self.env["stock.request.allocation"]
|
||||
stock_move_model = self.env["stock.move"].sudo()
|
||||
precision = self.env["decimal.precision"].precision_get(
|
||||
"Product Unit of Measure"
|
||||
)
|
||||
quants = self.env["stock.quant"]._gather(self.product_id, self.location_id)
|
||||
pending_qty = self.product_uom_qty
|
||||
for quant in quants.filtered(lambda x: x.available_quantity >= 0):
|
||||
qty_move = min(pending_qty, quant.available_quantity)
|
||||
if float_compare(qty_move, 0, precision_digits=precision) > 0:
|
||||
move = stock_move_model.create(self._prepare_stock_move(qty_move))
|
||||
move._action_confirm()
|
||||
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._action_done()
|
||||
|
||||
def _action_launch_procurement_rule(self):
|
||||
"""
|
||||
Launch procurement group (if not enough stock is available) run method
|
||||
with required/custom fields genrated by a
|
||||
stock request. procurement group will launch '_run_move',
|
||||
'_run_buy' or '_run_manufacture'
|
||||
depending on the stock request product rule.
|
||||
"""
|
||||
precision = self.env["decimal.precision"].precision_get(
|
||||
"Product Unit of Measure"
|
||||
)
|
||||
errors = []
|
||||
for request in self:
|
||||
if request._skip_procurement():
|
||||
continue
|
||||
qty = 0.0
|
||||
for move in request.move_ids.filtered(lambda r: r.state != "cancel"):
|
||||
qty += move.product_qty
|
||||
|
||||
if float_compare(qty, request.product_qty, precision_digits=precision) >= 0:
|
||||
continue
|
||||
|
||||
# If stock is available we use it and we do not execute rule
|
||||
if request.company_id.stock_request_check_available_first:
|
||||
if (
|
||||
float_compare(
|
||||
request.product_id.sudo()
|
||||
.with_context(location=request.location_id.id)
|
||||
.free_qty,
|
||||
request.product_uom_qty,
|
||||
precision_digits=precision,
|
||||
)
|
||||
>= 0
|
||||
):
|
||||
request._action_use_stock_available()
|
||||
continue
|
||||
|
||||
values = request._prepare_procurement_values(
|
||||
group_id=request.procurement_group_id
|
||||
)
|
||||
try:
|
||||
procurements = []
|
||||
procurements.append(
|
||||
self.env["procurement.group"].Procurement(
|
||||
request.product_id,
|
||||
request.product_uom_qty,
|
||||
request.product_uom_id,
|
||||
request.location_id,
|
||||
request.name,
|
||||
request.name,
|
||||
self.env.company,
|
||||
values,
|
||||
)
|
||||
)
|
||||
self.env["procurement.group"].run(procurements)
|
||||
except UserError as error:
|
||||
errors.append(error.name)
|
||||
if errors:
|
||||
raise UserError("\n".join(errors))
|
||||
return True
|
||||
|
||||
def action_view_transfer(self):
|
||||
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||
"stock.action_picking_tree_all"
|
||||
)
|
||||
pickings = self.mapped("picking_ids")
|
||||
if len(pickings) > 1:
|
||||
action["domain"] = [("id", "in", pickings.ids)]
|
||||
elif pickings:
|
||||
action["views"] = [(self.env.ref("stock.view_picking_form").id, "form")]
|
||||
action["res_id"] = pickings.id
|
||||
return action
|
||||
|
||||
@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 = self.env["stock.request.order"].browse(upd_vals["order_id"])
|
||||
upd_vals["expected_date"] = order.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()
|
||||
|
|
@ -0,0 +1,252 @@
|
|||
# Copyright 2017-2020 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class StockRequest(models.AbstractModel):
|
||||
_name = "stock.request.abstract"
|
||||
_description = "Stock Request Template"
|
||||
_inherit = ["mail.thread", "mail.activity.mixin"]
|
||||
|
||||
@api.model
|
||||
def default_get(self, fields):
|
||||
res = super(StockRequest, self).default_get(fields)
|
||||
warehouse = None
|
||||
if "warehouse_id" not in res and res.get("company_id"):
|
||||
warehouse = self.env["stock.warehouse"].search(
|
||||
[("company_id", "=", res["company_id"])], limit=1
|
||||
)
|
||||
if warehouse:
|
||||
res["warehouse_id"] = warehouse.id
|
||||
res["location_id"] = warehouse.lot_stock_id.id
|
||||
return res
|
||||
|
||||
@api.depends(
|
||||
"product_id",
|
||||
"product_uom_id",
|
||||
"product_uom_qty",
|
||||
"product_id.product_tmpl_id.uom_id",
|
||||
)
|
||||
def _compute_product_qty(self):
|
||||
for rec in self:
|
||||
rec.product_qty = rec.product_uom_id._compute_quantity(
|
||||
rec.product_uom_qty,
|
||||
rec.product_id.product_tmpl_id.uom_id,
|
||||
rounding_method="HALF-UP",
|
||||
)
|
||||
|
||||
name = fields.Char(copy=False, required=True, readonly=True, default="/")
|
||||
warehouse_id = fields.Many2one(
|
||||
comodel_name="stock.warehouse",
|
||||
string="Warehouse",
|
||||
check_company=True,
|
||||
ondelete="cascade",
|
||||
required=True,
|
||||
)
|
||||
location_id = fields.Many2one(
|
||||
comodel_name="stock.location",
|
||||
string="Location",
|
||||
domain="not allow_virtual_location and "
|
||||
"[('usage', 'in', ['internal', 'transit'])] or []",
|
||||
ondelete="cascade",
|
||||
required=True,
|
||||
)
|
||||
product_id = fields.Many2one(
|
||||
"product.product",
|
||||
"Product",
|
||||
domain=[("type", "in", ["product", "consu"])],
|
||||
ondelete="cascade",
|
||||
required=True,
|
||||
)
|
||||
allow_virtual_location = fields.Boolean(
|
||||
related="company_id.stock_request_allow_virtual_loc", readonly=True
|
||||
)
|
||||
allowed_uom_categ_id = fields.Many2one(related="product_id.uom_id.category_id")
|
||||
product_uom_id = fields.Many2one(
|
||||
comodel_name="uom.uom",
|
||||
string="Product Unit of Measure",
|
||||
domain="[('category_id', '=?', allowed_uom_categ_id)]",
|
||||
required=True,
|
||||
default=lambda self: self._context.get("product_uom_id", False),
|
||||
)
|
||||
product_uom_qty = fields.Float(
|
||||
"Quantity",
|
||||
digits="Product Unit of Measure",
|
||||
required=True,
|
||||
help="Quantity, specified in the unit of measure indicated in the request.",
|
||||
)
|
||||
product_qty = fields.Float(
|
||||
"Real Quantity",
|
||||
compute="_compute_product_qty",
|
||||
store=True,
|
||||
copy=False,
|
||||
digits="Product Unit of Measure",
|
||||
help="Quantity in the default UoM of the product",
|
||||
)
|
||||
procurement_group_id = fields.Many2one(
|
||||
"procurement.group",
|
||||
"Procurement Group",
|
||||
help="Moves created through this stock request will be put in this "
|
||||
"procurement group. If none is given, the moves generated by "
|
||||
"procurement rules will be grouped into one big picking.",
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
"res.company", "Company", required=True, default=lambda self: self.env.company
|
||||
)
|
||||
route_id = fields.Many2one(
|
||||
"stock.route",
|
||||
string="Route",
|
||||
domain="[('id', 'in', route_ids)]",
|
||||
ondelete="restrict",
|
||||
)
|
||||
|
||||
route_ids = fields.Many2many(
|
||||
"stock.route",
|
||||
string="Routes",
|
||||
compute="_compute_route_ids",
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
_sql_constraints = [
|
||||
("name_uniq", "unique(name, company_id)", "Name must be unique")
|
||||
]
|
||||
|
||||
@api.depends("product_id", "warehouse_id", "location_id")
|
||||
def _compute_route_ids(self):
|
||||
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.route"])
|
||||
routes_by_warehouse[warehouse.id] |= route
|
||||
for record in self:
|
||||
routes = route_obj
|
||||
if record.product_id:
|
||||
routes += record.product_id.mapped(
|
||||
"route_ids"
|
||||
) | record.product_id.mapped("categ_id").mapped("total_route_ids")
|
||||
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
|
||||
record.route_ids = routes.filtered(
|
||||
lambda r: any(p.location_dest_id.id in parents for p in r.rule_ids)
|
||||
)
|
||||
|
||||
def get_parents(self):
|
||||
location = self.location_id
|
||||
result = location
|
||||
while location.location_id:
|
||||
location = location.location_id
|
||||
result |= location
|
||||
return result
|
||||
|
||||
@api.constrains(
|
||||
"company_id", "product_id", "warehouse_id", "location_id", "route_id"
|
||||
)
|
||||
def _check_company_constrains(self):
|
||||
"""Check if the related models have the same company"""
|
||||
for rec in self:
|
||||
if (
|
||||
rec.product_id.company_id
|
||||
and rec.product_id.company_id != rec.company_id
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You have entered a product that is assigned "
|
||||
"to another company."
|
||||
)
|
||||
)
|
||||
if (
|
||||
rec.location_id.company_id
|
||||
and rec.location_id.company_id != rec.company_id
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You have entered a location that is "
|
||||
"assigned to another company."
|
||||
)
|
||||
)
|
||||
if rec.warehouse_id.company_id != rec.company_id:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You have entered a warehouse that is "
|
||||
"assigned to another company."
|
||||
)
|
||||
)
|
||||
if (
|
||||
rec.route_id
|
||||
and rec.route_id.company_id
|
||||
and rec.route_id.company_id != rec.company_id
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You have entered a route that is "
|
||||
"assigned to another company."
|
||||
)
|
||||
)
|
||||
|
||||
@api.constrains("product_id")
|
||||
def _check_product_uom(self):
|
||||
"""Check if the UoM has the same category as the
|
||||
product standard UoM"""
|
||||
if any(
|
||||
request.product_id.uom_id.category_id != request.product_uom_id.category_id
|
||||
for request in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You have to select a product unit of measure in the "
|
||||
"same category than the default unit "
|
||||
"of measure of the product"
|
||||
)
|
||||
)
|
||||
|
||||
@api.onchange("warehouse_id")
|
||||
def onchange_warehouse_id(self):
|
||||
"""Finds location id for changed warehouse."""
|
||||
if self._name == "stock.request" and self.order_id:
|
||||
# When the stock request is created from an order the wh and
|
||||
# location are taken from the order and we rely on it to change
|
||||
# all request associated. Thus, no need to apply
|
||||
# the onchange, as it could lead to inconsistencies.
|
||||
return
|
||||
if self.warehouse_id:
|
||||
loc_wh = self.location_id.warehouse_id
|
||||
if self.warehouse_id != loc_wh:
|
||||
self.location_id = self.warehouse_id.lot_stock_id.id
|
||||
if self.warehouse_id.company_id != self.company_id:
|
||||
self.company_id = self.warehouse_id.company_id
|
||||
|
||||
@api.onchange("location_id")
|
||||
def onchange_location_id(self):
|
||||
if self.location_id:
|
||||
loc_wh = self.location_id.warehouse_id
|
||||
if loc_wh and self.warehouse_id != loc_wh:
|
||||
self.warehouse_id = loc_wh
|
||||
self.with_context(no_change_childs=True).onchange_warehouse_id()
|
||||
|
||||
@api.onchange("company_id")
|
||||
def onchange_company_id(self):
|
||||
"""Sets a default warehouse when the company is changed."""
|
||||
if self.company_id and (
|
||||
not self.warehouse_id or self.warehouse_id.company_id != self.company_id
|
||||
):
|
||||
self.warehouse_id = self.env["stock.warehouse"].search(
|
||||
[
|
||||
"|",
|
||||
("company_id", "=", False),
|
||||
("company_id", "=", self.company_id.id),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
self.onchange_warehouse_id()
|
||||
|
||||
@api.onchange("product_id")
|
||||
def onchange_product_id(self):
|
||||
if self.product_id:
|
||||
self.product_uom_id = self.product_id.uom_id
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
# Copyright 2017-2020 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class StockRequestAllocation(models.Model):
|
||||
_name = "stock.request.allocation"
|
||||
_description = "Stock Request Allocation"
|
||||
|
||||
stock_request_id = fields.Many2one(
|
||||
string="Stock Request",
|
||||
comodel_name="stock.request",
|
||||
required=True,
|
||||
ondelete="cascade",
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
string="Company",
|
||||
comodel_name="res.company",
|
||||
readonly=True,
|
||||
related="stock_request_id.company_id",
|
||||
store=True,
|
||||
)
|
||||
stock_move_id = fields.Many2one(
|
||||
string="Stock Move",
|
||||
comodel_name="stock.move",
|
||||
required=True,
|
||||
ondelete="cascade",
|
||||
)
|
||||
product_id = fields.Many2one(
|
||||
string="Product",
|
||||
comodel_name="product.product",
|
||||
related="stock_request_id.product_id",
|
||||
readonly=True,
|
||||
)
|
||||
product_uom_id = fields.Many2one(
|
||||
string="UoM",
|
||||
comodel_name="uom.uom",
|
||||
related="stock_request_id.product_uom_id",
|
||||
readonly=True,
|
||||
)
|
||||
requested_product_uom_qty = fields.Float(
|
||||
"Requested Quantity (UoM)",
|
||||
help="Quantity of the stock request allocated to the stock move, "
|
||||
"in the UoM of the Stock Request",
|
||||
)
|
||||
requested_product_qty = fields.Float(
|
||||
"Requested Quantity",
|
||||
help="Quantity of the stock request allocated to the stock move, "
|
||||
"in the default UoM of the product",
|
||||
compute="_compute_requested_product_qty",
|
||||
)
|
||||
allocated_product_qty = fields.Float(
|
||||
"Allocated Quantity",
|
||||
copy=False,
|
||||
help="Quantity of the stock request allocated to the stock move, "
|
||||
"in the default UoM of the product",
|
||||
)
|
||||
open_product_qty = fields.Float(
|
||||
"Open Quantity",
|
||||
compute="_compute_open_product_qty",
|
||||
)
|
||||
|
||||
@api.depends(
|
||||
"stock_request_id.product_id",
|
||||
"stock_request_id.product_uom_id",
|
||||
"requested_product_uom_qty",
|
||||
)
|
||||
def _compute_requested_product_qty(self):
|
||||
for rec in self:
|
||||
rec.requested_product_qty = rec.product_uom_id._compute_quantity(
|
||||
rec.requested_product_uom_qty,
|
||||
rec.product_id.uom_id,
|
||||
rounding_method="HALF-UP",
|
||||
)
|
||||
|
||||
@api.depends(
|
||||
"requested_product_qty",
|
||||
"allocated_product_qty",
|
||||
"stock_move_id",
|
||||
"stock_move_id.state",
|
||||
)
|
||||
def _compute_open_product_qty(self):
|
||||
for rec in self:
|
||||
if rec.stock_move_id.state in ["cancel", "done"]:
|
||||
rec.open_product_qty = 0.0
|
||||
else:
|
||||
rec.open_product_qty = (
|
||||
rec.requested_product_qty - rec.allocated_product_qty
|
||||
)
|
||||
if rec.open_product_qty < 0.0:
|
||||
rec.open_product_qty = 0.0
|
||||
|
|
@ -0,0 +1,437 @@
|
|||
# Copyright 2018 Creu Blanca
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError, ValidationError
|
||||
|
||||
|
||||
class StockRequestOrder(models.Model):
|
||||
_name = "stock.request.order"
|
||||
_description = "Stock Request Order"
|
||||
_inherit = ["mail.thread", "mail.activity.mixin"]
|
||||
_order = "id desc"
|
||||
|
||||
@api.model
|
||||
def default_get(self, 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(
|
||||
[("company_id", "=", res["company_id"])], limit=1
|
||||
)
|
||||
if warehouse:
|
||||
res["warehouse_id"] = warehouse.id
|
||||
res["location_id"] = warehouse.lot_stock_id.id
|
||||
return res
|
||||
|
||||
def __get_request_order_states(self):
|
||||
return self.env["stock.request"].fields_get(allfields=["state"])["state"][
|
||||
"selection"
|
||||
]
|
||||
|
||||
def _get_request_order_states(self):
|
||||
return self.__get_request_order_states()
|
||||
|
||||
def _get_default_requested_by(self):
|
||||
return self.env["res.users"].browse(self.env.uid)
|
||||
|
||||
name = fields.Char(
|
||||
copy=False,
|
||||
required=True,
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
default="/",
|
||||
)
|
||||
state = fields.Selection(
|
||||
selection=_get_request_order_states,
|
||||
string="Status",
|
||||
copy=False,
|
||||
default="draft",
|
||||
index=True,
|
||||
readonly=True,
|
||||
tracking=True,
|
||||
compute="_compute_state",
|
||||
store=True,
|
||||
)
|
||||
requested_by = fields.Many2one(
|
||||
"res.users",
|
||||
required=True,
|
||||
tracking=True,
|
||||
default=lambda s: s._get_default_requested_by(),
|
||||
)
|
||||
warehouse_id = fields.Many2one(
|
||||
comodel_name="stock.warehouse",
|
||||
string="Warehouse",
|
||||
check_company=True,
|
||||
readonly=True,
|
||||
ondelete="cascade",
|
||||
required=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
)
|
||||
location_id = fields.Many2one(
|
||||
comodel_name="stock.location",
|
||||
string="Location",
|
||||
domain="not allow_virtual_location and "
|
||||
"[('usage', 'in', ['internal', 'transit'])] or []",
|
||||
readonly=True,
|
||||
ondelete="cascade",
|
||||
required=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
)
|
||||
allow_virtual_location = fields.Boolean(
|
||||
related="company_id.stock_request_allow_virtual_loc", readonly=True
|
||||
)
|
||||
procurement_group_id = fields.Many2one(
|
||||
"procurement.group",
|
||||
"Procurement Group",
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
help="Moves created through this stock request will be put in this "
|
||||
"procurement group. If none is given, the moves generated by "
|
||||
"procurement rules will be grouped into one big picking.",
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
"res.company",
|
||||
"Company",
|
||||
required=True,
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
default=lambda self: self.env.company,
|
||||
)
|
||||
expected_date = fields.Datetime(
|
||||
default=fields.Datetime.now,
|
||||
index=True,
|
||||
required=True,
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
help="Date when you expect to receive the goods.",
|
||||
)
|
||||
picking_policy = fields.Selection(
|
||||
[
|
||||
("direct", "Receive each product when available"),
|
||||
("one", "Receive all products at once"),
|
||||
],
|
||||
string="Shipping Policy",
|
||||
required=True,
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
default="direct",
|
||||
)
|
||||
move_ids = fields.One2many(
|
||||
comodel_name="stock.move",
|
||||
compute="_compute_move_ids",
|
||||
string="Stock Moves",
|
||||
readonly=True,
|
||||
)
|
||||
picking_ids = fields.One2many(
|
||||
"stock.picking",
|
||||
compute="_compute_picking_ids",
|
||||
string="Pickings",
|
||||
readonly=True,
|
||||
)
|
||||
picking_count = fields.Integer(
|
||||
string="Delivery Orders", compute="_compute_picking_ids", readonly=True
|
||||
)
|
||||
stock_request_ids = fields.One2many(
|
||||
"stock.request", inverse_name="order_id", copy=True
|
||||
)
|
||||
stock_request_count = fields.Integer(
|
||||
string="Stock requests", compute="_compute_stock_request_count", readonly=True
|
||||
)
|
||||
|
||||
route_ids = fields.Many2many(
|
||||
"stock.route",
|
||||
string="Routes",
|
||||
compute="_compute_route_ids",
|
||||
readonly=True,
|
||||
store=True,
|
||||
)
|
||||
|
||||
route_id = fields.Many2one(
|
||||
"stock.route",
|
||||
compute="_compute_route_id",
|
||||
inverse="_inverse_route_id",
|
||||
readonly=True,
|
||||
states={"draft": [("readonly", False)]},
|
||||
store=True,
|
||||
help="The route related to a stock request order",
|
||||
)
|
||||
|
||||
_sql_constraints = [
|
||||
("name_uniq", "unique(name, company_id)", "Stock Request name must be unique")
|
||||
]
|
||||
|
||||
@api.depends("warehouse_id", "location_id", "stock_request_ids")
|
||||
def _compute_route_ids(self):
|
||||
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.route"])
|
||||
routes_by_warehouse[warehouse.id] |= route
|
||||
for record in self:
|
||||
routes = route_obj
|
||||
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
|
||||
filtered_routes = routes.filtered(
|
||||
lambda r: any(p.location_dest_id.id in parents for p in r.rule_ids)
|
||||
)
|
||||
if record.stock_request_ids:
|
||||
all_routes = record.stock_request_ids.mapped("route_ids")
|
||||
common_routes = all_routes
|
||||
for line in record.stock_request_ids:
|
||||
common_routes &= line.route_ids
|
||||
final_routes = filtered_routes | common_routes
|
||||
record.route_ids = [(6, 0, final_routes.ids)]
|
||||
else:
|
||||
record.route_ids = [(6, 0, filtered_routes.ids)]
|
||||
|
||||
def get_parents(self):
|
||||
location = self.location_id
|
||||
result = location
|
||||
while location.location_id:
|
||||
location = location.location_id
|
||||
result |= location
|
||||
return result
|
||||
|
||||
@api.depends("stock_request_ids")
|
||||
def _compute_route_id(self):
|
||||
for order in self:
|
||||
if order.stock_request_ids:
|
||||
first_route = order.stock_request_ids[0].route_id or False
|
||||
if any(r.route_id != first_route for r in order.stock_request_ids):
|
||||
first_route = False
|
||||
order.route_id = first_route
|
||||
|
||||
def _inverse_route_id(self):
|
||||
for order in self:
|
||||
if order.route_id:
|
||||
order.stock_request_ids.write({"route_id": order.route_id.id})
|
||||
|
||||
@api.onchange("route_id")
|
||||
def _onchange_route_id(self):
|
||||
if self.route_id:
|
||||
for request in self.stock_request_ids:
|
||||
request.route_id = self.route_id
|
||||
|
||||
@api.depends("stock_request_ids.state")
|
||||
def _compute_state(self):
|
||||
for item in self:
|
||||
states = item.stock_request_ids.mapped("state")
|
||||
if not item.stock_request_ids or all(x == "draft" for x in states):
|
||||
item.state = "draft"
|
||||
elif all(x == "cancel" for x in states):
|
||||
item.state = "cancel"
|
||||
elif all(x in ("done", "cancel") for x in states):
|
||||
item.state = "done"
|
||||
else:
|
||||
item.state = "open"
|
||||
|
||||
@api.depends("stock_request_ids.allocation_ids")
|
||||
def _compute_picking_ids(self):
|
||||
for record in self:
|
||||
record.picking_ids = record.stock_request_ids.mapped("picking_ids")
|
||||
record.picking_count = len(record.picking_ids)
|
||||
|
||||
@api.depends("stock_request_ids")
|
||||
def _compute_move_ids(self):
|
||||
for record in self:
|
||||
record.move_ids = record.stock_request_ids.mapped("move_ids")
|
||||
|
||||
@api.depends("stock_request_ids")
|
||||
def _compute_stock_request_count(self):
|
||||
for record in self:
|
||||
record.stock_request_count = len(record.stock_request_ids)
|
||||
|
||||
@api.onchange("requested_by")
|
||||
def onchange_requested_by(self):
|
||||
self.change_childs()
|
||||
|
||||
@api.onchange("expected_date")
|
||||
def onchange_expected_date(self):
|
||||
self.change_childs()
|
||||
|
||||
@api.onchange("picking_policy")
|
||||
def onchange_picking_policy(self):
|
||||
self.change_childs()
|
||||
|
||||
@api.onchange("location_id")
|
||||
def onchange_location_id(self):
|
||||
if self.location_id:
|
||||
loc_wh = self.location_id.warehouse_id
|
||||
if loc_wh and self.warehouse_id != loc_wh:
|
||||
self.warehouse_id = loc_wh
|
||||
self.with_context(no_change_childs=True).onchange_warehouse_id()
|
||||
self.change_childs()
|
||||
|
||||
@api.onchange("warehouse_id")
|
||||
def onchange_warehouse_id(self):
|
||||
if self.warehouse_id:
|
||||
# search with sudo because the user may not have permissions
|
||||
loc_wh = self.location_id.warehouse_id
|
||||
if self.warehouse_id != loc_wh:
|
||||
self.location_id = self.warehouse_id.lot_stock_id
|
||||
self.with_context(no_change_childs=True).onchange_location_id()
|
||||
if self.warehouse_id.company_id != self.company_id:
|
||||
self.company_id = self.warehouse_id.company_id
|
||||
self.with_context(no_change_childs=True).onchange_company_id()
|
||||
self.change_childs()
|
||||
|
||||
@api.onchange("procurement_group_id")
|
||||
def onchange_procurement_group_id(self):
|
||||
self.change_childs()
|
||||
|
||||
@api.onchange("company_id")
|
||||
def onchange_company_id(self):
|
||||
if self.company_id and (
|
||||
not self.warehouse_id or self.warehouse_id.company_id != self.company_id
|
||||
):
|
||||
self.warehouse_id = self.env["stock.warehouse"].search(
|
||||
[("company_id", "=", self.company_id.id)], limit=1
|
||||
)
|
||||
self.with_context(no_change_childs=True).onchange_warehouse_id()
|
||||
self.change_childs()
|
||||
|
||||
def change_childs(self):
|
||||
if not self._context.get("no_change_childs", False):
|
||||
for line in self.stock_request_ids:
|
||||
line.warehouse_id = self.warehouse_id
|
||||
line.location_id = self.location_id
|
||||
line.company_id = self.company_id
|
||||
line.picking_policy = self.picking_policy
|
||||
line.expected_date = self.expected_date
|
||||
line.requested_by = self.requested_by
|
||||
line.procurement_group_id = self.procurement_group_id
|
||||
|
||||
def action_confirm(self):
|
||||
if not self.stock_request_ids:
|
||||
raise UserError(
|
||||
_("There should be at least one request item for confirming the order.")
|
||||
)
|
||||
self.mapped("stock_request_ids").action_confirm()
|
||||
return True
|
||||
|
||||
def action_draft(self):
|
||||
self.mapped("stock_request_ids").action_draft()
|
||||
return True
|
||||
|
||||
def action_cancel(self):
|
||||
self.mapped("stock_request_ids").action_cancel()
|
||||
return True
|
||||
|
||||
def action_done(self):
|
||||
return True
|
||||
|
||||
def action_view_transfer(self):
|
||||
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||
"stock.action_picking_tree_all"
|
||||
)
|
||||
|
||||
pickings = self.mapped("picking_ids")
|
||||
if len(pickings) > 1:
|
||||
action["domain"] = [("id", "in", pickings.ids)]
|
||||
elif pickings:
|
||||
action["views"] = [(self.env.ref("stock.view_picking_form").id, "form")]
|
||||
action["res_id"] = pickings.id
|
||||
return action
|
||||
|
||||
def action_view_stock_requests(self):
|
||||
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||
"stock_request.action_stock_request_form"
|
||||
)
|
||||
if len(self.stock_request_ids) > 1:
|
||||
action["domain"] = [("order_id", "in", self.ids)]
|
||||
elif self.stock_request_ids:
|
||||
action["views"] = [
|
||||
(self.env.ref("stock_request.view_stock_request_form").id, "form")
|
||||
]
|
||||
action["res_id"] = self.stock_request_ids.id
|
||||
return action
|
||||
|
||||
@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.order"
|
||||
)
|
||||
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 orders on draft state can be unlinked"))
|
||||
return super().unlink()
|
||||
|
||||
@api.constrains("warehouse_id", "company_id")
|
||||
def _check_warehouse_company(self):
|
||||
if any(
|
||||
request.warehouse_id.company_id != request.company_id for request in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"The company of the stock request must match with "
|
||||
"that of the warehouse."
|
||||
)
|
||||
)
|
||||
|
||||
@api.constrains("location_id", "company_id")
|
||||
def _check_location_company(self):
|
||||
if any(
|
||||
request.location_id.company_id
|
||||
and request.location_id.company_id != request.company_id
|
||||
for request in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"The company of the stock request must match with "
|
||||
"that of the location."
|
||||
)
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _create_from_product_multiselect(self, products):
|
||||
if not products:
|
||||
return False
|
||||
if products._name not in ("product.product", "product.template"):
|
||||
raise ValidationError(
|
||||
_("This action only works in the context of products")
|
||||
)
|
||||
if products._name == "product.template":
|
||||
# search instead of mapped so we don't include archived variants
|
||||
products = self.env["product.product"].search(
|
||||
[("product_tmpl_id", "in", products.ids)]
|
||||
)
|
||||
expected = self.default_get(["expected_date"])["expected_date"]
|
||||
order = self.env["stock.request.order"].create(
|
||||
dict(
|
||||
expected_date=expected,
|
||||
stock_request_ids=[
|
||||
(
|
||||
0,
|
||||
0,
|
||||
dict(
|
||||
product_id=product.id,
|
||||
product_uom_id=product.uom_id.id,
|
||||
product_uom_qty=1.0,
|
||||
expected_date=expected,
|
||||
),
|
||||
)
|
||||
for product in products
|
||||
],
|
||||
)
|
||||
)
|
||||
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||
"stock_request.stock_request_order_action"
|
||||
)
|
||||
action["views"] = [
|
||||
(self.env.ref("stock_request.stock_request_order_form").id, "form")
|
||||
]
|
||||
action["res_id"] = order.id
|
||||
return action
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
# Copyright 2018 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import _, api, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class StockRoute(models.Model):
|
||||
_inherit = "stock.route"
|
||||
|
||||
@api.constrains("company_id")
|
||||
def _check_company_stock_request(self):
|
||||
if any(
|
||||
rec.company_id
|
||||
and self.env["stock.request"].search(
|
||||
[("company_id", "!=", rec.company_id.id), ("route_id", "=", rec.id)],
|
||||
limit=1,
|
||||
)
|
||||
for rec in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You cannot change the company of the route, as it is "
|
||||
"already assigned to stock requests that belong to "
|
||||
"another company."
|
||||
)
|
||||
)
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
# Copyright 2017-2020 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class StockRule(models.Model):
|
||||
_inherit = "stock.rule"
|
||||
|
||||
def _get_stock_move_values(
|
||||
self,
|
||||
product_id,
|
||||
product_qty,
|
||||
product_uom,
|
||||
location_id,
|
||||
name,
|
||||
origin,
|
||||
company_id,
|
||||
values,
|
||||
):
|
||||
result = super(StockRule, self)._get_stock_move_values(
|
||||
product_id,
|
||||
product_qty,
|
||||
product_uom,
|
||||
location_id,
|
||||
name,
|
||||
origin,
|
||||
company_id,
|
||||
values,
|
||||
)
|
||||
if values.get("stock_request_id", False):
|
||||
result["allocation_ids"] = [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"stock_request_id": values.get("stock_request_id"),
|
||||
"requested_product_uom_qty": product_qty,
|
||||
},
|
||||
)
|
||||
]
|
||||
return result
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
# Copyright 2018 ForgeFlow, S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import _, api, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class StockWarehouse(models.Model):
|
||||
_inherit = "stock.warehouse"
|
||||
|
||||
@api.constrains("company_id")
|
||||
def _check_company_stock_request(self):
|
||||
if any(
|
||||
self.env["stock.request"].search(
|
||||
[
|
||||
("company_id", "!=", rec.company_id.id),
|
||||
("warehouse_id", "=", rec.id),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
for rec in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You cannot change the company of the warehouse, as it is "
|
||||
"already assigned to stock requests that belong to "
|
||||
"another company."
|
||||
)
|
||||
)
|
||||
if any(
|
||||
self.env["stock.request.order"].search(
|
||||
[
|
||||
("company_id", "!=", rec.company_id.id),
|
||||
("warehouse_id", "=", rec.id),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
for rec in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You cannot change the company of the warehouse, as it is "
|
||||
"already assigned to stock request orders that belong to "
|
||||
"another company."
|
||||
)
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue