From f4928d53cbd4584bd708c275d324f4ada0535233 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 3 Sep 2026 10:29:34 +0000 Subject: [PATCH] fix(stock): allow creating stock closing balances (backport #58590) (#58685) * fix(stock): allow creating stock closing balances (#58590) (cherry picked from commit 2918e98a2bd7788139b2906d8d2de734a652e6e9) # Conflicts: # erpnext/stock/doctype/stock_closing_entry/test_stock_closing_entry.py * chore: fix conflicts Removed redundant test cases and cleaned up the test structure for StockClosingEntry. --------- Co-authored-by: Krishna Pramod Shirsath <91021227+krishna-254@users.noreply.github.com> Co-authored-by: rohitwaghchaure (cherry picked from commit a1c8dc878d53bc61525fdc6a2b36b65aa569f637) --- .../stock_closing_entry.py | 2 +- .../test_stock_closing_entry.py | 48 ++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/erpnext/stock/doctype/stock_closing_entry/stock_closing_entry.py b/erpnext/stock/doctype/stock_closing_entry/stock_closing_entry.py index 30471ad817d..eb72f1e54cd 100644 --- a/erpnext/stock/doctype/stock_closing_entry/stock_closing_entry.py +++ b/erpnext/stock/doctype/stock_closing_entry/stock_closing_entry.py @@ -189,7 +189,7 @@ class StockClosingEntry(Document): new_doc.posting_datetime = get_combine_datetime(self.to_date, new_doc.posting_time) new_doc.stock_closing_entry = self.name new_doc.company = self.company - new_doc.save() + new_doc.save(ignore_permissions=True) def get_prepared_data(self): if attachments := get_attachments(self.doctype, self.name): diff --git a/erpnext/stock/doctype/stock_closing_entry/test_stock_closing_entry.py b/erpnext/stock/doctype/stock_closing_entry/test_stock_closing_entry.py index 91d3f3d7b34..d667fef4966 100644 --- a/erpnext/stock/doctype/stock_closing_entry/test_stock_closing_entry.py +++ b/erpnext/stock/doctype/stock_closing_entry/test_stock_closing_entry.py @@ -1,14 +1,22 @@ # Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and Contributors # See license.txt -# import frappe +from unittest.mock import patch +import frappe +from frappe.core.doctype.user_permission.test_user_permission import create_user +from frappe.utils import today + +from erpnext.stock.doctype.item.test_item import make_item from erpnext.tests.utils import ERPNextTestSuite # On ERPNextTestSuite, the doctype test records and all # link-field test record depdendencies are recursively loaded # Use these module variables to add/remove to/from that list +COMPANY = "_Test Company" +WAREHOUSE = "_Test Warehouse - _TC" + class TestStockClosingEntry(ERPNextTestSuite): """ @@ -16,4 +24,40 @@ class TestStockClosingEntry(ERPNextTestSuite): Use this class for testing interactions between multiple components. """ - pass + def make_stock_closing_entry(self, from_date, to_date): + entry = frappe.get_doc( + doctype="Stock Closing Entry", + company=COMPANY, + from_date=from_date, + to_date=to_date, + ).submit() + self.last_closing_entry = entry.name + return entry + + def test_non_administrator_can_generate_closing_balance(self): + item = make_item(properties={"is_stock_item": 1}).name + with patch("erpnext.stock.doctype.stock_closing_entry.stock_closing_entry.enqueue"): + entry = self.make_stock_closing_entry(today(), today()) + + user = create_user("test_stock_closing_balance@example.com", "Stock User") + self.assertFalse(frappe.has_permission("Stock Closing Balance", "create", user=user.name)) + + balance = frappe._dict( + item_code=item, + warehouse=WAREHOUSE, + actual_qty=1, + stock_value_difference=100, + fifo_queue=None, + ) + with ( + patch( + "erpnext.stock.doctype.stock_closing_entry.stock_closing_entry.StockClosing" + ) as stock_closing, + self.set_user(user.name), + ): + stock_closing.return_value.get_stock_closing_entries.return_value = {(item, WAREHOUSE): balance} + entry.create_stock_closing_balance_entries() + + self.assertTrue( + frappe.db.exists("Stock Closing Balance", {"stock_closing_entry": entry.name, "item_code": item}) + )