from odoo import api, fields, models
from odoo.exceptions import UserError, AccessError, ValidationError
from datetime import datetime
from dateutil import relativedelta


class CustomProject(models.Model):
    _inherit = 'project.project'

    revenue_amount_lines = fields.One2many('project.revenue.lines' , 'project_id')


class ProjectRevenueLines(models.Model):
    _name = 'project.revenue.lines'

    project_id  = fields.Many2one('project.project')
    # start_date = fields.Date(required=True)
    # end_date = fields.Date(required=True)
    date = fields.Date(required=True)
    fixed_amount = fields.Float()

    def edit_revenue_history_record(self):
        context = dict(self.env.context)
        context['form_view_initial_mode'] = 'edit'
        return {
            'name': ('Edit Revenue History'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'target': 'new',
            'res_model': 'project.revenue.lines',
            'res_id': self.id,
            'context': context,
            # 'view_id': view_id
        }

    """@api.onchange('start_date')
    def _onchange_start_date(self):
        if self.project_id:
            records = self.env['project.revenue.lines'].sudo().search([('project_id', '=', self.project_id.id)])
            if self.start_date:
                if self.start_date.day != 1:
                    raise AccessError('Please select first date of month')
                else:
                    pass
                    for line in records:
                        if self.start_date >= line.start_date and self.start_date <= line.end_date:
                            raise AccessError('Date already exist')"""

    def update_record(self):
        return {
            'type': 'ir.actions.act_window_close'}






