Skip to content
Snippets Groups Projects
analytic_code.py 6.86 KiB
Newer Older
Alexandre Allouche's avatar
Alexandre Allouche committed
# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2013 XCG Consulting (www.xcg-consulting.fr)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

Matthieu Gautier's avatar
Matthieu Gautier committed
from openerp import models, fields, api
Alexandre Allouche's avatar
Alexandre Allouche committed


Matthieu Gautier's avatar
Matthieu Gautier committed
class analytic_code(models.Model):
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
    _name = 'analytic.code'
Alexandre Allouche's avatar
Alexandre Allouche committed

    _parent_name = 'code_parent_id'
    _parent_store = True
    _parent_order = 'name'
    _order = 'parent_left'

Matthieu Gautier's avatar
Matthieu Gautier committed
    @api.depends('blacklist_ids')
    def _read_disabled_per_company(self):
        """Mark the code as disabled when it is in the blacklist (depending on
        the current user's company).
Matthieu Gautier's avatar
Matthieu Gautier committed
        company_id = self.env.user.company_id.id
Matthieu Gautier's avatar
Matthieu Gautier committed
        for anc in self:
            blacklist = (company.id for company in anc.blacklist_ids)
Matthieu Gautier's avatar
Matthieu Gautier committed
            anc.disabled_per_company = company_id in blacklist
Matthieu Gautier's avatar
Matthieu Gautier committed
    def _write_disabled_per_company(self):
        """Update the blacklist depending on the current user's company.
        """

Matthieu Gautier's avatar
Matthieu Gautier committed
        company_id = self.env.user.company_id.id
Matthieu Gautier's avatar
Matthieu Gautier committed
        for anc in self:
            blacklist = (company.id for company in anc.blacklist_ids)
Matthieu Gautier's avatar
Matthieu Gautier committed
            to_write = None
            if anc.disabled_per_company and company_id not in blacklist:
                to_write = [(4, company_id)]  # Link.
            elif not anc.disabled_per_company and company_id in blacklist:
                to_write = [(3, company_id)]  # Unlink.
Matthieu Gautier's avatar
Matthieu Gautier committed
            if to_write:
                anc.write({'blacklist_ids': to_write})
Matthieu Gautier's avatar
Matthieu Gautier committed
    def _search_disabled_per_company(self, operator, value):
        """Update the domain to take the blacklist into account (depending on
        the current user's company).
        """

Matthieu Gautier's avatar
Matthieu Gautier committed
        company_id = self.env.user.company_id.id
        # We assume the criterion was "disabled_per_company = False".
            '|',
            ('blacklist_ids', '=', False),
            ('blacklist_ids', '!=', company_id),  # Not blacklists_ids.id!
Matthieu Gautier's avatar
Matthieu Gautier committed
        if value is True:
    def _get_origin_id_selection(self):
        """Looks up the list of models that define dimensions"""
        registry = self.env.registry
        models = [
            model
            for name, model in registry.items()
            if getattr(model, '_dimension', False)
        ]

        res = [(model._name, model._description or model._name)
               for model in models]
        return res

Matthieu Gautier's avatar
Matthieu Gautier committed
    name = fields.Char(
        u"Name",
        size=128,
        translate=config.get_misc('analytic', 'translate', False),
        required=True,
    )
    nd_id = fields.Many2one(
        'analytic.dimension',
        string=u"Dimension",
        ondelete='cascade',
        required=True,
    origin_id = fields.Reference(
        _get_origin_id_selection,
        string="Original Object",
        help="The object that created this code",
        ondelete='cascade',
    )
Matthieu Gautier's avatar
Matthieu Gautier committed
    active = fields.Boolean(
        u"Active",
        help=(
            u"Determines whether an analytic code is in the referential."
        default=lambda *a: True
Matthieu Gautier's avatar
Matthieu Gautier committed
    )
    view_type = fields.Boolean(
        u"View type",
        help=(
            u"Determines whether an analytic code is not selectable (but "
            u"still in the referential)."
        default=lambda *a: False
Matthieu Gautier's avatar
Matthieu Gautier committed
    )
    blacklist_ids = fields.Many2many(
        'res.company',
        'analytic_code_company_rel',
        'code_id',
        'company_id',
        u"Blacklist",
        help=u"Companies the code is hidden in.",
    )
    disabled_per_company = fields.Boolean(
        string=u"Disable in my company",
        compute=_read_disabled_per_company,
        inverse=_write_disabled_per_company,
        search=_search_disabled_per_company,
Matthieu Gautier's avatar
Matthieu Gautier committed
        help=(
            u"Determines whether an analytic code is disabled for the "
            u"current company."
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
        ),
        default=lambda *a: False
Matthieu Gautier's avatar
Matthieu Gautier committed
    )
Matthieu Gautier's avatar
Matthieu Gautier committed
    nd_name = fields.Char(
        related='nd_id.name',
Matthieu Gautier's avatar
Matthieu Gautier committed
        string=u"Dimension Name",
        store=False
    )
    description = fields.Char(
        u"Description",
        size=512,
        translate=config.get_misc('analytic', 'translate', False),
    )
    code_parent_id = fields.Many2one(
        'analytic.code',
        u"Parent Code",
        select=True,
        ondelete='restrict',
    )
    child_ids = fields.One2many(
        'analytic.code',
        'code_parent_id',
        u"Child Codes",
    )
    parent_left = fields.Integer(u"Left parent", select=True)
    parent_right = fields.Integer(u"Right parent", select=True)
    _constraints = [
        # very useful base class constraint
        (
Matthieu Gautier's avatar
Matthieu Gautier committed
            models.Model._check_recursion,
            u"Error ! You can not create recursive analytic codes.",
            ['parent_id']
        ),
    ]

    def name_get(self, cr, uid, ids, context=None):
        if not ids:
            return []
        if isinstance(ids, (int, long)):
            ids = [ids]
Florent Aide's avatar
Florent Aide committed

        reads = self.read(cr, uid, ids,
                          [
                              'name',
                              'description'
                          ], context=context
                          )
        res = []
        for record in reads:
            name = record['name']
            if record['description']:
                name = name + ' ' + record['description']
Florent Aide's avatar
Florent Aide committed
    def name_search(self, cr, uid, name, args=None, operator='ilike',
                    context=None, limit=100):
        if not args:
            args = []

        if not context:
            context = {}

        ids = []
        if name:
            ids.extend(
                self.search(cr, uid, [
                    '|',
                    ('name', operator, name),
                    ('description', operator, name)
                ] + args, limit=limit, context=context,
                )
            )
            return self.name_get(cr, uid, ids)
        else:
            return super(analytic_code, self).name_search(
                cr, uid, name=name, args=args, operator=operator,
                context=context, limit=limit)