# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, tools


class BudgetHrsAnalysis(models.Model):

    _name = "project.budget.hrs.report"
    _description = "Project budget hours analysis report"
    #_order = 'project_id'
    _auto = False

    #analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account', readonly=True)
    project_id = fields.Many2one('project.project', string='Project', readonly=True)
    start_date = fields.Date(string='Start Date', readonly=True)
    end_date = fields.Date(string='End Date', readonly=True)
    partner_id = fields.Many2one('res.partner', string='Client', readonly=True)
    employee_id = fields.Many2one('hr.employee', string='Consultant', readonly=True)
    hours_type = fields.Char(string="Hours Type", readonly=True)
    hours = fields.Float("Hours", digits=(16, 2), readonly=True, group_operator="sum")
    #project_type = fields.Selection([
    #    ('hours_in_consultant', 'Hours are budgeted according to a consultant'),
    #    ('hours_no_limit', 'Total hours are budgeted without division to consultant'),
    #], string="Project Type", readonly=True)

    #budgeted_hours = fields.Float("Budgeted Hours", digits=(16, 2), readonly=True, group_operator="sum")
    #actual_hours = fields.Float("Actual Hours", digits=(16, 2), readonly=True, group_operator="sum")

    def init(self):
        '''Create the view'''
        tools.drop_view_if_exists(self._cr, self._table)
        self._cr.execute("""
        CREATE OR REPLACE VIEW %s AS (
        SELECT ROW_NUMBER() OVER() as id, project_id, start_date, end_date, partner_id, employee_id, hours_type,
        hours from (
            SELECT
              PRO.id AS project_id,
              Pro_emp_hours.start_date AS start_date,
              Pro_emp_hours.end_date AS end_date,
              PRO.partner_id AS partner_id,
              Pro_emp_hours.employee_id AS employee_id,
              'Budgeted Hours' as hours_type,
              Pro_emp_hours.budgeted_hours as hours
            FROM
              project_project PRO
              Left JOIN project_sale_line_employee_map Pro_emp ON Pro_emp.project_id = Pro.id
              Left JOIN project_consultant_hrs Pro_emp_hours ON Pro_emp_hours.project_id = Pro.id
              LEFT JOIN account_analytic_account AA ON Pro.analytic_account_id = AA.id
              LEFT JOIN account_analytic_line AAL ON AAL.account_id = AA.id
              and AAL.project_id = Pro.id
            Where
              PRO.active = 't'
              and PRO.pricing_type != 'fixed_rate'
              and PRO.project_type = 'hours_in_consultant'
            Union
            SELECT
              PRO.id AS project_id,
              Pro_emp_hours.start_date AS start_date,
              Pro_emp_hours.end_date AS end_date,
              PRO.partner_id AS partner_id,
              Pro_emp_hours.employee_id AS employee_id,
              'Actual Hours' as hours_type,
              Pro_emp_hours.actual_hours  AS hours
            FROM
              project_project PRO
              Left JOIN project_sale_line_employee_map Pro_emp ON Pro_emp.project_id = Pro.id
              Left JOIN project_consultant_hrs Pro_emp_hours ON Pro_emp_hours.project_id = Pro.id
              LEFT JOIN account_analytic_account AA ON Pro.analytic_account_id = AA.id
              LEFT JOIN account_analytic_line AAL ON AAL.account_id = AA.id
              and AAL.project_id = Pro.id
            Where
              PRO.active = 't'
              and PRO.pricing_type != 'fixed_rate'
              and PRO.project_type = 'hours_in_consultant'
              ) as res
               order by
              project_id desc,
              hours_type asc,
              employee_id
        )""" % (self._table,))


