From 692847255cc0627ca1f8f9edcdb7e625f39c7a30 Mon Sep 17 00:00:00 2001 From: dip-odoo Date: Wed, 20 Dec 2017 19:03:22 +0530 Subject: [PATCH] [IMP] stock: validate inventory adjustment with product tracked When you have some product tracked in an inventory adjustment, the lot/serial number is not required, so when you confirm an inventory adjustment with product tracked for which you didn't provide serial/lot number you get a warning to inform you. TASK-30921 --- addons/stock/__manifest__.py | 1 + addons/stock/models/stock_inventory.py | 19 ++++++++++++++- addons/stock/tests/test_inventory.py | 4 +++- addons/stock/views/stock_inventory_views.xml | 2 +- addons/stock/wizard/__init__.py | 1 + .../stock/wizard/stock_track_confirmation.py | 22 +++++++++++++++++ .../wizard/stock_track_confirmation_views.xml | 24 +++++++++++++++++++ 7 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 addons/stock/wizard/stock_track_confirmation.py create mode 100644 addons/stock/wizard/stock_track_confirmation_views.xml diff --git a/addons/stock/__manifest__.py b/addons/stock/__manifest__.py index 6b3a6b9ad01..860f86f631b 100644 --- a/addons/stock/__manifest__.py +++ b/addons/stock/__manifest__.py @@ -45,6 +45,7 @@ 'wizard/stock_quantity_history.xml', 'wizard/stock_warn_insufficient_qty_views.xml', 'wizard/product_replenish_views.xml', + 'wizard/stock_track_confirmation_views.xml', 'views/res_partner_views.xml', 'views/product_strategy_views.xml', diff --git a/addons/stock/models/stock_inventory.py b/addons/stock/models/stock_inventory.py index 225e559db41..8f1269cab46 100644 --- a/addons/stock/models/stock_inventory.py +++ b/addons/stock/models/stock_inventory.py @@ -4,7 +4,7 @@ from odoo import api, fields, models, _ from odoo.addons import decimal_precision as dp from odoo.exceptions import UserError -from odoo.tools import float_utils +from odoo.tools import float_utils, float_compare class Inventory(models.Model): @@ -162,6 +162,23 @@ class Inventory(models.Model): self.mapped('line_ids').write({'product_qty': 0}) return True + def action_validate(self): + inventory_lines = self.line_ids.filtered(lambda l: l.product_id.tracking in ['lot', 'serial'] and not l.prod_lot_id and l.theoretical_qty != l.product_qty) + lines = self.line_ids.filtered(lambda l: float_compare(l.product_qty, 1, precision_rounding=l.product_uom_id.rounding) > 0 and l.product_id.tracking == 'serial' and l.prod_lot_id) + if inventory_lines and not lines: + wiz_lines = [(0, 0, {'product_id': product.id, 'tracking': product.tracking}) for product in inventory_lines.mapped('product_id')] + wiz = self.env['stock.track.confirmation'].create({'inventory_id': self.id, 'tracking_line_ids': wiz_lines}) + return { + 'name': _('Tracked Products in Inventory Adjustment'), + 'type': 'ir.actions.act_window', + 'view_mode': 'form', + 'res_model': 'stock.track.confirmation', + 'target': 'new', + 'res_id': wiz.id, + } + else: + self.action_done() + def action_done(self): negative = next((line for line in self.mapped('line_ids') if line.product_qty < 0 and line.product_qty != line.theoretical_qty), False) if negative: diff --git a/addons/stock/tests/test_inventory.py b/addons/stock/tests/test_inventory.py index 88bd9d0aadb..1d0ad411e7a 100644 --- a/addons/stock/tests/test_inventory.py +++ b/addons/stock/tests/test_inventory.py @@ -135,7 +135,9 @@ class TestInventory(TransactionCase): 'product_qty': 10, 'location_id': self.stock_location.id, }) - inventory.action_done() + res_dict_for_warning_lot = inventory.action_validate() + wizard_warning_lot = self.env[(res_dict_for_warning_lot.get('res_model'))].browse(res_dict_for_warning_lot.get('res_id')) + wizard_warning_lot.action_confirm() # check self.assertEqual(self.env['stock.quant']._get_available_quantity(self.product2, self.stock_location, lot_id=lot1, strict=True), 1.0) diff --git a/addons/stock/views/stock_inventory_views.xml b/addons/stock/views/stock_inventory_views.xml index 02e28c83537..807db577f8a 100644 --- a/addons/stock/views/stock_inventory_views.xml +++ b/addons/stock/views/stock_inventory_views.xml @@ -114,7 +114,7 @@