Skip to content

Instantly share code, notes, and snippets.

@asbasawaraj
Last active January 17, 2020 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asbasawaraj/27f2a87e5fd78e01b1e0c2fa69be6d64 to your computer and use it in GitHub Desktop.
Save asbasawaraj/27f2a87e5fd78e01b1e0c2fa69be6d64 to your computer and use it in GitHub Desktop.
frappe.ui.form.on("Purchase Invoice", "refresh", function(frm) {
cur_frm.fields_dict['items'].grid.get_field('batch_no').new_doc = quick_entry_batch;
});
quick_entry_batch = function(doc, cdt, cdn){
console.log("Triggered");
quick_entry("Batch",
function(){},
{
"item":"testtes"
});
}
quick_entry = function(doctype, success, fields_map={}) {
frappe.model.with_doctype(doctype, function() {
var mandatory = [];
if (!fields_map == {}) {
$.each(fields_map, function(k,v) {
doc_field = frappe.meta.get_docfield(doctype, k)
mandatory.push(doc_field);
});
} else {
mandatory = $.map(frappe.get_meta(doctype).fields,
function(d) { return (d.reqd || d.bold && !d.read_only) ? d : null });
}
var meta = frappe.get_meta(doctype);
var doc = frappe.model.get_new_doc(doctype, null, null, true);
/*if(meta.quick_entry != 1) {
var d = frappe.model.make_new_doc_and_get_name(doctype);
d = locals[doctype][d];
$.each(fields_map, function(fieldname, fieldvalue) {
d[fieldname] = fieldvalue;
});
frappe.set_route('Form', doctype, d.name);
return;
}*/
var dialog = new frappe.ui.Dialog({
title: __("New {0}", [doctype]),
fields: mandatory,
});
var update_doc = function() {
var data = dialog.get_values(true);
$.each(data, function(key, value) {
if(key==='__name') {
dialog.doc.name = value;
} else {
if(!is_null(value)) {
dialog.doc[key] = value;
}
}
});
return dialog.doc;
}
var open_doc = function() {
dialog.hide();
update_doc();
frappe.set_route('Form', doctype, doc.name);
}
dialog.doc = doc;
// refresh dependencies etc
dialog.refresh();
dialog.set_primary_action(__('Save'), function() {
if(dialog.working) return;
var data = dialog.get_values();
if(data) {
dialog.working = true;
values = update_doc();
frappe.call({
method: "frappe.client.insert",
args: {
doc: values
},
callback: function(r) {
dialog.hide();
// delete the old doc
frappe.model.clear_doc(dialog.doc.doctype, dialog.doc.name);
var doc = r.message;
if(success) {
success(doc);
}
frappe.ui.form.update_calling_link(doc.name);
},
error: function() {
open_doc();
},
always: function() {
dialog.working = false;
},
freeze: true
});
}
});
var $link = $('<div class="text-muted small" style="padding-left: 10px; padding-top: 15px;">\
Ctrl+enter to save | <a class="edit-full">Edit in full page</a></div>').appendTo(dialog.body);
$link.find('.edit-full').on('click', function() {
// edit in form
open_doc();
});
// ctrl+enter to save
dialog.wrapper.keydown(function(e) {
if((e.ctrlKey || e.metaKey) && e.which==13) {
if(!frappe.request.ajax_count) {
// not already working -- double entry
dialog.get_primary_btn().trigger("click");
}
}
});
dialog.show();
//Set value and visibility if field map exists.
if (fields_map != {}) {
$.each(dialog.fields_dict, function(fieldname, field) {
field.set_input(fields_map[fieldname]);
});
} else {
// set defaults
$.each(dialog.fields_dict, function(fieldname, field) {
field.doctype = doc.doctype;
field.docname = doc.name;
if(!is_null(doc[fieldname])) {
field.set_input(doc[fieldname]);
}
});
}
});
}
@satishmalkar94
Copy link

satishmalkar94 commented Jan 17, 2020

The above script work for me but it will not filter the selected item int the purchase receipt..while creating new batch..
how i filter the selected item ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment