Skip to content

Commit

Permalink
refactor: update_serial_no function for old docs
Browse files Browse the repository at this point in the history
  • Loading branch information
noahjacob committed Dec 13, 2021
1 parent ef53eb7 commit 4cc9c8e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

import json

import frappe
from frappe import _, throw
Expand Down Expand Up @@ -306,13 +307,27 @@ def get_pending_data(self, data_type, s_date=None, item_name=None):
return schedule.name

@frappe.whitelist()
def update_serial_nos(s_id):
serial_nos = frappe.db.get_value('Maintenance Schedule Detail', s_id, 'serial_no')
def update_serial_nos(s_id, item):
if isinstance(item, str):
item = json.loads(item)
item = frappe._dict(item)

serial_nos = []
if s_id:
serial_nos = frappe.db.get_value('Maintenance Schedule Detail', s_id, 'serial_no')

# for old docs which were already saved.
elif item.get('prevdoc_docname'):
serial_nos = frappe.db.get_value('Maintenance Schedule Item', {
'parent': item.prevdoc_docname,
'item_code': item.item_code
}, 'serial_no')

if serial_nos:
serial_nos = get_serial_nos(serial_nos)
return serial_nos
else:
return False

return []

@frappe.whitelist()
def make_maintenance_visit(source_name, target_doc=None, item_name=None, s_id=None):
Expand Down
51 changes: 23 additions & 28 deletions erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,41 @@
// License: GNU General Public License v3. See license.txt

frappe.provide("erpnext.maintenance");
var serial_nos = [];
frappe.ui.form.on('Maintenance Visit', {
refresh: function (frm) {
//filters for serial_no based on item_code
frm.set_query('serial_no', 'purposes', function (frm, cdt, cdn) {
let item = locals[cdt][cdn];
if (serial_nos) {
return {
filters: {
'item_code': item.item_code,
'name': ["in", serial_nos]
}
};
} else {
return {
filters: {
'item_code': item.item_code
}
};
}
});
},
setup: function (frm) {
frm.set_query('contact_person', erpnext.queries.contact_query);
frm.set_query('customer_address', erpnext.queries.address_query);
frm.set_query('customer', erpnext.queries.customer);
},
onload: function (frm, cdt, cdn) {
let item = locals[cdt][cdn];
// filters for serial no based on item code
if (frm.doc.maintenance_type === "Scheduled") {
const schedule_id = item.purposes[0].prevdoc_detail_docname || frm.doc.maintenance_schedule_detail;
const schedule_id = frm.doc.maintenance_schedule_detail;
let item = frm.doc.purposes[0];
frappe.call({
method: "erpnext.maintenance.doctype.maintenance_schedule.maintenance_schedule.update_serial_nos",
args: {
s_id: schedule_id
},
callback: function (r) {
serial_nos = r.message;
s_id: schedule_id,
item: item
}
}).then((r) => {
serial_nos = r.message;
frm.set_query('serial_no', 'purposes', function () {
if (serial_nos.length > 0) {
return {
filters: {
'item_code': item.item_code,
'name': ["in", serial_nos]
}
};
}
return {
filters: {
'item_code': item.item_code
}
};
});

});
}
else {
Expand All @@ -63,7 +59,6 @@ frappe.ui.form.on('Maintenance Visit', {
contact_person: function (frm) {
erpnext.utils.get_contact_details(frm);
}

})

// TODO commonify this code
Expand Down

0 comments on commit 4cc9c8e

Please sign in to comment.