pos_custom_receipt/static/src/js/orderline.js

61 lines
2.3 KiB
JavaScript

/** @odoo-module */
import { patch } from "@web/core/utils/patch";
import { Orderline } from "@point_of_sale/app/components/orderline/orderline";
import { PosOrder } from "@point_of_sale/app/models/pos_order";
patch(Orderline.prototype, {
get lineScreenValues() {
const res = super.lineScreenValues;
// In POS, the internal note (line.note) is typically shown only in display mode.
// We override this to also include the parsed internalNote when printing receipts.
if (this.props.mode === "receipt" && this.line.note) {
try {
res.internalNote = JSON.parse(this.line.note);
} catch (e) {
res.internalNote = [{ text: this.line.note }]; // Fallback if not valid JSON
}
}
return res;
}
});
patch(PosOrder.prototype, {
getCashierName() {
const pos = this.pos || this.models?.env?.services?.pos;
let employee = this.employee_id || this.original_employee_id || this.user_id || null;
// If employee is just an ID/number, look up the record from the POS store models
if (employee && (typeof employee === 'number' || typeof employee === 'string')) {
const empId = parseInt(employee);
if (pos && pos.models && pos.models['hr.employee']) {
employee = pos.models['hr.employee'].get(empId) || { name: `Employee ${empId}` };
} else if (pos && pos.employees) {
employee = pos.employees.find(e => e.id === empId) || { name: `Employee ${empId}` };
} else {
employee = { name: `Employee ${empId}` };
}
}
const name = employee ? (employee.name || "") : "";
if (!name) {
return "";
}
// Extract prefix if present (e.g. "Served by:", "Order Taker:")
let temp = name.trim();
let prefix = "";
const prefixMatch = temp.match(/^(served\s+by:?|order\s+taker:?|kasir:?)\s*/i);
if (prefixMatch) {
prefix = prefixMatch[0];
temp = temp.substring(prefix.length).trim();
}
const parts = temp.split(/\s+/);
if (parts.length <= 2) {
return name;
}
return prefix + parts[0] + " " + parts[parts.length - 1];
}
});