Shreya | 5bf24be | 2018-03-21 22:20:46 +0530 | [diff] [blame] | 1 | frappe.provide("erpnext.timesheet"); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 2 | |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 3 | erpnext.timesheet.timer = function(frm, row, timestamp=0) { |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 4 | let dialog = new frappe.ui.Dialog({ |
| 5 | title: __("Timer"), |
| 6 | fields: |
| 7 | [ |
| 8 | {"fieldtype": "Link", "label": __("Activity Type"), "fieldname": "activity_type", |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 9 | "reqd": 1, "options": "Activity Type"}, |
| 10 | {"fieldtype": "Link", "label": __("Project"), "fieldname": "project", "options": "Project"}, |
Shreya | 8bb0f05 | 2018-03-23 11:46:45 +0530 | [diff] [blame] | 11 | {"fieldtype": "Link", "label": __("Task"), "fieldname": "task", "options": "Task"}, |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 12 | {"fieldtype": "Float", "label": __("Expected Hrs"), "fieldname": "expected_hours"}, |
| 13 | {"fieldtype": "Section Break"}, |
| 14 | {"fieldtype": "HTML", "fieldname": "timer_html"} |
| 15 | ] |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 16 | }); |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 17 | |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 18 | if (row) { |
| 19 | dialog.set_values({ |
| 20 | 'activity_type': row.activity_type, |
| 21 | 'project': row.project, |
Shreya | 8bb0f05 | 2018-03-23 11:46:45 +0530 | [diff] [blame] | 22 | 'task': row.task, |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 23 | 'expected_hours': row.expected_hours |
| 24 | }); |
| 25 | } |
Shreya | 5590114 | 2018-03-22 11:40:38 +0530 | [diff] [blame] | 26 | dialog.get_field("timer_html").$wrapper.append(get_timer_html()); |
| 27 | function get_timer_html() { |
| 28 | return ` |
| 29 | <div class="stopwatch"> |
| 30 | <span class="hours">00</span> |
| 31 | <span class="colon">:</span> |
| 32 | <span class="minutes">00</span> |
| 33 | <span class="colon">:</span> |
| 34 | <span class="seconds">00</span> |
| 35 | </div> |
| 36 | <div class="playpause text-center"> |
| 37 | <button class= "btn btn-primary btn-start"> ${ __("Start") } </button> |
| 38 | <button class= "btn btn-primary btn-complete"> ${ __("Complete") } </button> |
| 39 | </div> |
| 40 | `; |
Shreya | 822ba21 | 2018-03-22 11:56:05 +0530 | [diff] [blame] | 41 | } |
Shreya | 5590114 | 2018-03-22 11:40:38 +0530 | [diff] [blame] | 42 | erpnext.timesheet.control_timer(frm, dialog, row, timestamp); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 43 | dialog.show(); |
Shreya | 5bf24be | 2018-03-21 22:20:46 +0530 | [diff] [blame] | 44 | }; |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 45 | |
Shreya | 5590114 | 2018-03-22 11:40:38 +0530 | [diff] [blame] | 46 | erpnext.timesheet.control_timer = function(frm, dialog, row, timestamp=0) { |
shreyashah115@gmail.com | 5281fe8 | 2018-11-27 13:11:30 +0530 | [diff] [blame] | 47 | var $btn_start = dialog.$wrapper.find(".playpause .btn-start"); |
| 48 | var $btn_complete = dialog.$wrapper.find(".playpause .btn-complete"); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 49 | var interval = null; |
Shreya | 5bf24be | 2018-03-21 22:20:46 +0530 | [diff] [blame] | 50 | var currentIncrement = timestamp; |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 51 | var initialised = row ? true : false; |
| 52 | var clicked = false; |
Shreya Shah | 5d71609 | 2018-04-02 10:32:39 +0530 | [diff] [blame] | 53 | var flag = true; // Alert only once |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 54 | // If row with not completed status, initialize timer with the time elapsed on click of 'Start Timer'. |
| 55 | if (row) { |
| 56 | initialised = true; |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 57 | $btn_start.hide(); |
| 58 | $btn_complete.show(); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 59 | initialiseTimer(); |
| 60 | } |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 61 | if (!initialised) { |
| 62 | $btn_complete.hide(); |
| 63 | } |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 64 | $btn_start.click(function(e) { |
| 65 | if (!initialised) { |
| 66 | // New activity if no activities found |
| 67 | var args = dialog.get_values(); |
| 68 | if(!args) return; |
Shreya Shah | 5d71609 | 2018-04-02 10:32:39 +0530 | [diff] [blame] | 69 | if (frm.doc.time_logs.length <= 1 && !frm.doc.time_logs[0].activity_type && !frm.doc.time_logs[0].from_time) { |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 70 | frm.doc.time_logs = []; |
| 71 | } |
| 72 | row = frappe.model.add_child(frm.doc, "Timesheet Detail", "time_logs"); |
| 73 | row.activity_type = args.activity_type; |
| 74 | row.from_time = frappe.datetime.get_datetime_as_string(); |
Shreya | 8bb0f05 | 2018-03-23 11:46:45 +0530 | [diff] [blame] | 75 | row.project = args.project; |
| 76 | row.task = args.task; |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 77 | row.expected_hours = args.expected_hours; |
| 78 | row.completed = 0; |
Shreya | 5bf24be | 2018-03-21 22:20:46 +0530 | [diff] [blame] | 79 | let d = moment(row.from_time); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 80 | if(row.expected_hours) { |
| 81 | d.add(row.expected_hours, "hours"); |
Rushabh Mehta | 096b943 | 2018-06-25 22:37:43 +0530 | [diff] [blame] | 82 | row.to_time = d.format(frappe.defaultDatetimeFormat); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 83 | } |
| 84 | frm.refresh_field("time_logs"); |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 85 | frm.save(); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 86 | } |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 87 | |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 88 | if (clicked) { |
| 89 | e.preventDefault(); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (!initialised) { |
| 94 | initialised = true; |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 95 | $btn_start.hide(); |
| 96 | $btn_complete.show(); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 97 | initialiseTimer(); |
| 98 | } |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 99 | }); |
| 100 | |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 101 | // Stop the timer and update the time logged by the timer on click of 'Complete' button |
| 102 | $btn_complete.click(function() { |
Shreya | 822ba21 | 2018-03-22 11:56:05 +0530 | [diff] [blame] | 103 | var grid_row = cur_frm.fields_dict['time_logs'].grid.get_row(row.idx - 1); |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 104 | var args = dialog.get_values(); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 105 | grid_row.doc.completed = 1; |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 106 | grid_row.doc.activity_type = args.activity_type; |
| 107 | grid_row.doc.project = args.project; |
Shreya | 8bb0f05 | 2018-03-23 11:46:45 +0530 | [diff] [blame] | 108 | grid_row.doc.task = args.task; |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 109 | grid_row.doc.expected_hours = args.expected_hours; |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 110 | grid_row.doc.hours = currentIncrement / 3600; |
| 111 | grid_row.doc.to_time = frappe.datetime.now_datetime(); |
| 112 | grid_row.refresh(); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 113 | frm.save(); |
| 114 | reset(); |
| 115 | dialog.hide(); |
Shreya | 5bf24be | 2018-03-21 22:20:46 +0530 | [diff] [blame] | 116 | }); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 117 | function initialiseTimer() { |
| 118 | interval = setInterval(function() { |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 119 | var current = setCurrentIncrement(); |
| 120 | updateStopwatch(current); |
| 121 | }, 1000); |
| 122 | } |
| 123 | |
| 124 | function updateStopwatch(increment) { |
| 125 | var hours = Math.floor(increment / 3600); |
| 126 | var minutes = Math.floor((increment - (hours * 3600)) / 60); |
| 127 | var seconds = increment - (hours * 3600) - (minutes * 60); |
| 128 | |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 129 | // If modal is closed by clicking anywhere outside, reset the timer |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 130 | if (!$('.modal-dialog').is(':visible')) { |
| 131 | reset(); |
| 132 | } |
Shreya Shah | 5d71609 | 2018-04-02 10:32:39 +0530 | [diff] [blame] | 133 | if(hours > 99999) |
Shreya | 5bf24be | 2018-03-21 22:20:46 +0530 | [diff] [blame] | 134 | reset(); |
Shreya Shah | 5d71609 | 2018-04-02 10:32:39 +0530 | [diff] [blame] | 135 | if(cur_dialog && cur_dialog.get_value('expected_hours') > 0) { |
| 136 | if(flag && (currentIncrement >= (cur_dialog.get_value('expected_hours') * 3600))) { |
| 137 | frappe.utils.play_sound("alert"); |
| 138 | frappe.msgprint(__("Timer exceeded the given hours.")); |
| 139 | flag = false; |
| 140 | } |
| 141 | } |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 142 | $(".hours").text(hours < 10 ? ("0" + hours.toString()) : hours.toString()); |
| 143 | $(".minutes").text(minutes < 10 ? ("0" + minutes.toString()) : minutes.toString()); |
| 144 | $(".seconds").text(seconds < 10 ? ("0" + seconds.toString()) : seconds.toString()); |
| 145 | } |
| 146 | |
| 147 | function setCurrentIncrement() { |
| 148 | currentIncrement += 1; |
| 149 | return currentIncrement; |
| 150 | } |
| 151 | |
| 152 | function reset() { |
| 153 | currentIncrement = 0; |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 154 | initialised = false; |
| 155 | clearInterval(interval); |
| 156 | $(".hours").text("00"); |
| 157 | $(".minutes").text("00"); |
| 158 | $(".seconds").text("00"); |
Shreya | 8f06266 | 2018-03-21 22:20:11 +0530 | [diff] [blame] | 159 | $btn_complete.hide(); |
| 160 | $btn_start.show(); |
Shreya | 68ec22a | 2018-03-21 19:25:56 +0530 | [diff] [blame] | 161 | } |
Shreya | 5bf24be | 2018-03-21 22:20:46 +0530 | [diff] [blame] | 162 | }; |