This question has been flagged
1 Reply
2638 Views

Odoo 12

I have a calculation in my model A that gets a total result from a line

Model A

_inherit = 'stock.landed.cost'


total_former_cost = fields.Float(string="total", compute='_compute_total_former_cost', compute_sudo=True, store=True)


@api.depends('valuation_adjustment_lines.former_cost')

def _compute_total_former_cost(self)

    for gastos in self: 

    gastos.total_former_cost =

    round(sum(gastos.valuation_adjustment_lines.mapped(''former_cost)), 2)


Example_Result = 20


I need to capture that result, store it and use it in another line calculation


Model B

external_model = fields.Many2one('stock.landed.cost', string="gastos")  

total = fields.Float(string="porrateo", compute="def_compute_prueba", compute_sudo=True, store=True)


@api.one

@api.depends('external_model.total_former_cost')

def _compute_prueba(self):

    self.total = self.external_model.total_former_cost + 5


Expected result = 5 + 20 = 25

Result = 5


The problem is that no matter what i do, or what i try, i always get 0 from the function, i have tried other fields like, the product.product.qty or name and i get it right, so the inheriting is ok.

Do someone have an idea how to store and use that total, what should i try?.

Avatar
Discard
Best Answer

Hello Kevin Cruz,

I find another way please try this,


external_model = fields.Many2one('stock.landed.cost', string="gastos")

total_former_cost = fields.Float(string="total",related='external_model.total_former_cost')

total = fields.Float(string="porrateo",compute="def_compute_prueba", compute_sudo=True, store=True)


@api.depends('total_former_cost')

def _compute_prueba(self):

self.total = self.external_model.total_former_cost + 5

Regards,




Email:      odoo@aktivsoftware.com  

Skype: kalpeshmaheshwari

   

Avatar
Discard
Author

Thanks for answering, i tried that way and didn't work, but thanks to that, i realized that, i was doing wrong, i was trying to relate the model stock.landed.cost and stock.valuation.ajustment.cost but that is already done, in the original models they already related with their respective fields... so i used them to call the fields and compute them.

here i leave the code if usefull to someone

class StockLandedCostSv(models.Model):

_inherit = 'stock.valuation.adjustment.lines'

porrateo_int = fields.Float(string="Porrateo/ GI", required=False, )

amount_total = fields.Float(string="total", related='cost_id.amount_total')

@api.multi

@api.depends('cost_id.total_former_cost', 'porrateo_calc', 'amount_total', 'former_cost')

def _compute_prueba(self):

for calculo in self:

calculo.porrateo_calc = (calculo.amount_total / calculo.cost_id.total_former_cost) * calculo.former_cost

porrateo_calc = fields.Float(string="porrateo", compute="_compute_prueba", compute_sudo=True, store=True)