From 38fde3bc01e13b53abe9fe8020000c557bd2dd77 Mon Sep 17 00:00:00 2001
From: mreficent <miquel.raich@eficent.com>
Date: Mon, 26 Feb 2018 12:18:54 -0500
Subject: [PATCH] [IMP] account: support company_id in community reports
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Was PR #23259. Courtesy of Miquel Raïch (Eficent)
---
 .../report/account_aged_partner_balance.py        |  2 ++
 addons/account/report/account_balance.py          |  2 ++
 addons/account/report/account_general_ledger.py   |  2 ++
 addons/account/report/account_journal.py          |  2 ++
 addons/account/report/account_overdue_report.py   |  1 +
 addons/account/report/account_partner_ledger.py   |  2 ++
 addons/account/report/account_report_financial.py |  2 ++
 addons/account/report/account_tax.py              |  2 ++
 .../wizard/account_financial_report_view.xml      |  4 ++++
 .../account_report_aged_partner_balance_view.xml  |  1 +
 addons/account/wizard/account_report_common.py    | 15 ++++++++++++---
 .../account/wizard/account_report_common_view.xml |  2 +-
 addons/web/views/report_templates.xml             |  7 ++++---
 13 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/addons/account/report/account_aged_partner_balance.py b/addons/account/report/account_aged_partner_balance.py
index d280b6cb8504..0140d489817e 100644
--- a/addons/account/report/account_aged_partner_balance.py
+++ b/addons/account/report/account_aged_partner_balance.py
@@ -223,4 +223,6 @@ class ReportAgedPartnerBalance(models.AbstractModel):
             'time': time,
             'get_partner_lines': movelines,
             'get_direction': total,
+            'company_id': self.env['res.company'].browse(
+                data['form']['company_id'][0]),
         }
diff --git a/addons/account/report/account_balance.py b/addons/account/report/account_balance.py
index 93842ff1374d..3c54e886d13c 100644
--- a/addons/account/report/account_balance.py
+++ b/addons/account/report/account_balance.py
@@ -76,4 +76,6 @@ class ReportTrialBalance(models.AbstractModel):
             'docs': docs,
             'time': time,
             'Accounts': account_res,
+            'company_id': self.env['res.company'].browse(
+                data['form']['company_id'][0]),
         }
diff --git a/addons/account/report/account_general_ledger.py b/addons/account/report/account_general_ledger.py
index 9c3c79dacf42..922ec24ff0af 100644
--- a/addons/account/report/account_general_ledger.py
+++ b/addons/account/report/account_general_ledger.py
@@ -133,4 +133,6 @@ class ReportGeneralLedger(models.AbstractModel):
             'time': time,
             'Accounts': accounts_res,
             'print_journal': codes,
+            'company_id': self.env['res.company'].browse(
+                data['form']['company_id'][0]),
         }
diff --git a/addons/account/report/account_journal.py b/addons/account/report/account_journal.py
index 83e1b8bda8c2..083dae336a7d 100644
--- a/addons/account/report/account_journal.py
+++ b/addons/account/report/account_journal.py
@@ -115,4 +115,6 @@ class ReportJournal(models.AbstractModel):
             'sum_credit': self._sum_credit,
             'sum_debit': self._sum_debit,
             'get_taxes': self._get_taxes,
+            'company_id': self.env['res.company'].browse(
+                data['form']['company_id'][0]),
         }
diff --git a/addons/account/report/account_overdue_report.py b/addons/account/report/account_overdue_report.py
index dbfd933c99aa..4d7e4b87ede1 100644
--- a/addons/account/report/account_overdue_report.py
+++ b/addons/account/report/account_overdue_report.py
@@ -65,4 +65,5 @@ class ReportOverdue(models.AbstractModel):
             'Lines': lines_to_display,
             'Totals': totals,
             'Date': fields.date.today(),
+            'company_id': self.env['res.company'].browse([self.env.user.company_id.id]),
         }
diff --git a/addons/account/report/account_partner_ledger.py b/addons/account/report/account_partner_ledger.py
index 2da6c42abe78..e28ab7f0169a 100644
--- a/addons/account/report/account_partner_ledger.py
+++ b/addons/account/report/account_partner_ledger.py
@@ -119,4 +119,6 @@ class ReportPartnerLedger(models.AbstractModel):
             'time': time,
             'lines': self._lines,
             'sum_partner': self._sum_partner,
+            'company_id': self.env['res.company'].browse(
+                data['form']['company_id'][0]),
         }
diff --git a/addons/account/report/account_report_financial.py b/addons/account/report/account_report_financial.py
index c4a919411d00..e68e5d6d9fb2 100644
--- a/addons/account/report/account_report_financial.py
+++ b/addons/account/report/account_report_financial.py
@@ -158,4 +158,6 @@ class ReportFinancial(models.AbstractModel):
             'docs': docs,
             'time': time,
             'get_account_lines': report_lines,
+            'company_id': self.env['res.company'].browse(
+                data['form']['company_id'][0]),
         }
diff --git a/addons/account/report/account_tax.py b/addons/account/report/account_tax.py
index bcaf3697c290..97cdff1484a8 100644
--- a/addons/account/report/account_tax.py
+++ b/addons/account/report/account_tax.py
@@ -14,6 +14,8 @@ class ReportTax(models.AbstractModel):
         return {
             'data': data['form'],
             'lines': self.get_lines(data.get('form')),
+            'company_id': self.env['res.company'].browse(
+                data['form']['company_id'][0]),
         }
 
     def _sql_from_amls_one(self):
diff --git a/addons/account/wizard/account_financial_report_view.xml b/addons/account/wizard/account_financial_report_view.xml
index 3d73e7b376a2..0d5763da8124 100644
--- a/addons/account/wizard/account_financial_report_view.xml
+++ b/addons/account/wizard/account_financial_report_view.xml
@@ -28,6 +28,10 @@
                 </notebook>
             </field>
             <field name="journal_ids" position="replace"/>
+            <field name="company_id" position="replace"/>
+            <field name="date_to" position="after">
+                <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company" invisible="0"/>
+            </field>
         </field>
     </record>
 
diff --git a/addons/account/wizard/account_report_aged_partner_balance_view.xml b/addons/account/wizard/account_report_aged_partner_balance_view.xml
index 630474d5d077..42ca836364d3 100644
--- a/addons/account/wizard/account_report_aged_partner_balance_view.xml
+++ b/addons/account/wizard/account_report_aged_partner_balance_view.xml
@@ -14,6 +14,7 @@
                     <newline/>
                     <field name="result_selection" widget="radio"/>
                     <field name="target_move" widget="radio"/>
+                    <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
                 </group>
                 <field name="journal_ids" required="0" invisible="1"/>
                 <footer>
diff --git a/addons/account/wizard/account_report_common.py b/addons/account/wizard/account_report_common.py
index f7cba985485b..6b47f2a7e8ea 100644
--- a/addons/account/wizard/account_report_common.py
+++ b/addons/account/wizard/account_report_common.py
@@ -7,14 +7,22 @@ class AccountCommonReport(models.TransientModel):
     _name = "account.common.report"
     _description = "Account Common Report"
 
-    company_id = fields.Many2one('res.company', string='Company', readonly=True, default=lambda self: self.env.user.company_id)
-    journal_ids = fields.Many2many('account.journal', string='Journals', required=True, default=lambda self: self.env['account.journal'].search([]))
+    company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.user.company_id)
+    journal_ids = fields.Many2many('account.journal', string='Journals', required=True, default=lambda self: self.env['account.journal'].search([('company_id', '=', self.company_id.id)]))
     date_from = fields.Date(string='Start Date')
     date_to = fields.Date(string='End Date')
     target_move = fields.Selection([('posted', 'All Posted Entries'),
                                     ('all', 'All Entries'),
                                     ], string='Target Moves', required=True, default='posted')
 
+    @api.onchange('company_id')
+    def _onchange_company_id(self):
+        if self.company_id:
+            self.journal_ids = self.env['account.journal'].search(
+                [('company_id', '=', self.company_id.id)])
+        else:
+            self.journal_ids = self.env['account.journal'].search([])
+
     def _build_contexts(self, data):
         result = {}
         result['journal_ids'] = 'journal_ids' in data['form'] and data['form']['journal_ids'] or False
@@ -22,6 +30,7 @@ class AccountCommonReport(models.TransientModel):
         result['date_from'] = data['form']['date_from'] or False
         result['date_to'] = data['form']['date_to'] or False
         result['strict_range'] = True if result['date_from'] else False
+        result['company_id'] = data['form']['company_id'][0] or False
         return result
 
     def _print_report(self, data):
@@ -33,7 +42,7 @@ class AccountCommonReport(models.TransientModel):
         data = {}
         data['ids'] = self.env.context.get('active_ids', [])
         data['model'] = self.env.context.get('active_model', 'ir.ui.menu')
-        data['form'] = self.read(['date_from', 'date_to', 'journal_ids', 'target_move'])[0]
+        data['form'] = self.read(['date_from', 'date_to', 'journal_ids', 'target_move', 'company_id'])[0]
         used_context = self._build_contexts(data)
         data['form']['used_context'] = dict(used_context, lang=self.env.context.get('lang') or 'en_US')
         return self._print_report(data)
diff --git a/addons/account/wizard/account_report_common_view.xml b/addons/account/wizard/account_report_common_view.xml
index 569a95b82ad2..be111dba1949 100644
--- a/addons/account/wizard/account_report_common_view.xml
+++ b/addons/account/wizard/account_report_common_view.xml
@@ -6,7 +6,6 @@
         <field name="model">account.common.report</field>
         <field name="arch" type="xml">
         <form string="Report Options">
-            <field name="company_id" invisible="1"/>
             <group col="4">
                 <field name="target_move" widget="radio"/>
                 <field name="date_from"/>
@@ -14,6 +13,7 @@
             </group>
             <group>
                 <field name="journal_ids" widget="many2many_tags" options="{'no_create': True}"/>
+                <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
             </group>
             <footer>
                 <button name="check_report" string="Print" type="object" default_focus="1" class="oe_highlight"/>
diff --git a/addons/web/views/report_templates.xml b/addons/web/views/report_templates.xml
index 7f1f11089929..f6a79d48d77c 100644
--- a/addons/web/views/report_templates.xml
+++ b/addons/web/views/report_templates.xml
@@ -332,10 +332,11 @@
 
     <template id="internal_layout">
         <!-- Multicompany -->
-        <t t-if="o and 'company_id' in o">
-            <t t-set="company" t-value="o.company_id"/>
+        <t t-if="company_id">
+            <t t-set="company" t-value="company_id"/>
+            <t t-set="res_company" t-value="company_id"/>
         </t>
-        <t t-if="not o or not 'company_id' in o">
+        <t t-if="not company_id">
             <t t-set="company" t-value="res_company"/>
         </t>
 
-- 
GitLab