Merge branch 'develop' into multiple-users-pos-profile
diff --git a/erpnext/accounts/doctype/pos_profile/pos_profile.json b/erpnext/accounts/doctype/pos_profile/pos_profile.json
index 187454e..a7c2c3d 100644
--- a/erpnext/accounts/doctype/pos_profile/pos_profile.json
+++ b/erpnext/accounts/doctype/pos_profile/pos_profile.json
@@ -3,7 +3,7 @@
  "allow_guest_to_view": 0, 
  "allow_import": 0, 
  "allow_rename": 0, 
- "autoname": "hash", 
+ "autoname": "field:pos_profile_name", 
  "beta": 0, 
  "creation": "2013-05-24 12:15:51", 
  "custom": 0, 
@@ -17,6 +17,36 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "pos_profile_name", 
+   "fieldtype": "Data", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "POS Profile Name", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "user", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -422,6 +452,67 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "section_break_15", 
+   "fieldtype": "Section Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Applicable for Users", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "applicable_for_users", 
+   "fieldtype": "Table", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Applicable for Users", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "POS Profile User", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "section_break_11", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -1322,8 +1413,8 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-09-01 15:55:14.890452", 
- "modified_by": "Administrator", 
+ "modified": "2017-09-29 14:39:22.280700", 
+ "modified_by": "faris@erpnext.com", 
  "module": "Accounts", 
  "name": "POS Profile", 
  "owner": "Administrator", 
@@ -1375,7 +1466,7 @@
  "show_name_in_global_search": 0, 
  "sort_field": "modified", 
  "sort_order": "DESC", 
- "title_field": "user", 
+ "title_field": "pos_profile_name", 
  "track_changes": 0, 
  "track_seen": 0
 }
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/pos_profile/pos_profile.py b/erpnext/accounts/doctype/pos_profile/pos_profile.py
index 8d6a2db..3e37285 100644
--- a/erpnext/accounts/doctype/pos_profile/pos_profile.py
+++ b/erpnext/accounts/doctype/pos_profile/pos_profile.py
@@ -11,7 +11,7 @@
 
 class POSProfile(Document):
 	def validate(self):
-		self.check_for_duplicate()
+		# self.check_for_duplicate()
 		self.validate_all_link_fields()
 		self.validate_duplicate_groups()
 		self.check_default_payment()
@@ -94,3 +94,44 @@
 @frappe.whitelist()
 def get_series():
 	return frappe.get_meta("Sales Invoice").get_field("naming_series").options or ""
+
+@frappe.whitelist()
+def get_pos_profiles_for_user(user=None):
+	out = []
+	if not user:
+		user = frappe.session.user
+
+	res = frappe.db.sql('''
+		select
+			parent
+		from
+			`tabPOS Profile User`
+		where
+			user = %s
+	''', (user), as_dict=1)
+
+	if not res:
+		company = frappe.defaults.get_user_default('company')
+		res = frappe.db.sql('''
+			select
+				pos_profile_name
+			from
+				`tabPOS Profile`
+			where
+				company = %s
+		''', (company), as_dict=1)
+
+		out = [r.pos_profile_name for r in res]
+
+		return out
+
+	for r in res:
+		name = frappe.db.get_value('POS Profile', r.parent, 'pos_profile_name')
+		out.append(name)
+
+	return out
+
+@frappe.whitelist()
+def get_pos_profile(pos_profile_name):
+	name = frappe.db.get_value('POS Profile', { 'pos_profile_name': pos_profile_name })
+	return frappe.get_doc('POS Profile', name)
diff --git a/erpnext/accounts/doctype/pos_profile_user/__init__.py b/erpnext/accounts/doctype/pos_profile_user/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/accounts/doctype/pos_profile_user/__init__.py
diff --git a/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.js b/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.js
new file mode 100644
index 0000000..2f482d6
--- /dev/null
+++ b/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.js
@@ -0,0 +1,8 @@
+// Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on('POS Profile User', {
+	refresh: function(frm) {
+
+	}
+});
diff --git a/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.json b/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.json
new file mode 100644
index 0000000..22c7f72
--- /dev/null
+++ b/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.json
@@ -0,0 +1,93 @@
+{
+ "allow_copy": 0, 
+ "allow_guest_to_view": 0, 
+ "allow_import": 0, 
+ "allow_rename": 0, 
+ "beta": 0, 
+ "creation": "2017-10-27 16:46:06.060930", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "", 
+ "editable_grid": 1, 
+ "engine": "InnoDB", 
+ "fields": [
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "user", 
+   "fieldtype": "Link", 
+   "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, 
+   "label": "User", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "User", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }
+ ], 
+ "has_web_view": 0, 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "idx": 0, 
+ "image_view": 0, 
+ "in_create": 0, 
+ "is_submittable": 0, 
+ "issingle": 0, 
+ "istable": 0, 
+ "max_attachments": 0, 
+ "modified": "2017-10-27 16:46:12.784244", 
+ "modified_by": "Administrator", 
+ "module": "Accounts", 
+ "name": "POS Profile User", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [
+  {
+   "amend": 0, 
+   "apply_user_permissions": 0, 
+   "cancel": 0, 
+   "create": 1, 
+   "delete": 1, 
+   "email": 1, 
+   "export": 1, 
+   "if_owner": 0, 
+   "import": 0, 
+   "permlevel": 0, 
+   "print": 1, 
+   "read": 1, 
+   "report": 1, 
+   "role": "System Manager", 
+   "set_user_permissions": 0, 
+   "share": 1, 
+   "submit": 0, 
+   "write": 1
+  }
+ ], 
+ "quick_entry": 1, 
+ "read_only": 0, 
+ "read_only_onload": 0, 
+ "show_name_in_global_search": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC", 
+ "track_changes": 1, 
+ "track_seen": 0
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.py b/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.py
new file mode 100644
index 0000000..755b1e9
--- /dev/null
+++ b/erpnext/accounts/doctype/pos_profile_user/pos_profile_user.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.model.document import Document
+
+class POSProfileUser(Document):
+	pass
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index fd7a1b4..5f5b46b 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -448,6 +448,7 @@
 erpnext.patches.v8_9.delete_gst_doctypes_for_outside_india_accounts
 erpnext.patches.v8_9.set_default_fields_in_variant_settings
 erpnext.patches.v8_9.update_billing_gstin_for_indian_account
+erpnext.patches.v9_0.add_user_to_child_table_in_pos_profile
 erpnext.patches.v9_0.set_schedule_date_for_material_request_and_purchase_order
 erpnext.patches.v9_0.student_admission_childtable_migrate
 erpnext.patches.v9_0.fix_subscription_next_date #2017-10-23
diff --git a/erpnext/patches/v9_0/add_user_to_child_table_in_pos_profile.py b/erpnext/patches/v9_0/add_user_to_child_table_in_pos_profile.py
new file mode 100644
index 0000000..b2093c6
--- /dev/null
+++ b/erpnext/patches/v9_0/add_user_to_child_table_in_pos_profile.py
@@ -0,0 +1,21 @@
+# Copyright (c) 2017, Frappe and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	doctype = 'POS Profile'
+	frappe.reload_doctype(doctype)
+
+	for doc in frappe.get_all(doctype):
+		_doc = frappe.get_doc(doctype, doc.name)
+		user = frappe.db.get_value(doctype, doc.name, 'user')
+
+		if not user: continue
+
+		_doc.append('applicable_for_users', {
+			'user': user
+		})
+		_doc.pos_profile_name = user + ' - ' + _doc.company
+		_doc.save()
diff --git a/erpnext/selling/page/point_of_sale/point_of_sale.js b/erpnext/selling/page/point_of_sale/point_of_sale.js
index 196bb96..6f1cd9f 100644
--- a/erpnext/selling/page/point_of_sale/point_of_sale.js
+++ b/erpnext/selling/page/point_of_sale/point_of_sale.js
@@ -283,26 +283,60 @@
 	}
 
 	setup_pos_profile() {
+		return new Promise((resolve) => {
+			const on_submit = ({ pos_profile }) => {
+				this.get_pos_profile_doc(pos_profile)
+					.then(doc => {
+						this.pos_profile = doc;
+
+						if (!this.pos_profile) {
+              this.pos_profile = {
+                company: this.company,
+                currency: frappe.defaults.get_default('currency'),
+                selling_price_list: frappe.defaults.get_default('selling_price_list')
+              };
+            }
+
+						resolve();
+					});
+			}
+
+			frappe.call({
+				method: 'erpnext.accounts.doctype.pos_profile.pos_profile.get_pos_profiles_for_user'
+			})
+			.then((r) => {
+				if (r && r.message) {
+					const pos_profiles = r.message;
+
+					if(pos_profiles.length === 1) {
+						// load profile directly
+						on_submit({pos_profile: pos_profiles[0]});
+					} else {
+						// ask prompt
+						frappe.prompt(
+							[{ fieldtype: 'Select', label: 'POS Profile', options: pos_profiles }],
+							on_submit,
+							__('Select POS Profile')
+						)
+					}
+				}
+			});
+		});
+	}
+
+	get_pos_profile_doc(pos_profile_name) {
 		return new Promise(resolve => {
 			frappe.call({
-				method: 'erpnext.stock.get_item_details.get_pos_profile',
+				method: 'erpnext.accounts.doctype.pos_profile.pos_profile.get_pos_profile',
 				args: {
-					company: this.company
+					pos_profile_name
+				},
+				callback: (r) => {
+					resolve(r.message);
 				}
-			}).then(r => {
-				this.pos_profile = r.message;
-
-				if (!this.pos_profile) {
-					this.pos_profile = {
-						company: this.company,
-						currency: frappe.defaults.get_default('currency'),
-						selling_price_list: frappe.defaults.get_default('selling_price_list')
-					};
-				}
-				resolve();
 			});
-		})
-	}
+		});
+  }
 
 	setup_company() {
 		this.company = frappe.sys_defaults.company;