~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to cci_timesheet/cci_timesheet.py

  • Committer: Fabien Pinckaers
  • Date: 2008-11-12 06:43:12 UTC
  • Revision ID: fp@tinyerp.com-20081112064312-fp85io97i1e95tuz
moved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
import time
 
23
import pooler
 
24
from osv import fields, osv
 
25
 
 
26
class cci_timesheet_grant(osv.osv):
 
27
 
 
28
    _name="cci_timesheet.grant"
 
29
    _description="CCI Timesheet Grant"
 
30
    _columns = {
 
31
        'name': fields.char('Grant Name', size=128, required=True),
 
32
        'line_ids': fields.one2many('cci_timesheet.line', 'grant_id', 'Timesheet Lines',),
 
33
        'affectation_ids': fields.one2many('cci_timesheet.affectation', 'grant_id', 'Affectation Lines',),
 
34
    }
 
35
cci_timesheet_grant()
 
36
 
 
37
class cci_timesheet(osv.osv):
 
38
 
 
39
    _name="cci.timesheet"
 
40
    _description="CCI Timesheet"
 
41
 
 
42
    _columns = {
 
43
        'name': fields.char('Name', size=128, required=True,readonly=True,states={'draft':[('readonly',False)]}),
 
44
        'date_from' : fields.date('From Date', required=True,readonly=False,states={'validated':[('readonly',True)],'cancelled':[('readonly',True)]}),
 
45
        'date_to' : fields.date('To Date', required=True,readonly=False,states={'validated':[('readonly',True)],'cancelled':[('readonly',True)]}),
 
46
        'grant_id': fields.many2one('cci_timesheet.grant', 'Grant',readonly=True, required=True,states={'draft':[('readonly',False)]}),
 
47
        'state': fields.selection([('draft', 'Draft'), ('confirmed', 'Confirmed'), ('validated', 'Validated'),('cancelled', 'Cancelled')], 'State', readonly=True, required=True),
 
48
        'sending_date' : fields.date('Sending Date'),
 
49
        'asked_amount' : fields.float('Asked Amount',digits=(16,2)),
 
50
        'accepted_amount' : fields.float('Accepted Amount',digits=(16,2)),
 
51
        'line_ids': fields.one2many('cci_timesheet.line', 'timesheet_id', 'Timesheet Lines',readonly=False,states={'validated':[('readonly',True)],'cancelled':[('readonly',True)]}),
 
52
    }
 
53
    _defaults = {
 
54
        'state' : lambda *a: 'draft',
 
55
    }
 
56
 
 
57
    def check_timesheet_line(self, cr, uid, ids):
 
58
        if not ids:
 
59
            return True
 
60
        for data in self.browse(cr, uid, ids):
 
61
            for lines in data.line_ids:
 
62
                if not lines.grant_id:
 
63
                    return False
 
64
                if (data.grant_id.id != lines.grant_id.id) or lines.day_date < data.date_from or lines.day_date > data.date_to:
 
65
                    return False
 
66
        return True
 
67
 
 
68
    def set_to_draft(self, cr, uid, ids, *args):
 
69
        self.write(cr, uid, ids, {'state':'draft'})
 
70
        return True
 
71
 
 
72
    def set_to_validate(self, cr, uid, ids, *args):
 
73
        self.write(cr, uid, ids, {'state':'validated'})
 
74
 
 
75
        return True
 
76
 
 
77
    def set_to_confirm(self, cr, uid, ids, *args):
 
78
        self.write(cr, uid, ids, {'state':'confirmed'})
 
79
        return True
 
80
 
 
81
    def set_to_cancel(self, cr, uid, ids, *args):
 
82
        self.write(cr, uid, ids, {'state':'cancelled'})
 
83
        return True
 
84
 
 
85
    _constraints = [(check_timesheet_line, 'Warning: Date of Timesheet Line should be bewteen the Two dates of the Timesheet and Grant of Timesheet Line should be the same as the Timesheet', ['Timesheet lines'])]
 
86
 
 
87
cci_timesheet()
 
88
 
 
89
class cci_timesheet_line(osv.osv):
 
90
 
 
91
 
 
92
    def _get_diff_hours(self, cr, uid, ids, name, args, context=None):
 
93
        res={}
 
94
        for line in self.browse(cr, uid, ids, context):
 
95
            res[line.id] = line.hour_to - line.hour_from
 
96
        return res
 
97
 
 
98
    def check_timesheet(self, cr, uid, ids):
 
99
        if not ids:
 
100
            return True
 
101
        for lines in self.browse(cr, uid, ids):
 
102
            if lines.timesheet_id:
 
103
                if not lines.grant_id:
 
104
                    return False
 
105
                if (lines.timesheet_id.grant_id.id != lines.grant_id.id) or lines.day_date < lines.timesheet_id.date_from or lines.day_date > lines.timesheet_id.date_to:
 
106
                    return False
 
107
        return True
 
108
 
 
109
    #TODO: by default, the grant_id should be the grant defined on the timesheet
 
110
 
 
111
    _name="cci_timesheet.line"
 
112
    _description="CCI Timesheet Line"
 
113
 
 
114
    _columns = {
 
115
        'name': fields.char('Name', size=128, required=True),
 
116
        'day_date' : fields.date('Date of the Day', required=True),
 
117
        'hour_from' : fields.float('Hour From', required=True),
 
118
        'hour_to' : fields.float('Hour To',required=True),
 
119
        'user_id': fields.many2one('res.users', 'User', required=True),
 
120
        'grant_id': fields.many2one('cci_timesheet.grant', 'Grant',),
 
121
        'timesheet_id': fields.many2one('cci.timesheet', 'Timesheet', ondelete='cascade'),
 
122
        'zip_id': fields.many2one('res.partner.zip', 'Zip'),
 
123
        'partner_id': fields.many2one('res.partner', 'Partner'),
 
124
        'contact_id': fields.many2one('res.partner.contact', 'Contact'),
 
125
        'description': fields.text('Description'),
 
126
        'suppl_cost' : fields.float('Supplementary Cost',digits=(16,2)),
 
127
        'kms' : fields.integer('Kilometers'),
 
128
        'diff_hours' : fields.function(_get_diff_hours, method=True, string='Hour To - Hour From',type='float'),
 
129
    }
 
130
    _constraints = [(check_timesheet, 'Warning: Date of Timesheet Line should be bewteen the Two dates of the Timesheet and Grant of Timesheet Line should be the same as the Timesheet', ['Timesheet lines'])]
 
131
cci_timesheet_line()
 
132
 
 
133
class cci_timesheet_affectation(osv.osv):
 
134
    _name="cci_timesheet.affectation"
 
135
    _description="Timesheet Affectation"
 
136
 
 
137
    _columns = {
 
138
        'name': fields.char('Name', size=128, required=True),
 
139
        'user_id': fields.many2one('res.users', 'User', required=True),
 
140
        'grant_id': fields.many2one('cci_timesheet.grant', 'Grant', required=True),
 
141
        'percentage' : fields.float('Percentage', digits=(16,2),required=True),
 
142
        'hours_per_week' : fields.float('Hours Per Week',size=9, required=True),
 
143
        'date_from' : fields.date('From Date', required=True),
 
144
        'date_to' : fields.date('To Date', required=True),
 
145
        'rate' : fields.float('Rate', digits=(16,2), required=True),
 
146
    }
 
147
 
 
148
cci_timesheet_affectation()
 
149
 
 
150
 
 
151
class report_timesheet_affectation(osv.osv):
 
152
    _name = "report.timesheet.affectation"
 
153
    _description = "Report on Timesheet and Affectation"
 
154
    _auto = False
 
155
    _columns = {
 
156
        'name': fields.char('Name', size=128),
 
157
        'day_date' : fields.date('Date of the Day'),
 
158
        'hour_from' : fields.float('Hour From'),
 
159
        'hour_to' : fields.float('Hour To'),
 
160
        'user_name':  fields.char('Employee', size=32),
 
161
        'grant_name': fields.char('Grant', size=128),
 
162
        'timesheet_id': fields.integer('Timesheet Ref'),
 
163
        'description': fields.text('Description'),
 
164
        'diff_hours' : fields.float('Hours'),
 
165
 
 
166
        'affectation_name' : fields.char('Affectation', size=128),
 
167
        'th_percentage' : fields.float('Percentage'),
 
168
        'hours_per_week' : fields.float('Hours Per Week'),
 
169
        'date_from' : fields.date('From Date'),
 
170
        'date_to' : fields.date('To Date'),
 
171
        'rate' : fields.float('Rate'),
 
172
    }
 
173
 
 
174
    def init(self, cr):
 
175
        cr.execute("""
 
176
            create or replace view report_timesheet_affectation as (
 
177
            SELECT
 
178
                line.id as id,
 
179
                line.name as name,
 
180
                line.day_date as day_date,
 
181
                line.hour_from as hour_from,
 
182
                line.hour_to as hour_to,
 
183
                u.name as user_name,
 
184
                g.name as grant_name,
 
185
                line.timesheet_id as timesheet_id,
 
186
                line.description as description,
 
187
                (line.hour_to - line.hour_from) as diff_hours,
 
188
                affect.name as affectation_name,
 
189
                affect.percentage as th_percentage,
 
190
                affect.hours_per_week as hours_per_week,
 
191
                affect.date_from as date_from,
 
192
                affect.date_to as date_to,
 
193
                affect.rate as rate
 
194
            FROM
 
195
                cci_timesheet_line line,
 
196
                cci_timesheet_affectation affect,
 
197
                res_users u,
 
198
                cci_timesheet_grant g
 
199
            WHERE
 
200
                line.user_id = affect.user_id
 
201
                AND line.user_id = u.id
 
202
                AND line.grant_id = affect.grant_id
 
203
                AND line.grant_id = g.id
 
204
                AND (line.day_date <= affect.date_to AND line.day_date >= affect.date_from)
 
205
            )""")
 
206
 
 
207
report_timesheet_affectation()
 
208
 
 
209
class crm_case(osv.osv):
 
210
 
 
211
    _inherit = 'crm.case'
 
212
    _description = 'crm case'
 
213
    _columns = {
 
214
        'grant_id' : fields.many2one('cci_timesheet.grant','Grant'),
 
215
        'zip_id' : fields.many2one('res.partner.zip','Zip'),
 
216
        'timesheet_line_id':fields.many2one('cci_timesheet.line','Timesheet Line')
 
217
    }
 
218
 
 
219
    def onchange_partner_id(self, cr, uid, ids, part, email=False):
 
220
        data = super(crm_case, self).onchange_partner_id(cr, uid, ids, part, email)
 
221
        if not part:
 
222
            return data
 
223
        addr = self.pool.get('res.partner').address_get(cr, uid, [part])
 
224
        if addr['default']:
 
225
            data['value']['zip_id'] = self.pool.get('res.partner.address').browse(cr, uid, addr['default']).zip_id.id
 
226
        return data
 
227
crm_case()
 
228
 
 
229
class project_work(osv.osv):
 
230
    _inherit = "project.task.work"
 
231
    _description = "Task Work"
 
232
 
 
233
    def create(self, cr, uid, vals, *args, **kwargs):
 
234
        res = super(project_work, self).create(cr, uid, vals)
 
235
        self.write(cr, uid, [res], vals)
 
236
        return res
 
237
 
 
238
    def write(self, cr, uid, ids, vals, *args, **kwargs):
 
239
        res = {}
 
240
        if not ids:
 
241
            return res
 
242
        for work in self.browse(cr, uid, ids):
 
243
            if (not work.zip_id) and ('zip_id' not in vals or not vals['zip_id']):
 
244
                if work.task_id.project_id.partner_id:
 
245
                    temp = self.pool.get('res.partner').address_get(cr, uid, [work.task_id.project_id.partner_id.id])
 
246
                    vals['zip_id'] = self.pool.get('res.partner.address').browse(cr, uid, temp['default']).zip_id.id
 
247
            if (not work.partner_id) and ('partner_id' not in vals or not vals['partner_id']):
 
248
                if work.task_id.project_id.partner_id:
 
249
                    vals['partner_id'] = work.task_id.project_id.partner_id.id
 
250
 
 
251
            if (not work.contact_id) and ('contact_id' not in vals or not vals['contact_id']):
 
252
                if work.task_id.project_id.contact_id2:
 
253
                    vals['contact_id'] = work.task_id.project_id.contact_id2.id
 
254
        return super(project_work, self).write(cr, uid, ids, vals)
 
255
 
 
256
 
 
257
    _columns = {
 
258
        'grant_id' : fields.many2one('cci_timesheet.grant','Grant'),
 
259
        'zip_id' : fields.many2one('res.partner.zip','Zip'),
 
260
        'partner_id' : fields.many2one('res.partner','Partner'),
 
261
        'contact_id' : fields.many2one('res.partner.contact','Contact'),
 
262
        'timesheet_line_id':fields.many2one('cci_timesheet.line','Timesheet Line')
 
263
    }
 
264
    _defaults = {
 
265
#        'zip_id': _get_zip_id,
 
266
 #       'partner_id': _get_partner_id,
 
267
  #      'contact_id': _get_contact_id,
 
268
    }
 
269
project_work()
 
270
 
 
271
 
 
272
 
 
273
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
274