| 1 |
import { Component, onWillStart } from "@odoo/owl"; |
| 2 |
import { registry } from "@web/core/registry"; |
| 3 |
import { _t } from "@web/core/l10n/translation"; |
| 4 |
import { useService } from "@web/core/utils/hooks"; |
| 5 |
import { standardActionServiceProps } from "@web/webclient/actions/action_service"; |
| 6 |
import { BarcodeScanner } from "@barcodes/components/barcode_scanner"; |
| 7 |
|
| 8 |
export class BadgeScanner extends Component { |
| 9 |
static template = "hr.BadgeScannerTemplate" |
| 10 |
static components = { BarcodeScanner }; |
| 11 |
static props = { |
| 12 |
...standardActionServiceProps, |
| 13 |
}; |
| 14 |
setup() { |
| 15 |
this.employeeId = this.props.action?.context?.active_id; |
| 16 |
this.notification = useService("notification"); |
| 17 |
this.actionService = useService("action"); |
| 18 |
this.orm = useService("orm"); |
| 19 |
onWillStart(async () => { |
| 20 |
this.employee = await this.orm.read("hr.employee", [this.employeeId], ["name"]); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
async onBarcodeScanned(barcode) { |
| 25 |
if (!barcode) { |
| 26 |
this.notification.add(_t("No barcode received"), { |
| 27 |
type: "danger", |
| 28 |
}); |
| 29 |
return; |
| 30 |
} |
| 31 |
if (!this.employeeId) { |
| 32 |
this.notification.add(_t("Missing Employee ID"), { |
| 33 |
type: "danger", |
| 34 |
}); |
| 35 |
} |
| 36 |
try{ |
| 37 |
await this.orm.write("hr.employee", [this.employee[0].id], { barcode: barcode }) |
| 38 |
this.env.config.historyBack(); |
| 39 |
this.notification.add((_t("Badge updated: ") + barcode), { type: "success" }); |
| 40 |
} |
| 41 |
catch(error){ |
| 42 |
this.notification.add(_t("Failed to update badge: ") + error?.data?.message, { |
| 43 |
type: "danger", |
| 44 |
}); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
onClickBack() { |
| 49 |
this.env.config.historyBack(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
registry.category("actions").add("employee_barcode_scanner", BadgeScanner); |