Skip to content

Commit

Permalink
[MIG] stock_request: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BernatPForgeFlow committed Feb 7, 2022
1 parent 209baf7 commit f7dd60f
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 80 deletions.
4 changes: 2 additions & 2 deletions stock_request/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2017-2020 ForgeFlow, S.L.
# Copyright 2017-2021 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

{
"name": "Stock Request",
"summary": "Internal request for stock",
"version": "14.0.1.0.4",
"version": "15.0.1.0.0",
"license": "LGPL-3",
"website": "https://github.com/OCA/stock-logistics-warehouse",
"author": "ForgeFlow, Odoo Community Association (OCA)",
Expand Down
28 changes: 15 additions & 13 deletions stock_request/models/stock_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ class StockMoveLine(models.Model):

@api.model
def _stock_request_confirm_done_message_content(self, message_data):
title = _("Receipt confirmation %s for your Request %s") % (
message_data["picking_name"],
message_data["request_name"],
title = _(
"Receipt confirmation %(picking_name)s for your Request %(request_name)s",
picking_name=message_data["picking_name"],
request_name=message_data["request_name"],
)
message = "<h3>%s</h3>" % title
message += _(
"The following requested items from Stock Request %s "
"have now been received in %s using Picking %s:"
) % (
message_data["request_name"],
message_data["location_name"],
message_data["picking_name"],
"The following requested items from Stock Request %(request_name)s "
"have now been received in %(location_name)s using Picking %(picking_name)s:",
request_name=message_data["request_name"],
location_name=message_data["location_name"],
picking_name=message_data["picking_name"],
)
message += "<ul>"
message += _("<li><b>%s</b>: Transferred quantity %s %s</li>") % (
message_data["product_name"],
message_data["product_qty"],
message_data["product_uom"],
message += _(
"<li><b>%(product_name)s</b>: Transferred "
"quantity %(product_qty)s %(product_uom)s</li>",
product_name=message_data["product_name"],
product_qty=message_data["product_qty"],
product_uom=message_data["product_uom"],
)
message += "</ul>"
return message
Expand Down
5 changes: 0 additions & 5 deletions stock_request/models/stock_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ def _get_expected_date():
)
requested_by = fields.Many2one(
"res.users",
"Requested by",
required=True,
tracking=True,
default=lambda s: s._get_default_requested_by(),
)
expected_date = fields.Datetime(
"Expected Date",
index=True,
required=True,
readonly=True,
Expand Down Expand Up @@ -81,23 +79,20 @@ def _get_expected_date():
readonly=True,
)
qty_in_progress = fields.Float(
"Qty In Progress",
digits="Product Unit of Measure",
readonly=True,
compute="_compute_qty",
store=True,
help="Quantity in progress.",
)
qty_done = fields.Float(
"Qty Done",
digits="Product Unit of Measure",
readonly=True,
compute="_compute_qty",
store=True,
help="Quantity completed",
)
qty_cancelled = fields.Float(
"Qty Cancelled",
digits="Product Unit of Measure",
readonly=True,
compute="_compute_qty",
Expand Down
6 changes: 3 additions & 3 deletions stock_request/models/stock_request_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _compute_product_qty(self):
rec.product_uom_qty, rec.product_id.product_tmpl_id.uom_id
)

name = fields.Char("Name", copy=False, required=True, readonly=True, default="/")
name = fields.Char(copy=False, required=True, readonly=True, default="/")
warehouse_id = fields.Many2one(
"stock.warehouse", "Warehouse", ondelete="cascade", required=True
)
Expand Down Expand Up @@ -218,7 +218,7 @@ def onchange_warehouse_id(self):
# the onchange, as it could lead to inconsistencies.
return res
if self.warehouse_id:
loc_wh = self.location_id.get_warehouse()
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:
Expand All @@ -228,7 +228,7 @@ def onchange_warehouse_id(self):
@api.onchange("location_id")
def onchange_location_id(self):
if self.location_id:
loc_wh = self.location_id.get_warehouse()
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()
Expand Down
55 changes: 20 additions & 35 deletions stock_request/models/stock_request_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import _, api, fields, models
from odoo.exceptions import AccessError, UserError, ValidationError
from odoo.exceptions import UserError, ValidationError


class StockRequestOrder(models.Model):
Expand Down Expand Up @@ -34,7 +34,6 @@ def _get_default_requested_by(self):
return self.env["res.users"].browse(self.env.uid)

name = fields.Char(
"Name",
copy=False,
required=True,
readonly=True,
Expand All @@ -52,7 +51,6 @@ def _get_default_requested_by(self):
)
requested_by = fields.Many2one(
"res.users",
"Requested by",
required=True,
tracking=True,
default=lambda s: s._get_default_requested_by(),
Expand Down Expand Up @@ -95,7 +93,6 @@ def _get_default_requested_by(self):
default=lambda self: self.env.company,
)
expected_date = fields.Datetime(
"Expected Date",
default=fields.Datetime.now,
index=True,
required=True,
Expand Down Expand Up @@ -171,7 +168,7 @@ def onchange_picking_policy(self):
@api.onchange("location_id")
def onchange_location_id(self):
if self.location_id:
loc_wh = self.location_id.get_warehouse()
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()
Expand All @@ -186,7 +183,7 @@ def onchange_allow_virtual_location(self):
def onchange_warehouse_id(self):
if self.warehouse_id:
# search with sudo because the user may not have permissions
loc_wh = self.location_id.get_warehouse()
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()
Expand Down Expand Up @@ -323,36 +320,24 @@ def _create_from_product_multiselect(self, products):
[("product_tmpl_id", "in", products.ids)]
)
expected = self.default_get(["expected_date"])["expected_date"]
try:
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
],
)
)
except AccessError:
# TODO: if there is a nice way to hide the action from the
# Action-menu if the user doesn't have the necessary rights,
# that would be a better way of doing this
raise UserError(
_(
"Unfortunately it seems you do not have the necessary rights "
"for creating stock requests. Please contact your "
"administrator."
)
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.ref("stock_request.stock_request_order_action").read()[0]
action["views"] = [
(self.env.ref("stock_request.stock_request_order_form").id, "form")
Expand Down
1 change: 1 addition & 0 deletions stock_request/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@

* Héctor Villarreal <hector.villarreal@forgeflow.com>
* Kitti U. <kittiu@ecosoft.co.th>
* Bernat Puig <bernat.puig@forgeflow.com>
67 changes: 54 additions & 13 deletions stock_request/tests/test_stock_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def setUp(self):
def _create_user(self, name, group_ids, company_ids):
return (
self.env["res.users"]
.with_context({"no_reset_password": True})
.with_context(no_reset_password=True)
.create(
{
"name": name,
Expand All @@ -163,6 +163,25 @@ def _create_product(self, default_code, name, company_id, **vals):
)
)

def _create_product_template_attribute_line(
self, product_tmpl_id, attribute_id, value_id
):
return self.env["product.template.attribute.line"].create(
{
"product_tmpl_id": product_tmpl_id,
"attribute_id": attribute_id,
"value_ids": value_id,
}
)

def _create_product_attribute_value(self, name, attribute):
return self.env["product.attribute.value"].create(
{"name": name, "attribute_id": attribute}
)

def _create_product_attribute(self, name):
return self.env["product.attribute"].create({"name": name})


class TestStockRequestBase(TestStockRequest):
def setUp(self):
Expand Down Expand Up @@ -857,13 +876,40 @@ def test_stock_request_constrains(self):
stock_request.route_id = self.route_2

def test_stock_request_order_from_products(self):
product_a1 = self._create_product("CODEA1", "Product A1", self.main_company.id)
template_a = product_a1.product_tmpl_id
product_a2 = self._create_product(
"CODEA2", "Product A2", self.main_company.id, product_tmpl_id=template_a.id
template_a = self.env["product.template"].create({"name": "ProductTemplate"})
product_attribute = self._create_product_attribute("Attribute")
product_att_value = self._create_product_attribute_value(
"Name-1", product_attribute.id
)
product_tmpl_att_line = self._create_product_template_attribute_line(
template_a.id, product_attribute.id, product_att_value
)
template_a.attribute_line_ids |= product_tmpl_att_line
product_tmpl_att_value = self.env["product.template.attribute.value"].search(
[]
)[-1]
product_a1 = self.env["product.product"].search(
[
(
"product_template_variant_value_ids.name",
"=",
product_tmpl_att_value.name,
)
]
)
product_att_value = self._create_product_attribute_value(
"Name-2", product_attribute.id
)
template_a.attribute_line_ids.value_ids |= product_att_value
product_a2 = self.env["product.product"].search(
[("product_template_variant_value_ids.name", "=", product_att_value.name)]
)
product_att_value = self._create_product_attribute_value(
"Name-3", product_attribute.id
)
product_a3 = self._create_product(
"CODEA3", "Product A3", self.main_company.id, product_tmpl_id=template_a.id
template_a.attribute_line_ids.value_ids |= product_att_value
product_a3 = self.env["product.product"].search(
[("product_template_variant_value_ids.name", "=", product_att_value.name)]
)
product_b1 = self._create_product("CODEB1", "Product B1", self.main_company.id)
template_b = product_b1.product_tmpl_id
Expand Down Expand Up @@ -930,12 +976,7 @@ def test_stock_request_order_from_products(self):
# the action from the products, so test that they get a friendlier
# error message.
self.stock_request_user.groups_id -= self.stock_request_user_group
with self.assertRaisesRegex(
exceptions.UserError,
"Unfortunately it seems you do not have the necessary rights "
"for creating stock requests. Please contact your "
"administrator.",
):
with self.assertRaises(exceptions.AccessError):
order.with_user(self.stock_request_user)._create_from_product_multiselect(
template_a + template_b
)
Expand Down
10 changes: 10 additions & 0 deletions stock_request/views/product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<field name="name">Request Stock</field>
<field name="type">ir.actions.server</field>
<field name="state">code</field>
<field
name="groups_id"
eval="[(4, ref('stock_request.group_stock_request_user')),
(4, ref('stock_request.group_stock_request_manager'))]"
/>
<field name="model_id" ref="product.model_product_product" />
<field name="binding_model_id" ref="product.model_product_product" />
<field name="code">
Expand All @@ -17,6 +22,11 @@ action = records.env['stock.request.order']._create_from_product_multiselect(rec
<field name="name">Request Stock</field>
<field name="type">ir.actions.server</field>
<field name="state">code</field>
<field
name="groups_id"
eval="[(4, ref('stock_request.group_stock_request_user')),
(4, ref('stock_request.group_stock_request_manager'))]"
/>
<field name="model_id" ref="product.model_product_template" />
<field name="binding_model_id" ref="product.model_product_template" />
<field name="code">
Expand Down
2 changes: 1 addition & 1 deletion stock_request/views/stock_request_allocation_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="name">stock.request.allocation.tree</field>
<field name="model">stock.request.allocation</field>
<field name="arch" type="xml">
<tree string="Stock Request Allocations">
<tree>
<field name="stock_request_id" />
<field name="stock_move_id" />
<field name="product_id" />
Expand Down
6 changes: 0 additions & 6 deletions stock_request/views/stock_request_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
name="Master Data"
sequence="100"
/>
<menuitem
id="menu_stock_request_master_data"
parent="menu_stock_request_root"
name="Master Data"
sequence="100"
/>
<menuitem
id="menu_stock_request_config"
name="Settings"
Expand Down
2 changes: 1 addition & 1 deletion stock_request/views/stock_request_order_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<field name="name">stock.request.order.tree</field>
<field name="model">stock.request.order</field>
<field name="arch" type="xml">
<tree string="Stock requests">
<tree>
<field name="name" />
<field name="warehouse_id" groups="stock.group_stock_multi_locations" />
<field name="location_id" groups="stock.group_stock_multi_locations" />
Expand Down
1 change: 0 additions & 1 deletion stock_request/views/stock_request_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<field name="model">stock.request</field>
<field name="arch" type="xml">
<tree
string="Stock Requests"
decoration-muted="state == 'cancel'"
decoration-bf="message_needaction==True"
>
Expand Down

0 comments on commit f7dd60f

Please sign in to comment.