refactor: update_invoice_status with query builder

This commit is contained in:
Pruthvi Patel 2021-10-30 16:44:15 +05:30
parent 0ca467a28f
commit 6c96ed4e11

View File

@ -7,6 +7,7 @@ import json
import frappe import frappe
from frappe import _, throw from frappe import _, throw
from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied
from frappe.query_builder.functions import Sum
from frappe.utils import ( from frappe.utils import (
add_days, add_days,
add_months, add_months,
@ -1684,58 +1685,63 @@ def get_advance_payment_entries(party_type, party, party_account, order_doctype,
def update_invoice_status(): def update_invoice_status():
"""Updates status as Overdue for applicable invoices. Runs daily.""" """Updates status as Overdue for applicable invoices. Runs daily."""
today = getdate() today = getdate()
payment_schedule = frappe.qb.DocType("Payment Schedule")
for doctype in ("Sales Invoice", "Purchase Invoice"): for doctype in ("Sales Invoice", "Purchase Invoice"):
frappe.db.sql(""" invoice = frappe.qb.DocType(doctype)
UPDATE `tab{doctype}` invoice SET invoice.status = 'Overdue'
WHERE invoice.docstatus = 1 consider_base_amount = invoice.party_account_currency != invoice.currency
AND invoice.status REGEXP '^Unpaid|^Partly Paid' payment_amount = (
AND invoice.outstanding_amount > 0 frappe.qb.terms.Case()
AND ( .when(consider_base_amount, payment_schedule.base_payment_amount)
{or_condition} .else_(payment_schedule.payment_amount)
(
(
CASE
WHEN invoice.party_account_currency = invoice.currency
THEN (
CASE
WHEN invoice.disable_rounded_total
THEN invoice.grand_total
ELSE invoice.rounded_total
END
) )
ELSE (
CASE payable_amount = (
WHEN invoice.disable_rounded_total frappe.qb.from_(payment_schedule)
THEN invoice.base_grand_total .select(Sum(payment_amount))
ELSE invoice.base_rounded_total .where(
END (payment_schedule.parent == invoice.name)
) & (payment_schedule.due_date < today)
END
) - invoice.outstanding_amount
) < (
SELECT SUM(
CASE
WHEN invoice.party_account_currency = invoice.currency
THEN ps.payment_amount
ELSE ps.base_payment_amount
END
)
FROM `tabPayment Schedule` ps
WHERE ps.parent = invoice.name
AND ps.due_date < %(today)s
) )
) )
""".format(
doctype=doctype, total = (
or_condition=( frappe.qb.terms.Case()
"invoice.is_pos AND invoice.due_date < %(today)s OR" .when(invoice.disable_rounded_total, invoice.grand_total)
.else_(invoice.rounded_total)
)
base_total = (
frappe.qb.terms.Case()
.when(invoice.disable_rounded_total, invoice.base_grand_total)
.else_(invoice.base_rounded_total)
)
total_amount = (
frappe.qb.terms.Case()
.when(consider_base_amount, base_total)
.else_(total)
)
is_overdue = total_amount - invoice.outstanding_amount < payable_amount
conditions = (
(invoice.docstatus == 1)
& (invoice.outstanding_amount > 0)
& (
invoice.status.like('Unpaid%')
| invoice.status.like('Partly Paid%')
)
& (
(invoice.is_pos & invoice.due_date < today) | is_overdue
if doctype == "Sales Invoice" if doctype == "Sales Invoice"
else "" else is_overdue
) )
), {"today": today}
) )
frappe.qb.update(invoice).set("status", "Overdue").where(conditions).run()
@frappe.whitelist() @frappe.whitelist()
def get_payment_terms(terms_template, posting_date=None, grand_total=None, base_grand_total=None, bill_date=None): def get_payment_terms(terms_template, posting_date=None, grand_total=None, base_grand_total=None, bill_date=None):
if not terms_template: if not terms_template: