/* global Sha1 */ import { patch } from "@web/core/utils/patch"; import { PosStore } from "@point_of_sale/app/services/pos_store"; import { _t } from "@web/core/l10n/translation"; import { NumberPopup } from "@point_of_sale/app/components/popups/number_popup/number_popup"; import { makeAwaitable } from "@point_of_sale/app/utils/make_awaitable_dialog"; patch(PosStore.prototype, { async setTableFromUi(table, orderUuid = null) { if (!this.config.module_pos_restaurant || this.isOrderTransferMode) { return super.setTableFromUi(...arguments); } const cashier = await this._selectCashierByPin(); if (!cashier) { return; } this.setCashier(cashier); await super.setTableFromUi(...arguments); const order = this.getOrder(); if (order) { order.uiState.is_authorized = true; } }, async pay() { if (this.selectedOrder.lines.length === 0) { return; } if (!this.canPay) { this.notification.add(_t("You do not have permission to process payments."), { type: "danger", }); return; } if (!this.selectedOrder.uiState.is_authorized) { const cashier = await this._selectCashierByPin(); if (!cashier) { return; } this.setCashier(cashier); this.selectedOrder.uiState.is_authorized = true; } this.selectedOrder.update({ payer_id: this.getCashier() }); return super.pay(); }, async _selectCashierByPin() { if (!this.config.module_pos_hr) { return this.getCashier(); } const inputPin = await makeAwaitable(this.dialog, NumberPopup, { formatDisplayedValue: (x) => x.replace(/./g, "•"), title: _t("Enter PIN"), }); if (!inputPin) { return false; } const hashedPin = Sha1.hash(inputPin); const allEmployees = this.models["hr.employee"].getAll(); const employee = allEmployees.find(emp => emp._pin === hashedPin); if (!employee) { this.notification.add(_t("Invalid PIN"), { type: "danger", }); return false; } return employee; }, get canPay() { const cashier = this.getCashier(); if (!cashier) { return false; } // If pos_employee_role is installed, check role if (cashier.pos_role) { return ['cashier', 'outlet_manager', 'area_manager'].includes(cashier.pos_role); } // Default to true if no role system return true; }, });