feat: Immutable ledger (#18740)

* fix: Reverse GL entry on cancellation of document

* fix: Removed set posting time field for multiple docs

* fix: Stop future reposting and reverse entry for purchase receipt and delivery note

* fix: Change is_cancelled field from select to check

* Revert "fix: Removed set posting time field for multiple docs"

This reverts commit 81fb808db7da69ab1e58ba226c0cd0b77e5b90c8.

* fix: Multiple fixes in GL Entry

* fix: Remove future reporting from doctypes

* fix: Canceled entry filters in Stock Ledger and General Ledger Report

* fix: Remove print statement

* fix: Validation for back dated entries

* fix: Codacy fixes

* fix: Add ignore links to multiple doctypes

* fix: Codacy Fixes

* fix: Ignore GL Entry and Stock Ledger entry while cancel

* fix: Test case fixes

* fix: Patch

* fix: Codacy

* fix: Budget Test Cases

* fix: Patch

* fix: Patch

* fix: Multiple test cases

* fix: changes in make_reverse_entry function

* fix: Update patch

* fix: Test Cases

* fix: Test Case fixes

* fix: Move patch upward in patches.txt

* fix: Budget Test Cases

* fix: Test Case and codacy

* fix: Patch

* fix: Minor label and UX fixes

* fix: Move freezing date check

* fix: Test Cases

* fix: Test cases

* fix: Test Cases

* fix: Test Case

* fix: Remove update_gl_entries_after function

* fix: Remove update_gl_entries_after function

* fix: Test Cases

* fix: Fiscal Year wise backdated entry

* fix: Update entries only for current SLE

* fix: Remove is_cancelled

* fix: Test Cases

* fix: Test cases

* fix: Test Cases

* fix: Uncomment account and stock balance sync logic

* fix: Stock balance and Account balance out of sync fixes

* fix: Test Cases

* fix: Test cases for POS, Stock Reco and Purchase Receipt

* fix: Stock Reco tests

* fix: Test stock reco precision

* fix: Test stock reco for fifo precision

* fix: Test stock reco for fifo precision

* fix: Stock Entry test case

Co-authored-by: Nabin Hait <nabinhait@gmail.com>
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index 4789063..b5d6ca9 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -897,4 +897,60 @@
 	return frappe.get_all("Account", filters = {
 		"account_type": "Stock",
 		"company": company
-	})
\ No newline at end of file
+	})
+
+def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None,
+		warehouse_account=None, company=None):
+	def _delete_gl_entries(voucher_type, voucher_no):
+		frappe.db.sql("""delete from `tabGL Entry`
+			where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
+
+	if not warehouse_account:
+		warehouse_account = get_warehouse_account_map(company)
+
+	future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items)
+	gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date)
+
+	for voucher_type, voucher_no in future_stock_vouchers:
+		existing_gle = gle.get((voucher_type, voucher_no), [])
+		voucher_obj = frappe.get_doc(voucher_type, voucher_no)
+		expected_gle = voucher_obj.get_gl_entries(warehouse_account)
+		if expected_gle:
+			if not existing_gle or not compare_existing_and_expected_gle(existing_gle, expected_gle):
+				_delete_gl_entries(voucher_type, voucher_no)
+				voucher_obj.make_gl_entries(gl_entries=expected_gle, repost_future_gle=False, from_repost=True)
+		else:
+			_delete_gl_entries(voucher_type, voucher_no)
+
+def get_future_stock_vouchers(posting_date, posting_time, for_warehouses=None, for_items=None):
+	future_stock_vouchers = []
+
+	values = []
+	condition = ""
+	if for_items:
+		condition += " and item_code in ({})".format(", ".join(["%s"] * len(for_items)))
+		values += for_items
+
+	if for_warehouses:
+		condition += " and warehouse in ({})".format(", ".join(["%s"] * len(for_warehouses)))
+		values += for_warehouses
+
+	for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
+		from `tabStock Ledger Entry` sle
+		where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) {condition}
+		order by timestamp(sle.posting_date, sle.posting_time) asc, creation asc for update""".format(condition=condition),
+		tuple([posting_date, posting_time] + values), as_dict=True):
+			future_stock_vouchers.append([d.voucher_type, d.voucher_no])
+
+	return future_stock_vouchers
+
+def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
+	gl_entries = {}
+	if future_stock_vouchers:
+		for d in frappe.db.sql("""select * from `tabGL Entry`
+			where posting_date >= %s and voucher_no in (%s)""" %
+			('%s', ', '.join(['%s']*len(future_stock_vouchers))),
+			tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
+				gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
+
+	return gl_entries
\ No newline at end of file