Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/hr/doctype/expense_claim/expense_claim.js b/hr/doctype/expense_claim/expense_claim.js
index 5794429..d8bee8b 100644
--- a/hr/doctype/expense_claim/expense_claim.js
+++ b/hr/doctype/expense_claim/expense_claim.js
@@ -46,6 +46,13 @@
 	if (doc.docstatus == 0) unhide_field('calculate_total_amount');
 }
 
+cur_frm.cscript.validate = function(doc) {
+	if(cint(doc.docstatus) == 0) {
+		doc.approval_status = "Draft";
+	}
+	cur_frm.cscript.calculate_total(doc);
+}
+
 cur_frm.cscript.employee = function(doc,cdt,cdn){
 	if(doc.employee){
 		$c_obj(make_doclist(doc.doctype, doc.name),'set_approver','', function(r,rt){
diff --git a/hr/doctype/expense_claim/expense_claim.py b/hr/doctype/expense_claim/expense_claim.py
index b8bd41e..2371f3b 100644
--- a/hr/doctype/expense_claim/expense_claim.py
+++ b/hr/doctype/expense_claim/expense_claim.py
@@ -77,10 +77,13 @@
 			set(self.doc, 'remark', self.doc.remark)
 	
 	def approve_voucher(self):
+		missing_count = 0
 		for d in getlist(self.doclist, 'expense_voucher_details'):
 			if not d.sanctioned_amount:
-				msgprint("Please add 'Sanctioned Amount' for all expenses")
-				return cstr('Incomplete')
+				missing_count += 1
+		if missing_count == len(getlist(self.doclist, 'expense_voucher_details')):
+			msgprint("Please add 'Sanctioned Amount' for atleast one expense")
+			return cstr('Incomplete')
 		
 		if not self.doc.total_sanctioned_amount:
 			msgprint("Please calculate total sanctioned amount using button 'Calculate Total Amount'")
@@ -112,7 +115,7 @@
 		
 	def validate(self):
 		self.validate_fiscal_year()
-	
+		
 	def on_update(self):
 		set(self.doc, 'approval_status', 'Draft')
 	
diff --git a/selling/search_criteria/gross_profit/gross_profit.py b/selling/search_criteria/gross_profit/gross_profit.py
index ba3425e..02a4949 100644
--- a/selling/search_criteria/gross_profit/gross_profit.py
+++ b/selling/search_criteria/gross_profit/gross_profit.py
@@ -14,22 +14,26 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.	If not, see <http://www.gnu.org/licenses/>.
 
-# Add Columns
-# ------------
+
 from __future__ import unicode_literals
 from webnotes.utils import flt
 
-colnames[colnames.index('Rate*')] = 'Rate' 
-col_idx['Rate'] = col_idx['Rate*']
-col_idx.pop('Rate*')
-colnames[colnames.index('Amount*')] = 'Amount' 
-col_idx['Amount'] = col_idx['Amount*']
-col_idx.pop('Amount*')
-
 columns = [
-	['Purchase Cost','Currency','150px',''],
-	['Gross Profit','Currency','150px',''],
- 	['Gross Profit (%)','Currrency','150px','']
+	['Delivery Note', 'Link', '120px', 'Delivery Note'],
+	['Posting Date', 'Date', '120px', ''],
+	['Posting Time', 'Data', '120px', ''],
+	['Item Code', 'Link', '120px', 'Item'],
+	['Item Name', 'Data', '120px', ''],
+	['Description', 'Data', '120px', ''],
+	['Warehouse', 'Link', '120px', 'Warehouse'],
+	['Project Name', 'Link', '120px', 'Project'],
+	['Quantity', 'Currency', '120px', ''],
+	['Rate', 'Currency', '120px', ''],
+	['Amount', 'Currency', '120px', ''],
+	#['DN Item Row Id', 'Data', '120px', ''],
+	['Purchase Cost', 'Currency', '150px', ''],
+	['Gross Profit', 'Currency', '150px', ''],
+ 	['Gross Profit (%)', 'Currrency', '150px', '']
 ]
 
 for c in columns:
@@ -41,7 +45,7 @@
 
 sle = sql("""
 	select 
-		actual_qty, incoming_rate, voucher_no, item_code, warehouse
+		actual_qty, incoming_rate, voucher_no, item_code, warehouse, voucher_detail_no
 	from 
 		`tabStock Ledger Entry`
 	where 
@@ -50,7 +54,7 @@
 	order by posting_date desc, posting_time desc, name desc
 """, as_dict=1)
 
-def get_purchase_cost(dn, item, wh, qty):
+def get_purchase_cost(dn, item, wh, qty, dn_item_row_id):
 	from webnotes.utils import flt
 	global sle
  	purchase_cost = 0
@@ -61,13 +65,15 @@
 		packing_items = [[item, qty]]
 	for d in sle:
 		if d['voucher_no'] == dn and [d['item_code'], flt(abs(d['actual_qty']))] in packing_items:
-			purchase_cost += flt(d['incoming_rate'])*flt(abs(d['actual_qty']))
+			if not d['voucher_detail_no'] or d['voucher_detail_no'] == dn_item_row_id:
+				purchase_cost += flt(d['incoming_rate'])*flt(abs(d['actual_qty']))
 	return purchase_cost
 			
 out, tot_amount, tot_pur_cost = [], 0, 0
 for r in res:
-	purchase_cost = get_purchase_cost(r[col_idx['ID']], r[col_idx['Item Code']], \
-		r[col_idx['Warehouse']], r[col_idx['Quantity']])
+	purchase_cost = get_purchase_cost(r[col_idx['Delivery Note']], r[col_idx['Item Code']], \
+		r[col_idx['Warehouse']], r[col_idx['Quantity']], r[-1])
+	r.pop(-1)
 	r.append(purchase_cost)
 	
 	gp = flt(r[col_idx['Amount']]) - flt(purchase_cost)
@@ -79,7 +85,6 @@
 	
 	tot_amount += flt(r[col_idx['Amount']])
 	tot_pur_cost += flt(purchase_cost)
-
 # Add Total Row
 l_row = ['' for i in range(len(colnames))]
 l_row[col_idx['Project Name']] = '<b>TOTALS</b>'
diff --git a/selling/search_criteria/gross_profit/gross_profit.sql b/selling/search_criteria/gross_profit/gross_profit.sql
new file mode 100644
index 0000000..f59ea6a
--- /dev/null
+++ b/selling/search_criteria/gross_profit/gross_profit.sql
@@ -0,0 +1,10 @@
+SELECT 
+	dn.name, dn.posting_date, dn.posting_time, dn_item.item_code,
+	dn_item.item_name, dn_item.description, dn_item.warehouse, 
+	dn.project_name, dn_item.qty, dn_item.basic_rate, dn_item.amount, dn_item.name
+FROM 
+	`tabDelivery Note Item` dn_item, `tabDelivery Note` dn 
+WHERE 
+	dn.docstatus = 1
+ 	AND dn_item.parent = dn.name
+ORDER BY dn.name DESC
\ No newline at end of file