Debian Codegraph @

addons/point_of_sale/static/src/app/utils/debug/debug_widget.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { useBus, useService } from "@web/core/utils/hooks";
3 import { useRef, useState, Component, onMounted, onWillDestroy, useEffect } from "@odoo/owl";
4 import { usePos } from "@point_of_sale/app/hooks/pos_hook";
5 import { serializeDateTime } from "@web/core/l10n/dates";
6 import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
7 import { downloadPosLogs } from "../pretty_console_log";
8 const { DateTime } = luxon;
9
10 export class DebugWidget extends Component {
11 static template = "point_of_sale.DebugWidget";
12 static props = {};
13
14 setup() {
15 this.pos = usePos();
16 this.barcodeReader = useService("barcode_reader");
17 this.hardwareProxy = useService("hardware_proxy");
18 this.notification = useService("notification");
19 this.numberBuffer = useService("number_buffer");
20 this.dialog = useService("dialog");
21 this.importOrderInput = useRef("import-order-input");
22 this.state = useState({
23 isOpen: false,
24 barcodeInput: "",
25 weightInput: "",
26 buffer: this.numberBuffer.get(),
27 device_identifier: this.pos.device.data.device_identifier,
28 next_number: this.pos.device.data.next_number,
29 unsynced_number_stack: JSON.stringify(
30 this.pos.device?.data?.unsynced_number_stack || []
31 ),
32 });
33
34 useBus(this.numberBuffer, "buffer-update", this._onBufferUpdate);
35 onMounted(() => {
36 if (!this.importOrderInput || !this.importOrderInput.el) {
37 return;
38 }
39
40 this.importOrderInput.el.addEventListener("click", this.handleFileOrderImport);
41 });
42 onWillDestroy(() => {
43 if (this.importOrderInput?.el) {
44 this.importOrderInput.el.removeEventListener("click", this.handleFileOrderImport);
45 }
46 });
47
48 useEffect(
49 (isOpen) => {
50 if (!isOpen) {
51 return;
52 }
53
54 // Since addEventListener('storage') is not triggered from the same tab,
55 // we need to poll the device data to keep the widget updated.
56 const interval = setInterval(() => {
57 this.state.next_number = this.pos.device?.data?.next_number;
58 this.state.unsynced_number_stack = JSON.stringify(
59 this.pos.device?.data?.unsynced_number_stack || []
60 );
61 }, 500);
62
63 return () => clearInterval(interval);
64 },
65 () => [this.state.isOpen]
66 );
67 }
68 get isDisabled() {
69 return this.pos.cashier._role === "minimal";
70 }
71 disableDebugMode() {
72 const url = new URL(window.location.href);
73 url.searchParams.delete("debug");
74 window.history.replaceState({}, document.title, url);
75 window.location.reload();
76 }
77 handleFileOrderImport() {
78 document.getElementById("import-order-input").click();
79 }
80 toggleWidget() {
81 this.state.isOpen = !this.state.isOpen;
82 }
83 async barcodeScan() {
84 if (!this.barcodeReader) {
85 return;
86 }
87 await this.barcodeReader.scan(this.state.barcodeInput);
88 }
89 async barcodeScanEAN() {
90 if (!this.barcodeReader) {
91 return;
92 }
93 const ean = this.barcodeReader.parser.sanitize_ean(this.state.barcodeInput || "0");
94 this.state.barcodeInput = ean;
95 await this.barcodeReader.scan(ean);
96 }
97 _createBlob(contents) {
98 if (typeof contents !== "string") {
99 contents = JSON.stringify(contents, null, 2);
100 }
101 return new Blob([contents]);
102 }
103 deleteOrders({ paid = true } = {}) {
104 this.dialog.add(ConfirmationDialog, {
105 title: _t("Delete Orders?"),
106 body: _t(
107 `This operation will destroy all ${
108 paid ? "paid" : "unpaid"
109 } orders in the browser. You will lose all the data. This operation cannot be undone.`
110 ),
111 confirm: () => {
112 this.pos.data.network.unsyncData = [];
113 const orders = this.pos.models["pos.order"].filter(
114 (order) => order.finalized === paid
115 );
116
117 for (const order of orders) {
118 this.pos.data.localDeleteCascade(order, !paid);
119 }
120
121 if (!this.pos.getOrder()) {
122 return this.pos.addNewOrder();
123 }
124 },
125 });
126 }
127
128 deleteAllIndexedDB() {
129 if (!window.indexedDB || !window.indexedDB.databases) {
130 console.warn("IndexedDB is not supported in this browser.");
131 return;
132 }
133
134 window.indexedDB.databases().then((r) => {
135 for (var i = 0; i < r.length; i++) {
136 window.indexedDB.deleteDatabase(r[i].name);
137 }
138 });
139 }
140
141 async exportData() {
142 const data = await this.pos.data.synchronizeLocalDataInIndexedDB();
143 const blob = this._createBlob(data);
144 const url = URL.createObjectURL(blob);
145 const a = document.createElement("a");
146 const fileName = `pos_data${serializeDateTime(DateTime.now()).replace(/:|\s/gi, "-")}.json`;
147 a.href = url;
148 a.download = fileName;
149 a.click();
150 }
151 async importData(event) {
152 const file = event.target.files[0];
153 if (file) {
154 try {
155 const jsonData = JSON.parse(await file.text());
156 await this.pos.data.getLocalDataFromIndexedDB(jsonData);
157 } catch (error) {
158 console.warn("An error occured during import", error);
159 }
160 }
161 }
162
163 refreshDisplay() {
164 this.hardwareProxy.message("display_refresh", {});
165 }
166 async downloadLogs() {
167 await downloadPosLogs();
168 }
169 _onBufferUpdate({ detail: value }) {
170 this.state.buffer = value;
171 }
172 get bufferRepr() {
173 return `"${this.state.buffer}"`;
174 }
175 }