Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
Rushabh Mehta | 5e7ec20 | 2013-01-11 11:15:27 +0530 | [diff] [blame] | 3 | |
Rushabh Mehta | 7c93200 | 2014-03-11 16:15:05 +0530 | [diff] [blame] | 4 | // searches for enabled users |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | frappe.provide("erpnext.queries"); |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 6 | $.extend(erpnext.queries, { |
Rushabh Mehta | 7c93200 | 2014-03-11 16:15:05 +0530 | [diff] [blame] | 7 | user: function() { |
| 8 | return { query: "frappe.core.doctype.user.user.user_query" }; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 9 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 10 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 11 | lead: function() { |
Rushabh Mehta | 6de403f | 2013-12-13 14:10:14 +0530 | [diff] [blame] | 12 | return { query: "erpnext.controllers.queries.lead_query" }; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 13 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 14 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 15 | customer: function() { |
Rushabh Mehta | 6de403f | 2013-12-13 14:10:14 +0530 | [diff] [blame] | 16 | return { query: "erpnext.controllers.queries.customer_query" }; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 17 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 18 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 19 | supplier: function() { |
Rushabh Mehta | 6de403f | 2013-12-13 14:10:14 +0530 | [diff] [blame] | 20 | return { query: "erpnext.controllers.queries.supplier_query" }; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 21 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 22 | |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 23 | item: function(filters) { |
| 24 | var args = { query: "erpnext.controllers.queries.item_query" }; |
| 25 | if(filters) args["filters"] = filters; |
| 26 | return args; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 27 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 28 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 29 | bom: function() { |
Rushabh Mehta | 6de403f | 2013-12-13 14:10:14 +0530 | [diff] [blame] | 30 | return { query: "erpnext.controllers.queries.bom" }; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 31 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 32 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 33 | task: function() { |
Akhilesh Darjee | 4cdb799 | 2014-01-30 13:56:57 +0530 | [diff] [blame] | 34 | return { query: "erpnext.projects.utils.query_task" }; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 35 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 36 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 37 | customer_filter: function(doc) { |
| 38 | if(!doc.customer) { |
Makarand Bauskar | d893efd | 2017-01-30 12:35:34 +0530 | [diff] [blame] | 39 | frappe.throw(__("Please set {0}", [__(frappe.meta.get_label(doc.doctype, "customer", doc.name))])); |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 40 | } |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 41 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 42 | return { filters: { customer: doc.customer } }; |
| 43 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 44 | |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 45 | contact_query: function(doc) { |
Rushabh Mehta | 8d39fd9 | 2017-01-16 13:06:07 +0530 | [diff] [blame] | 46 | if(frappe.dynamic_link) { |
| 47 | if(!doc[frappe.dynamic_link.fieldname]) { |
Makarand Bauskar | d893efd | 2017-01-30 12:35:34 +0530 | [diff] [blame] | 48 | frappe.throw(__("Please set {0}", |
| 49 | [__(frappe.meta.get_label(doc.doctype, frappe.dynamic_link.fieldname, doc.name))])); |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return { |
KanchanChauhan | 1dc26b1 | 2017-06-13 15:26:35 +0530 | [diff] [blame] | 53 | query: 'frappe.contacts.doctype.contact.contact.contact_query', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 54 | filters: { |
| 55 | link_doctype: frappe.dynamic_link.doctype, |
| 56 | link_name: doc[frappe.dynamic_link.fieldname] |
| 57 | } |
| 58 | }; |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 59 | } |
| 60 | }, |
| 61 | |
| 62 | address_query: function(doc) { |
Rushabh Mehta | 8d39fd9 | 2017-01-16 13:06:07 +0530 | [diff] [blame] | 63 | if(frappe.dynamic_link) { |
| 64 | if(!doc[frappe.dynamic_link.fieldname]) { |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 65 | frappe.throw(__("Please set {0}", |
Makarand Bauskar | d893efd | 2017-01-30 12:35:34 +0530 | [diff] [blame] | 66 | [__(frappe.meta.get_label(doc.doctype, frappe.dynamic_link.fieldname, doc.name))])); |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 67 | } |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 68 | |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 69 | return { |
KanchanChauhan | 1dc26b1 | 2017-06-13 15:26:35 +0530 | [diff] [blame] | 70 | query: 'frappe.contacts.doctype.address.address.address_query', |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 71 | filters: { |
| 72 | link_doctype: frappe.dynamic_link.doctype, |
| 73 | link_name: doc[frappe.dynamic_link.fieldname] |
| 74 | } |
| 75 | }; |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 76 | } |
| 77 | }, |
| 78 | |
mbauskar | 8271b4c | 2017-01-20 14:47:15 +0530 | [diff] [blame] | 79 | company_address_query: function(doc) { |
| 80 | return { |
KanchanChauhan | 1dc26b1 | 2017-06-13 15:26:35 +0530 | [diff] [blame] | 81 | query: 'frappe.contacts.doctype.address.address.address_query', |
mbauskar | 8271b4c | 2017-01-20 14:47:15 +0530 | [diff] [blame] | 82 | filters: { is_your_company_address: 1, link_doctype: 'Company', link_name: doc.company || '' } |
| 83 | }; |
| 84 | }, |
| 85 | |
Subin Tom | 98d4176 | 2021-12-21 12:35:44 +0530 | [diff] [blame] | 86 | dispatch_address_query: function(doc) { |
| 87 | return { |
| 88 | query: 'frappe.contacts.doctype.address.address.address_query', |
| 89 | filters: { link_doctype: 'Company', link_name: doc.company || '' } |
| 90 | }; |
| 91 | }, |
| 92 | |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 93 | supplier_filter: function(doc) { |
| 94 | if(!doc.supplier) { |
Makarand Bauskar | d893efd | 2017-01-30 12:35:34 +0530 | [diff] [blame] | 95 | frappe.throw(__("Please set {0}", [__(frappe.meta.get_label(doc.doctype, "supplier", doc.name))])); |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 96 | } |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 97 | |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 98 | return { filters: { supplier: doc.supplier } }; |
| 99 | }, |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 100 | |
| 101 | lead_filter: function(doc) { |
| 102 | if(!doc.lead) { |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 103 | frappe.throw(__("Please specify a {0}", |
Makarand Bauskar | d893efd | 2017-01-30 12:35:34 +0530 | [diff] [blame] | 104 | [__(frappe.meta.get_label(doc.doctype, "lead", doc.name))])); |
Nabin Hait | fe8db59 | 2014-01-30 12:32:47 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return { filters: { lead: doc.lead } }; |
| 108 | }, |
| 109 | |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 110 | not_a_group_filter: function() { |
Saurabh | 6bc7b89 | 2016-07-14 14:03:19 +0530 | [diff] [blame] | 111 | return { filters: { is_group: 0 } }; |
Anand Doshi | 61cad50 | 2013-07-18 13:26:27 +0530 | [diff] [blame] | 112 | }, |
Anand Doshi | b71a18c | 2014-05-02 16:20:45 +0530 | [diff] [blame] | 113 | |
| 114 | employee: function() { |
| 115 | return { query: "erpnext.controllers.queries.employee_query" } |
Anand Doshi | faefeaa | 2014-06-24 18:53:04 +0530 | [diff] [blame] | 116 | }, |
| 117 | |
| 118 | warehouse: function(doc) { |
| 119 | return { |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 120 | filters: [ |
| 121 | ["Warehouse", "company", "in", ["", cstr(doc.company)]], |
Saurabh | 93d68ac | 2016-06-26 22:50:11 +0530 | [diff] [blame] | 122 | ["Warehouse", "is_group", "=",0] |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 123 | |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 124 | ] |
Deepesh Garg | 23ab5c5 | 2020-12-31 11:29:06 +0530 | [diff] [blame] | 125 | }; |
Deepesh Garg | 6e5748e | 2020-11-15 22:43:48 +0530 | [diff] [blame] | 126 | }, |
| 127 | |
| 128 | get_filtered_dimensions: function(doc, child_fields, dimension, company) { |
| 129 | let account = ''; |
| 130 | |
| 131 | child_fields.forEach((field) => { |
| 132 | if (!account) { |
| 133 | account = doc[field]; |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | return { |
| 138 | query: "erpnext.controllers.queries.get_filtered_dimensions", |
| 139 | filters: { |
| 140 | 'dimension': dimension, |
| 141 | 'account': account, |
| 142 | 'company': company |
| 143 | } |
Deepesh Garg | 9e9ea96 | 2020-11-16 12:03:47 +0530 | [diff] [blame] | 144 | }; |
Anand Doshi | b71a18c | 2014-05-02 16:20:45 +0530 | [diff] [blame] | 145 | } |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 146 | }); |
Rushabh Mehta | 1828c12 | 2015-08-10 17:04:07 +0530 | [diff] [blame] | 147 | |
| 148 | erpnext.queries.setup_queries = function(frm, options, query_fn) { |
| 149 | var me = this; |
| 150 | var set_query = function(doctype, parentfield) { |
| 151 | var link_fields = frappe.meta.get_docfields(doctype, frm.doc.name, |
| 152 | {"fieldtype": "Link", "options": options}); |
| 153 | $.each(link_fields, function(i, df) { |
| 154 | if(parentfield) { |
| 155 | frm.set_query(df.fieldname, parentfield, query_fn); |
| 156 | } else { |
| 157 | frm.set_query(df.fieldname, query_fn); |
| 158 | } |
| 159 | }); |
| 160 | }; |
| 161 | |
| 162 | set_query(frm.doc.doctype); |
| 163 | |
| 164 | // warehouse field in tables |
| 165 | $.each(frappe.meta.get_docfields(frm.doc.doctype, frm.doc.name, {"fieldtype": "Table"}), |
| 166 | function(i, df) { |
| 167 | set_query(df.options, df.fieldname); |
| 168 | }); |
| 169 | } |
suyashphadtare | 049a88c | 2017-01-12 17:49:37 +0530 | [diff] [blame] | 170 | |
| 171 | /* if item code is selected in child table |
| 172 | then list down warehouses with its quantity |
| 173 | else apply default filters. |
| 174 | */ |
| 175 | erpnext.queries.setup_warehouse_query = function(frm){ |
| 176 | frm.set_query('warehouse', 'items', function(doc, cdt, cdn) { |
| 177 | var row = locals[cdt][cdn]; |
| 178 | var filters = erpnext.queries.warehouse(frm.doc); |
| 179 | if(row.item_code){ |
| 180 | $.extend(filters, {"query":"erpnext.controllers.queries.warehouse_query"}); |
| 181 | filters["filters"].push(["Bin", "item_code", "=", row.item_code]); |
| 182 | } |
| 183 | return filters |
| 184 | }); |
| 185 | } |