Anand Doshi | e65e621 | 2012-11-19 15:43:18 +0530 | [diff] [blame] | 1 | // 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 | wn.provide('erpnext.utils'); |
| 18 | |
Nabin Hait | 6435437 | 2012-12-19 19:33:41 +0530 | [diff] [blame] | 19 | // TODO |
| 20 | erpnext.utils.Controller = Class.extend({ |
| 21 | init: function(opts) { |
| 22 | $.extend(this, opts); |
Nabin Hait | 079f5e0 | 2012-12-21 14:14:07 +0530 | [diff] [blame] | 23 | this.setup && this.setup(); |
Nabin Hait | 6435437 | 2012-12-19 19:33:41 +0530 | [diff] [blame] | 24 | }, |
| 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 Hait | 6435437 | 2012-12-19 19:33:41 +0530 | [diff] [blame] | 44 | } |
| 45 | }); |
| 46 | |
Anand Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 47 | // searches for enabled profiles |
Anand Doshi | e65e621 | 2012-11-19 15:43:18 +0530 | [diff] [blame] | 48 | erpnext.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 Doshi | b45cbda | 2012-11-21 18:00:22 +0530 | [diff] [blame] | 53 | 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 Doshi | 353d408 | 2012-11-19 17:35:50 +0530 | [diff] [blame] | 58 | }; |
| 59 | |
Anand Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 60 | // searches for active employees |
Anand Doshi | 353d408 | 2012-11-19 17:35:50 +0530 | [diff] [blame] | 61 | erpnext.utils.employee_query = function() { |
| 62 | return "select name, employee_name from `tabEmployee` \ |
| 63 | where status = 'Active' and docstatus < 2 and \ |
Anand Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 64 | (%(key)s like \"%s\" or employee_name like \"%%%s\") \ |
Anand Doshi | b45cbda | 2012-11-21 18:00:22 +0530 | [diff] [blame] | 65 | 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 Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | // searches for leads which are not converted |
| 72 | erpnext.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 Doshi | b45cbda | 2012-11-21 18:00:22 +0530 | [diff] [blame] | 76 | 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 Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 81 | }; |
Anand Doshi | 93a35ca | 2012-11-21 18:02:22 +0530 | [diff] [blame] | 82 | |
| 83 | // searches for customer |
| 84 | erpnext.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 |
| 100 | erpnext.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 Mehta | bbd5ea3 | 2012-12-04 16:31:54 +0530 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | wn.provide("erpnext.queries"); |
| 116 | |
| 117 | erpnext.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 Hait | 5999944 | 2012-12-10 18:11:42 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | erpnext.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 Hait | 3adaf60 | 2012-12-14 14:25:51 +0530 | [diff] [blame] | 150 | AND tabBOM.is_active=1 \ |
Nabin Hait | 5999944 | 2012-12-10 18:11:42 +0530 | [diff] [blame] | 151 | AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length |
| 152 | ? (" AND " + conditions.join(" AND ")) |
| 153 | : "") |
Rushabh Mehta | bbd5ea3 | 2012-12-04 16:31:54 +0530 | [diff] [blame] | 154 | } |