from odoo import fields, models, api from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT class PurchaseOrderLine(models.Model): _inherit = 'purchase.order.line' # Override method from addons/purchase/models/purchase.py @api.onchange('product_id') def onchange_product_id(self): if not self.product_id: return # Reset date, price and quantity since _onchange_quantity will provide default values self.price_unit = self.product_qty = 0.0 self._product_id_change() self._suggest_quantity() self._onchange_quantity() # Override method from addons/purchase/models/purchase.py @api.onchange('product_qty', 'product_uom') def _onchange_quantity(self): if not self.product_id: return params = {'order_id': self.order_id} seller = self.product_id._select_seller( partner_id=self.partner_id, quantity=self.product_qty, date=self.order_id.date_order and self.order_id.date_order.date(), uom_id=self.product_uom, params=params) if seller or not self.date_planned: self.date_planned = self._get_date_planned(seller).strftime(DEFAULT_SERVER_DATETIME_FORMAT) # If not seller, use the standard price. It needs a proper currency conversion. if not seller: po_line_uom = self.product_uom or self.product_id.uom_po_id price_unit = self.env['account.tax']._fix_tax_included_price_company( self.product_id.uom_id._compute_price(self.product_id.standard_price, po_line_uom), self.product_id.supplier_taxes_id, self.taxes_id, self.company_id, ) if price_unit and self.order_id.currency_id and self.order_id.company_id.currency_id != self.order_id.currency_id: price_unit = self.order_id.company_id.currency_id._convert( price_unit, self.order_id.currency_id, self.order_id.company_id, self.date_order or fields.Date.today(), ) self.price_unit = price_unit return price_unit = self.env['account.tax']._fix_tax_included_price_company(seller.price, self.product_id.supplier_taxes_id, self.taxes_id, self.company_id) if seller else 0.0 if price_unit and seller and self.order_id.currency_id and seller.currency_id != self.order_id.currency_id: price_unit = seller.currency_id._convert( price_unit, self.order_id.currency_id, self.order_id.company_id, self.date_order or fields.Date.today()) if seller and self.product_uom and seller.product_uom != self.product_uom: price_unit = seller.product_uom._compute_price(price_unit, self.product_uom) # Custom script purchase_pricelist = self.env['purchase.pricelist'].search([ ('product_id', '=', self.product_id.id), ('vendor_id', '=', self.partner_id.id) ], limit=1) price_unit = purchase_pricelist.product_price if not price_unit: product_supplierinfo = self.env['product.supplierinfo'].search([ ('product_tmpl_id', '=', self.product_id.product_tmpl_id.id), ('name', '=', self.partner_id.id) ], limit=1) price_unit = product_supplierinfo.price self.price_unit = price_unit