Debian Codegraph @

addons/point_of_sale/static/src/app/utils/printer/base_printer.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { ConnectionLostError } from "@web/core/network/rpc";
3 import { htmlToCanvas } from "@point_of_sale/app/services/render_service";
4 /**
5 * Implements basic printer functions.
6 */
7 export class BasePrinter {
8 constructor() {
9 this.setup(...arguments);
10 }
11
12 setup() {
13 this.receiptQueue = [];
14 }
15
16 /**
17 * Add the receipt to the queue of receipts to be printed and process it.
18 * We clear the print queue if printing is not successful.
19 * @param {String} receipt: The receipt to be printed, in HTML
20 * @returns {{ successful: boolean; message?: { title: string; body?: string }}}
21 */
22 async printReceipt(receipt) {
23 if (receipt) {
24 this.receiptQueue.push(receipt);
25 }
26 let image, printResult;
27 while (this.receiptQueue.length > 0) {
28 receipt = this.receiptQueue.shift();
29 image = this.processCanvas(
30 await htmlToCanvas(receipt, { addClass: "pos-receipt-print" })
31 );
32 try {
33 printResult = await this.sendPrintingJob(image);
34 } catch (error) {
35 // Error in communicating to the IoT box.
36 this.receiptQueue.length = 0;
37 if (error instanceof ConnectionLostError) {
38 return this.getOfflineError();
39 }
40 return this.getActionError();
41 }
42 // rpc call is okay but printing failed because
43 // IoT box can't find a printer.
44 if (!printResult || printResult.result === false) {
45 this.receiptQueue.length = 0;
46 return this.getResultsError(printResult);
47 }
48 }
49 return {
50 successful: true,
51 warningCode: this.getResultWarningCode(printResult),
52 };
53 }
54
55 async sendPrintingJob() {
56 throw new Error("Not implemented");
57 }
58
59 openCashbox() {
60 throw new Error("Not implemented");
61 }
62
63 /**
64 * Generate a jpeg image from a canvas
65 * @param {DOMElement} canvas
66 */
67 processCanvas(canvas) {
68 return canvas.toDataURL("image/jpeg").replace("data:image/jpeg;base64,", "");
69 }
70
71 /**
72 * Return value of this method will be the result of calling `printReceipt`
73 * if it failed to connect to the IoT box.
74 */
75 getActionError() {
76 return {
77 successful: false,
78 canRetry: true,
79 message: {
80 title: _t("Connection to IoT Box failed"),
81 body: _t(
82 "Please ensure the IoT box is turned on and connected to the network before retrying."
83 ),
84 },
85 };
86 }
87
88 /**
89 * Return value of this method will be the result of calling `printReceipt`
90 * if it failed due to the client being offline.
91 */
92 getOfflineError() {
93 return {
94 successful: false,
95 canRetry: true,
96 message: {
97 title: _t("No Internet Connection"),
98 body: _t("Please ensure you are connected to the internet before retrying."),
99 },
100 };
101 }
102
103 /**
104 * Return value of this method will be the result of calling `printReceipt`
105 * if the result coming from the IoT box is empty.
106 */
107 getResultsError(_printResult) {
108 return {
109 successful: false,
110 canRetry: true,
111 message: {
112 title: _t("Connection to the printer failed"),
113 body: _t(
114 "Your IoT box cannot find the printer, please ensure it is connected and turned on before retrying."
115 ),
116 },
117 };
118 }
119
120 getResultWarningCode(_printResult, options = {}) {
121 return undefined;
122 }
123 }