File: homework_location_wizard.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (63 lines) | stat: -rw-r--r-- 2,915 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, api

from odoo.addons.hr_homeworking.models.hr_homeworking import DAYS

class HomeworkLocationWizard(models.TransientModel):
    _name = 'homework.location.wizard'
    _description = 'Set Homework Location Wizard'

    work_location_id = fields.Many2one('hr.work.location', required=True, string="Location")
    work_location_name = fields.Char(related='work_location_id.name', string="Location name")
    work_location_type = fields.Selection(related="work_location_id.location_type")
    employee_id = fields.Many2one('hr.employee', default=lambda self: self.env.user.employee_id, required=True, ondelete="cascade")
    employee_name = fields.Char(related="employee_id.name")
    user_can_edit = fields.Boolean(compute='_compute_user_can_edit')
    weekly = fields.Boolean(default=False)
    date = fields.Date(string="Date")
    day_week_string = fields.Char(compute="_compute_day_week_string")

    @api.depends('date')
    def _compute_day_week_string(self):
        for record in self:
            record.day_week_string = record.date.strftime("%A")

    @api.depends('date')
    def _compute_user_can_edit(self):
        self.user_can_edit = self.env.user.can_edit

    def set_employee_location(self):
        self.ensure_one()
        default_employee_id = self.env.context.get('default_employee_id') or self.env.user.employee_id.id
        employee_id = self.env['hr.employee'].browse(self.employee_id.id or default_employee_id)
        employee_location = self.env['hr.employee.location'].search([
            ('date', '=', self.date),
            ('employee_id', '=', employee_id.id)
        ])
        weekday = self.date.weekday()
        default_location_for_current_date = DAYS[weekday]
        if self.weekly:
            # delete any exceptions on the current date
            if employee_location:
                employee_location.unlink()
            employee_id.sudo().user_id.write({
                default_location_for_current_date: self.work_location_id.id,
            })
        else:
            # check if work_location_id is the same as the default one for that day
            if self.work_location_id.id == employee_id[default_location_for_current_date].id:
                employee_location.unlink()
            # check if worklocation is set for that employee that day
            elif employee_location:
                employee_location.write({
                    'date': self.date,
                    'employee_id': employee_id.id,
                    'work_location_id': self.work_location_id.id
                })
            else:
                self.env['hr.employee.location'].create({
                    'date': self.date,
                    'employee_id': employee_id.id,
                    'work_location_id': self.work_location_id.id
                })