<commit_before># Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo import _, api, exceptions, fields, models


class Product(models.Model):
    _inherit = "product.product"

    is_cost_type = fields.Boolean(string="Is Cost Driver")

    activity_cost_ids = fields.One2many(
        "activity.cost.rule",
        "parent_id",
        string="Activity Costs",
        help="This product will also generate analytic items for these Activity Costs",
    )

    @api.constrains("is_cost_type", "activity_cost_ids")
    def constrains_is_cost_type(self):
        for product in self:
            if not product.is_cost_type and product.activity_cost_ids:
                raise exceptions.ValidationError(
                    _("Can't have Activity Costs set if it is not a Cost Driver.")
                )

    @api.onchange("activity_cost_ids")
    def onchange_for_standard_price(self):
        "Rollup Activity Costs to parent Cost Type"
        for product in self.filtered("is_cost_type"):
            if product.type in ["consu", "service"]:
                product.standard_price = sum(
                    x.standard_price * x.factor
                    for x in product.sudo().activity_cost_ids
                )
<commit_msg>[FIX] analytic_activity_based_cost: rounding issue<commit_after># Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo import _, api, exceptions, fields, models
from odoo.tools.float_utils import float_round


class Product(models.Model):
    _inherit = "product.product"

    is_cost_type = fields.Boolean(string="Is Cost Driver")

    activity_cost_ids = fields.One2many(
        "activity.cost.rule",
        "parent_id",
        string="Activity Costs",
        help="This product will also generate analytic items for these Activity Costs",
    )

    @api.constrains("is_cost_type", "activity_cost_ids")
    def constrains_is_cost_type(self):
        for product in self:
            if not product.is_cost_type and product.activity_cost_ids:
                raise exceptions.ValidationError(
                    _("Can't have Activity Costs set if it is not a Cost Driver.")
                )

    @api.onchange("activity_cost_ids")
    def onchange_for_standard_price(self):
        "Rollup Activity Costs to parent Cost Type"
        for product in self.filtered("is_cost_type"):
            if product.type in ["consu", "service"]:
                product.standard_price = sum(
                    float_round(
                        x.standard_price * x.factor,
                        precision_rounding=product.currency_id.rounding,
                    )
                    for x in product.sudo().activity_cost_ids
                )

{'fraction_non_alphanumeric': 0.08958407394241023, 'fraction_numerical': 0.004976892996800568, 'mean_word_length': 2.3380782918149468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1206882', 'n_tokens_mistral': 813, 'n_tokens_neox': 756, 'n_words': 205}