Merge pull request #2203 from rmehta/mailbox

Mailbox
diff --git a/erpnext/accounts/report/gross_profit/gross_profit.json b/erpnext/accounts/report/gross_profit/gross_profit.json
index 81e0aaf..69555d0 100644
--- a/erpnext/accounts/report/gross_profit/gross_profit.json
+++ b/erpnext/accounts/report/gross_profit/gross_profit.json
@@ -5,7 +5,7 @@
  "doctype": "Report", 
  "idx": 1, 
  "is_standard": "Yes", 
- "modified": "2014-06-03 07:18:17.077022", 
+ "modified": "2014-09-18 19:00:50.263854", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Gross Profit", 
diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py
index 76e7b4a..bff561d 100644
--- a/erpnext/accounts/report/gross_profit/gross_profit.py
+++ b/erpnext/accounts/report/gross_profit/gross_profit.py
@@ -14,7 +14,7 @@
 	source = get_source_data(filters)
 	item_sales_bom = get_item_sales_bom()
 	
-	columns = [__("Delivery Note/Sales Invoice") + "::120", _("Link") + "::30", _("Posting Date") + ":Date", _("Posting Time"), 
+	columns = [_("Delivery Note/Sales Invoice") + "::120", _("Link") + "::30", _("Posting Date") + ":Date", _("Posting Time"), 
 		_("Item Code") + ":Link/Item", _("Item Name"), _("Description"), _("Warehouse") + ":Link/Warehouse",
 		_("Qty") + ":Float", _("Selling Rate") + ":Currency", _("Avg. Buying Rate") + ":Currency", 
 		_("Selling Amount") + ":Currency", _("Buying Amount") + ":Currency",
@@ -29,7 +29,7 @@
 			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,
+			buying_amount = get_buying_amount(row.item_code, row.qty, 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
@@ -107,11 +107,10 @@
 		timestamp(si.posting_date, si.posting_time) as posting_datetime
 		from `tabSales Invoice` si, `tabSales Invoice Item` item
 		where item.parent = si.name and si.docstatus = 1 %s
-		and si.update_stock = 1
 		order by si.posting_date desc, si.posting_time desc""" % (conditions,), filters, as_dict=1)
 	
 	source = delivery_note_items + sales_invoice_items
 	if len(source) > len(delivery_note_items):
 		source.sort(key=lambda d: d.posting_datetime, reverse=True)
 	
-	return source
\ No newline at end of file
+	return source
diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py
index c441ae7..2b4e368 100644
--- a/erpnext/stock/utils.py
+++ b/erpnext/stock/utils.py
@@ -158,18 +158,27 @@
 
 	return buying_amount
 
-def get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries):
+def get_buying_amount(item_code, item_qty, 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
-	for i, sle in enumerate(stock_ledger_entries):
-		if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \
-			sle.voucher_detail_no == item_row:
-				previous_stock_value = len(stock_ledger_entries) > i+1 and \
-					flt(stock_ledger_entries[i+1].stock_value) or 0.0
-				buying_amount =  previous_stock_value - flt(sle.stock_value)
+	if frappe.db.get_value("Item", item_code, "is_stock_item") == "Yes":
+		for i, sle in enumerate(stock_ledger_entries):
+			if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \
+				sle.voucher_detail_no == item_row:
+					previous_stock_value = len(stock_ledger_entries) > i+1 and \
+						flt(stock_ledger_entries[i+1].stock_value) or 0.0
+					buying_amount =  previous_stock_value - flt(sle.stock_value)
 
-				return buying_amount
+					return buying_amount
+	else:
+		item_rate = frappe.db.sql("""select sum(base_amount) / sum(qty)
+			from `tabPurchase Invoice Item`
+			where item_code = %s and docstatus=1""" % ('%s'), item_code)
+		buying_amount = flt(item_qty) * flt(item_rate[0][0]) if item_rate else 0
+
+		return buying_amount
+
 	return 0.0