blob: 8370cc6ffffb088a6eba9078b0c97b0a88e3d7ec [file] [log] [blame]
Shreya5bf24be2018-03-21 22:20:46 +05301frappe.provide("erpnext.timesheet");
Shreya68ec22a2018-03-21 19:25:56 +05302
Ankush Menatec74a5e2024-03-10 19:45:40 +05303erpnext.timesheet.timer = function (frm, row, timestamp = 0) {
Shreya68ec22a2018-03-21 19:25:56 +05304 let dialog = new frappe.ui.Dialog({
5 title: __("Timer"),
Ankush Menatec74a5e2024-03-10 19:45:40 +05306 fields: [
7 {
8 fieldtype: "Link",
9 label: __("Activity Type"),
10 fieldname: "activity_type",
11 reqd: 1,
12 options: "Activity Type",
13 },
14 { fieldtype: "Link", label: __("Project"), fieldname: "project", options: "Project" },
15 { fieldtype: "Link", label: __("Task"), fieldname: "task", options: "Task" },
16 { fieldtype: "Float", label: __("Expected Hrs"), fieldname: "expected_hours" },
17 { fieldtype: "Section Break" },
18 { fieldtype: "HTML", fieldname: "timer_html" },
19 ],
Shreya68ec22a2018-03-21 19:25:56 +053020 });
Shreya8f062662018-03-21 22:20:11 +053021
Shreya68ec22a2018-03-21 19:25:56 +053022 if (row) {
23 dialog.set_values({
Ankush Menatec74a5e2024-03-10 19:45:40 +053024 activity_type: row.activity_type,
25 project: row.project,
26 task: row.task,
27 expected_hours: row.expected_hours,
Shreya68ec22a2018-03-21 19:25:56 +053028 });
29 }
Shreya55901142018-03-22 11:40:38 +053030 dialog.get_field("timer_html").$wrapper.append(get_timer_html());
31 function get_timer_html() {
32 return `
33 <div class="stopwatch">
34 <span class="hours">00</span>
35 <span class="colon">:</span>
36 <span class="minutes">00</span>
37 <span class="colon">:</span>
38 <span class="seconds">00</span>
39 </div>
40 <div class="playpause text-center">
Ankush Menatec74a5e2024-03-10 19:45:40 +053041 <button class= "btn btn-primary btn-start"> ${__("Start")} </button>
42 <button class= "btn btn-primary btn-complete"> ${__("Complete")} </button>
Shreya55901142018-03-22 11:40:38 +053043 </div>
44 `;
Shreya822ba212018-03-22 11:56:05 +053045 }
Shreya55901142018-03-22 11:40:38 +053046 erpnext.timesheet.control_timer(frm, dialog, row, timestamp);
Shreya68ec22a2018-03-21 19:25:56 +053047 dialog.show();
Shreya5bf24be2018-03-21 22:20:46 +053048};
Shreya68ec22a2018-03-21 19:25:56 +053049
Ankush Menatec74a5e2024-03-10 19:45:40 +053050erpnext.timesheet.control_timer = function (frm, dialog, row, timestamp = 0) {
shreyashah115@gmail.com5281fe82018-11-27 13:11:30 +053051 var $btn_start = dialog.$wrapper.find(".playpause .btn-start");
52 var $btn_complete = dialog.$wrapper.find(".playpause .btn-complete");
Shreya68ec22a2018-03-21 19:25:56 +053053 var interval = null;
Shreya5bf24be2018-03-21 22:20:46 +053054 var currentIncrement = timestamp;
Deepesh Garg64802d12022-11-20 19:45:51 +053055 var initialized = row ? true : false;
Shreya68ec22a2018-03-21 19:25:56 +053056 var clicked = false;
Shreya Shah5d716092018-04-02 10:32:39 +053057 var flag = true; // Alert only once
Shreya68ec22a2018-03-21 19:25:56 +053058 // If row with not completed status, initialize timer with the time elapsed on click of 'Start Timer'.
59 if (row) {
Deepesh Garg64802d12022-11-20 19:45:51 +053060 initialized = true;
Shreya8f062662018-03-21 22:20:11 +053061 $btn_start.hide();
62 $btn_complete.show();
Deepesh Garg64802d12022-11-20 19:45:51 +053063 initializeTimer();
Shreya68ec22a2018-03-21 19:25:56 +053064 }
Deepesh Garg64802d12022-11-20 19:45:51 +053065
66 if (!initialized) {
Shreya8f062662018-03-21 22:20:11 +053067 $btn_complete.hide();
68 }
Deepesh Garg64802d12022-11-20 19:45:51 +053069
Ankush Menatec74a5e2024-03-10 19:45:40 +053070 $btn_start.click(function (e) {
Deepesh Garg64802d12022-11-20 19:45:51 +053071 if (!initialized) {
Shreya68ec22a2018-03-21 19:25:56 +053072 // New activity if no activities found
73 var args = dialog.get_values();
Ankush Menatec74a5e2024-03-10 19:45:40 +053074 if (!args) return;
75 if (
76 frm.doc.time_logs.length == 1 &&
77 !frm.doc.time_logs[0].activity_type &&
78 !frm.doc.time_logs[0].from_time
79 ) {
Shreya68ec22a2018-03-21 19:25:56 +053080 frm.doc.time_logs = [];
81 }
82 row = frappe.model.add_child(frm.doc, "Timesheet Detail", "time_logs");
83 row.activity_type = args.activity_type;
84 row.from_time = frappe.datetime.get_datetime_as_string();
Shreya8bb0f052018-03-23 11:46:45 +053085 row.project = args.project;
86 row.task = args.task;
Shreya68ec22a2018-03-21 19:25:56 +053087 row.expected_hours = args.expected_hours;
88 row.completed = 0;
Shreya5bf24be2018-03-21 22:20:46 +053089 let d = moment(row.from_time);
Ankush Menatec74a5e2024-03-10 19:45:40 +053090 if (row.expected_hours) {
Shreya68ec22a2018-03-21 19:25:56 +053091 d.add(row.expected_hours, "hours");
Rushabh Mehta096b9432018-06-25 22:37:43 +053092 row.to_time = d.format(frappe.defaultDatetimeFormat);
Shreya68ec22a2018-03-21 19:25:56 +053093 }
94 frm.refresh_field("time_logs");
Shreya8f062662018-03-21 22:20:11 +053095 frm.save();
Shreya68ec22a2018-03-21 19:25:56 +053096 }
Shreya8f062662018-03-21 22:20:11 +053097
Shreya68ec22a2018-03-21 19:25:56 +053098 if (clicked) {
99 e.preventDefault();
100 return false;
101 }
102
Deepesh Garg64802d12022-11-20 19:45:51 +0530103 if (!initialized) {
104 initialized = true;
Shreya8f062662018-03-21 22:20:11 +0530105 $btn_start.hide();
106 $btn_complete.show();
Deepesh Garg64802d12022-11-20 19:45:51 +0530107 initializeTimer();
Shreya68ec22a2018-03-21 19:25:56 +0530108 }
Shreya68ec22a2018-03-21 19:25:56 +0530109 });
110
Shreya8f062662018-03-21 22:20:11 +0530111 // Stop the timer and update the time logged by the timer on click of 'Complete' button
Ankush Menatec74a5e2024-03-10 19:45:40 +0530112 $btn_complete.click(function () {
113 var grid_row = cur_frm.fields_dict["time_logs"].grid.get_row(row.idx - 1);
Shreya8f062662018-03-21 22:20:11 +0530114 var args = dialog.get_values();
Shreya68ec22a2018-03-21 19:25:56 +0530115 grid_row.doc.completed = 1;
Shreya8f062662018-03-21 22:20:11 +0530116 grid_row.doc.activity_type = args.activity_type;
117 grid_row.doc.project = args.project;
Shreya8bb0f052018-03-23 11:46:45 +0530118 grid_row.doc.task = args.task;
Shreya8f062662018-03-21 22:20:11 +0530119 grid_row.doc.expected_hours = args.expected_hours;
Shreya68ec22a2018-03-21 19:25:56 +0530120 grid_row.doc.hours = currentIncrement / 3600;
121 grid_row.doc.to_time = frappe.datetime.now_datetime();
122 grid_row.refresh();
Deepesh Garg64802d12022-11-20 19:45:51 +0530123 frm.dirty();
Shreya68ec22a2018-03-21 19:25:56 +0530124 frm.save();
125 reset();
126 dialog.hide();
Shreya5bf24be2018-03-21 22:20:46 +0530127 });
Deepesh Garg64802d12022-11-20 19:45:51 +0530128
129 function initializeTimer() {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530130 interval = setInterval(function () {
Shreya68ec22a2018-03-21 19:25:56 +0530131 var current = setCurrentIncrement();
132 updateStopwatch(current);
133 }, 1000);
134 }
135
136 function updateStopwatch(increment) {
137 var hours = Math.floor(increment / 3600);
Ankush Menatec74a5e2024-03-10 19:45:40 +0530138 var minutes = Math.floor((increment - hours * 3600) / 60);
139 var seconds = increment - hours * 3600 - minutes * 60;
Shreya68ec22a2018-03-21 19:25:56 +0530140
Shreya8f062662018-03-21 22:20:11 +0530141 // If modal is closed by clicking anywhere outside, reset the timer
Ankush Menatec74a5e2024-03-10 19:45:40 +0530142 if (!$(".modal-dialog").is(":visible")) {
Shreya68ec22a2018-03-21 19:25:56 +0530143 reset();
144 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530145 if (hours > 99999) reset();
146 if (cur_dialog && cur_dialog.get_value("expected_hours") > 0) {
147 if (flag && currentIncrement >= cur_dialog.get_value("expected_hours") * 3600) {
Shreya Shah5d716092018-04-02 10:32:39 +0530148 frappe.utils.play_sound("alert");
149 frappe.msgprint(__("Timer exceeded the given hours."));
150 flag = false;
151 }
152 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530153 $(".hours").text(hours < 10 ? "0" + hours.toString() : hours.toString());
154 $(".minutes").text(minutes < 10 ? "0" + minutes.toString() : minutes.toString());
155 $(".seconds").text(seconds < 10 ? "0" + seconds.toString() : seconds.toString());
Shreya68ec22a2018-03-21 19:25:56 +0530156 }
157
158 function setCurrentIncrement() {
159 currentIncrement += 1;
160 return currentIncrement;
161 }
162
163 function reset() {
164 currentIncrement = 0;
Deepesh Garg64802d12022-11-20 19:45:51 +0530165 initialized = false;
Shreya68ec22a2018-03-21 19:25:56 +0530166 clearInterval(interval);
167 $(".hours").text("00");
168 $(".minutes").text("00");
169 $(".seconds").text("00");
Shreya8f062662018-03-21 22:20:11 +0530170 $btn_complete.hide();
171 $btn_start.show();
Shreya68ec22a2018-03-21 19:25:56 +0530172 }
Ankush Menat4551d7d2021-08-19 13:41:10 +0530173};