mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[MIG/REF] stock_generate_putaway_from_inventory: Migration to 14.0
This commit is contained in:
@@ -4,8 +4,9 @@
|
||||
{
|
||||
"name": "Stock Generate Putaway from Inventory",
|
||||
"summary": "Generate Putaway locations per Product deduced from Inventory",
|
||||
"version": "12.0.1.0.0",
|
||||
"version": "14.0.1.0.0",
|
||||
"author": "Akretion, Odoo Community Association (OCA)",
|
||||
"website": "https://github.com/OCA/stock-logistics-warehouse",
|
||||
"category": "Warehouse",
|
||||
"depends": ["stock"],
|
||||
"license": "AGPL-3",
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
from . import stock_inventory
|
||||
from . import stock_location
|
||||
|
||||
@@ -8,52 +8,51 @@ from odoo.exceptions import ValidationError
|
||||
class StockInventory(models.Model):
|
||||
_inherit = "stock.inventory"
|
||||
|
||||
def action_generate_putaway_strategy(self):
|
||||
for inventory in self:
|
||||
inventory._generate_putaway_strategy()
|
||||
def action_generate_putaway_rules(self):
|
||||
self._generate_putaway_rules()
|
||||
|
||||
def _get_putaway_strategy(self, loc):
|
||||
if loc.putaway_strategy_id:
|
||||
return loc.putaway_strategy_id
|
||||
elif loc.location_id:
|
||||
return self._get_putaway_strategy(loc.location_id)
|
||||
|
||||
def _update_product_putaway_strategy(self, inventory_line, strategy):
|
||||
putaway_line_obj = self.env["stock.fixed.putaway.strat"]
|
||||
putaway_line = putaway_line_obj.search(
|
||||
[
|
||||
("product_id", "=", inventory_line.product_id.id),
|
||||
("putaway_id", "=", strategy.id),
|
||||
]
|
||||
)
|
||||
if putaway_line:
|
||||
putaway_line.write({"fixed_location_id": inventory_line.location_id.id})
|
||||
else:
|
||||
putaway_line_obj.create(
|
||||
{
|
||||
"product_id": inventory_line.product_id.id,
|
||||
"fixed_location_id": inventory_line.location_id.id,
|
||||
"putaway_id": strategy.id,
|
||||
}
|
||||
)
|
||||
|
||||
def _generate_putaway_strategy(self):
|
||||
if self.state != "done":
|
||||
def _generate_putaway_rules(self):
|
||||
for record in self:
|
||||
if record.state != "done":
|
||||
raise ValidationError(
|
||||
_(
|
||||
"Please validate the inventory before generating "
|
||||
"the putaway strategy."
|
||||
)
|
||||
)
|
||||
putaway = self._get_putaway_strategy(self.location_id)
|
||||
if not putaway:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"Inventory location doesn't have a putaway strategy. "
|
||||
"Please set a putaway strategy on the inventory's "
|
||||
"location."
|
||||
for location in record.location_ids:
|
||||
record.line_ids._generate_putaway_rules(location)
|
||||
|
||||
|
||||
class StockInventoryLine(models.Model):
|
||||
_inherit = "stock.inventory.line"
|
||||
|
||||
def _generate_putaway_rules(self, inventory_location):
|
||||
# Eliminate lines for other IN locations
|
||||
# and eliminate lines where quantity is null
|
||||
self.filtered(
|
||||
lambda x: x.location_id._is_child(inventory_location) and x.product_qty > 0
|
||||
)._update_product_putaway_rule(inventory_location)
|
||||
|
||||
def _update_product_putaway_rule(self, location_in):
|
||||
putaway_rule_obj = self.env["stock.putaway.rule"]
|
||||
putaway_rules = putaway_rule_obj.search(
|
||||
[
|
||||
("product_id", "in", self.mapped("product_id").ids),
|
||||
("location_in_id", "=", location_in.id),
|
||||
]
|
||||
)
|
||||
for record in self:
|
||||
putaway_rule = putaway_rules.filtered(
|
||||
lambda x: x.product_id == record.product_id
|
||||
)
|
||||
if putaway_rule:
|
||||
putaway_rule.location_out_id = record.location_id
|
||||
else:
|
||||
putaway_rule_obj.create(
|
||||
{
|
||||
"product_id": record.product_id.id,
|
||||
"location_in_id": location_in.id,
|
||||
"location_out_id": record.location_id.id,
|
||||
}
|
||||
)
|
||||
for line in self.line_ids:
|
||||
if line.product_qty > 0:
|
||||
self._update_product_putaway_strategy(line, putaway)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Copyright 2021 Akretion (https://www.akretion.com).
|
||||
# @author Pierrick Brun <pierrick.brun@akretion.com>
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class StockLocation(models.Model):
|
||||
_inherit = "stock.location"
|
||||
|
||||
def _is_child(self, location):
|
||||
self.ensure_one()
|
||||
if self == location:
|
||||
return True
|
||||
elif self.location_id:
|
||||
return self.location_id._is_child(location)
|
||||
else:
|
||||
return False
|
||||
@@ -14,57 +14,47 @@ class TestGeneratePutaway(TransactionCase):
|
||||
self.inventory = self.env["stock.inventory"].create(
|
||||
{
|
||||
"name": "example inventory",
|
||||
"location_id": self.inventory_location.id,
|
||||
"location_ids": [(6, 0, [self.inventory_location.id])],
|
||||
}
|
||||
)
|
||||
self.inventory_line_1_product = ref("product.product_product_24")
|
||||
self.inventory_line_1_location = ref("stock.stock_location_14")
|
||||
self.inventory_line_1_location = ref("stock.stock_location_components")
|
||||
self.inventory_line_1 = self.env["stock.inventory.line"].create(
|
||||
{
|
||||
"product_id": self.inventory_line_1_product.id,
|
||||
"location_id": self.inventory_line_1_location.id,
|
||||
"inventory_id": self.inventory.id,
|
||||
"product_qty": 1,
|
||||
}
|
||||
)
|
||||
self.irrelevant_location = ref("stock.stock_location_customers")
|
||||
|
||||
def test_error_not_validated(self):
|
||||
putaway = self.env["product.putaway"].create({"name": "Putaway example"})
|
||||
self.inventory.putaway_strategy_id = putaway
|
||||
self.inventory.action_cancel_draft()
|
||||
with self.assertRaises(ValidationError):
|
||||
self.inventory.action_generate_putaway_strategy()
|
||||
|
||||
def test_error_location_has_no_putaway_strategy(self):
|
||||
self.inventory_location.putaway_strategy_id = self.env["product.putaway"]
|
||||
self.inventory.action_start()
|
||||
self.inventory.action_validate()
|
||||
with self.assertRaises(ValidationError):
|
||||
self.inventory.action_generate_putaway_strategy()
|
||||
self.inventory.action_generate_putaway_rules()
|
||||
|
||||
def test_putaway_line_location_update(self):
|
||||
putaway = self.env["product.putaway"].create({"name": "Putaway example"})
|
||||
putaway_line_1 = self.env["stock.fixed.putaway.strat"].create(
|
||||
putaway = self.env["stock.putaway.rule"].create(
|
||||
{
|
||||
"putaway_id": putaway.id,
|
||||
"fixed_location_id": self.irrelevant_location.id,
|
||||
"location_out_id": self.irrelevant_location.id,
|
||||
"location_in_id": self.inventory_location.id,
|
||||
"product_id": self.inventory_line_1_product.id,
|
||||
}
|
||||
)
|
||||
self.inventory_location.putaway_strategy_id = putaway
|
||||
self.inventory.action_start()
|
||||
self.inventory.action_validate()
|
||||
self.inventory.action_generate_putaway_strategy()
|
||||
self.assertEqual(
|
||||
putaway_line_1.fixed_location_id, self.inventory_line_1_location
|
||||
)
|
||||
self.inventory.action_generate_putaway_rules()
|
||||
self.assertEqual(putaway.location_out_id, self.inventory_line_1_location)
|
||||
|
||||
def test_putaway_line_location_create(self):
|
||||
putaway = self.env["product.putaway"].create({"name": "Putaway example"})
|
||||
self.inventory_location.putaway_strategy_id = putaway
|
||||
self.inventory.action_start()
|
||||
self.inventory.action_validate()
|
||||
self.inventory.action_generate_putaway_strategy()
|
||||
putaway_line = putaway.product_location_ids.filtered(
|
||||
lambda r: r.product_id == self.inventory_line_1_product
|
||||
self.inventory.action_generate_putaway_rules()
|
||||
putaway = self.env["stock.putaway.rule"].search(
|
||||
[
|
||||
("product_id", "=", self.inventory_line_1_product.id),
|
||||
("location_in_id", "=", self.inventory_location.id),
|
||||
]
|
||||
)
|
||||
self.assertEqual(putaway_line.fixed_location_id, self.inventory_line_1_location)
|
||||
self.assertEqual(putaway.location_out_id, self.inventory_line_1_location)
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
<?xml version="1.0" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
<odoo>
|
||||
<record id="action_generate_putaway_strategy" model="ir.actions.server">
|
||||
<field name="name">Generate putaway strategies</field>
|
||||
<field name="model_id" ref="model_stock_inventory" />
|
||||
<field name="binding_model_id" ref="stock.model_stock_inventory" />
|
||||
<field name="state">code</field>
|
||||
<field name="code">
|
||||
records.action_generate_putaway_strategy()
|
||||
records.action_generate_putaway_rules()
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
Reference in New Issue
Block a user