Skip to content

Commit

Permalink
[IMP] ddmrp_warning: various improvements
Browse files Browse the repository at this point in the history
- Prevent Quantity multiple bigger than MOQ warning when MOQ is 0
- Unlink warning items when warning definition is deleted
- Unlink warning items when buffer is archived
  • Loading branch information
JordiMForgeFlow authored and LoisRForgeFlow committed Aug 8, 2024
1 parent 2b74850 commit df23844
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ddmrp_warning/data/ddmrp_warning_definition_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<field name="name">Quantity multiple bigger than MOQ</field>
<field
name="python_code"
>buffer.minimum_order_quantity &lt; buffer.qty_multiple</field>
>buffer.minimum_order_quantity &gt; 0 and buffer.minimum_order_quantity &lt; buffer.qty_multiple</field>
<field name="severity">2_mid</field>
</record>
<record
Expand Down
12 changes: 12 additions & 0 deletions ddmrp_warning/models/ddmrp_warning_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class DdmrpWarningDefinition(models.Model):
help="Domain based on Stock Buffer, to define if the "
"warning is applicable or not.",
)
ddmrp_warning_item_ids = fields.One2many(
comodel_name="ddmrp.warning.item",
inverse_name="warning_definition_id",
readonly=True,
)

def _eval_warning_domain(self, buffer, domain):
buffer_domain = [("id", "=", buffer.id)]
Expand Down Expand Up @@ -65,3 +70,10 @@ def evaluate_definition(self, buffer):
% ({"name": self._name, "error": error})
) from error
return res

def write(self, vals):
# Unlink warning items when definition is archived
res = super().write(vals)
if "active" in vals and not vals.get("active"):
self.ddmrp_warning_item_ids.unlink()
return res
1 change: 1 addition & 0 deletions ddmrp_warning/models/ddmrp_warning_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DdmrpWarningItem(models.Model):

warning_definition_id = fields.Many2one(
comodel_name="ddmrp.warning.definition",
ondelete="cascade",
)
buffer_id = fields.Many2one(comodel_name="stock.buffer", ondelete="cascade")
name = fields.Char(
Expand Down
7 changes: 7 additions & 0 deletions ddmrp_warning/models/stock_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,10 @@ def cron_generate_ddmrp_warnings(self, automatic=False):
if auto_commit:
self._cr.commit() # pylint: disable=E8102
return True

def write(self, vals):
# Unlink warning items when buffer is archived
res = super().write(vals)
if "active" in vals and not vals.get("active"):
self.ddmrp_warning_item_ids.unlink()
return res
14 changes: 14 additions & 0 deletions ddmrp_warning/tests/test_ddmrp_warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ def test_01_buffer_with_warnings(self):
self._refresh_involved_buffers()
new_count = len(self.buffer_warnings.ddmrp_warning_item_ids)
self.assertEqual(prev_count - new_count, 1)

def test_02_buffer_archive(self):
self._refresh_involved_buffers()
self.assertTrue(self.buffer_warnings.ddmrp_warning_item_ids)
self.buffer_warnings.active = False
self.assertFalse(self.buffer_warnings.ddmrp_warning_item_ids)

def test_03_definition_archive(self):
self._refresh_involved_buffers()
self.assertTrue(self.spike_warning.ddmrp_warning_item_ids)
self.assertTrue(self.buffer_warnings.ddmrp_warning_item_ids)
self.spike_warning.active = False
self.assertFalse(self.spike_warning.ddmrp_warning_item_ids)
self.assertFalse(self.buffer_warnings.ddmrp_warning_item_ids)

0 comments on commit df23844

Please sign in to comment.