<commit_before># -*- coding:utf-8 -*-
##############################################################################
#
#    Copyright (C) 2015 <NAME>. All Rights Reserved.
#
#    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/>.
#
##############################################################################

from openerp import fields, models, api


class HrSalaryRule(models.Model):
    _inherit = 'hr.salary.rule'

    employee_benefit_ids = fields.Many2many(
        'hr.employee.benefit.category',
        'salary_rule_employee_benefit_rel',
        'salary_rule_id', 'benefit_id', 'Salary Rules',
    )

    @api.multi
    def sum_benefits(self, payslip, **kwargs):
        """
        Method used to sum the employee benefits computed on the payslip

        Because there are many possible parameters and that the module
        needs to be inherited easily, arguments are passed through kwargs

        :param codes: The type of benefit over which to sum
        :type codes: list of string or single string

        :param employer: If True, sum over the employer contribution.
        If False, sum over the employee contribution

        Exemple
        -------
        payslip.compute_benefits(payslip, employer=True)
        Will return the employer contribution for the pay period
        """
        self.ensure_one()

        benefits = self._filter_benefits(payslip, **kwargs)

        employer = kwargs.get('employer', False)

        if employer:
            res = sum([ben.employer_amount for ben in benefits])
        else:
            res = sum([ben.employee_amount for ben in benefits])

        return res

    @api.multi
    def _filter_benefits(self, payslip, **kwargs):
        """ Filter the benefit records on the payslip
        :rtype: list of hr.payslip.benefit.line browse records
        """
        self.ensure_one()

        benefits = payslip.benefit_line_ids
        benefit_codes = kwargs.get('codes', False)

        if benefit_codes:
            if isinstance(benefit_codes, str):
                benefit_codes = [benefit_codes]

            return [
                ben for ben in benefits
                if ben.category_id.code in benefit_codes
            ]

        # If the salary rule is linked to no benefit category,
        # by default it accepts every categories.
        if self.employee_benefit_ids:
            return [
                ben for ben in benefits
                if ben.category_id in self.employee_benefit_ids
            ]

        return benefits<commit_msg>Fix methods to make better use of the new api<commit_after># -*- coding:utf-8 -*-
##############################################################################
#
#    Copyright (C) 2015 <NAME>. All Rights Reserved.
#
#    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/>.
#
##############################################################################

from openerp import fields, models, api


class HrSalaryRule(models.Model):
    _inherit = 'hr.salary.rule'

    employee_benefit_ids = fields.Many2many(
        'hr.employee.benefit.category',
        'salary_rule_employee_benefit_rel',
        'salary_rule_id', 'benefit_id', 'Salary Rules',
    )

    @api.multi
    def sum_benefits(self, payslip, **kwargs):
        """
        Method used to sum the employee benefits computed on the payslip

        Because there are many possible parameters and that the module
        needs to be inherited easily, arguments are passed through kwargs

        :param codes: The type of benefit over which to sum
        :type codes: list of string or single string

        :param employer: If True, sum over the employer contribution.
        If False, sum over the employee contribution

        Exemple
        -------
        payslip.compute_benefits(payslip, employer=True)
        Will return the employer contribution for the pay period
        """
        self.ensure_one()

        benefits = self._filter_benefits(payslip, **kwargs)

        employer = kwargs.get('employer', False)

        if employer:
            res = sum(ben.employer_amount for ben in benefits)
        else:
            res = sum(ben.employee_amount for ben in benefits)

        return res

    @api.multi
    @api.returns('hr.payslip.benefit.line')
    def _filter_benefits(self, payslip, **kwargs):
        """ Filter the benefit records on the payslip
        :rtype: record set of hr.payslip.benefit.line
        """
        self.ensure_one()

        benefits = payslip.benefit_line_ids
        benefit_codes = kwargs.get('codes', False)

        if benefit_codes:
            if isinstance(benefit_codes, str):
                benefit_codes = [benefit_codes]

            return benefits.filtered(
                lambda b: b.category_id.code in benefit_codes)

        # If the salary rule is linked to no benefit category,
        # by default it accepts every categories.
        if self.employee_benefit_ids:
            return benefits.filtered(
                lambda b: b.category_id in self.employee_benefit_ids)

        return benefits
{'fraction_non_alphanumeric': 0.11787912261322392, 'fraction_numerical': 0.002209247277891747, 'mean_word_length': 2.5566778900112235, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7548238', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1598, 'n_words': 614}