From 143ea718c3c6b37ad3d0fc69d8bb1886dfbe9e23 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 7 Jan 2013 17:03:11 +0530 Subject: [PATCH] commified item_code query --- .../purchase_invoice/purchase_invoice.js | 4 +- .../purchase_common/purchase_common.js | 8 ++- manufacturing/doctype/bom/bom.js | 7 +-- .../production_planning_tool.js | 9 +-- public/js/utils.js | 56 ++++++++++++++----- selling/doctype/opportunity/opportunity.js | 8 ++- selling/doctype/sales_common/sales_common.js | 24 +++----- stock/doctype/packing_slip/packing_slip.js | 2 +- stock/doctype/serial_no/serial_no.js | 10 +--- .../stock_uom_replace_utility.js | 2 +- 10 files changed, 78 insertions(+), 52 deletions(-) diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.js b/accounts/doctype/purchase_invoice/purchase_invoice.js index 1ab1bce9a6..bb56bd1a22 100644 --- a/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -193,7 +193,9 @@ cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) { } cur_frm.fields_dict['entries'].grid.get_field("item_code").get_query = function(doc, cdt, cdn) { - return 'SELECT tabItem.name, tabItem.description FROM tabItem WHERE tabItem.is_purchase_item="Yes" AND (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND tabItem.docstatus != 2 AND tabItem.%(key)s LIKE "%s" LIMIT 50' + return return erpnext.queries.item({ + 'ifnull(tabItem.is_purchase_item, "No")': 'Yes' + }) } cur_frm.fields_dict['credit_to'].get_query = function(doc) { diff --git a/buying/doctype/purchase_common/purchase_common.js b/buying/doctype/purchase_common/purchase_common.js index 519905a010..fa7c91f217 100644 --- a/buying/doctype/purchase_common/purchase_common.js +++ b/buying/doctype/purchase_common/purchase_common.js @@ -167,9 +167,13 @@ cur_frm.cscript.conversion_rate = function(doc,cdt,cdn) { // Only Is Purchase Item = 'Yes' and Items not moved to trash are allowed. cur_frm.fields_dict[fname].grid.get_field("item_code").get_query = function(doc, cdt, cdn) { if (doc.is_subcontracted =="Yes") { - return 'SELECT tabItem.name, tabItem.description FROM tabItem WHERE ifnull(tabItem.is_sub_contracted_item, "No")="Yes" AND (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND tabItem.docstatus != 2 AND tabItem.%(key)s LIKE "%s" LIMIT 50' + return erpnext.queries.item({ + 'ifnull(tabItem.is_sub_contracted_item, "No")': 'Yes' + }) } else { - return 'SELECT tabItem.name, tabItem.description FROM tabItem WHERE ifnull(tabItem.is_purchase_item, "No")="Yes" AND (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND tabItem.docstatus != 2 AND tabItem.%(key)s LIKE "%s" LIMIT 50' + return erpnext.queries.item({ + 'ifnull(tabItem.is_purchase_item, "No")': 'Yes' + }) } } diff --git a/manufacturing/doctype/bom/bom.js b/manufacturing/doctype/bom/bom.js index 179e63fd9e..f2746dea6b 100644 --- a/manufacturing/doctype/bom/bom.js +++ b/manufacturing/doctype/bom/bom.js @@ -174,10 +174,9 @@ var calculate_total = function(doc) { cur_frm.fields_dict['item'].get_query = function(doc) { - return 'SELECT DISTINCT `tabItem`.`name`, `tabItem`.description FROM `tabItem` \ - WHERE is_manufactured_item = "Yes" and (IFNULL(`tabItem`.`end_of_life`,"") = "" OR \ - `tabItem`.`end_of_life` = "0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND \ - `tabItem`.`%(key)s` like "%s" ORDER BY `tabItem`.`name` LIMIT 50'; + return return erpnext.queries.item({ + 'ifnull(tabItem.is_manufactured_item, "No")': 'Yes' + }) } cur_frm.fields_dict['project_name'].get_query = function(doc, dt, dn) { diff --git a/manufacturing/doctype/production_planning_tool/production_planning_tool.js b/manufacturing/doctype/production_planning_tool/production_planning_tool.js index 5d9a936268..95b84b2078 100644 --- a/manufacturing/doctype/production_planning_tool/production_planning_tool.js +++ b/manufacturing/doctype/production_planning_tool/production_planning_tool.js @@ -45,12 +45,9 @@ cur_frm.cscript.download_materials_required = function(doc, cdt, cdn) { } cur_frm.fields_dict['pp_details'].grid.get_field('item_code').get_query = function(doc) { - return 'SELECT DISTINCT `tabItem`.`name`,`tabItem`.`item_name` \ - FROM `tabItem` WHERE `tabItem`.is_pro_applicable = "Yes" \ - AND (IFNULL(`tabItem`.`end_of_life`,"") = "" \ - OR `tabItem`.`end_of_life`="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) \ - AND tabItem.%(key)s like "%s" \ - ORDER BY `tabItem`.`name` LIMIT 50'; + return erpnext.queries.item({ + 'ifnull(tabItem.is_pro_applicable, "No")': 'Yes' + }); } cur_frm.fields_dict['pp_details'].grid.get_field('bom_no').get_query = function(doc) { diff --git a/public/js/utils.js b/public/js/utils.js index 3eaad088ea..aa65d1ae38 100644 --- a/public/js/utils.js +++ b/public/js/utils.js @@ -114,16 +114,33 @@ erpnext.utils.supplier_query = function() { wn.provide("erpnext.queries"); +erpnext.queries.get_conditions = function(doctype, opts) { + conditions = []; + if (opts) { + $.each(opts, function(key, val) { + var lhs = "`tab" + doctype + "`.`" + key + "`"; + + if(key.indexOf(doctype)!=-1) { + // with function + lhs = key; + } + + if (esc_quotes(val).charAt(0) != "!") + conditions.push(lhs + "='"+esc_quotes(val)+"'"); + else + conditions.push(lhs + "!='"+esc_quotes(val).substr(1)+"'"); + }); + } + return conditions; +} + erpnext.queries.account = function(opts) { if(!opts) opts = {}; if(!opts.group_or_ledger) opts.group_or_ledger = "Ledger"; - conditions = []; - $.each(opts, function(key, val) { - conditions.push("tabAccount.`" + key + "`='"+esc_quotes(val)+"'"); - }); + var conditions = erpnext.queries.get_conditions("Account", opts); return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \ FROM tabAccount \ @@ -131,18 +148,29 @@ erpnext.queries.account = function(opts) { AND tabAccount.%(key)s LIKE "%s" ' + (conditions ? (" AND " + conditions.join(" AND ")) : "") + + " LIMIT 50" +} + +erpnext.queries.item = function(opts) { + var conditions = erpnext.queries.get_conditions("Item", opts); + + return 'SELECT tabItem.name, \ + if(length(tabItem.item_name) > 40, \ + concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \ + if(length(tabItem.description) > 40, \ + concat(substr(tabItem.description, 1, 40), "..."), description) as decription \ + FROM tabItem \ + WHERE tabItem.docstatus!=2 \ + AND (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00") \ + OR `tabItem`.`end_of_life` > NOW()) \ + AND tabItem.%(key)s LIKE "%s" ' + (conditions + ? (" AND " + conditions.join(" AND ")) + : "") + + " LIMIT 50" } erpnext.queries.bom = function(opts) { - conditions = []; - if (opts) { - $.each(opts, function(key, val) { - if (esc_quotes(val).charAt(0) != "!") - conditions.push("tabBOM.`" + key + "`='"+esc_quotes(val)+"'"); - else - conditions.push("tabBOM.`" + key + "`!='"+esc_quotes(val).substr(1)+"'"); - }); - } + var conditions = erpnext.queries.get_conditions("BOM", opts); return 'SELECT tabBOM.name, tabBOM.item \ FROM tabBOM \ @@ -151,4 +179,6 @@ erpnext.queries.bom = function(opts) { AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length ? (" AND " + conditions.join(" AND ")) : "") + + " LIMIT 50" + } \ No newline at end of file diff --git a/selling/doctype/opportunity/opportunity.js b/selling/doctype/opportunity/opportunity.js index cfc546c956..d5c665a70c 100644 --- a/selling/doctype/opportunity/opportunity.js +++ b/selling/doctype/opportunity/opportunity.js @@ -149,9 +149,13 @@ cur_frm.cscript.lead = function(doc, cdt, cdn) { //======================================= cur_frm.fields_dict['enquiry_details'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) { if (doc.enquiry_type == 'Maintenance') - return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem WHERE tabItem.is_service_item="Yes" AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") AND tabItem.%(key)s LIKE "%s" LIMIT 50'; + return erpnext.queries.item({ + 'ifnull(tabItem.is_service_item, "No")': 'Yes' + }); else - return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem WHERE tabItem.is_sales_item="Yes" AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") AND tabItem.%(key)s LIKE "%s" LIMIT 50'; + return erpnext.queries.item({ + 'ifnull(tabItem.is_sales_item, "No")': 'Yes' + }); } // Create New Quotation diff --git a/selling/doctype/sales_common/sales_common.js b/selling/doctype/sales_common/sales_common.js index de56a6c113..c1435bd3b3 100644 --- a/selling/doctype/sales_common/sales_common.js +++ b/selling/doctype/sales_common/sales_common.js @@ -249,21 +249,15 @@ cur_frm.cscript.price_list_name = function(doc, cdt, cdn) { // ******************** ITEM CODE ******************************** cur_frm.fields_dict[cur_frm.cscript.fname].grid.get_field("item_code").get_query = function(doc, cdt, cdn) { - if (inList(['Maintenance', 'Service'], doc.order_type)) - return 'SELECT tabItem.name,tabItem.item_name,tabItem.description \ - FROM tabItem WHERE tabItem.is_service_item="Yes" \ - AND tabItem.docstatus != 2 \ - AND (ifnull(`tabItem`.`end_of_life`,"") = "" \ - OR `tabItem`.`end_of_life` > NOW() \ - OR `tabItem`.`end_of_life`="0000-00-00") \ - AND tabItem.%(key)s LIKE "%s" LIMIT 50'; - else - return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem \ - WHERE tabItem.is_sales_item="Yes" AND tabItem.docstatus != 2 \ - AND (ifnull(`tabItem`.`end_of_life`,"") = "" \ - OR `tabItem`.`end_of_life` > NOW() \ - OR `tabItem`.`end_of_life`="0000-00-00") \ - AND tabItem.%(key)s LIKE "%s" LIMIT 50'; + if (inList(['Maintenance', 'Service'], doc.order_type)) { + return erpnext.queries.item({ + 'ifnull(tabItem.is_service_item, "No")': 'Yes' + }); + } else { + return erpnext.queries.item({ + 'ifnull(tabItem.is_sales_item, "No")': 'Yes' + }); + } } diff --git a/stock/doctype/packing_slip/packing_slip.js b/stock/doctype/packing_slip/packing_slip.js index 74f800deaa..a332ca8345 100644 --- a/stock/doctype/packing_slip/packing_slip.js +++ b/stock/doctype/packing_slip/packing_slip.js @@ -20,7 +20,7 @@ cur_frm.fields_dict['delivery_note'].get_query = function(doc, cdt, cdn) { cur_frm.fields_dict['item_details'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) { - var query = 'SELECT name, description FROM `tabItem` WHERE name IN ( \ + var query = 'SELECT name, item_name, description FROM `tabItem` WHERE name IN ( \ SELECT item_code FROM `tabDelivery Note Item` dnd \ WHERE parent="' + doc.delivery_note + '" AND IFNULL(qty, 0) > IFNULL(packed_qty, 0)) AND %(key)s LIKE "%s" LIMIT 50'; return query; diff --git a/stock/doctype/serial_no/serial_no.js b/stock/doctype/serial_no/serial_no.js index 49e88e2826..dad50d3ac9 100644 --- a/stock/doctype/serial_no/serial_no.js +++ b/stock/doctype/serial_no/serial_no.js @@ -14,14 +14,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -// ************************************** onload **************************************************** cur_frm.cscript.onload = function(doc, cdt, cdn) { if(!doc.status) set_multiple(cdt, cdn, {status:'In Store'}); if(doc.__islocal) hide_field(['supplier_name','address_display']) } -// ************************************** refresh *************************************************** cur_frm.cscript.refresh = function(doc, cdt, cdn) { if(!doc.__islocal) { flds = ['item_code', 'warehouse', 'purchase_document_type', 'purchase_document_no', 'purchase_date', 'purchase_time', 'purchase_rate', 'supplier'] @@ -31,7 +29,6 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) { } -// ************************************** triggers ************************************************** // item details // ------------- @@ -63,10 +60,9 @@ cur_frm.cscript.supplier = function(doc,dt,dn) { //item code //---------- cur_frm.fields_dict['item_code'].get_query = function(doc,cdt,cdn) { - return 'SELECT `tabItem`.`name`,`tabItem`.`description` FROM `tabItem` \ - WHERE `tabItem`.`docstatus`!= 2 AND ifnull(`tabItem`.`has_serial_no`, "No") = "Yes" \ - AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") \ - AND `tabItem`.%(key)s LIKE "%s" ORDER BY `tabItem`.`name` ASC LIMIT 50'; + return erpnext.queries.item({ + 'ifnull(tabItem.has_serial_no, "No")': 'Yes' + }); } cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query; diff --git a/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js b/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js index b71f94b385..cb883118c1 100644 --- a/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js +++ b/stock/doctype/stock_uom_replace_utility/stock_uom_replace_utility.js @@ -15,7 +15,7 @@ // along with this program. If not, see . cur_frm.fields_dict['item_code'].get_query = function(doc) { - return 'SELECT DISTINCT `tabItem`.`name`, `tabItem`.description FROM `tabItem` WHERE (IFNULL(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` ="0000-00-00" OR `tabItem`.`end_of_life` > NOW()) AND `tabItem`.`%(key)s` like "%s" ORDER BY `tabItem`.`name` LIMIT 50'; + return erpnext.queries.item(); } //==================== Get Items Stock UOM =====================================================