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/stock/utils.py b/erpnext/stock/utils.py
index 7f32b8d..f21dc3f 100644
--- a/erpnext/stock/utils.py
+++ b/erpnext/stock/utils.py
@@ -364,4 +364,16 @@
 			else:
 				row[data.converted_col] = flt(value_before_conversion) / conversion_factor
 
-		result[row_idx] = row
\ No newline at end of file
+		result[row_idx] = row
+
+def get_incoming_outgoing_rate_for_cancel(item_code, voucher_type, voucher_no, voucher_detail_no):
+	outgoing_rate = frappe.db.sql("""SELECT abs(stock_value_difference / actual_qty)
+		FROM `tabStock Ledger Entry`
+		WHERE voucher_type = %s and voucher_no = %s
+			and item_code = %s and voucher_detail_no = %s
+			ORDER BY CREATION DESC limit 1""",
+		(voucher_type, voucher_no, item_code, voucher_detail_no))
+
+	outgoing_rate = outgoing_rate[0][0] if outgoing_rate else 0.0
+
+	return outgoing_rate
\ No newline at end of file