Hi, i'm testing and i write this code:
Function read_group is the key!
# -*- coding: utf-8 -*-
import logging, re
from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.osv.expression import expression
from pprint import pformat
_logger = logging.getLogger(__name__)
class SaleReport(models.Model):
_inherit = "sale.report"
# @api.depends('partner_id')
def _compute_objetivo_zona(self):
for rec in self:
_logger.warning("sadjhasjkdasjdkjjjkjhkjhk")
_logger.warning(rec.partner_id.x_zonas.x_studio_objetivos)
objetivo_zona = 0.0
partner = rec.partner_id
zone_id = partner.x_zonas
if zone_id:
objetivo_zona = zone_id.x_studio_objetivos
_logger.warning(objetivo_zona)
_logger.warning("sadjhasjkdasjdkjjjkjhkjhk")
rec.update({
'objetivo_zona': 5.0
})
zone_id = fields.Many2one('x_zonas', 'Zonas', readonly=True)
objetivo_zona = fields.Float('Objetivo', default=0.0, readonly=True)
def _select(self):
return super(SaleReport, self)._select() + ", zone.x_studio_objetivos as objetivo_zona"
@api.model
def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
_logger.warning("HACK ESCONDER CAMPO")
_logger.warning(pformat(fields))
has_field_objetivo = False
newfields = []
# Buscar campos objetivos para no molestar en suma
for field in fields:
if field != 'objetivo_zona:sum':
newfields.append(field)
else:
has_field_objetivo = True
res = super(SaleReport, self).read_group(domain, newfields, groupby, offset=offset, limit=limit,
orderby=orderby,
lazy=lazy)
_logger.warning("RES ANTES")
_logger.warning(res)
if has_field_objetivo:
zones = {}
zone_total = 0
# Procesar Campos
for dato in res:
_logger.warning("DATO INDIVIDUAL")
_logger.warning(pformat(dato))
if 'zone_id' in dato:
if dato['zone_id']:
zone_id = dato['zone_id'][0]
if zone_id in zones:
zone = zones[zone_id]
dato['objetivo_zona'] = 0
else:
zone = self.env['x_zonas'].search([('id', '=', dato['zone_id'][0])])
zones[zone_id] = zone
if zone:
dato['objetivo_zona'] = zone.x_studio_objetivos
if zone:
zone_total += zone.x_studio_objetivos
_logger.warning(zone_total)
_logger.warning("RES FINAL")
_logger.warning(res)
return res
def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
fields['zone_id'] = ", partner.x_studio_zona as zone_id"
fields['objetivo_zona'] = ", zone.x_studio_objetivos as objetivo_zona"
#
from_clause = 'left join x_zonas zone on (partner.x_studio_zona = x_zonas.id)'
groupby += ', partner.x_studio_zona'
return super(SaleReport, self)._query(with_clause, fields, groupby, from_clause)