Debian Codegraph @

addons/point_of_sale/static/src/backend/test_epos/test_epos.js

raw
1 import { Component } from "@odoo/owl";
2 import { registry } from "@web/core/registry";
3 import { standardWidgetProps } from "@web/views/widgets/standard_widget_props";
4 import { _t } from "@web/core/l10n/translation";
5 import { useService } from "@web/core/utils/hooks";
6
7 const EPSON_ERRORS = {
8 DeviceNotFound: _t(
9 "Check the printer configuration for the 'Device ID' setting.\nIt should be set to: 'local_printer'"
10 ),
11 EPTR_AUTOMATICAL: _t(
12 "Continuous printing of high-density printing caused a printing error. Please retry later"
13 ),
14 EPTR_COVER_OPEN: _t("Printer cover is open, please close it before printing"),
15 EPTR_CUTTER: _t("The cutter has a foreign matter, please check the cutter mechanism"),
16 EPTR_MECHANICAL: _t("Mechanical error, please check the printer"),
17 EPTR_REC_EMPTY: _t("The paper is empty, please load paper into the printer"),
18 EPTR_UNRECOVERABLE: _t("Low voltage unrecoverable error occured, please check the printer"),
19 EX_BADPORT: _t("The device is not connected, please check the printer power / connection"),
20 EX_TIMEOUT: _t("Timeout occured, please try again"),
21 };
22
23 export class TestEPos extends Component {
24 static template = `point_of_sale.TestEPosButton`;
25 static props = {
26 ...standardWidgetProps,
27 };
28
29 setup() {
30 super.setup();
31 this.notification = useService("notification");
32 }
33
34 _getReceipt() {
35 return `
36 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
37 <s:Body>
38 <epos-print xmlns="http://www.epson-pos.com/schemas/2011/03/epos-print">
39 <feed line="1" />
40 <text align="center">This is a test receipt&#10;</text>
41 <feed line="3" />
42 <cut type="feed" />
43 </epos-print>
44 </s:Body>
45 </s:Envelope>`;
46 }
47
48 async onClick() {
49 try {
50 const data = this.props.record.data;
51 const printer_ip =
52 data.epson_printer_ip !== undefined
53 ? data.epson_printer_ip
54 : data.pos_epson_printer_ip;
55 if (!printer_ip) {
56 this.notification.add(
57 _t("Please configure a valid ePoS url in order to test the printer"),
58 { type: "danger" }
59 );
60 return;
61 }
62 const url = window.location.protocol + "//" + printer_ip;
63 this.address = url + "/cgi-bin/epos/service.cgi?devid=local_printer";
64 // Parse response
65 const result = await fetch(this.address, {
66 method: "POST",
67 body: this._getReceipt(),
68 signal: AbortSignal.timeout(15000),
69 });
70 const body = await result.text();
71 const parser = new DOMParser();
72 const parsedBody = parser.parseFromString(body, "application/xml");
73 const response = parsedBody.querySelector("response");
74 const success = response.getAttribute("success") === "true";
75 const errorCode = response.getAttribute("code");
76 if (!success || errorCode !== "") {
77 const errorMessage =
78 EPSON_ERRORS[errorCode] ||
79 _t("Failed to print a test receipt. Check your printer.");
80 this.notification.add(errorMessage, { type: "warning" });
81 } else {
82 this.notification.add(_t("Succesfully printed a test receipt"), { type: "info" });
83 }
84 } catch {
85 this.notification.add(
86 _t(
87 "Failed to reach the printer. Check the configured url. Make sure that the printer is online and you are on the same network."
88 ),
89 { type: "danger" }
90 );
91 }
92 }
93 }
94
95 export const TestEPosWidget = {
96 component: TestEPos,
97 };
98 registry.category("view_widgets").add("point_of_sale_test_epos", TestEPosWidget);