| 1 |
|
| 2 |
|
| 3 |
import { _t } from "@web/core/l10n/translation"; |
| 4 |
|
| 5 |
import { NumberPopup } from "@point_of_sale/app/components/popups/number_popup/number_popup"; |
| 6 |
import { useBarcodeReader } from "@point_of_sale/app/hooks/barcode_reader_hook"; |
| 7 |
import { usePos } from "@point_of_sale/app/hooks/pos_hook"; |
| 8 |
import { useService } from "@web/core/utils/hooks"; |
| 9 |
import { makeAwaitable, ask } from "@point_of_sale/app/utils/make_awaitable_dialog"; |
| 10 |
import { CashierSelectionPopup } from "@pos_hr/app/components/popups/cashier_selection_popup/cashier_selection_popup"; |
| 11 |
|
| 12 |
export function useCashierSelector({ exclusive, onScan } = { onScan: () => {}, exclusive: false }) { |
| 13 |
const pos = usePos(); |
| 14 |
const dialog = useService("dialog"); |
| 15 |
const notification = useService("notification"); |
| 16 |
useBarcodeReader( |
| 17 |
{ |
| 18 |
async cashier(code) { |
| 19 |
const employee = pos.models["hr.employee"].find( |
| 20 |
(emp) => emp._barcode === Sha1.hash(code.code) |
| 21 |
); |
| 22 |
if ( |
| 23 |
employee && |
| 24 |
employee !== pos.getCashier() && |
| 25 |
(!employee._pin || (await checkPin(employee))) |
| 26 |
) { |
| 27 |
onScan && onScan(employee); |
| 28 |
} |
| 29 |
return employee; |
| 30 |
}, |
| 31 |
}, |
| 32 |
exclusive |
| 33 |
); |
| 34 |
|
| 35 |
async function checkPin(employee, pin = false) { |
| 36 |
let inputPin = pin; |
| 37 |
if (!pin) { |
| 38 |
inputPin = await makeAwaitable(dialog, NumberPopup, { |
| 39 |
formatDisplayedValue: (x) => x.replace(/./g, "•"), |
| 40 |
title: _t("Password?"), |
| 41 |
}); |
| 42 |
} else { |
| 43 |
if (employee._pin !== Sha1.hash(inputPin)) { |
| 44 |
inputPin = await makeAwaitable(dialog, NumberPopup, { |
| 45 |
formatDisplayedValue: (x) => x.replace(/./g, "•"), |
| 46 |
title: _t("Password?"), |
| 47 |
}); |
| 48 |
} |
| 49 |
} |
| 50 |
if (!inputPin || employee._pin !== Sha1.hash(inputPin)) { |
| 51 |
notification.add(_t("PIN not found"), { |
| 52 |
type: "warning", |
| 53 |
title: _t(`Wrong PIN`), |
| 54 |
}); |
| 55 |
return false; |
| 56 |
} |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
return async function selectCashier(pin = false, login = false, list = false) { |
| 64 |
if (!pos.config.module_pos_hr) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
const wrongPinNotification = () => { |
| 69 |
notification.add(_t("PIN not found"), { |
| 70 |
type: "warning", |
| 71 |
title: _t(`Wrong PIN`), |
| 72 |
}); |
| 73 |
}; |
| 74 |
|
| 75 |
let employee = false; |
| 76 |
const allEmployees = pos.models["hr.employee"].filter( |
| 77 |
(employee) => employee.id !== pos.getCashier()?.id |
| 78 |
); |
| 79 |
const pinMatchEmployees = allEmployees.filter( |
| 80 |
(employee) => !pin || Sha1.hash(pin) === employee._pin |
| 81 |
); |
| 82 |
|
| 83 |
if (!pinMatchEmployees.length && !pin) { |
| 84 |
await ask(dialog, { |
| 85 |
title: _t("No Cashiers"), |
| 86 |
body: _t("There is no cashier available."), |
| 87 |
}); |
| 88 |
return; |
| 89 |
} else if (pin && !pinMatchEmployees.length) { |
| 90 |
wrongPinNotification(); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if (pinMatchEmployees.length > 1 || list) { |
| 95 |
employee = await makeAwaitable(dialog, CashierSelectionPopup, { |
| 96 |
currentCashier: pos.getCashier() || undefined, |
| 97 |
employees: allEmployees, |
| 98 |
}); |
| 99 |
|
| 100 |
if (!employee) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if (pin && Sha1.hash(pin) !== employee._pin) { |
| 105 |
wrongPinNotification(); |
| 106 |
return; |
| 107 |
} |
| 108 |
} else if (pinMatchEmployees.length === 1) { |
| 109 |
employee = pinMatchEmployees[0]; |
| 110 |
} |
| 111 |
|
| 112 |
if (!pin && employee && employee._pin) { |
| 113 |
const result = await checkPin(employee); |
| 114 |
|
| 115 |
if (!result) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (login && employee) { |
| 121 |
pos.hasLoggedIn = true; |
| 122 |
pos.setCashier(employee); |
| 123 |
} |
| 124 |
|
| 125 |
const currentScreen = pos.router.state.current; |
| 126 |
if (currentScreen === "LoginScreen" && login && employee) { |
| 127 |
const selectedScreen = pos.defaultPage; |
| 128 |
const props = { |
| 129 |
...selectedScreen.params, |
| 130 |
orderUuid: pos.selectedOrderUuid, |
| 131 |
}; |
| 132 |
if (selectedScreen.page === "FloorScreen") { |
| 133 |
delete props.orderUuid; |
| 134 |
} |
| 135 |
pos.navigate(selectedScreen.page, props); |
| 136 |
} |
| 137 |
|
| 138 |
return employee; |
| 139 |
}; |
| 140 |
} |