Activate employee based on dates

Closes #12077
This commit is contained in:
Cédric Krier
2024-01-30 10:34:34 +01:00
parent 4e75c63e51
commit 29de76f324
5 changed files with 132 additions and 1 deletions
+3
View File
@@ -34,6 +34,9 @@ class Line(ModelSQL, ModelView):
company = fields.Many2One('company.company', "Company", required=True,
help="The company which the employee attended.")
employee = fields.Many2One('company.employee', "Employee", required=True,
search_context={
'active_test': False,
},
domain=[
('company', '=', Eval('company')),
['OR',
+1
View File
@@ -1,3 +1,4 @@
* Activate employee based on dates
* Use template substitution for company header/footer
Version 7.0.0 - 2023-10-30
+41
View File
@@ -1,6 +1,7 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime as dt
from string import Template
from trytond.model import ModelSQL, ModelView, fields
@@ -98,6 +99,9 @@ class Employee(ModelSQL, ModelView):
help="The party which represents the employee.")
company = fields.Many2One('company.company', 'Company', required=True,
help="The company to which the employee belongs.")
active = fields.Function(
fields.Boolean("Active"),
'on_change_with_active', searcher='search_active')
start_date = fields.Date('Start Date',
domain=[If((Eval('start_date')) & (Eval('end_date')),
('start_date', '<=', Eval('end_date')),
@@ -129,6 +133,43 @@ class Employee(ModelSQL, ModelView):
def default_company():
return Transaction().context.get('company')
@fields.depends('start_date', 'end_date')
def on_change_with_active(self, name=None):
pool = Pool()
Date = pool.get('ir.date')
context = Transaction().context
date = context.get('date') or Date.today()
start_date = self.start_date or dt.date.min
end_date = self.end_date or dt.date.max
return start_date <= date <= end_date
@classmethod
def search_active(cls, name, domain):
pool = Pool()
Date = pool.get('ir.date')
context = Transaction().context
date = context.get('date') or Date.today()
_, operator, value = domain
if (operator == '=' and value) or (operator == '!=' and not value):
domain = [
['OR',
('start_date', '=', None),
('start_date', '<=', date),
],
['OR',
('end_date', '=', None),
('end_date', '>=', date),
],
]
elif (operator == '=' and not value) or (operator == '!=' and value):
domain = ['OR',
('start_date', '>', date),
('end_date', '<', date),
]
else:
domain = []
return domain
def get_rec_name(self, name):
return self.party.rec_name
+82
View File
@@ -1,6 +1,7 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime as dt
from collections import defaultdict
from contextlib import contextmanager
@@ -329,5 +330,86 @@ class CompanyTestCase(
self.assertEqual(
company.footer_used, "Dunder Mifflin - company@example.com")
@with_transaction()
def test_employee_active_no_dates(self):
"Test employee active with dates"
pool = Pool()
Employee = pool.get('company.employee')
company = create_company()
employee = create_employee(company, "Jim Halper")
self.assertEqual(employee.active, True)
self.assertEqual(Employee.search([
('active', '=', True),
]), [employee])
self.assertEqual(Employee.search([
('active', '!=', False),
]), [employee])
self.assertEqual(Employee.search([
('active', '=', False),
]), [])
self.assertEqual(Employee.search([
('active', '!=', True),
]), [])
@with_transaction()
def test_employee_active_start_date(self):
"Test employee active with start date"
pool = Pool()
Employee = pool.get('company.employee')
company = create_company()
employee = create_employee(company, "Jim Halper")
employee.start_date = dt.date.today()
employee.save()
with Transaction().set_context(date=employee.start_date):
self.assertEqual(Employee(employee).active, True)
self.assertEqual(Employee.search([
('active', '=', True),
]), [employee])
with Transaction().set_context(
date=employee.start_date - dt.timedelta(days=1)):
self.assertEqual(Employee(employee).active, False)
self.assertEqual(Employee.search([
('active', '=', True),
]), [])
with Transaction().set_context(
date=employee.start_date + dt.timedelta(days=1)):
self.assertEqual(Employee(employee).active, True)
self.assertEqual(Employee.search([
('active', '=', True),
]), [employee])
@with_transaction()
def test_employee_active_end_date(self):
"Test employee active with end date"
pool = Pool()
Employee = pool.get('company.employee')
company = create_company()
employee = create_employee(company, "Jim Halper")
employee.end_date = dt.date.today()
employee.save()
with Transaction().set_context(date=employee.end_date):
self.assertEqual(Employee(employee).active, True)
self.assertEqual(Employee.search([
('active', '=', True),
]), [employee])
with Transaction().set_context(
date=employee.end_date - dt.timedelta(days=1)):
self.assertEqual(Employee(employee).active, True)
self.assertEqual(Employee.search([
('active', '=', True),
]), [employee])
with Transaction().set_context(
date=employee.end_date + dt.timedelta(days=1)):
self.assertEqual(Employee(employee).active, False)
self.assertEqual(Employee.search([
('active', '=', True),
]), [])
del ModuleTestCase
+5 -1
View File
@@ -23,7 +23,11 @@ class Line(ModelSQL, ModelView):
company = fields.Many2One('company.company', 'Company', required=True,
help="The company on which the time is spent.")
employee = fields.Many2One(
'company.employee', "Employee", required=True, domain=[
'company.employee', "Employee", required=True,
search_context={
'active_test': False,
},
domain=[
('company', '=', Eval('company', -1)),
['OR',
('start_date', '=', None),