[FIX] point_of_sale: print not working in Mobile App

Before this commit, "document.execCommand" was first
method used when attempting to print, due to a bug
in Safari for Windows (ndr. which is deprecated as
the last version of Safari for Windows is 5.1.7
from may 2012).

On the mobile App, the "document.execCommand('print')"
call returns "true" even if nothing has actually been
done, which therefore prevents from executing "window.print()".

After this commit, we execute directly "window.print()"
as it's supported by all modern browsers (except Firefox Android,
which doesn't support printing at all).

Notes:
* "document.execCommand" is deprecated and the "print" command isn't even documented
* Safari 5.1.7 doesn't support Promise/Async/Await, and therefore not Odoo 13+.

References:
* https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
* https://developer.mozilla.org/en-US/docs/Web/API/Window/print
* https://caniuse.com/?search=await
* https://caniuse.com/?search=promise
* https://apple.stackexchange.com/questions/68836/where-can-i-download-safari-for-windows

opw-2486818

closes odoo/odoo#71494

X-original-commit: a11662e525aad9a843acf54b717f3df18d8c2cab
Signed-off-by: Pierre Paridans <pparidans@users.noreply.github.com>
This commit is contained in:
Romeo Fragomeli
2021-05-31 12:33:26 +00:00
parent d6283f71d3
commit 21f4dbad38
2 changed files with 2 additions and 7 deletions
@@ -39,13 +39,9 @@ odoo.define('point_of_sale.AbstractReceiptScreen', function (require) {
return await this._printWeb();
}
}
/**
* https://stackoverflow.com/questions/21285902/printing-a-part-of-webpage-with-javascript
*/
async _printWeb() {
try {
const isPrinted = document.execCommand('print', false, null);
if (!isPrinted) window.print();
window.print();
return true;
} catch (err) {
await this.showPopup('ErrorPopup', {
@@ -136,8 +136,7 @@ odoo.define('pos_restaurant.TipScreen', function (require) {
async _printWeb(receipt) {
try {
$(this.el).find('.pos-receipt-container').html(receipt);
const isPrinted = document.execCommand('print', false, null);
if (!isPrinted) window.print();
window.print();
} catch (err) {
await this.showPopup('ErrorPopup', {
title: this.env._t('Printing is not supported on some browsers'),