Skip to Content
Menu
This question has been flagged
3 Replies
5530 Views

Hi, I am using Odoo v11.0 CE on Win10

I inherits from stock move and add some fields 

every thing on client form is perfect

but when click on save button all values resets to 0.

Here is My Code

Thanks in advance

from odoo import models,fields, api
from odoo.addons import decimal_precision as dp
from num2words import num2words


class StockMove(models.Model):
_inherit = "stock.move"

currency_id = fields.Many2one(related='picking_id.currency_id',
store=True,
string='Currency',
readonly=True
)
price_unit_br = fields.Float(string='U. Price',
required=True,
digits=dp.get_precision('Product Price'),
compute='_compute_price_unit',
store=True
)

price_total_br = fields.Float(string='Total',
digits=dp.get_precision('Product Price'),
store=True,
compute='_compute_price_total'
)

qty_stock_location_br = fields.Float(string='Av. Qty')

@api.one
@api.depends('price_unit_br', 'qty_stock_location_br')
def _compute_price_total(self):
for line in self:
line.price_total_br = line.price_unit_br * line.qty_stock_location_br

@api.onchange('product_id', 'location_id')
def _onchange_stock_qty(self):
stock_domain = [
('product_id', '=', self.product_id.id),
('location_id', '=', self.location_id.id),
]
stock_av_qty = self.env['stock.quant'].\
search(stock_domain, limit=1).quantity
if stock_av_qty:
self.qty_stock_location_br = stock_av_qty
else:
self.qty_stock_location_br = 1000

@api.depends("product_id", "product_uom")
def _compute_price_unit(self):
for line in self:
po_domain = [
('product_id', '=', line.product_id.id),
('product_uom', '=', line.product_uom.id)
]
price_unit = self.env['purchase.order.line'].\
search(po_domain, order='date_planned', limit=1).price_unit
if price_unit > 0:
line.price_unit_br = price_unit
else:
line.price_unit_br = line.product_id.standard_price
 
Avatar
Discard
Best Answer

Hello,

Set Store = True on your computed field(total_price) after create a new record and check may be your problem solved.

Avatar
Discard
Author Best Answer
Stock Move have purchase order line relation, I don't know why your searching with domain.
So by using relation you can get unit price

Thank you subbaro for your answer

for the stock move related to purchase order I Already Update (prepare_stock_move) from purchase.py.


class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

@api.model
def _prepare_picking(self):
vals = super(PurchaseOrder, self)._prepare_picking()
vals.update({'amount_total_br': self.amount_untaxed, })
return vals

I am using the domain to get the unit price for 'internal transfers' which are not related to purchase order. I get unit price from the last purchase order done.


the problem with me is that "Computed field" such as "total_price" not saved to database.

can you please tell me why???


Avatar
Discard

maybe it is non-storable compute fields. after set store=true on your compute fields it will work, which is defined inside the Model.

something like: total_price = fields.Float(compute='xyz', store=True)

maybe it's useful to you!

Author

Thanks, it is already work after changing attribute order as you said, Also Thanks to @Manish bohra he give same answer,

Best Answer

Hello Ahmed,

Stock Move have purchase order line relation, I don't know why your searching with domain.

So by using relation you can get unit price

Avatar
Discard