Merge pull request #4277 from nabinhait/gross_profit_fix

[fix][report] Buying amount for product bundle in Gross Profit report
diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py
index bfeeb34..4afce91 100644
--- a/erpnext/accounts/report/gross_profit/gross_profit.py
+++ b/erpnext/accounts/report/gross_profit/gross_profit.py
@@ -98,11 +98,17 @@
 
 			row.base_amount = flt(row.base_net_amount)
 
-			product_bundles = self.product_bundles.get(row.parenttype, {}).get(row.parent, frappe._dict())
-
+			if row.update_stock:
+				product_bundles = self.product_bundles.get(row.parenttype, {}).get(row.parent, frappe._dict())
+			elif row.dn_detail:
+				product_bundles = self.product_bundles.get("Delivery Note", {})\
+					.get(row.delivery_note, frappe._dict())
+				row.item_row = row.dn_detail
+			
 			# get buying amount
 			if row.item_code in product_bundles:
-				row.buying_amount = self.get_buying_amount_from_product_bundle(row, product_bundles[row.item_code])
+				row.buying_amount = self.get_buying_amount_from_product_bundle(row, 
+					product_bundles[row.item_code])
 			else:
 				row.buying_amount = self.get_buying_amount(row, row.item_code)
 
@@ -142,7 +148,6 @@
 					new_row.qty += row.qty
 					new_row.buying_amount += row.buying_amount
 					new_row.base_amount += row.base_amount
-					# new_row.allocated_amount += (row.allocated_amount or 0) if new_row.allocated_amount else 0
 
 			new_row.gross_profit = new_row.base_amount - new_row.buying_amount
 			new_row.gross_profit_percent = ((new_row.gross_profit / new_row.base_amount) * 100.0) \
@@ -158,9 +163,9 @@
 
 	def get_buying_amount_from_product_bundle(self, row, product_bundle):
 		buying_amount = 0.0
-		for bom_item in product_bundle:
-			if bom_item.get("parent_detail_docname")==row.item_row:
-				buying_amount += self.get_buying_amount(row, bom_item.item_code)
+		for packed_item in product_bundle:
+			if packed_item.get("parent_detail_docname")==row.item_row:
+				buying_amount += self.get_buying_amount(row, packed_item.item_code)
 
 		return buying_amount
 
@@ -176,14 +181,14 @@
 		else:
 			my_sle = self.sle.get((item_code, row.warehouse))
 			if (row.update_stock or row.dn_detail) and my_sle:
-				parenttype, parent, item_row = row.parenttype, row.parent, row.item_row
+				parenttype, parent = row.parenttype, row.parent
 				if row.dn_detail:
-					parenttype, parent, item_row = "Delivery Note", row.delivery_note, row.dn_detail
+					parenttype, parent = "Delivery Note", row.delivery_note
 				
 				for i, sle in enumerate(my_sle):
 					# find the stock valution rate from stock ledger entry
 					if sle.voucher_type == parenttype and parent == sle.voucher_no and \
-						sle.voucher_detail_no == item_row:
+						sle.voucher_detail_no == row.item_row:
 							previous_stock_value = len(my_sle) > i+1 and \
 								flt(my_sle[i+1].stock_value) or 0.0
 							return  previous_stock_value - flt(sle.stock_value)