Skip to content

Commit

Permalink
feat: add option to enforce mandatory breaks (DRC-26)
Browse files Browse the repository at this point in the history
  • Loading branch information
scdanieli committed Sep 27, 2024
1 parent 1d5b0d8 commit 80a95b0
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ frappe.ui.form.on("Working Time", {
});

frappe.ui.form.on("Working Time Log", {
is_break: function (frm) {
frm.refresh_fields();
},
time_logs_add: function (frm, cdt, cdn) {
let current_row = locals[cdt][cdn];
let index = frm.doc.time_logs.findIndex((row) => row.name === current_row.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
"date",
"time_logs",
"section_break_7",
"indicated_break_time",
"mandatory_break_time",
"break_time",
"column_break_amiy",
"total_time",
"working_time",
"project_time",
"section_break_syac",
"amended_from"
],
"fields": [
Expand Down Expand Up @@ -60,6 +65,7 @@
"read_only": 1
},
{
"bold": 1,
"fieldname": "working_time",
"fieldtype": "Duration",
"hide_days": 1,
Expand All @@ -75,6 +81,7 @@
"options": "Working Time Log"
},
{
"bold": 1,
"fieldname": "break_time",
"fieldtype": "Duration",
"hide_days": 1,
Expand All @@ -89,6 +96,8 @@
{
"fieldname": "project_time",
"fieldtype": "Duration",
"hide_days": 1,
"hide_seconds": 1,
"label": "Project Time",
"read_only": 1
},
Expand All @@ -99,6 +108,38 @@
"label": "Department",
"options": "Department",
"read_only": 1
},
{
"fieldname": "column_break_amiy",
"fieldtype": "Column Break"
},
{
"fieldname": "section_break_syac",
"fieldtype": "Section Break"
},
{
"fieldname": "indicated_break_time",
"fieldtype": "Duration",
"hide_days": 1,
"hide_seconds": 1,
"label": "Indicated Break",
"read_only_depends_on": "eval:doc.time_logs.some(row => row.is_break === 1)"
},
{
"fieldname": "mandatory_break_time",
"fieldtype": "Duration",
"hide_days": 1,
"hide_seconds": 1,
"label": "Mandatory Break",
"read_only": 1
},
{
"fieldname": "total_time",
"fieldtype": "Duration",
"hide_days": 1,
"hide_seconds": 1,
"label": "Total Time",
"read_only": 1
}
],
"is_submittable": 1,
Expand All @@ -112,7 +153,7 @@
"link_fieldname": "working_time"
}
],
"modified": "2023-06-22 12:25:38.722346",
"modified": "2024-09-27 11:50:43.070779",
"modified_by": "Administrator",
"module": "Arbeitszeiterfassung S4A",
"name": "Working Time",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ def get_default_activity():

class WorkingTime(Document):
def before_validate(self):
self.break_time = self.working_time = self.project_time = 0
for log in self.time_logs:
log.set_duration()
duration = log.duration or 0
self.break_time += duration if log.is_break else 0
self.working_time += 0 if log.is_break else duration
self.project_time += duration if log.project and not log.is_break else 0
self.set_total_times()

def set_total_times(self):
(
self.total_time,
self.working_time,
self.project_time,
self.indicated_break_time,
self.mandatory_break_time,
self.break_time,
) = calculate_total_times(self.time_logs, self.indicated_break_time or 0)

def validate(self):
for log in self.time_logs:
Expand Down Expand Up @@ -111,3 +115,51 @@ def get_costing_rate(employee):
{"activity_type": get_default_activity(), "employee": employee},
"costing_rate",
)


def calculate_total_times(time_logs, user_indicated_break_time):
total_time = 0
total_working_time = 0
total_project_time = 0
total_indicated_break_time = 0

for log in time_logs:
log.set_duration()
duration = log.duration or 0

if log.is_break:
total_indicated_break_time += duration
else:
total_working_time += duration
if log.project:
total_project_time += duration

total_time += duration

mandatory_break_time = calcualte_mandatory_break_time(
total_working_time, total_indicated_break_time
)
actual_break_time = max(
mandatory_break_time, total_indicated_break_time or user_indicated_break_time
)
adjusted_working_time = total_time - actual_break_time

return (
total_time,
adjusted_working_time,
total_project_time,
total_indicated_break_time or user_indicated_break_time,
mandatory_break_time,
actual_break_time,
)


def calcualte_mandatory_break_time(working_time, break_time):
if not frappe.db.get_single_value("Working Time Settings", "enforce_mandatory_breaks"):
return 0
elif working_time + break_time > 9.75 * ONE_HOUR or working_time > 9 * ONE_HOUR:
return 45 * 60
elif working_time + break_time > 6.5 * ONE_HOUR or working_time > 6 * ONE_HOUR:
return 30 * 60
else:
return 0
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"default_activity"
"default_activity",
"enforce_mandatory_breaks"
],
"fields": [
{
Expand All @@ -17,12 +18,18 @@
"label": "Default Activity",
"options": "Activity Type",
"reqd": 1
},
{
"default": "0",
"fieldname": "enforce_mandatory_breaks",
"fieldtype": "Check",
"label": "Enforce Mandatory Breaks"
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2023-08-02 17:24:48.248467",
"modified": "2024-09-27 10:14:36.062308",
"modified_by": "Administrator",
"module": "Arbeitszeiterfassung S4A",
"name": "Working Time Settings",
Expand Down
9 changes: 9 additions & 0 deletions arbeitszeiterfassung_s4a/translations/de.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Working Time Settings,Arbeitszeiteinstellungen,
Default Activity,Standardtätigkeit,
Enforce Mandatory Breaks,Verpflichtende Pausen erzwingen,
Break,Pause,
Indicated Break,Angegebene Pause,
Mandatory Break,Verpflichtende Pause,
Total Time,Gesamtzeit,
Working Time,Arbeitszeit,
Project Time,Projektzeit,

0 comments on commit 80a95b0

Please sign in to comment.