Merge pull request #30686 from nabinhait/ignore-permlevel-for-fields
feat: Ignore permlevel for specific fields
diff --git a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js
index 035290d..5252798 100644
--- a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js
+++ b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js
@@ -140,26 +140,6 @@
}
}
- start_date(doc, cdt, cdn) {
- this.set_no_of_visits(doc, cdt, cdn);
- }
-
- end_date(doc, cdt, cdn) {
- this.set_no_of_visits(doc, cdt, cdn);
- }
-
- periodicity(doc, cdt, cdn) {
- this.set_no_of_visits(doc, cdt, cdn);
- }
-
- set_no_of_visits(doc, cdt, cdn) {
- var item = frappe.get_doc(cdt, cdn);
- let me = this;
- if (item.start_date && item.periodicity) {
- me.frm.call('validate_end_date_visits');
-
- }
- }
};
extend_cscript(cur_frm.cscript, new erpnext.maintenance.MaintenanceSchedule({frm: cur_frm}));
diff --git a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.py b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.py
index 9a23c07..04c080c 100644
--- a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.py
+++ b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.py
@@ -213,6 +213,26 @@
if chk:
throw(_("Maintenance Schedule {0} exists against {1}").format(chk[0][0], d.sales_order))
+ def validate_items_table_change(self):
+ doc_before_save = self.get_doc_before_save()
+ if not doc_before_save:
+ return
+ for prev_item, item in zip(doc_before_save.items, self.items):
+ fields = [
+ "item_code",
+ "start_date",
+ "end_date",
+ "periodicity",
+ "sales_person",
+ "no_of_visits",
+ "serial_no",
+ ]
+ for field in fields:
+ b_doc = prev_item.as_dict()
+ doc = item.as_dict()
+ if cstr(b_doc[field]) != cstr(doc[field]):
+ return True
+
def validate_no_of_visits(self):
return len(self.schedules) != sum(d.no_of_visits for d in self.items)
@@ -221,7 +241,7 @@
self.validate_maintenance_detail()
self.validate_dates_with_periodicity()
self.validate_sales_order()
- if not self.schedules or self.validate_no_of_visits():
+ if not self.schedules or self.validate_items_table_change() or self.validate_no_of_visits():
self.generate_schedule()
def on_update(self):
diff --git a/erpnext/maintenance/doctype/maintenance_schedule/test_maintenance_schedule.py b/erpnext/maintenance/doctype/maintenance_schedule/test_maintenance_schedule.py
index a98cd10..2268e0f 100644
--- a/erpnext/maintenance/doctype/maintenance_schedule/test_maintenance_schedule.py
+++ b/erpnext/maintenance/doctype/maintenance_schedule/test_maintenance_schedule.py
@@ -123,6 +123,36 @@
frappe.db.rollback()
+ def test_schedule_with_serials(self):
+ # Checks whether serials are automatically updated when changing in items table.
+ # Also checks if other fields trigger generate schdeule if changed in items table.
+ item_code = "_Test Serial Item"
+ make_serial_item_with_serial(item_code)
+ ms = make_maintenance_schedule(item_code=item_code, serial_no="TEST001, TEST002")
+ ms.save()
+
+ # Before Save
+ self.assertEqual(ms.schedules[0].serial_no, "TEST001, TEST002")
+ self.assertEqual(ms.schedules[0].sales_person, "Sales Team")
+ self.assertEqual(len(ms.schedules), 4)
+ self.assertFalse(ms.validate_items_table_change())
+ # After Save
+ ms.items[0].serial_no = "TEST001"
+ ms.items[0].sales_person = "_Test Sales Person"
+ ms.items[0].no_of_visits = 2
+ self.assertTrue(ms.validate_items_table_change())
+ ms.save()
+ self.assertEqual(ms.schedules[0].serial_no, "TEST001")
+ self.assertEqual(ms.schedules[0].sales_person, "_Test Sales Person")
+ self.assertEqual(len(ms.schedules), 2)
+ # When user manually deleted a row from schedules table.
+ ms.schedules.pop()
+ self.assertEqual(len(ms.schedules), 1)
+ ms.save()
+ self.assertEqual(len(ms.schedules), 2)
+
+ frappe.db.rollback()
+
def make_serial_item_with_serial(item_code):
serial_item_doc = create_item(item_code, is_stock_item=1)