Merge pull request #1342 from akhileshdarjee/hotfix

Item List refresh when price list changed in POS
diff --git a/accounts/doctype/sales_invoice/pos.js b/accounts/doctype/sales_invoice/pos.js
index b71b45e..a594f7f 100644
--- a/accounts/doctype/sales_invoice/pos.js
+++ b/accounts/doctype/sales_invoice/pos.js
@@ -109,6 +109,7 @@
 		this.party = party;
 		this.price_list = (party == "Customer" ? 
 			this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
+		this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
 		this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
 		this.net_total = "net_total_" + export_or_import;
 		this.grand_total = "grand_total_" + export_or_import;
@@ -269,22 +270,17 @@
 				this.frm.cscript.fname,	this.frm.doctype), function(i, d) {
 				if (d.item_code == item_code) {
 					caught = true;
-					if (serial_no) {
-						d.serial_no += '\n' + serial_no;
-						me.frm.script_manager.trigger("serial_no", d.doctype, d.name);
-					}
-					else {
-						d.qty += 1;
-						me.frm.script_manager.trigger("qty", d.doctype, d.name);
-					}
+					if (serial_no)
+						wn.model.set_value(d.doctype, d.name, "serial_no", d.serial_no + '\n' + serial_no);
+					else
+						wn.model.set_value(d.doctype, d.name, "qty", d.qty + 1);
 				}
 			});
 		}
 		
 		// if item not found then add new item
-		if (!caught) {
+		if (!caught)
 			this.add_new_item_to_grid(item_code, serial_no);
-		}
 
 		this.refresh();
 		this.refresh_search_box();
@@ -319,15 +315,16 @@
 					wn.model.clear_doc(d.doctype, d.name);
 					me.refresh_grid();
 				} else {
-					d.qty = qty;
-					me.frm.script_manager.trigger("qty", d.doctype, d.name);
+					wn.model.set_value(d.doctype, d.name, "qty", qty);
 				}
 			}
 		});
-		me.refresh();
+		this.refresh();
 	},
 	refresh: function() {
 		var me = this;
+
+		this.refresh_item_list();
 		this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
 		this.barcode.set_input("");
 
@@ -350,6 +347,14 @@
 				this.make_party();
 		}
 	},
+	refresh_item_list: function() {
+		var me = this;
+		// refresh item list on change of price list
+		if (this.frm.doc[this.price_list_field] != this.price_list) {
+			this.price_list = this.frm.doc[this.price_list_field];
+			this.make_item_list();
+		}
+	},
 	show_items_in_item_cart: function() {
 		var me = this;
 		var $items = this.wrapper.find("#cart tbody").empty();
@@ -383,9 +388,8 @@
 			)).appendTo($items);
 		});
 
-		this.wrapper.find(".increase-qty, .decrease-qty").on("click", function() {
-			var item_code = $(this).closest("tr").attr("id");
-			me.selected_item_qty_operation(item_code, $(this).attr("class"));
+		this.wrapper.find("input.qty").on("focus", function() {
+			$(this).select();
 		});
 	},
 	show_taxes: function() {
@@ -422,10 +426,16 @@
 
 		// append quantity to the respective item after change from input box
 		$(this.wrapper).find("input.qty").on("change", function() {
-			var item_code = $(this).closest("tr")[0].id;
+			var item_code = $(this).closest("tr").attr("id");
 			me.update_qty(item_code, $(this).val());
 		});
 
+		// increase/decrease qty on plus/minus button
+		$(this.wrapper).find(".increase-qty, .decrease-qty").on("click", function() {
+			var tr = $(this).closest("tr");
+			me.increase_decrease_qty(tr, $(this).attr("class"));
+		});
+
 		// on td click toggle the highlighting of row
 		$(this.wrapper).find("#cart tbody tr td").on("click", function() {
 			var row = $(this).closest("tr");
@@ -443,6 +453,15 @@
 		me.refresh_delete_btn();
 		this.barcode.$input.focus();
 	},
+	increase_decrease_qty: function(tr, operation) {
+		var item_code = tr.attr("id");
+		var item_qty = cint(tr.find("input.qty").val());
+
+		if (operation == "increase-qty")
+			this.update_qty(item_code, item_qty + 1);
+		else if (operation == "decrease-qty" && item_qty != 1)
+			this.update_qty(item_code, item_qty - 1);
+	},
 	disable_text_box_and_button: function() {
 		var me = this;
 		// if form is submitted & cancelled then disable all input box & buttons
@@ -514,27 +533,11 @@
 		this.refresh_grid();
 	},
 	refresh_grid: function() {
+		this.frm.dirty();
 		this.frm.fields_dict[this.frm.cscript.fname].grid.refresh();
 		this.frm.script_manager.trigger("calculate_taxes_and_totals");
 		this.refresh();
 	},
-	selected_item_qty_operation: function(item_code, operation) {
-		var me = this;
-		var child = wn.model.get_children(this.frm.doctype + " Item", this.frm.doc.name, 
-			this.frm.cscript.fname, this.frm.doctype);
-
-		$.each(child, function(i, d) {
-			if (d.item_code == item_code) {
-				if (operation == "increase-qty")
-					d.qty += 1;
-				else if (operation == "decrease-qty")
-					d.qty != 1 ? d.qty -= 1 : d.qty = 1;
-
-				me.frm.script_manager.trigger("qty", d.doctype, d.name);
-				me.refresh();
-			}
-		});
-	},
 	make_payment: function() {
 		var me = this;
 		var no_of_items = $(this.wrapper).find("#cart tbody tr").length;