blob: bf7143a10ecd01a64c4c0140d6af20a2ecb366fe [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 {
53 query: 'frappe.email.doctype.contact.contact.contact_query',
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053054 filters: { link_doctype: frappe.dynamic_link.doctype, link_name: doc[frappe.dynamic_link.fieldname] } };
Rushabh Mehtab92087c2017-01-13 18:53:11 +053055 }
56 },
57
58 address_query: function(doc) {
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053059 if(frappe.dynamic_link) {
60 if(!doc[frappe.dynamic_link.fieldname]) {
Makarand Bauskard893efd2017-01-30 12:35:34 +053061 frappe.throw(__("Please set {0}",
62 [__(frappe.meta.get_label(doc.doctype, frappe.dynamic_link.fieldname, doc.name))]));
Rushabh Mehtab92087c2017-01-13 18:53:11 +053063 }
64
65 return {
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053066 query: 'frappe.geo.doctype.address.address.address_query',
67 filters: { link_doctype: frappe.dynamic_link.doctype, link_name: doc[frappe.dynamic_link.fieldname] } };
Rushabh Mehtab92087c2017-01-13 18:53:11 +053068 }
69 },
70
mbauskar8271b4c2017-01-20 14:47:15 +053071 company_address_query: function(doc) {
72 return {
73 query: 'frappe.geo.doctype.address.address.address_query',
74 filters: { is_your_company_address: 1, link_doctype: 'Company', link_name: doc.company || '' }
75 };
76 },
77
Anand Doshid5d39ac2013-07-29 13:28:37 +053078 supplier_filter: function(doc) {
79 if(!doc.supplier) {
Makarand Bauskard893efd2017-01-30 12:35:34 +053080 frappe.throw(__("Please set {0}", [__(frappe.meta.get_label(doc.doctype, "supplier", doc.name))]));
Anand Doshid5d39ac2013-07-29 13:28:37 +053081 }
Anand Doshi652bc072014-04-16 15:21:46 +053082
Anand Doshid5d39ac2013-07-29 13:28:37 +053083 return { filters: { supplier: doc.supplier } };
84 },
Nabin Haitfe8db592014-01-30 12:32:47 +053085
86 lead_filter: function(doc) {
87 if(!doc.lead) {
Makarand Bauskard893efd2017-01-30 12:35:34 +053088 frappe.throw(__("Please specify a {0}",
89 [__(frappe.meta.get_label(doc.doctype, "lead", doc.name))]));
Nabin Haitfe8db592014-01-30 12:32:47 +053090 }
91
92 return { filters: { lead: doc.lead } };
93 },
94
Anand Doshi61cad502013-07-18 13:26:27 +053095 not_a_group_filter: function() {
Saurabh6bc7b892016-07-14 14:03:19 +053096 return { filters: { is_group: 0 } };
Anand Doshi61cad502013-07-18 13:26:27 +053097 },
Anand Doshib71a18c2014-05-02 16:20:45 +053098
99 employee: function() {
100 return { query: "erpnext.controllers.queries.employee_query" }
Anand Doshifaefeaa2014-06-24 18:53:04 +0530101 },
102
103 warehouse: function(doc) {
104 return {
Saurabh3d6aecd2016-06-20 17:25:45 +0530105 filters: [
106 ["Warehouse", "company", "in", ["", cstr(doc.company)]],
Saurabh93d68ac2016-06-26 22:50:11 +0530107 ["Warehouse", "is_group", "=",0]
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530108
Saurabh3d6aecd2016-06-20 17:25:45 +0530109 ]
Anand Doshifaefeaa2014-06-24 18:53:04 +0530110 }
Anand Doshib71a18c2014-05-02 16:20:45 +0530111 }
Anand Doshi652bc072014-04-16 15:21:46 +0530112});
Rushabh Mehta1828c122015-08-10 17:04:07 +0530113
114erpnext.queries.setup_queries = function(frm, options, query_fn) {
115 var me = this;
116 var set_query = function(doctype, parentfield) {
117 var link_fields = frappe.meta.get_docfields(doctype, frm.doc.name,
118 {"fieldtype": "Link", "options": options});
119 $.each(link_fields, function(i, df) {
120 if(parentfield) {
121 frm.set_query(df.fieldname, parentfield, query_fn);
122 } else {
123 frm.set_query(df.fieldname, query_fn);
124 }
125 });
126 };
127
128 set_query(frm.doc.doctype);
129
130 // warehouse field in tables
131 $.each(frappe.meta.get_docfields(frm.doc.doctype, frm.doc.name, {"fieldtype": "Table"}),
132 function(i, df) {
133 set_query(df.options, df.fieldname);
134 });
135}
suyashphadtare049a88c2017-01-12 17:49:37 +0530136
137/* if item code is selected in child table
138 then list down warehouses with its quantity
139 else apply default filters.
140*/
141erpnext.queries.setup_warehouse_query = function(frm){
142 frm.set_query('warehouse', 'items', function(doc, cdt, cdn) {
143 var row = locals[cdt][cdn];
144 var filters = erpnext.queries.warehouse(frm.doc);
145 if(row.item_code){
146 $.extend(filters, {"query":"erpnext.controllers.queries.warehouse_query"});
147 filters["filters"].push(["Bin", "item_code", "=", row.item_code]);
148 }
149 return filters
150 });
151}