[IMP] stock_change_qty_reason: black, isort

This commit is contained in:
Adrià Gil Sorribes
2019-12-23 08:53:43 +01:00
committed by GuillemCForgeFlow
parent 9245c4ad61
commit 82cf7c8244
8 changed files with 117 additions and 127 deletions

View File

@@ -2,25 +2,23 @@
# Copyright 2019 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': "Stock Change Quantity Reason",
'summary': """
"name": "Stock Change Quantity Reason",
"summary": """
Stock Quantity Change Reason """,
'author': 'ACSONE SA/NV, Odoo Community Association (OCA)',
'website': "https://github.com/OCA/stock-logistics-warehouse",
'category': 'Warehouse Management',
'version': '12.0.1.0.0',
'license': 'AGPL-3',
'depends': [
'stock',
"author": "ACSONE SA/NV, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-warehouse",
"category": "Warehouse Management",
"version": "12.0.1.0.0",
"license": "AGPL-3",
"depends": ["stock"],
"data": [
"security/ir.model.access.csv",
"security/stock_security.xml",
"views/base_config_view.xml",
"views/stock_inventory_line_reason_view.xml",
"views/stock_inventory_line_view.xml",
"views/stock_inventory_view.xml",
"wizard/stock_product_change_qty.xml",
],
'data': [
'security/ir.model.access.csv',
'security/stock_security.xml',
'views/base_config_view.xml',
'views/stock_inventory_line_reason_view.xml',
'views/stock_inventory_line_view.xml',
'views/stock_inventory_view.xml',
'wizard/stock_product_change_qty.xml'
],
'installable': True,
"installable": True,
}

View File

@@ -4,11 +4,12 @@ from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
_inherit = "res.config.settings"
group_qty_reason_preset = fields.Boolean(
string="Preset Change Qty Reason",
required=True,
implied_group='stock_change_qty_reason.group_qty_reason_preset',
implied_group="stock_change_qty_reason.group_qty_reason_preset",
help="Enable use of predefined Reasons to manage Inventory Adjustments"
"and Product Update Quantities Wizard.")
"and Product Update Quantities Wizard.",
)

View File

@@ -4,27 +4,26 @@ from odoo import api, fields, models
class StockInventory(models.Model):
_inherit = 'stock.inventory'
_inherit = "stock.inventory"
reason = fields.Char(help='Type in a reason for the '
'product quantity change')
preset_reason_id = fields.Many2one('stock.inventory.line.reason')
reason = fields.Char(help="Type in a reason for the " "product quantity change")
preset_reason_id = fields.Many2one("stock.inventory.line.reason")
def _get_inventory_lines_values(self):
vals = super(StockInventory, self)._get_inventory_lines_values()
for val in vals:
if self.preset_reason_id:
val['preset_reason_id'] = self.preset_reason_id.id
val["preset_reason_id"] = self.preset_reason_id.id
elif self.reason:
val['reason'] = self.reason
val["reason"] = self.reason
return vals
@api.onchange('reason')
@api.onchange("reason")
def onchange_reason(self):
for line in self.line_ids:
line.reason = self.reason
@api.onchange('preset_reason_id')
@api.onchange("preset_reason_id")
def onchange_preset_reason(self):
for line in self.line_ids:
line.preset_reason_id = self.preset_reason_id

View File

@@ -6,23 +6,26 @@ from odoo import fields, models
class StockInventoryLine(models.Model):
"""Class to inherit model stock.inventory.line"""
_inherit = "stock.inventory.line"
reason = fields.Char(help='Type in a reason for the '
'product quantity change')
preset_reason_id = fields.Many2one('stock.inventory.line.reason')
reason = fields.Char(help="Type in a reason for the " "product quantity change")
preset_reason_id = fields.Many2one("stock.inventory.line.reason")
def _get_move_values(self, qty, location_id, location_dest_id, out):
"""Function to super _get_move_value"""
res = super(StockInventoryLine, self)._get_move_values(
qty, location_id, location_dest_id, out)
context = self.env.context.get(
'change_quantity_reason', False) or self.reason \
if not self.preset_reason_id else self.preset_reason_id.name
if res.get('origin'):
res['origin'] = ' ,'.join([res.get('origin'), context])
qty, location_id, location_dest_id, out
)
context = (
self.env.context.get("change_quantity_reason", False) or self.reason
if not self.preset_reason_id
else self.preset_reason_id.name
)
if res.get("origin"):
res["origin"] = " ,".join([res.get("origin"), context])
else:
res['origin'] = context
res["origin"] = context
if self.preset_reason_id:
res['preset_reason_id'] = self.preset_reason_id.id
res["preset_reason_id"] = self.preset_reason_id.id
return res

View File

@@ -5,14 +5,17 @@ from odoo import fields, models
class StockInventoryLineReason(models.Model):
_name = 'stock.inventory.line.reason'
_description = 'Stock Inventory Line Reason'
_name = "stock.inventory.line.reason"
_description = "Stock Inventory Line Reason"
name = fields.Char('Reason Name')
description = fields.Text('Reason Description')
name = fields.Char("Reason Name")
description = fields.Text("Reason Description")
active = fields.Boolean(default=True)
_sql_constraints = [
('name_unique', 'UNIQUE(name)',
'You cannot have two reason with the same name.'),
(
"name_unique",
"UNIQUE(name)",
"You cannot have two reason with the same name.",
)
]

View File

@@ -6,5 +6,4 @@ from odoo import fields, models
class StockMove(models.Model):
_inherit = "stock.move"
preset_reason_id = fields.Many2one('stock.inventory.line.reason',
required=False)
preset_reason_id = fields.Many2one("stock.inventory.line.reason", required=False)

View File

@@ -7,100 +7,83 @@ from odoo.tests.common import SavepointCase
class TestStockQuantityChangeReason(SavepointCase):
@classmethod
def setUpClass(cls):
super(TestStockQuantityChangeReason, cls).setUpClass()
# MODELS
cls.stock_move = cls.env['stock.move']
cls.product_product_model = cls.env['product.product']
cls.product_category_model = cls.env['product.category']
cls.wizard_model = cls.env['stock.change.product.qty']
cls.preset_reason_id = cls.env['stock.inventory.line.reason']
cls.stock_location = cls.env.ref('stock.stock_location_stock')
cls.stock_move = cls.env["stock.move"]
cls.product_product_model = cls.env["product.product"]
cls.product_category_model = cls.env["product.category"]
cls.wizard_model = cls.env["stock.change.product.qty"]
cls.preset_reason_id = cls.env["stock.inventory.line.reason"]
cls.stock_location = cls.env.ref("stock.stock_location_stock")
# INSTANCES
cls.category = cls.product_category_model.create({
'name': 'Physical (test)'})
cls.category = cls.product_category_model.create({"name": "Physical (test)"})
def _create_product(self, name):
return self.product_product_model.create({
'name': name,
'categ_id': self.category.id,
'type': 'product'})
return self.product_product_model.create(
{"name": name, "categ_id": self.category.id, "type": "product"}
)
def _product_change_qty(self, product, new_qty, reason,
preset_reason_id=None):
values = {
'product_id': product.id,
'new_quantity': new_qty,
'reason': reason,
}
def _product_change_qty(self, product, new_qty, reason, preset_reason_id=None):
values = {"product_id": product.id, "new_quantity": new_qty, "reason": reason}
if preset_reason_id:
values.update({'preset_reason_id': preset_reason_id.id})
values.update({"preset_reason_id": preset_reason_id.id})
wizard = self.wizard_model.create(values)
wizard.change_product_qty()
def _create_reason(self, name, description=None):
return self.preset_reason_id.create({
'name': name,
'description': description})
return self.preset_reason_id.create({"name": name, "description": description})
def test_product_change_qty(self):
""" Check product quantity update move reason is well set
"""
# create products
product2 = self._create_product('product_product_2')
product3 = self._create_product('product_product_3')
product4 = self._create_product('product_product_4')
product5 = self._create_product('product_product_5')
product6 = self._create_product('product_product_6')
product2 = self._create_product("product_product_2")
product3 = self._create_product("product_product_3")
product4 = self._create_product("product_product_4")
product5 = self._create_product("product_product_5")
product6 = self._create_product("product_product_6")
# update qty on hand and add reason
self._product_change_qty(product2, 10, 'product_2_reason')
self._product_change_qty(product3, 0, 'product_3_reason')
self._product_change_qty(product4, 0, 'product_4_reason')
self._product_change_qty(product5, 10, 'product_5_reason')
self._product_change_qty(product6, 0, 'product_6_reason')
self._product_change_qty(product2, 10, "product_2_reason")
self._product_change_qty(product3, 0, "product_3_reason")
self._product_change_qty(product4, 0, "product_4_reason")
self._product_change_qty(product5, 10, "product_5_reason")
self._product_change_qty(product6, 0, "product_6_reason")
# check stock moves created
move2 = self.stock_move.search([('product_id', '=',
product2.id)])
move3 = self.stock_move.search([('product_id', '=',
product3.id)])
move4 = self.stock_move.search([('product_id', '=',
product4.id)])
move5 = self.stock_move.search([('product_id', '=',
product5.id)])
move6 = self.stock_move.search([('product_id', '=',
product6.id)])
move2 = self.stock_move.search([("product_id", "=", product2.id)])
move3 = self.stock_move.search([("product_id", "=", product3.id)])
move4 = self.stock_move.search([("product_id", "=", product4.id)])
move5 = self.stock_move.search([("product_id", "=", product5.id)])
move6 = self.stock_move.search([("product_id", "=", product6.id)])
self.assertEqual(move2.origin, 'product_2_reason')
self.assertEqual(move2.origin, "product_2_reason")
self.assertFalse(move3)
self.assertFalse(move4)
self.assertEqual(move5.origin, 'product_5_reason')
self.assertEqual(move5.origin, "product_5_reason")
self.assertFalse(move6)
def test_product_change_qty_with_preset_reason(self):
""" Check product quantity update move reason is well set
"""
# create reason
reason = self._create_reason('Test', 'Description Test')
reason = self._create_reason("Test", "Description Test")
# create products
product2 = self._create_product('product_product_2')
product3 = self._create_product('product_product_3')
product2 = self._create_product("product_product_2")
product3 = self._create_product("product_product_3")
# update qty on hand and add reason
self._product_change_qty(product2, 10, reason.name, reason)
self._product_change_qty(product3, 0, reason.name, reason)
# check stock moves created
move2 = self.stock_move.search([('product_id', '=',
product2.id)])
move3 = self.stock_move.search([('product_id', '=',
product3.id)])
move2 = self.stock_move.search([("product_id", "=", product2.id)])
move3 = self.stock_move.search([("product_id", "=", product3.id)])
# asserts
self.assertEqual(move2.origin, reason.name)
self.assertEqual(move2.preset_reason_id, reason)
@@ -109,24 +92,24 @@ class TestStockQuantityChangeReason(SavepointCase):
def test_inventory_adjustment_onchange_reason_preset_reason(self):
""" Check that adding a reason or a preset reason explode to lines
"""
product2 = self._create_product('product_product_2')
self._product_change_qty(product2, 50, 'product_2_reason')
inventory = self.env['stock.inventory'].create({
'name': 'remove product2',
'filter': 'product',
'location_id': self.stock_location.id,
'product_id': product2.id,
})
inventory.preset_reason_id = self._create_reason('Test 1',
'Description Test 1')
product2 = self._create_product("product_product_2")
self._product_change_qty(product2, 50, "product_2_reason")
inventory = self.env["stock.inventory"].create(
{
"name": "remove product2",
"filter": "product",
"location_id": self.stock_location.id,
"product_id": product2.id,
}
)
inventory.preset_reason_id = self._create_reason("Test 1", "Description Test 1")
inventory.action_start()
self.assertEqual(len(inventory.line_ids), 1)
inventory.preset_reason_id = self._create_reason('Test 2',
'Description Test 2')
inventory.preset_reason_id = self._create_reason("Test 2", "Description Test 2")
inventory.onchange_preset_reason()
self.assertEquals(inventory.line_ids.preset_reason_id,
inventory.preset_reason_id)
inventory.reason = 'Reason 2'
self.assertEquals(
inventory.line_ids.preset_reason_id, inventory.preset_reason_id
)
inventory.reason = "Reason 2"
inventory.onchange_reason()
self.assertEquals(inventory.line_ids.reason,
inventory.reason)
self.assertEquals(inventory.line_ids.reason, inventory.reason)

View File

@@ -6,11 +6,11 @@ from odoo import api, fields, models
class StockChangeProductQty(models.TransientModel):
"""Class to inherit model stock.change.product.qty"""
_inherit = 'stock.change.product.qty'
reason = fields.Char(help='Type in a reason for the '
'product quantity change')
preset_reason_id = fields.Many2one('stock.inventory.line.reason')
_inherit = "stock.change.product.qty"
reason = fields.Char(help="Type in a reason for the " "product quantity change")
preset_reason_id = fields.Many2one("stock.inventory.line.reason")
@api.multi
def change_product_qty(self):
@@ -23,13 +23,17 @@ class StockChangeProductQty(models.TransientModel):
def _action_start_line(self):
res = super(StockChangeProductQty, self)._action_start_line()
if self.preset_reason_id:
res.update({'preset_reason_id': self.preset_reason_id.id,
'reason': self.preset_reason_id.name})
res.update(
{
"preset_reason_id": self.preset_reason_id.id,
"reason": self.preset_reason_id.name,
}
)
elif self.reason:
res.update({'reason': self.reason})
res.update({"reason": self.reason})
return res
@api.onchange('preset_reason_id')
@api.onchange("preset_reason_id")
def onchange_preset_reason_id(self):
if self.preset_reason_id:
self.reason = self.preset_reason_id.name