blob: 4a88cd802deb72dbb4a5d78dde80afe3f57e0cd1 [file] [log] [blame]
Anand Doshie65e6212012-11-19 15:43:18 +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
17wn.provide('erpnext.utils');
18
Anand Doshi452e6862012-11-19 18:08:55 +053019// searches for enabled profiles
Anand Doshie65e6212012-11-19 15:43:18 +053020erpnext.utils.profile_query = function() {
21 return "select name, concat_ws(' ', first_name, middle_name, last_name) \
22 from `tabProfile` where ifnull(enabled, 0)=1 and docstatus < 2 and \
23 name not in ('Administrator', 'Guest') and (%(key)s like \"%s\" or \
24 concat_ws(' ', first_name, middle_name, last_name) like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053025 order by \
26 case when name like \"%s%%\" then 0 else 1 end, \
27 case when concat_ws(' ', first_name, middle_name, last_name) like \"%s%%\" \
28 then 0 else 1 end, \
29 name asc limit 50";
Anand Doshi353d4082012-11-19 17:35:50 +053030};
31
Anand Doshi452e6862012-11-19 18:08:55 +053032// searches for active employees
Anand Doshi353d4082012-11-19 17:35:50 +053033erpnext.utils.employee_query = function() {
34 return "select name, employee_name from `tabEmployee` \
35 where status = 'Active' and docstatus < 2 and \
Anand Doshi452e6862012-11-19 18:08:55 +053036 (%(key)s like \"%s\" or employee_name like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053037 order by \
38 case when name like \"%s%%\" then 0 else 1 end, \
39 case when employee_name like \"%s%%\" then 0 else 1 end, \
40 name limit 50";
Anand Doshi452e6862012-11-19 18:08:55 +053041};
42
43// searches for leads which are not converted
44erpnext.utils.lead_query = function() {
45 return "select name, lead_name, company_name from `tabLead` \
46 where docstatus < 2 and ifnull(status, '') != 'Converted' and \
47 (%(key)s like \"%s\" or lead_name like \"%%%s\" or company_name like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053048 order by \
49 case when name like \"%s%%\" then 0 else 1 end, \
50 case when lead_name like \"%s%%\" then 0 else 1 end, \
51 case when company_name like \"%s%%\" then 0 else 1 end, \
52 lead_name asc limit 50";
Anand Doshi452e6862012-11-19 18:08:55 +053053};
Anand Doshi93a35ca2012-11-21 18:02:22 +053054
55// searches for customer
56erpnext.utils.customer_query = function() {
57 if(sys_defaults.cust_master_name == "Customer Name") {
58 var fields = ["name", "customer_group", "country", "territory"];
59 } else {
60 var fields = ["name", "customer_name", "customer_group", "country", "territory"];
61 }
62
63 return "select " + fields.join(", ") + " from `tabCustomer` where docstatus < 2 and \
64 (%(key)s like \"%s\" or customer_name like \"%%%s\") \
65 order by \
66 case when name like \"%s%%\" then 0 else 1 end, \
67 case when customer_name like \"%s%%\" then 0 else 1 end, \
68 name, customer_name limit 50";
69};
70
71// searches for supplier
72erpnext.utils.supplier_query = function() {
73 if(sys_defaults.supp_master_name == "Supplier Name") {
74 var fields = ["name", "supplier_type"];
75 } else {
76 var fields = ["name", "supplier_name", "supplier_type"];
77 }
78
79 return "select " + fields.join(", ") + " from `tabSupplier` where docstatus < 2 and \
80 (%(key)s like \"%s\" or supplier_name like \"%%%s\") \
81 order by \
82 case when name like \"%s%%\" then 0 else 1 end, \
83 case when supplier_name like \"%s%%\" then 0 else 1 end, \
84 name, supplier_name limit 50";
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +053085};
86
87wn.provide("erpnext.queries");
88
89erpnext.queries.account = function(opts) {
90 if(!opts)
91 opts = {};
92 if(!opts.group_or_ledger)
93 opts.group_or_ledger = "Ledger";
94
95 conditions = [];
96 $.each(opts, function(key, val) {
97 conditions.push("tabAccount.`" + key + "`='"+esc_quotes(val)+"'");
98 });
99
100 return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \
101 FROM tabAccount \
102 WHERE tabAccount.docstatus!=2 \
103 AND tabAccount.%(key)s LIKE "%s" ' + (conditions
104 ? (" AND " + conditions.join(" AND "))
105 : "")
Nabin Hait59999442012-12-10 18:11:42 +0530106}
107
108erpnext.queries.bom = function(opts) {
109 conditions = [];
110 if (opts) {
111 $.each(opts, function(key, val) {
112 if (esc_quotes(val).charAt(0) != "!")
113 conditions.push("tabBOM.`" + key + "`='"+esc_quotes(val)+"'");
114 else
115 conditions.push("tabBOM.`" + key + "`!='"+esc_quotes(val).substr(1)+"'");
116 });
117 }
118
119 return 'SELECT tabBOM.name, tabBOM.item \
120 FROM tabBOM \
121 WHERE tabBOM.docstatus=1 \
Nabin Hait3adaf602012-12-14 14:25:51 +0530122 AND tabBOM.is_active=1 \
Nabin Hait59999442012-12-10 18:11:42 +0530123 AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length
124 ? (" AND " + conditions.join(" AND "))
125 : "")
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530126}