I have a function in frappe which I use to calculate the invoice due date. I have 4 types of customer credit terms 0, 7, 14 days from posting_date
and 30 days from last date of invoice month — i.e. if invoice is raised on any date in May payment_due_date
would be 30/06/15
.
So I have the below function get_due_date
. All customer payment_due_dates
for 0, 7, 14 day customers works fine but 30 days still won't. It simply adds 30 days to the posting_date
.
I am new to Javascript so I borrowed the code in the frappe.ui.form.on
section and I suspect the very last line is "overriding" my get_due_date
function incorrectly but I don't know the syntax to do it properly.
Can someone help please?
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()+2, 0);
return fmt(base)
}
}
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', frm.doc.name, 'payment_due_date', get_due_date(frm.doc.posting_date, days));
}
});