blob: 3eaad088ea2f4c0f672f5af935819c1a452fa318 [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
117erpnext.queries.account = function(opts) {
118 if(!opts)
119 opts = {};
120 if(!opts.group_or_ledger)
121 opts.group_or_ledger = "Ledger";
122
123 conditions = [];
124 $.each(opts, function(key, val) {
125 conditions.push("tabAccount.`" + key + "`='"+esc_quotes(val)+"'");
126 });
127
128 return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \
129 FROM tabAccount \
130 WHERE tabAccount.docstatus!=2 \
131 AND tabAccount.%(key)s LIKE "%s" ' + (conditions
132 ? (" AND " + conditions.join(" AND "))
133 : "")
Nabin Hait59999442012-12-10 18:11:42 +0530134}
135
136erpnext.queries.bom = function(opts) {
137 conditions = [];
138 if (opts) {
139 $.each(opts, function(key, val) {
140 if (esc_quotes(val).charAt(0) != "!")
141 conditions.push("tabBOM.`" + key + "`='"+esc_quotes(val)+"'");
142 else
143 conditions.push("tabBOM.`" + key + "`!='"+esc_quotes(val).substr(1)+"'");
144 });
145 }
146
147 return 'SELECT tabBOM.name, tabBOM.item \
148 FROM tabBOM \
149 WHERE tabBOM.docstatus=1 \
Nabin Hait3adaf602012-12-14 14:25:51 +0530150 AND tabBOM.is_active=1 \
Nabin Hait59999442012-12-10 18:11:42 +0530151 AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length
152 ? (" AND " + conditions.join(" AND "))
153 : "")
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530154}