Merge pull request #25928 from nextchamp-saqib/pos-fixes-8

fix(pos): multiple pos issues
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 1808005..f813425 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -17,7 +17,7 @@
 		var me = this;
 		this._super();
 
-		this.frm.ignore_doctypes_on_cancel_all = ['POS Invoice', 'Timesheet'];
+		this.frm.ignore_doctypes_on_cancel_all = ['POS Invoice', 'Timesheet', 'POS Invoice Merge Log', 'POS Closing Entry'];
 		if(!this.frm.doc.__islocal && !this.frm.doc.customer && this.frm.doc.debit_to) {
 			// show debit_to in print format
 			this.frm.set_df_property("debit_to", "print_hide", 0);
diff --git a/erpnext/selling/page/point_of_sale/point_of_sale.py b/erpnext/selling/page/point_of_sale/point_of_sale.py
index cb811df..7742f24 100644
--- a/erpnext/selling/page/point_of_sale/point_of_sale.py
+++ b/erpnext/selling/page/point_of_sale/point_of_sale.py
@@ -8,38 +8,52 @@
 from erpnext.accounts.doctype.pos_profile.pos_profile import get_item_groups
 from erpnext.accounts.doctype.pos_invoice.pos_invoice import get_stock_availability
 
-from six import string_types
+def search_by_term(search_term, warehouse, price_list):
+	result = search_for_serial_or_batch_or_barcode_number(search_term)
+
+	item_code = result.get("item_code") or search_term
+	serial_no = result.get("serial_no") or ""
+	batch_no = result.get("batch_no") or ""
+	barcode = result.get("barcode") or ""
+
+	if result:
+		item_info = frappe.db.get_value("Item", item_code,
+			["name as item_code", "item_name", "description", "stock_uom", "image as item_image", "is_stock_item"],
+			as_dict=1)
+
+		item_stock_qty = get_stock_availability(item_code, warehouse)
+		price_list_rate, currency = frappe.db.get_value('Item Price', {
+			'price_list': price_list,
+			'item_code': item_code
+		}, ["price_list_rate", "currency"])
+
+		item_info.update({
+			'serial_no': serial_no,
+			'batch_no': batch_no,
+			'barcode': barcode,
+			'price_list_rate': price_list_rate,
+			'currency': currency,
+			'actual_qty': item_stock_qty
+		})
+
+		return {'items': [item_info]}
 
 @frappe.whitelist()
-def get_items(start, page_length, price_list, item_group, pos_profile, search_value=""):
-	data = dict()
+def get_items(start, page_length, price_list, item_group, pos_profile, search_term=""):
+	warehouse, hide_unavailable_items = frappe.db.get_value(
+		'POS Profile', pos_profile, ['warehouse', 'hide_unavailable_items'])
+
 	result = []
 
-	warehouse, hide_unavailable_items = frappe.db.get_value('POS Profile', pos_profile, ['warehouse', 'hide_unavailable_items'])
+	if search_term:
+		result = search_by_term(search_term, warehouse, price_list)
+		if result:
+			return result
 
 	if not frappe.db.exists('Item Group', item_group):
 		item_group = get_root_of('Item Group')
 
-	if search_value:
-		data = search_serial_or_batch_or_barcode_number(search_value)
-
-	item_code = data.get("item_code") if data.get("item_code") else search_value
-	serial_no = data.get("serial_no") if data.get("serial_no") else ""
-	batch_no = data.get("batch_no") if data.get("batch_no") else ""
-	barcode = data.get("barcode") if data.get("barcode") else ""
-
-	if data:
-		item_info = frappe.db.get_value(
-			"Item", data.get("item_code"),
-			["name as item_code", "item_name", "description", "stock_uom", "image as item_image", "is_stock_item"]
-		, as_dict=1)
-		item_info.setdefault('serial_no', serial_no)
-		item_info.setdefault('batch_no', batch_no)
-		item_info.setdefault('barcode', barcode)
-
-		return { 'items': [item_info] }
-
-	condition = get_conditions(item_code, serial_no, batch_no, barcode)
+	condition = get_conditions(search_term)
 	condition += get_item_group_condition(pos_profile)
 
 	lft, rgt = frappe.db.get_value('Item Group', item_group, ['lft', 'rgt'])
@@ -106,14 +120,10 @@
 			})
 			result.append(row)
 
-	res = {
-		'items': result
-	}
-
-	return res
+	return {'items': result}
 
 @frappe.whitelist()
-def search_serial_or_batch_or_barcode_number(search_value):
+def search_for_serial_or_batch_or_barcode_number(search_value):
 	# search barcode no
 	barcode_data = frappe.db.get_value('Item Barcode', {'barcode': search_value}, ['barcode', 'parent as item_code'], as_dict=True)
 	if barcode_data:
@@ -139,27 +149,21 @@
 	
 	return items
 
-def get_conditions(item_code, serial_no, batch_no, barcode):
-	if serial_no or batch_no or barcode:
-		return "item.name = {0}".format(frappe.db.escape(item_code))
-
-	return make_condition(item_code)
-
-def make_condition(item_code):
+def get_conditions(search_term):
 	condition = "("
-	condition += """item.name like {item_code}
-		or item.item_name like {item_code}""".format(item_code = frappe.db.escape('%' + item_code + '%'))
-	condition += add_search_fields_condition(item_code)
+	condition += """item.name like {search_term}
+		or item.item_name like {search_term}""".format(search_term=frappe.db.escape('%' + search_term + '%'))
+	condition += add_search_fields_condition(search_term)
 	condition += ")"
 
 	return condition
 
-def add_search_fields_condition(item_code):
+def add_search_fields_condition(search_term):
 	condition = ''
 	search_fields = frappe.get_all('POS Search Fields', fields = ['fieldname'])
 	if search_fields:
 		for field in search_fields:
-			condition += " or item.{0} like {1}".format(field['fieldname'], frappe.db.escape('%' + item_code + '%'))
+			condition += " or item.`{0}` like {1}".format(field['fieldname'], frappe.db.escape('%' + search_term + '%'))
 	return condition
 
 def get_item_group_condition(pos_profile):
diff --git a/erpnext/selling/page/point_of_sale/pos_item_details.js b/erpnext/selling/page/point_of_sale/pos_item_details.js
index df62696..5e09df8 100644
--- a/erpnext/selling/page/point_of_sale/pos_item_details.js
+++ b/erpnext/selling/page/point_of_sale/pos_item_details.js
@@ -133,13 +133,24 @@
 		this.$item_description.html(get_description_html());
 		this.$item_price.html(format_currency(price_list_rate, this.currency));
 		if (image) {
-			this.$item_image.html(`<img src="${image}" alt="${image}">`);
+			this.$item_image.html(
+				`<img 
+					onerror="cur_pos.item_details.handle_broken_image(this)"
+					class="h-full" src="${image}"
+					alt="${frappe.get_abbr(item_name)}"
+					style="object-fit: cover;">`
+			);
 		} else {
 			this.$item_image.html(`<div class="item-abbr">${frappe.get_abbr(item_name)}</div>`);
 		}
 
 	}
 
+	handle_broken_image($img) {
+		const item_abbr = $($img).attr('alt');
+		$($img).replaceWith(`<div class="item-abbr">${item_abbr}</div>`);
+	}
+
 	render_discount_dom(item) {
 		if (item.discount_percentage) {
 			this.$dicount_section.html(
diff --git a/erpnext/selling/page/point_of_sale/pos_item_selector.js b/erpnext/selling/page/point_of_sale/pos_item_selector.js
index 5b48725..64c529e 100644
--- a/erpnext/selling/page/point_of_sale/pos_item_selector.js
+++ b/erpnext/selling/page/point_of_sale/pos_item_selector.js
@@ -51,7 +51,7 @@
 		});
 	}
 
-	get_items({start = 0, page_length = 40, search_value=''}) {
+	get_items({start = 0, page_length = 40, search_term=''}) {
 		const doc = this.events.get_frm().doc;
 		const price_list = (doc && doc.selling_price_list) || this.price_list;
 		let { item_group, pos_profile } = this;
@@ -61,7 +61,7 @@
 		return frappe.call({
 			method: "erpnext.selling.page.point_of_sale.point_of_sale.get_items",
 			freeze: true,
-			args: { start, page_length, price_list, item_group, search_value, pos_profile },
+			args: { start, page_length, price_list, item_group, search_term, pos_profile },
 		});
 	}
 
@@ -80,6 +80,7 @@
 		// eslint-disable-next-line no-unused-vars
 		const { item_image, serial_no, batch_no, barcode, actual_qty, stock_uom, price_list_rate } = item;
 		const indicator_color = actual_qty > 10 ? "green" : actual_qty <= 0 ? "red" : "orange";
+		const precision = flt(price_list_rate, 2) % 1 != 0 ? 2 : 0;
 
 		let qty_to_display = actual_qty;
 
@@ -121,7 +122,7 @@
 					<div class="item-name">
 						${frappe.ellipsis(item.item_name, 18)}
 					</div>
-					<div class="item-rate">${format_currency(price_list_rate, item.currency, 0) || 0}</div>
+					<div class="item-rate">${format_currency(price_list_rate, item.currency, precision) || 0}</div>
 				</div>
 			</div>`
 		);
@@ -302,7 +303,7 @@
 			}
 		}
 
-		this.get_items({ search_value: search_term })
+		this.get_items({ search_term })
 			.then(({ message }) => {
 				// eslint-disable-next-line no-unused-vars
 				const { items, serial_no, batch_no, barcode } = message;
diff --git a/erpnext/selling/page/point_of_sale/pos_payment.js b/erpnext/selling/page/point_of_sale/pos_payment.js
index 600f160..156fb77 100644
--- a/erpnext/selling/page/point_of_sale/pos_payment.js
+++ b/erpnext/selling/page/point_of_sale/pos_payment.js
@@ -171,7 +171,7 @@
 
 		this.setup_listener_for_payments();
 
-		this.$payment_modes.on('click', '.shortcut', () => {
+		this.$payment_modes.on('click', '.shortcut', function() {
 			const value = $(this).attr('data-value');
 			me.selected_mode.set_value(value);
 		});