Skip to content

Commit

Permalink
refactor: Working Time
Browse files Browse the repository at this point in the history
  • Loading branch information
scdanieli committed Jan 30, 2023
1 parent 5ad7162 commit d811713
Showing 1 changed file with 65 additions and 56 deletions.
121 changes: 65 additions & 56 deletions working_time/working_time/doctype/working_time/working_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from frappe.utils import time_diff_in_seconds
from working_time.jira_client import JiraClient

HALF_DAY = 3.25
OVERTIME_FACTOR = 1.15
MAX_HALF_DAY = HALF_DAY * OVERTIME_FACTOR * 60 * 60
FIVE_MINUTES = 5 * 60
ONE_HOUR = 60 * 60


class WorkingTime(Document):
def before_validate(self):
Expand Down Expand Up @@ -52,10 +58,6 @@ def set_durations(self):
self.working_time += log.duration

def create_attendance(self):
HALF_DAY = 3.25
OVERTIME_FACTOR = 1.15
MAX_HALF_DAY = HALF_DAY * OVERTIME_FACTOR * 60 * 60

if not frappe.db.exists(
"Attendance", {"employee": self.employee, "attendance_date": self.date}
):
Expand All @@ -74,17 +76,9 @@ def create_attendance(self):
attendance.submit()

def create_timesheets(self):
FIVE_MINUTES = 5 * 60
ONE_HOUR = 60 * 60

costing_rate = frappe.get_value(
"Activity Cost",
{"activity_type": "Default", "employee": self.employee},
"costing_rate",
)

for log in self.time_logs:
if log.duration:
if log.duration and log.project:
costing_rate = get_costing_rate(self.employee)
hours = math.ceil(log.duration / FIVE_MINUTES) * FIVE_MINUTES / ONE_HOUR
billing_hours = (
math.ceil(
Expand All @@ -94,46 +88,61 @@ def create_timesheets(self):
/ ONE_HOUR
)

if log.project:
customer, billing_rate, jira_site = frappe.get_value(
"Project",
log.project,
["customer", "billing_rate", "jira_site"],
)

if log.key:
jira_issue_url = f"https://{jira_site}/browse/{log.key}"
description = f"{JiraClient(jira_site).get_issue_summary(log.key)} ({log.key})"

if log.note:
description += f":\n\n{log.note}"
else:
jira_issue_url = None
description = log.note or "-"

doc = frappe.get_doc(
{
"doctype": "Timesheet",
"time_logs": [
{
"is_billable": 1,
"project": log.project,
"activity_type": "Default",
"base_billing_rate": billing_rate,
"base_costing_rate": costing_rate,
"costing_rate": costing_rate,
"billing_rate": billing_rate,
"hours": hours,
"from_time": self.date,
"billing_hours": billing_hours,
"description": description,
"jira_issue_url": jira_issue_url,
}
],
"parent_project": log.project,
"customer": customer,
"employee": self.employee,
}
)
customer, billing_rate, jira_site = frappe.get_value(
"Project",
log.project,
["customer", "billing_rate", "jira_site"],
)

doc.insert()
frappe.get_doc(
{
"doctype": "Timesheet",
"time_logs": [
{
"is_billable": 1,
"project": log.project,
"activity_type": "Default",
"base_billing_rate": billing_rate,
"base_costing_rate": costing_rate,
"costing_rate": costing_rate,
"billing_rate": billing_rate,
"hours": hours,
"from_time": self.date,
"billing_hours": billing_hours,
"description": get_description(
jira_site, log.key, log.note
),
"jira_issue_url": get_jira_issue_url(
jira_site, log.key
),
}
],
"parent_project": log.project,
"customer": customer,
"employee": self.employee,
}
).insert()


def get_costing_rate(employee):
return frappe.get_value(
"Activity Cost",
{"activity_type": "Default", "employee": employee},
"costing_rate",
)


def get_jira_issue_url(jira_site, key):
return f"https://{jira_site}/browse/{key}" if key else None


def get_description(jira_site, key, note):
if key:
description = f"{JiraClient(jira_site).get_issue_summary(key)} ({key})"

if note:
description += f":\n\n{note}"
else:
description = note or "-"

return description

0 comments on commit d811713

Please sign in to comment.