blob: a7e137e3f948fe0c7891ca21b3bc6296c8cf8e0e [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
Anand Doshibdee6e02013-07-09 13:03:39 +053018wn.provide("erpnext.utils");
Rushabh Mehta5e7ec202013-01-11 11:15:27 +053019erpnext.utils.profile_query = function() {
20 return "select name, concat_ws(' ', first_name, middle_name, last_name) \
21 from `tabProfile` where ifnull(enabled, 0)=1 and docstatus < 2 and \
22 name not in ('Administrator', 'Guest') and (%(key)s like \"%s\" or \
23 concat_ws(' ', first_name, middle_name, last_name) like \"%%%s\") \
24 order by \
25 case when name like \"%s%%\" then 0 else 1 end, \
26 case when concat_ws(' ', first_name, middle_name, last_name) like \"%s%%\" \
27 then 0 else 1 end, \
28 name asc limit 50";
29};
30
31// searches for active employees
32erpnext.utils.employee_query = function() {
33 return "select name, employee_name from `tabEmployee` \
34 where status = 'Active' and docstatus < 2 and \
35 (%(key)s like \"%s\" or employee_name like \"%%%s\") \
36 order by \
37 case when name like \"%s%%\" then 0 else 1 end, \
38 case when employee_name like \"%s%%\" then 0 else 1 end, \
39 name limit 50";
40};
41
42// searches for leads which are not converted
43erpnext.utils.lead_query = function() {
44 return "select name, lead_name, company_name from `tabLead` \
45 where docstatus < 2 and ifnull(status, '') != 'Converted' and \
46 (%(key)s like \"%s\" or lead_name like \"%%%s\" or company_name like \"%%%s\") \
47 order by \
48 case when name like \"%s%%\" then 0 else 1 end, \
49 case when lead_name like \"%s%%\" then 0 else 1 end, \
50 case when company_name like \"%s%%\" then 0 else 1 end, \
51 lead_name asc limit 50";
52};
53
54// searches for customer
55erpnext.utils.customer_query = function() {
56 if(sys_defaults.cust_master_name == "Customer Name") {
Rushabh Mehtadb7139a2013-01-17 18:22:22 +053057 var fields = ["name", "customer_group", "territory"];
Rushabh Mehta5e7ec202013-01-11 11:15:27 +053058 } else {
Rushabh Mehtadb7139a2013-01-17 18:22:22 +053059 var fields = ["name", "customer_name", "customer_group", "territory"];
Rushabh Mehta5e7ec202013-01-11 11:15:27 +053060 }
61
62 return "select " + fields.join(", ") + " from `tabCustomer` where docstatus < 2 and \
63 (%(key)s like \"%s\" or customer_name like \"%%%s\") \
64 order by \
65 case when name like \"%s%%\" then 0 else 1 end, \
66 case when customer_name like \"%s%%\" then 0 else 1 end, \
67 name, customer_name limit 50";
68};
69
70// searches for supplier
71erpnext.utils.supplier_query = function() {
72 if(sys_defaults.supp_master_name == "Supplier Name") {
73 var fields = ["name", "supplier_type"];
74 } else {
75 var fields = ["name", "supplier_name", "supplier_type"];
76 }
77
78 return "select " + fields.join(", ") + " from `tabSupplier` where docstatus < 2 and \
79 (%(key)s like \"%s\" or supplier_name like \"%%%s\") \
80 order by \
81 case when name like \"%s%%\" then 0 else 1 end, \
82 case when supplier_name like \"%s%%\" then 0 else 1 end, \
83 name, supplier_name limit 50";
84};
85
86wn.provide("erpnext.queries");
87
88erpnext.queries.get_conditions = function(doctype, opts) {
89 conditions = [];
90 if (opts) {
91 $.each(opts, function(key, val) {
92 var lhs = "`tab" + doctype + "`.`" + key + "`";
93
94 if(key.indexOf(doctype)!=-1) {
95 // with function
96 lhs = key;
97 }
98
99 if (esc_quotes(val).charAt(0) != "!")
100 conditions.push(lhs + "='"+esc_quotes(val)+"'");
101 else
102 conditions.push(lhs + "!='"+esc_quotes(val).substr(1)+"'");
103 });
104 }
105 return conditions;
106}
107
108erpnext.queries.account = function(opts) {
109 if(!opts)
110 opts = {};
111 if(!opts.group_or_ledger)
112 opts.group_or_ledger = "Ledger";
113
114 var conditions = erpnext.queries.get_conditions("Account", opts);
115
116 return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \
117 FROM tabAccount \
118 WHERE tabAccount.docstatus!=2 \
119 AND tabAccount.%(key)s LIKE "%s" ' + (conditions
120 ? (" AND " + conditions.join(" AND "))
121 : "")
122 + " LIMIT 50"
123}
124
125erpnext.queries.item = function(opts) {
126 var conditions = erpnext.queries.get_conditions("Item", opts);
127
128 return 'SELECT tabItem.name, \
129 if(length(tabItem.item_name) > 40, \
130 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \
131 if(length(tabItem.description) > 40, \
132 concat(substr(tabItem.description, 1, 40), "..."), description) as decription \
133 FROM tabItem \
134 WHERE tabItem.docstatus!=2 \
135 AND (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00") \
136 OR `tabItem`.`end_of_life` > NOW()) \
Nabin Hait6ce8af32013-04-11 16:57:01 +0530137 AND (tabItem.%(key)s LIKE \"%s\" OR tabItem.item_name LIKE \"%%%s\")' +
138 (conditions ? (" AND " + conditions.join(" AND ")) : "") + " LIMIT 50"
Rushabh Mehta5e7ec202013-01-11 11:15:27 +0530139}
140
141erpnext.queries.item_std = function() {
142 return 'SELECT tabItem.name, \
143 if(length(tabItem.item_name) > 40, \
144 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \
145 if(length(tabItem.description) > 40, \
146 concat(substr(tabItem.description, 1, 40), "..."), description) as decription \
147 FROM tabItem \
148 WHERE tabItem.docstatus!=2 \
149 AND tabItem.%(key)s LIKE "%s" LIMIT 50';
150}
151
152erpnext.queries.bom = function(opts) {
153 var conditions = erpnext.queries.get_conditions("BOM", opts);
154
155 return 'SELECT tabBOM.name, tabBOM.item \
156 FROM tabBOM \
157 WHERE tabBOM.docstatus=1 \
158 AND tabBOM.is_active=1 \
159 AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length
160 ? (" AND " + conditions.join(" AND "))
161 : "")
162 + " LIMIT 50"
163
Anand Doshi9e1d1202013-05-09 19:34:34 +0530164}
165
166erpnext.queries.task = function() {
167 return { query: "projects.utils.query_task" };
168};