1
votes

I am trying to create a setting page for one of my module by res.config.settings inherit. I read this page (https://www.odoo.com/de_DE/forum/hilfe-1/question/create-a-custom-configuration-page-for-custom-module-51842) and the Odoo's documentation, I've tried many things but none of those worked.

I managed to set/get the values, but it's impossible to make them multi-company dependent.

I am using Odoo 13 on Ubuntu 18.04.3 LTS

Does anyone know how to resolve this issue?

Here is my model:

# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.exceptions import UserError
from odoo import exceptions


class res_config_settings(models.TransientModel):
    _inherit = 'res.config.settings'
    _check_company_auto = True

    is_enabled = fields.Boolean(default=False, string="Is Enabled?", company_dependent=True, check_company=True)
    company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company)
    base_status = fields.Many2one('employee_status.status', string="Default Status", company_dependent=True, check_company=True)
    is_reset = fields.Boolean(default=False, string="Enable Reset?", company_dependent=True, check_company=True)
    reset_month = fields.Selection(
        [('1', 'January'), ('2', 'February'), ('3', 'March'), ('4', 'April'),
        ('5', 'May'), ('6', 'June'), ('7', 'July'), ('8', 'August'),
        ('9', 'September'), ('10', 'October'), ('11', 'November'), ('12', 'December'),
        ], string="Month of the reset", company_dependent=True, check_company=True)
    reset_status = fields.Many2one('employee_status.status', string="Reset Default Status", company_dependent=True, check_company=True)



    @api.model
    @api.depends_context('force_company')
    def get_values(self):
        res = super(res_config_settings, self).get_values()

        is_enabled = self.env['ir.config_parameter'].sudo().get_param('employee_status.is_enabled') 
        if is_enabled:
            base_status = int(self.env['ir.config_parameter'].sudo().get_param('employee_status.base_status'))
            is_reset = self.env['ir.config_parameter'].sudo().get_param('employee_status.is_reset')
            reset_month = self.env['ir.config_parameter'].sudo().get_param('employee_status.reset_month')
            reset_status = int(self.env['ir.config_parameter'].sudo().get_param('employee_status.reset_status')

            res.update(
                base_status = base_status,
                is_reset = is_reset,
                reset_month = reset_month,
                reset_status = reset_status,
            )
        return res


    @api.model
    @api.depends_context('force_company')
    def set_values(self):
        super(res_config_settings, self).set_values()
        param = self.env['ir.config_parameter'].sudo()

        if not self.base_status:
            raise exceptions.ValidationError('Please give default status.')


        if self.is_reset:
            if not self.reset_month:
                raise exceptions.ValidationError('Please give a month for the reset.')
                return
            if not self.reset_status:
                raise exceptions.ValidationError('Please give a reset status.')
                return


        field1 = self.base_status and self.base_status.id or False
        field2 = self.is_reset or False
        field3 = self.reset_month or False
        field4 = self.reset_status and self.reset_status.id or False

        allEmployees = self.env['hr.employee'].search([])
        for employee in allEmployees:

            employee.employee_status = field1

        param.set_param('employee_status.is_enabled', True)
        param.set_param('employee_status.base_status', field1)
        param.set_param('employee_status.is_reset', field2)
        param.set_param('employee_status.reset_month', field3)
        param.set_param('employee_status.reset_status', field4)

And the view:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <record id="employee_status_settings_view" model="ir.ui.view">
        <field name="name">Employee Status Configuration</field>
        <field name="model">res.config.settings</field>
        <field name="inherit_id" ref="base.res_config_settings_view_form" />
        <field name="arch" type="xml">
            <xpath expr="//div[hasclass('settings')]" position="inside">
                <div class="app_settings_block" data-string="Employee Status Data-String" string="Employee Status" data-key="employee_status">
                    <h2>Employee Default Status</h2>
                    <div class="row mt16 o_settings_container">
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="base_status"/>
                                <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
                                <div class="row">
                                    <div class="text-muted col-lg-8">
                                        The default Status for your employees.
                                    </div>
                                </div>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="base_status" required="1"
                                            class="o_light_label" groups="base.group_multi_company"
                                            domain="[('company_id', '=', company_id)]"
                                            context="{'default_company_id': company_id}"/>                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>


                    <h2>Status Reset</h2>
                    <div class="row mt16 o_settings_container">
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="is_reset"/>
                                <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="is_reset"
                                            class="o_light_label" groups="base.group_multi_company"
                                            domain="[('company_id', '=', company_id)]"
                                            context="{'default_company_id': company_id}"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>


                    <div class="row mt16 o_settings_container">
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="reset_month"/>
                                <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
                                <div class="row">
                                    <div class="text-muted col-lg-8">
                                      Select at which month's begin the reset occurs.
                                    </div>
                                </div>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="reset_month"
                                            class="o_light_label" groups="base.group_multi_company"
                                            domain="[('company_id', '=', company_id)]"
                                            context="{'default_company_id': company_id}"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row mt16 o_settings_container">
                        <div class="col-12 col-lg-6 o_setting_box">
                            <div class="o_setting_right_pane">
                                <label for="reset_status"/>
                                <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/>
                                <div class="row">
                                    <div class="text-muted col-lg-8">
                                      Set which status is set when the reset occurs.
                                    </div>
                                </div>
                                <div class="content-group">
                                    <div class="mt16">
                                        <field name="reset_status"
                                            class="o_light_label" groups="base.group_multi_company"
                                            domain="[('company_id', '=', company_id)]"
                                            context="{'default_company_id': company_id}"/>                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </xpath>
        </field>
    </record>

    <record id="employee_status_settings_action" model="ir.actions.act_window">
        <field name="name">Employee Status Configuration</field>
        <field name="res_model">res.config.settings</field>
        <field name="view_id" ref="employee_status_settings_view"/>
        <field name="view_mode">form</field>
        <field name="target">inline</field>
        <field name="context">{'module' : 'employee_status'}</field>
    </record>

    <menuitem id="employee_status.menu" name="Status"
            parent="hr.menu_hr_root"/>

    <menuitem id="employee_status.submenu_config" name="Status Configuration"
            parent="employee_status.menu"
            action="employee_status_settings_action"/>
</odoo>

Thanks for your help!

1
Hi Loïc, did you find anything on this?dirtyhandsphp
Hi @Loïc did you find a solution to this ? I see that ir_property table (where the parameters are stored) has column for column_id but the code (get / set param) do not take any such parameter (I guess they save it directly under the current company) but it is not filtering on it. I'll try to override those methods to take into account a context just for thisqdev
Hi to you both, the projet I was previously working on has been cancelled. Therefore, I did not try to fix this problem. But this post can maybe help someone else in the future! CheersLoïc G

1 Answers

0
votes

Maybe a useless question, but did you create the security like here? The rest looks like odoo documented... i am also new to this topic, hopefully someelse can help here, would be interesting for me, too. cheers!