# -*- coding: utf-8 -*-

from odoo import api, fields, models, _


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    def _timesheet_create_project_prepare_values(self):
        """Generate project values"""
        values = super()._timesheet_create_project_prepare_values()
        name = False
        budgeted_hours = 0
        budgeted_revenue = 0
        project_manager_id = False
        lead = self.order_id.opportunity_id
        if self.order_id and lead:
            name = lead.project_name
            budgeted_revenue = lead.expected_revenue
            if not name:
                name = self.order_id.client_order_ref
        if self.order_id and lead and lead.project_manager_id:
            project_manager_id = lead.project_manager_id.id
        if name:
            name = '%s - %s' % (name, self.order_id.name)
        else:
            name = self.order_id.name
        values['name'] = name
        if project_manager_id:
            values['user_id'] = project_manager_id
        if self.order_id:
            for line in self.order_id.order_line.filtered(lambda line: line.product_id.type == 'service'):
                budgeted_hours += line.product_uom_qty
        values['budgeted_hours'] = budgeted_hours
        values['budgeted_revenue'] = budgeted_revenue
        return values

    """def _timesheet_create_project_prepare_values(self):
        values = super()._timesheet_create_project_prepare_values()
        name = False
        budgeted_hours = 0
        budgeted_revenue = 0
        project_manager_id = False
        if self.order_id and self.order_id.opportunity_id:
            name = self.order_id.opportunity_id.project_name
            project_manager_id = self.order_id.opportunity_id.project_manager_id.id if self.order_id.opportunity_id.project_manager_id  else False
            budgeted_revenue = self.order_id.opportunity_id.expected_revenue
        elif self.order_id.client_order_ref:
            name = self.order_id.client_order_ref
        if name:
            name = '%s - %s' % (name, self.order_id.name)
        else:
            name = self.order_id.name
        values['name'] = name
        if project_manager_id:
            values['user_id'] = project_manager_id
        if self.order_id:
            for line in self.order_id.order_line.filtered(lambda line: line.product_id.type == 'service'):
                budgeted_hours += line.product_uom_qty
        values['budgeted_hours'] = budgeted_hours
        values['budgeted_revenue'] = budgeted_revenue
        values['allow_billable'] = True
        values['bill_type'] = 'customer_project'
        values['pricing_type'] = 'fixed_rate'
        return values"""







