0

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));
    }
});
wahwahwah
  • 3,254
  • 1
  • 21
  • 40
MonoJoker
  • 1
  • 4
  • Any errors in console? The reason I ask is that it seems to work fine here http://jsfiddle.net/sav018c4/ (I had to mimic the frappe-functions) – Mackan May 20 '15 at 06:14
  • Only a generic `Uncaught TypeError: Cannot read property '0' of undefined` which is coming up on all transaction loads in frappe for now. Does not seem to affect anything and appears regardless of my scripts running. – MonoJoker May 20 '15 at 06:25
  • If I change the posting_date within the invoice form I get an error in console saying: `Uncaught ReferenceError: get_db_value is not defined` and the form unloads on me... So issue with syntax on var days = line? – MonoJoker May 20 '15 at 06:28
  • OK I've fixed that: `var days = cur_frm.cscript.get_db_value('Customer', frm.doc.customer, 'credit_days');` And now if I change the posting_date I get a msgprint showing: 2015-06-30. So my value is correct but it doesn't seem to make it into the field payment_due_date! – MonoJoker May 20 '15 at 06:32
  • You seem to debug this quite well now. Since the question is now "_How to use frappe.model.set_value to update my field?_" it's not a good fit for this topic. Either edit the question to reflect what you're asking (title, body and tags), or close this issue as solved/answered and ask a new question on the new topic. – Mackan May 20 '15 at 07:01

0 Answers0