sale_order_line.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, models, fields
  4. class SaleOrderLine(models.Model):
  5. _inherit = "sale.order.line"
  6. expense_id = fields.Many2one('hr.expense', string='Expense')
  7. @api.depends('is_expense')
  8. def _compute_purchase_price(self):
  9. date_today = fields.Date.context_today(self)
  10. expense_lines = self.filtered('expense_id')
  11. for line in expense_lines:
  12. if line.expense_id.product_has_cost:
  13. product_cost = line.expense_id.untaxed_amount / line.expense_id.quantity
  14. else:
  15. product_cost = line.expense_id.untaxed_amount
  16. from_currency = line.expense_id.currency_id
  17. to_currency = line.currency_id or line.order_id.currency_id
  18. if to_currency and product_cost and from_currency != to_currency:
  19. line.purchase_price = from_currency._convert(
  20. from_amount=product_cost,
  21. to_currency=to_currency,
  22. company=line.company_id or self.env.company,
  23. date=line.order_id.date_order or date_today,
  24. round=False)
  25. else:
  26. line.purchase_price = product_cost
  27. return super(SaleOrderLine, self - expense_lines)._compute_purchase_price()