From 4a5862a492fff66a474b7069cb5ba45836249a0e Mon Sep 17 00:00:00 2001
From: "Lucas Perais (lpe)" <lpe@odoo.com>
Date: Thu, 24 Jan 2019 09:56:33 +0000
Subject: [PATCH] [FIX] point_of_sale: offline invoicing => backend printing

In a pos session:
OFFLINE
make an order with invoicing , try to validate
    The order stays there because it needs to be validated by the server
make another non invoiced order, validate

ONLINE
make another order
    At validation, all orders will be pushed to the server

Before this commit, when trying to validate the invoiced order
the report download couldn't find the order id, and crashed
This was because the order in question was already pushed
but treated as a non invoiced order

After this commit, an "warning" message is displayed to the customer
saying he/she has to print the invoice from the backend.
In most cases it is enough and acceptable, since a customer would actually leave the premises
and come back later for the invoice
It is also safer in terms of data consistency to keep pushing all orders once the connection is back

OPW 1918044

closes odoo/odoo#30485
---
 addons/point_of_sale/i18n/point_of_sale.pot   | 14 +++++++++++++
 addons/point_of_sale/static/src/js/models.js  | 20 +++++++++++++------
 addons/point_of_sale/static/src/js/screens.js | 11 ++++++++++
 3 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/addons/point_of_sale/i18n/point_of_sale.pot b/addons/point_of_sale/i18n/point_of_sale.pot
index 2067c6a928c0..dbf186e92ce8 100644
--- a/addons/point_of_sale/i18n/point_of_sale.pot
+++ b/addons/point_of_sale/i18n/point_of_sale.pot
@@ -2157,6 +2157,13 @@ msgstr ""
 msgid "Please define income account for this product: \"%s\" (id:%d)."
 msgstr ""
 
+#. module: point_of_sale
+#. openerp-web
+#: code:addons/point_of_sale/static/src/js/screens.js:2017
+#, python-format
+msgid "Please print the invoice from the backend"
+msgstr ""
+
 #. module: point_of_sale
 #: code:addons/point_of_sale/models/pos_order.py:446
 #, python-format
@@ -3051,6 +3058,13 @@ msgstr ""
 msgid "The order could not be sent to the server due to an unknown error"
 msgstr ""
 
+#. module: point_of_sale
+#. openerp-web
+#: code:addons/point_of_sale/static/src/js/screens.js:2018
+#, python-format
+msgid "The order has been synchronized earlier. Please make the invoice from the backend for the order: "
+msgstr ""
+
 #. module: point_of_sale
 #: model:ir.model.fields,help:point_of_sale.field_pos_config_iface_precompute_cash
 msgid "The payment input will behave similarily to bank payment input, and will be prefilled with the exact due amount"
diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js
index fa6215ee0e87..2fe1efe8bfa2 100644
--- a/addons/point_of_sale/static/src/js/models.js
+++ b/addons/point_of_sale/static/src/js/models.js
@@ -741,12 +741,20 @@ exports.PosModel = Backbone.Model.extend({
             transfer.pipe(function(order_server_id){    
 
                 // generate the pdf and download it
-                self.chrome.do_action('point_of_sale.pos_invoice_report',{additional_context:{ 
-                    active_ids:order_server_id,
-                }}).done(function () {
-                    invoiced.resolve();
-                    done.resolve();
-                });
+                if (order_server_id.length) {
+                    self.chrome.do_action('point_of_sale.pos_invoice_report',{additional_context:{ 
+                        active_ids:order_server_id,
+                    }}).done(function () {
+                        invoiced.resolve();
+                        done.resolve();
+                    });
+                } else {
+                    // The order has been pushed separately in batch when
+                    // the connection came back.
+                    // The user has to go to the backend to print the invoice
+                    invoiced.reject({code:401, message:'Backend Invoice', data:{order: order}});
+                    done.reject();
+                }
             });
 
             return done;
diff --git a/addons/point_of_sale/static/src/js/screens.js b/addons/point_of_sale/static/src/js/screens.js
index 82e6fa0e7b25..2b397e10a2e2 100644
--- a/addons/point_of_sale/static/src/js/screens.js
+++ b/addons/point_of_sale/static/src/js/screens.js
@@ -2012,6 +2012,17 @@ var PaymentScreenWidget = ScreenWidget.extend({
                             self.gui.show_screen('clientlist');
                         },
                     });
+                } else if (error.message === 'Backend Invoice') {
+                    self.gui.show_popup('confirm',{
+                        'title': _t('Please print the invoice from the backend'),
+                        'body': _t('The order has been synchronized earlier. Please make the invoice from the backend for the order: ') + error.data.order.name,
+                        confirm: function () {
+                            this.gui.show_screen('receipt');
+                        },
+                        cancel: function () {
+                            this.gui.show_screen('receipt');
+                        },
+                    });
                 } else if (error.code < 0) {        // XmlHttpRequest Errors
                     self.gui.show_popup('error',{
                         'title': _t('The order could not be sent'),
-- 
GitLab