From bd3863dd2e2b61ecdf3af13989082ca4bd345a16 Mon Sep 17 00:00:00 2001 From: Ananyobrata Pal Date: Thu, 10 Oct 2024 17:23:39 +0530 Subject: [PATCH] fix: make timesheet functionality work on standalone installations --- frappe_slack_connector/api/slash_timesheet.py | 5 ++ frappe_slack_connector/db/timesheet.py | 63 ++++++++++++++----- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/frappe_slack_connector/api/slash_timesheet.py b/frappe_slack_connector/api/slash_timesheet.py index 9f9eca2..a7c3327 100644 --- a/frappe_slack_connector/api/slash_timesheet.py +++ b/frappe_slack_connector/api/slash_timesheet.py @@ -40,6 +40,11 @@ def slash_timesheet(): ignore_permissions=True, ) + if not projects: + raise Exception("No projects found") + if not tasks: + raise Exception("No tasks found") + slack.slack_app.client.views_open( trigger_id=frappe.form_dict.get("trigger_id"), view={ diff --git a/frappe_slack_connector/db/timesheet.py b/frappe_slack_connector/db/timesheet.py index 336616f..1c387ad 100644 --- a/frappe_slack_connector/db/timesheet.py +++ b/frappe_slack_connector/db/timesheet.py @@ -10,17 +10,22 @@ def get_employee_working_hours(employee: str = None): """ if not employee: employee = get_employee_from_user() - working_hour, working_frequency = frappe.get_value( - "Employee", - employee, - ["custom_working_hours", "custom_work_schedule"], - ) + + working_hour = None + working_frequency = None + if custom_timesheet_exists(): + working_hour, working_frequency = frappe.get_value( + "Employee", + employee, + ["custom_working_hours", "custom_work_schedule"], + ) if not working_hour: working_hour = frappe.db.get_single_value( "HR Settings", "standard_working_hours" ) if not working_frequency: working_frequency = "Per Day" + return {"working_hour": working_hour or 8, "working_frequency": working_frequency} @@ -71,26 +76,50 @@ def create_timesheet_detail( employee: str, parent: str | None = None, ): + pms_installed = custom_timesheet_exists() + if parent: timesheet = frappe.get_doc("Timesheet", parent) else: timesheet = frappe.get_doc({"doctype": "Timesheet", "employee": employee}) - project, custom_is_billable = frappe.get_value( - "Task", task, ["project", "custom_is_billable"] - ) + project = None + custom_is_billable = None + + fields = ["project"] + if pms_installed: + fields.append("custom_is_billable") + + field_values = frappe.get_value("Task", task, fields) + if pms_installed: + project, custom_is_billable = field_values + else: + project = field_values + + logs = { + "task": task, + "hours": hours, + "description": description, + "from_time": getdate(date), + "to_time": getdate(date), + "project": project, + } + + if pms_installed: + logs.update({"is_billable": custom_is_billable}) timesheet.update({"parent_project": project}) timesheet.append( "time_logs", - { - "task": task, - "hours": hours, - "description": description, - "from_time": getdate(date), - "to_time": getdate(date), - "project": project, - "is_billable": custom_is_billable, - }, + logs, ) timesheet.save() + + +def custom_timesheet_exists() -> bool: + """ + Check if the custom fields for timesheet doctype exists + These fields are taken from the frappe_pms app if installed + """ + installed_apps = frappe.get_installed_apps() + return "frappe_pms" in installed_apps