From 70b509d2a420ab8f7b1334752032a52aaab1e6bc Mon Sep 17 00:00:00 2001 From: AaronHForgeFlow Date: Mon, 3 Jun 2024 15:00:40 +0200 Subject: [PATCH] [IMP] stock_inventory_discrepancy: Filter overdiscrepancy quants: Useful when performing inventory adjustments --- .../models/stock_quant.py | 25 +++++++++++++++++-- .../views/stock_quant_view.xml | 19 ++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/stock_inventory_discrepancy/models/stock_quant.py b/stock_inventory_discrepancy/models/stock_quant.py index 7ec8ef851..2674848b3 100644 --- a/stock_inventory_discrepancy/models/stock_quant.py +++ b/stock_inventory_discrepancy/models/stock_quant.py @@ -1,6 +1,7 @@ # Copyright 2023 Tecnativa - Ernesto Tejeda # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import UserError class StockQuant(models.Model): @@ -23,9 +24,29 @@ class StockQuant(models.Model): compute="_compute_discrepancy_threshold", ) has_over_discrepancy = fields.Boolean( - compute="_compute_has_over_discrepancy", + compute="_compute_has_over_discrepancy", search="_search_has_over_discrepancy" ) + def _search_has_over_discrepancy(self, operator, value): + if operator not in ["=", "!="]: + raise UserError(_("This operator is not supported")) + if value == "True": + value = True + elif value == "False": + value = False + if not isinstance(value, bool): + raise UserError(_("Value should be True or False (not %s)") % value) + ids = [] + for quant in self.search([]): + has_over_discrepancy = ( + quant.discrepancy_percent > quant.location_id.discrepancy_threshold + ) + if (operator == "=" and has_over_discrepancy == value) or ( + operator == "!=" and has_over_discrepancy != value + ): + ids.append(quant.id) + return [("id", "in", ids)] + @api.depends("quantity", "inventory_quantity") def _compute_discrepancy(self): for quant in self: diff --git a/stock_inventory_discrepancy/views/stock_quant_view.xml b/stock_inventory_discrepancy/views/stock_quant_view.xml index 0aa61ee9a..3e203613b 100644 --- a/stock_inventory_discrepancy/views/stock_quant_view.xml +++ b/stock_inventory_discrepancy/views/stock_quant_view.xml @@ -36,4 +36,23 @@ + + + stock.quant.last_has_over_discrepancy.search + stock.quant + + + + + + + + + + +