From c8d5b4a0cfc070c05c3f86ae6cc2b7395532c353 Mon Sep 17 00:00:00 2001
From: Pierre Masereel <pim@odoo.com>
Date: Wed, 6 Nov 2019 10:05:12 +0000
Subject: [PATCH] [FIX] point_of_sale: pos order to invoice wrong value

When a pos order is sent to the server, the field 'to_invoice' is never
sent to the server which lead to always having a value to 'false'.

We are now sending correctly this value to the server, and we can also
base the fact to create an invoice on the value of each order.

Because beeing offline could lead to create an order that should be
invoiced as one that should not be.

closes odoo/odoo#39843

Signed-off-by: pimodoo <pimodoo@users.noreply.github.com>
---
 addons/point_of_sale/models/pos_order.py     | 3 +--
 addons/point_of_sale/static/src/js/models.js | 1 +
 addons/point_of_sale/tests/common.py         | 1 +
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/addons/point_of_sale/models/pos_order.py b/addons/point_of_sale/models/pos_order.py
index c6738ee82747..ec6a7914f805 100644
--- a/addons/point_of_sale/models/pos_order.py
+++ b/addons/point_of_sale/models/pos_order.py
@@ -112,7 +112,6 @@ class PosOrder(models.Model):
         :type existing_order: pos.order.
         :returns number pos_order id
         """
-        to_invoice = order['to_invoice'] if not draft else False
         order = order['data']
         pos_session = self.env['pos.session'].browse(order['pos_session_id'])
         if pos_session.state == 'closing_control' or pos_session.state == 'closed':
@@ -138,7 +137,7 @@ class PosOrder(models.Model):
             except Exception as e:
                 _logger.error('Could not fully process the POS Order: %s', tools.ustr(e))
 
-        if to_invoice:
+        if pos_order.to_invoice and pos_order.state == 'paid':
             pos_order.action_pos_order_invoice()
             pos_order.account_move.sudo().with_context(force_company=self.env.user.company_id.id).post()
 
diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js
index 142e82b454ca..e89e167365a4 100644
--- a/addons/point_of_sale/static/src/js/models.js
+++ b/addons/point_of_sale/static/src/js/models.js
@@ -2388,6 +2388,7 @@ exports.Order = Backbone.Model.extend({
             creation_date: this.validation_date || this.creation_date, // todo: rename creation_date in master
             fiscal_position_id: this.fiscal_position ? this.fiscal_position.id : false,
             server_id: this.server_id ? this.server_id : false,
+            to_invoice: this.to_invoice ? this.to_invoice : false,
         };
         if (!this.is_paid && this.user_id) {
             json.user_id = this.user_id;
diff --git a/addons/point_of_sale/tests/common.py b/addons/point_of_sale/tests/common.py
index 98e5eda1125f..38e4de65f6ce 100644
--- a/addons/point_of_sale/tests/common.py
+++ b/addons/point_of_sale/tests/common.py
@@ -389,6 +389,7 @@ class TestPoSCommon(TransactionCase):
                 'statement_ids': payments,
                 'uid': uid,
                 'user_id': self.pos_manager.id,
+                'to_invoice': is_invoiced,
             },
             'id': uid,
             'to_invoice': is_invoiced,
-- 
GitLab