Problems calculating Due Date on Sales Invoice


Trying to setup custom due date for Sales Order as Credit Days are ignored when generating by default. So far I have:

function get_due_date(base, days){
    base = frappe.datetime.str_to_obj(base);
    days = parseInt(days, 10);
   var fmt = frappe.datetime.obj_to_str;    
    switch (parseInt(days)){
        case (0):
            return fmt(base);
       case (7):
       case (15):
            return fmt(frappe.datetime.add_days(base, days));
       case (30):
           base = new Date(base.getFullYear(), base.getMonth()+1, 0);
           return fmt(frappe.datetime.add_days(base, days));
function get_db_value(doctype, docname, field){
    var ret;{
        async: false,
        'method': 'frappe.client.get_value',
        'args': {
            'doctype': doctype,
            'filters': docname,
            'fieldname': field
       'callback': function(r){
           ret = r.message[field];
    return ret;

frappe.ui.form.on("Sales Invoice", "posting_date", function(frm) {
    var days = get_db_value('Customer', frm.doc.customer, 'credit_days');
    days = days*1;
    console.log([days, frm]);
    msgprint(get_due_date(frm.doc.posting_date, days));
    if (days && frm.doc.posting_date){
        frappe.model.set_value('Sales Invoice',, 'payment_due_date', get_due_date(frm.doc.posting_date, days));

This works for 7 and 15 days customers but for customers with value 30 it should calculate last day of NEXT month as due date. Currently it is just adding 30 days to the posting_date

Can anyone see why it’s not working and help me with the code?


1 Like