Merge pull request #2296 from nabinhait/stock_reco

Stock balance report and valuation fixes
diff --git a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
index 7bf6fcc..3c67508 100644
--- a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
@@ -76,13 +76,14 @@
 
 	def validate_entries_for_advance(self):
 		for d in self.get('entries'):
-			if not d.is_advance and not d.against_voucher and \
-					not d.against_invoice and not d.against_jv:
+			if not (d.against_voucher and d.against_invoice and d.against_jv):
 				master_type = frappe.db.get_value("Account", d.account, "master_type")
 				if (master_type == 'Customer' and flt(d.credit) > 0) or \
 						(master_type == 'Supplier' and flt(d.debit) > 0):
-					msgprint(_("Row {0}: Please check 'Is Advance' against Account {1} if this \
-						is an advance entry.").format(d.idx, d.account))
+					if not d.is_advance:
+						msgprint(_("Row {0}: Please check 'Is Advance' against Account {1} if this is an advance entry.").format(d.idx, d.account))
+					elif (d.against_sales_order or d.against_purchase_order) and d.is_advance != "Yes":
+						frappe.throw(_("Row {0}: Payment against Sales/Purchase Order should always be marked as advance").format(d.idx))
 
 	def validate_against_jv(self):
 		for d in self.get('entries'):
@@ -177,7 +178,7 @@
 	def validate_against_order_fields(self, doctype, payment_against_voucher):
 		for voucher_no, payment_list in payment_against_voucher.items():
 			voucher_properties = frappe.db.get_value(doctype, voucher_no, 
-				["docstatus", "per_billed", "advance_paid", "grand_total"])
+				["docstatus", "per_billed", "status", "advance_paid", "grand_total"])
 
 			if voucher_properties[0] != 1:
 				frappe.throw(_("{0} {1} is not submitted").format(doctype, voucher_no))
@@ -185,7 +186,10 @@
 			if flt(voucher_properties[1]) >= 100:
 				frappe.throw(_("{0} {1} is fully billed").format(doctype, voucher_no))
 
-			if flt(voucher_properties[3]) < flt(voucher_properties[2]) + flt(sum(payment_list)):
+			if cstr(voucher_properties[2]) == "Stopped":
+				frappe.throw(_("{0} {1} is stopped").format(doctype, voucher_no))
+
+			if flt(voucher_properties[4]) < flt(voucher_properties[3]) + flt(sum(payment_list)):
 				frappe.throw(_("Advance paid against {0} {1} cannot be greater \
 					than Grand Total {2}").format(doctype, voucher_no, voucher_properties[3]))
 
diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.py b/erpnext/accounts/doctype/payment_tool/payment_tool.py
index d8d6df3..578a316 100644
--- a/erpnext/accounts/doctype/payment_tool/payment_tool.py
+++ b/erpnext/accounts/doctype/payment_tool/payment_tool.py
@@ -91,6 +91,7 @@
 		where
 			%s = %s
 			and docstatus = 1
+			and ifnull(status, "") != "Stopped"
 			and ifnull(grand_total, 0) > ifnull(advance_paid, 0)
 			and ifnull(per_billed, 0) < 100.0
 		""" % (voucher_type, 'customer' if party_type == "Customer" else 'supplier', '%s'),
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
index e9bb996..3dcf136 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
@@ -169,6 +169,27 @@
    "search_index": 0
   }, 
   {
+   "fieldname": "shipping_address_name", 
+   "fieldtype": "Link", 
+   "hidden": 1, 
+   "in_filter": 1, 
+   "label": "Shipping Address Name", 
+   "options": "Address", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1
+  }, 
+  {
+   "fieldname": "shipping_address", 
+   "fieldtype": "Small Text", 
+   "hidden": 1, 
+   "label": "Shipping Address", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "read_only": 1
+  }, 
+  {
    "fieldname": "currency_section", 
    "fieldtype": "Section Break", 
    "label": "Currency and Price List", 
@@ -1192,7 +1213,7 @@
  "icon": "icon-file-text", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2014-10-08 14:23:05.034326", 
+ "modified": "2014-10-10 16:54:22.284284", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Sales Invoice", 
diff --git a/erpnext/accounts/report/accounts_payable/accounts_payable.py b/erpnext/accounts/report/accounts_payable/accounts_payable.py
index 3ae741e..5ce2c56 100644
--- a/erpnext/accounts/report/accounts_payable/accounts_payable.py
+++ b/erpnext/accounts/report/accounts_payable/accounts_payable.py
@@ -30,7 +30,8 @@
 	data = []
 	for gle in entries:
 		if cstr(gle.against_voucher) == gle.voucher_no or not gle.against_voucher \
-				or [gle.against_voucher_type, gle.against_voucher] in entries_after_report_date:
+				or [gle.against_voucher_type, gle.against_voucher] in entries_after_report_date \
+				or (gle.against_voucher_type == "Purchase Order"):
 			voucher_details = voucher_detail_map.get(gle.voucher_type, {}).get(gle.voucher_no, {})
 			
 			invoiced_amount = gle.credit > 0 and gle.credit or 0
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 3a0fb31..3dc81d1 100644
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -79,6 +79,9 @@
 		return (
 			# advance
 			(not gle.against_voucher) or 
+
+			# against sales order
+			(gle.against_voucher_type == "Sales Order") or
 			
 			# sales invoice
 			(gle.against_voucher==gle.voucher_no and gle.debit > 0) or