This question has been flagged
2 Replies
3702 Views

Hi Gets,


Here, I need to show the count of Assets which is the same sparepartslinitem added.

Please help to get count of Many2one field in "set_countofasset"

using v12 community.

from odoo import models, fields, api


class Asset(models.Model):
_name = 'asset'

name = fields.Char(string="Code", required=True)
assetname = fields.Char(string='Asset Name')
sparepartslines = fields.One2many('spareparts_lineitem', 'asset', string='Spareparts')
qtyinasset = fields.Float(String="Qty in Asset", compute='set_qtyinasset', readonly=True)
# qtyinsystem = fields.Float(String="Qty in System", compute='set_qtyinsystem', readonly=True)

@api.depends('sparepartslines.quantity')
def set_qtyinasset(self):
for rec in self:
total = 0.0
for line in self.sparepartslines:
total += line.quantity
rec.update({'qtyinasset': total})

class Spareparts(models.Model):
_name = 'spareparts'

name = fields.Char(string="Item number", required=True)
description = fields.Char(string='Description')


class Spareparts_lineitem(models.Model):
_name = 'spareparts_lineitem'

name = fields.Many2one('spareparts', string="Item number", required=True)
description = fields.Char(string='Description', related='name.description', store=True, readonly=True)
quantity = fields.Float(string='Quantity', required=True)
quantityinstore= fields.Float(string='Qty in Store')
asset = fields.Many2one('asset', string="Asset")
countofasset = fields.Float(string='Qty in Store', compute='set_countofasset', readonly=True)
Avatar
Discard
Best Answer

Little confusion above the requirement is the count of Assets which is the same spareparts added in the sparepartslinitem screen?? In that case
@api.depends('name')
def set_countofasset(self):
    for line in self:
        line.countofasset = self.search_count([('asset', '=',line.asset.id),('name','=',line.name.id)]

or see the below link

https://www.odoo.com/forum/help-1/question/how-to-count-stockable-product-in-orderline-in-quotations-148069

Avatar
Discard
Author Best Answer

Thank you so much @Prakash for your respond.

when i applied your code, it is getting the count of sparepartslines only.

But Actually I required the count of assets which is the same sparepartsline added in whole asset record.

Kindly help..

Avatar
Discard

updated answer