[IMP] stock_request: Add domain to some fields to prevent deprecate methods with domain return

TT41080
This commit is contained in:
Víctor Martínez
2023-01-12 13:55:40 +01:00
parent 8266dd7fa3
commit 6d265c245c
6 changed files with 62 additions and 79 deletions

View File

@@ -37,12 +37,17 @@ class StockRequest(models.AbstractModel):
name = fields.Char("Name", copy=False, required=True, readonly=True, default="/")
warehouse_id = fields.Many2one(
"stock.warehouse", "Warehouse", ondelete="cascade", required=True
comodel_name="stock.warehouse",
string="Warehouse",
check_company=True,
ondelete="cascade",
required=True,
)
location_id = fields.Many2one(
"stock.location",
"Location",
domain=[("usage", "in", ["internal", "transit"])],
comodel_name="stock.location",
string="Location",
domain="not allow_virtual_location and "
"[('usage', 'in', ['internal', 'transit'])] or []",
ondelete="cascade",
required=True,
)
@@ -56,9 +61,11 @@ class StockRequest(models.AbstractModel):
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(
"uom.uom",
"Product Unit of Measure",
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),
)
@@ -210,20 +217,18 @@ class StockRequest(models.AbstractModel):
@api.onchange("warehouse_id")
def onchange_warehouse_id(self):
"""Finds location id for changed warehouse."""
res = {"domain": {}}
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 res
return
if self.warehouse_id:
loc_wh = self.location_id.get_warehouse()
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
return res
@api.onchange("location_id")
def onchange_location_id(self):
@@ -233,33 +238,23 @@ class StockRequest(models.AbstractModel):
self.warehouse_id = loc_wh
self.with_context(no_change_childs=True).onchange_warehouse_id()
@api.onchange("allow_virtual_location")
def onchange_allow_virtual_location(self):
if self.allow_virtual_location:
return {"domain": {"location_id": []}}
@api.onchange("company_id")
def onchange_company_id(self):
"""Sets a default warehouse when the company is changed and limits
the user selection of warehouses."""
"""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", "=", self.company_id.id)], limit=1
[
"|",
("company_id", "=", False),
("company_id", "=", self.company_id.id),
],
limit=1,
)
self.onchange_warehouse_id()
return {"domain": {"warehouse_id": [("company_id", "=", self.company_id.id)]}}
@api.onchange("product_id")
def onchange_product_id(self):
res = {"domain": {}}
if self.product_id:
self.product_uom_id = self.product_id.uom_id.id
res["domain"]["product_uom_id"] = [
("category_id", "=", self.product_id.uom_id.category_id.id)
]
return res
res["domain"]["product_uom_id"] = []
return res
self.product_uom_id = self.product_id.uom_id

View File

@@ -58,18 +58,20 @@ class StockRequestOrder(models.Model):
default=lambda s: s._get_default_requested_by(),
)
warehouse_id = fields.Many2one(
"stock.warehouse",
"Warehouse",
comodel_name="stock.warehouse",
string="Warehouse",
check_company=True,
readonly=True,
ondelete="cascade",
required=True,
states={"draft": [("readonly", False)]},
)
location_id = fields.Many2one(
"stock.location",
"Location",
comodel_name="stock.location",
string="Location",
domain="not allow_virtual_location and "
"[('usage', 'in', ['internal', 'transit'])] or []",
readonly=True,
domain=[("usage", "in", ["internal", "transit"])],
ondelete="cascade",
required=True,
states={"draft": [("readonly", False)]},
@@ -177,11 +179,6 @@ class StockRequestOrder(models.Model):
self.with_context(no_change_childs=True).onchange_warehouse_id()
self.change_childs()
@api.onchange("allow_virtual_location")
def onchange_allow_virtual_location(self):
if self.allow_virtual_location:
return {"domain": {"location_id": []}}
@api.onchange("warehouse_id")
def onchange_warehouse_id(self):
if self.warehouse_id:
@@ -209,7 +206,6 @@ class StockRequestOrder(models.Model):
)
self.with_context(no_change_childs=True).onchange_warehouse_id()
self.change_childs()
return {"domain": {"warehouse_id": [("company_id", "=", self.company_id.id)]}}
def change_childs(self):
if not self._context.get("no_change_childs", False):

View File

@@ -5,12 +5,12 @@ from collections import Counter
from datetime import datetime
from odoo import exceptions, fields
from odoo.tests import common
from odoo.tests import common, new_test_user
class TestStockRequest(common.TransactionCase):
def setUp(self):
super(TestStockRequest, self).setUp()
super().setUp()
# common models
self.stock_request = self.env["stock.request"]
@@ -20,9 +20,6 @@ class TestStockRequest(common.TransactionCase):
self.stock_request_user_group = self.env.ref(
"stock_request.group_stock_request_user"
)
self.stock_request_manager_group = self.env.ref(
"stock_request.group_stock_request_manager"
)
self.main_company = self.env.ref("base.main_company")
self.warehouse = self.env.ref("stock.warehouse0")
self.categ_unit = self.env.ref("uom.product_uom_categ_unit")
@@ -40,15 +37,25 @@ class TestStockRequest(common.TransactionCase):
self.wh2 = self.env["stock.warehouse"].search(
[("company_id", "=", self.company_2.id)], limit=1
)
self.stock_request_user = self._create_user(
"stock_request_user",
[self.stock_request_user_group.id],
[self.main_company.id, self.company_2.id],
ctx = {
"mail_create_nolog": True,
"mail_create_nosubscribe": True,
"mail_notrack": True,
"no_reset_password": True,
}
self.stock_request_user = new_test_user(
self.env,
login="stock_request_user",
groups="stock_request.group_stock_request_user",
company_ids=[(6, 0, [self.main_company.id, self.company_2.id])],
context=ctx,
)
self.stock_request_manager = self._create_user(
"stock_request_manager",
[self.stock_request_manager_group.id],
[self.main_company.id, self.company_2.id],
self.stock_request_manager = new_test_user(
self.env,
login="stock_request_manager",
groups="stock_request.group_stock_request_manager",
company_ids=[(6, 0, [self.main_company.id, self.company_2.id])],
context=ctx,
)
self.product = self._create_product("SH", "Shoes", False)
self.product_company_2 = self._create_product(
@@ -135,22 +142,6 @@ class TestStockRequest(common.TransactionCase):
"stock.no_auto_scheduler", "True"
)
def _create_user(self, name, group_ids, company_ids):
return (
self.env["res.users"]
.with_context({"no_reset_password": True})
.create(
{
"name": name,
"password": "demo",
"login": name,
"email": "@".join([name, "test.com"]),
"groups_id": [(6, 0, group_ids)],
"company_ids": [(6, 0, company_ids)],
}
)
)
def _create_product(self, default_code, name, company_id, **vals):
return self.env["product.product"].create(
dict(
@@ -166,7 +157,7 @@ class TestStockRequest(common.TransactionCase):
class TestStockRequestBase(TestStockRequest):
def setUp(self):
super(TestStockRequestBase, self).setUp()
super().setUp()
def test_defaults(self):
vals = {
@@ -290,20 +281,13 @@ class TestStockRequestBase(TestStockRequest):
# Test onchange_product_id
stock_request.product_id = product
res = stock_request.onchange_product_id()
stock_request.onchange_product_id()
self.assertEqual(
res["domain"]["product_uom_id"],
[("category_id", "=", product.uom_id.category_id.id)],
)
self.assertEqual(
stock_request.product_uom_id, self.env.ref("uom.product_uom_kgm")
)
stock_request.product_id = self.env["product.product"]
res = stock_request.onchange_product_id()
self.assertEqual(res["domain"]["product_uom_id"], [])
# Test onchange_warehouse_id
wh2_2 = (
@@ -962,7 +946,6 @@ class TestStockRequestBase(TestStockRequest):
stock_request = self.stock_request.with_user(self.stock_request_user).create(
vals
)
stock_request.onchange_allow_virtual_location()
self.assertTrue(stock_request.allow_virtual_location)
vals = {
"company_id": self.main_company.id,
@@ -970,7 +953,6 @@ class TestStockRequestBase(TestStockRequest):
"location_id": self.virtual_loc.id,
}
order = self.request_order.with_user(self.stock_request_user).create(vals)
order.onchange_allow_virtual_location()
self.assertTrue(order.allow_virtual_location)
def test_onchange_wh_no_effect_from_order(self):

View File

@@ -121,6 +121,7 @@
<tree editable="bottom">
<field name="name" readonly="1" />
<field name="product_id" />
<field name="allowed_uom_categ_id" invisible="1" />
<field
name="product_uom_id"
options="{'no_open': True, 'no_create': True}"
@@ -138,6 +139,10 @@
<field name="expected_date" invisible="1" />
<field name="picking_policy" invisible="1" />
<field name="warehouse_id" invisible="1" />
<field
name="allow_virtual_location"
invisible="1"
/>
<field name="location_id" invisible="1" />
<field name="procurement_group_id" invisible="1" />
<field name="company_id" invisible="1" />

View File

@@ -22,6 +22,7 @@
groups="stock.group_stock_multi_locations"
/>
<field name="product_id" />
<field name="allowed_uom_categ_id" invisible="1" />
<field
name="product_uom_id"
options="{'no_open': True, 'no_create': True}"
@@ -172,10 +173,10 @@
widget="selection"
groups="stock.group_stock_multi_locations"
/>
<field name="allow_virtual_location" invisible="1" />
<field
name="location_id"
groups="stock.group_stock_multi_locations"
domain="['|', ('company_id', '=', company_id), ('company_id', '=', False)]"
/>
<field
name="route_id"
@@ -197,6 +198,7 @@
<label for="product_uom_qty" />
<div>
<field name="product_uom_qty" class="oe_inline" />
<field name="allowed_uom_categ_id" invisible="1" />
<field
name="product_uom_id"
class="oe_inline"

View File

@@ -14,6 +14,7 @@
<field name="message_needaction" invisible="1" />
<field name="name" />
<field name="warehouse_id" groups="stock.group_stock_multi_locations" />
<field name="allow_virtual_location" invisible="1" />
<field name="location_id" groups="stock.group_stock_multi_locations" />
<field
name="route_id"
@@ -87,6 +88,7 @@
widget="selection"
groups="stock.group_stock_multi_locations"
/>
<field name="allow_virtual_location" invisible="1" />
<field
name="location_id"
groups="stock.group_stock_multi_locations"
@@ -111,6 +113,7 @@
<label for="product_uom_qty" />
<div>
<field name="product_uom_qty" class="oe_inline" />
<field name="allowed_uom_categ_id" invisible="1" />
<field
name="product_uom_id"
class="oe_inline"