show sales invoice data in gross profit report
diff --git a/accounts/report/gross_profit/gross_profit.js b/accounts/report/gross_profit/gross_profit.js
index 77ac581..1f07df9 100644
--- a/accounts/report/gross_profit/gross_profit.js
+++ b/accounts/report/gross_profit/gross_profit.js
@@ -6,6 +6,6 @@
 			"fieldtype": "Link",
 			"options": "Company",
 			"default": wn.defaults.get_user_default("company")
-		}
+		},
 	]
 }
\ No newline at end of file
diff --git a/accounts/report/gross_profit/gross_profit.py b/accounts/report/gross_profit/gross_profit.py
index 2480e17..a479161 100644
--- a/accounts/report/gross_profit/gross_profit.py
+++ b/accounts/report/gross_profit/gross_profit.py
@@ -1,31 +1,29 @@
 from __future__ import unicode_literals
 import webnotes
 from webnotes.utils import flt
-from stock.utils import get_buying_amount, get_sales_bom
+from stock.utils import get_buying_amount
 
 def execute(filters=None):
 	if not filters: filters = {}
 	
 	stock_ledger_entries = get_stock_ledger_entries(filters)
-	item_sales_bom = get_sales_bom()
 	
-	delivery_note_items = webnotes.conn.sql("""select dn.name, dn.posting_date, dn.posting_time,
-		dn.project_name, item.item_code, item.item_name, item.description, item.warehouse,
-		item.qty, item.basic_rate, item.amount, item.name as "item_row"
-		from `tabDelivery Note` dn, `tabDelivery Note Item` item
-		where item.parent = dn.name and dn.docstatus = 1
-		order by dn.posting_date desc, dn.posting_time desc""", as_dict=1)
+	item_sales_bom = get_item_sales_bom()
+			
+	source = get_source_data(filters)
 	
-	columns = ["Delivery Note:Link/Delivery Note", "Posting Date:Date", "Posting Time", 
+	columns = ["Delivery Note/Sales Invoice::120", "Posting Date:Date", "Posting Time", 
 		"Item Code:Link/Item", "Item Name", "Description", "Warehouse:Link/Warehouse",
 		"Qty:Float", "Selling Rate:Currency", "Selling Amount:Currency", "Buying Amount:Currency",
 		"Gross Profit:Currency", "Gross Profit %:Percent", "Project:Link/Project"]
-		
+	
 	data = []
-	for row in delivery_note_items:
+	for row in source:
 		selling_amount = flt(row.amount)
 		buying_amount = get_buying_amount(row.item_code, row.warehouse, -1*row.qty, 
-			"Delivery Note", row.name, row.item_row, stock_ledger_entries, item_sales_bom)
+			row.parenttype, row.name, row.item_row, stock_ledger_entries, 
+			item_sales_bom.get(row.parenttype, {}).get(row.name, webnotes._dict()))
+		
 		buying_amount = buying_amount > 0 and buying_amount or 0
 		
 		if selling_amount:
@@ -34,7 +32,8 @@
 		else:
 			gross_profit = gross_profit_percent = 0.0
 		
-		data.append([row.name, row.posting_date, row.posting_time, row.item_code, row.item_name,
+		name = """<a href="%s">%s</a>""" % ("/".join(["#Form", row.parenttype, row.name]), row.name)
+		data.append([name, row.posting_date, row.posting_time, row.item_code, row.item_name,
 			row.description, row.warehouse, row.qty, row.basic_rate, row.amount, buying_amount,
 			gross_profit, gross_profit_percent, row.project])
 			
@@ -52,3 +51,44 @@
 	query += " order by item_code desc, warehouse desc, posting_date desc, posting_time desc, name desc"
 
 	return webnotes.conn.sql(query, filters, as_dict=True)
+	
+def get_item_sales_bom():
+	item_sales_bom = {}
+	
+	for d in webnotes.conn.sql("""select parenttype, parent, parent_item,
+		item_code, warehouse, -1*qty as total_qty
+		from `tabDelivery Note Packing Item` where docstatus=1""", as_dict=1):
+		item_sales_bom.setdefault(d.parenttype, webnotes._dict()).setdefault(d.parent,
+			webnotes._dict()).setdefault(d.parent_item, []).append(d)
+
+	return item_sales_bom
+	
+def get_source_data(filters):
+	conditions = ""
+	if filters.get("company"):
+		conditions += " and company=%(company)s"
+	
+	delivery_note_items = webnotes.conn.sql("""select item.parenttype, dn.name, 
+		dn.posting_date, dn.posting_time, dn.project_name, 
+		item.item_code, item.item_name, item.description, item.warehouse,
+		item.qty, item.basic_rate, item.amount, item.name as "item_row",
+		timestamp(dn.posting_date, dn.posting_time) as posting_datetime
+		from `tabDelivery Note` dn, `tabDelivery Note Item` item
+		where item.parent = dn.name and dn.docstatus = 1 %s
+		order by dn.posting_date desc, dn.posting_time desc""" % (conditions,), filters, as_dict=1)
+
+	sales_invoice_items = webnotes.conn.sql("""select item.parenttype, si.name, 
+		si.posting_date, si.posting_time, si.project_name,
+		item.item_code, item.item_name, item.description, item.warehouse,
+		item.qty, item.basic_rate, item.amount, item.name as "item_row",
+		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.is_pos = 1 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
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index fa8927c..538c5c1 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -41,9 +41,14 @@
 				self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)
 
 	def set_buying_amount(self):
-		from stock.utils import get_buying_amount, get_sales_bom
+		from stock.utils import get_buying_amount
 		stock_ledger_entries = self.get_stock_ledger_entries()
-		item_sales_bom = get_sales_bom()
+
+		item_sales_bom = {}
+		for d in self.doclist.get({"parentfield": "packing_details"}):
+			new_d = webnotes._dict(d.fields.copy())
+			new_d.total_qty = -1 * d.qty
+			item_sales_bom.setdefault(d.parent_item, []).append(new_d)
 		
 		if stock_ledger_entries:
 			for item in self.doclist.get({"parentfield": self.fname}):
diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py
index 6bc661d..5e3c9e9 100644
--- a/stock/doctype/delivery_note/delivery_note.py
+++ b/stock/doctype/delivery_note/delivery_note.py
@@ -414,4 +414,4 @@
 				
 		if gl_entries:
 			from accounts.general_ledger import make_gl_entries
-			make_gl_entries(gl_entries)
\ No newline at end of file
+			make_gl_entries(gl_entries)
diff --git a/stock/utils.py b/stock/utils.py
index b4d0770..1406af6 100644
--- a/stock/utils.py
+++ b/stock/utils.py
@@ -171,7 +171,8 @@
 		buying_amount = 0.0
 		for bom_item in item_sales_bom[item_code]:
 			buying_amount += _get_buying_amount(voucher_type, voucher_no, "[** No Item Row **]",
-				bom_item.item_code, warehouse, bom_item.qty * qty, stock_ledger_entries)
+				bom_item.item_code, bom_item.warehouse or warehouse, 
+				bom_item.total_qty or (bom_item.qty * qty), stock_ledger_entries)
 		return buying_amount
 	else:
 		# doesn't have sales bom
@@ -184,21 +185,10 @@
 		if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \
 			(sle.voucher_detail_no == item_row or (sle.voucher_type != "Stock Reconciliation" 
 			and sle.item_code == item_code and sle.warehouse == warehouse and flt(sle.qty) == qty)):
-				# print "previous_sle", stock_ledger_entries[i+1]
-				# print "current sle", sle
 				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 0.0
-
-def get_sales_bom():
-	item_sales_bom = {}
-	# for r in webnotes.conn.sql("""select parent_item, item_code, qty, warehouse, voucher_detail_no
-	# 	from `tabDelivery Note Packing Item` where docstatus = 1""", as_dict=1):
-	for r in webnotes.conn.sql("""select parent, item_code, qty from `tabSales BOM Item`""",
-	 	as_dict=1):
-			item_sales_bom.setdefault(r.parent, []).append(r)
-			
-	return item_sales_bom
\ No newline at end of file
+	return 0.0
\ No newline at end of file