I thought for the benefit of the community that I would share some of the custom scripts that I use.
//Lock Sales Order for Approver Review
frappe.ui.form.on("Sales Order", "onload", function (frm) {
if (user_roles.indexOf("Approver" == 0) && frm.doc.workflow_state == "Pending Approval") {
msgprint("Not Authorized");
frm.disable_save();
}
});
//Reloads the page if in review stage
frappe.ui.form.on("Sales Order", "validate", function (frm) {
if (frm.doc.workflow_state === "Pending Approval") {
validated = true;
location.reload();
}
});
// I set this up to automatically update the workflow state after approval.
frappe.ui.form.on("Sales Order", "refresh", function (frm) {
//Variables for delivery and billing status
var NS = frm.doc.delivery_status === "Not Delivered" ;
var PS = frm.doc.delivery_status === "Partly Delivered" ;
var FS = frm.doc.delivery_status === "Fully Delivered";
var NB = frm.doc.billing_status === "Not Billed" ;
var PB = frm.doc.billing_status === "Partly Billed" ;
var FB = frm.doc.billing_status === "Fully Billed" ;
//Variables for naming workflows. Your workflow names may vary
var NBNS = "NOT SHIPPED | NOT INVOICED";
var NBPS = "PARTIALLY SHIPPED | NOT INVOICED";
var NBFS = "SHIPPED | NOT INVOICED";
var PBNS = "NOT SHIPPED | PARTIALLY INVOICED";
var PBPS = "PARTIALLY SHIPPED | PARTIALLY INVOICED";
var PBFS = "SHIPPED | PARTIALLY INVOICED";
var FBNS = "NOT SHIPPED | INVOICED";
var FBPS = "PARTIALLY SHIPPED | INVOICED";
var FBFS = "COMPLETE";
//following if function checks to see if the document is submitted
if (frm.doc.docstatus == 1) {
if (NB && NS) {
cur_frm.set_value("workflow_state", NBNS);
} else if (NB && PS) {
cur_frm.set_value("workflow_state", NBPS);
} else if (NB && FS) {
cur_frm.set_value("workflow_state", NBFS);
} else if (PB && NS) {
cur_frm.set_value("workflow_state", PBNS);
} else if (PB && PS) {
cur_frm.set_value("workflow_state", PBPS);
} else if (PB && FS) {
cur_frm.set_value("workflow_state", PBFS);
} else if (FB && NS) {
cur_frm.set_value("workflow_state", FBNS);
} else if (FB && PS) {
cur_frm.set_value("workflow_state", FBPS);
} else if (FB && FS) {
cur_frm.set_value("workflow_state", FBFS);
}
}
});
//Sets the Production Due Date when user changes Delivery Date
cur_frm.cscript.custom_delivery_date = function (doc) {
cur_frm.set_value("production_due_date", frappe.datetime.add_days(doc.delivery_date, doc.due_date_days_before * -1));
};
//Sets the Production Due Date when user changes Due Date Days Before
cur_frm.cscript.custom_due_date_days_before = function (doc) {
cur_frm.set_value("production_due_date", frappe.datetime.add_days(doc.delivery_date, doc.due_date_days_before * -1));
};