Rushabh Mehta | 5e7ec20 | 2013-01-11 11:15:27 +0530 | [diff] [blame] | 1 | // ERPNext - web based ERP (http://erpnext.com) |
| 2 | // Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | // searches for enabled profiles |
| 18 | erpnext.utils.profile_query = function() { |
| 19 | return "select name, concat_ws(' ', first_name, middle_name, last_name) \ |
| 20 | from `tabProfile` where ifnull(enabled, 0)=1 and docstatus < 2 and \ |
| 21 | name not in ('Administrator', 'Guest') and (%(key)s like \"%s\" or \ |
| 22 | concat_ws(' ', first_name, middle_name, last_name) like \"%%%s\") \ |
| 23 | order by \ |
| 24 | case when name like \"%s%%\" then 0 else 1 end, \ |
| 25 | case when concat_ws(' ', first_name, middle_name, last_name) like \"%s%%\" \ |
| 26 | then 0 else 1 end, \ |
| 27 | name asc limit 50"; |
| 28 | }; |
| 29 | |
| 30 | // searches for active employees |
| 31 | erpnext.utils.employee_query = function() { |
| 32 | return "select name, employee_name from `tabEmployee` \ |
| 33 | where status = 'Active' and docstatus < 2 and \ |
| 34 | (%(key)s like \"%s\" or employee_name like \"%%%s\") \ |
| 35 | order by \ |
| 36 | case when name like \"%s%%\" then 0 else 1 end, \ |
| 37 | case when employee_name like \"%s%%\" then 0 else 1 end, \ |
| 38 | name limit 50"; |
| 39 | }; |
| 40 | |
| 41 | // searches for leads which are not converted |
| 42 | erpnext.utils.lead_query = function() { |
| 43 | return "select name, lead_name, company_name from `tabLead` \ |
| 44 | where docstatus < 2 and ifnull(status, '') != 'Converted' and \ |
| 45 | (%(key)s like \"%s\" or lead_name like \"%%%s\" or company_name like \"%%%s\") \ |
| 46 | order by \ |
| 47 | case when name like \"%s%%\" then 0 else 1 end, \ |
| 48 | case when lead_name like \"%s%%\" then 0 else 1 end, \ |
| 49 | case when company_name like \"%s%%\" then 0 else 1 end, \ |
| 50 | lead_name asc limit 50"; |
| 51 | }; |
| 52 | |
| 53 | // searches for customer |
| 54 | erpnext.utils.customer_query = function() { |
| 55 | if(sys_defaults.cust_master_name == "Customer Name") { |
Rushabh Mehta | db7139a | 2013-01-17 18:22:22 +0530 | [diff] [blame] | 56 | var fields = ["name", "customer_group", "territory"]; |
Rushabh Mehta | 5e7ec20 | 2013-01-11 11:15:27 +0530 | [diff] [blame] | 57 | } else { |
Rushabh Mehta | db7139a | 2013-01-17 18:22:22 +0530 | [diff] [blame] | 58 | var fields = ["name", "customer_name", "customer_group", "territory"]; |
Rushabh Mehta | 5e7ec20 | 2013-01-11 11:15:27 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return "select " + fields.join(", ") + " from `tabCustomer` where docstatus < 2 and \ |
| 62 | (%(key)s like \"%s\" or customer_name like \"%%%s\") \ |
| 63 | order by \ |
| 64 | case when name like \"%s%%\" then 0 else 1 end, \ |
| 65 | case when customer_name like \"%s%%\" then 0 else 1 end, \ |
| 66 | name, customer_name limit 50"; |
| 67 | }; |
| 68 | |
| 69 | // searches for supplier |
| 70 | erpnext.utils.supplier_query = function() { |
| 71 | if(sys_defaults.supp_master_name == "Supplier Name") { |
| 72 | var fields = ["name", "supplier_type"]; |
| 73 | } else { |
| 74 | var fields = ["name", "supplier_name", "supplier_type"]; |
| 75 | } |
| 76 | |
| 77 | return "select " + fields.join(", ") + " from `tabSupplier` where docstatus < 2 and \ |
| 78 | (%(key)s like \"%s\" or supplier_name like \"%%%s\") \ |
| 79 | order by \ |
| 80 | case when name like \"%s%%\" then 0 else 1 end, \ |
| 81 | case when supplier_name like \"%s%%\" then 0 else 1 end, \ |
| 82 | name, supplier_name limit 50"; |
| 83 | }; |
| 84 | |
| 85 | wn.provide("erpnext.queries"); |
| 86 | |
| 87 | erpnext.queries.get_conditions = function(doctype, opts) { |
| 88 | conditions = []; |
| 89 | if (opts) { |
| 90 | $.each(opts, function(key, val) { |
| 91 | var lhs = "`tab" + doctype + "`.`" + key + "`"; |
| 92 | |
| 93 | if(key.indexOf(doctype)!=-1) { |
| 94 | // with function |
| 95 | lhs = key; |
| 96 | } |
| 97 | |
| 98 | if (esc_quotes(val).charAt(0) != "!") |
| 99 | conditions.push(lhs + "='"+esc_quotes(val)+"'"); |
| 100 | else |
| 101 | conditions.push(lhs + "!='"+esc_quotes(val).substr(1)+"'"); |
| 102 | }); |
| 103 | } |
| 104 | return conditions; |
| 105 | } |
| 106 | |
| 107 | erpnext.queries.account = function(opts) { |
| 108 | if(!opts) |
| 109 | opts = {}; |
| 110 | if(!opts.group_or_ledger) |
| 111 | opts.group_or_ledger = "Ledger"; |
| 112 | |
| 113 | var conditions = erpnext.queries.get_conditions("Account", opts); |
| 114 | |
| 115 | return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \ |
| 116 | FROM tabAccount \ |
| 117 | WHERE tabAccount.docstatus!=2 \ |
| 118 | AND tabAccount.%(key)s LIKE "%s" ' + (conditions |
| 119 | ? (" AND " + conditions.join(" AND ")) |
| 120 | : "") |
| 121 | + " LIMIT 50" |
| 122 | } |
| 123 | |
| 124 | erpnext.queries.item = function(opts) { |
| 125 | var conditions = erpnext.queries.get_conditions("Item", opts); |
| 126 | |
| 127 | return 'SELECT tabItem.name, \ |
| 128 | if(length(tabItem.item_name) > 40, \ |
| 129 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \ |
| 130 | if(length(tabItem.description) > 40, \ |
| 131 | concat(substr(tabItem.description, 1, 40), "..."), description) as decription \ |
| 132 | FROM tabItem \ |
| 133 | WHERE tabItem.docstatus!=2 \ |
| 134 | AND (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00") \ |
| 135 | OR `tabItem`.`end_of_life` > NOW()) \ |
Nabin Hait | 6ce8af3 | 2013-04-11 16:57:01 +0530 | [diff] [blame] | 136 | AND (tabItem.%(key)s LIKE \"%s\" OR tabItem.item_name LIKE \"%%%s\")' + |
| 137 | (conditions ? (" AND " + conditions.join(" AND ")) : "") + " LIMIT 50" |
Rushabh Mehta | 5e7ec20 | 2013-01-11 11:15:27 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | erpnext.queries.item_std = function() { |
| 141 | return 'SELECT tabItem.name, \ |
| 142 | if(length(tabItem.item_name) > 40, \ |
| 143 | concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \ |
| 144 | if(length(tabItem.description) > 40, \ |
| 145 | concat(substr(tabItem.description, 1, 40), "..."), description) as decription \ |
| 146 | FROM tabItem \ |
| 147 | WHERE tabItem.docstatus!=2 \ |
| 148 | AND tabItem.%(key)s LIKE "%s" LIMIT 50'; |
| 149 | } |
| 150 | |
| 151 | erpnext.queries.bom = function(opts) { |
| 152 | var conditions = erpnext.queries.get_conditions("BOM", opts); |
| 153 | |
| 154 | return 'SELECT tabBOM.name, tabBOM.item \ |
| 155 | FROM tabBOM \ |
| 156 | WHERE tabBOM.docstatus=1 \ |
| 157 | AND tabBOM.is_active=1 \ |
| 158 | AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length |
| 159 | ? (" AND " + conditions.join(" AND ")) |
| 160 | : "") |
| 161 | + " LIMIT 50" |
| 162 | |
Anand Doshi | 9e1d120 | 2013-05-09 19:34:34 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | erpnext.queries.task = function() { |
| 166 | return { query: "projects.utils.query_task" }; |
| 167 | }; |