Merge pull request #2108 from neilLasrado/fix-issue-448

[Fix] Issue #448
diff --git a/erpnext/accounts/doctype/cost_center/cost_center.json b/erpnext/accounts/doctype/cost_center/cost_center.json
index 36cb6ae..e65f74b 100644
--- a/erpnext/accounts/doctype/cost_center/cost_center.json
+++ b/erpnext/accounts/doctype/cost_center/cost_center.json
@@ -144,8 +144,8 @@
  ], 
  "icon": "icon-money", 
  "idx": 1, 
- "in_create": 1, 
- "modified": "2014-05-27 03:49:08.910126", 
+ "in_create": 0, 
+ "modified": "2014-08-26 12:16:11.163750", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Cost Center", 
diff --git a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
index 2275743..d26dec0 100755
--- a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
+++ b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
@@ -228,6 +228,17 @@
    "permlevel": 0
   }, 
   {
+   "fieldname": "project_name", 
+   "fieldtype": "Link", 
+   "in_filter": 1, 
+   "in_list_view": 0, 
+   "label": "Project Name", 
+   "options": "Project", 
+   "permlevel": 0, 
+   "print_hide": 1, 
+   "read_only": 0
+  }, 
+  {
    "default": ":Company", 
    "fieldname": "cost_center", 
    "fieldtype": "Link", 
@@ -250,17 +261,6 @@
    "permlevel": 0
   }, 
   {
-   "fieldname": "project_name", 
-   "fieldtype": "Link", 
-   "in_filter": 1, 
-   "in_list_view": 0, 
-   "label": "Project Name", 
-   "options": "Project", 
-   "permlevel": 0, 
-   "print_hide": 1, 
-   "read_only": 0
-  }, 
-  {
    "fieldname": "brand", 
    "fieldtype": "Data", 
    "hidden": 1, 
@@ -318,23 +318,6 @@
    "width": "150px"
   }, 
   {
-   "allow_on_submit": 1, 
-   "fieldname": "page_break", 
-   "fieldtype": "Check", 
-   "in_list_view": 0, 
-   "label": "Page Break", 
-   "no_copy": 1, 
-   "permlevel": 0, 
-   "print_hide": 1, 
-   "read_only": 0, 
-   "report_hide": 1
-  }, 
-  {
-   "fieldname": "col_break6", 
-   "fieldtype": "Column Break", 
-   "permlevel": 0
-  }, 
-  {
    "fieldname": "purchase_order", 
    "fieldtype": "Link", 
    "in_filter": 1, 
@@ -350,6 +333,11 @@
    "search_index": 1
   }, 
   {
+   "fieldname": "col_break6", 
+   "fieldtype": "Column Break", 
+   "permlevel": 0
+  }, 
+  {
    "fieldname": "po_detail", 
    "fieldtype": "Data", 
    "hidden": 1, 
@@ -380,6 +368,18 @@
    "search_index": 1
   }, 
   {
+   "allow_on_submit": 1, 
+   "fieldname": "page_break", 
+   "fieldtype": "Check", 
+   "in_list_view": 0, 
+   "label": "Page Break", 
+   "no_copy": 1, 
+   "permlevel": 0, 
+   "print_hide": 1, 
+   "read_only": 0, 
+   "report_hide": 1
+  }, 
+  {
    "fieldname": "pr_detail", 
    "fieldtype": "Data", 
    "hidden": 1, 
@@ -421,7 +421,7 @@
  ], 
  "idx": 1, 
  "istable": 1, 
- "modified": "2014-07-24 05:50:20.570629", 
+ "modified": "2014-08-26 12:34:42.790959", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Purchase Invoice Item", 
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js
index 3c63508..c2e76e2 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.js
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.js
@@ -385,6 +385,6 @@
 		});
 	}
 });
-
+cur_frm.add_fetch('project_name', 'cost_center', 'cost_center');
 var tname = cur_frm.cscript.tname;
 var fname = cur_frm.cscript.fname;
diff --git a/erpnext/projects/doctype/project/project.json b/erpnext/projects/doctype/project/project.json
index c7c459c..f7a5754 100644
--- a/erpnext/projects/doctype/project/project.json
+++ b/erpnext/projects/doctype/project/project.json
@@ -1,5 +1,6 @@
 {
-  "allow_import": 1, 
+ "allow_attach": 1, 
+ "allow_import": 1, 
  "allow_rename": 1, 
  "autoname": "field:project_name", 
  "creation": "2013-03-07 11:55:07", 
@@ -208,6 +209,13 @@
    "search_index": 0
   }, 
   {
+   "fieldname": "cost_center", 
+   "fieldtype": "Link", 
+   "label": "Default Cost Center", 
+   "options": "Cost Center", 
+   "permlevel": 0
+  }, 
+  {
    "fieldname": "column_break0", 
    "fieldtype": "Column Break", 
    "label": "Margin", 
@@ -265,7 +273,7 @@
  "icon": "icon-puzzle-piece", 
  "idx": 1, 
  "max_attachments": 4, 
- "modified": "2014-08-04 03:22:11.416219", 
+ "modified": "2014-08-26 14:59:27.052172", 
  "modified_by": "Administrator", 
  "module": "Projects", 
  "name": "Project", 
diff --git a/erpnext/projects/doctype/project/project.py b/erpnext/projects/doctype/project/project.py
index 547e12a..88e6e12 100644
--- a/erpnext/projects/doctype/project/project.py
+++ b/erpnext/projects/doctype/project/project.py
@@ -67,3 +67,7 @@
 
 	def on_trash(self):
 		delete_events(self.doctype, self.name)
+
+@frappe.whitelist()
+def get_cost_center_name(project_name):
+	return frappe.db.get_value("Project", project_name, "cost_center")
\ No newline at end of file
diff --git a/erpnext/public/js/transaction.js b/erpnext/public/js/transaction.js
index 801ea57..d5209c1 100644
--- a/erpnext/public/js/transaction.js
+++ b/erpnext/public/js/transaction.js
@@ -151,9 +151,11 @@
 							transaction_date: me.frm.doc.transaction_date,
 							ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
 							doctype: item.doctype,
-							name: item.name
+							name: item.name,
+							project_name: item.project_name || me.frm.doc.project_name
 						}
 					},
+					
 					callback: function(r) {
 						if(!r.exc) {
 							me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 20f1028..66e935a 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -585,3 +585,18 @@
 		});
 	}
 });
+
+frappe.ui.form.on(cur_frm.doctype,"project_name", function(frm) {
+	frappe.call({
+		method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
+		args: {	project_name: frm.doc.project_name	},
+		callback: function(r, rt) {
+			if(!r.exc) { 
+				$.each(frm.doc[cur_frm.cscript.fname] || [], function(i, row) {
+					frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
+					msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message));
+				})
+			}
+		}
+	})
+})
diff --git a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
index 0222e58..69c2755 100755
--- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
+++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
@@ -301,16 +301,6 @@
    "width": "100px"
   }, 
   {
-   "default": ":Company", 
-   "depends_on": "eval:cint(sys_defaults.auto_accounting_for_stock)", 
-   "fieldname": "cost_center", 
-   "fieldtype": "Link", 
-   "label": "Cost Center", 
-   "options": "Cost Center", 
-   "permlevel": 0, 
-   "print_hide": 1
-  }, 
-  {
    "fieldname": "project_name", 
    "fieldtype": "Link", 
    "in_filter": 1, 
@@ -321,6 +311,16 @@
    "read_only": 0
   }, 
   {
+   "default": ":Company", 
+   "depends_on": "eval:cint(sys_defaults.auto_accounting_for_stock)", 
+   "fieldname": "cost_center", 
+   "fieldtype": "Link", 
+   "label": "Cost Center", 
+   "options": "Cost Center", 
+   "permlevel": 0, 
+   "print_hide": 1
+  }, 
+  {
    "fieldname": "qa_no", 
    "fieldtype": "Link", 
    "label": "Quality Inspection", 
@@ -557,7 +557,7 @@
  ], 
  "idx": 1, 
  "istable": 1, 
- "modified": "2014-08-11 12:48:13.509109", 
+ "modified": "2014-08-26 12:38:04.065982", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Purchase Receipt Item", 
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index efb793d..fffb52e 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -29,6 +29,7 @@
 			"is_subcontracted": "Yes" / "No",
 			"transaction_type": "selling",
 			"ignore_pricing_rule": 0/1
+			"project_name": "",
 		}
 	"""
 	args = process_args(args)
@@ -149,7 +150,8 @@
 			or args.expense_account
 			or frappe.db.get_value("Item Group", item.item_group, "default_expense_account")
 			or frappe.db.get_value("Company", args.company, "default_expense_account")),
-		"cost_center": ((item.selling_cost_center if args.transaction_type == "selling" else item.buying_cost_center)
+		"cost_center": (frappe.db.get_value("Project", args.project_name, "cost_center")
+			or (item.selling_cost_center if args.transaction_type == "selling" else item.buying_cost_center)
 			or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")
 			or frappe.db.get_value("Company", args.company, "cost_center")),
 		"batch_no": None,