|
| 1 | +import { Component, useState } from "@odoo/owl"; |
| 2 | +import { useService } from "@web/core/utils/hooks"; |
| 3 | +import { Dialog } from "@web/core/dialog/dialog"; |
| 4 | +import { Input } from "@point_of_sale/app/generic_components/inputs/input/input"; |
| 5 | +import { usePos } from "@point_of_sale/app/store/pos_hook"; |
| 6 | +import { _t } from "@web/core/l10n/translation"; |
| 7 | +import { unaccent } from "@web/core/utils/strings"; |
| 8 | +import { fuzzyLookup } from "@web/core/utils/search"; |
| 9 | +import { useHotkey } from "@web/core/hotkeys/hotkey_hook"; |
| 10 | +import { makeActionAwaitable } from "@point_of_sale/app/store/make_awaitable_dialog"; |
| 11 | + |
| 12 | + |
| 13 | +export class SalespersonList extends Component { |
| 14 | + static template = "salesperson_button_in_pos.SalespersonList"; |
| 15 | + static components = {Dialog, Input }; |
| 16 | + static props = { |
| 17 | + getPayload: { type: Function }, |
| 18 | + close: { type: Function }, |
| 19 | + currentSelectedSalesperson: { type: Object }, |
| 20 | + }; |
| 21 | + |
| 22 | + setup(){ |
| 23 | + this.pos = usePos(); |
| 24 | + this.dialog = useService("dialog"); |
| 25 | + this.action=useService("action"); |
| 26 | + this.ui = useState(useService("ui")); |
| 27 | + this.notification = useService("notification"); |
| 28 | + this.allSalesperson = this.pos.models["hr.employee"]?.getAll(); |
| 29 | + this.state = useState({ |
| 30 | + query: "", |
| 31 | + selectedSalesPerson: null |
| 32 | + }); |
| 33 | + useHotkey("enter", () => this.onEnter()); |
| 34 | + this.loadSalespeople(); |
| 35 | + } |
| 36 | + |
| 37 | + async loadSalespeople() { |
| 38 | + this.state.salespeople = await this.getSalespersons(); |
| 39 | + } |
| 40 | + |
| 41 | + async editSalesperson(salesperson = false) { |
| 42 | + try { |
| 43 | + const actionProps = salesperson && salesperson.id ? { resId: salesperson.id } : {}; |
| 44 | + |
| 45 | + await this.env.services.action.doAction("salesperson_button_in_pos.action_salesperson_create_form_view", { |
| 46 | + props: actionProps, |
| 47 | + onClose: async () => { |
| 48 | + await this.loadSalespeople(); |
| 49 | + this.props.close(); |
| 50 | + }, |
| 51 | + }); |
| 52 | + } catch (error) { |
| 53 | + console.error("Error opening salesperson form:", error); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + get filteredSalespersons() { |
| 58 | + if (!this.state.query) { |
| 59 | + return this.allSalesperson; |
| 60 | + } |
| 61 | + return fuzzyLookup( |
| 62 | + this.state.query, |
| 63 | + this.allSalesperson, |
| 64 | + (salesperson) => unaccent(salesperson.name) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + getSalespersons() { |
| 69 | + const users = this.pos.models["res.users"].getAll(); |
| 70 | + const query = this.state.query?.toLowerCase() || ""; |
| 71 | + return users.filter((u) => u.name?.toLowerCase().includes(query)); |
| 72 | + } |
| 73 | + |
| 74 | + selectSalesperson(salesperson) { |
| 75 | + const currentOrder = this.pos.get_order(); |
| 76 | + if (!currentOrder) return; |
| 77 | + |
| 78 | + if (this.props.currentSelectedSalesperson?.id === salesperson.id) { |
| 79 | + this.props.getPayload(null); |
| 80 | + } else { |
| 81 | + this.props.getPayload(salesperson); |
| 82 | + } |
| 83 | + this.props.close(); |
| 84 | + } |
| 85 | + |
| 86 | + onEnter() { |
| 87 | + this.notification.add(_t('No more customer found for "%s".', this.state.query),300); |
| 88 | + } |
| 89 | + |
| 90 | + onSearchInput(event) { |
| 91 | + this.state.query = event?.target?.value; |
| 92 | + } |
| 93 | +} |
0 commit comments