diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 32e761b4f44f..55c2bef4bfdf 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -10,7 +10,7 @@ from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied from frappe.query_builder import Criterion from frappe.query_builder.custom import ConstantColumn -from frappe.query_builder.functions import Abs, Sum +from frappe.query_builder.functions import Abs, IfNull, Sum from frappe.utils import ( add_days, add_months, @@ -362,13 +362,20 @@ def on_trash(self): == 1 ) ).run() - frappe.db.sql( - "delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s", (self.doctype, self.name) - ) - frappe.db.sql( - "delete from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s", - (self.doctype, self.name), - ) + + gl_entry = frappe.qb.DocType("GL Entry") + frappe.qb.from_(gl_entry).delete().where( + ((gl_entry.voucher_type == self.doctype) & (gl_entry.voucher_no == self.name)) + | ( + (IfNull(gl_entry.against_voucher_type, "") == self.doctype) + & (IfNull(gl_entry.against_voucher, "") == self.name) + ) + ).where(gl_entry.is_cancelled == 1).run() + + sle = frappe.qb.DocType("Stock Ledger Entry") + frappe.qb.from_(sle).delete().where( + (sle.voucher_type == self.doctype) & (sle.voucher_no == self.name) & (sle.is_cancelled == 1) + ).run() def remove_serial_and_batch_bundle(self): bundles = frappe.get_all(