Skip to content

Commit

Permalink
[MIG] stock_putaway_product_form: Migration to 13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
victoralmau committed Oct 28, 2020
1 parent 39cbe9e commit 1fa6d5b
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 131 deletions.
1 change: 0 additions & 1 deletion stock_putaway_product_form/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

from . import models
from . import tests
from .hooks import post_init_hook
4 changes: 1 addition & 3 deletions stock_putaway_product_form/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
{
"name": "Putaway strategies on product form view",
"summary": "Edit putaway strategies directly from the product form view",
"version": "12.0.1.0.0",
"version": "13.0.1.0.0",
"category": "Inventory",
"website": "https://github.com/OCA/stock-logistics-warehouse",
"author": "Akretion, Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["stock"],
"external_dependencies": {"python": ["openupgradelib"]},
"data": ["views/product.xml"],
"demo": ["demo/putaway_strategies.xml"],
"maintainers": ["kevinkhao", "sebastienbeau"],
"post_init_hook": "post_init_hook",
}
42 changes: 21 additions & 21 deletions stock_putaway_product_form/demo/putaway_strategies.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" ?>
<odoo>
<!--Shows simplest case for categories -->
<record id="putaway_strat_1" model="product.putaway">
<field name="name">Putaway Strategy 1</field>
</record>
<record id="putaway_strat_1_line_1" model="stock.fixed.putaway.strat">
<odoo noupdate="1">
<record id="putaway_rule_1" model="stock.putaway.rule">
<field name="sequence">1</field>
<field name="company_id" ref="stock.res_company_1" />
<field name="product_id" ref="product.product_product_11" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_1" />
<field name="fixed_location_id" ref="stock.stock_location_shop0" />
<field name="location_in_id" ref="stock.stock_location_shop0" />
<field name="location_out_id" ref="stock.stock_location_shop0" />
</record>
<record id="putaway_strat_1_line_2" model="stock.fixed.putaway.strat">
<record id="putaway_rule_2" model="stock.putaway.rule">
<field name="sequence">2</field>
<field name="company_id" ref="stock.res_company_1" />
<field name="category_id" ref="product.product_category_5" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_1" />
<field name="fixed_location_id" ref="stock.stock_location_shop0" />
</record>
<!--Shows inherited case for categories-->
<record id="putaway_strat_2" model="product.putaway">
<field name="name">Putaway Strategy 2</field>
<field name="location_in_id" ref="stock.stock_location_shop0" />
<field name="location_out_id" ref="stock.stock_location_shop0" />
</record>
<record id="putaway_strat_2_line_1" model="stock.fixed.putaway.strat">
<record id="putaway_rule_3" model="stock.putaway.rule">
<field name="sequence">3</field>
<field name="company_id" ref="stock.res_company_1" />
<field name="category_id" ref="product.product_category_1" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_2" />
<field name="fixed_location_id" ref="stock.stock_location_stock" />
<field name="location_in_id" ref="stock.stock_location_shop0" />
<field name="location_out_id" ref="stock.stock_location_shop0" />
</record>
<record id="putaway_strat_2_line_2" model="stock.fixed.putaway.strat">
<record id="putaway_rule_4" model="stock.putaway.rule">
<field name="sequence">4</field>
<field name="company_id" ref="stock.res_company_1" />
<field name="category_id" ref="product.product_category_all" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_2" />
<field name="fixed_location_id" ref="stock.stock_location_stock" />
<field name="location_in_id" ref="stock.stock_location_shop0" />
<field name="location_out_id" ref="stock.stock_location_shop0" />
</record>
</odoo>
21 changes: 0 additions & 21 deletions stock_putaway_product_form/hooks.py

This file was deleted.

2 changes: 1 addition & 1 deletion stock_putaway_product_form/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import product_product
from . import product_template
from . import putaway_strategy
from . import stock_putaway_rule
2 changes: 1 addition & 1 deletion stock_putaway_product_form/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProductProduct(models.Model):
_inherit = "product.product"

product_product_putaway_ids = fields.One2many(
comodel_name="stock.fixed.putaway.strat",
comodel_name="stock.putaway.rule",
inverse_name="product_id",
string="Product putaway strategies by product variant",
)
25 changes: 4 additions & 21 deletions stock_putaway_product_form/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@ class ProductTemplate(models.Model):
_inherit = "product.template"

product_tmpl_putaway_ids = fields.One2many(
comodel_name="stock.fixed.putaway.strat",
comodel_name="stock.putaway.rule",
inverse_name="product_tmpl_id",
string="Product putaway strategies by product",
)

product_putaway_categ_ids = fields.Many2many(
comodel_name="stock.fixed.putaway.strat",
comodel_name="stock.putaway.rule",
string="Product putaway strategies by category",
compute="_compute_putaway_categ_ids",
)

def _find_closest_categ_match(self, categ, putaway_lines):
"""Returns the putaway line with the nearest product category"""
lines_match_categ = putaway_lines.filtered(lambda r: r.category_id == categ)
if lines_match_categ:
return lines_match_categ[0]
elif categ.parent_id:
return self._find_closest_categ_match(categ.parent_id, putaway_lines)
else:
return self.env["stock.fixed.putaway.strat"]

def _get_categ_and_parents(self, categ):
parent_categ_iterator = categ
res = self.env["product.category"]
Expand All @@ -42,17 +32,10 @@ def _compute_putaway_categ_ids(self):
i.e closest to our product category's parents)
putaway.strat per product.putaway"""
for rec in self:
res = self.env["stock.fixed.putaway.strat"]
categ = rec.categ_id
categs = self._get_categ_and_parents(categ)
# get matching lines from our category or its parents
product_putaway_categ_lines = self.env["stock.fixed.putaway.strat"].search(
rules = self.env["stock.putaway.rule"].search(
[("category_id", "in", categs.ids)]
)
# from these, get the matching putaway.strats and find
# the lowest-level category match
product_putaways = product_putaway_categ_lines.mapped("putaway_id")
for el in product_putaways:
lines = el.fixed_location_ids
res += self._find_closest_categ_match(categ, lines)
rec.product_putaway_categ_ids = res
rec.product_putaway_categ_ids = rules
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
# Copyright 2020 Sergio Teruel - Tecnativa <sergio.teruel@tecnativa.com>
# Copyright 2020 Sergio Teruel - Tecnativa

from odoo import api, fields, models


class PutAwayStrategy(models.Model):
_inherit = "product.putaway"

# Remove product domain to allow to select product templates
product_location_ids = fields.One2many(domain=[])

def _get_putaway_rule(self, product):
return super(
PutAwayStrategy, self.with_context(filter_putaway_rule=True)
)._get_putaway_rule(product)


class FixedPutAwayStrategy(models.Model):
_inherit = "stock.fixed.putaway.strat"
class StockPutawayRule(models.Model):
_inherit = "stock.putaway.rule"

product_tmpl_id = fields.Many2one(
comodel_name="product.template",
Expand All @@ -38,7 +26,7 @@ def _compute_product_tmpl_id(self):
rec.product_tmpl_id = params.get("id", False)

def filtered(self, func):
res = super(FixedPutAwayStrategy, self).filtered(func)
res = super(StockPutawayRule, self).filtered(func)
if res or not self.env.context.get("filter_putaway_rule"):
return res
product = func.__closure__[0].cell_contents
Expand Down
1 change: 1 addition & 0 deletions stock_putaway_product_form/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* Sergio Teruel
* Víctor Martínez
67 changes: 29 additions & 38 deletions stock_putaway_product_form/tests/test_product_putaway.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
class TestProductPutaway(TransactionCase):
def setUp(self):
super().setUp()
self.putawayObj = self.env["product.putaway"]
self.putawayLineObj = self.env["stock.fixed.putaway.strat"]
self.putawayRuleObj = self.env["stock.putaway.rule"]
ProductTemplate = self.env["product.template"]
ProductAttribute = self.env["product.attribute"]
ProductAttributeValue = self.env["product.attribute.value"]
Expand All @@ -16,10 +15,10 @@ def setUp(self):
self.product_tmpl_chair = ref("product.product_product_11_product_template")
self.product_product_chair = ref("product.product_product_11")
self.category_services = ref("product.product_category_3")
self.putaway_line_1 = ref("stock_putaway_product_form.putaway_strat_1_line_1")
self.putaway_line_2 = ref("stock_putaway_product_form.putaway_strat_1_line_2")
self.putaway_line_3 = ref("stock_putaway_product_form.putaway_strat_2_line_1")
self.putaway_line_4 = ref("stock_putaway_product_form.putaway_strat_2_line_2")
self.putaway_rule_1 = ref("stock_putaway_product_form.putaway_rule_1")
self.putaway_rule_2 = ref("stock_putaway_product_form.putaway_rule_2")
self.putaway_rule_3 = ref("stock_putaway_product_form.putaway_rule_3")
self.putaway_rule_4 = ref("stock_putaway_product_form.putaway_rule_4")

# Add a product with variants
self.template = ProductTemplate.create(
Expand All @@ -41,33 +40,24 @@ def setUp(self):
"value_ids": [(6, 0, [self.size_m.id, self.size_l.id])],
}
)
self.template.create_variant_ids()

def test_tmpl_has_putaways_from_products(self):
self.assertIn(
self.putaway_line_1, self.product_tmpl_chair.product_tmpl_putaway_ids,
)
self.putaway_line_1.product_id = self.env["product.product"]
self.assertNotIn(
self.putaway_line_1, self.product_tmpl_chair.product_tmpl_putaway_ids,
)
self.template._create_variant_ids()

def test_tmpl_has_putaways_from_category_simple(self):
self.assertIn(
self.putaway_line_2, self.product_tmpl_chair.product_putaway_categ_ids,
self.putaway_rule_2, self.product_tmpl_chair.product_putaway_categ_ids,
)
self.product_tmpl_chair.categ_id = self.category_services
self.assertNotIn(
self.putaway_line_2, self.product_tmpl_chair.product_putaway_categ_ids,
self.putaway_rule_2, self.product_tmpl_chair.product_putaway_categ_ids,
)

def test_tmpl_has_putaways_from_category_parent(self):
# chair is under category: all/saleable/office
self.assertIn(
self.putaway_line_3, self.product_tmpl_chair.product_putaway_categ_ids,
self.putaway_rule_3, self.product_tmpl_chair.product_putaway_categ_ids,
)
self.assertNotIn(
self.putaway_line_4, self.product_tmpl_chair.product_putaway_categ_ids,
self.assertIn(
self.putaway_rule_4, self.product_tmpl_chair.product_putaway_categ_ids,
)

def test_apply_putaway(self):
Expand All @@ -80,23 +70,24 @@ def test_apply_putaway(self):
location2 = location.copy(
{"name": "Location test 2", "location_id": location.id}
)
variant1 = self.template.product_variant_ids[0]
variant2 = self.template.product_variant_ids[1]
putaway = self.putawayObj.create({"name": "Putaway for test"})
val_list = [
variant = self.template.product_variant_ids[1]
rule1 = self.putawayRuleObj.create(
{
"putaway_id": putaway.id,
"sequence": 1,
"company_id": location1.company_id.id,
"product_tmpl_id": self.template.id,
"fixed_location_id": location1.id,
},
"location_in_id": location1.id,
"location_out_id": location1.id,
}
)
self.assertEqual(rule1.location_in_id, location1)
rule2 = self.putawayRuleObj.create(
{
"putaway_id": putaway.id,
"product_id": variant2.id,
"fixed_location_id": location2.id,
},
]
self.putawayLineObj.create(val_list)
location_applied = putaway._get_putaway_rule(variant1).fixed_location_id
self.assertEqual(location_applied, location1)
location_applied = putaway._get_putaway_rule(variant2).fixed_location_id
self.assertEqual(location_applied, location2)
"sequence": 2,
"company_id": location1.company_id.id,
"product_id": variant.id,
"location_in_id": location2.id,
"location_out_id": location2.id,
}
)
self.assertEqual(rule2.location_in_id, location2)
43 changes: 35 additions & 8 deletions stock_putaway_product_form/views/product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@
>Keep empty product field to apply strategy to all variants.</div>
</p>
</group>
<field name="product_tmpl_putaway_ids" nolabel="1">
<field
name="product_tmpl_putaway_ids"
domain="[('company_id', '=', company_id)]"
nolabel="1"
>
<tree editable="top">
<field
name="product_id"
domain="[('product_tmpl_id', '=', parent.id)]"
/>
<field name="putaway_id" />
<field name="fixed_location_id" />
<field
name="location_in_id"
domain="[('company_id', '=', parent.company_id)]"
/>
<field
name="location_out_id"
domain="[('company_id', '=', parent.company_id)]"
/>
</tree>
</field>
</group>
Expand All @@ -42,11 +52,21 @@
string="Put Away Strategies by category"
attrs="{'readonly': 1}"
>
<field name="product_putaway_categ_ids" nolabel="1">
<field
name="product_putaway_categ_ids"
domain="[('company_id', '=', company_id)]"
nolabel="1"
>
<tree>
<field name="category_id" />
<field name="putaway_id" widget="selection" />
<field name="fixed_location_id" />
<field
name="location_in_id"
domain="[('company_id', '=', parent.company_id)]"
/>
<field
name="location_out_id"
domain="[('company_id', '=', parent.company_id)]"
/>
</tree>
</field>
</group>
Expand All @@ -68,12 +88,19 @@
>
<field
name="product_product_putaway_ids"
domain="[('company_id', '=', company_id)]"
groups="stock.group_adv_location"
nolabel="1"
>
<tree editable="bottom">
<field name="putaway_id" />
<field name="fixed_location_id" />
<field
name="location_in_id"
domain="[('company_id', '=', parent.company_id)]"
/>
<field
name="location_out_id"
domain="[('company_id', '=', parent.company_id)]"
/>
</tree>
</field>
</group>
Expand Down

0 comments on commit 1fa6d5b

Please sign in to comment.