Browse Source

fix: convert raw queries with frappe ORM

develop
Rucha Mahabal 3 years ago
parent
commit
baa12bcee6
  1. 45
      erpnext/payroll/doctype/payroll_entry/payroll_entry.py

45
erpnext/payroll/doctype/payroll_entry/payroll_entry.py

@ -7,6 +7,7 @@ from dateutil.relativedelta import relativedelta
from frappe import _ from frappe import _
from frappe.desk.reportview import get_filters_cond, get_match_cond from frappe.desk.reportview import get_filters_cond, get_match_cond
from frappe.model.document import Document from frappe.model.document import Document
from frappe.query_builder.functions import Coalesce
from frappe.utils import ( from frappe.utils import (
DATE_FORMAT, DATE_FORMAT,
add_days, add_days,
@ -157,11 +158,20 @@ class PayrollEntry(Document):
Returns list of salary slips based on selected criteria Returns list of salary slips based on selected criteria
""" """
ss_list = frappe.db.sql(""" ss = frappe.qb.DocType("Salary Slip")
select t1.name, t1.salary_structure from `tabSalary Slip` t1 ss_list = (
where t1.docstatus = %s and t1.start_date >= %s and t1.end_date <= %s and t1.payroll_entry = %s frappe.qb.from_(ss)
and (t1.journal_entry is null or t1.journal_entry = "") and ifnull(salary_slip_based_on_timesheet,0) = %s .select(ss.name, ss.salary_structure)
""", (ss_status, self.start_date, self.end_date, self.name, self.salary_slip_based_on_timesheet), as_dict=as_dict) .where(
(ss.docstatus == ss_status)
& (ss.start_date >= self.start_date)
& (ss.end_date <= self.end_date)
& (ss.payroll_entry == self.name)
& ((ss.journal_entry.isnull()) | (ss.journal_entry == ""))
& (Coalesce(ss.salary_slip_based_on_timesheet, 0) == self.salary_slip_based_on_timesheet)
)
).run(as_dict=as_dict)
return ss_list return ss_list
@frappe.whitelist() @frappe.whitelist()
@ -190,19 +200,20 @@ class PayrollEntry(Document):
def get_salary_components(self, component_type): def get_salary_components(self, component_type):
salary_slips = self.get_sal_slip_list(ss_status = 1, as_dict = True) salary_slips = self.get_sal_slip_list(ss_status = 1, as_dict = True)
if salary_slips: if salary_slips:
salary_components = frappe.db.sql(""" ss = frappe.qb.DocType("Salary Slip")
SELECT ssd = frappe.qb.DocType("Salary Detail")
ssd.salary_component, ssd.amount, ssd.parentfield, ss.salary_structure, ss.employee salary_components = (
FROM frappe.qb.from_(ss)
`tabSalary Slip` ss, .join(ssd)
`tabSalary Detail` ssd .on(ss.name == ssd.parent)
WHERE .select(ssd.salary_component, ssd.amount, ssd.parentfield, ss.salary_structure, ss.employee)
ss.name = ssd.parent .where(
and ssd.parentfield = '%s' (ssd.parentfield == component_type)
and ss.name in (%s) & (ss.name.isin(tuple([d.name for d in salary_slips])))
""" % (component_type, ', '.join(['%s']*len(salary_slips))), )
tuple([d.name for d in salary_slips]), as_dict=True) ).run(as_dict=True)
return salary_components return salary_components

Loading…
Cancel
Save