| 1 |
|
| 2 |
|
| 3 |
from odoo import api, models |
| 4 |
|
| 5 |
|
| 6 |
class SaleOrderLine(models.Model): |
| 7 |
_inherit = "sale.order.line" |
| 8 |
|
| 9 |
@api.depends('analytic_line_ids.amount', 'qty_delivered_method') |
| 10 |
def _compute_purchase_price(self): |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
service_non_timesheet_sols = self.filtered( |
| 15 |
lambda sol: not sol.is_expense and sol.is_service and |
| 16 |
sol.product_id.service_policy == 'ordered_prepaid' and |
| 17 |
sol.state == 'sale' and sol.purchase_price != 0 |
| 18 |
) |
| 19 |
timesheet_sols = self.filtered( |
| 20 |
lambda sol: sol.qty_delivered_method == 'timesheet' and not sol.product_id.standard_price |
| 21 |
) |
| 22 |
super(SaleOrderLine, self - timesheet_sols - service_non_timesheet_sols)._compute_purchase_price() |
| 23 |
if timesheet_sols: |
| 24 |
group_amount = self.env['account.analytic.line']._read_group( |
| 25 |
[('so_line', 'in', timesheet_sols.ids), ('project_id', '!=', False)], |
| 26 |
['so_line'], |
| 27 |
['amount:sum', 'unit_amount:sum']) |
| 28 |
mapped_sol_timesheet_amount = { |
| 29 |
so_line.id: - amount_sum / unit_amount_sum if unit_amount_sum else 0.0 |
| 30 |
for so_line, amount_sum, unit_amount_sum in group_amount |
| 31 |
} |
| 32 |
for line in timesheet_sols: |
| 33 |
line = line.with_company(line.company_id) |
| 34 |
product_cost = mapped_sol_timesheet_amount.get(line.id, line.product_id.standard_price) |
| 35 |
product_uom = line.product_uom_id or line.product_id.uom_id |
| 36 |
if product_uom != line.company_id.project_time_mode_id: |
| 37 |
product_cost = product_uom._compute_quantity( |
| 38 |
product_cost, |
| 39 |
line.company_id.project_time_mode_id |
| 40 |
) |
| 41 |
|
| 42 |
line.purchase_price = line._convert_to_sol_currency( |
| 43 |
product_cost, line.product_id.cost_currency_id) |