| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import api, fields, models |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrderLine(models.Model): |
| 8 |
_inherit = 'sale.order.line' |
| 9 |
|
| 10 |
@api.depends('move_ids', 'move_ids.value', 'move_ids.picking_id.state') |
| 11 |
def _compute_purchase_price(self): |
| 12 |
line_ids_to_pass = set() |
| 13 |
for line in self: |
| 14 |
product = line.product_id.with_company(line.company_id) |
| 15 |
if not line.has_valued_move_ids(): |
| 16 |
line_ids_to_pass.add(line.id) |
| 17 |
elif line.product_id and line.product_id.categ_id and line.product_id.categ_id.property_cost_method != 'standard': |
| 18 |
|
| 19 |
qty_from_delivery = line.qty_delivered |
| 20 |
price_unit_from_delivery = line.move_ids.filtered(lambda m: m.state == 'done')._get_price_unit() if qty_from_delivery > 0 else 0 |
| 21 |
if qty_from_delivery <= 0: |
| 22 |
purch_price = product.standard_price |
| 23 |
else: |
| 24 |
qty_from_std_price = max(line.product_uom_qty - qty_from_delivery, 0) |
| 25 |
purch_price = (qty_from_delivery * price_unit_from_delivery + qty_from_std_price * product.standard_price) / (qty_from_delivery + qty_from_std_price) |
| 26 |
purch_price_uom = line.product_id.uom_id._compute_price(purch_price, line.product_uom_id) |
| 27 |
line.purchase_price = line._convert_to_sol_currency( |
| 28 |
purch_price_uom, |
| 29 |
product.cost_currency_id, |
| 30 |
) |
| 31 |
elif not line.product_uom_qty and line.qty_delivered: |
| 32 |
|
| 33 |
line_ids_to_pass.add(line.id) |
| 34 |
return super(SaleOrderLine, self.browse(line_ids_to_pass))._compute_purchase_price() |