| 1 | import { useCashierSelector } from "@pos_hr/app/utils/select_cashier_mixin"; |
| 2 | import { _t } from "@web/core/l10n/translation"; |
| 3 | import { LoginScreen } from "@point_of_sale/app/screens/login_screen/login_screen"; |
| 4 | import { patch } from "@web/core/utils/patch"; |
| 5 | import { useAutofocus } from "@web/core/utils/hooks"; |
| 6 | import { onWillUnmount, useExternalListener, useState } from "@odoo/owl"; |
| 7 | |
| 8 | patch(LoginScreen.prototype, { |
| 9 | setup() { |
| 10 | super.setup(...arguments); |
| 11 | |
| 12 | this.state = useState({ |
| 13 | pin: "", |
| 14 | }); |
| 15 | |
| 16 | if (this.pos.config.module_pos_hr) { |
| 17 | this.cashierSelector = useCashierSelector({ |
| 18 | onScan: (employee) => employee && this.selectOneCashier(employee), |
| 19 | exclusive: true, |
| 20 | }); |
| 21 | |
| 22 | useAutofocus(); |
| 23 | useExternalListener(window, "keypress", async (ev) => { |
| 24 | if (this.pos.login && ev.key === "Enter" && this.state.pin) { |
| 25 | await this.selectCashier(this.state.pin, true); |
| 26 | } |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | onWillUnmount(() => { |
| 31 | this.state.pin = ""; |
| 32 | this.pos.login = false; |
| 33 | }); |
| 34 | }, |
| 35 | async selectCashier(pin = false, login = false, list = false) { |
| 36 | return await this.cashierSelector(pin, login, list); |
| 37 | }, |
| 38 | openRegister() { |
| 39 | if (this.pos.config.module_pos_hr) { |
| 40 | this.pos.login = true; |
| 41 | } else { |
| 42 | super.openRegister(); |
| 43 | } |
| 44 | }, |
| 45 | async clickBack() { |
| 46 | if (!this.pos.config.module_pos_hr) { |
| 47 | super.clickBack(); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (this.pos.login) { |
| 52 | this.state.pin = ""; |
| 53 | this.pos.login = false; |
| 54 | } else { |
| 55 | const employee = await this.selectCashier(); |
| 56 | if (employee && employee.user_id?.id === this.pos.user.id) { |
| 57 | super.clickBack(); |
| 58 | return; |
| 59 | } else if (employee) { |
| 60 | this.pos.notification.add( |
| 61 | _t( |
| 62 | "Only the cashier linked to the logged-in user (%s) can proceed to the Backend.", |
| 63 | this.pos.user.name |
| 64 | ), |
| 65 | { type: "danger" } |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | }, |
| 70 | get backBtnName() { |
| 71 | return this.pos.login && this.pos.config.module_pos_hr ? _t("Discard") : super.backBtnName; |
| 72 | }, |
| 73 | maskedInput(ev) { |
| 74 | ev.preventDefault(); |
| 75 | const input = ev.target; |
| 76 | const pin = this.state.pin || ""; |
| 77 | const maskedLen = input.value.length; |
| 78 | this.state.pin = maskedLen < pin.length ? pin.slice(0, maskedLen) : pin + (ev.data || ""); |
| 79 | |
| 80 | input.value = "•".repeat(this.state.pin.length); |
| 81 | }, |
| 82 | }); |