# -*- coding: utf-8 -*-
###############################################################################
#
#    Tech-Receptives Solutions Pvt. Ltd.
#    Copyright (C) 2009-TODAY Tech-Receptives(<http://www.techreceptives.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

from odoo import api, fields, models, tools, _
from odoo.modules import get_module_resource
from odoo.osv.expression import get_unaccent_wrapper
from odoo.exceptions import UserError, ValidationError

class ProjectTaskType(models.Model):
    _inherit = "project.task.type"
    
    default_project_stage = fields.Boolean('Default project stage',default=False)
    type_done = fields.Boolean('Type Done',default=False)
    type_cancel = fields.Boolean('Type Cancel',default=False)
    
class Project(models.Model):
    _inherit = "project.project"

    code = fields.Char('Code',required=False)
    department_id = fields.Many2one('hr.department', string='Department',track_visibility="onchange")
    total_cost = fields.Float(string='Total Cost', compute="_compute_project_cost")
    estimated_budget = fields.Float(string='Estimated Budget')
    start_date = fields.Date(string='Start Date')
    end_date = fields.Date(string='End Date')
    department_manager_id = fields.Many2one('res.users', 'Department Manager', related='department_id.manager_id.user_id', store=True, readonly=True)
    operational_objective_id = fields.Many2one('prime.project.objective', string='Operational Objective',track_visibility="onchange")
    budget_execution = fields.Float(compute='_get_budget_execution', string='Budget Execution', group_operator="avg")
    tag_ids = fields.Many2many('project.tags', string='Tags')

    @api.depends('tasks')
    def _compute_project_cost(self):
        total_cost = 0.0
        for project in self:
            for task in project.tasks:
                total_cost += task.task_cost
            project.total_cost = total_cost

    @api.depends('total_cost','estimated_budget')
    def _get_budget_execution(self):
        for project in self:
            estimated_budget = self.estimated_budget
            total_cost = self.total_cost
            budget_execution = 0.0
            if estimated_budget > 0 and total_cost > 0:
                budget_execution = round(100.0 * (total_cost / estimated_budget), 2)
            project.budget_execution = budget_execution

    @api.model
    def create(self, vals):
        print("VALS CREATE::::::::::::::::::::::: ",vals)
        ids_stages = []
        all_stages = self.env['project.task.type'].search([('default_project_stage','=',True)])
        for stage in all_stages:
            ids_stages.append(stage.id)
        vals['type_ids'] = [(6, 0, ids_stages)]
        res = super(Project, self).create(vals)
        return res

class ProjectTask(models.Model):
    _inherit = "project.task"

    task_cost = fields.Float('Cost')
    department_id = fields.Many2one('hr.department', 'Project Department', related='project_id.department_id', store=True, readonly=True)
    previous_user_id = fields.Many2one('res.users', string='Previous owner')
    task_final_stage = fields.Boolean('Final Stage', compute="_compute_task_final_stage")

    def _compute_task_final_stage(self):
        task_final_stage = False
        if self.stage_id:
            stage_id = self.stage_id.id
            last_stage = self.env['project.task.type'].search([('type_done','=',True)],limit=1)
            if stage_id == last_stage.id:
                task_final_stage = True
            self.task_final_stage = task_final_stage
