Merge PR #1246 into 13.0

Signed-off-by pedrobaeza
This commit is contained in:
OCA-git-bot
2021-09-21 13:55:25 +00:00
2 changed files with 26 additions and 9 deletions

View File

@@ -1,6 +1,8 @@
# Copyright 2021 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
import operator as operator_lib
from odoo import _, fields, models
class StockProductionLot(models.Model):
@@ -8,12 +10,16 @@ class StockProductionLot(models.Model):
product_qty = fields.Float(search="_search_product_qty")
@api.model
def _is_unsupported_search_operator(self, operator):
return operator != ">"
def _search_product_qty(self, operator, value):
if self._is_unsupported_search_operator(operator) or value:
operator_mapping = {
">": operator_lib.gt,
">=": operator_lib.ge,
"<": operator_lib.lt,
"<=": operator_lib.le,
"=": operator_lib.eq,
"!=": operator_lib.ne,
}
if operator not in operator_mapping:
raise ValueError(_("Unsupported search"))
ids = []
domain = [
@@ -23,7 +29,18 @@ class StockProductionLot(models.Model):
groups = self.env["stock.quant"].read_group(
domain, ["lot_id", "quantity"], ["lot_id"]
)
involved_lot_ids = [group["lot_id"][0] for group in groups]
if (
(operator == "=" and value == 0)
or (operator == "<" and value > 0)
or (operator == "<=" and value >= 0)
):
ids.extend(
self.env["stock.production.lot"]
.search([("id", "not in", involved_lot_ids)])
.ids
)
for group in groups:
if group["quantity"] > value:
if operator_mapping[operator](group["quantity"], value):
ids.append(group["lot_id"][0])
return [("id", "in", ids)]

View File

@@ -28,9 +28,9 @@ class TestStockLotFilterAvailable(SavepointCase):
def test_bad_operator(self):
with self.assertRaises(ValueError):
self.StockProductionLot._search_product_qty("in", [10, 15])
with self.assertRaises(ValueError):
self.StockProductionLot._search_product_qty(">", 2)
def test_good_operator(self):
domain = self.StockProductionLot._search_product_qty(">", 0)
self.assertTrue(self.lot.id in domain[0][2])
domain = self.StockProductionLot._search_product_qty(">", 2)
self.assertTrue(self.lot.id in domain[0][2])