Friday, 22 May 2015

Update all child record using for loop

script example:


frappe.ui.form.on("Sales Invoice", "onload", function(frm) {
   for(i=0;i<frm.doc.entries.length;i++){
        frm.doc.entries[i].batch_copy = frm.doc.entries[i].batch_no;
    }
});

Monday, 18 May 2015

javascript inheritance, frappe dialog email customization


frappe.views.CommunicationComposer =  frappe.views.CommunicationComposer.extend({
    make_fun: function(){
         show_alert('Hi');
    },
    make: function(){
        this._super();
        this.make_fun();
    }
})


http://ejohn.org/blog/simple-javascript-inheritance/


make is a convencion in frappe for the method that create the HTML widget


if you override the complete class by extending by the basic Class and not by CommunicationComposer the email_box cant show, because you destroyied the original class

Override example:
frappe.views.CommunicationComposer = Class.extend({
    my_fun : function() {
         show_alert('Hi');

    },

});


reference: http://ejohn.org/blog/simple-javascript-inheritance/

Thanks: @MaxMorais 

Thursday, 14 May 2015

Interaction with refresh comment and custom field

frappe.ui.form.on("Sales Order", "refresh", function(frm, doctype, name) {
cur_frm.add_custom_button(__('Add Interaction'), function() {
var d = frappe.prompt([
    {'fieldname': 'responce_reson', 'fieldtype': 'Data', 'label': 'Responce Reson', 'reqd': 1},
    {'fieldname': 'next_date', 'fieldtype': 'Date', 'label': 'Next Contact Date', 'reqd': 1},
    {'fieldname': 'comment', 'fieldtype': 'Text', 'label': 'Comment', 'reqd': 1}
],
function(values){
    var c = d.get_values()
    var cmnt = c.comment
    var me = frm.doc
    return frappe.call({
            method: "frappe.desk.form.utils.add_comment",
            args: {
                doc:{
                    doctype: "Comment",
                    comment_type: "Comment",
                    comment_doctype: frm.doc.doctype,
                    comment_docname: frm.doc.name,
                    comment: cmnt,
                                        responce_reson: c.responce_reson,
                                        next_date: c.next_date,
                    comment_by: user
                }
            },
            callback: function(r) {
            if (cur_frm) {
                        if (cur_frm.docname && (frappe.last_edited_communication[cur_frm.doctype] || {})[cur_frm.docname]) {
                            delete frappe.last_edited_communication[cur_frm.doctype][cur_frm.docname];
                        }
                        // clear input
                        cur_frm.comments.input.val("");
                        cur_frm.reload_doc();
                    }
            }
        });
},
'Add Interaction',
'Submit'
)
}, "icon-exclamation", "btn-default");

                        frm.comments.refresh();

});

Interaction with refresh comment and custom field

frappe.ui.form.on("Sales Order", "refresh", function(frm, doctype, name) {
cur_frm.add_custom_button(__('Add Interaction'), function() {
var d = frappe.prompt([
    {'fieldname': 'responce_reson', 'fieldtype': 'Data', 'label': 'Responce Reson', 'reqd': 1},
    {'fieldname': 'next_date', 'fieldtype': 'Date', 'label': 'Next Contact Date', 'reqd': 1},
    {'fieldname': 'comment', 'fieldtype': 'Text', 'label': 'Comment', 'reqd': 1}
],
function(values){
    var c = d.get_values()
    var cmnt = c.comment
    var me = frm.doc
    return frappe.call({
            method: "frappe.desk.form.utils.add_comment",
            args: {
                doc:{
                    doctype: "Comment",
                    comment_type: "Comment",
                    comment_doctype: frm.doc.doctype,
                    comment_docname: frm.doc.name,
                    comment: cmnt,
                                        responce_reson: c.responce_reson,
                                        next_date: c.next_date,
                    comment_by: user
                }
            },
            callback: function(r) {
            if (cur_frm) {
                        if (cur_frm.docname && (frappe.last_edited_communication[cur_frm.doctype] || {})[cur_frm.docname]) {
                            delete frappe.last_edited_communication[cur_frm.doctype][cur_frm.docname];
                        }
                        // clear input
                        cur_frm.comments.input.val("");
                        cur_frm.reload_doc();
                    }
            }
        });
},
'Add Interaction',
'Submit'
)
}, "icon-exclamation", "btn-default");

                        frm.comments.refresh();

});

Interaction to be updated

frappe.ui.form.on("Sales Order", "generate_document", function(frm, doctype, name) {
    frappe.model.open_mapped_doc({
            method: "ngse.ngse_doc.ef_doc.apply_comment",
            frm: cur_frm
        });
});

frappe.ui.form.on("Sales Order", "refresh", function(frm, doctype, name) {
cur_frm.add_custom_button(__('Generate Document'), function() {
    msgprint(frm.doc.owner)
    msgprint(frm.doc.doctype)
var d = frappe.prompt([
    {'fieldname': 'birth', 'fieldtype': 'Date', 'label': 'Birth Date', 'reqd': 0},
    {'fieldname': 'comment', 'fieldtype': 'Text', 'label': 'Comment', 'reqd': 1}
],
function(values){
    var c = d.get_values()
    var cmnt = c.comment
    var me = frm.doc
    return frappe.call({
            method: "frappe.desk.form.utils.add_comment",
            args: {
                doc:{
                    doctype: "Comment",
                    comment_type: "Comment",
                    comment_doctype: frm.doc.doctype,
                    comment_docname: frm.doc.name,
                    comment: cmnt,
                    comment_by: user
                }
            },
            callback: function(r) {
                if(!r.exc) {
                    frm.get_docinfo().comments =
                        me.get_comments().concat([r.message]);
                    input.val("");
                    refresh(true);
                }
            }
        });
},
'Age verification',
'Subscribe me'
)
}, "icon-exclamation", "btn-default");
});

frappe.ui.form.on("Sales Order", "press_me", function(frm, doctype, name) {
    msgprint(frm.doc.owner)
    msgprint(frm.doc.doctype)
var d = frappe.prompt([
    {'fieldname': 'birth', 'fieldtype': 'Date', 'label': 'Birth Date', 'reqd': 0},
    {'fieldname': 'comment', 'fieldtype': 'Text', 'label': 'Comment', 'reqd': 1}
],
function(values){
    var c = d.get_values()
    var cmnt = c.comment
    var me = frm.doc
    return frappe.call({
            method: "frappe.desk.form.utils.add_comment",
            args: {
                doc:{
                    doctype: "Comment",
                    comment_type: "Comment",
                    comment_doctype: frm.doc.doctype,
                    comment_docname: frm.doc.name,
                    comment: cmnt,
                    comment_by: user
                }
            },
            callback: function(r) {
                if(!r.exc) {
                    frm.get_docinfo().comments =
                        me.get_comments().concat([r.message]);
                    input.val("");
                    refresh(true);
                }
            }
        });
},
'Age verification',
'Subscribe me'
)
});

working with comment and prompt box



frappe.ui.form.on("Sales Order", "press_me", function(frm, doctype, name) {
var d = frappe.prompt([
    {'fieldname': 'birth', 'fieldtype': 'Date', 'label': 'Birth Date', 'reqd': 0},
    {'fieldname': 'text', 'fieldtype': 'Text', 'label': 'Comment', 'reqd': 1}
],
function(values){
    var c = d.get_values()
    msgprint(frm.doc.customer)
    msgprint(frm.doc.doctype)
    frappe.model.open_mapped_doc({
            "method": "ngse.ngse_doc.ef_doc.apply_prompt_comment",
                        "args:": {
                                 "source_name": cur_frm,
                                 "dtype": frm.doc.doctype,
                         },
        });
},
'Age verification',
'Subscribe me'
)
});



@frappe.whitelist(allow_guest=True)
def apply_comment(source_name, target_doc=None):
        comment = frappe.new_doc("Comment")
        comment.comment = "test two"
        comment.comment_doctype = "Sales Order"
        comment.comment_docname = "SO-00110"
        comment.save()
        msgprint("after save")
        cur_doc = frappe.get_doc("Sales Order", source_name)

@frappe.whitelist(allow_guest=True)
def apply_prompt_comment(source_name, dtype, target_doc=None):
        comment = frappe.new_doc("Comment")
        comment.comment = dtype
        comment.comment_doctype = "Sales Order"
        comment.comment_docname = "SO-00110"
        comment.save()
        msgprint("Interaction Submited")
        cur_doc = frappe.get_doc("Sales Order", source_name)

Custom Script to Manupulate Child Table Data e.g Purchase Order Item

When we create Purchase Order from Material Request and Purchase UOM is not same as stock uom then we need to reset Purchase order qty.


E.g.1) always change qty = qty/conversion factor
frappe.ui.form.on('Purchase Order', 'validate', function(frm){
    for (var idx in frm.doc.items){
        if (frm.doc.items[idx].stock_uom != frm.doc.items[idx].uom){
            frm.doc.items[idx].qty = frm.doc.items[idx].qty/frm.doc.items[idx].conversion_factor
        }
    }
    });




E.g. 2) change only when PO created from Material Request

frappe.ui.form.on('Purchase Order', 'validate', function(frm){
    if(frm.doc.items[0].prevdoc_docname){
    for (var idx in frm.doc.items){
        if (frm.doc.items[idx].stock_uom != frm.doc.items[idx].uom){
            frm.doc.items[idx].qty = frm.doc.items[idx].qty/frm.doc.items[idx].conversion_factor
        }
    }

    }
    });


Note:
Here, for loop is used to execute loop and change all item values ``for (var idx in frm.doc.items)``



Previously we are trying this for UOM conversion

cur_frm.cscript.rate_per_weight_unit = function(doc, cdt, cdn) {
    var item = frappe.get_doc(cdt, cdn);
    var rate = item.rate_per_weight_unit * item.quantity_in_weight / item.qty;
    frappe.model.set_value(cdt, cdn, "rate", rate);
};