Par défaut, les paliers sont de type 'normal'
[OpenERP/cmmi.git] / projet.py
1 #-*- coding: utf8 -*-
2 '''
3 '''
4
5 from openerp.osv import osv, fields
6
7
8 class Projet(osv.Model):
9
10     _name = "cmmi.projet"
11
12     _description = "Table des projets."
13
14     def _get_main_domain(self, cr, uid, ids, field_name=None, arg=None, context=None):
15         if type(ids) in (int, float):
16             ids = [ids]
17
18         projects = self.browse(cr, uid, ids, context=context)
19
20         result = {}
21
22         for project in projects:
23             for domain in project.domains:
24                 if domain.main:
25                     result[project.id] = domain.name
26                     break
27             else:
28                 result[project.id] = "Any domain is selected as main domain yet"
29
30         return result
31
32
33     def _get_main_moa(self, cr, uid, ids, field_name=None, arg=None, context=None):
34         if type(ids) in (int, float):
35             ids = [ids]
36
37         projects = self.browse(cr, uid, ids, context=context)
38
39         result = {}
40
41         for project in projects:
42             for moa in project.moa_ids:
43                 if moa.main:
44                     result[project.id] = moa.name
45                     break
46             else:
47                 result[project.id] = "Any MOA is selected as main MOA yet"
48
49         return result
50
51
52     def _get_main_moe(self, cr, uid, ids, field_name=None, arg=None, context=None):
53         if type(ids) in (int, float):
54             ids = [ids]
55
56         projects = self.browse(cr, uid, ids, context=context)
57
58         result = {}
59
60         for project in projects:
61             for moe in project.moe_ids:
62                 if moe.main:
63                     result[project.id] = moe.name
64                     break
65             else:
66                 result[project.id] = "Any MOE is selected as main MOE yet"
67         return result
68
69
70     def _get_phases_selectionnees(self, cr, uid, ids, field_name=None, arg=None, context=None):
71         result = {}
72         for projet in self.browse(cr, uid, ids, context=context):
73             res = []
74             for phase in projet.phases:
75                 #import pdb; pdb.set_trace()
76                 if phase.selectionne:
77                     res.append(phase.id)
78             result[projet.id] = res
79         return result
80
81
82     _columns = {
83         "name": fields.char(string="Title", size=64, required=True),
84         "description": fields.text(string="Description"),
85         # Backrefs
86         "domains": fields.one2many("cmmi.projet.domaine",
87                                     "project_id",
88                                     string="Domaines"),
89         "moe_ids": fields.one2many("cmmi.projet.moe",
90                                    "project_id",
91                                    string="MOEs"),
92         "moa_ids": fields.one2many("cmmi.projet.moa",
93                                    "project_id",
94                                    string="MOAs"),
95         "team_members": fields.one2many("cmmi.projet.teammember",
96                                          "projet_id",
97                                          string="Team Members"),
98         "modules": fields.one2many("cmmi.description.module",
99                                    "projet_id",
100                                    string="Modules"),
101         "chantiers": fields.one2many("cmmi.axes.chantier",
102                                      "projet_id",
103                                      string="Chantiers"),
104         "paliers": fields.one2many("cmmi.axes.palier",
105                                    "projet_id",
106                                    string="Paliers"),
107         "phases": fields.one2many("cmmi.projet.phase",
108                                   "projet_id",
109                                   string="Phases"),
110         "evolutions": fields.one2many("cmmi.evolution",
111                                       "projet_id",
112                                       string="Evolutions"),
113
114         # Champs fonction rapatriant les mo ou domaine principaux
115         "moe_id": fields.function(_get_main_moe,
116                                   type="string",
117                                   string="MOE principale"),
118         "moa_id": fields.function(_get_main_moa,
119                                   type="string",
120                                   string="MOA principale"),
121         "main_domain": fields.function(_get_main_domain,
122                                        type="string",
123                                        string="Domaine principal"),
124         "phases_selectionnees": fields.function(_get_phases_selectionnees,
125                                                 type="one2many",
126                                                 obj="cmmi.projet.phase",
127                                                 string="Phases sélectionnées"),
128     }
129
130
131     def create(self, cr, uid, vals, context=None):
132         project_id = osv.Model.create(self, cr, uid, vals, context=context)
133
134         # Récupération des ids de toutes les phases
135         phase_model = self.pool.get("cmmi.phase")
136         phases_ids = phase_model.search(cr, uid, [])
137
138         # Création des relations
139         projet_phase_model = self.pool.get("cmmi.projet.phase")
140         for phase_id in phases_ids:
141             projet_phase_model.create(
142                 cr,
143                 uid,
144                 {
145                     'projet_id': project_id,
146                     'phase_id': phase_id,
147                     'selectionne': False,
148                 }
149             )
150
151         return project_id
152
153
154     def action_add_domain(self, cr, uid, ids, context=None):
155         pass
156
157     def action_add_moe(self, cr, uid, ids, context=None):
158         pass
159
160     def action_add_moa(self, cr, uid, ids, context=None):
161         pass
162
163
164
165 class ProjetDomaine(osv.Model):
166
167     _name = "cmmi.projet.domaine"
168
169     _description = "Rattachement des domaines a un projet."
170
171     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
172         if isinstance(ids, (int, long)):
173             ids = [ids]
174         #return {i: r.domaine_id.name for i, r in
175         #        zip(ids, self.browse(cr, uid, ids, context=context))}
176         return dict([(i, r.domaine_id.name) for i, r in
177                 zip(ids, self.browse(cr, uid, ids, context=context))])
178
179     _columns = {
180         "name": fields.function(_get_name,
181                                 type='char',
182                                 store=True,
183                                 string="Nom du domaine"),
184         "main": fields.boolean(string="Domaine principal ?"),
185         "project_id": fields.many2one("cmmi.projet",
186                                       string="Projet"),
187         "domaine_id": fields.many2one("cmmi.description.domaine",
188                                       string="Domaine"),
189     }
190
191     def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
192         if not main:
193             return {'value': {'main': True},
194                     'warning': {
195                         'title'   : "Integrity Warning",
196                         'message' : "One of the domains should be the main domain",
197                     }
198                 }
199         ids = self.search(
200             cr,
201             uid,
202             [
203                 ('project_id', '=', project),
204                 ('domaine_id', '!=', domaine),
205             ],
206             context=context,
207         )
208         current_id = self.search(
209             cr,
210             uid,
211             [
212                 ('project_id', '=', project),
213                 ('domaine_id', '=', domaine),
214             ],
215             context=context,
216         )
217         self.write(cr, uid, ids, {'main': False}, context=context)
218         self.write(cr, uid, current_id, {'main': True}, context=context)
219
220         return {'value': {'main': True}}
221
222
223
224 class ProjetMoe(osv.Model):
225     _name = "cmmi.projet.moe"
226
227     _description = "Rattachement des MOEs au projet."
228
229     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
230         if isinstance(ids, (int, long)):
231             ids = [ids]
232         return dict([(i, r.moe_id.name) for i, r in
233                 zip(ids, self.browse(cr, uid, ids, context=context))])
234
235     _columns = {
236         "name": fields.function(_get_name,
237                                 type='char',
238                                 store=True, # Permet d'enregistrer le champ.
239                                 string="Nom de la MOE"),
240         "main": fields.boolean(string="MOE principale ?"),
241         "project_id": fields.many2one("cmmi.projet",
242                                       string="Projet",
243                                       required=True),
244         "moe_id": fields.many2one("cmmi.mo.moe",
245                                   string="MOE",
246                                   required=True),
247     }
248
249     def onchange_main(self, cr, uid, ids, project, moe, main, context=None):
250         if not main:
251             return {'value': {'main': True},
252                     'warning': {
253                         'title'   : "Integrity Warning",
254                         'message' : "Une des MOE doit être la MOE principale",
255                     }
256                 }
257         ids = self.search(
258             cr,
259             uid,
260             [
261                 ('project_id', '=', project),
262                 ('moe_id', '!=', moe),
263             ],
264             context=context,
265         )
266         current_id = self.search(
267             cr,
268             uid,
269             [
270                 ('project_id', '=', project),
271                 ('moe_id', '=', moe),
272             ],
273             context=context,
274         )
275         self.write(cr, uid, ids, {'main': False}, context=context)
276         self.write(cr, uid, current_id, {'main': True}, context=context)
277
278         return {'value': {'main': True}}
279
280
281
282 class ProjetMoa(osv.Model):
283     _name = "cmmi.projet.moa"
284
285     _description = "Rattachement des MOAs a une projet."
286
287     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
288         if isinstance(ids, (int, long)):
289             ids = [ids]
290         return dict([(i, r.moa_id.name) for i, r in
291                 zip(ids, self.browse(cr, uid, ids, context=context))])
292
293     _columns = {
294         "name": fields.function(_get_name,
295                                 type='char',
296                                 store=True,
297                                 string="Nom de la MOA"),
298         "main": fields.boolean(string="MOA principale ?"),
299         "project_id": fields.many2one("cmmi.projet",
300                                       string="Projet",
301                                       required=True),
302         "moa_id": fields.many2one("cmmi.mo.moa",
303                                   string="MOA",
304                                   required=True),
305     }
306
307     def onchange_main(self, cr, uid, ids, project, moa, main, context=None):
308         if not main:
309             return {'value': {'main': True},
310                     'warning': {
311                         'title'   : "Integrity Warning",
312                         'message' : "Une des MOA doit être la MOA principale",
313                     }
314                 }
315         ids = self.search(
316             cr,
317             uid,
318             [
319                 ('project_id', '=', project),
320                 ('moa_id', '!=', moa),
321             ],
322             context=context,
323         )
324         current_id = self.search(
325             cr,
326             uid,
327             [
328                 ('project_id', '=', project),
329                 ('moa_id', '=', moa),
330             ],
331             context=context,
332         )
333         self.write(cr, uid, ids, {'main': False}, context=context)
334         self.write(cr, uid, current_id, {'main': True}, context=context)
335
336         return {'value': {'main': True}}
337
338
339
340 class ProjetPhase(osv.Model):
341     _name = "cmmi.projet.phase"
342
343     _description = "Rattachement des Phases aux projets"
344
345     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
346         if isinstance(ids, (int, long)):
347             ids = [ids]
348         return dict([(i, r.phase_id.name) for i, r in
349                 zip(ids, self.browse(cr, uid, ids, context=context))])
350
351     _columns = {
352         "name": fields.function(_get_name,
353                                 type='char',
354                                 store=True,
355                                 string="Nom de la phase"),
356         "phase_id": fields.many2one("cmmi.phase",
357                                     string="Phase",
358                                     required=True),
359         "projet_id": fields.many2one("cmmi.projet",
360                                      string="Projet",
361                                      required=True),
362         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
363     }
364
365
366
367 class ProjetTeammember(osv.Model):
368     _name = "cmmi.projet.teammember"
369
370     _description = "Rattachement des Team members a un projet."
371
372     def _get_partner_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
373         if isinstance(ids, (int, long)):
374             ids = [ids]
375         return dict([(i, r.partner_id.name) for i, r in
376                 zip(ids, self.browse(cr, uid, ids, context=context))])
377
378     def _get_project_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
379         if isinstance(ids, (int, long)):
380             ids = [ids]
381         return dict([(i, r.projet_id.name) for i, r in
382                 zip(ids, self.browse(cr, uid, ids, context=context))])
383
384     _columns = {
385         "name": fields.function(_get_partner_name,
386                                 type='char',
387                                 store=True,
388                                 string="Nom du partner"),
389         "projet_name": fields.function(_get_project_name,
390                                        type='char',
391                                        store=True,
392                                        string="Nom du projet"),
393         "affecte": fields.integer(string="Affecté à"),
394         "depuis": fields.date(string="Depuis"),
395         "jusqua": fields.date(string="Jusqu'à"),
396         "projet_id": fields.many2one("cmmi.projet",
397                                      string="Projet",
398                                      required=True),
399         "partner_id": fields.many2one("res.partner",
400                                     string="Team Member",
401                                     required=True),
402     }