Merge branch 'develop' into attendance_template_fix
diff --git a/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py b/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py
index 7551eae..ecb55dd 100644
--- a/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py
+++ b/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py
@@ -57,7 +57,7 @@
 	if not start_date and not last_completion_date:
 		start_date = frappe.utils.now()
 
-	if last_completion_date and (last_completion_date > start_date or not start_date):
+	if last_completion_date and ((start_date and last_completion_date > start_date) or not start_date):
 		start_date = last_completion_date
 	if periodicity == 'Daily':
 		next_due_date = add_days(start_date, 1)
@@ -71,10 +71,11 @@
 		next_due_date = add_years(start_date, 2)
 	if periodicity == 'Quarterly':
 		next_due_date = add_months(start_date, 3)
-	if end_date and (start_date >= end_date or last_completion_date >= end_date or next_due_date):
+	if end_date and ((start_date and start_date >= end_date) or (last_completion_date and last_completion_date >= end_date) or next_due_date):
 		next_due_date = ""
 	return next_due_date
 
+
 def update_maintenance_log(asset_maintenance, item_code, item_name, task):
 	asset_maintenance_log = frappe.get_value("Asset Maintenance Log", {"asset_maintenance": asset_maintenance,
 		"task": task.maintenance_task, "maintenance_status": ('in',['Planned','Overdue'])})
diff --git a/erpnext/manufacturing/doctype/job_card/job_card.py b/erpnext/manufacturing/doctype/job_card/job_card.py
index a8faa13..9d2e620 100644
--- a/erpnext/manufacturing/doctype/job_card/job_card.py
+++ b/erpnext/manufacturing/doctype/job_card/job_card.py
@@ -105,7 +105,6 @@
 		for_quantity, time_in_mins = 0, 0
 		from_time_list, to_time_list = [], []
 
-
 		for d in frappe.get_all('Job Card',
 			filters = {'docstatus': 1, 'operation_id': self.operation_id}):
 			doc = frappe.get_doc('Job Card', d.name)
@@ -125,8 +124,8 @@
 				if data.name == self.operation_id:
 					data.completed_qty = for_quantity
 					data.actual_operation_time = time_in_mins
-					data.actual_start_time = min(from_time_list)
-					data.actual_end_time = max(to_time_list)
+					data.actual_start_time = min(from_time_list) if from_time_list else None
+					data.actual_end_time = max(to_time_list) if to_time_list else None
 
 			wo.flags.ignore_validate_update_after_submit = True
 			wo.update_operation_status()
diff --git a/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py b/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py
index cd50568..f396705 100644
--- a/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py
+++ b/erpnext/selling/report/customer_credit_balance/customer_credit_balance.py
@@ -60,7 +60,7 @@
 	conditions = ""
 
 	if filters.get("customer"):
-		conditions += " AND c.name = " + filters.get("customer")
+		conditions += " AND c.name = '" + filters.get("customer") + "'"
 
 	return frappe.db.sql("""SELECT
 			c.name, c.customer_name,
@@ -69,6 +69,6 @@
 		FROM `tabCustomer` c, `tabCustomer Credit Limit` ccl
 		WHERE
 			c.name = ccl.parent
-			AND ccl.company = %s
-			{0}
-	""".format(conditions), (filters.get("company")), as_dict=1) #nosec
+			AND ccl.company = '{0}'
+			{1}
+	""".format( filters.get("company"),conditions), as_dict=1) #nosec
diff --git a/erpnext/stock/report/total_stock_summary/total_stock_summary.py b/erpnext/stock/report/total_stock_summary/total_stock_summary.py
index b25e096..41e2f86 100644
--- a/erpnext/stock/report/total_stock_summary/total_stock_summary.py
+++ b/erpnext/stock/report/total_stock_summary/total_stock_summary.py
@@ -15,8 +15,8 @@
 
 def get_columns():
 	columns = [
-		_("Company") + ":Link/Item:250",
-		_("Warehouse") + ":Link/Item:150",
+		_("Company") + ":Link/Company:250",
+		_("Warehouse") + ":Link/Warehouse:150",
 		_("Item") + ":Link/Item:150",
 		_("Description") + "::300",
 		_("Current Qty") + ":Float:100",
@@ -30,7 +30,7 @@
 
 	if filters.get("group_by") == "Warehouse":
 		if filters.get("company"):
-			conditions += " AND warehouse.company = %s" % frappe.db.escape(filters.get("company"), percent=False)
+			conditions += " AND warehouse.company = '%s'" % frappe.db.escape(filters.get("company"), percent=False)
 
 		conditions += " GROUP BY ledger.warehouse, item.item_code"
 		columns += "'' as company, ledger.warehouse"