Files
tryton/line.py
T

254 lines
9.1 KiB
Python
Raw Normal View History

2010-01-20 11:05:35 +01:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2013-09-06 22:35:00 +02:00
from sql import Literal
from sql.aggregate import Max, Sum
from sql.conditionals import Coalesce
from sql.functions import Extract
2009-02-28 17:07:12 +01:00
from trytond.model import ModelView, ModelSQL, fields
2012-01-02 18:10:18 +01:00
from trytond.wizard import Wizard, StateView, StateAction, Button
2011-08-04 16:57:47 +02:00
from trytond.pyson import Eval, PYSONEncoder, Date, Get
2010-08-10 11:59:04 -04:00
from trytond.transaction import Transaction
2011-07-03 13:19:21 +02:00
from trytond.pool import Pool
2012-09-24 22:18:28 +02:00
__all__ = ['Line', 'EnterLinesStart', 'EnterLines',
'HoursEmployee',
'OpenHoursEmployeeStart', 'OpenHoursEmployee',
'HoursEmployeeWeekly', 'HoursEmployeeMonthly']
2008-05-29 17:55:36 +02:00
2009-02-28 17:07:12 +01:00
class Line(ModelSQL, ModelView):
2008-05-29 17:55:36 +02:00
'Timesheet Line'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.line'
2008-05-29 17:55:36 +02:00
employee = fields.Many2One('company.employee', 'Employee', required=True,
2012-04-03 20:07:54 +02:00
select=True, domain=[
2013-10-09 18:26:02 +02:00
('company', '=', Eval('context', {}).get('company', -1)),
2012-04-03 20:07:54 +02:00
])
2012-03-05 10:53:10 +01:00
date = fields.Date('Date', required=True, select=True)
2008-05-29 17:55:36 +02:00
hours = fields.Float('Hours', digits=(16, 2), required=True)
2008-06-09 19:30:57 +02:00
work = fields.Many2One('timesheet.work', 'Work',
2013-01-26 15:13:51 +01:00
required=True, select=True, domain=[
('timesheet_available', '=', True),
['OR',
('timesheet_start_date', '=', None),
('timesheet_start_date', '<=', Eval('date')),
],
['OR',
('timesheet_end_date', '=', None),
('timesheet_end_date', '>=', Eval('date')),
],
],
depends=['date'])
2008-05-29 17:55:36 +02:00
description = fields.Char('Description')
2012-09-24 22:18:28 +02:00
@classmethod
def __setup__(cls):
super(Line, cls).__setup__()
cls._sql_constraints += [
('check_move_hours_pos',
'CHECK(hours >= 0.0)', 'Hours field must be positive'),
]
2012-09-24 22:18:28 +02:00
@staticmethod
def default_employee():
User = Pool().get('res.user')
2008-05-29 17:55:36 +02:00
employee_id = None
2010-08-10 11:59:04 -04:00
if Transaction().context.get('employee'):
employee_id = Transaction().context['employee']
2008-05-29 17:55:36 +02:00
else:
2012-09-24 22:18:28 +02:00
user = User(Transaction().user)
2008-05-29 17:55:36 +02:00
if user.employee:
employee_id = user.employee.id
if employee_id:
return employee_id
2008-05-29 17:55:36 +02:00
2012-09-24 22:18:28 +02:00
@staticmethod
def default_date():
Date_ = Pool().get('ir.date')
return Transaction().context.get('date') or Date_.today()
2008-10-08 12:20:33 +02:00
2012-09-24 22:18:28 +02:00
@classmethod
def view_header_get(cls, value, view_type='form'):
2010-08-10 11:59:04 -04:00
if not Transaction().context.get('employee'):
return value
2012-09-24 22:18:28 +02:00
Employee = Pool().get('company.employee')
employee = Employee(Transaction().context['employee'])
2013-03-27 15:47:13 +00:00
return value + " (" + employee.rec_name + ")"
2008-05-29 17:55:36 +02:00
2012-01-02 18:10:18 +01:00
class EnterLinesStart(ModelView):
'Enter Lines'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.line.enter.start'
2008-09-10 09:53:32 +02:00
employee = fields.Many2One('company.employee', 'Employee', required=True,
2013-10-09 18:26:02 +02:00
domain=[('company', '=', Eval('context', {}).get('company', -1))])
date = fields.Date('Date', required=True)
2008-05-29 17:55:36 +02:00
2012-09-24 22:18:28 +02:00
@staticmethod
def default_employee():
Line = Pool().get('timesheet.line')
return Line.default_employee()
2008-05-29 17:55:36 +02:00
2012-09-24 22:18:28 +02:00
@staticmethod
def default_date():
Line = Pool().get('timesheet.line')
return Line.default_date()
2008-05-29 17:55:36 +02:00
class EnterLines(Wizard):
'Enter Lines'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.line.enter'
2012-01-02 18:10:18 +01:00
start = StateView('timesheet.line.enter.start',
'timesheet.line_enter_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Enter', 'enter', 'tryton-ok', default=True),
2008-05-29 17:55:36 +02:00
])
2012-01-02 18:10:18 +01:00
enter = StateAction('timesheet.act_line_form')
2012-09-24 22:18:28 +02:00
def do_enter(self, action):
date = self.start.date
2012-01-02 18:10:18 +01:00
date = Date(date.year, date.month, date.day)
action['pyson_domain'] = PYSONEncoder().encode([
2012-09-24 22:18:28 +02:00
('employee', '=', self.start.employee.id),
2012-01-02 18:10:18 +01:00
('date', '=', date),
])
action['pyson_context'] = PYSONEncoder().encode({
2012-09-24 22:18:28 +02:00
'employee': self.start.employee.id,
2012-01-02 18:10:18 +01:00
'date': date,
})
2012-09-24 22:18:28 +02:00
action['name'] += " - " + self.start.employee.rec_name
2012-01-02 18:10:18 +01:00
return action, {}
2012-09-24 22:18:28 +02:00
def transition_enter(self):
2012-01-02 18:10:18 +01:00
return 'end'
2008-05-29 17:55:36 +02:00
2009-02-28 17:07:12 +01:00
class HoursEmployee(ModelSQL, ModelView):
2008-05-29 17:55:36 +02:00
'Hours per Employee'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.hours_employee'
2012-03-05 10:53:10 +01:00
employee = fields.Many2One('company.employee', 'Employee', select=True)
hours = fields.Float('Hours', digits=(16, 2))
2008-05-29 17:55:36 +02:00
2012-09-24 22:18:28 +02:00
@staticmethod
def table_query():
2013-09-06 22:35:00 +02:00
pool = Pool()
Line = pool.get('timesheet.line')
line = Line.__table__()
where = Literal(True)
2010-08-10 11:59:04 -04:00
if Transaction().context.get('start_date'):
2013-09-06 22:35:00 +02:00
where &= line.date >= Transaction().context['start_date']
2010-08-10 11:59:04 -04:00
if Transaction().context.get('end_date'):
2013-09-06 22:35:00 +02:00
where &= line.date <= Transaction().context['end_date']
return line.select(
line.employee.as_('id'),
Max(line.create_uid).as_('create_uid'),
Max(line.create_date).as_('create_date'),
Max(line.write_uid).as_('write_uid'),
Max(line.write_date).as_('write_date'),
line.employee,
Sum(Coalesce(line.hours, 0)).as_('hours'),
where=where,
group_by=line.employee)
2008-05-29 17:55:36 +02:00
2012-01-02 18:10:18 +01:00
class OpenHoursEmployeeStart(ModelView):
'Open Hours per Employee'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.hours_employee.open.start'
2008-05-29 17:55:36 +02:00
start_date = fields.Date('Start Date')
end_date = fields.Date('End Date')
class OpenHoursEmployee(Wizard):
'Open Hours per Employee'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.hours_employee.open'
2012-01-02 18:10:18 +01:00
start = StateView('timesheet.hours_employee.open.start',
'timesheet.hours_employee_open_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Open', 'open_', 'tryton-ok', default=True),
])
open_ = StateAction('timesheet.act_hours_employee_form')
2012-09-24 22:18:28 +02:00
def do_open_(self, action):
2012-01-02 18:10:18 +01:00
action['pyson_context'] = PYSONEncoder().encode({
2012-09-24 22:18:28 +02:00
'start_date': self.start.start_date,
'end_date': self.start.end_date,
2012-01-02 18:10:18 +01:00
})
return action, {}
2012-09-24 22:18:28 +02:00
def transition_open_(self):
2012-01-02 18:10:18 +01:00
return 'end'
2008-05-29 17:55:36 +02:00
2008-06-17 17:43:39 +02:00
2009-02-28 17:07:12 +01:00
class HoursEmployeeWeekly(ModelSQL, ModelView):
2008-06-17 17:43:39 +02:00
'Hours per Employee per Week'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.hours_employee_weekly'
2012-03-05 10:53:10 +01:00
year = fields.Char('Year', select=True)
week = fields.Integer('Week', select=True)
employee = fields.Many2One('company.employee', 'Employee', select=True)
hours = fields.Float('Hours', digits=(16, 2), select=True)
2008-06-17 17:43:39 +02:00
2012-09-24 22:18:28 +02:00
@classmethod
def __setup__(cls):
super(HoursEmployeeWeekly, cls).__setup__()
cls._order.insert(0, ('year', 'DESC'))
cls._order.insert(1, ('week', 'DESC'))
cls._order.insert(2, ('employee', 'ASC'))
2008-06-17 17:43:39 +02:00
2012-09-24 22:18:28 +02:00
@classmethod
def table_query(cls):
2013-09-06 22:35:00 +02:00
pool = Pool()
Line = pool.get('timesheet.line')
line = Line.__table__()
type_name = cls.year.sql_type().base
year_column = Extract('YEAR', line.date).cast(type_name).as_('year')
week_column = Extract('WEEK', line.date).as_('week')
return line.select(
Max(Extract('WEEK', line.date)
+ Extract('YEAR', line.date) * 100
+ line.employee * 1000000).as_('id'),
Max(line.create_uid).as_('create_uid'),
Max(line.create_date).as_('create_date'),
Max(line.write_uid).as_('write_uid'),
Max(line.write_date).as_('write_date'),
year_column,
week_column,
line.employee,
Sum(Coalesce(line.hours, 0)).as_('hours'),
group_by=(year_column, week_column, line.employee))
2008-06-17 17:43:39 +02:00
2009-02-28 17:07:12 +01:00
class HoursEmployeeMonthly(ModelSQL, ModelView):
2008-06-17 17:43:39 +02:00
'Hours per Employee per Month'
2012-09-24 22:18:28 +02:00
__name__ = 'timesheet.hours_employee_monthly'
2012-03-05 10:53:10 +01:00
year = fields.Char('Year', select=True)
month = fields.Integer('Month', select=True)
employee = fields.Many2One('company.employee', 'Employee', select=True)
hours = fields.Float('Hours', digits=(16, 2), select=True)
2008-06-17 17:43:39 +02:00
2012-09-24 22:18:28 +02:00
@classmethod
def __setup__(cls):
super(HoursEmployeeMonthly, cls).__setup__()
cls._order.insert(0, ('year', 'DESC'))
cls._order.insert(1, ('month', 'DESC'))
cls._order.insert(2, ('employee', 'ASC'))
2008-06-17 17:43:39 +02:00
2012-09-24 22:18:28 +02:00
@classmethod
def table_query(cls):
2013-09-06 22:35:00 +02:00
pool = Pool()
Line = pool.get('timesheet.line')
line = Line.__table__()
type_name = cls.year.sql_type().base
year_column = Extract('YEAR', line.date).cast(type_name).as_('year')
month_column = Extract('MONTH', line.date).as_('month')
return line.select(
Max(Extract('MONTH', line.date)
+ Extract('YEAR', line.date) * 100
+ line.employee * 1000000).as_('id'),
Max(line.create_uid).as_('create_uid'),
Max(line.create_date).as_('create_date'),
Max(line.write_uid).as_('write_uid'),
Max(line.write_date).as_('write_date'),
year_column,
month_column,
line.employee,
Sum(Coalesce(line.hours, 0)).as_('hours'),
group_by=(year_column, month_column, line.employee))