From 3c7e70ffb3159d49ad50fa64f96bdbddadca9734 Mon Sep 17 00:00:00 2001 From: bhupen Date: Wed, 30 Nov 2016 14:32:11 +0530 Subject: [PATCH] [Fix] Disallow same items to be fetched multiple times in sales invoice --- erpnext/public/js/utils.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js index 39e87f86f8..c80d418af0 100644 --- a/erpnext/public/js/utils.js +++ b/erpnext/public/js/utils.js @@ -143,8 +143,35 @@ erpnext.utils.map_current_doc = function(opts) { if(!cur_frm.doc.items[0].item_code) { cur_frm.doc.items = cur_frm.doc.items.splice(1); } + + // find the doctype of the items table + var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options; + + // find the link fieldname from items table for the given + // source_doctype + var link_fieldname = null; + frappe.get_meta(items_doctype).fields.forEach(function(d) { + if(d.options===opts.source_doctype) link_fieldname = d.fieldname; }); + + // search in existing items if the source_name is already set + var already_set = false; + + $.each(cur_frm.doc.items, function(i, d) { + if(d[link_fieldname]==opts.source_name) { + already_set = true; + return false; + } + }); + + if(already_set) { + frappe.msgprint(__("You have already selected items from {0} {1}", + [opts.source_doctype, opts.source_name])); + return; + } + } + return frappe.call({ // Sometimes we hit the limit for URL length of a GET request // as we send the full target_doc. Hence this is a POST request.