From 94514ffee90943b2892d22aedc6d51f96670c0b0 Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Mon, 11 Mar 2019 10:37:28 +0530 Subject: [PATCH 1/7] feat: timesheet Employee Summary Report --- .../employee_billing_summary/__init__.py | 0 .../employee_billing_summary.js | 27 ++++ .../employee_billing_summary.json | 36 ++++++ .../employee_billing_summary.py | 119 ++++++++++++++++++ erpnext/public/node_modules | 1 + 5 files changed, 183 insertions(+) create mode 100644 erpnext/projects/report/employee_billing_summary/__init__.py create mode 100644 erpnext/projects/report/employee_billing_summary/employee_billing_summary.js create mode 100644 erpnext/projects/report/employee_billing_summary/employee_billing_summary.json create mode 100644 erpnext/projects/report/employee_billing_summary/employee_billing_summary.py create mode 120000 erpnext/public/node_modules diff --git a/erpnext/projects/report/employee_billing_summary/__init__.py b/erpnext/projects/report/employee_billing_summary/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js new file mode 100644 index 0000000000..e6e674666d --- /dev/null +++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js @@ -0,0 +1,27 @@ +// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +/* eslint-disable */ + +frappe.query_reports["Employee Billing Summary"] = { + "filters": [ + { + fieldname: "employee", + label: __("Employee"), + fieldtype: "Link", + options: "Employee", + }, + { + fieldname:"from_date", + label: __("From Date"), + fieldtype: "Date", + default: frappe.datetime.get_today() + }, + { + fieldname:"to_date", + label: __("To Date"), + fieldtype: "Date", + default: frappe.datetime.add_days(frappe.datetime.get_today(), 30) + }, + + ] +} diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.json b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.json new file mode 100644 index 0000000000..433ebac5dd --- /dev/null +++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.json @@ -0,0 +1,36 @@ +{ + "add_total_row": 0, + "creation": "2019-03-08 15:08:19.929728", + "disable_prepared_report": 0, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 0, + "is_standard": "Yes", + "modified": "2019-03-08 15:08:19.929728", + "modified_by": "Administrator", + "module": "Projects", + "name": "Employee Billing Summary", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "Timesheet", + "report_name": "Employee Billing Summary", + "report_type": "Script Report", + "roles": [ + { + "role": "Projects User" + }, + { + "role": "HR User" + }, + { + "role": "Manufacturing User" + }, + { + "role": "Employee" + }, + { + "role": "Accounts User" + } + ] +} \ No newline at end of file diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py new file mode 100644 index 0000000000..47323efabe --- /dev/null +++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py @@ -0,0 +1,119 @@ +# 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 import _ +from frappe.utils import time_diff_in_hours + +def execute(filters=None): + filters = frappe._dict(filters or {}) + print(filters) + + columns = get_columns() + + data = get_data(filters) + return columns, data + +def get_columns(): + return [ + { + "label": _("Employee ID"), + "fieldtype": "Link", + "fieldname": "employee", + "options": "Employee", + "width": 300 + }, + { + "label": _("Employee Name"), + "fieldtype": "data", + "fieldname": "employee_name", + "hidden": 1, + "width": 200 + }, + { + "label": _("Timesheet"), + "fieldtype": "Link", + "fieldname": "timesheet", + "options": "Timesheet", + "width": 150 + }, + { + "label": _("Date"), + "fieldtype": "Date", + "fieldname": "date", + "width": 150 + }, + { + "label": _("Total Billable Hours"), + "fieldtype": "Int", + "fieldname": "total_billable_hours", + "width": 50 + }, + { + "label": _("Total Hours"), + "fieldtype": "Int", + "fieldname": "total_hours", + "width": 50 + }, + { + "label": _("Amount"), + "fieldtype": "Int", + "fieldname": "amount", + "width": 50 + } + ] + +def get_data(filters): + data = [] + if "employee" in filters: + record= frappe.db.sql('''SELECT + employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount + FROM + `tabTimesheet` + WHERE + employee = %s and (start_date <= %s and end_date >= %s)''',(filters.employee, filters.to_date, filters.from_date), + as_dict=1 + ) + for entries in record: + + timesheet_details = frappe.get_all( + "Timesheet Detail", + filters={"parent": entries.name}, + fields=["*"] + ) + + total_hours = 0 + total_billable_hours = 0 + print("-------------------------------------------->>>>>>>") + for time in timesheet_details: + time_start = time.from_time + time_end = frappe.utils.add_to_date(time.from_time, hours=time.hours) + + from_date = frappe.utils.get_datetime(filters.from_date) + to_date = frappe.utils.get_datetime(filters.to_date) + + if time_start <= from_date and time_end <= to_date: + total_hours += abs(time_diff_in_hours(time_end, from_date)) + print(from_date, time_end) + print("case 1", entries.name,time_diff_in_hours(time_end, from_date)) + elif time_start >= from_date and time_end >= to_date: + total_hours += abs(time_diff_in_hours(to_date, time_start)) + print(time_start, to_date) + print("case 2", entries.name,time_diff_in_hours(to_date, time_start)) + elif time_start >= from_date and time_end <= to_date: + total_hours = entries.total_hours + print("case 3 all set", entries.name) + + print(total_hours) + print("-------------------------------------------->>>>>>>") + row = { + "employee": entries.employee, + "employee_name": entries.employee_name, + "timesheet": entries.name, + "total_billable_hours": entries.total_billable_hours, + "total_hours": entries.total_hours, + "amount": 1 + } + data.append(row) + return data \ No newline at end of file diff --git a/erpnext/public/node_modules b/erpnext/public/node_modules new file mode 120000 index 0000000000..229573e057 --- /dev/null +++ b/erpnext/public/node_modules @@ -0,0 +1 @@ +/Users/anuragmishra/test/apps/erpnext/node_modules \ No newline at end of file From 3d27aabdeb4dee973c5142a8580636fafa6cc22d Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Mon, 11 Mar 2019 11:46:01 +0530 Subject: [PATCH 2/7] Commonify code --- .../employee_billing_summary.js | 1 - .../employee_billing_summary.py | 35 ++++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js index e6e674666d..b792e818d8 100644 --- a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js +++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js @@ -22,6 +22,5 @@ frappe.query_reports["Employee Billing Summary"] = { fieldtype: "Date", default: frappe.datetime.add_days(frappe.datetime.get_today(), 30) }, - ] } diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py index 47323efabe..491fa764d9 100644 --- a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py +++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py @@ -8,8 +8,6 @@ from frappe.utils import time_diff_in_hours def execute(filters=None): filters = frappe._dict(filters or {}) - print(filters) - columns = get_columns() data = get_data(filters) @@ -85,7 +83,8 @@ def get_data(filters): total_hours = 0 total_billable_hours = 0 - print("-------------------------------------------->>>>>>>") + total_amount = 0 + for time in timesheet_details: time_start = time.from_time time_end = frappe.utils.add_to_date(time.from_time, hours=time.hours) @@ -94,26 +93,28 @@ def get_data(filters): to_date = frappe.utils.get_datetime(filters.to_date) if time_start <= from_date and time_end <= to_date: - total_hours += abs(time_diff_in_hours(time_end, from_date)) - print(from_date, time_end) - print("case 1", entries.name,time_diff_in_hours(time_end, from_date)) + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, from_date, total_hours, total_billable_hours, total_amount) elif time_start >= from_date and time_end >= to_date: - total_hours += abs(time_diff_in_hours(to_date, time_start)) - print(time_start, to_date) - print("case 2", entries.name,time_diff_in_hours(to_date, time_start)) + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, to_date, time_start, total_hours, total_billable_hours, total_amount) elif time_start >= from_date and time_end <= to_date: - total_hours = entries.total_hours - print("case 3 all set", entries.name) + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, time_start, total_hours, total_billable_hours, total_amount) - print(total_hours) - print("-------------------------------------------->>>>>>>") row = { "employee": entries.employee, "employee_name": entries.employee_name, "timesheet": entries.name, - "total_billable_hours": entries.total_billable_hours, - "total_hours": entries.total_hours, - "amount": 1 + "total_billable_hours": total_billable_hours, + "total_hours": total_hours, + "amount": total_amount } + data.append(row) - return data \ No newline at end of file + return data + +def get_billable_and_total_hours(time, end, start, total_hours, total_billable_hours, total_amount): + total_hours += abs(time_diff_in_hours(end, start)) + if time.billable: + total_billable_hours += abs(time_diff_in_hours(end, start)) + total_amount += total_billable_hours * time.billing_rate + + return total_hours, total_billable_hours, total_amount From 61aab377b67d2feb09ab31b1655806f4bc9b49d5 Mon Sep 17 00:00:00 2001 From: Anurag Mishra <32095923+Anurag810@users.noreply.github.com> Date: Mon, 11 Mar 2019 12:02:25 +0530 Subject: [PATCH 3/7] Minor Fixes --- erpnext/public/node_modules | 1 - 1 file changed, 1 deletion(-) delete mode 120000 erpnext/public/node_modules diff --git a/erpnext/public/node_modules b/erpnext/public/node_modules deleted file mode 120000 index 229573e057..0000000000 --- a/erpnext/public/node_modules +++ /dev/null @@ -1 +0,0 @@ -/Users/anuragmishra/test/apps/erpnext/node_modules \ No newline at end of file From b57c024ff7954a08d169bbdf84e016064ca3fcdd Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Mon, 11 Mar 2019 17:43:44 +0530 Subject: [PATCH 4/7] feat: Project Billing Summary for timesheet --- erpnext/projects/report/billing_summary.py | 137 ++++++++++++++++++ .../employee_billing_summary.py | 110 +------------- .../project_billing_summary/__init__.py | 0 .../project_billing_summary.js | 26 ++++ .../project_billing_summary.json | 36 +++++ .../project_billing_summary.py | 14 ++ 6 files changed, 215 insertions(+), 108 deletions(-) create mode 100644 erpnext/projects/report/billing_summary.py create mode 100644 erpnext/projects/report/project_billing_summary/__init__.py create mode 100644 erpnext/projects/report/project_billing_summary/project_billing_summary.js create mode 100644 erpnext/projects/report/project_billing_summary/project_billing_summary.json create mode 100644 erpnext/projects/report/project_billing_summary/project_billing_summary.py diff --git a/erpnext/projects/report/billing_summary.py b/erpnext/projects/report/billing_summary.py new file mode 100644 index 0000000000..e34e90b1a4 --- /dev/null +++ b/erpnext/projects/report/billing_summary.py @@ -0,0 +1,137 @@ +# 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 import _ +from frappe.utils import time_diff_in_hours + + +def get_columns(): + return [ + { + "label": _("Employee ID"), + "fieldtype": "Link", + "fieldname": "employee", + "options": "Employee", + "width": 300 + }, + { + "label": _("Employee Name"), + "fieldtype": "data", + "fieldname": "employee_name", + "hidden": 1, + "width": 200 + }, + { + "label": _("Timesheet"), + "fieldtype": "Link", + "fieldname": "timesheet", + "options": "Timesheet", + "width": 150 + }, + { + "label": _("Total Billable Hours"), + "fieldtype": "Int", + "fieldname": "total_billable_hours", + "width": 50 + }, + { + "label": _("Total Hours"), + "fieldtype": "Int", + "fieldname": "total_hours", + "width": 50 + }, + { + "label": _("Amount"), + "fieldtype": "Int", + "fieldname": "amount", + "width": 100 + } + ] + +def get_data(filters): + data = [] + + if "employee" in filters: + record= frappe.db.sql('''SELECT + employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount + FROM + `tabTimesheet` + WHERE + employee = %s and (start_date <= %s and end_date >= %s)''',(filters.employee, filters.to_date, filters.from_date), + as_dict=1 + ) + + elif "project" in filters: + record= frappe.db.sql('''SELECT + employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount + FROM + `tabTimesheet` + WHERE + start_date <= %s and end_date >= %s''',(filters.to_date, filters.from_date), + as_dict=1 + ) + else: + record = {} + + + for entries in record: + + timesheet_details_filter = {"parent": entries.name} + + if "project" in filters: + timesheet_details_filter["project"] = filters.project + + timesheet_details = frappe.get_all( + "Timesheet Detail", + filters = timesheet_details_filter, + fields=["*"] + ) + + total_hours = 0 + total_billable_hours = 0 + total_amount = 0 + check_entries = False + + for time in timesheet_details: + + check_entries = True + + time_start = time.from_time + time_end = frappe.utils.add_to_date(time.from_time, hours=time.hours) + + from_date = frappe.utils.get_datetime(filters.from_date) + to_date = frappe.utils.get_datetime(filters.to_date) + + if time_start <= from_date and time_end <= to_date: + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, from_date, total_hours, total_billable_hours, total_amount) + elif time_start >= from_date and time_end >= to_date: + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, to_date, time_start, total_hours, total_billable_hours, total_amount) + elif time_start >= from_date and time_end <= to_date: + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, time_start, total_hours, total_billable_hours, total_amount) + + row = { + "employee": entries.employee, + "employee_name": entries.employee_name, + "timesheet": entries.name, + "total_billable_hours": total_billable_hours, + "total_hours": total_hours, + "amount": total_amount + } + + if check_entries: + data.append(row) + check_entries = False + + return data + + +def get_billable_and_total_hours(time, end, start, total_hours, total_billable_hours, total_amount): + total_hours += abs(time_diff_in_hours(end, start)) + if time.billable: + total_billable_hours += abs(time_diff_in_hours(end, start)) + total_amount += total_billable_hours * time.billing_rate + + return total_hours, total_billable_hours, total_amount \ No newline at end of file diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py index 491fa764d9..cd5ad7803a 100644 --- a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py +++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.py @@ -4,117 +4,11 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import time_diff_in_hours +from erpnext.projects.report.billing_summary import get_columns, get_data def execute(filters=None): filters = frappe._dict(filters or {}) columns = get_columns() data = get_data(filters) - return columns, data - -def get_columns(): - return [ - { - "label": _("Employee ID"), - "fieldtype": "Link", - "fieldname": "employee", - "options": "Employee", - "width": 300 - }, - { - "label": _("Employee Name"), - "fieldtype": "data", - "fieldname": "employee_name", - "hidden": 1, - "width": 200 - }, - { - "label": _("Timesheet"), - "fieldtype": "Link", - "fieldname": "timesheet", - "options": "Timesheet", - "width": 150 - }, - { - "label": _("Date"), - "fieldtype": "Date", - "fieldname": "date", - "width": 150 - }, - { - "label": _("Total Billable Hours"), - "fieldtype": "Int", - "fieldname": "total_billable_hours", - "width": 50 - }, - { - "label": _("Total Hours"), - "fieldtype": "Int", - "fieldname": "total_hours", - "width": 50 - }, - { - "label": _("Amount"), - "fieldtype": "Int", - "fieldname": "amount", - "width": 50 - } - ] - -def get_data(filters): - data = [] - if "employee" in filters: - record= frappe.db.sql('''SELECT - employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount - FROM - `tabTimesheet` - WHERE - employee = %s and (start_date <= %s and end_date >= %s)''',(filters.employee, filters.to_date, filters.from_date), - as_dict=1 - ) - for entries in record: - - timesheet_details = frappe.get_all( - "Timesheet Detail", - filters={"parent": entries.name}, - fields=["*"] - ) - - total_hours = 0 - total_billable_hours = 0 - total_amount = 0 - - for time in timesheet_details: - time_start = time.from_time - time_end = frappe.utils.add_to_date(time.from_time, hours=time.hours) - - from_date = frappe.utils.get_datetime(filters.from_date) - to_date = frappe.utils.get_datetime(filters.to_date) - - if time_start <= from_date and time_end <= to_date: - total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, from_date, total_hours, total_billable_hours, total_amount) - elif time_start >= from_date and time_end >= to_date: - total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, to_date, time_start, total_hours, total_billable_hours, total_amount) - elif time_start >= from_date and time_end <= to_date: - total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, time_start, total_hours, total_billable_hours, total_amount) - - row = { - "employee": entries.employee, - "employee_name": entries.employee_name, - "timesheet": entries.name, - "total_billable_hours": total_billable_hours, - "total_hours": total_hours, - "amount": total_amount - } - - data.append(row) - return data - -def get_billable_and_total_hours(time, end, start, total_hours, total_billable_hours, total_amount): - total_hours += abs(time_diff_in_hours(end, start)) - if time.billable: - total_billable_hours += abs(time_diff_in_hours(end, start)) - total_amount += total_billable_hours * time.billing_rate - - return total_hours, total_billable_hours, total_amount + return columns, data \ No newline at end of file diff --git a/erpnext/projects/report/project_billing_summary/__init__.py b/erpnext/projects/report/project_billing_summary/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/projects/report/project_billing_summary/project_billing_summary.js b/erpnext/projects/report/project_billing_summary/project_billing_summary.js new file mode 100644 index 0000000000..18dbbd19bb --- /dev/null +++ b/erpnext/projects/report/project_billing_summary/project_billing_summary.js @@ -0,0 +1,26 @@ +// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +/* eslint-disable */ + +frappe.query_reports["Project Billing Summary"] = { + "filters": [ + { + fieldname: "project", + label: __("Project"), + fieldtype: "Link", + options: "Project", + }, + { + fieldname:"from_date", + label: __("From Date"), + fieldtype: "Date", + default: frappe.datetime.get_today() + }, + { + fieldname:"to_date", + label: __("To Date"), + fieldtype: "Date", + default: frappe.datetime.add_days(frappe.datetime.get_today(), 30) + }, + ] +} diff --git a/erpnext/projects/report/project_billing_summary/project_billing_summary.json b/erpnext/projects/report/project_billing_summary/project_billing_summary.json new file mode 100644 index 0000000000..a3f91c802d --- /dev/null +++ b/erpnext/projects/report/project_billing_summary/project_billing_summary.json @@ -0,0 +1,36 @@ +{ + "add_total_row": 0, + "creation": "2019-03-11 16:22:39.460524", + "disable_prepared_report": 0, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 0, + "is_standard": "Yes", + "modified": "2019-03-11 16:22:39.460524", + "modified_by": "Administrator", + "module": "Projects", + "name": "Project Billing Summary", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "Timesheet", + "report_name": "Project Billing Summary", + "report_type": "Script Report", + "roles": [ + { + "role": "Projects User" + }, + { + "role": "HR User" + }, + { + "role": "Manufacturing User" + }, + { + "role": "Employee" + }, + { + "role": "Accounts User" + } + ] +} \ No newline at end of file diff --git a/erpnext/projects/report/project_billing_summary/project_billing_summary.py b/erpnext/projects/report/project_billing_summary/project_billing_summary.py new file mode 100644 index 0000000000..cd5ad7803a --- /dev/null +++ b/erpnext/projects/report/project_billing_summary/project_billing_summary.py @@ -0,0 +1,14 @@ +# 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 import _ +from erpnext.projects.report.billing_summary import get_columns, get_data + +def execute(filters=None): + filters = frappe._dict(filters or {}) + columns = get_columns() + + data = get_data(filters) + return columns, data \ No newline at end of file From 5c17e7760bb0c4f3ab58c668d5ab130743ea8b05 Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Mon, 11 Mar 2019 18:17:22 +0530 Subject: [PATCH 5/7] fix: readability --- erpnext/projects/report/billing_summary.py | 104 +++++++++++---------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/erpnext/projects/report/billing_summary.py b/erpnext/projects/report/billing_summary.py index e34e90b1a4..cbea5f5c40 100644 --- a/erpnext/projects/report/billing_summary.py +++ b/erpnext/projects/report/billing_summary.py @@ -54,63 +54,37 @@ def get_columns(): def get_data(filters): data = [] - if "employee" in filters: - record= frappe.db.sql('''SELECT - employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount - FROM - `tabTimesheet` - WHERE - employee = %s and (start_date <= %s and end_date >= %s)''',(filters.employee, filters.to_date, filters.from_date), - as_dict=1 - ) - - elif "project" in filters: - record= frappe.db.sql('''SELECT - employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount - FROM - `tabTimesheet` - WHERE - start_date <= %s and end_date >= %s''',(filters.to_date, filters.from_date), - as_dict=1 - ) - else: - record = {} - + record = get_records(filters) for entries in record: - - timesheet_details_filter = {"parent": entries.name} - - if "project" in filters: - timesheet_details_filter["project"] = filters.project - - timesheet_details = frappe.get_all( - "Timesheet Detail", - filters = timesheet_details_filter, - fields=["*"] - ) - total_hours = 0 total_billable_hours = 0 total_amount = 0 - check_entries = False + entries_exists = False - for time in timesheet_details: + timesheet_details = get_timesheet_details(filters, entries.name) - check_entries = True + for activity in timesheet_details: - time_start = time.from_time - time_end = frappe.utils.add_to_date(time.from_time, hours=time.hours) + entries_exists = True + + time_start = activity.from_time + time_end = frappe.utils.add_to_date(activity.from_time, hours=activity.hours) from_date = frappe.utils.get_datetime(filters.from_date) to_date = frappe.utils.get_datetime(filters.to_date) if time_start <= from_date and time_end <= to_date: - total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, from_date, total_hours, total_billable_hours, total_amount) + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(activity, + time_end, from_date, total_hours, total_billable_hours, total_amount) + elif time_start >= from_date and time_end >= to_date: - total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, to_date, time_start, total_hours, total_billable_hours, total_amount) + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(activity, + to_date, time_start, total_hours, total_billable_hours, total_amount) + elif time_start >= from_date and time_end <= to_date: - total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(time, time_end, time_start, total_hours, total_billable_hours, total_amount) + total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(activity, + time_end, time_start, total_hours, total_billable_hours, total_amount) row = { "employee": entries.employee, @@ -121,17 +95,51 @@ def get_data(filters): "amount": total_amount } - if check_entries: + if entries_exists: data.append(row) - check_entries = False + entries_exists = False return data +def get_records(filters): + if "employee" in filters: + return frappe.db.sql('''SELECT + employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount + FROM + `tabTimesheet` + WHERE + employee = %s and (start_date <= %s and end_date >= %s)''', + (filters.employee, filters.to_date, filters.from_date), + as_dict=1 + ) -def get_billable_and_total_hours(time, end, start, total_hours, total_billable_hours, total_amount): + elif "project" in filters: + return frappe.db.sql('''SELECT + employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount + FROM + `tabTimesheet` + WHERE + start_date <= %s and end_date >= %s''',(filters.to_date, filters.from_date), + as_dict=1 + ) + else: + return {} + +def get_billable_and_total_hours(activity, end, start, total_hours, total_billable_hours, total_amount): total_hours += abs(time_diff_in_hours(end, start)) - if time.billable: + if activity.billable: total_billable_hours += abs(time_diff_in_hours(end, start)) - total_amount += total_billable_hours * time.billing_rate + total_amount += total_billable_hours * activity.billing_rate + return total_hours, total_billable_hours, total_amount - return total_hours, total_billable_hours, total_amount \ No newline at end of file +def get_timesheet_details(filters, parent): + timesheet_details_filter = {"parent": parent} + + if "project" in filters: + timesheet_details_filter["project"] = filters.project + + return frappe.get_all( + "Timesheet Detail", + filters = timesheet_details_filter, + fields=["*"] + ) \ No newline at end of file From f71c147ea8dbcd70cbf66cfbaee79667907996f6 Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Tue, 12 Mar 2019 11:00:02 +0530 Subject: [PATCH 6/7] Added mandatory feilds to the report --- .../employee_billing_summary/employee_billing_summary.js | 7 +++++-- .../project_billing_summary/project_billing_summary.js | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js index b792e818d8..65c2a690cf 100644 --- a/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js +++ b/erpnext/projects/report/employee_billing_summary/employee_billing_summary.js @@ -9,18 +9,21 @@ frappe.query_reports["Employee Billing Summary"] = { label: __("Employee"), fieldtype: "Link", options: "Employee", + reqd: 1 }, { fieldname:"from_date", label: __("From Date"), fieldtype: "Date", - default: frappe.datetime.get_today() + default: frappe.datetime.get_today(), + reqd: 1 }, { fieldname:"to_date", label: __("To Date"), fieldtype: "Date", - default: frappe.datetime.add_days(frappe.datetime.get_today(), 30) + default: frappe.datetime.add_days(frappe.datetime.get_today(), 30), + reqd: 1 }, ] } diff --git a/erpnext/projects/report/project_billing_summary/project_billing_summary.js b/erpnext/projects/report/project_billing_summary/project_billing_summary.js index 18dbbd19bb..62362c35cf 100644 --- a/erpnext/projects/report/project_billing_summary/project_billing_summary.js +++ b/erpnext/projects/report/project_billing_summary/project_billing_summary.js @@ -9,18 +9,21 @@ frappe.query_reports["Project Billing Summary"] = { label: __("Project"), fieldtype: "Link", options: "Project", + reqd: 1 }, { fieldname:"from_date", label: __("From Date"), fieldtype: "Date", - default: frappe.datetime.get_today() + default: frappe.datetime.get_today(), + reqd: 1 }, { fieldname:"to_date", label: __("To Date"), fieldtype: "Date", - default: frappe.datetime.add_days(frappe.datetime.get_today(), 30) + default: frappe.datetime.add_days(frappe.datetime.get_today(), 30), + reqd: 1 }, ] } From 0dc62e8747b18d0f2cffe0daa85ae3e4d1bd09d3 Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Tue, 12 Mar 2019 13:12:31 +0530 Subject: [PATCH 7/7] fix: Removed raw query and used frappe.get_all --- erpnext/projects/report/billing_summary.py | 40 +++++----------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/erpnext/projects/report/billing_summary.py b/erpnext/projects/report/billing_summary.py index cbea5f5c40..214dcef8fd 100644 --- a/erpnext/projects/report/billing_summary.py +++ b/erpnext/projects/report/billing_summary.py @@ -7,7 +7,6 @@ import frappe from frappe import _ from frappe.utils import time_diff_in_hours - def get_columns(): return [ { @@ -53,7 +52,6 @@ def get_columns(): def get_data(filters): data = [] - record = get_records(filters) for entries in record: @@ -61,27 +59,20 @@ def get_data(filters): total_billable_hours = 0 total_amount = 0 entries_exists = False - timesheet_details = get_timesheet_details(filters, entries.name) - for activity in timesheet_details: - entries_exists = True - time_start = activity.from_time time_end = frappe.utils.add_to_date(activity.from_time, hours=activity.hours) - from_date = frappe.utils.get_datetime(filters.from_date) to_date = frappe.utils.get_datetime(filters.to_date) if time_start <= from_date and time_end <= to_date: total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(activity, time_end, from_date, total_hours, total_billable_hours, total_amount) - elif time_start >= from_date and time_end >= to_date: total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(activity, to_date, time_start, total_hours, total_billable_hours, total_amount) - elif time_start >= from_date and time_end <= to_date: total_hours, total_billable_hours, total_amount = get_billable_and_total_hours(activity, time_end, time_start, total_hours, total_billable_hours, total_amount) @@ -102,28 +93,15 @@ def get_data(filters): return data def get_records(filters): - if "employee" in filters: - return frappe.db.sql('''SELECT - employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount - FROM - `tabTimesheet` - WHERE - employee = %s and (start_date <= %s and end_date >= %s)''', - (filters.employee, filters.to_date, filters.from_date), - as_dict=1 - ) + record_filters = [ + ["start_date", "<=", filters.to_date], + ["end_date", ">=", filters.from_date] + ] - elif "project" in filters: - return frappe.db.sql('''SELECT - employee, employee_name, name, total_billable_hours, total_hours, total_billable_amount - FROM - `tabTimesheet` - WHERE - start_date <= %s and end_date >= %s''',(filters.to_date, filters.from_date), - as_dict=1 - ) - else: - return {} + if "employee" in filters: + record_filters.append(["employee", "=", filters.employee]) + + return frappe.get_all("Timesheet", filters=record_filters, fields=[" * "] ) def get_billable_and_total_hours(activity, end, start, total_hours, total_billable_hours, total_amount): total_hours += abs(time_diff_in_hours(end, start)) @@ -142,4 +120,4 @@ def get_timesheet_details(filters, parent): "Timesheet Detail", filters = timesheet_details_filter, fields=["*"] - ) \ No newline at end of file + )