blob: a6a79b5a02eff9a52c3ffca159803982f56c9dbc [file] [log] [blame]
Rushabh Mehta5e7ec202013-01-11 11:15:27 +05301// 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
18erpnext.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
31erpnext.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
42erpnext.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
54erpnext.utils.customer_query = function() {
55 if(sys_defaults.cust_master_name == "Customer Name") {
56 var fields = ["name", "customer_group", "country", "territory"];
57 } else {
58 var fields = ["name", "customer_name", "customer_group", "country", "territory"];
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
70erpnext.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
85wn.provide("erpnext.queries");
86
87erpnext.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
107erpnext.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
124erpnext.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()) \
136 AND tabItem.%(key)s LIKE "%s" ' + (conditions
137 ? (" AND " + conditions.join(" AND "))
138 : "")
139 + " LIMIT 50"
140}
141
142erpnext.queries.item_std = function() {
143 return 'SELECT tabItem.name, \
144 if(length(tabItem.item_name) > 40, \
145 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \
146 if(length(tabItem.description) > 40, \
147 concat(substr(tabItem.description, 1, 40), "..."), description) as decription \
148 FROM tabItem \
149 WHERE tabItem.docstatus!=2 \
150 AND tabItem.%(key)s LIKE "%s" LIMIT 50';
151}
152
153erpnext.queries.bom = function(opts) {
154 var conditions = erpnext.queries.get_conditions("BOM", opts);
155
156 return 'SELECT tabBOM.name, tabBOM.item \
157 FROM tabBOM \
158 WHERE tabBOM.docstatus=1 \
159 AND tabBOM.is_active=1 \
160 AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length
161 ? (" AND " + conditions.join(" AND "))
162 : "")
163 + " LIMIT 50"
164
165}