80 lines
2.6 KiB
JavaScript

/** @odoo-module */
import { registry } from "@web/core/registry";
import { KanbanController } from "@web/views/kanban/kanban_controller";
import { kanbanView } from "@web/views/kanban/kanban_view";
import { onWillUnmount } from "@odoo/owl";
export class KdsKanbanController extends KanbanController {
setup() {
super.setup();
console.log("[KDS Controller] Setup");
// Direct access to services to avoid useService potential conflicts
this.busService = this.env.services.bus_service;
this.notification = this.env.services.notification;
const kdsChannel = "kds_channel";
if (this.busService) {
console.log(`[KDS Controller] Subscribing to channel: ${kdsChannel}`);
this.busService.addChannel(kdsChannel);
const handler = this._onKdsNotification.bind(this);
this.busService.addEventListener("notification", handler);
onWillUnmount(() => {
if (this.busService) {
this.busService.removeEventListener("notification", handler);
}
});
} else {
console.error("[KDS Controller] Bus service not found!");
}
}
_onKdsNotification(event) {
// console.log("[KDS Controller] Notification received:", event);
const notifications = event.detail || [];
let shouldReload = false;
for (const notif of notifications) {
// console.log("[KDS Controller] Processing notification:", notif);
if (notif.type === "kds_new_order") {
console.log("[KDS Controller] New order notification:", notif.payload);
// Show notification to user
if (this.notification) {
const payload = notif.payload;
this.notification.add(
`New Order: ${payload.qty}x ${payload.product_name} - ${payload.table_name}`,
{
title: "Kitchen Display",
type: "info",
}
);
}
shouldReload = true;
}
}
if (shouldReload) {
// Reload the view to show the new order
console.log("[KDS Controller] Reloading view...");
this.model.load();
}
}
}
export const kdsKanbanView = {
...kanbanView,
Controller: KdsKanbanController,
};
registry.category("views").add("kds_kanban", kdsKanbanView);
console.log("[KDS Backend] kds_backend.js loaded (JS Class mode - manual services)");