Monday, 7 September 2015

Substring in javascript

Substring in Javascript:

The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

Syntax

str.substr(start[, length])
 
 
Frappe Example

frappe.ui.form.on("Quotation", "validate", function(frm) {
       var a = frm.doc.customer;
       a= a.substr(0,3);
        msgprint(a);
});

Above script returns first three letter  of customer name.


reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

Thursday, 20 August 2015

Comparing date in frappe or js

I want to check given date1 is Today or not
I used 
var date2 =get_today();
var result = (date1 == date2); 
It works successfully.


After that, I want to check, given date1 is Today or Yesterday or The day before yesterday. I used <= operator, but its not working properly.
After searching I come up with this solution, before comparing date we need to parse it.
Solution is:

var a = Date.parse(dataContext.ETD);
var b =  Date.parse(get_today());
var d = (a - b)/(1000 * 3600 * 24);
var c = ((d==0)||(d==(-1))||(d==(-2))); //returns true if ETD is today, yesterday or the day before yesterday
 
Please write comment if you have any doubt or suggestion. 

Ability to Color cells based on cell value Reports

Here is my code,
You can put this code in js file of report.
Code:


"formatter":function (row, cell, value, columnDef, dataContext, default_formatter) {
                    value = default_formatter(row, cell, value, columnDef, dataContext);
            if (columnDef.id == "Debit") {
                                   if(dataContext.Debit>1){
                                    value = "<span style='color:blue;font-weight:bold'>" + value + "</span>";
                                    }
                        msgprint(dataContext.Debit)

            }


            if (dataContext.Debit) {
            }

            return value;
        }
 
 
 

Wednesday, 12 August 2015

fetch address using frappe.call

frappe.ui.form.on("Documents Required", "buyer", function(frm, cdt, cdn) {
    var d = frappe.get_doc(cdt, cdn);
    msgprint("hi from ui");
        if(d.buyer) {
        return frm.call({
            method: "erpnext.utilities.doctype.address.address.get_address_display",
            child: d,
            args: {
                "address_dict":  d.buyer
            },
                        callback: function(r) {
            if(r.message)
                                    msgprint("hi from callback"+r.message);
                                frappe.model.set_value(cdt, cdn, "buyer_address", r.message);
        }
        });
     }
})

Script to filter address in child table and document

# Script to filter records in child table

cur_frm.set_query("buyer", "documents_required", function(doc) {
            if(doc.customer) return {filters: { 'address_type': 'Buyer', customer: doc.customer } };
        });


#script to filter records in document

cur_frm.set_query('buyer', function (doc) {
    if(doc.customer){
        return {
            filters: {
                'address_type': 'Buyer',
                'customer' : doc.customer
            }
        }
    }
    else {
        return {
        filters: {
                'address_type': 'Empty',
            }
        }
    }
});

Monday, 20 July 2015

remove words from string in Jinjha

This post help you to remove selected word from string.
This is also useful to replace word in string.

I have used this feature to remove Currency and float precision in ERPNext print format
e.g 1) to remove USD and 'only' form string
{{ doc.total_amount_in_words | replace("USD", "") | replace("only.", "") }}

e.g. 2) to remove float precision
{{ doc.get_formatted("net_total_export", doc) | replace(".00", "") }}<br>

e.g. 3) apply multiple formats
{{ '{:20,.0f}'.format(55.258) }}

Friday, 19 June 2015

How to write Client Side and Server Side Script in Frappe



Lets see, we have one requirement.
In Purchase Invoice, We need to add Payment Due Date. Payment Due Date is equal to Posting Date + Credit Days.
Also one more requirement, Supplier Invoice No should be unique in all Purchase Invoice.
Steps to achieve this. 1)  Create custom field ‘Credit Days’ and ‘Due Date’ in Purchase Invoice.
To fetch value of Credit Days from supplier into purchase invoice, write this script in Purchase Invoice.

    cur_frm.add_fetch(supplier ,'credit_days','credit_days');

2) To calculate Due Date = Posting Date + Credit Days, write below script in Purchase Invoice.



    frappe.ui.form.on("Purchase Invoice ", " validate ", function(frm) {
        var nos = frm.doc.credit_days* 1;
        var ddate = frappe.datetime.add_days(frm.doc.posting_date,nos);
        cur_frm.set_value("due_date", ddate);
    });



Note:
i) doc.field_name will give value of current doc.
ii) cur_frm.set_value("field_name", value) used to set value.

3) To add validation, Supplier Invoice No should be unique in all Purchase Invoice.
(This will explain how to call server side method using hooks.)

In hooks.py write this code

    doc_events = {
        "Purchase Invoice": {
            "validate": "sf_custom_changes.sf_acc.purchase.validate_bill_no"
        }
    }

In purchase.py write this code

    def validate_bill_no(self, method):
            if self.bill_no:
                # validate bill no is unique
                bill_list = frappe.db.sql("""select name from `tabPurchase Invoice` where bill_no=%s and docstatus =1""",
                    self.bill_no)
                if len(bill_list) > 0:
                    items = [e[0] for e in bill_list if e[0]!=self.name]
                    frappe.throw(_("Supplier Invoice Number must be unique. Current Supplier Invoice Number already exists for {0}").format(comma_and(items)))
            if self.bill_date:
                    if getdate(self.bill_date) > getdate(self.posting_date):
                        frappe.throw(_("Supplier Invoice Date cannot be after Purchase Order Date"))


This will help you to write client and server side script. Please ask if you have any doubt in this example.