blob: 12307fb9b3141664968862b1dd87a747fa761418 [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) {
Anand Doshi652bc072014-04-16 15:21:46 +053039 frappe.throw(__("Please specify a") + " " +
Pratik Vyasb52618c2014-04-14 16:25:30 +053040 __(frappe.meta.get_label(doc.doctype, "customer", doc.name)));
Anand Doshi61cad502013-07-18 13:26:27 +053041 }
Anand Doshi652bc072014-04-16 15:21:46 +053042
Anand Doshi61cad502013-07-18 13:26:27 +053043 return { filters: { customer: doc.customer } };
44 },
Nabin Haitfe8db592014-01-30 12:32:47 +053045
Anand Doshid5d39ac2013-07-29 13:28:37 +053046 supplier_filter: function(doc) {
47 if(!doc.supplier) {
Anand Doshi652bc072014-04-16 15:21:46 +053048 frappe.throw(__("Please specify a") + " " +
Pratik Vyasb52618c2014-04-14 16:25:30 +053049 __(frappe.meta.get_label(doc.doctype, "supplier", doc.name)));
Anand Doshid5d39ac2013-07-29 13:28:37 +053050 }
Anand Doshi652bc072014-04-16 15:21:46 +053051
Anand Doshid5d39ac2013-07-29 13:28:37 +053052 return { filters: { supplier: doc.supplier } };
53 },
Nabin Haitfe8db592014-01-30 12:32:47 +053054
55 lead_filter: function(doc) {
56 if(!doc.lead) {
Anand Doshi652bc072014-04-16 15:21:46 +053057 frappe.throw(__("Please specify a") + " " +
Pratik Vyasb52618c2014-04-14 16:25:30 +053058 __(frappe.meta.get_label(doc.doctype, "lead", doc.name)));
Nabin Haitfe8db592014-01-30 12:32:47 +053059 }
60
61 return { filters: { lead: doc.lead } };
62 },
63
Anand Doshi61cad502013-07-18 13:26:27 +053064 not_a_group_filter: function() {
65 return { filters: { is_group: "No" } };
66 },
Anand Doshib71a18c2014-05-02 16:20:45 +053067
68 employee: function() {
69 return { query: "erpnext.controllers.queries.employee_query" }
Anand Doshifaefeaa2014-06-24 18:53:04 +053070 },
71
72 warehouse: function(doc) {
73 return {
nabinhaitd5fd5352014-07-14 12:39:50 +053074 filters: [["Warehouse", "company", "in", ["", cstr(doc.company)]]]
Anand Doshifaefeaa2014-06-24 18:53:04 +053075 }
Anand Doshib71a18c2014-05-02 16:20:45 +053076 }
Anand Doshi652bc072014-04-16 15:21:46 +053077});
Rushabh Mehta1828c122015-08-10 17:04:07 +053078
79erpnext.queries.setup_queries = function(frm, options, query_fn) {
80 var me = this;
81 var set_query = function(doctype, parentfield) {
82 var link_fields = frappe.meta.get_docfields(doctype, frm.doc.name,
83 {"fieldtype": "Link", "options": options});
84 $.each(link_fields, function(i, df) {
85 if(parentfield) {
86 frm.set_query(df.fieldname, parentfield, query_fn);
87 } else {
88 frm.set_query(df.fieldname, query_fn);
89 }
90 });
91 };
92
93 set_query(frm.doc.doctype);
94
95 // warehouse field in tables
96 $.each(frappe.meta.get_docfields(frm.doc.doctype, frm.doc.name, {"fieldtype": "Table"}),
97 function(i, df) {
98 set_query(df.options, df.fieldname);
99 });
100}