[IMP] stock_inventory_discrepancy: centralize logic for over discrepancy assessment and ease extension.

This commit is contained in:
Lois Rilo
2021-05-27 13:54:19 +02:00
committed by Mateu Griful
parent be0f757ddb
commit ff6fb1d970
4 changed files with 15 additions and 9 deletions

View File

@@ -1,5 +1,4 @@
# Copyright 2017-2020 ForgeFlow S.L. # Copyright 2017-21 ForgeFlow S.L. (https://www.forgeflow.com)
# (http://www.forgeflow.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # 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
@@ -33,7 +32,7 @@ class StockInventory(models.Model):
def _compute_over_discrepancy_line_count(self): def _compute_over_discrepancy_line_count(self):
for inventory in self: for inventory in self:
lines = inventory.line_ids.filtered( lines = inventory.line_ids.filtered(
lambda line: line.discrepancy_percent > line.discrepancy_threshold lambda line: line._has_over_discrepancy()
) )
inventory.over_discrepancy_line_count = len(lines) inventory.over_discrepancy_line_count = len(lines)
@@ -57,9 +56,7 @@ class StockInventory(models.Model):
def _action_done(self): def _action_done(self):
for inventory in self: for inventory in self:
if inventory.over_discrepancy_line_count and inventory.line_ids.filtered( if inventory.over_discrepancy_line_count > 0.0:
lambda t: t.discrepancy_threshold > 0.0
):
if self.user_has_groups( if self.user_has_groups(
"stock_inventory_discrepancy.group_stock_inventory_validation" "stock_inventory_discrepancy.group_stock_inventory_validation"
) and not self.user_has_groups( ) and not self.user_has_groups(

View File

@@ -1,5 +1,4 @@
# Copyright 2017-2020 ForgeFlow S.L. # Copyright 2017-21 ForgeFlow S.L. (https://www.forgeflow.com)
# (http://www.forgeflow.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # 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
@@ -29,6 +28,7 @@ class StockInventoryLine(models.Model):
help="Maximum Discrepancy Rate Threshold", help="Maximum Discrepancy Rate Threshold",
compute="_compute_discrepancy_threshold", compute="_compute_discrepancy_threshold",
) )
has_over_discrepancy = fields.Boolean(compute="_compute_has_over_discrepancy",)
@api.depends("theoretical_qty", "product_qty") @api.depends("theoretical_qty", "product_qty")
def _compute_discrepancy(self): def _compute_discrepancy(self):
@@ -52,3 +52,10 @@ class StockInventoryLine(models.Model):
line.discrepancy_threshold = whs.discrepancy_threshold line.discrepancy_threshold = whs.discrepancy_threshold
else: else:
line.discrepancy_threshold = False line.discrepancy_threshold = False
def _compute_has_over_discrepancy(self):
for rec in self:
rec.has_over_discrepancy = rec._has_over_discrepancy()
def _has_over_discrepancy(self):
return self.discrepancy_percent > self.discrepancy_threshold > 0

View File

@@ -173,6 +173,7 @@ class TestInventoryDiscrepancy(TransactionCase):
) )
inventory.with_user(self.user).action_start() inventory.with_user(self.user).action_start()
inventory.with_user(self.user).action_validate() inventory.with_user(self.user).action_validate()
self.assertTrue(inventory.line_ids.has_over_discrepancy)
self.assertEqual( self.assertEqual(
inventory.over_discrepancy_line_count, inventory.over_discrepancy_line_count,
1, 1,

View File

@@ -59,11 +59,12 @@
<field name="discrepancy_qty" /> <field name="discrepancy_qty" />
<field name="discrepancy_percent" /> <field name="discrepancy_percent" />
<field name="discrepancy_threshold" /> <field name="discrepancy_threshold" />
<field name="has_over_discrepancy" invisible="1" />
</field> </field>
<xpath expr="//tree" position="attributes"> <xpath expr="//tree" position="attributes">
<attribute <attribute
name="decoration-danger" name="decoration-danger"
>theoretical_qty &lt; 0 or discrepancy_percent &gt; discrepancy_threshold or "product_qty != theoretical_qty"</attribute> >theoretical_qty &lt; 0 or has_over_discrepancy</attribute>
</xpath> </xpath>
</field> </field>
</record> </record>