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); |
| 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 Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 53 | // searches for enabled profiles |
Anand Doshi | e65e621 | 2012-11-19 15:43:18 +0530 | [diff] [blame] | 54 | erpnext.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 Doshi | b45cbda | 2012-11-21 18:00:22 +0530 | [diff] [blame] | 59 | 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 Doshi | 353d408 | 2012-11-19 17:35:50 +0530 | [diff] [blame] | 64 | }; |
| 65 | |
Anand Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 66 | // searches for active employees |
Anand Doshi | 353d408 | 2012-11-19 17:35:50 +0530 | [diff] [blame] | 67 | erpnext.utils.employee_query = function() { |
| 68 | return "select name, employee_name from `tabEmployee` \ |
| 69 | where status = 'Active' and docstatus < 2 and \ |
Anand Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 70 | (%(key)s like \"%s\" or employee_name like \"%%%s\") \ |
Anand Doshi | b45cbda | 2012-11-21 18:00:22 +0530 | [diff] [blame] | 71 | 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 Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | // searches for leads which are not converted |
| 78 | erpnext.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 Doshi | b45cbda | 2012-11-21 18:00:22 +0530 | [diff] [blame] | 82 | 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 Doshi | 452e686 | 2012-11-19 18:08:55 +0530 | [diff] [blame] | 87 | }; |
Anand Doshi | 93a35ca | 2012-11-21 18:02:22 +0530 | [diff] [blame] | 88 | |
| 89 | // searches for customer |
| 90 | erpnext.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 |
| 106 | erpnext.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 Mehta | bbd5ea3 | 2012-12-04 16:31:54 +0530 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | wn.provide("erpnext.queries"); |
| 122 | |
| 123 | erpnext.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 Hait | 5999944 | 2012-12-10 18:11:42 +0530 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | erpnext.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 Hait | 3adaf60 | 2012-12-14 14:25:51 +0530 | [diff] [blame] | 156 | AND tabBOM.is_active=1 \ |
Nabin Hait | 5999944 | 2012-12-10 18:11:42 +0530 | [diff] [blame] | 157 | AND tabBOM.%(key)s LIKE "%s" ' + (conditions.length |
| 158 | ? (" AND " + conditions.join(" AND ")) |
| 159 | : "") |
Rushabh Mehta | bbd5ea3 | 2012-12-04 16:31:54 +0530 | [diff] [blame] | 160 | } |