refactor: convert raw sql to query_builder

This commit is contained in:
ruthra kumar 2023-08-30 13:25:23 +05:30
parent de910ab152
commit 0130aea2aa

View File

@ -705,72 +705,87 @@ def cancel_exchange_gain_loss_journal(parent_doc: dict | object) -> None:
frappe.get_doc("Journal Entry", doc[0]).cancel() frappe.get_doc("Journal Entry", doc[0]).cancel()
def unlink_ref_doc_from_payment_entries(ref_doc): def update_accounting_ledgers_after_reference_removal(ref_type: str = None, ref_no: str = None):
remove_ref_doc_link_from_jv(ref_doc.doctype, ref_doc.name) # General Ledger
remove_ref_doc_link_from_pe(ref_doc.doctype, ref_doc.name) gle = qb.DocType("GL Entry")
qb.update(gle).set(gle.against_voucher_type, None).set(gle.against_voucher, None).set(
frappe.db.sql( gle.modified, now()
"""update `tabGL Entry` ).set(gle.modified_by, frappe.session.user).where(
set against_voucher_type=null, against_voucher=null, (gle.against_voucher_type == ref_type) & (gle.against_voucher == ref_no)
modified=%s, modified_by=%s ).run()
where against_voucher_type=%s and against_voucher=%s
and voucher_no != ifnull(against_voucher, '')""",
(now(), frappe.session.user, ref_doc.doctype, ref_doc.name),
)
# Payment Ledger
ple = qb.DocType("Payment Ledger Entry") ple = qb.DocType("Payment Ledger Entry")
qb.update(ple).set(ple.against_voucher_type, ple.voucher_type).set( qb.update(ple).set(ple.against_voucher_type, ple.voucher_type).set(
ple.against_voucher_no, ple.voucher_no ple.against_voucher_no, ple.voucher_no
).set(ple.modified, now()).set(ple.modified_by, frappe.session.user).where( ).set(ple.modified, now()).set(ple.modified_by, frappe.session.user).where(
(ple.against_voucher_type == ref_doc.doctype) (ple.against_voucher_type == ref_type) & (ple.against_voucher_no == ref_no) & (ple.delinked == 0)
& (ple.against_voucher_no == ref_doc.name)
& (ple.delinked == 0)
).run() ).run()
def remove_ref_from_advance_section(ref_doc: object = None):
if ref_doc.doctype in ("Sales Invoice", "Purchase Invoice"): if ref_doc.doctype in ("Sales Invoice", "Purchase Invoice"):
ref_doc.set("advances", []) ref_doc.set("advances", [])
adv_type = qb.DocType(f"{ref_doc.doctype} Advance")
qb.from_(adv_type).delete().where(adv_type.parent == ref_doc.name).run()
frappe.db.sql(
"""delete from `tab{0} Advance` where parent = %s""".format(ref_doc.doctype), ref_doc.name def unlink_ref_doc_from_payment_entries(ref_doc):
) remove_ref_doc_link_from_jv(ref_doc.doctype, ref_doc.name)
remove_ref_doc_link_from_pe(ref_doc.doctype, ref_doc.name)
update_accounting_ledgers_after_reference_removal(ref_doc.doctype, ref_doc.name)
def remove_ref_doc_link_from_jv(ref_type, ref_no): def remove_ref_doc_link_from_jv(ref_type, ref_no):
linked_jv = frappe.db.sql_list( jea = qb.DocType("Journal Entry Account")
"""select parent from `tabJournal Entry Account`
where reference_type=%s and reference_name=%s and docstatus < 2""", linked_jv = (
(ref_type, ref_no), qb.from_(jea)
.select(jea.parent)
.select(
(jea.reference_type == ref_type) & (jea.reference_name == ref_no) & (jea.docstatus.lt(2))
) )
.run(as_list=1)
)
linked_jv = convert_to_list(linked_jv)
if linked_jv: if linked_jv:
frappe.db.sql( qb.update(jea).set(jea.reference_type, None).set(jea.reference_name, None).set(
"""update `tabJournal Entry Account` jea.modified, now()
set reference_type=null, reference_name = null, ).set(jea.modified_by, frappe.session.user).where(
modified=%s, modified_by=%s (jea.reference_type == ref_type) & (jea.reference_name == ref_no)
where reference_type=%s and reference_name=%s ).run()
and docstatus < 2""",
(now(), frappe.session.user, ref_type, ref_no),
)
frappe.msgprint(_("Journal Entries {0} are un-linked").format("\n".join(linked_jv))) frappe.msgprint(_("Journal Entries {0} are un-linked").format("\n".join(linked_jv)))
def convert_to_list(result):
"""
Convert tuple to list
"""
return [x[0] for x in result]
def remove_ref_doc_link_from_pe(ref_type, ref_no): def remove_ref_doc_link_from_pe(ref_type, ref_no):
linked_pe = frappe.db.sql_list( per = qb.DocType("Payment Entry Reference")
"""select parent from `tabPayment Entry Reference` pay = qb.DocType("Payment Entry")
where reference_doctype=%s and reference_name=%s and docstatus < 2""",
(ref_type, ref_no), linked_pe = (
qb.from_(per)
.select(per.parent)
.where(
(per.reference_doctype == ref_type) & (per.reference_name == ref_no) & (per.docstatus.lt(2))
) )
.run(as_list=1)
)
linked_pe = convert_to_list(linked_pe)
if linked_pe: if linked_pe:
frappe.db.sql( qb.update(per).set(per.allocated_amount, 0).set(per.modified, now()).set(
"""update `tabPayment Entry Reference` per.modified_by, frappe.session.user
set allocated_amount=0, modified=%s, modified_by=%s ).where(
where reference_doctype=%s and reference_name=%s (per.docstatus.lt(2) & (per.reference_doctype == ref_type) & (per.reference_name == ref_no))
and docstatus < 2""", ).run()
(now(), frappe.session.user, ref_type, ref_no),
)
for pe in linked_pe: for pe in linked_pe:
try: try:
@ -785,19 +800,13 @@ def remove_ref_doc_link_from_pe(ref_type, ref_no):
msg += _("Please cancel payment entry manually first") msg += _("Please cancel payment entry manually first")
frappe.throw(msg, exc=PaymentEntryUnlinkError, title=_("Payment Unlink Error")) frappe.throw(msg, exc=PaymentEntryUnlinkError, title=_("Payment Unlink Error"))
frappe.db.sql( qb.update(pay).set(pay.total_allocated_amount, pe_doc.total_allocated_amount).set(
"""update `tabPayment Entry` set total_allocated_amount=%s, pay.base_total_allocated_amount, pe_doc.base_total_allocated_amount
base_total_allocated_amount=%s, unallocated_amount=%s, modified=%s, modified_by=%s ).set(pay.unallocated_amount, pe_doc.unallocated_amount).set(pay.modified, now()).set(
where name=%s""", pay.modified_by, frappe.session.user
( ).where(
pe_doc.total_allocated_amount, pay.name == pe
pe_doc.base_total_allocated_amount, ).run()
pe_doc.unallocated_amount,
now(),
frappe.session.user,
pe,
),
)
frappe.msgprint(_("Payment Entries {0} are un-linked").format("\n".join(linked_pe))) frappe.msgprint(_("Payment Entries {0} are un-linked").format("\n".join(linked_pe)))