Debian Codegraph @

addons/hr_attendance/static/src/public_kiosk/public_kiosk_app.js

raw symbols SCIP
1 import { App, whenReady, Component, useState } from "@odoo/owl";
2 import { CardLayout } from "@hr_attendance/components/card_layout/card_layout";
3 import { KioskManualSelection } from "@hr_attendance/components/manual_selection/manual_selection";
4 import { makeEnv, startServices } from "@web/env";
5 import { getTemplate } from "@web/core/templates";
6 import { _t, appTranslateFn } from "@web/core/l10n/translation";
7 import { MainComponentsContainer } from "@web/core/main_components_container";
8 import { rpc } from "@web/core/network/rpc";
9 import { useService, useBus } from "@web/core/utils/hooks";
10 import { url } from "@web/core/utils/urls";
11 import { KioskGreetings } from "@hr_attendance/components/greetings/greetings";
12 import { KioskPinCode } from "@hr_attendance/components/pin_code/pin_code";
13 import { KioskBarcodeScanner } from "@hr_attendance/components/kiosk_barcode/kiosk_barcode";
14 import { browser } from "@web/core/browser/browser";
15 import { isIosApp } from "@web/core/browser/feature_detection";
16 import { DocumentationLink } from "@web/views/widgets/documentation_link/documentation_link";
17 import { NewEmployeeDialog } from "@hr_attendance/components/new_employee_dialog/new_employee_dialog";
18 import { session } from "@web/session";
19
20 class kioskAttendanceApp extends Component{
21 static template = "hr_attendance.public_kiosk_app";
22 static props = {
23 token: { type: String },
24 companyId: { type: Number },
25 companyName: { type: String },
26 departments: { type: Array },
27 kioskMode: { type: String },
28 barcodeSource: { type: String },
29 fromTrialMode: { type: Boolean },
30 deviceTrackingEnabled: { type: Boolean },
31 };
32 static components = {
33 KioskBarcodeScanner,
34 CardLayout,
35 KioskManualSelection,
36 KioskGreetings,
37 KioskPinCode,
38 MainComponentsContainer,
39 DocumentationLink,
40 };
41
42 setup() {
43 this.dialogService = useService("dialog");
44 this.barcode = useService("barcode");
45 this.notification = useService("notification");
46 this.ui = useService("ui");
47 this.companyImageUrl = url("/web/binary/company_logo", {
48 company: this.props.companyId,
49 });
50 this.state = useState({
51 active_display: "settings",
52 displayDemoMessage: browser.localStorage.getItem("hr_attendance.ShowDemoMessage") !== "false",
53 });
54 this.lockScanner = false;
55 if (this.props.kioskMode === 'settings' || this.props.fromTrialMode){
56 this.manualKioskMode = false;
57 useBus(this.barcode.bus, "barcode_scanned", (ev) => this.onBarcodeScanned(ev.detail.barcode));
58 }
59 else if (this.props.kioskMode !== 'manual') {
60 useBus(this.barcode.bus, "barcode_scanned", (ev) => this.onBarcodeScanned(ev.detail.barcode));
61 this.state.active_display = "main";
62 this.manualKioskMode = false;
63 } else {
64 this.manualKioskMode = true;
65 this.state.active_display = "manual";
66 }
67 }
68
69 switchDisplay(screen) {
70 const displays = ["main", "greet", "manual", "pin", "settings"];
71 if (displays.includes(screen)) {
72 this.state.active_display = screen;
73 } else {
74 this.state.active_display = "main";
75 }
76 }
77
78 newSetUp() {
79 this.dialogService.add(NewEmployeeDialog, { 'token': this.props.token });
80 }
81
82 async setSetting(mode) {
83 await rpc("/hr_attendance/set_settings", {
84 token: this.props.token,
85 mode: mode,
86 });
87 this.props.kioskMode = mode;
88 if (mode !== "manual") {
89 this.manualKioskMode = false;
90 this.state.active_display = "main";
91 this.props.kioskMode = mode;
92 } else {
93 this.manualKioskMode = true;
94 this.state.active_display = "manual";
95 this.props.kioskMode = "manual";
96 }
97 }
98
99 async kioskConfirm(employeeId){
100 const employee = await rpc('attendance_employee_data',
101 {
102 'token': this.props.token,
103 'employee_id': employeeId
104 })
105 if (employee && employee.employee_name){
106 if (employee.use_pin){
107 this.employeeData = employee
108 this.switchDisplay('pin')
109 }else{
110 await this.onManualSelection(employeeId, false)
111 }
112 }
113 }
114
115 kioskReturn() {
116 if (this.state.active_display === "settings"){
117 history.back();
118 } else if (
119 (["manual", "barcode"].includes(this.props.kioskMode) ||
120 (this.props.kioskMode === "barcode_manual" &&
121 this.state.active_display === "main")) &&
122 this.props.fromTrialMode
123 ) {
124 this.switchDisplay("settings");
125 } else if (this.props.kioskMode === 'manual') {
126 this.switchDisplay("manual");
127 } else {
128 this.switchDisplay("main");
129 }
130 }
131
132 displayNotification(text){
133 this.notification.add(text, { type: "danger" });
134 }
135
136 async makeRpcWithGeolocation(route, params) {
137 if (!this.props.deviceTrackingEnabled || !navigator.geolocation || isIosApp()) {
138 // iOS app lacks permissions or tracking disabled
139 return rpc(route, { ...params });
140 }
141
142 return new Promise((resolve) => {
143 navigator.geolocation.getCurrentPosition(
144 async ({ coords: { latitude, longitude } }) => {
145 const result = await rpc(route, {
146 ...params,
147 latitude,
148 longitude,
149 });
150 resolve(result);
151 },
152 async (err) => {
153 const result = await rpc(route, {
154 ...params
155 });
156 resolve(result);
157 },
158 { enableHighAccuracy: true }
159 );
160 });
161 }
162
163 async onManualSelection(employeeId, enteredPin) {
164 const result = await this.makeRpcWithGeolocation('manual_selection',
165 {
166 'token': this.props.token,
167 'employee_id': employeeId,
168 'pin_code': enteredPin
169 })
170 if (result && result.attendance) {
171 this.employeeData = result
172 this.switchDisplay('greet')
173 }else{
174 if (enteredPin){
175 this.displayNotification(_t("Wrong Pin"))
176 }
177 }
178 }
179
180 async onBarcodeScanned(barcode){
181 if (this.lockScanner || this.state.active_display !== 'main') {
182 return;
183 }
184 this.lockScanner = true;
185 this.ui.block();
186
187 let result;
188 try {
189 result = await rpc("attendance_barcode_scanned", {
190 barcode: barcode,
191 token: this.props.token,
192 });
193
194 if (result && result.employee_name) {
195 this.employeeData = result;
196 this.switchDisplay("greet");
197 } else {
198 this.displayNotification(
199 _t("No employee corresponding to Badge ID '%(barcode)s.'", { barcode })
200 );
201 }
202 } catch (error) {
203 this.displayNotification(error.data.message);
204 } finally {
205 this.lockScanner = false;
206 this.ui.unblock();
207 }
208 }
209
210 removeDemoMessage() {
211 this.state.displayDemoMessage = false;
212 browser.localStorage.setItem("hr_attendance.ShowDemoMessage", "false");
213 return;
214 }
215 }
216
217 export async function createPublicKioskAttendance(document, kiosk_backend_info) {
218 await whenReady();
219 const env = makeEnv();
220 await startServices(env);
221 session.server_version_info = kiosk_backend_info.server_version_info;
222 const app = new App(kioskAttendanceApp, {
223 getTemplate,
224 env: env,
225 props:
226 {
227 token : kiosk_backend_info.token,
228 companyId: kiosk_backend_info.company_id,
229 companyName: kiosk_backend_info.company_name,
230 departments: kiosk_backend_info.departments,
231 kioskMode: kiosk_backend_info.kiosk_mode,
232 barcodeSource: kiosk_backend_info.barcode_source,
233 fromTrialMode: kiosk_backend_info.from_trial_mode,
234 deviceTrackingEnabled: kiosk_backend_info.device_tracking_enabled,
235 },
236 dev: env.debug,
237 translateFn: appTranslateFn,
238 translatableAttributes: ["data-tooltip"],
239 });
240 return app.mount(document.body);
241 }
242 export default { kioskAttendanceApp, createPublicKioskAttendance };