refactor: use CURRENT_DATE instead of CURDATE() (#31356)

* refactor: use CURRENT_DATE instead of CURDATE()

* style: reformat to black spec

* refactor: use QB for auto_close queries

Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
Conor
2022-06-15 01:37:33 -05:00
committed by GitHub
parent 37e9622426
commit b8f728a40a
12 changed files with 35 additions and 28 deletions

View File

@@ -8,7 +8,8 @@ import frappe
from frappe import _
from frappe.email.inbox import link_communication_to_document
from frappe.model.mapper import get_mapped_doc
from frappe.query_builder import DocType
from frappe.query_builder import DocType, Interval
from frappe.query_builder.functions import Now
from frappe.utils import cint, flt, get_fullname
from erpnext.crm.utils import add_link_in_communication, copy_comments
@@ -398,15 +399,17 @@ def auto_close_opportunity():
frappe.db.get_single_value("CRM Settings", "close_opportunity_after_days") or 15
)
opportunities = frappe.db.sql(
""" select name from tabOpportunity where status='Replied' and
modified<DATE_SUB(CURDATE(), INTERVAL %s DAY) """,
(auto_close_after_days),
as_dict=True,
)
table = frappe.qb.DocType("Opportunity")
opportunities = (
frappe.qb.from_(table)
.select(table.name)
.where(
(table.modified < (Now() - Interval(days=auto_close_after_days))) & (table.status == "Replied")
)
).run(pluck=True)
for opportunity in opportunities:
doc = frappe.get_doc("Opportunity", opportunity.get("name"))
doc = frappe.get_doc("Opportunity", opportunity)
doc.status = "Closed"
doc.flags.ignore_permissions = True
doc.flags.ignore_mandatory = True