Merge branch 'develop' into repost-item-valuation
diff --git a/erpnext/accounts/doctype/bank_clearance/bank_clearance.js b/erpnext/accounts/doctype/bank_clearance/bank_clearance.js
index 7e57c2f..ceba99a 100644
--- a/erpnext/accounts/doctype/bank_clearance/bank_clearance.js
+++ b/erpnext/accounts/doctype/bank_clearance/bank_clearance.js
@@ -37,6 +37,14 @@
 
 	refresh: function(frm) {
 		frm.disable_save();
+
+		if (frm.doc.account && frm.doc.from_date && frm.doc.to_date) {
+			frm.add_custom_button(__('Get Payment Entries'), () =>
+				frm.trigger("get_payment_entries")
+			);
+
+			frm.change_custom_button_type('Get Payment Entries', null, 'primary');
+		}
 	},
 
 	update_clearance_date: function(frm) {
@@ -46,22 +54,30 @@
 			callback: function(r, rt) {
 				frm.refresh_field("payment_entries");
 				frm.refresh_fields();
+
+				if (!frm.doc.payment_entries.length) {
+					frm.change_custom_button_type('Get Payment Entries', null, 'primary');
+					frm.change_custom_button_type('Update Clearance Date', null, 'default');
+				}
 			}
 		});
 	},
+
 	get_payment_entries: function(frm) {
 		return frappe.call({
 			method: "get_payment_entries",
 			doc: frm.doc,
 			callback: function(r, rt) {
 				frm.refresh_field("payment_entries");
-				frm.refresh_fields();
 
-				$(frm.fields_dict.payment_entries.wrapper).find("[data-fieldname=amount]").each(function(i,v){
-					if (i !=0){
-						$(v).addClass("text-right")
-					}
-				})
+				if (frm.doc.payment_entries.length) {
+					frm.add_custom_button(__('Update Clearance Date'), () =>
+						frm.trigger("update_clearance_date")
+					);
+
+					frm.change_custom_button_type('Get Payment Entries', null, 'default');
+					frm.change_custom_button_type('Update Clearance Date', null, 'primary');
+				}
 			}
 		});
 	}
diff --git a/erpnext/accounts/doctype/bank_clearance/bank_clearance.json b/erpnext/accounts/doctype/bank_clearance/bank_clearance.json
index a436d1e..591d019 100644
--- a/erpnext/accounts/doctype/bank_clearance/bank_clearance.json
+++ b/erpnext/accounts/doctype/bank_clearance/bank_clearance.json
@@ -1,4 +1,5 @@
 {
+ "actions": [],
  "allow_copy": 1,
  "creation": "2013-01-10 16:34:05",
  "doctype": "DocType",
@@ -13,11 +14,8 @@
   "bank_account",
   "include_reconciled_entries",
   "include_pos_transactions",
-  "get_payment_entries",
   "section_break_10",
-  "payment_entries",
-  "update_clearance_date",
-  "total_amount"
+  "payment_entries"
  ],
  "fields": [
   {
@@ -77,11 +75,6 @@
    "label": "Include POS Transactions"
   },
   {
-   "fieldname": "get_payment_entries",
-   "fieldtype": "Button",
-   "label": "Get Payment Entries"
-  },
-  {
    "fieldname": "section_break_10",
    "fieldtype": "Section Break"
   },
@@ -91,25 +84,14 @@
    "fieldtype": "Table",
    "label": "Payment Entries",
    "options": "Bank Clearance Detail"
-  },
-  {
-   "fieldname": "update_clearance_date",
-   "fieldtype": "Button",
-   "label": "Update Clearance Date"
-  },
-  {
-   "fieldname": "total_amount",
-   "fieldtype": "Currency",
-   "label": "Total Amount",
-   "options": "account_currency",
-   "read_only": 1
   }
  ],
  "hide_toolbar": 1,
  "icon": "fa fa-check",
  "idx": 1,
  "issingle": 1,
- "modified": "2020-04-06 16:12:06.628008",
+ "links": [],
+ "modified": "2022-11-28 17:24:13.008692",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Bank Clearance",
@@ -126,5 +108,6 @@
  "quick_entry": 1,
  "read_only": 1,
  "sort_field": "modified",
- "sort_order": "ASC"
+ "sort_order": "ASC",
+ "states": []
 }
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/bank_clearance/bank_clearance.py b/erpnext/accounts/doctype/bank_clearance/bank_clearance.py
index 78c3526..80878ac 100644
--- a/erpnext/accounts/doctype/bank_clearance/bank_clearance.py
+++ b/erpnext/accounts/doctype/bank_clearance/bank_clearance.py
@@ -179,7 +179,6 @@
 		)
 
 		self.set("payment_entries", [])
-		self.total_amount = 0.0
 		default_currency = erpnext.get_default_currency()
 
 		for d in entries:
@@ -198,7 +197,6 @@
 			d.pop("debit")
 			d.pop("account_currency")
 			row.update(d)
-			self.total_amount += flt(amount)
 
 	@frappe.whitelist()
 	def update_clearance_date(self):
diff --git a/erpnext/accounts/doctype/pos_invoice/pos_invoice.js b/erpnext/accounts/doctype/pos_invoice/pos_invoice.js
index 15c2922..56b8579 100644
--- a/erpnext/accounts/doctype/pos_invoice/pos_invoice.js
+++ b/erpnext/accounts/doctype/pos_invoice/pos_invoice.js
@@ -5,6 +5,8 @@
 frappe.provide("erpnext.accounts");
 
 erpnext.selling.POSInvoiceController = class POSInvoiceController extends erpnext.selling.SellingController {
+	settings = {};
+
 	setup(doc) {
 		this.setup_posting_date_time_check();
 		super.setup(doc);
@@ -12,21 +14,37 @@
 
 	company() {
 		erpnext.accounts.dimensions.update_dimension(this.frm, this.frm.doctype);
+		this.frm.set_value("set_warehouse", "");
+		this.frm.set_value("taxes_and_charges", "");
 	}
 
 	onload(doc) {
 		super.onload();
 		this.frm.ignore_doctypes_on_cancel_all = ['POS Invoice Merge Log', 'POS Closing Entry'];
+
 		if(doc.__islocal && doc.is_pos && frappe.get_route_str() !== 'point-of-sale') {
 			this.frm.script_manager.trigger("is_pos");
 			this.frm.refresh_fields();
 		}
 
+		this.frm.set_query("set_warehouse", function(doc) {
+			return {
+				filters: {
+					company: doc.company ? doc.company : '',
+				}
+			}
+		});
+
 		erpnext.accounts.dimensions.setup_dimension_filters(this.frm, this.frm.doctype);
 	}
 
+	onload_post_render(frm) {
+		this.pos_profile(frm);
+	}
+
 	refresh(doc) {
 		super.refresh();
+
 		if (doc.docstatus == 1 && !doc.is_return) {
 			this.frm.add_custom_button(__('Return'), this.make_sales_return, __('Create'));
 			this.frm.page.set_inner_btn_group_as_primary(__('Create'));
@@ -36,6 +54,18 @@
 			this.frm.return_print_format = "Sales Invoice Return";
 			this.frm.set_value('consolidated_invoice', '');
 		}
+
+		this.frm.set_query("customer", (function () {
+			const customer_groups = this.settings?.customer_groups;
+
+			if (!customer_groups?.length) return {};
+
+			return {
+				filters: {
+					customer_group: ["in", customer_groups],
+				}
+			}
+		}).bind(this));
 	}
 
 	is_pos() {
@@ -88,6 +118,25 @@
 			});
 	}
 
+	pos_profile(frm) {
+		if (!frm.pos_profile || frm.pos_profile == '') {
+			this.update_customer_groups_settings([]);
+			return;
+		}
+
+		frappe.call({
+			method: "erpnext.selling.page.point_of_sale.point_of_sale.get_pos_profile_data",
+			args: { "pos_profile": frm.pos_profile },
+			callback: ({ message: profile }) => {
+				this.update_customer_groups_settings(profile?.customer_groups);
+			},
+		});
+	}
+
+	update_customer_groups_settings(customer_groups) {
+		this.settings.customer_groups = customer_groups?.map((group) => group.name)
+	}
+
 	amount(){
 		this.write_off_outstanding_amount_automatically()
 	}
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 1849e8b..b7a80c1 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -239,6 +239,14 @@
 					else:
 						item.set(field_map.get(self.doctype), default_deferred_account)
 
+	def validate_auto_repeat_subscription_dates(self):
+		if (
+			self.get("from_date")
+			and self.get("to_date")
+			and getdate(self.from_date) > getdate(self.to_date)
+		):
+			frappe.throw(_("To Date cannot be before From Date"), title=_("Invalid Auto Repeat Date"))
+
 	def validate_deferred_start_and_end_date(self):
 		for d in self.items:
 			if d.get("enable_deferred_revenue") or d.get("enable_deferred_expense"):
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 48fe7cb..0514604 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -41,6 +41,7 @@
 		self.validate_from_warehouse()
 		self.set_supplier_address()
 		self.validate_asset_return()
+		self.validate_auto_repeat_subscription_dates()
 
 		if self.doctype == "Purchase Invoice":
 			self.validate_purchase_receipt_if_update_stock()
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index 88d2f06..965335b 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -40,6 +40,7 @@
 		self.set_customer_address()
 		self.validate_for_duplicate_items()
 		self.validate_target_warehouse()
+		self.validate_auto_repeat_subscription_dates()
 
 	def set_missing_values(self, for_validate=False):
 
diff --git a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py
index d6f9bae..8e914e6 100644
--- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py
+++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py
@@ -5,7 +5,7 @@
 from frappe import _
 from frappe.exceptions import QueryDeadlockError, QueryTimeoutError
 from frappe.model.document import Document
-from frappe.utils import cint, get_link_to_form, get_weekday, now, nowtime
+from frappe.utils import cint, get_link_to_form, get_weekday, getdate, now, nowtime
 from frappe.utils.user import get_users_with_role
 from rq.timeouts import JobTimeoutException
 
@@ -25,6 +25,27 @@
 		self.set_status(write=False)
 		self.reset_field_values()
 		self.set_company()
+		self.validate_accounts_freeze()
+
+	def validate_accounts_freeze(self):
+		acc_settings = frappe.db.get_value(
+			"Accounts Settings",
+			"Accounts Settings",
+			["acc_frozen_upto", "frozen_accounts_modifier"],
+			as_dict=1,
+		)
+		if not acc_settings.acc_frozen_upto:
+			return
+		if getdate(self.posting_date) <= getdate(acc_settings.acc_frozen_upto):
+			if (
+				acc_settings.frozen_accounts_modifier
+				and frappe.session.user in get_users_with_role(acc_settings.frozen_accounts_modifier)
+			):
+				frappe.msgprint(_("Caution: This might alter frozen accounts."))
+				return
+			frappe.throw(
+				_("You cannot repost item valuation before {}").format(acc_settings.acc_frozen_upto)
+			)
 
 	def reset_field_values(self):
 		if self.based_on == "Transaction":
@@ -240,7 +261,7 @@
 def notify_error_to_stock_managers(doc, traceback):
 	recipients = get_users_with_role("Stock Manager")
 	if not recipients:
-		get_users_with_role("System Manager")
+		recipients = get_users_with_role("System Manager")
 
 	subject = _("Error while reposting item valuation")
 	message = (
diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py
index e0f2479..f166775 100644
--- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py
+++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py
@@ -327,3 +327,26 @@
 		# outstanding should not be affected
 		sinv.reload()
 		self.assertEqual(sinv.outstanding_amount, 100)
+
+	def test_account_freeze_validation(self):
+		today = nowdate()
+
+		riv = frappe.get_doc(
+			doctype="Repost Item Valuation",
+			item_code="_Test Item",
+			warehouse="_Test Warehouse - _TC",
+			based_on="Item and Warehouse",
+			posting_date=today,
+			posting_time="00:01:00",
+		)
+		riv.flags.dont_run_in_test = True  # keep it queued
+
+		accounts_settings = frappe.get_doc("Accounts Settings")
+		accounts_settings.acc_frozen_upto = today
+		accounts_settings.frozen_accounts_modifier = ""
+		accounts_settings.save()
+
+		self.assertRaises(frappe.ValidationError, riv.save)
+
+		accounts_settings.acc_frozen_upto = ""
+		accounts_settings.save()
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index 266ea5f..b910244 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -1079,7 +1079,8 @@
 	if (frm.doc.purpose === 'Material Receipt') return;
 
 	frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() {
-		new erpnext.SerialNoBatchSelector({
+		if (frm.batch_selector?.dialog?.display) return;
+		frm.batch_selector = new erpnext.SerialNoBatchSelector({
 			frm: frm,
 			item: item,
 			warehouse_details: get_warehouse_type_and_name(item),
diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
index d92d0f1..3a0b38a 100644
--- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
+++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
@@ -230,7 +230,7 @@
 
 			if item.has_serial_no or item.has_batch_no:
 				has_serial_no = True
-				self.get_sle_for_serialized_items(row, sl_entries)
+				self.get_sle_for_serialized_items(row, sl_entries, item)
 			else:
 				if row.serial_no or row.batch_no:
 					frappe.throw(
@@ -282,7 +282,7 @@
 		if has_serial_no and sl_entries:
 			self.update_valuation_rate_for_serial_no()
 
-	def get_sle_for_serialized_items(self, row, sl_entries):
+	def get_sle_for_serialized_items(self, row, sl_entries, item):
 		from erpnext.stock.stock_ledger import get_previous_sle
 
 		serial_nos = get_serial_nos(row.serial_no)
@@ -348,6 +348,9 @@
 		if row.qty:
 			args = self.get_sle_for_items(row)
 
+			if item.has_serial_no and item.has_batch_no:
+				args["qty_after_transaction"] = row.qty
+
 			args.update(
 				{
 					"actual_qty": row.qty,
diff --git a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
index 7b984d3..eaea301 100644
--- a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
+++ b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
@@ -644,6 +644,38 @@
 		)
 		self.assertEqual(len(active_sr_no), 0)
 
+	def test_serial_no_batch_no_item(self):
+		item = self.make_item(
+			"Test Serial No Batch No Item",
+			{
+				"is_stock_item": 1,
+				"has_serial_no": 1,
+				"has_batch_no": 1,
+				"serial_no_series": "SRS9.####",
+				"batch_number_series": "BNS9.####",
+				"create_new_batch": 1,
+			},
+		)
+
+		warehouse = "_Test Warehouse - _TC"
+
+		sr = create_stock_reconciliation(
+			item_code=item.name,
+			warehouse=warehouse,
+			qty=1,
+			rate=100,
+		)
+
+		sl_entry = frappe.db.get_value(
+			"Stock Ledger Entry",
+			{"voucher_type": "Stock Reconciliation", "voucher_no": sr.name},
+			["actual_qty", "qty_after_transaction"],
+			as_dict=1,
+		)
+
+		self.assertEqual(flt(sl_entry.actual_qty), 1.0)
+		self.assertEqual(flt(sl_entry.qty_after_transaction), 1.0)
+
 
 def create_batch_item_with_batch(item_name, batch_id):
 	batch_item_doc = create_item(item_name, is_stock_item=1)