blob: af3e57926f262d8d0ebaa8ba9a0ca600f8a33931 [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);
23 },
24
25 onload_post_render: function() {
26 this.setup_defaults();
27 },
28
29 setup_defaults: function() {
30 var me = this;
31
32 var defaults = {
33 posting_date: wn.datetime.get_today(),
34 }
35
36 $.each(defaults, function(k, v) {
37 if(!me.frm.doc[k]) me.frm.set_value(k, v);
38 });
39 },
40
41 refresh: function() {
42 erpnext.hide_naming_series();
43
44 if(this.frm.doc.__islocal && this.frm.fields_dict.naming_series
45 && !this.frm.doc.naming_series) {
46 var series_opts = $.map((this.frm.fields_dict.naming_series.df.options ||
47 "").split("\n"), function(v) { return v ? v : null; });
48 if (series_opts.length==1) this.frm.set_value("naming_series", series_opts[0]);
49 }
50 }
51});
52
Anand Doshi452e6862012-11-19 18:08:55 +053053// searches for enabled profiles
Anand Doshie65e6212012-11-19 15:43:18 +053054erpnext.utils.profile_query = function() {
55 return "select name, concat_ws(' ', first_name, middle_name, last_name) \
56 from `tabProfile` where ifnull(enabled, 0)=1 and docstatus < 2 and \
57 name not in ('Administrator', 'Guest') and (%(key)s like \"%s\" or \
58 concat_ws(' ', first_name, middle_name, last_name) like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053059 order by \
60 case when name like \"%s%%\" then 0 else 1 end, \
61 case when concat_ws(' ', first_name, middle_name, last_name) like \"%s%%\" \
62 then 0 else 1 end, \
63 name asc limit 50";
Anand Doshi353d4082012-11-19 17:35:50 +053064};
65
Anand Doshi452e6862012-11-19 18:08:55 +053066// searches for active employees
Anand Doshi353d4082012-11-19 17:35:50 +053067erpnext.utils.employee_query = function() {
68 return "select name, employee_name from `tabEmployee` \
69 where status = 'Active' and docstatus < 2 and \
Anand Doshi452e6862012-11-19 18:08:55 +053070 (%(key)s like \"%s\" or employee_name like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053071 order by \
72 case when name like \"%s%%\" then 0 else 1 end, \
73 case when employee_name like \"%s%%\" then 0 else 1 end, \
74 name limit 50";
Anand Doshi452e6862012-11-19 18:08:55 +053075};
76
77// searches for leads which are not converted
78erpnext.utils.lead_query = function() {
79 return "select name, lead_name, company_name from `tabLead` \
80 where docstatus < 2 and ifnull(status, '') != 'Converted' and \
81 (%(key)s like \"%s\" or lead_name like \"%%%s\" or company_name like \"%%%s\") \
Anand Doshib45cbda2012-11-21 18:00:22 +053082 order by \
83 case when name like \"%s%%\" then 0 else 1 end, \
84 case when lead_name like \"%s%%\" then 0 else 1 end, \
85 case when company_name like \"%s%%\" then 0 else 1 end, \
86 lead_name asc limit 50";
Anand Doshi452e6862012-11-19 18:08:55 +053087};
Anand Doshi93a35ca2012-11-21 18:02:22 +053088
89// searches for customer
90erpnext.utils.customer_query = function() {
91 if(sys_defaults.cust_master_name == "Customer Name") {
92 var fields = ["name", "customer_group", "country", "territory"];
93 } else {
94 var fields = ["name", "customer_name", "customer_group", "country", "territory"];
95 }
96
97 return "select " + fields.join(", ") + " from `tabCustomer` where docstatus < 2 and \
98 (%(key)s like \"%s\" or customer_name like \"%%%s\") \
99 order by \
100 case when name like \"%s%%\" then 0 else 1 end, \
101 case when customer_name like \"%s%%\" then 0 else 1 end, \
102 name, customer_name limit 50";
103};
104
105// searches for supplier
106erpnext.utils.supplier_query = function() {
107 if(sys_defaults.supp_master_name == "Supplier Name") {
108 var fields = ["name", "supplier_type"];
109 } else {
110 var fields = ["name", "supplier_name", "supplier_type"];
111 }
112
113 return "select " + fields.join(", ") + " from `tabSupplier` where docstatus < 2 and \
114 (%(key)s like \"%s\" or supplier_name like \"%%%s\") \
115 order by \
116 case when name like \"%s%%\" then 0 else 1 end, \
117 case when supplier_name like \"%s%%\" then 0 else 1 end, \
118 name, supplier_name limit 50";
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530119};
120
121wn.provide("erpnext.queries");
122
123erpnext.queries.account = function(opts) {
124 if(!opts)
125 opts = {};
126 if(!opts.group_or_ledger)
127 opts.group_or_ledger = "Ledger";
128
129 conditions = [];
130 $.each(opts, function(key, val) {
131 conditions.push("tabAccount.`" + key + "`='"+esc_quotes(val)+"'");
132 });
133
134 return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \
135 FROM tabAccount \
136 WHERE tabAccount.docstatus!=2 \
137 AND tabAccount.%(key)s LIKE "%s" ' + (conditions
138 ? (" AND " + conditions.join(" AND "))
139 : "")
Nabin Hait59999442012-12-10 18:11:42 +0530140}
141
142erpnext.queries.bom = function(opts) {
143 conditions = [];
144 if (opts) {
145 $.each(opts, function(key, val) {
146 if (esc_quotes(val).charAt(0) != "!")
147 conditions.push("tabBOM.`" + key + "`='"+esc_quotes(val)+"'");
148 else
149 conditions.push("tabBOM.`" + key + "`!='"+esc_quotes(val).substr(1)+"'");
150 });
151 }
152
153 return 'SELECT tabBOM.name, tabBOM.item \
154 FROM tabBOM \
155 WHERE tabBOM.docstatus=1 \
Nabin Hait3adaf602012-12-14 14:25:51 +0530156 AND tabBOM.is_active=1 \
Nabin Hait59999442012-12-10 18:11:42 +0530157 AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length
158 ? (" AND " + conditions.join(" AND "))
159 : "")
Rushabh Mehtabbd5ea32012-12-04 16:31:54 +0530160}