blob: 1141f99623b0f76e26ce369a3e4a3ed7d9eecc5a [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) {
Rushabh Mehtab92087c2017-01-13 18:53:11 +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]) {
Rushabh Mehtab92087c2017-01-13 18:53:11 +053048 frappe.throw(__("Please set {0}", __(frappe.meta.get_label(doc.doctype,
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053049 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]) {
Rushabh Mehtab92087c2017-01-13 18:53:11 +053061 frappe.throw(__("Please set {0}", __(frappe.meta.get_label(doc.doctype,
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053062 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
Anand Doshid5d39ac2013-07-29 13:28:37 +053071 supplier_filter: function(doc) {
72 if(!doc.supplier) {
Rushabh Mehtab92087c2017-01-13 18:53:11 +053073 frappe.throw(__("Please set {0}", __(frappe.meta.get_label(doc.doctype, "supplier", doc.name))));
Anand Doshid5d39ac2013-07-29 13:28:37 +053074 }
Anand Doshi652bc072014-04-16 15:21:46 +053075
Anand Doshid5d39ac2013-07-29 13:28:37 +053076 return { filters: { supplier: doc.supplier } };
77 },
Nabin Haitfe8db592014-01-30 12:32:47 +053078
79 lead_filter: function(doc) {
80 if(!doc.lead) {
Anand Doshi652bc072014-04-16 15:21:46 +053081 frappe.throw(__("Please specify a") + " " +
Pratik Vyasb52618c2014-04-14 16:25:30 +053082 __(frappe.meta.get_label(doc.doctype, "lead", doc.name)));
Nabin Haitfe8db592014-01-30 12:32:47 +053083 }
84
85 return { filters: { lead: doc.lead } };
86 },
87
Anand Doshi61cad502013-07-18 13:26:27 +053088 not_a_group_filter: function() {
Saurabh6bc7b892016-07-14 14:03:19 +053089 return { filters: { is_group: 0 } };
Anand Doshi61cad502013-07-18 13:26:27 +053090 },
Anand Doshib71a18c2014-05-02 16:20:45 +053091
92 employee: function() {
93 return { query: "erpnext.controllers.queries.employee_query" }
Anand Doshifaefeaa2014-06-24 18:53:04 +053094 },
95
96 warehouse: function(doc) {
97 return {
Saurabh3d6aecd2016-06-20 17:25:45 +053098 filters: [
99 ["Warehouse", "company", "in", ["", cstr(doc.company)]],
Saurabh93d68ac2016-06-26 22:50:11 +0530100 ["Warehouse", "is_group", "=",0]
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530101
Saurabh3d6aecd2016-06-20 17:25:45 +0530102 ]
Anand Doshifaefeaa2014-06-24 18:53:04 +0530103 }
Anand Doshib71a18c2014-05-02 16:20:45 +0530104 }
Anand Doshi652bc072014-04-16 15:21:46 +0530105});
Rushabh Mehta1828c122015-08-10 17:04:07 +0530106
107erpnext.queries.setup_queries = function(frm, options, query_fn) {
108 var me = this;
109 var set_query = function(doctype, parentfield) {
110 var link_fields = frappe.meta.get_docfields(doctype, frm.doc.name,
111 {"fieldtype": "Link", "options": options});
112 $.each(link_fields, function(i, df) {
113 if(parentfield) {
114 frm.set_query(df.fieldname, parentfield, query_fn);
115 } else {
116 frm.set_query(df.fieldname, query_fn);
117 }
118 });
119 };
120
121 set_query(frm.doc.doctype);
122
123 // warehouse field in tables
124 $.each(frappe.meta.get_docfields(frm.doc.doctype, frm.doc.name, {"fieldtype": "Table"}),
125 function(i, df) {
126 set_query(df.options, df.fieldname);
127 });
128}