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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
# 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
from sql import Literal
from sql.conditionals import Coalesce
from trytond.model import Model, fields
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, If
from trytond.transaction import Transaction
class PeriodMixin(Model):
start_date = fields.Date(
"Start Date",
domain=[
If(Eval('start_date') & Eval('end_date'),
('start_date', '<=', Eval('end_date')),
()),
])
end_date = fields.Date(
"End Date",
domain=[
If(Eval('start_date') & Eval('end_date'),
('end_date', '>=', Eval('start_date')),
()),
])
@classmethod
def __setup__(cls):
super().__setup__()
if (hasattr(cls, 'parent')
and hasattr(cls, 'childs')
and hasattr(cls, 'company')):
cls.parent.domain = [
('company', '=', Eval('company', 0)),
['OR',
If(Bool(Eval('start_date')),
('start_date', '>=', Eval('start_date', None)),
()),
('start_date', '=', None),
],
['OR',
If(Bool(Eval('end_date')),
('end_date', '<=', Eval('end_date', None)),
()),
('end_date', '=', None),
],
]
cls.parent.depends.update({'company', 'start_date', 'end_date'})
cls.childs.domain = [
('company', '=', Eval('company', 0)),
If(Bool(Eval('start_date')),
('start_date', '>=', Eval('start_date', None)),
()),
If(Bool(Eval('end_date')),
('end_date', '<=', Eval('end_date', None)),
()),
]
cls.childs.depends.update({'company', 'start_date', 'end_date'})
class ActivePeriodMixin(PeriodMixin):
active = fields.Function(fields.Boolean("Active"), 'on_change_with_active')
@classmethod
def _active_dates(cls):
pool = Pool()
Date = pool.get('ir.date')
FiscalYear = pool.get('account.fiscalyear')
Period = pool.get('account.period')
context = Transaction().context
today = Date.today()
date = context.get('date')
from_date, to_date = context.get('from_date'), context.get('to_date')
period_ids = context.get('periods')
fiscalyear_id = context.get('fiscalyear')
if date:
fiscalyears = FiscalYear.search([
('start_date', '<=', date),
('end_date', '>=', date),
])
elif from_date or to_date:
domain = []
if from_date:
domain.append(('end_date', '>=', from_date))
if to_date:
domain.append(('start_date', '<=', to_date))
fiscalyears = FiscalYear.search(domain)
elif period_ids:
periods = Period.browse(period_ids)
fiscalyears = list(set(p.fiscalyear for p in periods))
elif fiscalyear_id:
fiscalyears = FiscalYear.browse([fiscalyear_id])
else:
fiscalyears = FiscalYear.search([
('start_date', '<=', today),
('end_date', '>=', today),
], limit=1)
if not fiscalyears:
return (from_date or date or today, to_date or date or today)
return (
min(f.start_date for f in fiscalyears),
max(f.end_date for f in fiscalyears))
@classmethod
def default_active(cls):
return True
@fields.depends('start_date', 'end_date')
def on_change_with_active(self, name=None):
from_date, to_date = self._active_dates()
start_date = self.start_date or datetime.date.min
end_date = self.end_date or datetime.date.max
return (start_date <= to_date <= end_date
or start_date <= from_date <= end_date
or (from_date <= start_date and end_date <= to_date))
@classmethod
def domain_active(cls, domain, tables):
table, _ = tables[None]
_, operator, value = domain
from_date, to_date = cls._active_dates()
start_date = Coalesce(table.start_date, datetime.date.min)
end_date = Coalesce(table.end_date, datetime.date.max)
expression = (((start_date <= to_date) & (end_date >= to_date))
| ((start_date <= from_date) & (end_date >= from_date))
| ((start_date >= from_date) & (end_date <= to_date)))
if operator in {'=', '!='}:
if (operator == '=') != value:
expression = ~expression
elif operator in {'in', 'not in'}:
if True in value and False not in value:
pass
elif False in value and True not in value:
expression = ~expression
else:
expression = Literal(True)
else:
expression = Literal(True)
return expression
class ContextCompanyMixin(Model):
context_company = fields.Function(fields.Boolean("Context Company"),
'get_context_company')
def get_context_company(self, name):
context = Transaction().context
return self.company.id == context.get('company')
@classmethod
def domain_context_company(cls, domain, tables):
context = Transaction().context
table, _ = tables[None]
_, operator, value = domain
expression = table.company == context.get('company')
if operator in {'=', '!='}:
if (operator == '=') != value:
expression = ~expression
elif operator in {'in', 'not in'}:
if True in value and False not in value:
pass
elif False in value and True not in value:
expression = ~expression
else:
expression = Literal(True)
else:
expression = Literal(True)
return expression
|