Merge pull request #15868 from ESS-LLP/fix-membership

fix: Membership - allow system users to create membership
diff --git a/erpnext/__init__.py b/erpnext/__init__.py
index 45a1b1f..de8063d 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.65'
+__version__ = '10.1.66'
 
 def get_default_company(user=None):
 	'''Get default company for user'''
diff --git a/erpnext/accounts/doctype/bank_statement_transaction_entry/bank_statement_transaction_entry.py b/erpnext/accounts/doctype/bank_statement_transaction_entry/bank_statement_transaction_entry.py
index 7a7d7d2..f558194 100644
--- a/erpnext/accounts/doctype/bank_statement_transaction_entry/bank_statement_transaction_entry.py
+++ b/erpnext/accounts/doctype/bank_statement_transaction_entry/bank_statement_transaction_entry.py
@@ -16,10 +16,11 @@
 class BankStatementTransactionEntry(Document):
 	def autoname(self):
 		self.name = self.bank_account + "-" + self.from_date + "-" + self.to_date
-		mapper_name = self.bank + "-Statement-Settings"
-		if not frappe.db.exists("Bank Statement Settings", mapper_name):
-			self.create_settings(self.bank)
-		self.bank_settings = mapper_name
+		if self.bank:
+			mapper_name = self.bank + "-Statement-Settings"
+			if not frappe.db.exists("Bank Statement Settings", mapper_name):
+				self.create_settings(self.bank)
+			self.bank_settings = mapper_name
 
 	def create_settings(self, bank):
 		mapper = frappe.new_doc("Bank Statement Settings")
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 607051e..1b63f71 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -747,9 +747,11 @@
 					self.get_gl_dict({
 						"account": tax.account_head,
 						"against": self.customer,
-						"credit": flt(tax.base_tax_amount_after_discount_amount),
-						"credit_in_account_currency": flt(tax.base_tax_amount_after_discount_amount) \
-							if account_currency==self.company_currency else flt(tax.tax_amount_after_discount_amount),
+						"credit": flt(tax.base_tax_amount_after_discount_amount,
+							tax.precision("tax_amount_after_discount_amount")),
+						"credit_in_account_currency": (flt(tax.base_tax_amount_after_discount_amount,
+							tax.precision("base_tax_amount_after_discount_amount")) if account_currency==self.company_currency else
+							flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount_after_discount_amount"))),
 						"cost_center": tax.cost_center
 					}, account_currency)
 				)
@@ -757,7 +759,7 @@
 	def make_item_gl_entries(self, gl_entries):
 		# income account gl entries
 		for item in self.get("items"):
-			if flt(item.base_net_amount):
+			if flt(item.base_net_amount, item.precision("base_net_amount")):
 				if item.is_fixed_asset:
 					asset = frappe.get_doc("Asset", item.asset)
 
@@ -774,9 +776,10 @@
 						self.get_gl_dict({
 							"account": item.income_account if not item.enable_deferred_revenue else item.deferred_revenue_account,
 							"against": self.customer,
-							"credit": item.base_net_amount,
-							"credit_in_account_currency": item.base_net_amount \
-								if account_currency==self.company_currency else item.net_amount,
+							"credit": flt(item.base_net_amount, item.precision("base_net_amount")),
+							"credit_in_account_currency": (flt(item.base_net_amount, item.precision("base_net_amount"))
+								if account_currency==self.company_currency
+								else flt(item.net_amount, item.precision("net_amount"))),
 							"cost_center": item.cost_center
 						}, account_currency)
 					)
@@ -875,7 +878,7 @@
 
 	def make_write_off_gl_entry(self, gl_entries):
 		# write off entries, applicable if only pos
-		if self.write_off_account and self.write_off_amount:
+		if self.write_off_account and flt(self.write_off_amount, self.precision("write_off_amount")):
 			write_off_account_currency = get_account_currency(self.write_off_account)
 			default_cost_center = frappe.get_cached_value('Company',  self.company,  'cost_center')
 
@@ -885,10 +888,11 @@
 					"party_type": "Customer",
 					"party": self.customer,
 					"against": self.write_off_account,
-					"credit": self.base_write_off_amount,
-					"credit_in_account_currency": self.base_write_off_amount \
-						if self.party_account_currency==self.company_currency else self.write_off_amount,
-					"against_voucher": self.return_against if cint(self.is_return) and self.return_against else self.name,
+					"credit": flt(self.base_write_off_amount, self.precision("base_write_off_amount")),
+					"credit_in_account_currency": (flt(self.base_write_off_amount,
+						self.precision("base_write_off_amount")) if self.party_account_currency==self.company_currency
+						else flt(self.write_off_amount, self.precision("write_off_amount"))),
+					"against_voucher": self.return_against if cint(self.is_return) else self.name,
 					"against_voucher_type": self.doctype,
 					"cost_center": self.cost_center
 				}, self.party_account_currency)
@@ -897,15 +901,16 @@
 				self.get_gl_dict({
 					"account": self.write_off_account,
 					"against": self.customer,
-					"debit": self.base_write_off_amount,
-					"debit_in_account_currency": self.base_write_off_amount \
-						if write_off_account_currency==self.company_currency else self.write_off_amount,
+					"debit": flt(self.base_write_off_amount, self.precision("base_write_off_amount")),
+					"debit_in_account_currency": (flt(self.base_write_off_amount,
+						self.precision("base_write_off_amount")) if write_off_account_currency==self.company_currency
+						else flt(self.write_off_amount, self.precision("write_off_amount"))),
 					"cost_center": self.cost_center or self.write_off_cost_center or default_cost_center
 				}, write_off_account_currency)
 			)
 
 	def make_gle_for_rounding_adjustment(self, gl_entries):
-		if self.rounding_adjustment:
+		if flt(self.rounding_adjustment, self.precision("rounding_adjustment")):
 			round_off_account, round_off_cost_center = \
 				get_round_off_account_and_cost_center(self.company)
 
@@ -913,8 +918,10 @@
 				self.get_gl_dict({
 					"account": round_off_account,
 					"against": self.customer,
-					"credit_in_account_currency": self.base_rounding_adjustment,
-					"credit": self.base_rounding_adjustment,
+					"credit_in_account_currency": flt(self.rounding_adjustment,
+						self.precision("rounding_adjustment")),
+					"credit": flt(self.base_rounding_adjustment,
+						self.precision("base_rounding_adjustment")),
 					"cost_center": self.cost_center or round_off_cost_center,
 				}
 			))
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.html b/erpnext/accounts/report/accounts_receivable/accounts_receivable.html
index 2284fcd..f26cd33 100644
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.html
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.html
@@ -75,7 +75,7 @@
 				<td class="text-right">{%= format_currency(balance_row[range3]) %}</td>
 				<td class="text-right">{%= format_currency(balance_row[range4]) %}</td>
 				<td class="text-right">
-					{%= format_currency(flt(balance_row[__("Outstanding Amount")]), data[data.length-1]["currency"]) %}
+					{%= format_currency(flt(balance_row[("outstanding_amount")]), data[data.length-1]["currency"]) %}
 					</td>
 			</tr>
 				<td>{%= __("PDC/LC") %}</td>
@@ -84,7 +84,7 @@
 				<td></td>
 				<td></td>
 				<td class="text-right">
-					{%= format_currency(flt(balance_row[__("PDC/LC Amount")]), data[data.length-1]["currency"]) %}
+					{%= format_currency(flt(balance_row[("pdc/lc_amount")]), data[data.length-1]["currency"]) %}
 				</td>
 			<tr class="cvs-footer">
 				<th class="text-left">{%= __("Cheques Required") %}</th>
@@ -93,7 +93,7 @@
 				<th></th>
 				<th></th>
 				<th class="text-right">
-					{%= format_currency(flt(balance_row[__("Outstanding Amount")]-balance_row[__("PDC/LC Amount")]), data[data.length-1]["currency"]) %}</th>
+					{%= format_currency(flt(balance_row[("outstanding_amount")]-balance_row[("pdc/lc_amount")]), data[data.length-1]["currency"]) %}</th>
 			</tr>
 		</tbody>
 
@@ -177,10 +177,10 @@
 							<td style="text-align: right">
 								{%= data[i]["po_no"] %}</td>
 						{% } %}
-						<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>
+						<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>
@@ -205,9 +205,9 @@
 								{%= data[i][__("Customer LPO")] %}</td>
 						{% } %}
 						<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>
+						<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 { %}
@@ -228,10 +228,10 @@
 					{% } else { %}
 						<td><b>{%= __("Total") %}</b></td>
 					{% } %}
-					<td style="text-align: right">{%= format_currency(data[i][__("Total Invoiced Amt")], data[i]["currency"]) %}</td>
-					<td style="text-align: right">{%= format_currency(data[i][__("Total Paid Amt")], data[i]["currency"]) %}</td>
+					<td style="text-align: right">{%= format_currency(data[i][("total_invoiced_amt")], data[i]["currency"]) %}</td>
+					<td style="text-align: right">{%= format_currency(data[i][("total_paid_amt")], data[i]["currency"]) %}</td>
 					<td style="text-align: right">{%= report.report_name === "Accounts Receivable Summary" ? format_currency(data[i][__("Credit Note Amt")], data[i]["currency"]) : format_currency(data[i][__("Debit Note Amt")], data[i]["currency"]) %}</td>
-					<td style="text-align: right">{%= format_currency(data[i][__("Total Outstanding Amt")], data[i]["currency"]) %}</td>
+					<td style="text-align: right">{%= format_currency(data[i][("total_outstanding_amt")], data[i]["currency"]) %}</td>
 				{% } %}
 			{% } %}
 			</tr>
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 6bdafd7..572f81d 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -89,20 +89,40 @@
 					"width": 120
 				})
 
-		columns.append({
+		columns += [
+		{
 			"fieldname": "currency",
 			"label": _("Currency"),
 			"fieldtype": "Link",
 			"options": "Currency",
 			"width": 100
-		})
-
-		columns += [
-			_("PDC/LC Date") + ":Date:110",
-			_("PDC/LC Ref") + ":Data:110",
-			_("PDC/LC Amount") + ":Currency/currency:130",
-			_("Remaining Balance") + ":Currency/currency:130"
-		]
+		},
+		{
+			"fieldname": "pdc/lc_date",
+			"label": _("PDC/LC Date"),
+			"fieldtype": "Date",
+			"width": 110
+		},
+		{
+			"fieldname": "pdc/lc_ref",
+			"label": _("PDC/LC Ref"),
+			"fieldtype": "Data",
+			"width": 110
+		},
+		{
+			"fieldname": "pdc/lc_amount",
+			"label": _("PDC/LC Amount"),
+			"fieldtype": "Currency",
+			"options": "Currency",
+			"width": 130
+		},
+		{
+			"fieldname": "remaining_balance",
+			"label": _("Remaining Balance"),
+			"fieldtype": "Currency",
+			"options": "Currency",
+			"width": 130
+		}]
 
 		if args.get('party_type') == 'Customer':
 			columns.append({
@@ -140,7 +160,6 @@
 
 		data = []
 		pdc_details = get_pdc_details(args.get("party_type"), self.filters.report_date)
-
 		gl_entries_data = self.get_entries_till(self.filters.report_date, args.get("party_type"))
 
 		if gl_entries_data:
@@ -464,7 +483,6 @@
 			and pent.party_type = %s
 			group by pent.party, pref.reference_name""", (report_date, 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"
 			if party_type == 'Supplier' else "jea.credit_in_account_currency")
diff --git a/erpnext/healthcare/page/medical_record/medical_record.css b/erpnext/healthcare/page/medical_record/medical_record.css
index 70014da..977625b 100644
--- a/erpnext/healthcare/page/medical_record/medical_record.css
+++ b/erpnext/healthcare/page/medical_record/medical_record.css
@@ -14,10 +14,6 @@
 	margin-bottom: -4px;
 }
 
-.medical_record-row > * {
-	z-index: -999;
-}
-
 .date-indicator {
     background:none;
     font-size:12px;
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 9d21e4b..3a0ada8 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -12,7 +12,7 @@
 source_link = "https://github.com/frappe/erpnext"
 
 develop_version = '11.x.x-develop'
-staging_version = '11.0.3-beta.16'
+staging_version = '11.0.3-beta.17'
 
 error_report_email = "support@erpnext.com"
 
diff --git a/erpnext/hr/utils.py b/erpnext/hr/utils.py
index 281b7fc..f35eb59 100644
--- a/erpnext/hr/utils.py
+++ b/erpnext/hr/utils.py
@@ -178,7 +178,8 @@
 		}, as_dict = 1)
 
 	if overlap_doc:
-		exists_for = doc.employee
+		if doc.get("employee"):
+			exists_for = doc.employee
 		if company:
 			exists_for = company
 		throw_overlap_error(doc, exists_for, overlap_doc[0].name, from_date, to_date)
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index db39545..a7b4a3e 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -5,7 +5,7 @@
 import frappe
 import json
 import frappe.utils
-from frappe.utils import cstr, flt, getdate, comma_and, cint
+from frappe.utils import cstr, flt, getdate, comma_and, cint, nowdate
 from frappe import _
 from frappe.model.utils import get_fetch_values
 from frappe.model.mapper import get_mapped_doc
@@ -346,7 +346,15 @@
 
 	def set_indicator(self):
 		"""Set indicator for portal"""
-		if self.per_billed < 100 and self.per_delivered < 100:
+		if self.status == 'Closed':
+			self.indicator_color = "green"
+			self.indicator_title = _("Closed")
+
+		elif self.per_delivered < 100 and getdate(self.delivery_date) < getdate(nowdate()):
+			self.indicator_color = "red"
+			self.indicator_title = _("Overdue")
+
+		elif self.per_billed < 100 and self.per_delivered < 100:
 			self.indicator_color = "orange"
 			self.indicator_title = _("Not Paid and Not Delivered")
 
diff --git a/erpnext/selling/doctype/sms_center/sms_center.py b/erpnext/selling/doctype/sms_center/sms_center.py
index f3674ae..bb6ba1f 100644
--- a/erpnext/selling/doctype/sms_center/sms_center.py
+++ b/erpnext/selling/doctype/sms_center/sms_center.py
@@ -15,19 +15,24 @@
 	def create_receiver_list(self):
 		rec, where_clause = '', ''
 		if self.send_to == 'All Customer Contact':
-			where_clause = self.customer and " and customer = '%s'" % \
-				self.customer.replace("'", "\'") or " and ifnull(customer, '') != ''"
+			where_clause = " and dl.link_doctype = 'Customer'"
+			if self.customer:
+				where_clause += " and dl.link_name = '%s'" % \
+					self.customer.replace("'", "\'") or " and ifnull(dl.link_name, '') != ''"
 		if self.send_to == 'All Supplier Contact':
-			where_clause = self.supplier and " and supplier = '%s'" % \
-				self.supplier.replace("'", "\'") or " and ifnull(supplier, '') != ''"
+			where_clause = " and dl.link_doctype = 'Supplier'"
+			if self.supplier:
+				where_clause += " and dl.link_name = '%s'" % \
+					self.supplier.replace("'", "\'") or " and ifnull(dl.link_name, '') != ''"
 		if self.send_to == 'All Sales Partner Contact':
-			where_clause = self.sales_partner and " and sales_partner = '%s'" % \
-				self.sales_partner.replace("'", "\'") or " and ifnull(sales_partner, '') != ''"
-
+			where_clause = " and dl.link_doctype = 'Sales Partner'"
+			if self.sales_partner:
+				where_clause += "and dl.link_name = '%s'" % \
+					self.sales_partner.replace("'", "\'") or " and ifnull(dl.link_name, '') != ''"
 		if self.send_to in ['All Contact', 'All Customer Contact', 'All Supplier Contact', 'All Sales Partner Contact']:
-			rec = frappe.db.sql("""select CONCAT(ifnull(first_name,''), ' ', ifnull(last_name,'')),
-				mobile_no from `tabContact` where ifnull(mobile_no,'')!='' and
-				docstatus != 2 %s""" % where_clause)
+			rec = frappe.db.sql("""select CONCAT(ifnull(c.first_name,''), ' ', ifnull(c.last_name,'')),
+				c.mobile_no from `tabContact` c, `tabDynamic Link` dl  where ifnull(c.mobile_no,'')!='' and
+				c.docstatus != 2 and dl.parent = c.name%s""" % where_clause)
 
 		elif self.send_to == 'All Lead (Open)':
 			rec = frappe.db.sql("""select lead_name, mobile_no from `tabLead` where
@@ -50,7 +55,6 @@
 				where ifnull(tabEmployee.cell_number,'')!=''""")
 
 		rec_list = ''
-
 		for d in rec:
 			rec_list += d[0] + ' - ' + d[1] + '\n'
 		self.receiver_list = rec_list
diff --git a/erpnext/stock/report/supplier_wise_sales_analytics/supplier_wise_sales_analytics.py b/erpnext/stock/report/supplier_wise_sales_analytics/supplier_wise_sales_analytics.py
index 917a334..2a02b46 100644
--- a/erpnext/stock/report/supplier_wise_sales_analytics/supplier_wise_sales_analytics.py
+++ b/erpnext/stock/report/supplier_wise_sales_analytics/supplier_wise_sales_analytics.py
@@ -85,6 +85,14 @@
 			is_stock_item=1 and name=pri.item_code)""", as_dict=1):
 			item_supplier_map.setdefault(d.item_code, []).append(d.supplier)
 
+	for d in frappe.db.sql("""select pr.supplier, pri.item_code from
+		`tabPurchase Invoice` pr, `tabPurchase Invoice Item` pri
+		where pr.name=pri.parent and pr.docstatus=1 and
+		ifnull(pr.update_stock, 0) = 1 and pri.item_code=(select name from `tabItem`
+			where is_stock_item=1 and name=pri.item_code)""", as_dict=1):
+			if d.item_code not in item_supplier_map:
+				item_supplier_map.setdefault(d.item_code, []).append(d.supplier)
+
 	if supplier:
 		for item_code, suppliers in iteritems(item_supplier_map):
 			if supplier not in suppliers: