Skip to Content
Menu
This question has been flagged
2 Replies
2088 Views

I am trying to add a new field on account.analytic.account. I have developed this module in python but I d'n't kbow how the filter product_uom_id for hours only:

# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models, fields

class mod_analytic_account(models.Model):
_inherit = 'account.analytic.account'

analytic_lines_ids = fields.One2many('account.analytic.line', 'account_id' , string = 'Analytic Account', store = True)

hores_realitzades=fields.Float(String = 'Hores realitzades',
compute="sum_hores_realitzades",store = True,
)
 

@api.multi
def sum_hores_realitzades(self):
for sel in self:
visited = []
total = 0.0
for il in sel.analytic_lines_ids.filtered(lambda r: r.product_uom_id == 'product.product_uom_hour'):
if il.id not in visited:
total += il.unit_amount
visited.append(il.id)
sel.hores_realitzades = total



@api.multi
def sum_hores_realitzades(self):
for il in sel.analytic_lines_ids.filtered(lambda r: r.product_uom_id == 'product.product_uom_hour'):
visited = []
total = 0.0
for il in sel.analytic_lines_ids:
if il.id not in visited:
total += il.unit_amount
visited.append(il.id)
sel.hores_realitzades = total


Avatar
Discard
Best Answer

hello,

Please try this code, 

hores_realitzades=fields.Float(String = 'Hores realitzades', compute="sum_hores_realitzades",store = True)


@api.depends('analytic_lines_ids','analytic_lines_ids.unit_amount')
def sum_hores_realitzades(self):
    for sel in self:
        visited = []
        total = 0.0
        for il in sel.analytic_lines_ids.filtered(lambda r: r.product_uom_id.id == self.env.ref('product.product_uom_hour').id):
            if il.id not in visited:
                total += il.unit_amount
                visited.append(il.id)
        sel.hores_realitzades = total

Hope it will work for you,

thanks,

Avatar
Discard
Author Best Answer

Thanks

Avatar
Discard
Related Posts Replies Views Activity
2
Aug 21
4689
2
Jul 24
424
2
May 24
386
1
Feb 24
319
1
Nov 22
3401