From 607a21e02acbcad21c7d304f748e6eddaa0cc422 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Thu, 5 Sep 2019 12:42:29 +0530 Subject: [PATCH 1/4] feat: Init Employee Leave Balance Summary report --- .../__init__.py | 0 .../employee_leave_balance_summary.js | 36 +++++++++++ .../employee_leave_balance_summary.json | 34 ++++++++++ .../employee_leave_balance_summary.py | 63 +++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 erpnext/hr/report/employee_leave_balance_summary/__init__.py create mode 100644 erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js create mode 100644 erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.json create mode 100644 erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py diff --git a/erpnext/hr/report/employee_leave_balance_summary/__init__.py b/erpnext/hr/report/employee_leave_balance_summary/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js new file mode 100644 index 0000000000..a05db33050 --- /dev/null +++ b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js @@ -0,0 +1,36 @@ +// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +/* eslint-disable */ + +frappe.query_reports["Employee Leave Balance Summary"] = { + "filters": [ + { + "fieldname":"from_date", + "label": __("From Date"), + "fieldtype": "Date", + "reqd": 1, + "default": frappe.defaults.get_default("year_start_date") + }, + { + "fieldname":"to_date", + "label": __("To Date"), + "fieldtype": "Date", + "reqd": 1, + "default": frappe.defaults.get_default("year_end_date") + }, + { + "fieldname":"company", + "label": __("Company"), + "fieldtype": "Link", + "options": "Company", + "reqd": 1, + "default": frappe.defaults.get_user_default("Company") + }, + { + "fieldname":"employee", + "label": __("Employee"), + "fieldtype": "Link", + "options": "Employee", + } + ] +}; diff --git a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.json b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.json new file mode 100644 index 0000000000..60fe1ae25c --- /dev/null +++ b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.json @@ -0,0 +1,34 @@ +{ + "add_total_row": 0, + "creation": "2019-09-05 11:18:06.209397", + "disable_prepared_report": 0, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 0, + "is_standard": "Yes", + "letter_head": "sapcon-old", + "modified": "2019-09-05 11:18:06.209397", + "modified_by": "Administrator", + "module": "HR", + "name": "Employee Leave Balance Summary", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "Employee", + "report_name": "Employee Leave Balance Summary", + "report_type": "Script Report", + "roles": [ + { + "role": "Employee" + }, + { + "role": "HR Manager" + }, + { + "role": "HR User" + }, + { + "role": "Leave Approver" + } + ] +} \ No newline at end of file diff --git a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py new file mode 100644 index 0000000000..5512f7aa25 --- /dev/null +++ b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py @@ -0,0 +1,63 @@ +# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals +import frappe +from frappe.utils import flt +from frappe import _ +from erpnext.hr.doctype.leave_application.leave_application \ + import get_leave_balance_on, get_leaves_for_period + +from erpnext.hr.report.employee_leave_balance.employee_leave_balance \ + import get_total_allocated_leaves + +def execute(filters=None): + columns = get_columns() + data = get_data(filters) + + return columns, data + +def get_columns(): + columns = [] + columns.append(_('Leave Type') ) + columns.append(_('Employee')) + columns.append(_('Employee Name')) + columns.append(_('Opening Balance') + ':Float:160') + columns.append(_('Leaves Taken') + ':Float:160') + columns.append(_('Closing Balance') + ':Float:160') + + return columns + +def get_data(filters): + leave_types = frappe.db.sql_list("SELECT `name` FROM `tabLeave Type` ORDER BY `name` ASC") + + if filters.to_date <= filters.from_date: + frappe.throw(_('From date can not be greater than than To date')) + + conditions = { + 'status': 'Active', + } + + if filters.get('employee'): + conditions['name'] = filters.get('employee') + + active_employees = frappe.get_all('Employee', + filters=conditions, + fields=['name', 'employee_name', 'department', 'user_id']) + + data = [] + + for leave_type in leave_types: + for employee in active_employees: + row = [leave_type, employee.name, employee.employee_name] + + leaves_taken = get_leaves_for_period(employee.name, leave_type, + filters.from_date, filters.to_date) * -1 + + opening = get_total_allocated_leaves(employee.name, leave_type, filters.from_date, filters.to_date) + closing = flt(opening) - flt(leaves_taken) + + row += [opening, leaves_taken, closing] + data.append(row) + + return data From 21a4f82f6995ae152fc850ce06a094b733b37108 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Sat, 7 Sep 2019 20:21:56 +0530 Subject: [PATCH 2/4] fix: Re-arrange filters --- .../employee_leave_balance_summary.js | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js index a05db33050..838f4ad147 100644 --- a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js +++ b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.js @@ -2,35 +2,35 @@ // For license information, please see license.txt /* eslint-disable */ -frappe.query_reports["Employee Leave Balance Summary"] = { - "filters": [ +frappe.query_reports['Employee Leave Balance Summary'] = { + filters: [ { - "fieldname":"from_date", - "label": __("From Date"), - "fieldtype": "Date", - "reqd": 1, - "default": frappe.defaults.get_default("year_start_date") + fieldname:'company', + label: __('Company'), + fieldtype: 'Link', + options: 'Company', + reqd: 1, + default: frappe.defaults.get_user_default('Company') }, { - "fieldname":"to_date", - "label": __("To Date"), - "fieldtype": "Date", - "reqd": 1, - "default": frappe.defaults.get_default("year_end_date") + fieldname:'employee', + label: __('Employee'), + fieldtype: 'Link', + options: 'Employee', }, { - "fieldname":"company", - "label": __("Company"), - "fieldtype": "Link", - "options": "Company", - "reqd": 1, - "default": frappe.defaults.get_user_default("Company") + fieldname:'from_date', + label: __('From Date'), + fieldtype: 'Date', + reqd: 1, + default: frappe.defaults.get_default('year_start_date') }, { - "fieldname":"employee", - "label": __("Employee"), - "fieldtype": "Link", - "options": "Employee", + fieldname:'to_date', + label: __('To Date'), + fieldtype: 'Date', + reqd: 1, + default: frappe.defaults.get_default('year_end_date') } ] }; From c5588a9b930ff4ed2d4a5a0bc6d57044df9f1e71 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Sat, 7 Sep 2019 21:26:43 +0530 Subject: [PATCH 3/4] fix: Column value and data indentation --- .../employee_leave_balance_summary.py | 65 ++++++++++++++----- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py index 5512f7aa25..f52a34866a 100644 --- a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py +++ b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py @@ -5,35 +5,59 @@ from __future__ import unicode_literals import frappe from frappe.utils import flt from frappe import _ -from erpnext.hr.doctype.leave_application.leave_application \ - import get_leave_balance_on, get_leaves_for_period +from erpnext.hr.doctype.leave_application.leave_application import get_leaves_for_period -from erpnext.hr.report.employee_leave_balance.employee_leave_balance \ - import get_total_allocated_leaves +from ..employee_leave_balance.employee_leave_balance import get_total_allocated_leaves def execute(filters=None): + if filters.to_date <= filters.from_date: + frappe.throw(_('From date can not be greater than than To date')) + columns = get_columns() data = get_data(filters) return columns, data def get_columns(): - columns = [] - columns.append(_('Leave Type') ) - columns.append(_('Employee')) - columns.append(_('Employee Name')) - columns.append(_('Opening Balance') + ':Float:160') - columns.append(_('Leaves Taken') + ':Float:160') - columns.append(_('Closing Balance') + ':Float:160') + columns = [{ + 'label': _('Leave Type'), + 'fieldtype': 'Link', + 'fieldname': 'leave_type', + 'width': 300, + 'options': 'Leave Type' + }, { + 'label': _('Employee'), + 'fieldtype': 'Link', + 'fieldname': 'employee', + 'width': 100, + 'options': 'Employee' + }, { + 'label': _('Employee Name'), + 'fieldtype': 'Data', + 'fieldname': 'employee_name', + 'width': 100, + }, { + 'label': _('Opening Balance'), + 'fieldtype': 'float', + 'fieldname': 'opening_balance', + 'width': 160, + }, { + 'label': _('Leaves Taken'), + 'fieldtype': 'float', + 'fieldname': 'leaves_taken', + 'width': 160, + }, { + 'label': _('Closing Balance'), + 'fieldtype': 'float', + 'fieldname': 'closing_balance', + 'width': 160, + }] return columns def get_data(filters): leave_types = frappe.db.sql_list("SELECT `name` FROM `tabLeave Type` ORDER BY `name` ASC") - if filters.to_date <= filters.from_date: - frappe.throw(_('From date can not be greater than than To date')) - conditions = { 'status': 'Active', } @@ -48,8 +72,14 @@ def get_data(filters): data = [] for leave_type in leave_types: + data.append({ + 'leave_type': leave_type + }) for employee in active_employees: - row = [leave_type, employee.name, employee.employee_name] + row = frappe._dict({ + 'employee': employee.name, + 'employee_name': employee.employee_name + }) leaves_taken = get_leaves_for_period(employee.name, leave_type, filters.from_date, filters.to_date) * -1 @@ -57,7 +87,10 @@ def get_data(filters): opening = get_total_allocated_leaves(employee.name, leave_type, filters.from_date, filters.to_date) closing = flt(opening) - flt(leaves_taken) - row += [opening, leaves_taken, closing] + row.opening_balance = opening + row.leaves_taken = leaves_taken + row.closing_balance = closing + row.indent = 1 data.append(row) return data From 5ae40a950091482dac63eb9552d42bd7931ee332 Mon Sep 17 00:00:00 2001 From: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> Date: Sun, 8 Sep 2019 15:16:35 +0530 Subject: [PATCH 4/4] fix: Use absolute import instead of relative import Co-Authored-By: Mangesh-Khairnar --- .../employee_leave_balance_summary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py index f52a34866a..64a5c1c13a 100644 --- a/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py +++ b/erpnext/hr/report/employee_leave_balance_summary/employee_leave_balance_summary.py @@ -7,7 +7,7 @@ from frappe.utils import flt from frappe import _ from erpnext.hr.doctype.leave_application.leave_application import get_leaves_for_period -from ..employee_leave_balance.employee_leave_balance import get_total_allocated_leaves +from erpnext.hr.report.employee_leave_balance.employee_leave_balance import get_total_allocated_leaves def execute(filters=None): if filters.to_date <= filters.from_date: