Merge develop into contacts-ref
diff --git a/erpnext/accounts/doctype/bank_transaction/bank_transaction.py b/erpnext/accounts/doctype/bank_transaction/bank_transaction.py
index 4e50570..b8ebeba 100644
--- a/erpnext/accounts/doctype/bank_transaction/bank_transaction.py
+++ b/erpnext/accounts/doctype/bank_transaction/bank_transaction.py
@@ -50,7 +50,7 @@
 			if paid_amount and allocated_amount:
 				if  flt(allocated_amount[0]["allocated_amount"]) > flt(paid_amount):
 					frappe.throw(_("The total allocated amount ({0}) is greated than the paid amount ({1}).".format(flt(allocated_amount[0]["allocated_amount"]), flt(paid_amount))))
-				elif flt(allocated_amount[0]["allocated_amount"]) == flt(paid_amount):
+				else:
 					if payment_entry.payment_document in ["Payment Entry", "Journal Entry", "Purchase Invoice", "Expense Claim"]:
 						self.clear_simple_entry(payment_entry)
 
diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py
index 5bb269e..4665d75 100644
--- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py
+++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py
@@ -257,11 +257,8 @@
 		voucher_type = ('Credit Note'
 			if d.voucher_type == 'Sales Invoice' else 'Debit Note')
 
-		dr_or_cr = ('credit_in_account_currency'
-			if d.reference_type == 'Sales Invoice' else 'debit_in_account_currency')
-
 		reconcile_dr_or_cr = ('debit_in_account_currency'
-			if dr_or_cr == 'credit_in_account_currency' else 'credit_in_account_currency')
+			if d.dr_or_cr == 'credit_in_account_currency' else 'credit_in_account_currency')
 
 		jv = frappe.get_doc({
 			"doctype": "Journal Entry",
@@ -272,8 +269,7 @@
 					'account': d.account,
 					'party': d.party,
 					'party_type': d.party_type,
-					reconcile_dr_or_cr: (abs(d.allocated_amount)
-						if abs(d.unadjusted_amount) > abs(d.allocated_amount) else abs(d.unadjusted_amount)),
+					d.dr_or_cr: abs(d.allocated_amount),
 					'reference_type': d.against_voucher_type,
 					'reference_name': d.against_voucher
 				},
@@ -281,7 +277,8 @@
 					'account': d.account,
 					'party': d.party,
 					'party_type': d.party_type,
-					dr_or_cr: abs(d.allocated_amount),
+					reconcile_dr_or_cr: (abs(d.allocated_amount)
+						if abs(d.unadjusted_amount) > abs(d.allocated_amount) else abs(d.unadjusted_amount)),
 					'reference_type': d.voucher_type,
 					'reference_name': d.voucher_no
 				}
diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py
index e2f99d6..479548c 100755
--- a/erpnext/accounts/doctype/sales_invoice/pos.py
+++ b/erpnext/accounts/doctype/sales_invoice/pos.py
@@ -307,7 +307,7 @@
 	# example: {'Consulting Services': {'Excise 12 - TS': '12.000'}}
 
 	itemwise_tax = {}
-	taxes = frappe.db.sql(""" select parent, tax_type, tax_rate from `tabItem Tax`""", as_dict=1)
+	taxes = frappe.db.sql(""" select parent, tax_type, tax_rate from `tabItem Tax Template Detail`""", as_dict=1)
 
 	for tax in taxes:
 		if tax.parent not in itemwise_tax:
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 97e7104..0e4ee12 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -197,8 +197,10 @@
 		if self.filters.based_on_payment_terms and gl_entries_data:
 			self.payment_term_map = self.get_payment_term_detail(voucher_nos)
 
+		self.gle_inclusion_map = {}
 		for gle in gl_entries_data:
 			if self.is_receivable_or_payable(gle, self.dr_or_cr, future_vouchers, return_entries):
+				self.gle_inclusion_map[gle.name] = True
 				outstanding_amount, credit_note_amount, payment_amount = self.get_outstanding_amount(
 					gle,self.filters.report_date, self.dr_or_cr, return_entries)
 				temp_outstanding_amt = outstanding_amount
@@ -409,7 +411,9 @@
 		for e in self.get_gl_entries_for(gle.party, gle.party_type, gle.voucher_type, gle.voucher_no):
 			if getdate(e.posting_date) <= report_date \
 				and (e.name!=gle.name or (e.voucher_no in return_entries and not return_entries.get(e.voucher_no))):
-
+				if e.name!=gle.name and self.gle_inclusion_map.get(e.name):
+					continue
+				self.gle_inclusion_map[e.name] = True
 				amount = flt(e.get(reverse_dr_or_cr), self.currency_precision) - flt(e.get(dr_or_cr), self.currency_precision)
 				if e.voucher_no not in return_entries:
 					payment_amount += amount
diff --git a/erpnext/communication/doctype/call_log/call_log.py b/erpnext/communication/doctype/call_log/call_log.py
index 2934204..411f56c 100644
--- a/erpnext/communication/doctype/call_log/call_log.py
+++ b/erpnext/communication/doctype/call_log/call_log.py
@@ -6,15 +6,13 @@
 import frappe
 from frappe import _
 from frappe.model.document import Document
-from erpnext.crm.doctype.utils import get_scheduled_employees_for_popup
+from erpnext.crm.doctype.utils import get_scheduled_employees_for_popup, strip_number
 from frappe.contacts.doctype.contact.contact import get_contact_with_phone_number
 from erpnext.crm.doctype.lead.lead import get_lead_with_phone_number
 
 class CallLog(Document):
 	def before_insert(self):
-		# strip 0 from the start of the number for proper number comparisions
-		# eg. 07888383332 should match with 7888383332
-		number = self.get('from').lstrip('0')
+		number = strip_number(self.get('from'))
 		self.contact = get_contact_with_phone_number(number)
 		self.lead = get_lead_with_phone_number(number)
 
@@ -48,13 +46,14 @@
 	doc.add_comment('Comment', frappe.bold(_('Call Summary')) + '<br><br>' + summary)
 
 def get_employees_with_number(number):
+	number = strip_number(number)
 	if not number: return []
 
 	employee_emails = frappe.cache().hget('employees_with_number', number)
 	if employee_emails: return employee_emails
 
 	employees = frappe.get_all('Employee', filters={
-		'cell_number': ['like', '{}'.format(number.lstrip('0'))],
+		'cell_number': ['like', '%{}%'.format(number)],
 		'user_id': ['!=', '']
 	}, fields=['user_id'])
 
@@ -64,27 +63,33 @@
 	return employee
 
 def set_caller_information(doc, state):
-	'''Called from hoooks on creation of Lead or Contact'''
+	'''Called from hooks on creation of Lead or Contact'''
 	if doc.doctype not in ['Lead', 'Contact']: return
 
 	numbers = [doc.get('phone'), doc.get('mobile_no')]
-	for_doc = doc.doctype.lower()
+	# contact for Contact and lead for Lead
+	fieldname = doc.doctype.lower()
+
+	# contact_name or lead_name
+	display_name_field = '{}_name'.format(fieldname)
 
 	# Contact now has all the nos saved in child table
 	if doc.doctype == 'Contact':
 		numbers = [nos.phone for nos in doc.phone_nos]
 
 	for number in numbers:
+		number = strip_number(number)
 		if not number: continue
 
 		filters = frappe._dict({
-			'from': ['like', '%{}'.format(number.lstrip('0'))],
-			for_doc: ''
+			'from': ['like', '%{}'.format(number)],
+			fieldname: ''
 		})
 
 		logs = frappe.get_all('Call Log', filters=filters)
 
 		for log in logs:
-			call_log = frappe.get_doc('Call Log', log.name)
-			call_log.set(for_doc, doc.name)
-			call_log.save(ignore_permissions=True)
+			frappe.db.set_value('Call Log', log.name, {
+				fieldname: doc.name,
+				display_name_field: doc.get_title()
+			}, update_modified=False)
diff --git a/erpnext/controllers/queries.py b/erpnext/controllers/queries.py
index 57c063a..19ec053 100644
--- a/erpnext/controllers/queries.py
+++ b/erpnext/controllers/queries.py
@@ -371,7 +371,7 @@
 
 	return frappe.db.sql("""select tabAccount.name from `tabAccount`
 		where (tabAccount.report_type = "Profit and Loss"
-				or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed"))
+				or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed", "Capital Work in Progress"))
 			and tabAccount.is_group=0
 			and tabAccount.docstatus!=2
 			and tabAccount.{key} LIKE %(txt)s
diff --git a/erpnext/crm/doctype/utils.py b/erpnext/crm/doctype/utils.py
index 5553276..535458a 100644
--- a/erpnext/crm/doctype/utils.py
+++ b/erpnext/crm/doctype/utils.py
@@ -54,6 +54,8 @@
 
 
 def get_scheduled_employees_for_popup(communication_medium):
+	if not communication_medium: return []
+
 	now_time = frappe.utils.nowtime()
 	weekday = frappe.utils.get_weekday()
 
@@ -73,3 +75,10 @@
 	employee_emails = set([employee.user_id for employee in employees])
 
 	return employee_emails
+
+def strip_number(number):
+	if not number: return
+	# strip 0 from the start of the number for proper number comparisions
+	# eg. 07888383332 should match with 7888383332
+	number = number.lstrip('0')
+	return number
\ No newline at end of file
diff --git a/erpnext/regional/report/gstr_1/gstr_1.py b/erpnext/regional/report/gstr_1/gstr_1.py
index 2da1085..c397133 100644
--- a/erpnext/regional/report/gstr_1/gstr_1.py
+++ b/erpnext/regional/report/gstr_1/gstr_1.py
@@ -156,33 +156,21 @@
 
 
 		if self.filters.get("type_of_business") ==  "B2B":
-			customers = frappe.get_all("Customer",
-				filters={
-					"gst_category": ["in", ["Registered Regular", "Deemed Export", "SEZ"]]
-			})
-
-			if customers:
-				conditions += """ and ifnull(gst_category, '') != 'Overseas' and is_return != 1
-					and customer in ({0})""".format(", ".join([frappe.db.escape(c.name) for c in customers]))
+			conditions += "and ifnull(gst_category, '') in ('Registered Regular', 'Deemed Export', 'SEZ') and is_return != 1"
 
 		if self.filters.get("type_of_business") in ("B2C Large", "B2C Small"):
 			b2c_limit = frappe.db.get_single_value('GST Settings', 'b2c_limit')
 			if not b2c_limit:
 				frappe.throw(_("Please set B2C Limit in GST Settings."))
 
-		customers = frappe.get_all("Customer",
-			filters={
-				"gst_category": ["in", ["Unregistered"]]
-		})
-
 		if self.filters.get("type_of_business") ==  "B2C Large" and customers:
 			conditions += """ and SUBSTR(place_of_supply, 1, 2) != SUBSTR(company_gstin, 1, 2)
-				and grand_total > {0} and is_return != 1 and customer in ({1})""".\
+				and grand_total > {0} and is_return != 1 and gst_category ='Unregistered' """.\
 					format(flt(b2c_limit), ", ".join([frappe.db.escape(c.name) for c in customers]))
 		elif self.filters.get("type_of_business") ==  "B2C Small" and customers:
 			conditions += """ and (
 				SUBSTR(place_of_supply, 1, 2) = SUBSTR(company_gstin, 1, 2)
-					or grand_total <= {0}) and is_return != 1 and customer in ({1})""".\
+					or grand_total <= {0}) and is_return != 1 and gst_category ='Unregistered' """.\
 						format(flt(b2c_limit), ", ".join([frappe.db.escape(c.name) for c in customers]))
 
 		elif self.filters.get("type_of_business") ==  "CDNR":