From 29404e096b369228242f6fb3f02f8e6fe4b7b622 Mon Sep 17 00:00:00 2001
From: Yannick Tivisse <yti@odoo.com>
Date: Thu, 11 Feb 2016 10:00:38 +0100
Subject: [PATCH] [IMP] base,base_setup: easier multi-company configuration

- The 'Extra Access Rights' is now hidden out of debug mode
- The fields 'company_id' (current company) and 'company_ids'
  (allowed companies) are hidden if there are no more than 1
  company in the database.
- If there are more than 1 company, the fields are displayed
  on the user form view and setting more than 1 company on the
  user will add him into the group 'group_multi_company'. On the
  same way, setting only one company on a user will remove him
  from this group
- The group 'group_multi_currencies' is displayed on the General
  Settings. Enbaling this feature will add all the users into this
  group. This is more accurate as this feature is more global
  than per employee.
---
 addons/account/models/res_config.py           |  2 +-
 addons/account/views/company_view.xml         |  4 +--
 addons/base_setup/res_config.py               |  4 +++
 addons/base_setup/res_config_view.xml         |  7 ++++
 addons/website_slides/view/website_slides.xml |  2 +-
 openerp/addons/base/res/res_company.py        |  9 +++--
 openerp/addons/base/res/res_company_view.xml  |  7 ++--
 openerp/addons/base/res/res_currency_view.xml | 23 ++++++++++++-
 openerp/addons/base/res/res_users.py          | 33 ++++++++++++++++---
 openerp/addons/base/res/res_users_view.xml    |  3 +-
 10 files changed, 77 insertions(+), 17 deletions(-)

diff --git a/addons/account/models/res_config.py b/addons/account/models/res_config.py
index 0c02063b0cb4..4f7d7e134f31 100644
--- a/addons/account/models/res_config.py
+++ b/addons/account/models/res_config.py
@@ -75,7 +75,7 @@ class AccountConfigSettings(models.TransientModel):
     module_account_reports = fields.Boolean("Get dynamic accounting reports")
     group_multi_currency = fields.Boolean(string='Allow multi currencies',
         implied_group='base.group_multi_currency',
-        help="Allows you multi currency environment")
+        help="Allows to work in a multi currency environment")
     group_analytic_accounting = fields.Boolean(string='Analytic accounting',
         implied_group='analytic.group_analytic_accounting',
         help="Allows you to use the analytic accounting.")
diff --git a/addons/account/views/company_view.xml b/addons/account/views/company_view.xml
index fe6bebbd2fa2..63805ceb6d93 100644
--- a/addons/account/views/company_view.xml
+++ b/addons/account/views/company_view.xml
@@ -6,10 +6,8 @@
             <field name="inherit_id" ref="base.view_company_form"/>
             <field name="model">res.company</field>
             <field name="arch" type="xml">
-                <field name="currency_id" position="after">
-                    <field name="anglo_saxon_accounting"/>
-                </field>
                 <xpath expr="//group[@name='account_grp']" position="inside">
+                    <field name="anglo_saxon_accounting"/>
                     <field name="bank_account_code_prefix" groups="base.group_no_one"/>
                     <field name="cash_account_code_prefix" groups="base.group_no_one"/>
                     <field name="accounts_code_digits" groups="base.group_no_one"/>
diff --git a/addons/base_setup/res_config.py b/addons/base_setup/res_config.py
index fa017effdab7..c04e41fa00ef 100644
--- a/addons/base_setup/res_config.py
+++ b/addons/base_setup/res_config.py
@@ -28,6 +28,10 @@ class base_config_settings(osv.osv_memory):
             help="Share your partners to all companies defined in your instance.\n"
                  " * Checked : Partners are visible for every companies, even if a company is defined on the partner.\n"
                  " * Unchecked : Each company can see only its partner (partners where company is defined). Partners not related to a company are visible for all companies."),
+        'group_multi_currency': fields.boolean('Allow multi currencies',
+            implied_group='base.group_multi_currency',
+            help="Allows to work in a multi currency environment"),
+
     }
 
     def open_company(self, cr, uid, ids, context=None):
diff --git a/addons/base_setup/res_config_view.xml b/addons/base_setup/res_config_view.xml
index 38a09f2b8347..6067af0ca52b 100644
--- a/addons/base_setup/res_config_view.xml
+++ b/addons/base_setup/res_config_view.xml
@@ -62,6 +62,13 @@
                             <label for="group_light_multi_company"/>
                         </div>
                     </group>
+                    <group>
+                        <label for="id" string="Multi Currencies"/>
+                        <div name="group_multi_currency">
+                            <field name="group_multi_currency" class="oe_inline"/>
+                            <label for="group_multi_currency"/>
+                        </div>
+                    </group>
                     <group attrs="{'invisible': [('group_light_multi_company','=',False)]}">
                         <label for="module_inter_company_rules" string="Inter company"/>
                         <div name="inter_company">
diff --git a/addons/website_slides/view/website_slides.xml b/addons/website_slides/view/website_slides.xml
index f4008a5a5450..6a11bbbd1398 100644
--- a/addons/website_slides/view/website_slides.xml
+++ b/addons/website_slides/view/website_slides.xml
@@ -201,7 +201,7 @@
                     <t t-set="header_object" t-value="category"/>
                 </t>
             </t>
-            <section t-if="not channel.promoted_slide_id and not tag and not slide_type and not search and not is_public_user and not category and display_channel_settings" class="wrap mt16 mb0">
+            <section t-if="not channel.promoted_slide_id and not tag and not slide_type and not search and not is_public_user and not category and display_channel_settings" class="wrap mt16 mb0" groups="base.group_website_designer">
                 <div class="container">
                     <div class="alert alert-danger alert-dismissable mb0">
                         <button type="button" class="close o_slides_hide_channel_settings" t-att-data-channel-id="channel.id" data-dismiss="alert">x</button>
diff --git a/openerp/addons/base/res/res_company.py b/openerp/addons/base/res/res_company.py
index 602e3c9d0aad..cf8dcf00ff29 100644
--- a/openerp/addons/base/res/res_company.py
+++ b/openerp/addons/base/res/res_company.py
@@ -112,6 +112,11 @@ class Company(models.Model):
     def _get_euro(self):
         return self.env['res.currency.rate'].search([('rate', '=', 1)], limit=1).currency_id
 
+    @api.model
+    def _get_user_currency(self):
+        currency_id = self.env['res.users'].browse(self._uid).company_id.currency_id
+        return currency_id or self._get_euro()
+
     name = fields.Char(related='partner_id.name', string='Company Name', required=True, store=True)
     parent_id = fields.Many2one('res.company', string='Parent Company', index=True)
     child_ids = fields.One2many('res.company', 'parent_id', string='Child Companies')
@@ -130,7 +135,7 @@ class Company(models.Model):
     # logo_web: do not store in attachments, since the image is retrieved in SQL for
     # performance reasons (see addons/web/controllers/main.py, Binary.company_logo)
     logo_web = fields.Binary(compute='_compute_logo_web', store=True)
-    currency_id = fields.Many2one('res.currency', string='Currency', required=True, default=lambda self: self._get_euro())
+    currency_id = fields.Many2one('res.currency', string='Currency', required=True, default=lambda self: self._get_user_currency())
     user_ids = fields.Many2many('res.users', 'res_company_users_rel', 'cid', 'user_id', string='Accepted Users')
     account_no = fields.Char(string='Account No.')
     street = fields.Char(compute='_compute_address', inverse='_inverse_street')
@@ -237,7 +242,7 @@ class Company(models.Model):
     def on_change_country(self, country_id):
         # This function is called from account/models/chart_template.py, hence decorated with `multi`.
         self.ensure_one()
-        currency_id = self._get_euro()
+        currency_id = self._get_user_currency()
         if country_id:
             currency_id = self.env['res.country'].browse(country_id).currency_id.id
         return {'value': {'currency_id': currency_id}}
diff --git a/openerp/addons/base/res/res_company_view.xml b/openerp/addons/base/res/res_company_view.xml
index 17bc2a00c0fe..763b22d22189 100644
--- a/openerp/addons/base/res/res_company_view.xml
+++ b/openerp/addons/base/res/res_company_view.xml
@@ -42,15 +42,14 @@
                                     <field name="email"/>
                                     <field name="vat"/>
                                     <field name="company_registry"/>
+                                    <field name="currency_id" options="{'no_create': True}" id="company_currency"/>
                                     <field name="parent_id"  groups="base.group_multi_company"/>
                                 </group>
                             </group>
                         </page>
                         <page name="configuration" string="Configuration">
                             <group>
-                                <group name="account_grp" string="Accounting">
-                                    <field name="currency_id"/>
-                                </group>
+                                <group name="account_grp" string="Accounting"/>
                             </group>
                         </page>
                         <page name="report" string="Report Configuration">
@@ -109,7 +108,7 @@
             <field name="view_type">form</field>
             <field name="help">Create and manage the companies that will be managed by Odoo from here. Shops or subsidiaries can be created and maintained from here.</field>
         </record>
-        <menuitem action="action_res_company_form" id="menu_action_res_company_form" parent="base.menu_users" groups="base.group_light_multi_company"/>
+        <menuitem action="action_res_company_form" id="menu_action_res_company_form" parent="base.menu_users"/>
 
         <record id="ir_open_company" model="ir.values">
             <field eval="'tree_but_open'" name="key2"/>
diff --git a/openerp/addons/base/res/res_currency_view.xml b/openerp/addons/base/res/res_currency_view.xml
index 5721688225f1..f31343b7e6fd 100644
--- a/openerp/addons/base/res/res_currency_view.xml
+++ b/openerp/addons/base/res/res_currency_view.xml
@@ -69,10 +69,16 @@
                         <button name="%(act_view_currency_rates)d" string="View Rates" type="action" attrs="{'invisible': [('active', '=', False)]}"/>
                     </header>
                     <sheet>
+                        <div class="oe_button_box" name="button_box">
+                            <button name="toggle_active" type="object"
+                                    class="oe_stat_button" icon="fa-archive">
+                                <field name="active" widget="boolean_button"
+                                    options='{"terminology": "active"}'/>
+                            </button>
+                        </div>
                         <group col="4">
                             <field name="name"/>
                             <field name="rate"/>
-                            <field name="active"/>
                         </group>
 
                         <group groups="base.group_no_one">
@@ -101,5 +107,20 @@
 
         <menuitem action="action_currency_form" id="menu_action_currency_form" parent="menu_localisation" sequence="3" groups="base.group_multi_currency"/>
 
+        <!-- We override the view res_company here because the action wasn't defined before -->
+        <record id="action_view_company_form_link_2_currencies" model="ir.ui.view">
+            <field name="name">res.company.form</field>
+            <field name="model">res.company</field>
+            <field name="inherit_id" ref="base.view_company_form"/>
+            <field name="arch" type="xml">
+                <xpath expr="//field[@id='company_currency']" position="after">
+                    <label for="id"/>
+                    <p class="text-muted">
+                        Click <a type="action" name="%(base.action_currency_form)d">here</a> to activate a new currency.
+                    </p>
+                </xpath>
+            </field>
+        </record>
+
     </data>
 </openerp>
diff --git a/openerp/addons/base/res/res_users.py b/openerp/addons/base/res/res_users.py
index 9f621a0eeb5d..00f30c4bc83d 100644
--- a/openerp/addons/base/res/res_users.py
+++ b/openerp/addons/base/res/res_users.py
@@ -182,6 +182,9 @@ class Users(models.Model):
         default_user = self.env.ref('base.default_user', raise_if_not_found=False)
         return (default_user or self.env['res.users']).groups_id
 
+    def _companies_count(self):
+        return self.env['res.company'].search_count([])
+
     partner_id = fields.Many2one('res.partner', required=True, ondelete='restrict', auto_join=True,
         string='Related Partner', help='Partner-related data of the user')
     login = fields.Char(required=True, help="Used to log into the system")
@@ -201,7 +204,8 @@ class Users(models.Model):
     login_date = fields.Datetime(related='log_ids.create_date', string='Latest connection')
     share = fields.Boolean(compute='_compute_share', string='Share User', store=True,
          help="External user with limited access, created only for the purpose of sharing data.")
-
+    companies_count = fields.Integer(compute='_compute_companies_count', string="Number of Companies", default=_companies_count)
+    
     @api.v7
     def _get_company(self, cr, uid, context=None, uid2=False):
         user = self.browse(cr, uid, uid2 or uid, context=context)
@@ -251,6 +255,12 @@ class Users(models.Model):
         for user in self:
             user.share = not user.has_group('base.group_user')
 
+    @api.multi
+    def _compute_companies_count(self):
+        companies_count = self._companies_count()
+        for user in self:
+            user.companies_count = companies_count
+
     @api.onchange('login')
     def on_change_login(self):
         if self.login and tools.single_email_re.match(self.login):
@@ -697,7 +707,7 @@ class GroupsView(models.Model):
             for app, kind, gs in self.get_groups_by_application():
                 # hide groups in categories 'Hidden' and 'Extra' (except for group_no_one)
                 attrs = {}
-                if app.xml_id in ('base.module_category_hidden', 'base.module_category_extra'):
+                if app.xml_id in ('base.module_category_hidden', 'base.module_category_extra', 'base.module_category_usability'):
                     attrs['groups'] = 'base.group_no_one'
 
                 if kind == 'selection':
@@ -769,12 +779,27 @@ class UsersView(models.Model):
     @api.model
     def create(self, values):
         values = self._remove_reified_groups(values)
-        return super(UsersView, self).create(values)
+        user = super(UsersView, self).create(values)
+        group_multi_company = self.env.ref('base.group_multi_company', False)
+        if group_multi_company and 'company_ids' in values:
+            if len(user.company_ids) <= 1 and user.id in group_multi_company.users.ids:
+                group_multi_company.write({'users': [(3, user.id)]})
+            elif len(user.company_ids) > 1 and user.id not in group_multi_company.users.ids:
+                group_multi_company.write({'users': [(4, user.id)]})
+        return user
 
     @api.multi
     def write(self, values):
         values = self._remove_reified_groups(values)
-        return super(UsersView, self).write(values)
+        res = super(UsersView, self).write(values)
+        group_multi_company = self.env.ref('base.group_multi_company', False)
+        if group_multi_company and 'company_ids' in values:
+            for user in self:
+                if len(user.company_ids) <= 1 and user.id in group_multi_company.users.ids:
+                    group_multi_company.write({'users': [(3, user.id)]})
+                elif len(user.company_ids) > 1 and user.id not in group_multi_company.users.ids:
+                    group_multi_company.write({'users': [(4, user.id)]})
+        return res
 
     def _remove_reified_groups(self, values):
         """ return `values` without reified group fields """
diff --git a/openerp/addons/base/res/res_users_view.xml b/openerp/addons/base/res/res_users_view.xml
index d0c342742247..cb693aaadea0 100644
--- a/openerp/addons/base/res/res_users_view.xml
+++ b/openerp/addons/base/res/res_users_view.xml
@@ -190,9 +190,10 @@
                         </div>
                         <notebook colspan="4">
                             <page name="access_rights" string="Access Rights">
-                                <group string="Multi Companies" groups="base.group_light_multi_company">
+                                <group string="Multi Companies" attrs="{'invisible': [('companies_count', '&lt;=', 1)]}">
                                     <field string="Allowed Companies" name="company_ids" widget="many2many_tags"/>
                                     <field string="Current Company" name="company_id" context="{'user_preference': 0}"/>
+                                    <field string="Companies count" name="companies_count" invisible="1"/>
                                 </group>
                                 <field name="groups_id"/>
                             </page>
-- 
GitLab