From 2721dd522c500a91e639096f9277f8049fc680e5 Mon Sep 17 00:00:00 2001
From: "Lucas Perais (lpe)" <lpe@odoo.com>
Date: Thu, 19 Sep 2019 11:35:15 +0000
Subject: [PATCH] [FIX] base, account, product, hr_holidays: document layout
 save and print

In an onboarding situation, the company doesn't have a logo
make an invoice, send and print, print

The document layout editor's layout opens, because nothing is set up
on the company

Click Save

Before this commit, it was impossible to make the invoice print
because each time the document layout was displayed

After this commit, the invoice prints when clicking on Save
There is no default external layout for main_company

Also, the heuristic used to evaluate whether a company
has been set up, onboardingly speaking, has changed.
Before we used the existence of the logo, after we check if
a layout has been setup. When saving the document layout
modal, a report layout is written on the company

So, practically, the document layout modal only appears once
when trying to print invoices (or other documents)

closes odoo/odoo#37137

Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
---
 addons/account/models/company.py              |  2 +-
 addons/account/wizard/base_document_layout.py |  3 ++-
 .../hr_holidays/tests/test_holidays_flow.py   |  1 +
 .../product/tests/test_product_pricelist.py   |  6 ++++++
 addons/web/__manifest__.py                    |  1 -
 addons/web/data/res_company.xml               |  8 --------
 odoo/addons/base/models/ir_actions_report.py  | 19 ++++++++++++++-----
 .../base/wizard/base_document_layout.py       |  2 +-
 8 files changed, 25 insertions(+), 17 deletions(-)
 delete mode 100644 addons/web/data/res_company.xml

diff --git a/addons/account/models/company.py b/addons/account/models/company.py
index e8376d6e350c..0c1e652b775e 100644
--- a/addons/account/models/company.py
+++ b/addons/account/models/company.py
@@ -488,7 +488,7 @@ class ResCompany(models.Model):
 
     def action_save_onboarding_invoice_layout(self):
         """ Set the onboarding step as done """
-        if bool(self.logo) and self.logo != self._get_logo():
+        if bool(self.external_report_layout_id):
             self.set_onboarding_step_done('account_onboarding_invoice_layout_state')
 
     def action_save_onboarding_sale_tax(self):
diff --git a/addons/account/wizard/base_document_layout.py b/addons/account/wizard/base_document_layout.py
index 7ed9a4586f26..ab76e69aaa6e 100644
--- a/addons/account/wizard/base_document_layout.py
+++ b/addons/account/wizard/base_document_layout.py
@@ -5,6 +5,7 @@ class BaseDocumentLayout(models.TransientModel):
     _inherit = 'base.document.layout'
 
     def document_layout_save(self):
-        super(BaseDocumentLayout, self).document_layout_save()
+        res = super(BaseDocumentLayout, self).document_layout_save()
         for wizard in self:
             wizard.company_id.action_save_onboarding_invoice_layout()
+        return res
diff --git a/addons/hr_holidays/tests/test_holidays_flow.py b/addons/hr_holidays/tests/test_holidays_flow.py
index f165aee200b8..4f97fb5ac3f8 100644
--- a/addons/hr_holidays/tests/test_holidays_flow.py
+++ b/addons/hr_holidays/tests/test_holidays_flow.py
@@ -210,6 +210,7 @@ class TestHolidaysFlow(TestHrHolidaysBase):
             'emp': [(6, 0, [self.ref('hr.employee_admin'), self.ref('hr.employee_qdp'), self.ref('hr.employee_al')])],
             'holiday_type': 'Approved'
         }
+        self.env.company.external_report_layout_id = self.env.ref('web.external_layout_standard').id
         test_reports.try_report_action(self.env.cr, self.env.uid, 'action_hr_holidays_summary_employee', wiz_data=data_dict, context=ctx, our_module='hr_holidays')
 
     def test_sql_constraint_dates(self):
diff --git a/addons/product/tests/test_product_pricelist.py b/addons/product/tests/test_product_pricelist.py
index cb13db1b3274..acad92c3930b 100644
--- a/addons/product/tests/test_product_pricelist.py
+++ b/addons/product/tests/test_product_pricelist.py
@@ -3,6 +3,7 @@
 
 from odoo.tests.common import TransactionCase
 from odoo.tools import float_compare, test_reports
+from odoo.exceptions import UserError
 
 
 class TestProductPricelist(TransactionCase):
@@ -124,4 +125,9 @@ class TestProductPricelist(TransactionCase):
             'qty5': 30,
             'price_list': self.customer_pricelist.id,
         }
+
+        with self.assertRaises(UserError):
+            test_reports.try_report_action(self.cr, self.uid, 'action_product_price_list', wiz_data=data_dict, context=ctx, our_module='product')
+
+        self.env.company.external_report_layout_id = self.env.ref('web.external_layout_standard').id
         test_reports.try_report_action(self.cr, self.uid, 'action_product_price_list', wiz_data=data_dict, context=ctx, our_module='product')
diff --git a/addons/web/__manifest__.py b/addons/web/__manifest__.py
index 0faf273b4fa9..2321ff7389c8 100644
--- a/addons/web/__manifest__.py
+++ b/addons/web/__manifest__.py
@@ -18,7 +18,6 @@ This module provides the core of the Odoo Web Client.
         'security/ir.model.access.csv',
         'views/webclient_templates.xml',
         'views/report_templates.xml',
-        'data/res_company.xml',
         'data/report_layout.xml',
     ],
     'qweb': [
diff --git a/addons/web/data/res_company.xml b/addons/web/data/res_company.xml
deleted file mode 100644
index 44db6af89312..000000000000
--- a/addons/web/data/res_company.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<odoo>
-    <data noupdate="1">
-         <record id="base.main_company" model="res.company">
-            <field name="external_report_layout_id" ref="web.external_layout_standard"/>
-         </record>
-    </data>
-</odoo>
\ No newline at end of file
diff --git a/odoo/addons/base/models/ir_actions_report.py b/odoo/addons/base/models/ir_actions_report.py
index a3d354cfabc0..4f0e96824fbf 100644
--- a/odoo/addons/base/models/ir_actions_report.py
+++ b/odoo/addons/base/models/ir_actions_report.py
@@ -18,6 +18,7 @@ import lxml.html
 import tempfile
 import subprocess
 import re
+import json
 
 from lxml import etree
 from contextlib import closing
@@ -790,10 +791,6 @@ class IrActionsReport(models.Model):
         :param docids: id/ids/browserecord of the records to print (if not used, pass an empty list)
         :param report_name: Name of the template to generate an action for
         """
-        discard_logo_check = self.env.context.get('discard_logo_check')
-        if self.env.is_admin() and ((not self.env.company.external_report_layout_id) or (not discard_logo_check and not self.env.company.logo)) and config:
-            return self.env.ref('base.action_base_document_layout_configurator').read()[0]
-
         context = self.env.context
         if docids:
             if isinstance(docids, models.Model):
@@ -804,7 +801,7 @@ class IrActionsReport(models.Model):
                 active_ids = docids
             context = dict(self.env.context, active_ids=active_ids)
 
-        return {
+        report_action = {
             'context': context,
             'data': data,
             'type': 'ir.actions.report',
@@ -813,3 +810,15 @@ class IrActionsReport(models.Model):
             'report_file': self.report_file,
             'name': self.name,
         }
+
+        discard_logo_check = self.env.context.get('discard_logo_check')
+        if self.env.is_admin() and not self.env.company.external_report_layout_id and config and not discard_logo_check:
+            action = self.env.ref('base.action_base_document_layout_configurator').read()[0]
+            ctx = action.get('context')
+            py_ctx = json.loads(ctx) if ctx else {}
+            report_action['close_on_report_download'] = True
+            py_ctx['report_action'] = report_action
+            action['context'] = py_ctx
+            return action
+
+        return report_action
diff --git a/odoo/addons/base/wizard/base_document_layout.py b/odoo/addons/base/wizard/base_document_layout.py
index afbe91f3c8e9..e41ed375d90d 100644
--- a/odoo/addons/base/wizard/base_document_layout.py
+++ b/odoo/addons/base/wizard/base_document_layout.py
@@ -186,4 +186,4 @@ class BaseDocumentLayout(models.TransientModel):
 
     def document_layout_save(self):
         # meant to be overridden
-        pass
+        return self.env.context.get('report_action') or {'type': 'ir.actions.act_window_close'}
-- 
GitLab