Merge branch 'staging-fixes' into new-fix-branch
diff --git a/erpnext/accounts/report/general_ledger/general_ledger.py b/erpnext/accounts/report/general_ledger/general_ledger.py
index 524f5f7..7e50d9b 100644
--- a/erpnext/accounts/report/general_ledger/general_ledger.py
+++ b/erpnext/accounts/report/general_ledger/general_ledger.py
@@ -16,8 +16,6 @@
 		return [], []
 
 	account_details = {}
-	if not filters.get("group_by"):
-		filters['group_by'] = _('Group by Voucher (Consolidated)')
 
 	if filters and filters.get('print_in_account_currency') and \
 		not filters.get('account'):
diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py
index 19075d3..2ed664c 100644
--- a/erpnext/accounts/report/gross_profit/gross_profit.py
+++ b/erpnext/accounts/report/gross_profit/gross_profit.py
@@ -236,7 +236,7 @@
 							previous_stock_value = len(my_sle) > i+1 and \
 								flt(my_sle[i+1].stock_value) or 0.0
 							if previous_stock_value:
-								return previous_stock_value - flt(sle.stock_value)
+								return (previous_stock_value - flt(sle.stock_value)) * flt(row.qty) / abs(flt(sle.qty))
 							else:
 								return flt(row.qty) * self.get_average_buying_rate(row, item_code)
 			else:
diff --git a/erpnext/accounts/report/trial_balance/trial_balance.py b/erpnext/accounts/report/trial_balance/trial_balance.py
index 0a3f56a..07dcd4e 100644
--- a/erpnext/accounts/report/trial_balance/trial_balance.py
+++ b/erpnext/accounts/report/trial_balance/trial_balance.py
@@ -232,6 +232,13 @@
 			"width": 300
 		},
 		{
+			"fieldname": "currency",
+			"label": _("Currency"),
+			"fieldtype": "Link",
+			"options": "Currency",
+			"hidden": 1
+		},
+		{
 			"fieldname": "opening_debit",
 			"label": _("Opening (Dr)"),
 			"fieldtype": "Currency",
@@ -272,13 +279,6 @@
 			"fieldtype": "Currency",
 			"options": "currency",
 			"width": 120
-		},
-		{
-			"fieldname": "currency",
-			"label": _("Currency"),
-			"fieldtype": "Link",
-			"options": "Currency",
-			"hidden": 1
 		}
 	]
 
diff --git a/erpnext/healthcare/doctype/patient/patient.js b/erpnext/healthcare/doctype/patient/patient.js
index d0ab94c..de5bce0 100644
--- a/erpnext/healthcare/doctype/patient/patient.js
+++ b/erpnext/healthcare/doctype/patient/patient.js
@@ -120,3 +120,16 @@
 		}
 	});
 };
+
+frappe.ui.form.on('Patient Relation', {
+	patient_relation_add: function(frm){
+		frm.fields_dict['patient_relation'].grid.get_field('patient').get_query = function(frm){
+			var patient_list = [];
+			if(!frm.doc.__islocal) patient_list.push(frm.doc.name);
+			$.each(frm.doc.patient_relation, function(idx, val){
+				if (val.patient) patient_list.push(val.patient);
+			});
+			return { filters: [['Patient', 'name', 'not in', patient_list]] };
+		};
+	}
+});
\ No newline at end of file
diff --git a/erpnext/hr/doctype/holiday_list/holiday_list.js b/erpnext/hr/doctype/holiday_list/holiday_list.js
index b4c56da..462bd8b 100644
--- a/erpnext/hr/doctype/holiday_list/holiday_list.js
+++ b/erpnext/hr/doctype/holiday_list/holiday_list.js
@@ -3,7 +3,9 @@
 
 frappe.ui.form.on('Holiday List', {
 	refresh: function(frm) {
-		frm.set_value('total_holidays', frm.doc.holidays.length);
+		if (frm.doc.holidays) {
+			frm.set_value('total_holidays', frm.doc.holidays.length);
+		}
 	},
 	from_date: function(frm) {
 		if (frm.doc.from_date && !frm.doc.to_date) {
diff --git a/erpnext/projects/doctype/timesheet/timesheet.json b/erpnext/projects/doctype/timesheet/timesheet.json
index e5198de..0be147b 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.json
+++ b/erpnext/projects/doctype/timesheet/timesheet.json
@@ -1,5 +1,6 @@
 {
  "allow_copy": 0, 
+ "allow_events_in_timeline": 0, 
  "allow_guest_to_view": 0, 
  "allow_import": 1, 
  "allow_rename": 0, 
@@ -457,39 +458,6 @@
    "ignore_user_permissions": 0, 
    "ignore_xss_filter": 0, 
    "in_filter": 0, 
-   "in_global_search": 1, 
-   "in_list_view": 0, 
-   "in_standard_filter": 0, 
-   "label": "User", 
-   "length": 0, 
-   "no_copy": 0, 
-   "options": "User", 
-   "permlevel": 0, 
-   "precision": "", 
-   "print_hide": 0, 
-   "print_hide_if_no_value": 0, 
-   "read_only": 1, 
-   "remember_last_selected_value": 0, 
-   "report_hide": 0, 
-   "reqd": 0, 
-   "search_index": 0, 
-   "set_only_once": 0, 
-   "translatable": 0, 
-   "unique": 0
-  }, 
-  {
-   "allow_bulk_edit": 0, 
-   "allow_in_quick_entry": 0, 
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "columns": 0, 
-   "fieldname": "start_date", 
-   "fieldtype": "Date", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "ignore_xss_filter": 0, 
-   "in_filter": 0, 
    "in_global_search": 0, 
    "in_list_view": 1, 
    "in_standard_filter": 0, 
@@ -1032,7 +1000,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-08-28 14:44:32.912004", 
+ "modified": "2018-11-15 07:58:42.629845", 
  "modified_by": "Administrator", 
  "module": "Projects", 
  "name": "Timesheet", 
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index a33b87f..035c58d 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -33,7 +33,7 @@
 				item.margin_rate_or_amount = 0;
 				item.rate_with_margin = 0;
 			}
-			item.base_rate_with_margin = item.rate_with_margin * flt(this.frm.doc.conversion_rate);
+			item.base_rate_with_margin = item.rate_with_margin * flt(frm.doc.conversion_rate);
 
 			cur_frm.cscript.set_gross_profit(item);
 			cur_frm.cscript.calculate_taxes_and_totals();
@@ -185,6 +185,12 @@
 		}
 	},
 
+	is_return: function() {
+		if(!this.frm.doc.is_return && this.frm.doc.return_against) {
+			this.frm.set_value('return_against', '');
+		}
+	},
+
 	setup_quality_inspection: function() {
 		if(!in_list(["Delivery Note", "Sales Invoice", "Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)) {
 			return;
diff --git a/erpnext/selling/report/address_and_contacts/address_and_contacts.json b/erpnext/selling/report/address_and_contacts/address_and_contacts.json
index da38bab..876c39c 100644
--- a/erpnext/selling/report/address_and_contacts/address_and_contacts.json
+++ b/erpnext/selling/report/address_and_contacts/address_and_contacts.json
@@ -6,8 +6,7 @@
  "docstatus": 0, 
  "doctype": "Report", 
  "idx": 0, 
- "is_standard": "Yes", 
- "letter_head": "Test", 
+ "is_standard": "Yes",  
  "modified": "2018-06-01 09:39:39.604944", 
  "modified_by": "Administrator", 
  "module": "Selling", 
@@ -30,4 +29,4 @@
    "role": "Accounts User"
   }
  ]
-}
\ No newline at end of file
+}
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index d90db56..01ee9db 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -594,22 +594,21 @@
 	if not user:
 		user = frappe.session['user']
 
-	pos_profile = frappe.db.sql("""select pf.*
-		from
-			`tabPOS Profile` pf, `tabPOS Profile User` pfu
-		where
-			pfu.parent = pf.name and pfu.user = %s and pf.company = %s
-			and pf.disabled = 0 and pfu.default=1""", (user, company), as_dict=1)
-
-	if not pos_profile:
-		pos_profile = frappe.db.sql("""select pf.*
-			from
-				`tabPOS Profile` pf left join `tabPOS Profile User` pfu
-			on
+	pos_profile = frappe.db.sql("""SELECT pf.*
+		FROM
+			`tabPOS Profile` pf LEFT JOIN `tabPOS Profile User` pfu
+		ON
 				pf.name = pfu.parent
-			where
-				ifnull(pfu.user, '') = '' and pf.company = %s
-				and pf.disabled = 0""", (company), as_dict=1)
+		WHERE
+			(
+				(pfu.user = %(user)s AND pf.company = %(company)s AND pfu.default=1)
+				OR (pfu.user = %(user)s AND pfu.default=1)
+				OR (ifnull(pfu.user, '') = '' AND pf.company = %(company)s)
+			) AND pf.disabled = 0
+	""", {
+		'user': user,
+		'company': company
+	}, as_dict=1)
 
 	return pos_profile and pos_profile[0] or None
 
diff --git a/erpnext/stock/report/stock_projected_qty/stock_projected_qty.js b/erpnext/stock/report/stock_projected_qty/stock_projected_qty.js
index 6589688..babc6dc 100644
--- a/erpnext/stock/report/stock_projected_qty/stock_projected_qty.js
+++ b/erpnext/stock/report/stock_projected_qty/stock_projected_qty.js
@@ -7,7 +7,8 @@
 			"fieldname":"company",
 			"label": __("Company"),
 			"fieldtype": "Link",
-			"options": "Company"
+			"options": "Company",
+			"default": frappe.defaults.get_user_default("Company")
 		},
 		{
 			"fieldname":"warehouse",