[IMP] stock_generate_putaway_from_inventory: black

This commit is contained in:
Kevin Khao
2020-02-27 11:13:25 +01:00
committed by Pierrick Brun
parent 9aa537f275
commit fd8977714c
3 changed files with 52 additions and 40 deletions

View File

@@ -10,8 +10,6 @@
"category": "Warehouse", "category": "Warehouse",
"depends": ["stock_putaway_product"], "depends": ["stock_putaway_product"],
"license": "AGPL-3", "license": "AGPL-3",
"data": [ "data": ["views/stock_inventory.xml",],
"views/stock_inventory.xml", "installable": True,
],
'installable': True,
} }

View File

@@ -7,7 +7,7 @@ from odoo.exceptions import UserError
class StockLocation(models.Model): class StockLocation(models.Model):
_inherit = 'stock.location' _inherit = "stock.location"
def _get_putaway_strategy(self): def _get_putaway_strategy(self):
if self.putaway_strategy_id: if self.putaway_strategy_id:
@@ -17,24 +17,28 @@ class StockLocation(models.Model):
class StockInventory(models.Model): class StockInventory(models.Model):
_inherit = 'stock.inventory' _inherit = "stock.inventory"
@api.multi @api.multi
def generate_putaway_strategy(self): def generate_putaway_strategy(self):
putaway_locations = {} putaway_locations = {}
for inventory in self: for inventory in self:
if self.state != 'done': if self.state != "done":
raise UserError(_( raise UserError(
'Please, validate the stock adjustment before')) _("Please, validate the stock adjustment before")
)
strategy = self.location_id._get_putaway_strategy() strategy = self.location_id._get_putaway_strategy()
if not strategy: if not strategy:
raise UserError(_( raise UserError(
'Please, specify a Putaway Strategy ' _(
'on the inventory\'s location (or a parent one)')) "Please, specify a Putaway Strategy "
"on the inventory's location (or a parent one)"
)
)
putaway_locations.update(self._prepare_putaway_locations(strategy)) putaway_locations.update(self._prepare_putaway_locations(strategy))
for putaway_location in putaway_locations.values(): for putaway_location in putaway_locations.values():
putaway_location.pop('qty') putaway_location.pop("qty")
self.env['stock.product.putaway.strategy'].create(putaway_location) self.env["stock.product.putaway.strategy"].create(putaway_location)
def _prepare_putaway_locations(self, strategy): def _prepare_putaway_locations(self, strategy):
self.ensure_one() self.ensure_one()
@@ -42,26 +46,29 @@ class StockInventory(models.Model):
for line in self.line_ids: for line in self.line_ids:
if line.product_id.product_putaway_ids: if line.product_id.product_putaway_ids:
continue continue
if (line.product_id.id not in putaway_locations if (
or line.product_qty > line.product_id.id not in putaway_locations
putaway_locations[line.product_id.id]['qty']): or line.product_qty
> putaway_locations[line.product_id.id]["qty"]
):
# If there is several lines for a product, we will use the # If there is several lines for a product, we will use the
# one having more products # one having more products
putaway_locations[line.product_id.id] = line.\ putaway_locations[
_prepare_putaway_location(strategy) line.product_id.id
] = line._prepare_putaway_location(strategy)
return putaway_locations return putaway_locations
class StockInventoryLine(models.Model): class StockInventoryLine(models.Model):
_inherit = 'stock.inventory.line' _inherit = "stock.inventory.line"
def _prepare_putaway_location(self, strategy): def _prepare_putaway_location(self, strategy):
self.ensure_one() self.ensure_one()
res = { res = {
'qty': self.product_qty, "qty": self.product_qty,
'product_product_id': self.product_id.id, "product_product_id": self.product_id.id,
'product_tmpl_id': self.product_id.product_tmpl_id.id, "product_tmpl_id": self.product_id.product_tmpl_id.id,
'fixed_location_id': self.location_id.id, "fixed_location_id": self.location_id.id,
'putaway_id': strategy.id, "putaway_id": strategy.id,
} }
return res return res

View File

@@ -10,26 +10,33 @@ class TestGeneratePutaway(TransactionCase):
def test_generate(self): def test_generate(self):
"""Test default methods""" """Test default methods"""
self.product_10 = self.env.ref('product.product_product_10') self.product_10 = self.env.ref("product.product_product_10")
self.product_25 = self.env.ref('product.product_product_25') self.product_25 = self.env.ref("product.product_product_25")
inventory = self.env.ref('stock.stock_inventory_0') inventory = self.env.ref("stock.stock_inventory_0")
self.env['stock.inventory.line'].create({ self.env["stock.inventory.line"].create(
'product_id': self.product_25.id, {
'product_uom_id': self.ref('product.product_uom_unit'), "product_id": self.product_25.id,
'inventory_id': inventory.id, "product_uom_id": self.ref("product.product_uom_unit"),
'product_qty': 1.0, "inventory_id": inventory.id,
'location_id': self.ref('stock.stock_location_components'), "product_qty": 1.0,
}) "location_id": self.ref("stock.stock_location_components"),
}
)
inventory.generate_putaway_strategy() inventory.generate_putaway_strategy()
self.assertEquals( self.assertEquals(
len(self.product_25.product_putaway_ids), 1, len(self.product_25.product_putaway_ids),
'pas le bon nombre de putaway strategy créées') 1,
"pas le bon nombre de putaway strategy créées",
)
self.assertEquals( self.assertEquals(
self.product_10.product_putaway_ids.fixed_location_id.id, self.product_10.product_putaway_ids.fixed_location_id.id,
self.ref('stock.stock_location_components')) self.ref("stock.stock_location_components"),
)
self.assertEquals( self.assertEquals(
self.product_10.product_putaway_ids.putaway_id.id, self.product_10.product_putaway_ids.putaway_id.id,
self.ref('stock_putaway_product.product_putaway_per_product_wh')) self.ref("stock_putaway_product.product_putaway_per_product_wh"),
)
self.assertEquals( self.assertEquals(
self.product_25.product_putaway_ids.fixed_location_id.id, self.product_25.product_putaway_ids.fixed_location_id.id,
self.ref('stock.stock_location_14')) self.ref("stock.stock_location_14"),
)