Merge pull request #3192 from neilLasrado/purchase-costing

Purchase costing in Projects
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
index 03ed987..edbefba 100755
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
@@ -6,21 +6,6 @@
  "doctype": "DocType", 
  "fields": [
   {
-   "fieldname": "supplier_section", 
-   "fieldtype": "Section Break", 
-   "label": "", 
-   "options": "icon-user", 
-   "permlevel": 0
-  }, 
-  {
-   "fieldname": "column_break0", 
-   "fieldtype": "Column Break", 
-   "oldfieldtype": "Column Break", 
-   "permlevel": 0, 
-   "read_only": 0, 
-   "width": "50%"
-  }, 
-  {
    "fieldname": "naming_series", 
    "fieldtype": "Select", 
    "label": "Series", 
@@ -953,7 +938,7 @@
  "icon": "icon-file-text", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2015-04-27 20:32:12.436976", 
+ "modified": "2015-04-30 03:05:13.790265", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Purchase Invoice", 
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 094d30c..c0ebf68 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -239,6 +239,7 @@
 		self.update_against_document_in_jv()
 		self.update_prevdoc_status()
 		self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
+		self.update_project()
 
 	def make_gl_entries(self):
 		auto_accounting_for_stock = \
@@ -373,9 +374,17 @@
 		self.update_prevdoc_status()
 		self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
 		self.make_gl_entries_on_cancel()
-
-	def on_update(self):
-		pass
+		self.update_project()
+		
+	def update_project(self):
+		project_list = []
+		for d in self.items:
+			if d.project_name and d.project_name not in project_list:
+				project = frappe.get_doc("Project", d.project_name)
+				project.flags.dont_sync_tasks = True
+				project.update_purchase_costing()
+				project.save()
+				project_list.append(d.project_name)
 
 @frappe.whitelist()
 def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index 5ae47d7..7f46b08 100644
--- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -6,7 +6,6 @@
 import unittest
 import frappe
 import frappe.model
-import json
 from frappe.utils import cint
 import frappe.defaults
 from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory, \
@@ -234,5 +233,47 @@
 	def test_recurring_invoice(self):
 		from erpnext.controllers.tests.test_recurring_document import test_recurring_document
 		test_recurring_document(self, test_records)
+		
+	def test_total_purchase_cost_for_project(self):		
+		purchase_invoice = frappe.new_doc('Purchase Invoice')
+		purchase_invoice.update({
+			"credit_to": "_Test Payable - _TC",
+			"supplier": "_Test Supplier",
+			"company": "_Test Company",
+			"items": [
+				{
+					"rate": 500,
+					"qty": 1,
+					"project_name": "_Test Project",
+					"item_code": "_Test Item Home Desktop 100",
+					"expense_account": "_Test Account Cost for Goods Sold - _TC",
+					"cost_center": "_Test Cost Center - _TC"
+				},
+				{
+					"rate": 1500,
+					"qty": 1,
+					"project_name": "_Test Project",
+					"item_code": "_Test Item Home Desktop 200",
+					"expense_account": "_Test Account Cost for Goods Sold - _TC",
+					"cost_center": "_Test Cost Center - _TC"
+				}
+			]
+		})
+		purchase_invoice.save()
+		purchase_invoice.submit()		
+		self.assertEqual(frappe.db.get_value("Project", "_Test Project", "total_purchase_cost"), 2000)
+		
+		purchase_invoice1 = frappe.copy_doc(purchase_invoice)
+		purchase_invoice1.save()
+		purchase_invoice1.submit()
+		
+		self.assertEqual(frappe.db.get_value("Project", "_Test Project", "total_purchase_cost"), 4000)
+		
+		purchase_invoice1.cancel()		
+		self.assertEqual(frappe.db.get_value("Project", "_Test Project", "total_purchase_cost"), 2000)
+		
+		purchase_invoice.cancel()		
+		self.assertEqual(frappe.db.get_value("Project", "_Test Project", "total_purchase_cost"), 0)
+		
 
 test_records = frappe.get_test_records('Purchase Invoice')
diff --git a/erpnext/projects/doctype/project/project.json b/erpnext/projects/doctype/project/project.json
index 4db087b..2aca991 100644
--- a/erpnext/projects/doctype/project/project.json
+++ b/erpnext/projects/doctype/project/project.json
@@ -304,6 +304,15 @@
    "read_only": 1
   }, 
   {
+   "fieldname": "total_purchase_cost", 
+   "fieldtype": "Currency", 
+   "hidden": 0, 
+   "label": "Total Purchase Cost (via Purchase Invoice)", 
+   "permlevel": 0, 
+   "precision": "", 
+   "read_only": 1
+  }, 
+  {
    "fieldname": "margin", 
    "fieldtype": "Section Break", 
    "label": "", 
@@ -347,7 +356,7 @@
  "icon": "icon-puzzle-piece", 
  "idx": 1, 
  "max_attachments": 4, 
- "modified": "2015-04-23 05:51:53.642253", 
+ "modified": "2015-04-27 07:37:44.239930", 
  "modified_by": "Administrator", 
  "module": "Projects", 
  "name": "Project", 
diff --git a/erpnext/projects/doctype/project/project.py b/erpnext/projects/doctype/project/project.py
index d8513ff..c914f22 100644
--- a/erpnext/projects/doctype/project/project.py
+++ b/erpnext/projects/doctype/project/project.py
@@ -89,6 +89,10 @@
 		self.gross_margin = flt(total_cost.billing_amount) - flt(total_cost.costing_amount)
 		if self.total_billing_amount:
 			self.per_gross_margin = (self.gross_margin / flt(self.total_billing_amount)) *100
+			
+	def update_purchase_costing(self):
+		self.total_purchase_cost = frappe.db.sql("""select sum(amount) as cost
+			from `tabPurchase Invoice Item` where project_name = %s and docstatus=1 """, self.name, as_dict=1)[0].cost or 0
 
 @frappe.whitelist()
 def get_cost_center_name(project_name):