Merge pull request #1842 from anandpdoshi/anand-wip

Warehouse query filtered by Company, Fixed Gantt Chart query for task
diff --git a/erpnext/controllers/queries.py b/erpnext/controllers/queries.py
index 0f1d5f6..16d27d4 100644
--- a/erpnext/controllers/queries.py
+++ b/erpnext/controllers/queries.py
@@ -254,3 +254,4 @@
 	return frappe.widgets.reportview.execute("Account", filters = filter_list,
 		fields = ["name", "parent_account"],
 		limit_start=start, limit_page_length=page_len, as_list=True)
+
diff --git a/erpnext/projects/doctype/task/task.py b/erpnext/projects/doctype/task/task.py
index 7fbaa45..6b0b237 100644
--- a/erpnext/projects/doctype/task/task.py
+++ b/erpnext/projects/doctype/task/task.py
@@ -52,7 +52,7 @@
 		frappe.msgprint(_("No Permission"), raise_exception=1)
 
 	conditions = build_match_conditions("Task")
-	conditions and (" and " + conditions) or ""
+	conditions = conditions and (" and " + conditions) or ""
 
 	if filters:
 		filters = json.loads(filters)
diff --git a/erpnext/public/js/controllers/stock_controller.js b/erpnext/public/js/controllers/stock_controller.js
index e50b2af..97bcb6c 100644
--- a/erpnext/public/js/controllers/stock_controller.js
+++ b/erpnext/public/js/controllers/stock_controller.js
@@ -4,6 +4,37 @@
 frappe.provide("erpnext.stock");
 
 erpnext.stock.StockController = frappe.ui.form.Controller.extend({
+	onload: function() {
+		// warehouse query if company
+		if (this.frm.fields_dict.company) {
+			this.setup_warehouse_query();
+		}
+	},
+
+	setup_warehouse_query: function() {
+		var me = this;
+
+		var _set_warehouse_query = function(doctype, parentfield) {
+			var warehouse_link_fields = frappe.meta.get_docfields(doctype, me.frm.doc.name,
+				{"fieldtype": "Link", "options": "Warehouse"});
+			$.each(warehouse_link_fields, function(i, df) {
+				me.frm.set_query(df.fieldname, parentfield, function() {
+					return erpnext.queries.warehouse(me.frm.doc);
+				})
+			});
+		};
+
+		_set_warehouse_query(me.frm.doc.doctype);
+
+		// warehouse field in tables
+		var table_fields = frappe.meta.get_docfields(me.frm.doc.doctype, me.frm.doc.name,
+			{"fieldtype": "Table"});
+
+		$.each(table_fields, function(i, df) {
+			_set_warehouse_query(df.options, df.fieldname);
+		});
+	},
+
 	show_stock_ledger: function() {
 		var me = this;
 		if(this.frm.doc.docstatus===1) {
@@ -17,12 +48,12 @@
 				frappe.set_route("query-report", "Stock Ledger");
 			}, "icon-bar-chart");
 		}
-		
+
 	},
 
 	show_general_ledger: function() {
 		var me = this;
-		if(this.frm.doc.docstatus===1 && cint(frappe.defaults.get_default("auto_accounting_for_stock"))) { 
+		if(this.frm.doc.docstatus===1 && cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
 			cur_frm.appframe.add_button(__('Accounting Ledger'), function() {
 				frappe.route_options = {
 					voucher_no: me.frm.doc.name,
@@ -46,4 +77,4 @@
 		}
 		refresh_field(this.frm.cscript.fname);
 	}
-});
\ No newline at end of file
+});
diff --git a/erpnext/public/js/queries.js b/erpnext/public/js/queries.js
index 1f404db..b57b765 100644
--- a/erpnext/public/js/queries.js
+++ b/erpnext/public/js/queries.js
@@ -67,5 +67,11 @@
 
 	employee: function() {
 		return { query: "erpnext.controllers.queries.employee_query" }
+	},
+
+	warehouse: function(doc) {
+		return {
+			filters: [["Warehouse", "company", "in", ["", doc.company]]]
+		}
 	}
 });
diff --git a/erpnext/public/js/transaction.js b/erpnext/public/js/transaction.js
index ea576d5..a4b1abb 100644
--- a/erpnext/public/js/transaction.js
+++ b/erpnext/public/js/transaction.js
@@ -7,6 +7,8 @@
 erpnext.TransactionController = erpnext.stock.StockController.extend({
 	onload: function() {
 		var me = this;
+		this._super();
+
 		if(this.frm.doc.__islocal) {
 			var today = get_today(),
 				currency = frappe.defaults.get_user_default("currency");