Merge pull request #32809 from monolithon/develop

feat: Optional Hungarian COA for microenterprises
diff --git a/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.js b/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.js
index 46ba27c..28e79b5 100644
--- a/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.js
+++ b/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.js
@@ -12,6 +12,9 @@
 				},
 			};
 		});
+		let no_bank_transactions_text =
+			`<div class="text-muted text-center">${__("No Matching Bank Transactions Found")}</div>`
+		set_field_options("no_bank_transactions", no_bank_transactions_text);
 	},
 
 	onload: function (frm) {
diff --git a/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.json b/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.json
index b643e6e..f666101 100644
--- a/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.json
+++ b/erpnext/accounts/doctype/bank_reconciliation_tool/bank_reconciliation_tool.json
@@ -81,8 +81,7 @@
   },
   {
    "fieldname": "no_bank_transactions",
-   "fieldtype": "HTML",
-   "options": "<div class=\"text-muted text-center\">No Matching Bank Transactions Found</div>"
+   "fieldtype": "HTML"
   }
  ],
  "hide_toolbar": 1,
@@ -109,4 +108,4 @@
  "quick_entry": 1,
  "sort_field": "modified",
  "sort_order": "DESC"
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/doctype/bank_statement_import/bank_statement_import.js b/erpnext/accounts/doctype/bank_statement_import/bank_statement_import.js
index f745620..04af323 100644
--- a/erpnext/accounts/doctype/bank_statement_import/bank_statement_import.js
+++ b/erpnext/accounts/doctype/bank_statement_import/bank_statement_import.js
@@ -100,7 +100,7 @@
 
 		if (frm.doc.status.includes("Success")) {
 			frm.add_custom_button(
-				__("Go to {0} List", [frm.doc.reference_doctype]),
+				__("Go to {0} List", [__(frm.doc.reference_doctype)]),
 				() => frappe.set_route("List", frm.doc.reference_doctype)
 			);
 		}
diff --git a/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.json b/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.json
index 3f85668..4bb1865 100644
--- a/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.json
+++ b/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.json
@@ -8,6 +8,7 @@
  "engine": "InnoDB",
  "field_order": [
   "barcode",
+  "has_item_scanned",
   "item_code",
   "col_break1",
   "item_name",
@@ -808,11 +809,19 @@
    "fieldtype": "Check",
    "label": "Grant Commission",
    "read_only": 1
+  },
+  {
+   "default": "0",
+   "depends_on": "barcode",
+   "fieldname": "has_item_scanned",
+   "fieldtype": "Check",
+   "label": "Has Item Scanned",
+   "read_only": 1
   }
  ],
  "istable": 1,
  "links": [],
- "modified": "2021-10-05 12:23:47.506290",
+ "modified": "2022-11-02 12:52:39.125295",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "POS Invoice Item",
@@ -820,5 +829,6 @@
  "owner": "Administrator",
  "permissions": [],
  "sort_field": "modified",
- "sort_order": "DESC"
+ "sort_order": "DESC",
+ "states": []
 }
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json
index 7f1a1ec..77055f9 100644
--- a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json
+++ b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json
@@ -8,6 +8,7 @@
  "engine": "InnoDB",
  "field_order": [
   "barcode",
+  "has_item_scanned",
   "item_code",
   "col_break1",
   "item_name",
@@ -872,12 +873,20 @@
    "label": "Purchase Order Item",
    "print_hide": 1,
    "read_only": 1
+  },
+  {
+   "default": "0",
+   "depends_on": "barcode",
+   "fieldname": "has_item_scanned",
+   "fieldtype": "Check",
+   "label": "Has Item Scanned",
+   "read_only": 1
   }
  ],
  "idx": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-10-26 11:38:36.119339",
+ "modified": "2022-11-02 12:53:12.693217",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Sales Invoice Item",
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index f2ee1eb..a195c57 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -1009,7 +1009,7 @@
 				"{range3}-{range4}".format(
 					range3=cint(self.filters["range3"]) + 1, range4=self.filters["range4"]
 				),
-				"{range4}-{above}".format(range4=cint(self.filters["range4"]) + 1, above=_("Above")),
+				_("{range4}-Above").format(range4=cint(self.filters["range4"]) + 1),
 			]
 		):
 			self.add_column(label=label, fieldname="range" + str(i + 1))
diff --git a/erpnext/accounts/report/budget_variance_report/budget_variance_report.js b/erpnext/accounts/report/budget_variance_report/budget_variance_report.js
index 718b6e2..5955c2e 100644
--- a/erpnext/accounts/report/budget_variance_report/budget_variance_report.js
+++ b/erpnext/accounts/report/budget_variance_report/budget_variance_report.js
@@ -75,7 +75,7 @@
 	"formatter": function (value, row, column, data, default_formatter) {
 		value = default_formatter(value, row, column, data);
 
-		if (column.fieldname.includes('variance')) {
+		if (column.fieldname.includes(__("variance"))) {
 
 			if (data[column.fieldname] < 0) {
 				value = "<span style='color:red'>" + value + "</span>";
diff --git a/erpnext/accounts/report/budget_variance_report/budget_variance_report.py b/erpnext/accounts/report/budget_variance_report/budget_variance_report.py
index 7b774ba..96cfab9 100644
--- a/erpnext/accounts/report/budget_variance_report/budget_variance_report.py
+++ b/erpnext/accounts/report/budget_variance_report/budget_variance_report.py
@@ -383,8 +383,8 @@
 		"data": {
 			"labels": labels,
 			"datasets": [
-				{"name": "Budget", "chartType": "bar", "values": budget_values},
-				{"name": "Actual Expense", "chartType": "bar", "values": actual_values},
+				{"name": _("Budget"), "chartType": "bar", "values": budget_values},
+				{"name": _("Actual Expense"), "chartType": "bar", "values": actual_values},
 			],
 		},
 		"type": "bar",
diff --git a/erpnext/accounts/report/deferred_revenue_and_expense/deferred_revenue_and_expense.py b/erpnext/accounts/report/deferred_revenue_and_expense/deferred_revenue_and_expense.py
index 1eb257a..6cc86c3 100644
--- a/erpnext/accounts/report/deferred_revenue_and_expense/deferred_revenue_and_expense.py
+++ b/erpnext/accounts/report/deferred_revenue_and_expense/deferred_revenue_and_expense.py
@@ -396,7 +396,7 @@
 				"labels": [period.label for period in self.period_list],
 				"datasets": [
 					{
-						"name": "Actual Posting",
+						"name": _("Actual Posting"),
 						"chartType": "bar",
 						"values": [x.actual for x in self.period_total],
 					}
@@ -410,7 +410,7 @@
 
 		if self.filters.with_upcoming_postings:
 			chart["data"]["datasets"].append(
-				{"name": "Expected", "chartType": "line", "values": [x.total for x in self.period_total]}
+				{"name": _("Expected"), "chartType": "line", "values": [x.total for x in self.period_total]}
 			)
 
 		return chart
diff --git a/erpnext/assets/doctype/asset/asset.js b/erpnext/assets/doctype/asset/asset.js
index 5512d41..7e54219 100644
--- a/erpnext/assets/doctype/asset/asset.js
+++ b/erpnext/assets/doctype/asset/asset.js
@@ -432,7 +432,11 @@
 
 	set_values_from_purchase_doc: function(frm, doctype, purchase_doc) {
 		frm.set_value('company', purchase_doc.company);
-		frm.set_value('purchase_date', purchase_doc.posting_date);
+		if (purchase_doc.bill_date) {
+			frm.set_value('purchase_date', purchase_doc.bill_date);
+		} else {
+			frm.set_value('purchase_date', purchase_doc.posting_date);
+		}
 		const item = purchase_doc.items.find(item => item.item_code === frm.doc.item_code);
 		if (!item) {
 			doctype_field = frappe.scrub(doctype)
diff --git a/erpnext/buying/doctype/purchase_order/test_purchase_order.py b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
index 6c1bcc7..5a96131 100644
--- a/erpnext/buying/doctype/purchase_order/test_purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
@@ -5,7 +5,7 @@
 import json
 
 import frappe
-from frappe.tests.utils import FrappeTestCase
+from frappe.tests.utils import FrappeTestCase, change_settings
 from frappe.utils import add_days, flt, getdate, nowdate
 from frappe.utils.data import today
 
@@ -709,13 +709,10 @@
 		pi.insert()
 		self.assertTrue(pi.get("payment_schedule"))
 
+	@change_settings("Accounts Settings", {"unlink_advance_payment_on_cancelation_of_order": 1})
 	def test_advance_payment_entry_unlink_against_purchase_order(self):
 		from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry
 
-		frappe.db.set_value(
-			"Accounts Settings", "Accounts Settings", "unlink_advance_payment_on_cancelation_of_order", 1
-		)
-
 		po_doc = create_purchase_order()
 
 		pe = get_payment_entry("Purchase Order", po_doc.name, bank_account="_Test Bank - _TC")
@@ -735,9 +732,31 @@
 		pe_doc = frappe.get_doc("Payment Entry", pe.name)
 		pe_doc.cancel()
 
-		frappe.db.set_value(
-			"Accounts Settings", "Accounts Settings", "unlink_advance_payment_on_cancelation_of_order", 0
-		)
+	@change_settings("Accounts Settings", {"unlink_advance_payment_on_cancelation_of_order": 1})
+	def test_advance_paid_upon_payment_entry_cancellation(self):
+		from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry
+
+		po_doc = create_purchase_order()
+
+		pe = get_payment_entry("Purchase Order", po_doc.name, bank_account="_Test Bank - _TC")
+		pe.reference_no = "1"
+		pe.reference_date = nowdate()
+		pe.paid_from_account_currency = po_doc.currency
+		pe.paid_to_account_currency = po_doc.currency
+		pe.source_exchange_rate = 1
+		pe.target_exchange_rate = 1
+		pe.paid_amount = po_doc.grand_total
+		pe.save(ignore_permissions=True)
+		pe.submit()
+
+		po_doc.reload()
+		self.assertEqual(po_doc.advance_paid, po_doc.base_grand_total)
+
+		pe_doc = frappe.get_doc("Payment Entry", pe.name)
+		pe_doc.cancel()
+
+		po_doc.reload()
+		self.assertEqual(po_doc.advance_paid, 0)
 
 	def test_schedule_date(self):
 		po = create_purchase_order(do_not_submit=True)
diff --git a/erpnext/buying/doctype/supplier/test_supplier.py b/erpnext/buying/doctype/supplier/test_supplier.py
index e2dbf21..b9fc344 100644
--- a/erpnext/buying/doctype/supplier/test_supplier.py
+++ b/erpnext/buying/doctype/supplier/test_supplier.py
@@ -156,6 +156,8 @@
 	def test_serach_fields_for_supplier(self):
 		from erpnext.controllers.queries import supplier_query
 
+		frappe.db.set_value("Buying Settings", None, "supp_master_name", "Naming Series")
+
 		supplier_name = create_supplier(supplier_name="Test Supplier 1").name
 
 		make_property_setter(
@@ -187,6 +189,8 @@
 		self.assertEqual(data[0].supplier_type, "Company")
 		self.assertTrue("supplier_type" in data[0])
 
+		frappe.db.set_value("Buying Settings", None, "supp_master_name", "Supplier Name")
+
 
 def create_supplier(**args):
 	args = frappe._dict(args)
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 22291a3..7f5dc02 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -7,7 +7,7 @@
 import frappe
 from frappe import _, throw
 from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied
-from frappe.query_builder.functions import Sum
+from frappe.query_builder.functions import Abs, Sum
 from frappe.utils import (
 	add_days,
 	add_months,
@@ -1334,30 +1334,20 @@
 		return stock_items
 
 	def set_total_advance_paid(self):
-		if self.doctype == "Sales Order":
-			dr_or_cr = "credit_in_account_currency"
-			rev_dr_or_cr = "debit_in_account_currency"
-			party = self.customer
-		else:
-			dr_or_cr = "debit_in_account_currency"
-			rev_dr_or_cr = "credit_in_account_currency"
-			party = self.supplier
-
-		advance = frappe.db.sql(
-			"""
-			select
-				account_currency, sum({dr_or_cr}) - sum({rev_dr_cr}) as amount
-			from
-				`tabGL Entry`
-			where
-				against_voucher_type = %s and against_voucher = %s and party=%s
-				and docstatus = 1
-		""".format(
-				dr_or_cr=dr_or_cr, rev_dr_cr=rev_dr_or_cr
-			),
-			(self.doctype, self.name, party),
-			as_dict=1,
-		)  # nosec
+		ple = frappe.qb.DocType("Payment Ledger Entry")
+		party = self.customer if self.doctype == "Sales Order" else self.supplier
+		advance = (
+			frappe.qb.from_(ple)
+			.select(ple.account_currency, Abs(Sum(ple.amount)).as_("amount"))
+			.where(
+				(ple.against_voucher_type == self.doctype)
+				& (ple.against_voucher_no == self.name)
+				& (ple.party == party)
+				& (ple.delinked == 0)
+				& (ple.company == self.company)
+			)
+			.run(as_dict=True)
+		)
 
 		if advance:
 			advance = advance[0]
diff --git a/erpnext/controllers/queries.py b/erpnext/controllers/queries.py
index 3bdc017..b0cf724 100644
--- a/erpnext/controllers/queries.py
+++ b/erpnext/controllers/queries.py
@@ -85,7 +85,7 @@
 
 	fields = ["name"]
 	if cust_master_name != "Customer Name":
-		fields = ["customer_name"]
+		fields.append("customer_name")
 
 	fields = get_fields(doctype, fields)
 	searchfields = frappe.get_meta(doctype).get_search_fields()
@@ -123,7 +123,7 @@
 
 	fields = ["name"]
 	if supp_master_name != "Supplier Name":
-		fields = ["supplier_name"]
+		fields.append("supplier_name")
 
 	fields = get_fields(doctype, fields)
 
diff --git a/erpnext/public/js/bank_reconciliation_tool/data_table_manager.js b/erpnext/public/js/bank_reconciliation_tool/data_table_manager.js
index 5bb58fa..9ef8ce6 100644
--- a/erpnext/public/js/bank_reconciliation_tool/data_table_manager.js
+++ b/erpnext/public/js/bank_reconciliation_tool/data_table_manager.js
@@ -30,28 +30,28 @@
 	get_dt_columns() {
 		this.columns = [
 			{
-				name: "Date",
+				name: __("Date"),
 				editable: false,
 				width: 100,
 			},
 
 			{
-				name: "Party Type",
+				name: __("Party Type"),
 				editable: false,
 				width: 95,
 			},
 			{
-				name: "Party",
+				name: __("Party"),
 				editable: false,
 				width: 100,
 			},
 			{
-				name: "Description",
+				name: __("Description"),
 				editable: false,
 				width: 350,
 			},
 			{
-				name: "Deposit",
+				name: __("Deposit"),
 				editable: false,
 				width: 100,
 				format: (value) =>
@@ -60,7 +60,7 @@
 					"</span>",
 			},
 			{
-				name: "Withdrawal",
+				name: __("Withdrawal"),
 				editable: false,
 				width: 100,
 				format: (value) =>
@@ -69,26 +69,26 @@
 					"</span>",
 			},
 			{
-				name: "Unallocated Amount",
+				name: __("Unallocated Amount"),
 				editable: false,
 				width: 100,
 				format: (value) =>
-					"<span style='color:blue;'>" +
+					"<span style='color:var(--blue-500);'>" +
 					format_currency(value, this.currency) +
 					"</span>",
 			},
 			{
-				name: "Reference Number",
+				name: __("Reference Number"),
 				editable: false,
 				width: 140,
 			},
 			{
-				name: "Actions",
+				name: __("Actions"),
 				editable: false,
 				sortable: false,
 				focusable: false,
 				dropdown: false,
-				width: 80,
+				width: 100,
 			},
 		];
 	}
@@ -118,7 +118,7 @@
 			row["reference_number"],
 			`
 			<Button class="btn btn-primary btn-xs center"  data-name = ${row["name"]} >
-				Actions
+				${__("Actions")}
 			</a>
 			`,
 		];
diff --git a/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js b/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js
index cb34c9b..ca01f68 100644
--- a/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js
+++ b/erpnext/public/js/bank_reconciliation_tool/dialog_manager.js
@@ -87,33 +87,33 @@
 	get_dt_columns() {
 		this.columns = [
 			{
-				name: "Document Type",
+				name: __("Document Type"),
 				editable: false,
 				width: 125,
 			},
 			{
-				name: "Document Name",
+				name: __("Document Name"),
 				editable: false,
 				width: 150,
 			},
 			{
-				name: "Reference Date",
+				name: __("Reference Date"),
 				editable: false,
 				width: 120,
 			},
 			{
-				name: "Amount",
+				name: __("Amount"),
 				editable: false,
 				width: 100,
 			},
 			{
-				name: "Party",
+				name: __("Party"),
 				editable: false,
 				width: 120,
 			},
 
 			{
-				name: "Reference Number",
+				name: __("Reference Number"),
 				editable: false,
 				width: 140,
 			},
@@ -222,7 +222,7 @@
 			{
 				fieldtype: "HTML",
 				fieldname: "no_matching_vouchers",
-				options: "<div class=\"text-muted text-center\">No Matching Vouchers Found</div>"
+				options: __("<div class=\"text-muted text-center\">{0}</div>", [__("No Matching Vouchers Found")])
 			},
 			{
 				fieldtype: "Section Break",
@@ -444,10 +444,7 @@
 				vouchers: vouchers,
 			},
 			callback: (response) => {
-				const alert_string =
-					"Bank Transaction " +
-					this.bank_transaction.name +
-					" Matched";
+				const alert_string = __("Bank Transaction {0} Matched", [this.bank_transaction.name]);
 				frappe.show_alert(alert_string);
 				this.update_dt_cards(response.message);
 				this.dialog.hide();
@@ -471,10 +468,7 @@
 				cost_center: values.cost_center,
 			},
 			callback: (response) => {
-				const alert_string =
-					"Bank Transaction " +
-					this.bank_transaction.name +
-					" added as Payment Entry";
+				const alert_string = __("Bank Transaction {0} added as Payment Entry", [this.bank_transaction.name]);
 				frappe.show_alert(alert_string);
 				this.update_dt_cards(response.message);
 				this.dialog.hide();
@@ -498,10 +492,7 @@
 				second_account: values.second_account,
 			},
 			callback: (response) => {
-				const alert_string =
-					"Bank Transaction " +
-					this.bank_transaction.name +
-					" added as Journal Entry";
+				const alert_string = __("Bank Transaction {0} added as Journal Entry", [this.bank_transaction.name]);
 				frappe.show_alert(alert_string);
 				this.update_dt_cards(response.message);
 				this.dialog.hide();
@@ -520,10 +511,7 @@
 				party: values.party,
 			},
 			callback: (response) => {
-				const alert_string =
-					"Bank Transaction " +
-					this.bank_transaction.name +
-					" updated";
+				const alert_string = __("Bank Transaction {0} updated", [this.bank_transaction.name]);
 				frappe.show_alert(alert_string);
 				this.update_dt_cards(response.message);
 				this.dialog.hide();
diff --git a/erpnext/public/js/bank_reconciliation_tool/number_card.js b/erpnext/public/js/bank_reconciliation_tool/number_card.js
index e10d109..7e1b269 100644
--- a/erpnext/public/js/bank_reconciliation_tool/number_card.js
+++ b/erpnext/public/js/bank_reconciliation_tool/number_card.js
@@ -15,20 +15,20 @@
 		var chart_data = [
 			{
 				value: this.bank_statement_closing_balance,
-				label: "Closing Balance as per Bank Statement",
+				label: __("Closing Balance as per Bank Statement"),
 				datatype: "Currency",
 				currency: this.currency,
 			},
 			{
 				value: this.cleared_balance,
-				label: "Closing Balance as per ERP",
+				label: __("Closing Balance as per ERP"),
 				datatype: "Currency",
 				currency: this.currency,
 			},
 			{
 				value:
 					this.bank_statement_closing_balance - this.cleared_balance,
-				label: "Difference",
+				label: __("Difference"),
 				datatype: "Currency",
 				currency: this.currency,
 			},
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index dd957c7..677ca78 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -341,6 +341,7 @@
 		this.set_dynamic_labels();
 		this.setup_sms();
 		this.setup_quality_inspection();
+		this.validate_has_items();
 	}
 
 	scan_barcode() {
@@ -348,6 +349,12 @@
 		barcode_scanner.process_scan();
 	}
 
+	validate_has_items () {
+		let table = this.frm.doc.items;
+		this.frm.has_items = (table && table.length
+			&& table[0].qty && table[0].item_code);
+	}
+
 	apply_default_taxes() {
 		var me = this;
 		var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
diff --git a/erpnext/public/js/utils/barcode_scanner.js b/erpnext/public/js/utils/barcode_scanner.js
index 83b108b..a4f74bd 100644
--- a/erpnext/public/js/utils/barcode_scanner.js
+++ b/erpnext/public/js/utils/barcode_scanner.js
@@ -47,42 +47,49 @@
 				return;
 			}
 
-			frappe
-				.call({
-					method: this.scan_api,
-					args: {
-						search_value: input,
-					},
-				})
-				.then((r) => {
-					const data = r && r.message;
-					if (!data || Object.keys(data).length === 0) {
-						this.show_alert(__("Cannot find Item with this Barcode"), "red");
-						this.clean_up();
-						this.play_fail_sound();
-						reject();
-						return;
-					}
+			this.scan_api_call(input, (r) => {
+				const data = r && r.message;
+				if (!data || Object.keys(data).length === 0) {
+					this.show_alert(__("Cannot find Item with this Barcode"), "red");
+					this.clean_up();
+					this.play_fail_sound();
+					reject();
+					return;
+				}
 
-					me.update_table(data).then(row => {
-						this.play_success_sound();
-						resolve(row);
-					}).catch(() => {
-						this.play_fail_sound();
-						reject();
-					});
+				me.update_table(data).then(row => {
+					this.play_success_sound();
+					resolve(row);
+				}).catch(() => {
+					this.play_fail_sound();
+					reject();
 				});
+			});
 		});
 	}
 
+	scan_api_call(input, callback) {
+		frappe
+			.call({
+				method: this.scan_api,
+				args: {
+					search_value: input,
+				},
+			})
+			.then((r) => {
+				callback(r);
+			});
+	}
+
 	update_table(data) {
 		return new Promise((resolve, reject) => {
 			let cur_grid = this.frm.fields_dict[this.items_table_name].grid;
 
 			const {item_code, barcode, batch_no, serial_no, uom} = data;
 
-			let row = this.get_row_to_modify_on_scan(item_code, batch_no, uom);
+			let row = this.get_row_to_modify_on_scan(item_code, batch_no, uom, barcode);
 
+			this.is_new_row = false;
 			if (!row) {
 				if (this.dont_allow_new_row) {
 					this.show_alert(__("Maximum quantity scanned for item {0}.", [item_code]), "red");
@@ -90,11 +97,13 @@
 					reject();
 					return;
 				}
+				this.is_new_row = true;
 
 				// add new row if new item/batch is scanned
 				row = frappe.model.add_child(this.frm.doc, cur_grid.doctype, this.items_table_name);
 				// trigger any row add triggers defined on child table.
 				this.frm.script_manager.trigger(`${this.items_table_name}_add`, row.doctype, row.name);
+				this.frm.has_items = false;
 			}
 
 			if (this.is_duplicate_serial_no(row, serial_no)) {
@@ -105,7 +114,7 @@
 
 			frappe.run_serially([
 				() => this.set_selector_trigger_flag(data),
-				() => this.set_item(row, item_code).then(qty => {
+				() => this.set_item(row, item_code, barcode, batch_no, serial_no).then(qty => {
 					this.show_scan_message(row.idx, row.item_code, qty);
 				}),
 				() => this.set_barcode_uom(row, uom),
@@ -136,7 +145,7 @@
 		frappe.flags.hide_serial_batch_dialog = false;
 	}
 
-	set_item(row, item_code) {
+	set_item(row, item_code, barcode, batch_no, serial_no) {
 		return new Promise(resolve => {
 			const increment = async (value = 1) => {
 				const item_data = {item_code: item_code};
@@ -149,12 +158,186 @@
 				frappe.prompt(__("Please enter quantity for item {0}", [item_code]), ({value}) => {
 					increment(value).then((value) => resolve(value));
 				});
+			} else if (this.frm.has_items) {
+				this.prepare_item_for_scan(row, item_code, barcode, batch_no, serial_no);
 			} else {
 				increment().then((value) => resolve(value));
 			}
 		});
 	}
 
+	prepare_item_for_scan(row, item_code, barcode, batch_no, serial_no) {
+		var me = this;
+		this.dialog = new frappe.ui.Dialog({
+			title: __("Scan barcode for item {0}", [item_code]),
+			fields: me.get_fields_for_dialog(row, item_code, barcode, batch_no, serial_no),
+		})
+
+		this.dialog.set_primary_action(__("Update"), () => {
+			const item_data = {item_code: item_code};
+			item_data[this.qty_field] = this.dialog.get_value("scanned_qty");
+			item_data["has_item_scanned"] = 1;
+
+			this.remaining_qty = flt(this.dialog.get_value("qty")) - flt(this.dialog.get_value("scanned_qty"));
+			frappe.model.set_value(row.doctype, row.name, item_data);
+
+			frappe.run_serially([
+				() => this.set_batch_no(row, this.dialog.get_value("batch_no")),
+				() => this.set_barcode(row, this.dialog.get_value("barcode")),
+				() => this.set_serial_no(row, this.dialog.get_value("serial_no")),
+				() => this.add_child_for_remaining_qty(row),
+				() => this.clean_up()
+			]);
+
+			this.dialog.hide();
+		});
+
+		this.dialog.show();
+
+		this.$scan_btn = this.dialog.$wrapper.find(".link-btn");
+		this.$scan_btn.css("display", "inline");
+	}
+
+	get_fields_for_dialog(row, item_code, barcode, batch_no, serial_no) {
+		let fields = [
+			{
+				fieldtype: "Data",
+				fieldname: "barcode_scanner",
+				options: "Barcode",
+				label: __("Scan Barcode"),
+				onchange: (e) => {
+					if (!e) {
+						return;
+					}
+
+					if (e.target.value) {
+						this.scan_api_call(e.target.value, (r) => {
+							if (r.message) {
+								this.update_dialog_values(item_code, r);
+							}
+						})
+					}
+				}
+			},
+			{
+				fieldtype: "Section Break",
+			},
+			{
+				fieldtype: "Float",
+				fieldname: "qty",
+				label: __("Quantity to Scan"),
+				default: row[this.qty_field] || 1,
+			},
+			{
+				fieldtype: "Column Break",
+				fieldname: "column_break_1",
+			},
+			{
+				fieldtype: "Float",
+				read_only: 1,
+				fieldname: "scanned_qty",
+				label: __("Scanned Quantity"),
+				default: 1,
+			},
+			{
+				fieldtype: "Section Break",
+			}
+		]
+
+		if (batch_no) {
+			fields.push({
+				fieldtype: "Link",
+				fieldname: "batch_no",
+				options: "Batch No",
+				label: __("Batch No"),
+				default: batch_no,
+				read_only: 1,
+				hidden: 1
+			});
+		}
+
+		if (serial_no) {
+			fields.push({
+				fieldtype: "Small Text",
+				fieldname: "serial_no",
+				label: __("Serial Nos"),
+				default: serial_no,
+				read_only: 1,
+			});
+		}
+
+		if (barcode) {
+			fields.push({
+				fieldtype: "Data",
+				fieldname: "barcode",
+				options: "Barcode",
+				label: __("Barcode"),
+				default: barcode,
+				read_only: 1,
+				hidden: 1
+			});
+		}
+
+		return fields;
+	}
+
+	update_dialog_values(scanned_item, r) {
+		const {item_code, barcode, batch_no, serial_no} = r.message;
+
+		this.dialog.set_value("barcode_scanner", "");
+		if (item_code === scanned_item &&
+			(this.dialog.get_value("barcode") === barcode || batch_no || serial_no)) {
+
+			if (batch_no) {
+				this.dialog.set_value("batch_no", batch_no);
+			}
+
+			if (serial_no) {
+
+				this.validate_duplicate_serial_no(serial_no);
+				let serial_nos = this.dialog.get_value("serial_no") + "\n" + serial_no;
+				this.dialog.set_value("serial_no", serial_nos);
+			}
+
+			let qty = flt(this.dialog.get_value("scanned_qty")) + 1.0;
+			this.dialog.set_value("scanned_qty", qty);
+		}
+	}
+
+	validate_duplicate_serial_no(serial_no) {
+		let serial_nos = this.dialog.get_value("serial_no") ?
+			this.dialog.get_value("serial_no").split("\n") : [];
+
+		if (in_list(serial_nos, serial_no)) {
+			frappe.throw(__("Serial No {0} already scanned", [serial_no]));
+		}
+	}
+
+	add_child_for_remaining_qty(prev_row) {
+		if (this.remaining_qty && this.remaining_qty >0) {
+			let cur_grid = this.frm.fields_dict[this.items_table_name].grid;
+			let row = frappe.model.add_child(this.frm.doc, cur_grid.doctype, this.items_table_name);
+
+			let ignore_fields = ["name", "idx", "batch_no", "barcode",
+				"received_qty", "serial_no", "has_item_scanned"];
+
+			for (let key in prev_row) {
+				if (in_list(ignore_fields, key)) {
+					continue;
+				}
+
+				row[key] = prev_row[key];
+			}
+
+			row[this.qty_field] = this.remaining_qty;
+			if (this.qty_field == "qty" && frappe.meta.has_field(row.doctype, "stock_qty")) {
+				row["stock_qty"] = this.remaining_qty * row.conversion_factor;
+			}
+
+			this.frm.script_manager.trigger("item_code", row.doctype, row.name);
+		}
+	}
+
 	async set_serial_no(row, serial_no) {
 		if (serial_no && frappe.meta.has_field(row.doctype, this.serial_no_field)) {
 			const existing_serial_nos = row[this.serial_no_field];
@@ -205,7 +388,7 @@
 		return is_duplicate;
 	}
 
-	get_row_to_modify_on_scan(item_code, batch_no, uom) {
+	get_row_to_modify_on_scan(item_code, batch_no, uom, barcode) {
 		let cur_grid = this.frm.fields_dict[this.items_table_name].grid;
 
 		// Check if batch is scanned and table has batch no field
@@ -214,12 +397,14 @@
 
 		const matching_row = (row) => {
 			const item_match = row.item_code == item_code;
-			const batch_match = row[this.batch_no_field] == batch_no;
+			const batch_match = (!row[this.batch_no_field] || row[this.batch_no_field] == batch_no);
 			const uom_match = !uom || row[this.uom_field] == uom;
 			const qty_in_limit = flt(row[this.qty_field]) < flt(row[this.max_qty_field]);
+			const item_scanned = row.has_item_scanned;
 
 			return item_match
 				&& uom_match
+				&& !item_scanned
 				&& (!is_batch_no_scan || batch_match)
 				&& (!check_max_qty || qty_in_limit)
 		}
diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py
index 691adcc..a621c73 100644
--- a/erpnext/selling/doctype/customer/test_customer.py
+++ b/erpnext/selling/doctype/customer/test_customer.py
@@ -345,6 +345,8 @@
 	def test_serach_fields_for_customer(self):
 		from erpnext.controllers.queries import customer_query
 
+		frappe.db.set_value("Selling Settings", None, "cust_master_name", "Naming Series")
+
 		make_property_setter(
 			"Customer", None, "search_fields", "customer_group", "Data", for_doctype="Doctype"
 		)
@@ -369,6 +371,8 @@
 		self.assertEqual(data[0].territory, "_Test Territory")
 		self.assertTrue("territory" in data[0])
 
+		frappe.db.set_value("Selling Settings", None, "cust_master_name", "Customer Name")
+
 
 def get_customer_dict(customer_name):
 	return {
diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py
index adfb39c..dfa341b 100644
--- a/erpnext/selling/doctype/sales_order/test_sales_order.py
+++ b/erpnext/selling/doctype/sales_order/test_sales_order.py
@@ -6,7 +6,7 @@
 import frappe
 import frappe.permissions
 from frappe.core.doctype.user_permission.test_user_permission import create_user
-from frappe.tests.utils import FrappeTestCase
+from frappe.tests.utils import FrappeTestCase, change_settings
 from frappe.utils import add_days, flt, getdate, nowdate, today
 
 from erpnext.controllers.accounts_controller import update_child_qty_rate
@@ -1346,6 +1346,33 @@
 
 		self.assertRaises(frappe.LinkExistsError, so_doc.cancel)
 
+	@change_settings("Accounts Settings", {"unlink_advance_payment_on_cancelation_of_order": 1})
+	def test_advance_paid_upon_payment_cancellation(self):
+		from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry
+
+		so = make_sales_order()
+
+		pe = get_payment_entry("Sales Order", so.name, bank_account="_Test Bank - _TC")
+		pe.reference_no = "1"
+		pe.reference_date = nowdate()
+		pe.paid_from_account_currency = so.currency
+		pe.paid_to_account_currency = so.currency
+		pe.source_exchange_rate = 1
+		pe.target_exchange_rate = 1
+		pe.paid_amount = so.grand_total
+		pe.save(ignore_permissions=True)
+		pe.submit()
+		so.reload()
+
+		self.assertEqual(so.advance_paid, so.base_grand_total)
+
+		# cancel advance payment
+		pe.reload()
+		pe.cancel()
+
+		so.reload()
+		self.assertEqual(so.advance_paid, 0)
+
 	def test_cancel_sales_order_after_cancel_payment_entry(self):
 		from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry
 
diff --git a/erpnext/selling/report/sales_analytics/sales_analytics.py b/erpnext/selling/report/sales_analytics/sales_analytics.py
index 1863528..605d2fa 100644
--- a/erpnext/selling/report/sales_analytics/sales_analytics.py
+++ b/erpnext/selling/report/sales_analytics/sales_analytics.py
@@ -313,11 +313,13 @@
 
 	def get_period(self, posting_date):
 		if self.filters.range == "Weekly":
-			period = "Week " + str(posting_date.isocalendar()[1]) + " " + str(posting_date.year)
+			period = _("Week {0} {1}").format(str(posting_date.isocalendar()[1]), str(posting_date.year))
 		elif self.filters.range == "Monthly":
-			period = str(self.months[posting_date.month - 1]) + " " + str(posting_date.year)
+			period = _(str(self.months[posting_date.month - 1])) + " " + str(posting_date.year)
 		elif self.filters.range == "Quarterly":
-			period = "Quarter " + str(((posting_date.month - 1) // 3) + 1) + " " + str(posting_date.year)
+			period = _("Quarter {0} {1}").format(
+				str(((posting_date.month - 1) // 3) + 1), str(posting_date.year)
+			)
 		else:
 			year = get_fiscal_year(posting_date, company=self.filters.company)
 			period = str(year[0])
diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py
index 52854a0..f14288b 100644
--- a/erpnext/stock/doctype/batch/batch.py
+++ b/erpnext/stock/doctype/batch/batch.py
@@ -291,7 +291,7 @@
 	batches = get_batches(item_code, warehouse, qty, throw, serial_no)
 
 	for batch in batches:
-		if cint(qty) <= cint(batch.qty):
+		if flt(qty) <= flt(batch.qty):
 			batch_no = batch.batch_id
 			break
 
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
index 77c3253..3229463 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -8,6 +8,7 @@
  "engine": "InnoDB",
  "field_order": [
   "barcode",
+  "has_item_scanned",
   "item_code",
   "item_name",
   "col_break1",
@@ -809,13 +810,21 @@
    "label": "Purchase Order Item",
    "print_hide": 1,
    "read_only": 1
+  },
+  {
+   "default": "0",
+   "depends_on": "barcode",
+   "fieldname": "has_item_scanned",
+   "fieldtype": "Check",
+   "label": "Has Item Scanned",
+   "read_only": 1
   }
  ],
  "idx": 1,
  "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-10-26 16:05:17.720768",
+ "modified": "2022-11-02 12:54:07.225623",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Delivery Note Item",
diff --git a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
index 474ee92..557bb59 100644
--- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
+++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
@@ -8,6 +8,7 @@
  "engine": "InnoDB",
  "field_order": [
   "barcode",
+  "has_item_scanned",
   "section_break_2",
   "item_code",
   "product_bundle",
@@ -996,12 +997,20 @@
   {
    "fieldname": "column_break_102",
    "fieldtype": "Column Break"
+  },
+  {
+   "default": "0",
+   "depends_on": "barcode",
+   "fieldname": "has_item_scanned",
+   "fieldtype": "Check",
+   "label": "Has Item Scanned",
+   "read_only": 1
   }
  ],
  "idx": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-10-26 16:06:02.524435",
+ "modified": "2022-11-02 12:49:28.746701",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Purchase Receipt Item",
diff --git a/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json b/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
index 5fe11a2..95f4f5f 100644
--- a/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
+++ b/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
@@ -8,6 +8,7 @@
  "engine": "InnoDB",
  "field_order": [
   "barcode",
+  "has_item_scanned",
   "section_break_2",
   "s_warehouse",
   "col_break1",
@@ -498,14 +499,14 @@
    "read_only": 1
   },
   {
-    "fieldname": "sco_rm_detail",
-    "fieldtype": "Data",
-    "hidden": 1,
-    "label": "SCO Supplied Item",
-    "no_copy": 1,
-    "print_hide": 1,
-    "read_only": 1
-   },
+   "fieldname": "sco_rm_detail",
+   "fieldtype": "Data",
+   "hidden": 1,
+   "label": "SCO Supplied Item",
+   "no_copy": 1,
+   "print_hide": 1,
+   "read_only": 1
+  },
   {
    "default": "0",
    "depends_on": "eval:parent.purpose===\"Repack\" && doc.t_warehouse",
@@ -563,13 +564,21 @@
    "fieldname": "is_process_loss",
    "fieldtype": "Check",
    "label": "Is Process Loss"
+  },
+  {
+   "default": "0",
+   "depends_on": "barcode",
+   "fieldname": "has_item_scanned",
+   "fieldtype": "Check",
+   "label": "Has Item Scanned",
+   "read_only": 1
   }
  ],
  "idx": 1,
  "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2022-06-17 05:06:33.621264",
+ "modified": "2022-11-02 13:00:34.258828",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Stock Entry Detail",
diff --git a/erpnext/stock/doctype/stock_reconciliation_item/stock_reconciliation_item.json b/erpnext/stock/doctype/stock_reconciliation_item/stock_reconciliation_item.json
index 79c2fcc..7c3e151 100644
--- a/erpnext/stock/doctype/stock_reconciliation_item/stock_reconciliation_item.json
+++ b/erpnext/stock/doctype/stock_reconciliation_item/stock_reconciliation_item.json
@@ -7,6 +7,7 @@
  "engine": "InnoDB",
  "field_order": [
   "barcode",
+  "has_item_scanned",
   "item_code",
   "item_name",
   "warehouse",
@@ -177,11 +178,18 @@
    "label": "Allow Zero Valuation Rate",
    "print_hide": 1,
    "read_only": 1
+  },
+  {
+   "depends_on": "barcode",
+   "fieldname": "has_item_scanned",
+   "fieldtype": "Data",
+   "label": "Has Item Scanned",
+   "read_only": 1
   }
  ],
  "istable": 1,
  "links": [],
- "modified": "2022-04-02 04:19:40.380587",
+ "modified": "2022-11-02 13:01:23.580937",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Stock Reconciliation Item",
diff --git a/erpnext/stock/report/stock_ageing/stock_ageing.py b/erpnext/stock/report/stock_ageing/stock_ageing.py
index 7c430e4..2fa97ae 100644
--- a/erpnext/stock/report/stock_ageing/stock_ageing.py
+++ b/erpnext/stock/report/stock_ageing/stock_ageing.py
@@ -198,11 +198,11 @@
 		f"0 - {filters['range1']}",
 		f"{cint(filters['range1']) + 1} - {cint(filters['range2'])}",
 		f"{cint(filters['range2']) + 1} - {cint(filters['range3'])}",
-		f"{cint(filters['range3']) + 1} - {_('Above')}",
+		_("{0} - Above").format(cint(filters["range3"]) + 1),
 	]
 	for i, label in enumerate(ranges):
 		fieldname = "range" + str(i + 1)
-		add_column(range_columns, label=f"Age ({label})", fieldname=fieldname)
+		add_column(range_columns, label=_("Age ({0})").format(label), fieldname=fieldname)
 
 
 def add_column(
diff --git a/erpnext/stock/report/stock_analytics/stock_analytics.py b/erpnext/stock/report/stock_analytics/stock_analytics.py
index 89ca9d9..27b94ab 100644
--- a/erpnext/stock/report/stock_analytics/stock_analytics.py
+++ b/erpnext/stock/report/stock_analytics/stock_analytics.py
@@ -114,11 +114,13 @@
 	months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
 
 	if filters.range == "Weekly":
-		period = "Week " + str(posting_date.isocalendar()[1]) + " " + str(posting_date.year)
+		period = _("Week {0} {1}").format(str(posting_date.isocalendar()[1]), str(posting_date.year))
 	elif filters.range == "Monthly":
-		period = str(months[posting_date.month - 1]) + " " + str(posting_date.year)
+		period = _(str(months[posting_date.month - 1])) + " " + str(posting_date.year)
 	elif filters.range == "Quarterly":
-		period = "Quarter " + str(((posting_date.month - 1) // 3) + 1) + " " + str(posting_date.year)
+		period = _("Quarter {0} {1}").format(
+			str(((posting_date.month - 1) // 3) + 1), str(posting_date.year)
+		)
 	else:
 		year = get_fiscal_year(posting_date, company=filters.company)
 		period = str(year[2])