refactor: convert sql query to query builder

This commit is contained in:
rs-rethik
2024-12-20 12:42:47 +05:30
parent c14a2d73bf
commit 2d58e845e6

View File

@@ -383,13 +383,14 @@ class AccountsController(TransactionBase):
== 1 == 1
) )
).run() ).run()
frappe.db.sql( gle = frappe.qb.DocType("GL Entry")
"delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s", (self.doctype, self.name) frappe.qb.from_(gle).delete().where(
) (gle.voucher_type == self.doctype) & (gle.voucher_no == self.name)
frappe.db.sql( ).run()
"delete from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s", sle = frappe.qb.DocType("Stock Ledger Entry")
(self.doctype, self.name), frappe.qb.from_(gle).delete().where(
) (sle.voucher_type == self.doctype) & (sle.voucher_no == self.name)
).run()
def remove_serial_and_batch_bundle(self): def remove_serial_and_batch_bundle(self):
bundles = frappe.get_all( bundles = frappe.get_all(
@@ -1164,11 +1165,12 @@ class AccountsController(TransactionBase):
def clear_unallocated_advances(self, childtype, parentfield): def clear_unallocated_advances(self, childtype, parentfield):
self.set(parentfield, self.get(parentfield, {"allocated_amount": ["not in", [0, None, ""]]})) self.set(parentfield, self.get(parentfield, {"allocated_amount": ["not in", [0, None, ""]]}))
frappe.db.sql( doctype = frappe.qb.DocType(childtype)
"""delete from `tab{}` where parentfield={} and parent = {} frappe.qb.from_(doctype).delete().where(
and allocated_amount = 0""".format(childtype, "%s", "%s"), (doctype.parentfield == parentfield)
(parentfield, self.name), & (doctype.parent == self.name)
) & (doctype.allocated_amount == 0)
).run()
@frappe.whitelist() @frappe.whitelist()
def apply_shipping_rule(self): def apply_shipping_rule(self):
@@ -2143,11 +2145,9 @@ class AccountsController(TransactionBase):
for adv in self.advances: for adv in self.advances:
consider_for_total_advance = True consider_for_total_advance = True
if adv.reference_name == linked_doc_name: if adv.reference_name == linked_doc_name:
frappe.db.sql( doctype = frappe.qb.DocType(self.doctype + " Advance")
f"""delete from `tab{self.doctype} Advance` frappe.qb.from_(doctype).delete().where(doctype.name == adv.name).run()
where name = %s""",
adv.name,
)
consider_for_total_advance = False consider_for_total_advance = False
if consider_for_total_advance: if consider_for_total_advance: