Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/doctype/sales_invoice/sales_invoice.txt b/accounts/doctype/sales_invoice/sales_invoice.txt
index dbdf42f..2a2a2e1 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.txt
+++ b/accounts/doctype/sales_invoice/sales_invoice.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-24 19:29:05", 
   "docstatus": 0, 
-  "modified": "2013-08-09 14:45:42", 
+  "modified": "2013-08-31 10:19:01", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -179,7 +179,6 @@
   "search_index": 1
  }, 
  {
-  "default": "Today", 
   "description": "Enter the date by which payments from customer is expected against this invoice.", 
   "doctype": "DocField", 
   "fieldname": "due_date", 
diff --git a/accounts/report/gross_profit/gross_profit.py b/accounts/report/gross_profit/gross_profit.py
index 3aba234..590babb 100644
--- a/accounts/report/gross_profit/gross_profit.py
+++ b/accounts/report/gross_profit/gross_profit.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import webnotes
 from webnotes.utils import flt
-from stock.utils import get_buying_amount
+from stock.utils import get_buying_amount, get_sales_bom_buying_amount
 
 def execute(filters=None):
 	if not filters: filters = {}
@@ -21,10 +21,15 @@
 	data = []
 	for row in source:
 		selling_amount = flt(row.amount)
-
-		buying_amount = get_buying_amount(row.item_code, row.parenttype, row.name, row.item_row,
-			stock_ledger_entries.get((row.item_code, row.warehouse), []), 
-			item_sales_bom.get(row.parenttype, {}).get(row.name, webnotes._dict()))
+		
+		item_sales_bom_map = item_sales_bom.get(row.parenttype, {}).get(row.name, webnotes._dict())
+		
+		if item_sales_bom_map.get(row.item_code):
+			buying_amount = get_sales_bom_buying_amount(row.item_code, row.warehouse, 
+				row.parenttype, row.name, row.item_row, stock_ledger_entries, item_sales_bom_map)
+		else:
+			buying_amount = get_buying_amount(row.parenttype, row.name, row.item_row,
+				stock_ledger_entries.get((row.item_code, row.warehouse), []))
 		
 		buying_amount = buying_amount > 0 and buying_amount or 0
 
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index 4b4b1c7..a13d747 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -85,7 +85,7 @@
 				self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)
 
 	def set_buying_amount(self, stock_ledger_entries = None):
-		from stock.utils import get_buying_amount
+		from stock.utils import get_buying_amount, get_sales_bom_buying_amount
 		if not stock_ledger_entries:
 			stock_ledger_entries = self.get_stock_ledger_entries()
 
@@ -99,13 +99,18 @@
 			for item in self.doclist.get({"parentfield": self.fname}):
 				if item.item_code in self.stock_items or \
 						(item_sales_bom and item_sales_bom.get(item.item_code)):
-					buying_amount = get_buying_amount(item.item_code, self.doc.doctype, self.doc.name, item.name, 
-						stock_ledger_entries.get((item.item_code, item.warehouse), []), 
-						item_sales_bom)
+					if item.item_code in self.stock_items:
+						buying_amount = get_buying_amount(self.doc.doctype, self.doc.name, 
+							item.name, stock_ledger_entries.get((item.item_code, 
+								item.warehouse), []))
+					elif item_sales_bom and item_sales_bom.get(item.item_code):
+						buying_amount = get_sales_bom_buying_amount(item.item_code, item.warehouse, 
+							self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, 
+							item_sales_bom)
 					
-					item.buying_amount = buying_amount >= 0.01 and buying_amount or 0
-					webnotes.conn.set_value(item.doctype, item.name, "buying_amount", 
-						item.buying_amount)
+						item.buying_amount = buying_amount >= 0.01 and buying_amount or 0
+						webnotes.conn.set_value(item.doctype, item.name, "buying_amount", 
+							item.buying_amount)
 						
 	def check_expense_account(self, item):
 		if item.buying_amount and not item.expense_account:
diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.py b/stock/doctype/stock_reconciliation/stock_reconciliation.py
index 617ec69..dbb0a34 100644
--- a/stock/doctype/stock_reconciliation/stock_reconciliation.py
+++ b/stock/doctype/stock_reconciliation/stock_reconciliation.py
@@ -293,7 +293,7 @@
 		
 		self.doc.stock_value_difference = 0.0
 		for d in self.entries:
-			self.doc.stock_value_difference -= get_buying_amount(d.item_code, self.doc.doctype, self.doc.name,
+			self.doc.stock_value_difference -= get_buying_amount(self.doc.doctype, self.doc.name,
 				d.voucher_detail_no, stock_ledger_entries.get((d.item_code, d.warehouse), []))
 		webnotes.conn.set(self.doc, "stock_value_difference", self.doc.stock_value_difference)
 		
diff --git a/stock/utils.py b/stock/utils.py
index f04b663..52471e4 100644
--- a/stock/utils.py
+++ b/stock/utils.py
@@ -164,20 +164,18 @@
 		webnotes.throw(_("Not allowed entry in Warehouse") \
 			+ ": " + warehouse, UserNotAllowedForWarehouse)
 
-def get_buying_amount(item_code, voucher_type, voucher_no, voucher_detail_no, 
-		stock_ledger_entries, item_sales_bom=None):
-	if item_sales_bom and item_sales_bom.get(item_code):
-		# sales bom item
-		buying_amount = 0.0
-		for bom_item in item_sales_bom[item_code]:
-			if bom_item.get("parent_detail_docname")==voucher_detail_no:
-				buying_amount += _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, stock_ledger_entries)
-		return buying_amount
-	else:
-		# doesn't have sales bom
-		return _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, stock_ledger_entries)
+def get_sales_bom_buying_amount(item_code, warehouse, voucher_type, voucher_no, voucher_detail_no, 
+		stock_ledger_entries, item_sales_bom):
+	# sales bom item
+	buying_amount = 0.0
+	for bom_item in item_sales_bom[item_code]:
+		if bom_item.get("parent_detail_docname")==voucher_detail_no:
+			buying_amount += get_buying_amount(voucher_type, voucher_no, voucher_detail_no, 
+				stock_ledger_entries.get((bom_item.item_code, warehouse), []))
+
+	return buying_amount
 		
-def _get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries):
+def get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries):
 	# IMP NOTE
 	# stock_ledger_entries should already be filtered by item_code and warehouse and 
 	# sorted by posting_date desc, posting_time desc