From 16be0f0944c2deb0886dc79a1f547249f1fcd360 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 29 Jul 2026 15:24:08 +0530 Subject: [PATCH] fix: let Purchase Receipt cancel defer to Frappe's linked-document check (backport #57592) (#57597) fix: let Purchase Receipt cancel defer to Frappe's linked-document check (#57592) on_cancel pre-blocked cancellation with its own "Purchase Invoice is already submitted" guard, duplicating the check Frappe already runs for any submitted linked document. Drop the guard and the unused check_next_docstatus() method it mirrored so the receipt defers to the framework: the Cancel All Documents flow cancels the invoice first and then the receipt, and a direct cancel is still rejected by Frappe's linked-document check. Add a regression test that a direct cancel of a receipt with a submitted invoice is rejected and rolls back, leaving no stray stock or GL entries. (cherry picked from commit cfe18e842739ee7c3f032f2fd007fce52e434fa9) # Conflicts: # erpnext/stock/doctype/purchase_receipt/purchase_receipt.py # erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py Co-authored-by: Jatin3128 <140256508+Jatin3128@users.noreply.github.com> --- .../purchase_receipt/purchase_receipt.py | 19 -------------- .../purchase_receipt/test_purchase_receipt.py | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index c40631a6d82..d1a8cb16a70 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -433,29 +433,10 @@ class PurchaseReceipt(BuyingController): row.received_qty, ) - def check_next_docstatus(self): - submit_rv = frappe.db.sql( - """select t1.name - from `tabPurchase Invoice` t1,`tabPurchase Invoice Item` t2 - where t1.name = t2.parent and t2.purchase_receipt = %s and t1.docstatus = 1""", - (self.name), - ) - if submit_rv: - frappe.throw(_("Purchase Invoice {0} is already submitted").format(self.submit_rv[0][0])) - def on_cancel(self): super().on_cancel() self.check_for_on_hold_or_closed_status("Purchase Order", "purchase_order") - # Check if Purchase Invoice has been submitted against current Purchase Order - submitted = frappe.db.sql( - """select t1.name - from `tabPurchase Invoice` t1,`tabPurchase Invoice Item` t2 - where t1.name = t2.parent and t2.purchase_receipt = %s and t1.docstatus = 1""", - self.name, - ) - if submitted: - frappe.throw(_("Purchase Invoice {0} is already submitted").format(submitted[0][0])) self.update_prevdoc_status() self.update_billing_status() diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py index eb867ff1f96..b0e6c6f8cac 100644 --- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py @@ -6180,6 +6180,32 @@ class TestPurchaseReceipt(ERPNextTestSuite): srbnb_credit = sum(flt(row.credit) for row in gl_entries if row.account == srbnb_account) self.assertAlmostEqual(srbnb_credit, pi_base_net_amount, places=2) + def test_cancel_blocked_by_submitted_invoice_rolls_back(self): + """A submitted Purchase Invoice must block cancelling its Purchase Receipt. Frappe's backlink + check rejects the cancel only after on_cancel has run stock, GL, and status work, so the whole + transaction has to roll back: the receipt stays submitted with no leaked ledger entries.""" + pr = make_purchase_receipt() + pi = make_purchase_invoice(pr.name) + pi.insert() + pi.submit() + + pr.reload() + status_before = pr.status + sle_before = frappe.db.count("Stock Ledger Entry", {"voucher_no": pr.name}) + gle_before = frappe.db.count("GL Entry", {"voucher_no": pr.name}) + + frappe.db.savepoint("before_blocked_cancel") + with self.assertRaises(frappe.LinkExistsError) as cm: + pr.cancel() + self.assertIn(pi.name, str(cm.exception)) + frappe.db.rollback(save_point="before_blocked_cancel") # mimic the request-level rollback + + pr.reload() + self.assertEqual(pr.docstatus, 1) + self.assertEqual(pr.status, status_before) + self.assertEqual(frappe.db.count("Stock Ledger Entry", {"voucher_no": pr.name}), sle_before) + self.assertEqual(frappe.db.count("GL Entry", {"voucher_no": pr.name}), gle_before) + def create_asset_category_for_pr_test(): category_name = "Test Asset Category for PR"