2017-04-01 00:30:53 +02:00
|
|
|
# This file is part of Tryton. The COPYRIGHT file at the toplevel of this
|
|
|
|
|
# repository contains the full copyright notices and license terms.
|
2020-04-10 18:53:44 +02:00
|
|
|
import functools
|
|
|
|
|
|
2017-04-01 00:30:53 +02:00
|
|
|
from trytond.model import MultiValueMixin, ValueMixin, fields
|
2020-04-10 18:53:44 +02:00
|
|
|
from trytond.pool import Pool
|
|
|
|
|
from trytond.pyson import Eval
|
2017-04-01 00:30:53 +02:00
|
|
|
from trytond.transaction import Transaction
|
|
|
|
|
|
2020-04-10 18:53:44 +02:00
|
|
|
__all__ = ['CompanyMultiValueMixin', 'CompanyValueMixin',
|
|
|
|
|
'set_employee', 'reset_employee', 'employee_field']
|
2017-04-01 00:30:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CompanyMultiValueMixin(MultiValueMixin):
|
2019-09-07 23:45:08 +02:00
|
|
|
__slots__ = ()
|
2017-04-01 00:30:53 +02:00
|
|
|
|
2017-04-29 10:39:06 +02:00
|
|
|
def multivalue_records(self, field):
|
|
|
|
|
Value = self.multivalue_model(field)
|
|
|
|
|
records = super(CompanyMultiValueMixin, self).multivalue_records(field)
|
|
|
|
|
if issubclass(Value, CompanyValueMixin):
|
|
|
|
|
# Sort to get record with empty company at the end
|
|
|
|
|
# and so give priority to record with company filled.
|
|
|
|
|
records = sorted(records, key=lambda r: r.company is None)
|
|
|
|
|
return records
|
|
|
|
|
|
2017-04-01 00:30:53 +02:00
|
|
|
def get_multivalue(self, name, **pattern):
|
|
|
|
|
Value = self.multivalue_model(name)
|
|
|
|
|
if issubclass(Value, CompanyValueMixin):
|
|
|
|
|
pattern.setdefault('company', Transaction().context.get('company'))
|
|
|
|
|
return super(CompanyMultiValueMixin, self).get_multivalue(
|
|
|
|
|
name, **pattern)
|
|
|
|
|
|
2021-09-23 10:08:37 +02:00
|
|
|
def set_multivalue(self, name, value, save=True, **pattern):
|
2017-04-01 00:30:53 +02:00
|
|
|
Value = self.multivalue_model(name)
|
|
|
|
|
if issubclass(Value, CompanyValueMixin):
|
|
|
|
|
pattern.setdefault('company', Transaction().context.get('company'))
|
|
|
|
|
return super(CompanyMultiValueMixin, self).set_multivalue(
|
2021-09-23 10:08:37 +02:00
|
|
|
name, value, save=save, **pattern)
|
2017-04-01 00:30:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CompanyValueMixin(ValueMixin):
|
2019-09-07 23:45:08 +02:00
|
|
|
__slots__ = ()
|
2022-10-11 00:44:49 +02:00
|
|
|
company = fields.Many2One('company.company', "Company", ondelete='CASCADE')
|
2020-04-10 18:53:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def employee_field(string, states=None, company='company'):
|
|
|
|
|
if states is None:
|
2020-06-01 10:01:53 +01:00
|
|
|
states = ['done', 'cancel', 'cancelled']
|
2020-04-10 18:53:44 +02:00
|
|
|
return fields.Many2One(
|
|
|
|
|
'company.employee', string,
|
|
|
|
|
domain=[('company', '=', Eval(company, -1))],
|
|
|
|
|
states={
|
|
|
|
|
'readonly': Eval('state').in_(states),
|
2022-04-08 19:07:13 +02:00
|
|
|
})
|
2020-04-10 18:53:44 +02:00
|
|
|
|
|
|
|
|
|
2023-09-17 09:58:08 +02:00
|
|
|
def set_employee(field, company='company', when='after'):
|
2020-04-10 18:53:44 +02:00
|
|
|
def decorator(func):
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
|
def wrapper(cls, records, *args, **kwargs):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
User = pool.get('res.user')
|
|
|
|
|
user = User(Transaction().user)
|
|
|
|
|
|
2023-09-17 09:58:08 +02:00
|
|
|
if when == 'after':
|
|
|
|
|
result = func(cls, records, *args, **kwargs)
|
2020-04-10 18:53:44 +02:00
|
|
|
employee = user.employee
|
|
|
|
|
if employee:
|
|
|
|
|
emp_company = employee.company
|
|
|
|
|
cls.write(
|
|
|
|
|
[r for r in records
|
|
|
|
|
if not getattr(r, field)
|
|
|
|
|
and getattr(r, company) == emp_company], {
|
|
|
|
|
field: employee.id,
|
|
|
|
|
})
|
2023-09-17 09:58:08 +02:00
|
|
|
if when == 'before':
|
|
|
|
|
result = func(cls, records, *args, **kwargs)
|
2020-04-10 18:53:44 +02:00
|
|
|
return result
|
|
|
|
|
return wrapper
|
2023-09-17 09:58:08 +02:00
|
|
|
assert when in {'before', 'after'}
|
2020-04-10 18:53:44 +02:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
2023-09-17 09:58:08 +02:00
|
|
|
def reset_employee(*fields, when='after'):
|
2020-04-10 18:53:44 +02:00
|
|
|
def decorator(func):
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
|
def wrapper(cls, records, *args, **kwargs):
|
2023-09-17 09:58:08 +02:00
|
|
|
if when == 'after':
|
|
|
|
|
result = func(cls, records, *args, **kwargs)
|
2020-04-10 18:53:44 +02:00
|
|
|
cls.write(records, {f: None for f in fields})
|
2023-09-17 09:58:08 +02:00
|
|
|
if when == 'before':
|
|
|
|
|
result = func(cls, records, *args, **kwargs)
|
2020-04-10 18:53:44 +02:00
|
|
|
return result
|
|
|
|
|
return wrapper
|
2023-09-17 09:58:08 +02:00
|
|
|
assert when in {'before', 'after'}
|
2020-04-10 18:53:44 +02:00
|
|
|
return decorator
|