Try ERPNext Buy Support Partners Foundation

Some customs scripts that I use

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));
};
9 Likes

@cpurbaugh thank you for the contribution dude…

@vjfalk can we add a develop git or repo for sharing scripts / print layouts / customizations under Erpnext ?
I think it would be much better and more informative for everyone…

You can create a Wiki for now on the ERPNext repo, and if it is getting too big, we can look into have a separate repository maybe.

Hi @cpurbaugh

This a very help full custom script since I still want to use / imidate the old logic combined with a custom workflow.

It works almost perfect:

I have to currently go to the sales order and press “Refresh” manually.
Do you know why it does not actualise itself automatically?

Many thanks!
Best
Thomas

Thomas,

Are you referring to the workflow state script? I no longer use this, as it was buggy.

Yes exactly.

What do you use instead now? I would be glad for some tipps for that problem!

We use reports extensively to track order status.