|
| 1 | +/** @odoo-module **/ |
| 2 | + |
| 3 | +import { Component, useState } from "@odoo/owl"; |
| 4 | +import { useService } from "@web/core/utils/hooks"; |
| 5 | +import { registry } from "@web/core/registry"; |
| 6 | +import { scanBarcode } from "@web/core/barcode/barcode_dialog"; |
| 7 | +import { rpc } from "@web/core/network/rpc"; |
| 8 | + |
| 9 | +export class KioskMainScreen extends Component { |
| 10 | + static template = "time_tracking_kiosk.KioskMainScreen"; |
| 11 | + |
| 12 | + setup() { |
| 13 | + this.state = useState({ |
| 14 | + employeeId: "", |
| 15 | + employee: null, |
| 16 | + currentProject: null, |
| 17 | + currentTask: null, |
| 18 | + isTimerRunning: false, |
| 19 | + timerStartTime: null, |
| 20 | + timesheetId: null, |
| 21 | + screen: "scan", |
| 22 | + elapsedTime: "00:00:00", |
| 23 | + timerInterval: null, |
| 24 | + }); |
| 25 | + this.orm = useService("orm"); |
| 26 | + this.notification = useService("notification"); |
| 27 | + this.scanBarcode = () => scanBarcode(this.env, this.facingMode); |
| 28 | + } |
| 29 | + |
| 30 | + async openMobileScanner() { |
| 31 | + let error = null; |
| 32 | + let barcode = null; |
| 33 | + try { |
| 34 | + barcode = await this.scanBarcode(); |
| 35 | + } catch (err) { |
| 36 | + error = err.message; |
| 37 | + } |
| 38 | + |
| 39 | + if (barcode) { |
| 40 | + this.props.onBarcodeScanned(barcode); |
| 41 | + if ("vibrate" in window.navigator) { |
| 42 | + window.navigator.vibrate(100); |
| 43 | + } |
| 44 | + } else { |
| 45 | + this.notification.add(error || _t("Please, Scan again!"), { |
| 46 | + type: "warning", |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + async onScanBadge() { |
| 52 | + if (!this.state.employeeId) { |
| 53 | + this.notification.add("Please scan or enter an employee ID", { |
| 54 | + type: "warning", |
| 55 | + }); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + const employees = await this.orm.call( |
| 61 | + "hr.employee", |
| 62 | + "search_read", |
| 63 | + [[["barcode", "=", this.state.employeeId]]], |
| 64 | + { limit: 1 } |
| 65 | + ); |
| 66 | + if (employees.length) { |
| 67 | + const employee = employees[0]; |
| 68 | + const tasks = await this.orm.call( |
| 69 | + "project.task", |
| 70 | + "search_read", |
| 71 | + [[["user_ids", "in", [employee.user_id[0]]]]], |
| 72 | + { |
| 73 | + fields: ["id", "name", "project_id", "effective_hours", "stage_id"], |
| 74 | + } |
| 75 | + ); |
| 76 | + if (tasks.length == 0) { |
| 77 | + this.notification.add( |
| 78 | + `Employee ${employee.name} has no tasks currently assigned. Please assign tasks to enable time tracking.`, |
| 79 | + { |
| 80 | + type: "warning", |
| 81 | + } |
| 82 | + ); |
| 83 | + this.state.screen = "scan"; |
| 84 | + return; |
| 85 | + } |
| 86 | + const stages = await this.orm.call( |
| 87 | + "project.task.type", |
| 88 | + "search_read", |
| 89 | + [[["id", "in", tasks.map((task) => task.stage_id[0])]]], |
| 90 | + { fields: ["id", "fold"] } |
| 91 | + ); |
| 92 | + |
| 93 | + const stageMap = stages.reduce((map, stage) => { |
| 94 | + map[stage.id] = stage.fold; |
| 95 | + return map; |
| 96 | + }, {}); |
| 97 | + |
| 98 | + const projectsMap = {}; |
| 99 | + |
| 100 | + tasks.forEach((task) => { |
| 101 | + if (stageMap[task.stage_id[0]]) return; |
| 102 | + const projectId = task.project_id[0]; |
| 103 | + const projectName = task.project_id[1]; |
| 104 | + if (!projectsMap[projectId]) { |
| 105 | + projectsMap[projectId] = { |
| 106 | + id: projectId, |
| 107 | + name: projectName, |
| 108 | + tasks: [], |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + projectsMap[projectId].tasks.push({ |
| 113 | + id: task.id, |
| 114 | + name: task.name, |
| 115 | + effective_hours: task.effective_hours, |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + this.state.employee = { |
| 120 | + ...employee, |
| 121 | + assigned_projects: Object.values(projectsMap), |
| 122 | + }; |
| 123 | + |
| 124 | + const activeTimesheet = await this.orm.call( |
| 125 | + "account.analytic.line", |
| 126 | + "search_read", |
| 127 | + [ |
| 128 | + [ |
| 129 | + ["employee_id", "=", employee.id], |
| 130 | + ["timer_active", "=", true], |
| 131 | + ], |
| 132 | + ], |
| 133 | + { |
| 134 | + limit: 1, |
| 135 | + fields: ["id", "timer_start_time", "project_id", "task_id"], |
| 136 | + } |
| 137 | + ); |
| 138 | + |
| 139 | + if (activeTimesheet.length) { |
| 140 | + this.state.isTimerRunning = true; |
| 141 | + this.state.timerStartTime = new Date( |
| 142 | + activeTimesheet[0].timer_start_time |
| 143 | + ); |
| 144 | + |
| 145 | + this.state.timerStartTime = new Date( |
| 146 | + this.state.timerStartTime.getTime() - |
| 147 | + this.state.timerStartTime.getTimezoneOffset() * 60000 |
| 148 | + ); |
| 149 | + this.state.timesheetId = activeTimesheet[0].id; |
| 150 | + |
| 151 | + if (activeTimesheet[0].project_id && activeTimesheet[0].task_id) { |
| 152 | + const projectId = activeTimesheet[0].project_id[0]; |
| 153 | + const projectName = activeTimesheet[0].project_id[1]; |
| 154 | + |
| 155 | + this.state.currentProject = |
| 156 | + this.state.employee.assigned_projects.find( |
| 157 | + (proj) => proj.id === projectId |
| 158 | + ); |
| 159 | + |
| 160 | + if (!this.state.currentProject) { |
| 161 | + this.state.currentProject = { |
| 162 | + id: projectId, |
| 163 | + name: projectName, |
| 164 | + tasks: [], |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + const taskId = activeTimesheet[0].task_id[0]; |
| 169 | + const taskName = activeTimesheet[0].task_id[1]; |
| 170 | + |
| 171 | + this.state.currentTask = this.state.currentProject.tasks.find( |
| 172 | + (task) => task.id === taskId |
| 173 | + ); |
| 174 | + |
| 175 | + if (!this.state.currentTask) { |
| 176 | + this.state.currentTask = { |
| 177 | + id: taskId, |
| 178 | + name: taskName, |
| 179 | + }; |
| 180 | + |
| 181 | + if ( |
| 182 | + !this.state.currentProject.tasks.some( |
| 183 | + (task) => task.id === taskId |
| 184 | + ) |
| 185 | + ) { |
| 186 | + this.state.currentProject.tasks.push(this.state.currentTask); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + this.state.screen = "timer"; |
| 192 | + this.startElapsedTimeUpdater(); |
| 193 | + } else { |
| 194 | + this.state.screen = "projects"; |
| 195 | + } |
| 196 | + |
| 197 | + this.notification.add(`Employee Found: ${employee.name}`, { |
| 198 | + type: "success", |
| 199 | + }); |
| 200 | + } else { |
| 201 | + this.notification.add("Employee not found!", { type: "danger" }); |
| 202 | + } |
| 203 | + } catch (error) { |
| 204 | + this.notification.add("An error occurred while fetching employee data.", { |
| 205 | + type: "danger", |
| 206 | + }); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + onProjectChange(event) { |
| 211 | + const projectId = Number(event.target.value); |
| 212 | + |
| 213 | + this.state.currentProject = |
| 214 | + this.state.employee.assigned_projects.find( |
| 215 | + (proj) => proj.id === projectId |
| 216 | + ) || null; |
| 217 | + |
| 218 | + this.state.currentTask = null; |
| 219 | + } |
| 220 | + |
| 221 | + onTaskChange(event) { |
| 222 | + const taskId = Number(event.target.value); |
| 223 | + |
| 224 | + this.state.currentTask = |
| 225 | + this.state.currentProject?.tasks.find((task) => task.id === taskId) || |
| 226 | + null; |
| 227 | + } |
| 228 | + |
| 229 | + backToMainScreen() { |
| 230 | + if (this.state.timerInterval) { |
| 231 | + clearInterval(this.state.timerInterval); |
| 232 | + this.state.timerInterval = null; |
| 233 | + } |
| 234 | + |
| 235 | + this.state.employeeId = ""; |
| 236 | + this.state.employee = null; |
| 237 | + this.state.currentProject = null; |
| 238 | + this.state.currentTask = null; |
| 239 | + this.state.timerStartTime = null; |
| 240 | + this.state.screen = "scan"; |
| 241 | + } |
| 242 | + |
| 243 | + async startTimer() { |
| 244 | + const { currentProject, currentTask, employee } = this.state; |
| 245 | + |
| 246 | + if (!currentProject || !currentTask) { |
| 247 | + this.notification.add("Please select a project and task first!", { |
| 248 | + type: "warning", |
| 249 | + }); |
| 250 | + return; |
| 251 | + } |
| 252 | + |
| 253 | + try { |
| 254 | + const timesheetData = await rpc("/timesheet/create", { |
| 255 | + project_id: currentProject.id, |
| 256 | + task_id: currentTask.id, |
| 257 | + employee_id: employee.id, |
| 258 | + }); |
| 259 | + |
| 260 | + if (timesheetData.id) { |
| 261 | + this.state.isTimerRunning = true; |
| 262 | + this.state.timerStartTime = new Date(); |
| 263 | + this.state.timesheetId = timesheetData.id; |
| 264 | + this.state.screen = "timer"; |
| 265 | + this.startElapsedTimeUpdater(); |
| 266 | + this.notification.add("Timer started successfully!", { type: "success" }); |
| 267 | + } else { |
| 268 | + this.notification.add("Failed to start timer.", { type: "danger" }); |
| 269 | + } |
| 270 | + } catch (error) { |
| 271 | + this.notification.add("Error starting timer.", { type: "danger" }); |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | + async stopTimer() { |
| 276 | + if (!this.state.isTimerRunning || !this.state.timesheetId) { |
| 277 | + this.notification.add("No active timer found.", { type: "warning" }); |
| 278 | + return; |
| 279 | + } |
| 280 | + |
| 281 | + try { |
| 282 | + const response = await rpc("/timesheet/stop", { |
| 283 | + timesheet_id: this.state.timesheetId, |
| 284 | + }); |
| 285 | + |
| 286 | + if (response.error) { |
| 287 | + this.notification.add(response.error, { type: "danger" }); |
| 288 | + return; |
| 289 | + } |
| 290 | + |
| 291 | + if (response.id) { |
| 292 | + this.notification.add( |
| 293 | + `Timesheet stopped. Worked ${ |
| 294 | + Math.round(response.unit_amount * 60) / 100 |
| 295 | + } hours`, |
| 296 | + { type: "success" } |
| 297 | + ); |
| 298 | + } else { |
| 299 | + this.notification.add("Failed to stop timer.", { type: "danger" }); |
| 300 | + } |
| 301 | + } catch (error) { |
| 302 | + this.notification.add("Error stopping timer.", { type: "danger" }); |
| 303 | + } |
| 304 | + |
| 305 | + if (this.state.timerInterval) { |
| 306 | + clearInterval(this.state.timerInterval); |
| 307 | + this.state.timerInterval = null; |
| 308 | + } |
| 309 | + |
| 310 | + this.state.isTimerRunning = false; |
| 311 | + this.state.timerStartTime = null; |
| 312 | + this.state.timesheetId = null; |
| 313 | + this.state.screen = "scan"; |
| 314 | + } |
| 315 | + |
| 316 | + startElapsedTimeUpdater() { |
| 317 | + if (this.state.timerInterval) { |
| 318 | + clearInterval(this.state.timerInterval); |
| 319 | + } |
| 320 | + |
| 321 | + this.updateElapsedTime(); |
| 322 | + |
| 323 | + this.state.timerInterval = setInterval(() => { |
| 324 | + this.updateElapsedTime(); |
| 325 | + }, 1000); |
| 326 | + } |
| 327 | + |
| 328 | + updateElapsedTime() { |
| 329 | + if (!this.state.timerStartTime) return; |
| 330 | + |
| 331 | + const now = new Date(); |
| 332 | + const elapsedMilliseconds = now - this.state.timerStartTime; |
| 333 | + const totalSeconds = Math.floor(elapsedMilliseconds / 1000); |
| 334 | + const hours = Math.floor(totalSeconds / 3600); |
| 335 | + const minutes = Math.floor((totalSeconds % 3600) / 60); |
| 336 | + const seconds = totalSeconds % 60; |
| 337 | + |
| 338 | + this.state.elapsedTime = `${hours.toString().padStart(2, "0")}:${minutes |
| 339 | + .toString() |
| 340 | + .padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`; |
| 341 | + } |
| 342 | + |
| 343 | + formatTime(date) { |
| 344 | + if (!date) return "00:00:00"; |
| 345 | + |
| 346 | + const hours = date.getHours().toString().padStart(2, "0"); |
| 347 | + const minutes = date.getMinutes().toString().padStart(2, "0"); |
| 348 | + const seconds = date.getSeconds().toString().padStart(2, "0"); |
| 349 | + |
| 350 | + return `${hours}:${minutes}:${seconds}`; |
| 351 | + } |
| 352 | +} |
| 353 | + |
| 354 | +registry.category("actions").add("time_tracking_kiosk_main", KioskMainScreen); |
0 commit comments