Merge pull request #7529 from rohitwaghchaure/budget_issue

[Fix] Budget not working properly
diff --git a/erpnext/accounts/doctype/budget/budget.js b/erpnext/accounts/doctype/budget/budget.js
index 6697b17..cadf1e7 100644
--- a/erpnext/accounts/doctype/budget/budget.js
+++ b/erpnext/accounts/doctype/budget/budget.js
@@ -43,9 +43,18 @@
 	},
 
 	budget_against: function(frm) {
+		frm.trigger("set_null_value")
 		frm.trigger("toggle_reqd_fields")
 	},
 
+	set_null_value: function(frm) {
+		if(frm.doc.budget_against == 'Cost Center') {
+			frm.set_value('project', null)
+		} else {
+			frm.set_value('cost_center', null)
+		}
+	},
+
 	toggle_reqd_fields: function(frm) {
 		frm.toggle_reqd("cost_center", frm.doc.budget_against=="Cost Center");
 		frm.toggle_reqd("project", frm.doc.budget_against=="Project");
diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py
index 15d6f10..a6348f1 100644
--- a/erpnext/accounts/doctype/budget/budget.py
+++ b/erpnext/accounts/doctype/budget/budget.py
@@ -22,6 +22,7 @@
 			frappe.throw(_("{0} is mandatory").format(self.budget_against))
 		self.validate_duplicate()
 		self.validate_accounts()
+		self.set_null_value()
 
 	def validate_duplicate(self):
 		budget_against_field = frappe.scrub(self.budget_against)
@@ -54,25 +55,31 @@
 				else:
 					account_list.append(d.account)
 
+	def set_null_value(self):
+		if self.budget_against == 'Cost Center':
+			self.project = None
+		else:
+			self.cost_center = None
+
 def validate_expense_against_budget(args):
 	args = frappe._dict(args)
 	if not args.cost_center and not args.project:
 		return
-	for budget_against in [args.project, args.cost_center]:
-		if budget_against \
+	for budget_against in ['project', 'cost_center']:
+		if args.get(budget_against) \
 				and frappe.db.get_value("Account", {"name": args.account, "root_type": "Expense"}):
 
-			if args.project:
+			if args.project and budget_against == 'project':
 				condition = "and b.project='%s'" % frappe.db.escape(args.project)
 				args.budget_against_field = "Project"
 			
-			elif args.cost_center:
+			elif args.cost_center and budget_against == 'cost_center':
 				cc_lft, cc_rgt = frappe.db.get_value("Cost Center", args.cost_center, ["lft", "rgt"])
 				condition = """and exists(select name from `tabCost Center` 
 					where lft<=%s and rgt>=%s and name=b.cost_center)""" % (cc_lft, cc_rgt)
 				args.budget_against_field = "Cost Center"
-			
-			args.budget_against = budget_against
+
+			args.budget_against = args.get(budget_against)
 
 			budget_records = frappe.db.sql("""
 				select
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 462f198..e600a57 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -362,3 +362,4 @@
 erpnext.patches.v7_2.update_website_for_variant
 erpnext.patches.v7_2.update_doctype_status
 erpnext.patches.v7_2.update_salary_slips
+erpnext.patches.v7_2.set_null_value_to_fields
\ No newline at end of file
diff --git a/erpnext/patches/v7_2/set_null_value_to_fields.py b/erpnext/patches/v7_2/set_null_value_to_fields.py
new file mode 100644
index 0000000..6388be4
--- /dev/null
+++ b/erpnext/patches/v7_2/set_null_value_to_fields.py
@@ -0,0 +1,11 @@
+# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	fields = {"Cost Center": "project", "Project": "cost_center"}
+	for budget_against, field in fields.items():
+		frappe.db.sql(""" update `tabBudget` set {field} = null
+			where budget_against = %s """.format(field = field), budget_against)