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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 35 additions and 28 deletions

View File

@ -1444,7 +1444,7 @@ def get_negative_outstanding_invoices(
voucher_type = "Sales Invoice" if party_type == "Customer" else "Purchase Invoice" voucher_type = "Sales Invoice" if party_type == "Customer" else "Purchase Invoice"
supplier_condition = "" supplier_condition = ""
if voucher_type == "Purchase Invoice": if voucher_type == "Purchase Invoice":
supplier_condition = "and (release_date is null or release_date <= CURDATE())" supplier_condition = "and (release_date is null or release_date <= CURRENT_DATE)"
if party_account_currency == company_currency: if party_account_currency == company_currency:
grand_total_field = "base_grand_total" grand_total_field = "base_grand_total"
rounded_total_field = "base_rounded_total" rounded_total_field = "base_rounded_total"

View File

@ -100,7 +100,7 @@ def get_sales_details(filters):
sales_data = frappe.db.sql( sales_data = frappe.db.sql(
""" """
select s.territory, s.customer, si.item_group, si.item_code, si.qty, {date_field} as last_order_date, select s.territory, s.customer, si.item_group, si.item_code, si.qty, {date_field} as last_order_date,
DATEDIFF(CURDATE(), {date_field}) as days_since_last_order DATEDIFF(CURRENT_DATE, {date_field}) as days_since_last_order
from `tab{doctype}` s, `tab{doctype} Item` si from `tab{doctype}` s, `tab{doctype} Item` si
where s.name = si.parent and s.docstatus = 1 where s.name = si.parent and s.docstatus = 1
order by days_since_last_order """.format( # nosec order by days_since_last_order """.format( # nosec

View File

@ -330,7 +330,7 @@ class TestPurchaseOrder(FrappeTestCase):
else: else:
# update valid from # update valid from
frappe.db.sql( frappe.db.sql(
"""UPDATE `tabItem Tax` set valid_from = CURDATE() """UPDATE `tabItem Tax` set valid_from = CURRENT_DATE
where parent = %(item)s and item_tax_template = %(tax)s""", where parent = %(item)s and item_tax_template = %(tax)s""",
{"item": item, "tax": tax_template}, {"item": item, "tax": tax_template},
) )

View File

@ -691,7 +691,7 @@ def get_doctype_wise_filters(filters):
def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters): def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters):
query = """select batch_id from `tabBatch` query = """select batch_id from `tabBatch`
where disabled = 0 where disabled = 0
and (expiry_date >= CURDATE() or expiry_date IS NULL) and (expiry_date >= CURRENT_DATE or expiry_date IS NULL)
and name like {txt}""".format( and name like {txt}""".format(
txt=frappe.db.escape("%{0}%".format(txt)) txt=frappe.db.escape("%{0}%".format(txt))
) )

View File

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

View File

@ -28,7 +28,7 @@ def daily_reminder():
for drafts in draft: for drafts in draft:
number_of_drafts = drafts[0] number_of_drafts = drafts[0]
update = frappe.db.sql( update = frappe.db.sql(
"""SELECT name,date,time,progress,progress_details FROM `tabProject Update` WHERE `tabProject Update`.project = %s AND date = DATE_ADD(CURDATE(), INTERVAL -1 DAY);""", """SELECT name,date,time,progress,progress_details FROM `tabProject Update` WHERE `tabProject Update`.project = %s AND date = DATE_ADD(CURRENT_DATE, INTERVAL -1 DAY);""",
project_name, project_name,
) )
email_sending(project_name, frequency, date_start, date_end, progress, number_of_drafts, update) email_sending(project_name, frequency, date_start, date_end, progress, number_of_drafts, update)
@ -39,7 +39,7 @@ def email_sending(
): ):
holiday = frappe.db.sql( holiday = frappe.db.sql(
"""SELECT holiday_date FROM `tabHoliday` where holiday_date = CURDATE();""" """SELECT holiday_date FROM `tabHoliday` where holiday_date = CURRENT_DATE;"""
) )
msg = ( msg = (
"<p>Project Name: " "<p>Project Name: "

View File

@ -644,7 +644,7 @@ class TestSalesOrder(FrappeTestCase):
else: else:
# update valid from # update valid from
frappe.db.sql( frappe.db.sql(
"""UPDATE `tabItem Tax` set valid_from = CURDATE() """UPDATE `tabItem Tax` set valid_from = CURRENT_DATE
where parent = %(item)s and item_tax_template = %(tax)s""", where parent = %(item)s and item_tax_template = %(tax)s""",
{"item": item, "tax": tax_template}, {"item": item, "tax": tax_template},
) )

View File

@ -31,13 +31,13 @@ def execute(filters=None):
def get_sales_details(doctype): def get_sales_details(doctype):
cond = """sum(so.base_net_total) as 'total_order_considered', cond = """sum(so.base_net_total) as 'total_order_considered',
max(so.posting_date) as 'last_order_date', max(so.posting_date) as 'last_order_date',
DATEDIFF(CURDATE(), max(so.posting_date)) as 'days_since_last_order' """ DATEDIFF(CURRENT_DATE, max(so.posting_date)) as 'days_since_last_order' """
if doctype == "Sales Order": if doctype == "Sales Order":
cond = """sum(if(so.status = "Stopped", cond = """sum(if(so.status = "Stopped",
so.base_net_total * so.per_delivered/100, so.base_net_total * so.per_delivered/100,
so.base_net_total)) as 'total_order_considered', so.base_net_total)) as 'total_order_considered',
max(so.transaction_date) as 'last_order_date', max(so.transaction_date) as 'last_order_date',
DATEDIFF(CURDATE(), max(so.transaction_date)) as 'days_since_last_order'""" DATEDIFF(CURRENT_DATE, max(so.transaction_date)) as 'days_since_last_order'"""
return frappe.db.sql( return frappe.db.sql(
"""select """select

View File

@ -64,7 +64,7 @@ def get_data(conditions, filters):
soi.delivery_date as delivery_date, soi.delivery_date as delivery_date,
so.name as sales_order, so.name as sales_order,
so.status, so.customer, soi.item_code, so.status, so.customer, soi.item_code,
DATEDIFF(CURDATE(), soi.delivery_date) as delay_days, DATEDIFF(CURRENT_DATE, soi.delivery_date) as delay_days,
IF(so.status in ('Completed','To Bill'), 0, (SELECT delay_days)) as delay, IF(so.status in ('Completed','To Bill'), 0, (SELECT delay_days)) as delay,
soi.qty, soi.delivered_qty, soi.qty, soi.delivered_qty,
(soi.qty - soi.delivered_qty) AS pending_qty, (soi.qty - soi.delivered_qty) AS pending_qty,

View File

@ -854,7 +854,7 @@ class EmailDigest(Document):
sql_po = """select {fields} from `tabPurchase Order Item` sql_po = """select {fields} from `tabPurchase Order Item`
left join `tabPurchase Order` on `tabPurchase Order`.name = `tabPurchase Order Item`.parent left join `tabPurchase Order` on `tabPurchase Order`.name = `tabPurchase Order Item`.parent
where status<>'Closed' and `tabPurchase Order Item`.docstatus=1 and curdate() > `tabPurchase Order Item`.schedule_date where status<>'Closed' and `tabPurchase Order Item`.docstatus=1 and CURRENT_DATE > `tabPurchase Order Item`.schedule_date
and received_qty < qty order by `tabPurchase Order Item`.parent DESC, and received_qty < qty order by `tabPurchase Order Item`.parent DESC,
`tabPurchase Order Item`.schedule_date DESC""".format( `tabPurchase Order Item`.schedule_date DESC""".format(
fields=fields_po fields=fields_po
@ -862,7 +862,7 @@ class EmailDigest(Document):
sql_poi = """select {fields} from `tabPurchase Order Item` sql_poi = """select {fields} from `tabPurchase Order Item`
left join `tabPurchase Order` on `tabPurchase Order`.name = `tabPurchase Order Item`.parent left join `tabPurchase Order` on `tabPurchase Order`.name = `tabPurchase Order Item`.parent
where status<>'Closed' and `tabPurchase Order Item`.docstatus=1 and curdate() > `tabPurchase Order Item`.schedule_date where status<>'Closed' and `tabPurchase Order Item`.docstatus=1 and CURRENT_DATE > `tabPurchase Order Item`.schedule_date
and received_qty < qty order by `tabPurchase Order Item`.idx""".format( and received_qty < qty order by `tabPurchase Order Item`.idx""".format(
fields=fields_poi fields=fields_poi
) )

View File

@ -335,7 +335,7 @@ def get_batches(item_code, warehouse, qty=1, throw=False, serial_no=None):
on (`tabBatch`.batch_id = `tabStock Ledger Entry`.batch_no ) on (`tabBatch`.batch_id = `tabStock Ledger Entry`.batch_no )
where `tabStock Ledger Entry`.item_code = %s and `tabStock Ledger Entry`.warehouse = %s where `tabStock Ledger Entry`.item_code = %s and `tabStock Ledger Entry`.warehouse = %s
and `tabStock Ledger Entry`.is_cancelled = 0 and `tabStock Ledger Entry`.is_cancelled = 0
and (`tabBatch`.expiry_date >= CURDATE() or `tabBatch`.expiry_date IS NULL) {0} and (`tabBatch`.expiry_date >= CURRENT_DATE or `tabBatch`.expiry_date IS NULL) {0}
group by batch_id group by batch_id
order by `tabBatch`.expiry_date ASC, `tabBatch`.creation ASC order by `tabBatch`.expiry_date ASC, `tabBatch`.creation ASC
""".format( """.format(

View File

@ -11,6 +11,8 @@ from frappe.core.utils import get_parent_doc
from frappe.email.inbox import link_communication_to_document from frappe.email.inbox import link_communication_to_document
from frappe.model.document import Document from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc from frappe.model.mapper import get_mapped_doc
from frappe.query_builder import Interval
from frappe.query_builder.functions import Now
from frappe.utils import date_diff, get_datetime, now_datetime, time_diff_in_seconds from frappe.utils import date_diff, get_datetime, now_datetime, time_diff_in_seconds
from frappe.utils.user import is_website_user from frappe.utils.user import is_website_user
@ -190,15 +192,17 @@ def auto_close_tickets():
frappe.db.get_value("Support Settings", "Support Settings", "close_issue_after_days") or 7 frappe.db.get_value("Support Settings", "Support Settings", "close_issue_after_days") or 7
) )
issues = frappe.db.sql( table = frappe.qb.DocType("Issue")
""" select name from tabIssue where status='Replied' and issues = (
modified<DATE_SUB(CURDATE(), INTERVAL %s DAY) """, frappe.qb.from_(table)
(auto_close_after_days), .select(table.name)
as_dict=True, .where(
) (table.modified < (Now() - Interval(days=auto_close_after_days))) & (table.status == "Replied")
)
).run(pluck=True)
for issue in issues: for issue in issues:
doc = frappe.get_doc("Issue", issue.get("name")) doc = frappe.get_doc("Issue", issue)
doc.status = "Closed" doc.status = "Closed"
doc.flags.ignore_permissions = True doc.flags.ignore_permissions = True
doc.flags.ignore_mandatory = True doc.flags.ignore_mandatory = True