| 1 |
import { _t } from "@web/core/l10n/translation"; |
| 2 |
import { registry } from "@web/core/registry"; |
| 3 |
import { RetryPrintPopup } from "@point_of_sale/app/components/popups/retry_print_popup/retry_print_popup"; |
| 4 |
import { PrinterService } from "@point_of_sale/app/services/printer_service"; |
| 5 |
import { AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 6 |
import { logPosMessage } from "../utils/pretty_console_log"; |
| 7 |
|
| 8 |
export const posPrinterService = { |
| 9 |
dependencies: ["hardware_proxy", "dialog", "renderer"], |
| 10 |
start(env, { hardware_proxy, dialog, renderer }) { |
| 11 |
return new PosPrinterService(env, { hardware_proxy, dialog, renderer }); |
| 12 |
}, |
| 13 |
}; |
| 14 |
export class PosPrinterService extends PrinterService { |
| 15 |
constructor(...args) { |
| 16 |
super(...args); |
| 17 |
this.setup(...args); |
| 18 |
} |
| 19 |
setup(env, { hardware_proxy, dialog, renderer }) { |
| 20 |
super.setup(...arguments); |
| 21 |
this.renderer = renderer; |
| 22 |
this.hardware_proxy = hardware_proxy; |
| 23 |
this.dialog = dialog; |
| 24 |
this.device = hardware_proxy.printer; |
| 25 |
} |
| 26 |
async print() { |
| 27 |
this.setPrinter(this.hardware_proxy.printer); |
| 28 |
return super.print(...arguments); |
| 29 |
} |
| 30 |
printWeb() { |
| 31 |
try { |
| 32 |
return super.printWeb(...arguments); |
| 33 |
} catch { |
| 34 |
this.dialog.add(AlertDialog, { |
| 35 |
title: _t("Printing is not supported on some browsers"), |
| 36 |
body: _t("It is possible to print your tickets by making use of an IoT Box."), |
| 37 |
}); |
| 38 |
return false; |
| 39 |
} |
| 40 |
} |
| 41 |
async printHtml() { |
| 42 |
this.setPrinter(this.hardware_proxy.printer); |
| 43 |
try { |
| 44 |
return await super.printHtml(...arguments); |
| 45 |
} catch (error) { |
| 46 |
if (error.body === undefined) { |
| 47 |
logPosMessage( |
| 48 |
"PosPrinterService", |
| 49 |
"printHtml", |
| 50 |
"An unknown error occured in printHtml", |
| 51 |
false, |
| 52 |
[error] |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
this.dialog.closeAll(); |
| 57 |
this.dialog.add(RetryPrintPopup, { |
| 58 |
title: error.title, |
| 59 |
message: error.body, |
| 60 |
canRetry: error.canRetry, |
| 61 |
retry: () => { |
| 62 |
this.printHtml(...arguments); |
| 63 |
}, |
| 64 |
download: () => { |
| 65 |
this.printWeb(...arguments); |
| 66 |
}, |
| 67 |
}); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
registry.category("services").add("printer", posPrinterService); |