<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2016 Open Net Sarl (https://www.open-net.ch)
#    Copyright (C) 2018 Compassion CH (http://www.compassion.ch)
#    @author: <NAME> <<EMAIL>>
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################

from datetime import date

from odoo import models, fields, api


class HrAttendanceDay(models.Model):
    """
    The instances of hr.attendance.day is created either at the first
    attendance of the day or by the method
    hr.employee._cron_create_attendance() called by a cron everyday.
    """
    _name = "hr.attendance.day"
    _order = 'date DESC'
    _sql_constraints = [('unique_product', 'unique(date, employee_id)',
                         'This "Attendance day" already exist for this '
                         'employee')]

    ##########################################################################
    #                                 FIELDS                                 #
    ##########################################################################
    date = fields.Date("Date",
                       # readonly=True, todo: uncomment
                       required=True)
    employee_id = fields.Many2one('hr.employee', "Employee",
                                  ondelete="cascade", required=True)

    # Working schedule
    cal_att_ids = fields.Many2many('resource.calendar.attendance',
                                   string="Working schedule",
                                   required=True)
    working_day = fields.Char("Working day", compute='_compute_working_day',
                              readonly=True, store=True)
    name = fields.Char(compute='_compute_name', store=True)

    # Leaves
    leave_ids = fields.Many2many('hr.holidays', readonly=True, string='Leaves')
    in_leave = fields.Boolean('In leave', compute='_compute_in_leave',
                              store=True)
    public_holiday_id = fields.Many2one('hr.holidays.public.line',
                                        'Public holidays')

    # Due hours
    due_hours = fields.Float('Due hours', compute='_compute_due_hours',
                             readonly=True, store=True)

    # Attendances
    attendance_ids = fields.One2many('hr.attendance', 'attendance_day_id',
                                     'Attendances', readonly=True)

    # Worked
    paid_hours = fields.Float('Paid hours',
                              compute='_compute_paid_hours', store=True,
                              readonly=True,
                              oldname="worked_hours")

    coefficient = fields.Float(help='Worked hours coefficient')

    # Break
    due_break_min = fields.Float('Minimum break due',
                                 compute='_compute_due_break')
    due_break_total = fields.Float('Total break due',
                                   compute='_compute_due_break')
    break_ids = fields.One2many('hr.attendance.break',
                                'attendance_day_id',
                                'Breaks',
                                readonly=True)
    break_total = fields.Float('Total break',
                               compute='_compute_break_total',
                               store=True, )
    rule_id = fields.Many2one('hr.attendance.rules', 'Rules',
                              compute='_compute_rule_id')

    # Extra hours
    extra_hours = fields.Float("Extra hours",
                               compute='_compute_extra_hours',
                               store=True)
    extra_hours_lost = fields.Float()

    ##########################################################################
    #                             FIELDS METHODS                             #
    ##########################################################################
    @api.multi
    @api.depends('date')
    def _compute_working_day(self):
        for att_day in self.filtered('date'):
            att_day.working_day = fields.Date.from_string(
                att_day.date).strftime('%A')

    @api.multi
    @api.depends('working_day')
    def _compute_name(self):
        for rd in self.filtered('working_day'):
            rd.name = rd.working_day + ' ' + rd.date

    @api.multi
    @api.depends('leave_ids')
    def _compute_in_leave(self):
        for att_day in self:
            if att_day.leave_ids:
                att_day.in_leave = True
            if att_day.public_holiday_id:
                att_day.in_leave = True

    @api.multi
    @api.depends('cal_att_ids', 'cal_att_ids.due_hours', 'leave_ids.state',
                 'public_holiday_id')
    def _compute_due_hours(self):
        """First search the due hours based on the contract and after remove
        somme hours if they are vacation"""
        for att_day in self:

            # Public holidays
            if att_day.public_holiday_id:
                att_day.due_hours = 0
                continue

            # Contract
            due_hours = sum(att_day.mapped('cal_att_ids.due_hours'))

            # Leaves
            if att_day.leave_ids:
                for leave in att_day.leave_ids.filtered(
                        lambda l: l.state in ('validate', 'validate1')):
                    if not leave.holiday_status_id.remove_from_due_hours:
                        working_day = self.env[
                            'base.config.settings'].get_work_day_duration()
                        due_hours -= leave.compute_work_day(
                            att_day.date) * working_day

            if due_hours < 0:
                due_hours = 0

            att_day.due_hours = due_hours

    @api.multi
    @api.depends('attendance_ids.worked_hours')
    def _compute_paid_hours(self):
        for att_day in self.filtered('attendance_ids'):
            logged_hours = sum(att_day.attendance_ids.mapped('paid_hours'))

            # Take only the breaks edited by the system
            breaks = att_day.break_ids.filtered(lambda r: r.system_modified)

            for break_id in breaks.filtered(lambda r: not r.is_offered):
                logged_duration = break_id.logged_duration
                if logged_duration:
                    # the break was modified
                    deduct = logged_duration - break_id.original_duration
                else:
                    # the break was created
                    deduct = logged_duration
                logged_hours -= deduct

            att_day.paid_hours = logged_hours<commit_msg>Review fixes<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2016 Open Net Sarl (https://www.open-net.ch)
#    Copyright (C) 2018 Compassion CH (http://www.compassion.ch)
#    @author: <NAME> <<EMAIL>>
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################

from datetime import date

from odoo import models, fields, api


class HrAttendanceDay(models.Model):
    """
    The instances of hr.attendance.day is created either at the first
    attendance of the day or by the method
    hr.employee._cron_create_attendance() called by a cron everyday.
    """
    _name = "hr.attendance.day"
    _order = 'date DESC'
    _sql_constraints = [('unique_product', 'unique(date, employee_id)',
                         'This "Attendance day" already exist for this '
                         'employee')]

    ##########################################################################
    #                                 FIELDS                                 #
    ##########################################################################
    date = fields.Date("Date",
                       # readonly=True, todo: uncomment
                       required=True)
    employee_id = fields.Many2one('hr.employee', "Employee",
                                  ondelete="cascade", required=True)

    # Working schedule
    cal_att_ids = fields.Many2many('resource.calendar.attendance',
                                   string="Working schedule",
                                   required=True)
    working_day = fields.Char("Working day", compute='_compute_working_day',
                              readonly=True, store=True)
    name = fields.Char(compute='_compute_name', store=True)

    # Leaves
    leave_ids = fields.Many2many('hr.holidays', readonly=True, string='Leaves')
    in_leave = fields.Boolean('In leave', compute='_compute_in_leave',
                              store=True)
    public_holiday_id = fields.Many2one('hr.holidays.public.line',
                                        'Public holidays')

    # Due hours
    due_hours = fields.Float('Due hours', compute='_compute_due_hours',
                             readonly=True, store=True)

    # Attendances
    attendance_ids = fields.One2many('hr.attendance', 'attendance_day_id',
                                     'Attendances', readonly=True)

    # Worked
    paid_hours = fields.Float('Paid hours',
                              compute='_compute_paid_hours', store=True,
                              readonly=True,
                              oldname="worked_hours")

    coefficient = fields.Float(help='Worked hours coefficient')

    # Break
    due_break_min = fields.Float('Minimum break due',
                                 compute='_compute_due_break')
    due_break_total = fields.Float('Total break due',
                                   compute='_compute_due_break')
    break_ids = fields.One2many('hr.attendance.break',
                                'attendance_day_id',
                                'Breaks',
                                readonly=True)
    break_total = fields.Float('Total break',
                               compute='_compute_break_total',
                               store=True, )
    rule_id = fields.Many2one('hr.attendance.rules', 'Rules',
                              compute='_compute_rule_id')

    # Extra hours
    extra_hours = fields.Float("Extra hours",
                               compute='_compute_extra_hours',
                               store=True)
    extra_hours_lost = fields.Float()

    ##########################################################################
    #                             FIELDS METHODS                             #
    ##########################################################################
    @api.multi
    @api.depends('date')
    def _compute_working_day(self):
        for att_day in self.filtered('date'):
            att_day.working_day = fields.Date.from_string(
                att_day.date).strftime('%A')

    @api.multi
    @api.depends('working_day')
    def _compute_name(self):
        for rd in self.filtered('working_day'):
            rd.name = rd.working_day + ' ' + rd.date

    @api.multi
    @api.depends('leave_ids')
    def _compute_in_leave(self):
        for att_day in self:
            if att_day.leave_ids:
                att_day.in_leave = True
            if att_day.public_holiday_id:
                att_day.in_leave = True

    @api.multi
    @api.depends('cal_att_ids', 'cal_att_ids.due_hours', 'leave_ids.state',
                 'public_holiday_id')
    def _compute_due_hours(self):
        """First search the due hours based on the contract and after remove
        somme hours if they are vacation"""
        for att_day in self:

            # Public holidays
            if att_day.public_holiday_id:
                att_day.due_hours = 0
                continue

            # Contract
            due_hours = sum(att_day.mapped('cal_att_ids.due_hours'))

            # Leaves
            if att_day.leave_ids:
                for leave in att_day.leave_ids.filtered(
                        lambda l: l.state in ('validate', 'validate1')):
                    if not leave.holiday_status_id.remove_from_due_hours:
                        working_day = self.env[
                            'base.config.settings'].get_work_day_duration()
                        due_hours -= leave.compute_work_day(
                            att_day.date) * working_day

            if due_hours < 0:
                due_hours = 0

            att_day.due_hours = due_hours

    @api.multi
    @api.depends('attendance_ids.worked_hours')
    def _compute_paid_hours(self):
        for att_day in self.filtered('attendance_ids'):
            logged_hours = sum(att_day.attendance_ids.mapped('worked_hours'))

            # Take only the breaks edited by the system
            breaks = att_day.break_ids.filtered(lambda r: r.system_modified)

            for break_id in breaks.filtered(lambda r: not r.is_offered):
                logged_duration = break_id.logged_duration
                if logged_duration:
                    # the break was modified
                    deduct = logged_duration - break_id.original_duration
                else:
                    # the break was created
                    deduct = logged_duration
                logged_hours -= deduct

            att_day.paid_hours = logged_hours
{'fraction_non_alphanumeric': 0.15317549793310786, 'fraction_numerical': 0.003006388575723412, 'mean_word_length': 1.8000841750841752, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7552122', 'n_tokens_mistral': 3606, 'n_tokens_neox': 3377, 'n_words': 688}