Merge pull request #8129 from rohitwaghchaure/pos_v8_enhance_and_issue

[fix] customer edit, numeric keypad visibility, on selection of item highlight the background issue in the pos
diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py
index 49e2f15..59766bd 100644
--- a/erpnext/accounts/doctype/sales_invoice/pos.py
+++ b/erpnext/accounts/doctype/sales_invoice/pos.py
@@ -170,6 +170,9 @@
 
 def get_customers_address(customers):
 	customer_address = {}
+	if isinstance(customers, basestring):
+		customers = [frappe._dict({'name': customers})]
+
 	for data in customers:
 		address = frappe.db.sql(""" select name, address_line1, address_line2, city, state,
 			email_id, phone, fax, pincode from `tabAddress` where is_primary_address =1 and name in
@@ -292,6 +295,7 @@
 			if not frappe.db.exists('Sales Invoice', {'offline_pos_name': name}):
 				validate_records(doc)
 				si_doc = frappe.new_doc('Sales Invoice')
+				si_doc.due_date = doc.get('posting_date')
 				si_doc.offline_pos_name = name
 				si_doc.update(doc)
 				submit_invoice(si_doc, name, doc)
@@ -328,10 +332,16 @@
 	customer_doc.flags.ignore_mandatory = True
 	customer_doc.save(ignore_permissions = True)
 	frappe.db.commit()
+	return customer_doc.name
 
 def make_address(args, customer):
-	if args.get('name'):
-		address = frappe.get_doc('Address', args.get('name'))
+	if not args.get('address_line1'): return
+	
+	name = args.get('name') or get_customers_address(customer)[customer].get("name")
+
+	if name:
+		address = frappe.get_doc('Address', name) 
+		frappe.errprint(address)
 	else:
 		address = frappe.new_doc('Address')
 		address.country = frappe.db.get_value('Company', args.get('company'), 'country')
diff --git a/erpnext/accounts/page/pos/pos.js b/erpnext/accounts/page/pos/pos.js
index c021000..fb89d7e 100644
--- a/erpnext/accounts/page/pos/pos.js
+++ b/erpnext/accounts/page/pos/pos.js
@@ -475,6 +475,7 @@
 		this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
 		this.list_customers = this.wrapper.find('.list-customers');
 		this.numeric_keypad = this.wrapper.find('.numeric_keypad');
+		this.list_customers_btn.addClass("view_customer")
 
 		me.render_list_customers();
 		me.toggle_totals_area(false);
@@ -495,9 +496,7 @@
 				me.toggle_totals_area(false);
 				me.toggle_delete_button()
 				me.list_customers.hide();
-				if(me.frm.doc.items.length > 0) {
-					me.numeric_keypad.show();
-				}
+				me.numeric_keypad.show();
 			}
 		});
 		this.add_customer_btn.on('click', function() {
@@ -694,7 +693,8 @@
 						.get(0);
 				}
 			});
-		var customers = this.customers.map(function (c) {
+
+		this.customers_mapper = this.customers.map(function (c) {
 			return {
 				label: c.name,
 				value: c.name,
@@ -703,7 +703,7 @@
 			}
 		});
 
-		customers.push({
+		this.customers_mapper.push({
 			label: "<span class='text-primary link-option'>"
 			+ "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
 			+ __("Create a new Customer")
@@ -711,11 +711,11 @@
 			value: 'is_action',
 			action: me.add_customer
 		});
-		this.party_field.awesomeplete.list = customers;
+		this.autocomplete_customers();
 
 		this.party_field.$input
 			.on('input', function (e) {
-				me.party_field.awesomeplete.list = customers;
+				me.party_field.awesomeplete.list = this.customers_mapper;
 			})
 			.on('awesomplete-select', function (e) {
 				var customer = me.party_field.awesomeplete
@@ -731,6 +731,7 @@
 				me.update_customer_data(customer);
 				me.refresh();
 				me.set_focus();
+				me.list_customers_btn.removeClass("view_customer");
 			})
 			.on('focus', function (e) {
 				$(e.target).val('').trigger('input');
@@ -754,6 +755,10 @@
 			});
 	},
 
+	autocomplete_customers: function() {
+		this.party_field.awesomeplete.list = this.customers_mapper;
+	},
+
 	toggle_edit_button: function(flag) {
 		this.page.wrapper.find('.edit-customer-btn').toggle(flag);
 	},
@@ -768,10 +773,10 @@
 
 	add_customer: function() {
 		this.frm.doc.customer = "";
-		this.update_customer()
+		this.update_customer(true)
 	},
 
-	update_customer: function () {
+	update_customer: function (new_customer) {
 		var me = this;
 		if (!this.connection_status) return;
 
@@ -844,14 +849,14 @@
 		this.render_address_data()
 
 		this.customer_doc.set_primary_action(__("Save"), function () {
-			me.make_offline_customer();
+			me.make_offline_customer(new_customer);
 			me.pos_bill.show();
 		});
 	},
 
 	render_address_data: function() {
 		var me = this;
-		this.address_data = this.address[this.frm.doc.customer] || this.get_address_from_localstorage();
+		this.address_data = this.address[this.frm.doc.customer];
 		this.customer_doc.set_values(this.address_data)
 
 		if(!this.customer_doc.fields_dict.full_name.$input.val()) {
@@ -864,20 +869,32 @@
 		return this.address_details[this.frm.doc.customer]
 	},
 
-	make_offline_customer: function() {
+	make_offline_customer: function(new_customer) {
 		this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
 		this.customer_details = this.get_customers_details();
 		this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
 		this.party_field.$input.val(this.frm.doc.customer);
-		this.customers.push({
-			name: this.frm.doc.customer,
-			customer_name: this.frm.doc.customer
-		});
+		this.update_address_and_customer_list(new_customer)
+		this.autocomplete_customers();
 		this.update_customer_in_localstorage()
 		this.update_customer_in_localstorage()
 		this.customer_doc.hide()
 	},
 
+	update_address_and_customer_list: function(new_customer) {
+		var me = this;
+		if(new_customer) {
+			this.customers_mapper.push({
+				label: this.frm.doc.customer,
+				value: this.frm.doc.customer,
+				customer_group: "",
+				territory: ""
+			});
+		}
+
+		this.address[this.frm.doc.customer] = this.customer_doc.get_values();
+	},
+
 	get_prompt_details: function() {
 		this.prompt_details = this.customer_doc.get_values();
 		this.prompt_details['country'] = this.pos_profile_data.country;
@@ -1072,7 +1089,7 @@
 		// if form is local then allow this function
 		// $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
 		$(this.wrapper).on("click", ".pos-item-wrapper", function () {
-			if(me.list_customers_btn.hasClass("view_customer")) return;
+			if($(me.pos_bill).is(":hidden")) return;
 
 			me.customer_validate();
 			if (me.frm.doc.docstatus == 0) {
@@ -1221,7 +1238,7 @@
 
 	customer_validate: function () {
 		var me = this;
-		if (!this.frm.doc.customer) {
+		if (!this.frm.doc.customer || this.party_field.get_value() == "") {
 			frappe.throw(__("Please select customer"))
 		}
 	},
@@ -1339,7 +1356,8 @@
 				projected_qty: d.projected_qty,
 				rate: format_number(d.rate, me.frm.doc.currency),
 				enabled: me.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
-				amount: format_currency(d.amount, me.frm.doc.currency)
+				amount: format_currency(d.amount, me.frm.doc.currency),
+				selected_class: (me.item_code == d.item_code) ? "active" : ""
 			})).appendTo($items);
 		});
 
@@ -1457,12 +1475,14 @@
 			sn = data.serial_no.split('\n')
 			if(sn.length) {
 				serial_no_list = me.serial_no_data[data.item_code]
-				$.each(sn, function(i, serial_no) {
-					if(in_list(Object.keys(serial_no_list), serial_no)) {
-						delete serial_no_list[serial_no]
-					}
-				})
-				me.serial_no_data[data.item_code] = serial_no_list;
+				if(serial_no_list) {
+					$.each(sn, function(i, serial_no) {
+						if(in_list(Object.keys(serial_no_list), serial_no)) {
+							delete serial_no_list[serial_no]
+						}
+					})
+					me.serial_no_data[data.item_code] = serial_no_list;
+				}
 			}
 		})
 	},
diff --git a/erpnext/public/js/pos/pos_bill_item_new.html b/erpnext/public/js/pos/pos_bill_item_new.html
index 80a33d4..cb626ce 100644
--- a/erpnext/public/js/pos/pos_bill_item_new.html
+++ b/erpnext/public/js/pos/pos_bill_item_new.html
@@ -1,4 +1,4 @@
-<div class="pos-list-row pos-bill-item" data-item-code="{{ item_code }}">
+<div class="pos-list-row pos-bill-item {{ selected_class }}" data-item-code="{{ item_code }}">
 	<div class="cell subject">
 		<!--<input class="list-row-checkbox" type="checkbox" data-name="{{item_code}}">-->
 		<a class="grey list-id" title="{{ item_name }}">{{ strip_html(__(item_name)) || item_code }}</a>
diff --git a/erpnext/public/js/pos/pos_selected_item.html b/erpnext/public/js/pos/pos_selected_item.html
index dfc9915..65e4ef1 100644
--- a/erpnext/public/js/pos/pos_selected_item.html
+++ b/erpnext/public/js/pos/pos_selected_item.html
@@ -1,22 +1,22 @@
 <div class="pos-selected-item-action" data-item-code="{%= item_code %}">
 	<div class="pos-list-row">
 		<div class="cell">Quantity:</div>
-		<input class="form-control cell pos-item-qty" value="{%= qty %}"/>
+		<input type="tel" class="form-control cell pos-item-qty" value="{%= qty %}"/>
 	</div>
 	<div class="pos-list-row">
 		<div class="cell">Price List Rate:</div>
-		<input class="form-control cell" disabled value="{%= price_list_rate %}"/>
+		<input type="tel" class="form-control cell" disabled value="{%= price_list_rate %}"/>
 	</div>
 	<div class="pos-list-row">
 		<div class="cell">Discount:</div>
-		<input class="form-control cell pos-item-disc" value="{%= discount_percentage %}">
+		<input type="tel" class="form-control cell pos-item-disc" value="{%= discount_percentage %}">
 	</div>
 	<div class="pos-list-row">
 		<div class="cell">Price:</div>
-		<input class="form-control cell pos-item-price" value="{%= rate %}"/>
-	</div>
+		<input type="tel" class="form-control cell pos-item-price" value="{%= rate %}"/>
+	</div> 
 	<div class="pos-list-row">
 		<div class="cell">Amount:</div>
-		<input class="form-control cell pos-amount" value="{%= amount %}"/>
+		<input type="tel" class="form-control cell pos-amount" value="{%= amount %}"/>
 	</div>
 </div>
\ No newline at end of file