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

from odoo import fields, models, tools


class ReportProjectTaskUser(models.Model):
    _name = "report.project.maintenance"
    _description = "Project Maintenance Analysis"
    _order = 'project_id'
    _auto = False

    project_id = fields.Many2one('project.project', string='Project', readonly=True)
    maintenance_type_id = fields.Many2one('project.maintenance.type', string='Maintenance Type', readonly=True)
    maintenance_id = fields.Many2one('project.maintenance', string='Maintenance', readonly=True)
    cost = fields.Float("Cost", digits=(16, 2), readonly=True)
    #total_amount = fields.Float("Total Cost", digits=(16, 2), readonly=True)
    #nbr = fields.Integer('# of Maintenance', readonly=True)

    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 min(lines.id) as id,
            pro.id as project_id,
            details.maintenance_type_id as maintenance_type_id,
            lines.maintenance_id as maintenance_id,
            lines.cost as cost
            from project_project pro
            left join project_maintenance_details as details ON (pro.id = details.project_id)
            left join project_maintenance_lines as lines ON (details.id = lines.details_id)
            left join project_maintenance as main ON (main.id = details.maintenance_type_id)
            group by pro.id, details.maintenance_type_id, lines.maintenance_id, lines.cost
        )""" % (self._table,))
