From ed1b8cfe3ff319558ca3aabdb6cc37747023da20 Mon Sep 17 00:00:00 2001 From: Ben Cornwell-Mott Date: Tue, 26 Jul 2016 06:11:03 -0700 Subject: [PATCH] Added a Calendar view for timesheet logs --- .../projects/doctype/timesheet/timesheet.py | 29 +++++++++++++++++++ .../doctype/timesheet/timesheet_calendar.js | 27 +++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 erpnext/projects/doctype/timesheet/timesheet_calendar.js diff --git a/erpnext/projects/doctype/timesheet/timesheet.py b/erpnext/projects/doctype/timesheet/timesheet.py index abcffad1c7..9140927279 100644 --- a/erpnext/projects/doctype/timesheet/timesheet.py +++ b/erpnext/projects/doctype/timesheet/timesheet.py @@ -6,6 +6,7 @@ from __future__ import unicode_literals import frappe from frappe import _ +import json from datetime import timedelta from frappe.utils import flt, time_diff_in_hours, get_datetime, getdate, cint, get_datetime_str from frappe.model.document import Document @@ -290,3 +291,31 @@ def get_activity_cost(employee=None, activity_type=None): ["costing_rate", "billing_rate"], as_dict=True) return rate[0] if rate else {} + +@frappe.whitelist() +def get_events(start, end, filters=None): + """Returns events for Gantt / Calendar view rendering. + :param start: Start date-time. + :param end: End date-time. + :param filters: Filters (JSON). + """ + filters = json.loads(filters) + + conditions = get_conditions(filters) + return frappe.db.sql("""select `tabTimesheet Detail`.name as name, `tabTimesheet Detail`.parent as parent, + from_time, hours, activity_type, project, to_time from `tabTimesheet Detail`, + `tabTimesheet` where `tabTimesheet Detail`.parent = `tabTimesheet`.name and + (from_time between %(start)s and %(end)s) {conditions}""".format(conditions=conditions), + { + "start": start, + "end": end + }, as_dict=True, update={"allDay": 0}) + +def get_conditions(filters): + conditions = [] + abbr = {'employee': 'tabTimesheet', 'project': 'tabTimesheet Detail'} + for key in filters: + if filters.get(key): + conditions.append("`%s`.%s = '%s'"%(abbr.get(key), key, filters.get(key))) + + return " and {}".format(" and ".join(conditions)) if conditions else "" diff --git a/erpnext/projects/doctype/timesheet/timesheet_calendar.js b/erpnext/projects/doctype/timesheet/timesheet_calendar.js new file mode 100644 index 0000000000..6db3e5aacd --- /dev/null +++ b/erpnext/projects/doctype/timesheet/timesheet_calendar.js @@ -0,0 +1,27 @@ +frappe.views.calendar["Timesheet"] = { + field_map: { + "start": "from_time", + "end": "to_time", + "name": "parent", + "id": "parent", + "title": "activity_type", + "allDay": "allDay", + "child_name": "name" + }, + gantt: true, + filters: [ + { + "fieldtype": "Link", + "fieldname": "project", + "options": "Project", + "label": __("Project") + }, + { + "fieldtype": "Link", + "fieldname": "employee", + "options": "Employee", + "label": __("Employee") + } + ], + get_events_method: "erpnext.projects.doctype.timesheet.timesheet.get_events" +} \ No newline at end of file