blob: b7d880ae408ef3bbb7bf8ec28cbcda641433444c [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta5e7ec202013-01-11 11:15:27 +05303
Rushabh Mehta7c932002014-03-11 16:15:05 +05304// searches for enabled users
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305frappe.provide("erpnext.queries");
Anand Doshi61cad502013-07-18 13:26:27 +05306$.extend(erpnext.queries, {
Rushabh Mehta7c932002014-03-11 16:15:05 +05307 user: function() {
8 return { query: "frappe.core.doctype.user.user.user_query" };
Anand Doshi61cad502013-07-18 13:26:27 +05309 },
Nabin Haitfe8db592014-01-30 12:32:47 +053010
Anand Doshi61cad502013-07-18 13:26:27 +053011 lead: function() {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053012 return { query: "erpnext.controllers.queries.lead_query" };
Anand Doshi61cad502013-07-18 13:26:27 +053013 },
Nabin Haitfe8db592014-01-30 12:32:47 +053014
Anand Doshi61cad502013-07-18 13:26:27 +053015 customer: function() {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053016 return { query: "erpnext.controllers.queries.customer_query" };
Anand Doshi61cad502013-07-18 13:26:27 +053017 },
Nabin Haitfe8db592014-01-30 12:32:47 +053018
Anand Doshi61cad502013-07-18 13:26:27 +053019 supplier: function() {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053020 return { query: "erpnext.controllers.queries.supplier_query" };
Anand Doshi61cad502013-07-18 13:26:27 +053021 },
Nabin Haitfe8db592014-01-30 12:32:47 +053022
Anand Doshi652bc072014-04-16 15:21:46 +053023 item: function(filters) {
24 var args = { query: "erpnext.controllers.queries.item_query" };
25 if(filters) args["filters"] = filters;
26 return args;
Anand Doshi61cad502013-07-18 13:26:27 +053027 },
Nabin Haitfe8db592014-01-30 12:32:47 +053028
Anand Doshi61cad502013-07-18 13:26:27 +053029 bom: function() {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053030 return { query: "erpnext.controllers.queries.bom" };
Anand Doshi61cad502013-07-18 13:26:27 +053031 },
Nabin Haitfe8db592014-01-30 12:32:47 +053032
Anand Doshi61cad502013-07-18 13:26:27 +053033 task: function() {
Akhilesh Darjee4cdb7992014-01-30 13:56:57 +053034 return { query: "erpnext.projects.utils.query_task" };
Anand Doshi61cad502013-07-18 13:26:27 +053035 },
Nabin Haitfe8db592014-01-30 12:32:47 +053036
Anand Doshi61cad502013-07-18 13:26:27 +053037 customer_filter: function(doc) {
38 if(!doc.customer) {
Makarand Bauskard893efd2017-01-30 12:35:34 +053039 frappe.throw(__("Please set {0}", [__(frappe.meta.get_label(doc.doctype, "customer", doc.name))]));
Anand Doshi61cad502013-07-18 13:26:27 +053040 }
Anand Doshi652bc072014-04-16 15:21:46 +053041
Anand Doshi61cad502013-07-18 13:26:27 +053042 return { filters: { customer: doc.customer } };
43 },
Nabin Haitfe8db592014-01-30 12:32:47 +053044
Rushabh Mehtab92087c2017-01-13 18:53:11 +053045 contact_query: function(doc) {
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053046 if(frappe.dynamic_link) {
47 if(!doc[frappe.dynamic_link.fieldname]) {
Makarand Bauskard893efd2017-01-30 12:35:34 +053048 frappe.throw(__("Please set {0}",
49 [__(frappe.meta.get_label(doc.doctype, frappe.dynamic_link.fieldname, doc.name))]));
Rushabh Mehtab92087c2017-01-13 18:53:11 +053050 }
51
52 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053053 query: 'frappe.contacts.doctype.contact.contact.contact_query',
Rushabh Mehta919a74a2017-06-22 16:37:04 +053054 filters: {
55 link_doctype: frappe.dynamic_link.doctype,
56 link_name: doc[frappe.dynamic_link.fieldname]
57 }
58 };
Rushabh Mehtab92087c2017-01-13 18:53:11 +053059 }
60 },
61
62 address_query: function(doc) {
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053063 if(frappe.dynamic_link) {
64 if(!doc[frappe.dynamic_link.fieldname]) {
Rushabh Mehta919a74a2017-06-22 16:37:04 +053065 frappe.throw(__("Please set {0}",
Makarand Bauskard893efd2017-01-30 12:35:34 +053066 [__(frappe.meta.get_label(doc.doctype, frappe.dynamic_link.fieldname, doc.name))]));
Rushabh Mehtab92087c2017-01-13 18:53:11 +053067 }
Deepesh Garg6e2c13f2019-12-10 15:55:05 +053068
Rushabh Mehtab92087c2017-01-13 18:53:11 +053069 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053070 query: 'frappe.contacts.doctype.address.address.address_query',
Rushabh Mehta919a74a2017-06-22 16:37:04 +053071 filters: {
72 link_doctype: frappe.dynamic_link.doctype,
73 link_name: doc[frappe.dynamic_link.fieldname]
74 }
75 };
Rushabh Mehtab92087c2017-01-13 18:53:11 +053076 }
77 },
78
mbauskar8271b4c2017-01-20 14:47:15 +053079 company_address_query: function(doc) {
80 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053081 query: 'frappe.contacts.doctype.address.address.address_query',
mbauskar8271b4c2017-01-20 14:47:15 +053082 filters: { is_your_company_address: 1, link_doctype: 'Company', link_name: doc.company || '' }
83 };
84 },
85
Subin Tom98d41762021-12-21 12:35:44 +053086 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 Doshid5d39ac2013-07-29 13:28:37 +053093 supplier_filter: function(doc) {
94 if(!doc.supplier) {
Makarand Bauskard893efd2017-01-30 12:35:34 +053095 frappe.throw(__("Please set {0}", [__(frappe.meta.get_label(doc.doctype, "supplier", doc.name))]));
Anand Doshid5d39ac2013-07-29 13:28:37 +053096 }
Anand Doshi652bc072014-04-16 15:21:46 +053097
Anand Doshid5d39ac2013-07-29 13:28:37 +053098 return { filters: { supplier: doc.supplier } };
99 },
Nabin Haitfe8db592014-01-30 12:32:47 +0530100
101 lead_filter: function(doc) {
102 if(!doc.lead) {
Rushabh Mehta919a74a2017-06-22 16:37:04 +0530103 frappe.throw(__("Please specify a {0}",
Makarand Bauskard893efd2017-01-30 12:35:34 +0530104 [__(frappe.meta.get_label(doc.doctype, "lead", doc.name))]));
Nabin Haitfe8db592014-01-30 12:32:47 +0530105 }
106
107 return { filters: { lead: doc.lead } };
108 },
109
Anand Doshi61cad502013-07-18 13:26:27 +0530110 not_a_group_filter: function() {
Saurabh6bc7b892016-07-14 14:03:19 +0530111 return { filters: { is_group: 0 } };
Anand Doshi61cad502013-07-18 13:26:27 +0530112 },
Anand Doshib71a18c2014-05-02 16:20:45 +0530113
114 employee: function() {
115 return { query: "erpnext.controllers.queries.employee_query" }
Anand Doshifaefeaa2014-06-24 18:53:04 +0530116 },
117
118 warehouse: function(doc) {
119 return {
Saurabh3d6aecd2016-06-20 17:25:45 +0530120 filters: [
121 ["Warehouse", "company", "in", ["", cstr(doc.company)]],
Saurabh93d68ac2016-06-26 22:50:11 +0530122 ["Warehouse", "is_group", "=",0]
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530123
Saurabh3d6aecd2016-06-20 17:25:45 +0530124 ]
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530125 };
Deepesh Garg6e5748e2020-11-15 22:43:48 +0530126 },
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 Garg9e9ea962020-11-16 12:03:47 +0530144 };
Anand Doshib71a18c2014-05-02 16:20:45 +0530145 }
Anand Doshi652bc072014-04-16 15:21:46 +0530146});
Rushabh Mehta1828c122015-08-10 17:04:07 +0530147
148erpnext.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}
suyashphadtare049a88c2017-01-12 17:49:37 +0530170
171/* if item code is selected in child table
172 then list down warehouses with its quantity
173 else apply default filters.
174*/
175erpnext.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}