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);
};

Tuesday 12 May 2015

Working with BITBUCKET


How to add repository to BITBUCKET?

For existing repository.(initialize, add all files, commit, push
1) git init
2) git remote add origin https://<username>@bitbucket.org/<repo-name>.git
3) git add .
4) git commit -m 'initial commit'
5) git push -u origin master

 For new repository. (create first file, commit, and push)
1) git init
2) git remote add origin https://<username>@bitbucket.org/<repo-name>.git
3) echo "Sambhaji" >>contributors.txt
4) git commit -m 'initial commit with contributors'
5) git push -u origin master

Thursday 7 May 2015

GL Report With running credit and running debit

python file:

def execute(filters=None):
    if not filters: filters = {}
   
    columns = get_columns()
    data = get_entries(filters)
   
    return columns, data
   
def get_columns():
    return [_("Journal Voucher") + ":Link/Journal Voucher:140", _("Account") + ":Link/Account:140",
        _("Posting Date") + ":Date:100", _("Against Account") + ":Link/Account:200",
        _("Debit") + ":Currency:120", _("Running Debit") + ":Currency:120",
                _("Credit") + ":Currency:120", _("Running Credit") + ":Currency:120",
                _("Posting Reference") + "::130", _("Reference") + "::100", _("Ref Date") + ":Date:110",
    ]

def get_conditions(filters):
    conditions = ""
    if not filters.get("account"):
        msgprint(_("Please select Bank Account"), raise_exception=1)
    else:
        conditions += " and jvd.account = %(account)s"
       
    if filters.get("from_date"): conditions += " and jv.posting_date>=%(from_date)s"
    if filters.get("to_date"): conditions += " and jv.posting_date<=%(to_date)s"
   
    return conditions
   
def get_entries(filters):
    conditions = get_conditions(filters)
    entries =  frappe.db.sql("""select jv.name, jvd.account, jv.posting_date,
        jvd.against_account, jvd.debit, (@rdeb:=@rdeb+(ifnull(jvd.debit, 0))) as RunningDebit,
                jvd.credit, (@rcredit:=@rcredit+(ifnull(jvd.credit, 0))) as RunningCredit,
                jvd.remark, jv.cheque_no, jv.cheque_date
        from `tabJournal Voucher Detail` jvd, `tabJournal Voucher` jv
        JOIN (select @rcredit := 0.0, @rdeb:=0.0) B
        where jvd.parent = jv.name and jv.docstatus=1 %s
        order by jv.name DESC
        """ % conditions, filters, as_list=1)
    return entries