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

from odoo import fields, models, tools


class BudgetAmtAnalysis(models.Model):

    _name = "project.budget.amt.report"
    _description = "Project budget amount 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)
    parent_project = fields.Many2one('project.project', string='Parent Project', readonly=True)
    #is_sub_project = fields.Boolean("Is Sub Project", readonly=True)
    #sub_project = fields.Many2one('project.project', string='Sub Project', readonly=True)
    partner_id = fields.Many2one('res.partner', string='Client', readonly=True)
    amount_type = fields.Char(string="Amount Type")
    revenue = fields.Float("Amount", digits=(16, 2), readonly=True, group_operator="sum")
    pricing_type = fields.Selection([
        ('fixed_rate', 'Fixed rate'),
        ('employee_rate', 'Consultant rate')
    ], string="Pricing", readonly=True)
    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)
    employee_id = fields.Many2one('hr.employee', string='Consultant', readonly=True)
    start_date = fields.Date(string='Start Date', readonly=True)
    end_date = fields.Date(string='End Date', readonly=True)
    timesheet_date = fields.Date(string='Timesheet Date', readonly=True)
    timesheet_sdatetime = fields.Datetime(string='Timesheet Start Time', readonly=True)
    #budgeted_revenue = fields.Float("Budgeted Revenue", digits=(16, 2), readonly=True, group_operator="sum")
    #actual_revenue = fields.Float("Actual Revenue", 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, parentproject as parent_project, start_date, end_date, timesheet_date, timesheet_sdatetime, partner_id, employee_id, amount_type, pricing_type, project_type, revenue from (
	      SELECT
               pro.id AS project_id,
               (select project_id from project_subproject_rel as par where pro.id=par.id limit 1) as parentproject,
               pro.date_start AS start_date,
               pro.date AS end_date,
               pro.partner_id AS partner_id,
               pro_emp.employee_id AS employee_id,
               'Budgeted Revenue' as amount_type,
               pro.pricing_type  as pricing_type,
               pro.project_type  as project_type,
               null::date AS timesheet_date,
               null::timestamp AS timesheet_sdatetime,
               pro_emp.cost AS revenue
               FROM project_project pro
               Left JOIN project_sale_line_employee_map pro_emp ON pro_emp.project_id = pro.id
               WHERE PRO.active = 't' and pro.pricing_type = 'employee_rate' and pro.project_type = 'hours_in_consultant'
        union
            SELECT
               pro.id AS project_id,
               (select project_id from project_subproject_rel as par where pro.id=par.id limit 1) as parentproject,
               pro.date_start AS start_date,
               pro.date AS end_date,
               pro.partner_id AS partner_id,
               AAL.employee_id AS employee_id,
               'Actual Cost' as amount_type,
               pro.pricing_type  as pricing_type,
               pro.project_type  as project_type,
               AAL.date AS timesheet_date,
               AAL.start_datetime AS timesheet_sdatetime,
               (sum(AAL.unit_amount) * pro_emp.employee_price) AS revenue
               FROM project_project PRO
               Left JOIN project_sale_line_employee_map pro_emp ON pro_emp.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 and AAL.employee_id = pro_emp.employee_id
               WHERE PRO.active = 't' and AAL.employee_id is not null and PRO.pricing_type = 'employee_rate' 
               and PRO.project_type in ('hours_in_consultant', 'hours_no_limit')
               group by pro.id, aal.employee_id, aal.date, aal.start_datetime, pro_emp.employee_price
        union
            SELECT
               pro.id AS project_id,
               (select project_id from project_subproject_rel as par where pro.id=par.id limit 1) as parentproject,
               pro.date_start AS start_date,
               pro.date AS end_date,
               pro.partner_id AS partner_id,
               AAL.employee_id AS employee_id,
               'Actual Revenue' as amount_type,
               pro.pricing_type  as pricing_type,
               pro.project_type  as project_type,
               AAL.date AS timesheet_date,
               AAL.start_datetime AS timesheet_sdatetime,
               (sum(AAL.unit_amount) * pro.hourly_rate) AS revenue
               FROM project_project PRO
               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 = 'employee_rate' and PRO.project_type = 'hours_no_limit'
               group by pro.id, aal.employee_id, aal.date, aal.unit_amount, aal.start_datetime
        union
            SELECT
               pro.id AS project_id,
               (select project_id from project_subproject_rel as par where pro.id=par.id limit 1) as parentproject,
               pro.date_start AS start_date,
               pro.date AS end_date,
               pro.partner_id AS partner_id,
               null::int AS employee_id,
               'Budgeted Revenue' as amount_type,
               pro.pricing_type  as pricing_type,
               pro.project_type  as project_type,
               null::date AS timesheet_date,
               null::timestamp AS timesheet_sdatetime,
               pro.budgeted_revenue AS revenue
               FROM project_project pro
               --Left JOIN project_sale_line_employee_map pro_emp ON pro_emp.project_id = pro.id
               WHERE PRO.active = 't' and pro.pricing_type = 'employee_rate' and pro.project_type = 'hours_no_limit'
        union
            SELECT
               pro.id AS project_id,
               (select project_id from project_subproject_rel as par where pro.id=par.id limit 1) as parentproject,
               pro.date_start AS start_date,
               pro.date AS end_date,
               pro.partner_id AS partner_id,
               AAL.employee_id AS employee_id,
               'Actual Revenue' as amount_type,
               pro.pricing_type  as pricing_type,
               pro.project_type  as project_type,
               AAL.date AS timesheet_date,
               AAL.start_datetime AS timesheet_sdatetime,
               (sum(AAL.unit_amount) * pro_emp.price_unit) AS revenue
               FROM project_project PRO
               Left JOIN project_sale_line_employee_map pro_emp ON pro_emp.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 and AAL.employee_id = pro_emp.employee_id
               WHERE PRO.active = 't' and AAL.employee_id is not null and PRO.pricing_type = 'employee_rate' and PRO.project_type = 'hours_in_consultant'
               group by pro.id, aal.employee_id, aal.date, aal.start_datetime, pro_emp.price_unit
        union
            SELECT
               pro.id AS project_id,
               (select project_id from project_subproject_rel as par where pro.id=par.id limit 1) as parentproject,
               pro.date_start AS start_date,
               pro.date AS end_date,
               pro.partner_id AS partner_id,
               AAL.employee_id AS employee_id,
               'Actual Cost' as amount_type,
               pro.pricing_type  as pricing_type,
               pro.project_type  as project_type,
               AAL.date AS timesheet_date,
               AAL.start_datetime AS timesheet_sdatetime,
               (AAL.amount * -1) AS revenue
               FROM project_project PRO
               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
               --and AAL.employee_id = pro_emp.employee_id
               WHERE PRO.active = 't' and PRO.pricing_type = 'fixed_rate')
               as res
               order by
               project_id,
               amount_type desc
               --group by Pro.id, PRO.partner_id, Pro.budgeted_revenue, AAL.amount
        )""" % (self._table,))