Files
tryton/work.py
T

214 lines
6.8 KiB
Python
Raw Normal View History

2009-02-28 17:07:12 +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.
from trytond.model import ModelView, ModelSQL, fields
2012-01-02 18:10:18 +01:00
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.pyson import PYSONEncoder, Not, Bool, Eval
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
2008-05-29 17:55:36 +02:00
2009-02-28 17:07:12 +01:00
class Work(ModelSQL, ModelView):
2008-06-09 16:54:33 +02:00
'Work'
_name = 'timesheet.work'
2008-05-29 17:55:36 +02:00
_description = __doc__
name = fields.Char('Name', required=True)
2008-06-02 17:18:11 +02:00
active = fields.Boolean('Active')
2009-06-03 16:32:58 +02:00
parent = fields.Many2One('timesheet.work', 'Parent', left="left",
2012-03-05 10:53:10 +01:00
right="right", select=True, ondelete="RESTRICT")
left = fields.Integer('Left', required=True, select=True)
right = fields.Integer('Right', required=True, select=True)
2009-05-22 19:11:29 +02:00
children = fields.One2Many('timesheet.work', 'parent', 'Children')
2010-03-11 16:55:07 +01:00
hours = fields.Function(fields.Float('Timesheet Hours', digits=(16, 2),
help="Total time spent on this work"), 'get_hours')
2009-06-03 16:33:29 +02:00
timesheet_available = fields.Boolean('Available on timesheets',
states={
'readonly': Bool(Eval('timesheet_lines', [0])),
},
help="Allow to fill in timesheets with this work")
2012-03-14 09:35:19 +01:00
company = fields.Many2One('company.company', 'Company', required=True,
select=True)
timesheet_lines = fields.One2Many('timesheet.line', 'work',
2011-08-04 16:57:47 +02:00
'Timesheet Lines',
depends=['timesheet_available', 'active'],
states={
'invisible': Not(Bool(Eval('timesheet_available'))),
'readonly': Not(Bool(Eval('active'))),
})
2008-06-02 17:18:11 +02:00
def __init__(self):
2008-06-09 16:54:33 +02:00
super(Work, self).__init__()
self._constraints += [
2008-08-05 11:36:49 +02:00
('check_recursion', 'recursive_works'),
2009-05-22 19:11:29 +02:00
('check_parent_company', 'parent_company'),
]
2008-08-05 11:36:49 +02:00
self._error_messages.update({
'recursive_works': 'You can not create recursive works!',
2009-05-22 19:11:29 +02:00
'parent_company': 'Every work must be in the same company '\
'as it\'s parent work!',
2008-08-05 11:36:49 +02:00
})
2010-08-10 11:59:04 -04:00
def default_active(self):
2008-06-02 17:18:11 +02:00
return True
2012-03-30 13:05:54 +02:00
def default_left(self):
return 0
def default_right(self):
return 0
2010-08-10 11:59:04 -04:00
def default_timesheet_available(self):
2009-05-22 19:11:29 +02:00
return True
2008-06-02 17:18:11 +02:00
2010-08-10 11:59:04 -04:00
def default_company(self):
2012-03-27 10:44:13 +02:00
return Transaction().context.get('company')
2008-06-09 19:17:07 +02:00
2010-08-10 11:59:04 -04:00
def check_parent_company(self, ids):
for work in self.browse(ids):
2009-05-22 19:11:29 +02:00
if not work.parent:
continue
if work.parent.company.id != work.company.id:
return False
return True
def _tree_qty(self, hours_by_wt, children, ids, to_compute):
2008-06-03 01:57:48 +02:00
res = 0
for h in ids:
2009-05-22 19:11:29 +02:00
if (not children.get(h)) or (not to_compute[h]):
2008-06-03 01:57:48 +02:00
res += hours_by_wt.setdefault(h, 0)
else:
sub_qty = self._tree_qty(
2009-05-22 19:11:29 +02:00
hours_by_wt, children, children[h], to_compute)
2008-06-03 01:57:48 +02:00
hours_by_wt.setdefault(h, 0)
hours_by_wt[h] += sub_qty
res += hours_by_wt[h]
to_compute[h] = False
return res
def get_hours(self, ids, name):
2010-08-10 11:59:04 -04:00
all_ids = self.search([
('parent', 'child_of', ids),
])
# force inactive ids to be in all_ids
all_ids = all_ids + ids
2008-06-09 16:54:33 +02:00
clause = "SELECT work, sum(hours) FROM timesheet_line "\
"WHERE work IN (%s) "\
2009-08-26 15:57:29 +02:00
% ",".join(('%s',) * len(all_ids))
2008-06-03 01:57:48 +02:00
date_cond = ""
args = []
2010-08-10 11:59:04 -04:00
if Transaction().context.get('from_date'):
2008-06-03 15:14:09 +02:00
date_cond = " AND date >= %s"
2010-08-10 11:59:04 -04:00
args.append(Transaction().context['from_date'])
if Transaction().context.get('to_date'):
2008-06-03 15:14:09 +02:00
date_cond += " AND date <= %s"
2010-08-10 11:59:04 -04:00
args.append(Transaction().context['to_date'])
2008-06-09 16:54:33 +02:00
clause += date_cond + " GROUP BY work"
2008-06-03 01:57:48 +02:00
2010-08-10 11:59:04 -04:00
Transaction().cursor.execute(clause, all_ids + args)
2008-06-03 01:57:48 +02:00
2012-03-30 16:10:58 +02:00
hours_by_wt = dict((i[0], i[1]) for i in
2010-08-10 11:59:04 -04:00
Transaction().cursor.fetchall())
2008-06-03 01:57:48 +02:00
to_compute = dict.fromkeys(all_ids, True)
2010-08-10 11:59:04 -04:00
works = self.browse(all_ids)
2009-05-22 19:11:29 +02:00
children = {}
2008-06-09 16:54:33 +02:00
for work in works:
if work.parent:
2009-05-22 19:11:29 +02:00
children.setdefault(work.parent.id, []).append(work.id)
self._tree_qty(hours_by_wt, children, ids, to_compute)
2008-06-03 01:57:48 +02:00
return hours_by_wt
def get_rec_name(self, ids, name):
2008-06-02 17:18:11 +02:00
if not ids:
return {}
res = {}
2012-04-03 20:07:54 +02:00
2008-06-09 16:54:33 +02:00
def _name(work):
if work.parent:
return _name(work.parent) + '\\' + work.name
2008-06-02 17:18:11 +02:00
else:
2008-06-09 16:54:33 +02:00
return work.name
2010-08-10 11:59:04 -04:00
for work in self.browse(ids):
res[work.id] = _name(work)
2008-06-02 17:18:11 +02:00
return res
def copy(self, ids, default=None):
if default is None:
default = {}
default = default.copy()
if 'timesheet_lines' not in default:
2012-03-27 10:44:13 +02:00
default['timesheet_lines'] = None
return super(Work, self).copy(ids, default=default)
2010-08-10 11:59:04 -04:00
def write(self, ids, vals):
2008-06-02 17:18:11 +02:00
child_ids = None
if not vals.get('active', True):
2010-08-10 11:59:04 -04:00
child_ids = self.search([
2008-06-02 17:18:11 +02:00
('parent', 'child_of', ids),
2010-08-10 11:59:04 -04:00
])
res = super(Work, self).write(ids, vals)
2008-06-02 17:18:11 +02:00
if child_ids:
2010-08-10 11:59:04 -04:00
self.write(child_ids, {
2008-06-02 17:18:11 +02:00
'active': False,
2010-08-10 11:59:04 -04:00
})
2008-06-02 17:18:11 +02:00
return res
2008-05-29 17:55:36 +02:00
2008-06-09 16:54:33 +02:00
Work()
2008-06-03 01:57:48 +02:00
2012-01-02 18:10:18 +01:00
class OpenWorkStart(ModelView):
'Open Work'
_name = 'timesheet.work.open.start'
_description = __doc__
2008-06-03 01:57:48 +02:00
from_date = fields.Date('From Date')
to_date = fields.Date('To Date')
2012-01-02 18:10:18 +01:00
OpenWorkStart()
2008-06-03 01:57:48 +02:00
2008-06-09 16:54:33 +02:00
class OpenWork(Wizard):
'Open Work'
_name = 'timesheet.work.open'
2008-06-03 01:57:48 +02:00
2012-01-02 18:10:18 +01:00
start = StateView('timesheet.work.open.start',
'timesheet.work_open_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Open', 'open_', 'tryton-ok', default=True),
])
open_ = StateAction('timesheet.act_work_hours_board')
def do_open_(self, session, action):
action['pyson_context'] = PYSONEncoder().encode({
'from_date': session.start.from_date,
'to_date': session.start.to_date,
})
return action, {}
def transition_open_(self, session):
return 'end'
2008-06-03 01:57:48 +02:00
2008-06-09 16:54:33 +02:00
OpenWork()
2008-06-09 16:54:33 +02:00
class OpenWork2(OpenWork):
_name = 'timesheet.work.open2'
2012-01-02 18:10:18 +01:00
open_ = StateAction('timesheet.act_work_form2')
2008-06-09 16:54:33 +02:00
OpenWork2()
2008-06-09 16:54:33 +02:00
class OpenWorkGraph(Wizard):
_name = 'timesheet.work.open_graph'
2012-01-02 18:10:18 +01:00
start_state = 'open_'
open_ = StateAction('timesheet.act_work_form3')
2012-01-02 18:10:18 +01:00
def do_open_(self, session, action):
2011-07-03 13:19:21 +02:00
pool = Pool()
work_obj = pool.get('timesheet.work')
2010-08-10 11:59:04 -04:00
if 'active_id' in Transaction().context:
work = work_obj.browse(Transaction().context['active_id'])
2012-01-02 18:10:18 +01:00
action['name'] = action['name'] + ' - ' + work.rec_name
return action, {}
2008-06-09 16:54:33 +02:00
OpenWorkGraph()