# -*- coding: utf-8 -*-

from odoo import api, fields, models, _
from datetime import datetime

class HrEmployee(models.Model):
    _inherit = 'hr.employee'

    budgeted_hour_week = fields.Integer("Working days per week", default=5)

    @api.onchange('resource_calendar_id')
    def onchange_(self):
        if self.resource_calendar_id and self.resource_calendar_id.attendance_ids:
            total_week = [val.dayofweek for val in self.resource_calendar_id.attendance_ids]
            res = list(set(total_week))
            self.budgeted_hour_week = len(res)

    employee_cost_change = fields.One2many('hr.employee.cost' , 'employee_id')

    def write(self, values):
        if 'timesheet_cost' in values:
            self.employee_cost_change.create({'employee_id': self.id,
                                              'user_id': self.env.uid,
                                              'date': fields.Datetime.today(),
                                              'timesheet_cost': self.timesheet_cost,
                                              })
        res = super(HrEmployee, self).write(values)
        return res


class EmployeeCostChangeHistory(models.Model):
    _name = 'hr.employee.cost'

    employee_id = fields.Many2one('hr.employee')
    user_id = fields.Many2one('res.users')
    date = fields.Datetime()
    timesheet_cost = fields.Float("Timesheet Cost", digits=(16, 2), readonly=True, group_operator="sum")

class EmployeePublic(models.Model):
    _inherit = 'hr.employee.public'

    budgeted_hour_week = fields.Integer("Working days per week")
