The `analytic_distribution` field is a Json. It was stored temporarily as a char. Search is not available yet, so we do queries by hand when we need to search on keys. Also added a constraint on account_analytic_distribution_model, so we don't have models with accounts specific to a company when the model has no company or another company. It would cause an issue when looking at the models from another company. X-original-commit: 7064c95aa04e5138bb12ae97acfee04ebb67cc0e Part-of: odoo/odoo#103097
52 lines
2.2 KiB
Python
52 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
from odoo import models, fields, api
|
|
from odoo.tools.float_utils import float_round
|
|
|
|
|
|
class AnalyticMixin(models.AbstractModel):
|
|
_name = 'analytic.mixin'
|
|
_description = 'Analytic Mixin'
|
|
|
|
analytic_distribution = fields.Json(
|
|
'Analytic',
|
|
compute="_compute_analytic_distribution", store=True, copy=True, readonly=False,
|
|
precompute=True
|
|
)
|
|
|
|
def init(self):
|
|
# Add a gin index for json search on the keys, on the models that actually have a table
|
|
query = ''' SELECT table_name
|
|
FROM information_schema.tables
|
|
WHERE table_name=%s '''
|
|
self.env.cr.execute(query, [self._table])
|
|
if self.env.cr.dictfetchone():
|
|
query = f"""
|
|
CREATE INDEX IF NOT EXISTS {self._table}_analytic_distribution_index
|
|
ON {self._table} USING gin(analytic_distribution);
|
|
"""
|
|
self.env.cr.execute(query)
|
|
|
|
def _compute_analytic_distribution(self):
|
|
pass
|
|
|
|
def write(self, vals):
|
|
""" Format the analytic_distribution float value, so equality on analytic_distribution can be done """
|
|
decimal_precision = self.env['decimal.precision'].precision_get('Percentage Analytic')
|
|
vals = self._sanitize_values(vals, decimal_precision)
|
|
return super().write(vals)
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
""" Format the analytic_distribution float value, so equality on analytic_distribution can be done """
|
|
decimal_precision = self.env['decimal.precision'].precision_get('Percentage Analytic')
|
|
vals_list = [self._sanitize_values(vals, decimal_precision) for vals in vals_list]
|
|
return super().create(vals_list)
|
|
|
|
def _sanitize_values(self, vals, decimal_precision):
|
|
""" Normalize the float of the distribution """
|
|
if 'analytic_distribution' in vals:
|
|
vals['analytic_distribution'] = vals.get('analytic_distribution') and {
|
|
account_id: float_round(distribution, decimal_precision) for account_id, distribution in vals['analytic_distribution'].items()}
|
|
return vals
|