blob: aa65d1ae38b3c3300214eac1bd8358c606eb5c16 [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
Nabin Hait64354372012-12-19 19:33:41 +053019// TODO
20erpnext.utils.Controller = Class.extend({
21 init: function(opts) {
22 $.extend(this, opts);
Nabin Hait079f5e02012-12-21 14:14:07 +053023 this.setup && this.setup();
Nabin Hait64354372012-12-19 19:33:41 +053024 },
25
26 onload_post_render: function() {
27 this.setup_defaults();
28 },
29
30 setup_defaults: function() {
31 var me = this;
32
33 var defaults = {
34 posting_date: wn.datetime.get_today(),
35 }
36
37 $.each(defaults, function(k, v) {
38 if(!me.frm.doc[k]) me.frm.set_value(k, v);
39 });
40 },
41
42 refresh: function() {
43 erpnext.hide_naming_series();
Nabin Hait64354372012-12-19 19:33:41 +053044 }
45});
46
Anand Doshi452e6862012-11-19 18:08:55 +053047// searches for enabled profiles
Anand Doshie65e6212012-11-19 15:43:18 +053048erpnext.utils.profile_query = function() {
49 return "select name, concat_ws(' ', first_name, middle_name, last_name) \
50 from `tabProfile` where ifnull(enabled, 0)=1 and docstatus < 2 and \
51 name not in ('Administrator', 'Guest') and (%(key)s like \"%s\" or \
52 concat_ws(' ', first_name, middle_name, last_name) like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053053 order by \
54 case when name like \"%s%%\" then 0 else 1 end, \
55 case when concat_ws(' ', first_name, middle_name, last_name) like \"%s%%\" \
56 then 0 else 1 end, \
57 name asc limit 50";
Anand Doshi353d4082012-11-19 17:35:50 +053058};
59
Anand Doshi452e6862012-11-19 18:08:55 +053060// searches for active employees
Anand Doshi353d4082012-11-19 17:35:50 +053061erpnext.utils.employee_query = function() {
62 return "select name, employee_name from `tabEmployee` \
63 where status = 'Active' and docstatus < 2 and \
Anand Doshi452e6862012-11-19 18:08:55 +053064 (%(key)s like \"%s\" or employee_name like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053065 order by \
66 case when name like \"%s%%\" then 0 else 1 end, \
67 case when employee_name like \"%s%%\" then 0 else 1 end, \
68 name limit 50";
Anand Doshi452e6862012-11-19 18:08:55 +053069};
70
71// searches for leads which are not converted
72erpnext.utils.lead_query = function() {
73 return "select name, lead_name, company_name from `tabLead` \
74 where docstatus < 2 and ifnull(status, '') != 'Converted' and \
75 (%(key)s like \"%s\" or lead_name like \"%%%s\" or company_name like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053076 order by \
77 case when name like \"%s%%\" then 0 else 1 end, \
78 case when lead_name like \"%s%%\" then 0 else 1 end, \
79 case when company_name like \"%s%%\" then 0 else 1 end, \
80 lead_name asc limit 50";
Anand Doshi452e6862012-11-19 18:08:55 +053081};
Anand Doshi93a35ca2012-11-21 18:02:22 +053082
83// searches for customer
84erpnext.utils.customer_query = function() {
85 if(sys_defaults.cust_master_name == "Customer Name") {
86 var fields = ["name", "customer_group", "country", "territory"];
87 } else {
88 var fields = ["name", "customer_name", "customer_group", "country", "territory"];
89 }
90
91 return "select " + fields.join(", ") + " from `tabCustomer` where docstatus < 2 and \
92 (%(key)s like \"%s\" or customer_name like \"%%%s\") \
93 order by \
94 case when name like \"%s%%\" then 0 else 1 end, \
95 case when customer_name like \"%s%%\" then 0 else 1 end, \
96 name, customer_name limit 50";
97};
98
99// searches for supplier
100erpnext.utils.supplier_query = function() {
101 if(sys_defaults.supp_master_name == "Supplier Name") {
102 var fields = ["name", "supplier_type"];
103 } else {
104 var fields = ["name", "supplier_name", "supplier_type"];
105 }
106
107 return "select " + fields.join(", ") + " from `tabSupplier` where docstatus < 2 and \
108 (%(key)s like \"%s\" or supplier_name like \"%%%s\") \
109 order by \
110 case when name like \"%s%%\" then 0 else 1 end, \
111 case when supplier_name like \"%s%%\" then 0 else 1 end, \
112 name, supplier_name limit 50";
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530113};
114
115wn.provide("erpnext.queries");
116
Rushabh Mehta143ea712013-01-07 17:03:11 +0530117erpnext.queries.get_conditions = function(doctype, opts) {
118 conditions = [];
119 if (opts) {
120 $.each(opts, function(key, val) {
121 var lhs = "`tab" + doctype + "`.`" + key + "`";
122
123 if(key.indexOf(doctype)!=-1) {
124 // with function
125 lhs = key;
126 }
127
128 if (esc_quotes(val).charAt(0) != "!")
129 conditions.push(lhs + "='"+esc_quotes(val)+"'");
130 else
131 conditions.push(lhs + "!='"+esc_quotes(val).substr(1)+"'");
132 });
133 }
134 return conditions;
135}
136
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530137erpnext.queries.account = function(opts) {
138 if(!opts)
139 opts = {};
140 if(!opts.group_or_ledger)
141 opts.group_or_ledger = "Ledger";
142
Rushabh Mehta143ea712013-01-07 17:03:11 +0530143 var conditions = erpnext.queries.get_conditions("Account", opts);
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530144
145 return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \
146 FROM tabAccount \
147 WHERE tabAccount.docstatus!=2 \
148 AND tabAccount.%(key)s LIKE "%s" ' + (conditions
149 ? (" AND " + conditions.join(" AND "))
150 : "")
Rushabh Mehta143ea712013-01-07 17:03:11 +0530151 + " LIMIT 50"
152}
153
154erpnext.queries.item = function(opts) {
155 var conditions = erpnext.queries.get_conditions("Item", opts);
156
157 return 'SELECT tabItem.name, \
158 if(length(tabItem.item_name) > 40, \
159 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, \
160 if(length(tabItem.description) > 40, \
161 concat(substr(tabItem.description, 1, 40), "..."), description) as decription \
162 FROM tabItem \
163 WHERE tabItem.docstatus!=2 \
164 AND (ifnull(`tabItem`.`end_of_life`,"") in ("", "0000-00-00") \
165 OR `tabItem`.`end_of_life` > NOW()) \
166 AND tabItem.%(key)s LIKE "%s" ' + (conditions
167 ? (" AND " + conditions.join(" AND "))
168 : "")
169 + " LIMIT 50"
Nabin Hait59999442012-12-10 18:11:42 +0530170}
171
172erpnext.queries.bom = function(opts) {
Rushabh Mehta143ea712013-01-07 17:03:11 +0530173 var conditions = erpnext.queries.get_conditions("BOM", opts);
Nabin Hait59999442012-12-10 18:11:42 +0530174
175 return 'SELECT tabBOM.name, tabBOM.item \
176 FROM tabBOM \
177 WHERE tabBOM.docstatus=1 \
Nabin Hait3adaf602012-12-14 14:25:51 +0530178 AND tabBOM.is_active=1 \
Nabin Hait59999442012-12-10 18:11:42 +0530179 AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length
180 ? (" AND " + conditions.join(" AND "))
181 : "")
Rushabh Mehta143ea712013-01-07 17:03:11 +0530182 + " LIMIT 50"
183
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530184}