Merge branch 'master' into develop
diff --git a/erpnext/__init__.py b/erpnext/__init__.py
index 3c7b9f3..120d65f 100644
--- a/erpnext/__init__.py
+++ b/erpnext/__init__.py
@@ -5,7 +5,7 @@
 from erpnext.hooks import regional_overrides
 from frappe.utils import getdate
 
-__version__ = '10.1.2'
+__version__ = '10.1.3'
 
 def get_default_company(user=None):
 	'''Get default company for user'''
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index ed2e3e4..44658f7 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -101,6 +101,8 @@
 		self.set_billing_hours_and_amount()
 		self.update_timesheet_billing_for_project()
 		self.set_status()
+		if self.is_pos and not self.is_return:
+			self.verify_payment_amount_is_positive()
 
 	def before_save(self):
 		set_account_for_mode_of_payment(self)
@@ -905,6 +907,11 @@
 			project.update_billed_amount()
 			project.save()
 
+	def verify_payment_amount_is_positive(self):
+		for entry in self.payments:
+			if entry.amount < 0:
+				frappe.throw(_("Row #{0} (Payment Table): Amount must be positive").format(entry.idx))
+
 def get_list_context(context=None):
 	from erpnext.controllers.website_list_for_contact import get_list_context
 	list_context = get_list_context(context)
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.html b/erpnext/accounts/report/accounts_receivable/accounts_receivable.html
index b622ab4..d96fc99 100644
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.html
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.html
@@ -1,10 +1,15 @@
-<style>
-@media screen {
-  .print-format {
-    padding: 8mm;
-  }
-}
-</style>
+{% if(filters.show_pdc_in_print) { %}
+	<style>
+	@media screen {
+	  .print-format {
+	    padding: 8mm;
+		margin:4mm;
+		font-size:10px;
+		font-family: Tahoma, sans-serif;
+	  }
+	}
+	</style>
+{% } %}
 
 <h2 class="text-center">{%= __(report.report_name) %}</h2>
 <h4 class="text-center">{%= filters.customer || filters.supplier %} </h4>
@@ -82,21 +87,24 @@
 		<tr>
 			{% if(report.report_name === "Accounts Receivable" || report.report_name === "Accounts Payable") { %}
 				<th style="width: 10%">{%= __("Date") %}</th>
-				<th style="width: 10%">{%= __("Ref") %}</th>
+				<th style="width: 15%">{%= __("Ref") %}</th>
+				{% if(!filters.show_pdc_in_print) { %}
 				<th style="width: 20%">{%= (filters.customer || filters.supplier) ? __("Remarks"): __("Party") %}</th>
+				{% } %}
 				<th style="width: 10%">{%= __("Invoiced Amount") %}</th>
 				{% if(!filters.show_pdc_in_print) { %}
 					<th style="width: 10%">{%= __("Paid Amount") %}</th>
 					<th style="width: 10%">{%= report.report_name === "Accounts Receivable" ? __('Credit Note') : __('Debit Note') %}</th>
 				{% } %}
-				<th style="width: 6%">{%= __("Outstanding Amount") %}</th>
+				<th style="width: 15%">{%= __("Outstanding Amount") %}</th>
 				{% if(filters.show_pdc_in_print) { %}
 					{% if(report.report_name === "Accounts Receivable") { %}
-						<th style="width: 6%">{%= __("Customer LPO No.") %}</th>
+						<th style="width: 10%">{%= __("Customer LPO No.") %}</th>
 					{% } %}
-					<th style="width: 6%">{%= __("PDC/LC Date") %}</th>
-					<th style="width: 6%">{%= __("PDC/LC Ref") %}</th>
-					<th style="width: 6%">{%= __("PDC/LC Amount") %}</th>
+					<th style="width: 10%">{%= __("PDC/LC Date") %}</th>
+					<th style="width: 10%">{%= __("PDC/LC Ref") %}</th>
+					<th style="width: 10%">{%= __("PDC/LC Amount") %}</th>
+					<th style="width: 10%">{%= __("Remaining Balance") %}</th>
 				{% } %}
 			{% } else { %}
 				<th style="width: 40%">{%= (filters.customer || filters.supplier) ? __("Remarks"): __("Party") %}</th>
@@ -115,6 +123,7 @@
 					<td>{%= dateutil.str_to_user(data[i][__("Posting Date")]) %}</td>
 					<td>{%= data[i][__("Voucher Type")] %}
 						<br>{%= data[i][__("Voucher No")] %}</td>
+					{% if(!filters.show_pdc_in_print) { %}	
 					<td>
 						{% if(!(filters.customer || filters.supplier)) { %}
 							{%= data[i][__("Customer")] || data[i][__("Supplier")] %}
@@ -127,6 +136,7 @@
 						<br>{%= __("Remarks") %}:
 						{%= data[i][__("Remarks")] %}
 					</td>
+					{% } %}
 					<td style="text-align: right">
 						{%= format_currency(data[i]["Invoiced Amount"], data[i]["currency"]) %}</td>
 
@@ -147,10 +157,13 @@
 						<td style="text-align: right">{%= frappe.datetime.str_to_user(data[i][__("PDC/LC Date")]) %}</td>
 						<td style="text-align: right">{%= data[i][__("PDC/LC Ref")] %}</td>
 						<td style="text-align: right">{%= format_currency(data[i][__("PDC/LC Amount")], data[i]["currency"]) %}</td>
+						<td style="text-align: right">{%= format_currency(data[i][__("Remaining Balance")], data[i]["currency"]) %}</td>
 					{% } %}
 				{% } else { %}
 					<td></td>
+					{% if(!filters.show_pdc_in_print) { %}
 					<td></td>
+					{% } %}
 					<td><b>{%= __("Total") %}</b></td>
 					<td style="text-align: right">
 						{%= format_currency(data[i]["Invoiced Amount"], data[i]["currency"] ) %}</td>
@@ -171,6 +184,7 @@
 						<td style="text-align: right">{%= frappe.datetime.str_to_user(data[i][__("PDC/LC Date")]) %}</td>
 						<td style="text-align: right">{%= data[i][__("PDC/LC Ref")] %}</td>
 						<td style="text-align: right">{%= format_currency(data[i][__("PDC/LC Amount")], data[i]["currency"]) %}</td>
+						<td style="text-align: right">{%= format_currency(data[i][__("Remaining Balance")], data[i]["currency"]) %}</td>
 					{% } %}
 				{% } %}
 			{% } else { %}
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 29beda0..cf3b1bd 100644
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -159,7 +159,7 @@
 					else:
 						row.append(company_currency)
 
-					pdc = pdc_details.get(gle.voucher_no, {})
+					pdc = pdc_details.get((gle.voucher_no, gle.party), {})
 					remaining_balance = outstanding_amount - flt(pdc.get("pdc_amount"))
 					row += [pdc.get("pdc_date"), pdc.get("pdc_ref"),
 						flt(pdc.get("pdc_amount")), remaining_balance]
@@ -405,25 +405,33 @@
 		on
 			(pref.parent = pent.name)
 		where
-			pent.docstatus = 0 and pent.reference_date > pent.posting_date
+			pent.docstatus < 2 and pent.reference_date >= pent.posting_date
 			and pent.party_type = %s
-			group by pref.reference_name""", party_type, as_dict=1):
-			pdc_details.setdefault(pdc.invoice_no, pdc)
+			group by pent.party, pref.reference_name""", party_type, as_dict=1):
+			pdc_details.setdefault((pdc.invoice_no, pdc.party), pdc)
+
+	if scrub(party_type):
+		amount_field = "jea.debit_in_account_currency + jea.credit_in_account_currency"
+	else:
+		amount_field = "jea.debit + jea.credit"
 
 	for pdc in frappe.db.sql("""
 		select
 			jea.reference_name as invoice_no, jea.party, jea.party_type,
-			max(je.cheque_date) as pdc_date, sum(ifnull(je.total_amount,0)) as pdc_amount,
+			max(je.cheque_date) as pdc_date, sum(ifnull({0},0)) as pdc_amount,
 			GROUP_CONCAT(je.cheque_no SEPARATOR ', ') as pdc_ref
 		from
 			`tabJournal Entry` as je inner join `tabJournal Entry Account` as jea
 		on
 			(jea.parent = je.name)
 		where
-			je.docstatus = 0 and je.cheque_date > je.posting_date
+			je.docstatus < 2 and je.cheque_date >= je.posting_date
 			and jea.party_type = %s
-			group by jea.reference_name""", party_type, as_dict=1):
-			pdc_details.setdefault(pdc.invoice_no, pdc)
+			group by jea.party, jea.reference_name""".format(amount_field), party_type, as_dict=1):
+			if (pdc.invoice_no, pdc.party) in pdc_details:
+				pdc_details[(pdc.invoice_no, pdc.party)]["pdc_amount"] += pdc.pdc_amount
+			else:
+				pdc_details.setdefault((pdc.invoice_no, pdc.party), pdc)
 
 	return pdc_details
 
diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
index a39b831..ac9f4ce 100644
--- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
+++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
@@ -105,7 +105,7 @@
 
 	if filters.get("mode_of_payment"):
 		conditions += """ and exists(select name from `tabSales Invoice Payment`
-			where parent=si.name
+			where parent=`tabSales Invoice`.name
 				and ifnull(`tabSales Invoice Payment`.mode_of_payment, '') = %(mode_of_payment)s)"""
 
 	return conditions
diff --git a/erpnext/projects/doctype/timesheet/timesheet.py b/erpnext/projects/doctype/timesheet/timesheet.py
index 57a51d2..82494b0 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.py
+++ b/erpnext/projects/doctype/timesheet/timesheet.py
@@ -292,7 +292,7 @@
 		cond = "and parent = %(parent)s"
 
 	return frappe.db.sql("""select name, parent, billing_hours, billing_amount as billing_amt
-			from `tabTimesheet Detail` where docstatus=1 and project = %(project)s {0} and billable = 1
+			from `tabTimesheet Detail` where parenttype = 'Timesheet' and docstatus=1 and project = %(project)s {0} and billable = 1
 			and sales_invoice is null""".format(cond), {'project': project, 'parent': parent}, as_dict=1)
 
 @frappe.whitelist()
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 36d0c39..1c89d46 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -104,8 +104,8 @@
 	def validate_delivery_date(self):
 		if self.order_type == 'Sales':
 			if not self.delivery_date:
-				self.delivery_date = max([d.delivery_date for d in self.get("items") if d.delivery_date])
-
+				delivery_date_list = [d.delivery_date for d in self.get("items") if d.delivery_date]
+				self.delivery_date = max(delivery_date_list) if delivery_date_list else None
 			if self.delivery_date:
 				for d in self.get("items"):
 					if not d.delivery_date: