Projet - Ajout de nouveaux champs : 'use_chantier' et 'use_palier'
[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         "use_chantier": fields.boolean(string="Utilisation de la notion de chantier ?"),
86         "use_palier": fields.boolean(string="Utilisation de la notion de palier ?"),
87
88         # Backrefs
89         "domains": fields.one2many("cmmi.projet.domaine",
90                                     "project_id",
91                                     string="Domaines"),
92         "moe_ids": fields.one2many("cmmi.projet.moe",
93                                    "project_id",
94                                    string="MOEs"),
95         "moa_ids": fields.one2many("cmmi.projet.moa",
96                                    "project_id",
97                                    string="MOAs"),
98         "team_members": fields.one2many("cmmi.projet.teammember",
99                                          "projet_id",
100                                          string="Team Members"),
101         "modules": fields.one2many("cmmi.description.module",
102                                    "projet_id",
103                                    string="Modules"),
104         "chantiers": fields.one2many("cmmi.axes.chantier",
105                                      "projet_id",
106                                      string="Chantiers"),
107         "paliers": fields.one2many("cmmi.axes.palier",
108                                    "projet_id",
109                                    string="Paliers"),
110         "phases": fields.one2many("cmmi.projet.phase",
111                                   "projet_id",
112                                   string="Phases"),
113         "evolutions": fields.one2many("cmmi.evolution",
114                                       "projet_id",
115                                       string="Evolutions"),
116
117         # Champs fonction rapatriant les mo ou domaine principaux
118         "moe_id": fields.function(_get_main_moe,
119                                   type="string",
120                                   string="MOE principale"),
121         "moa_id": fields.function(_get_main_moa,
122                                   type="string",
123                                   string="MOA principale"),
124         "main_domain": fields.function(_get_main_domain,
125                                        type="string",
126                                        string="Domaine principal"),
127         "phases_selectionnees": fields.function(_get_phases_selectionnees,
128                                                 type="one2many",
129                                                 obj="cmmi.projet.phase",
130                                                 string="Phases sélectionnées"),
131     }
132
133
134     def create(self, cr, uid, vals, context=None):
135         project_id = osv.Model.create(self, cr, uid, vals, context=context)
136
137         # Récupération des ids de toutes les phases
138         phase_model = self.pool.get("cmmi.phase")
139         phases_ids = phase_model.search(cr, uid, [])
140
141         # Création des relations
142         projet_phase_model = self.pool.get("cmmi.projet.phase")
143         for phase_id in phases_ids:
144             projet_phase_model.create(
145                 cr,
146                 uid,
147                 {
148                     'projet_id': project_id,
149                     'phase_id': phase_id,
150                     'selectionne': False,
151                 }
152             )
153
154         return project_id
155
156
157     def action_add_domain(self, cr, uid, ids, context=None):
158         pass
159
160     def action_add_moe(self, cr, uid, ids, context=None):
161         pass
162
163     def action_add_moa(self, cr, uid, ids, context=None):
164         pass
165
166
167
168 class ProjetDomaine(osv.Model):
169
170     _name = "cmmi.projet.domaine"
171
172     _description = "Rattachement des domaines a un projet."
173
174     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
175         if isinstance(ids, (int, long)):
176             ids = [ids]
177         #return {i: r.domaine_id.name for i, r in
178         #        zip(ids, self.browse(cr, uid, ids, context=context))}
179         return dict([(i, r.domaine_id.name) for i, r in
180                 zip(ids, self.browse(cr, uid, ids, context=context))])
181
182     _columns = {
183         "name": fields.function(_get_name,
184                                 type='char',
185                                 store=True,
186                                 string="Nom du domaine"),
187         "main": fields.boolean(string="Domaine principal ?"),
188         "project_id": fields.many2one("cmmi.projet",
189                                       string="Projet"),
190         "domaine_id": fields.many2one("cmmi.description.domaine",
191                                       string="Domaine"),
192     }
193
194     def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
195         if not main:
196             return {'value': {'main': True},
197                     'warning': {
198                         'title'   : "Integrity Warning",
199                         'message' : "One of the domains should be the main domain",
200                     }
201                 }
202         ids = self.search(
203             cr,
204             uid,
205             [
206                 ('project_id', '=', project),
207                 ('domaine_id', '!=', domaine),
208             ],
209             context=context,
210         )
211         current_id = self.search(
212             cr,
213             uid,
214             [
215                 ('project_id', '=', project),
216                 ('domaine_id', '=', domaine),
217             ],
218             context=context,
219         )
220         self.write(cr, uid, ids, {'main': False}, context=context)
221         self.write(cr, uid, current_id, {'main': True}, context=context)
222
223         return {'value': {'main': True}}
224
225
226
227 class ProjetMoe(osv.Model):
228     _name = "cmmi.projet.moe"
229
230     _description = "Rattachement des MOEs au projet."
231
232     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
233         if isinstance(ids, (int, long)):
234             ids = [ids]
235         return dict([(i, r.moe_id.name) for i, r in
236                 zip(ids, self.browse(cr, uid, ids, context=context))])
237
238     _columns = {
239         "name": fields.function(_get_name,
240                                 type='char',
241                                 store=True, # Permet d'enregistrer le champ.
242                                 string="Nom de la MOE"),
243         "main": fields.boolean(string="MOE principale ?"),
244         "project_id": fields.many2one("cmmi.projet",
245                                       string="Projet",
246                                       required=True),
247         "moe_id": fields.many2one("cmmi.mo.moe",
248                                   string="MOE",
249                                   required=True),
250     }
251
252     def onchange_main(self, cr, uid, ids, project, moe, main, context=None):
253         if not main:
254             return {'value': {'main': True},
255                     'warning': {
256                         'title'   : "Integrity Warning",
257                         'message' : "Une des MOE doit être la MOE principale",
258                     }
259                 }
260         ids = self.search(
261             cr,
262             uid,
263             [
264                 ('project_id', '=', project),
265                 ('moe_id', '!=', moe),
266             ],
267             context=context,
268         )
269         current_id = self.search(
270             cr,
271             uid,
272             [
273                 ('project_id', '=', project),
274                 ('moe_id', '=', moe),
275             ],
276             context=context,
277         )
278         self.write(cr, uid, ids, {'main': False}, context=context)
279         self.write(cr, uid, current_id, {'main': True}, context=context)
280
281         return {'value': {'main': True}}
282
283
284
285 class ProjetMoa(osv.Model):
286     _name = "cmmi.projet.moa"
287
288     _description = "Rattachement des MOAs a une projet."
289
290     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
291         if isinstance(ids, (int, long)):
292             ids = [ids]
293         return dict([(i, r.moa_id.name) for i, r in
294                 zip(ids, self.browse(cr, uid, ids, context=context))])
295
296     _columns = {
297         "name": fields.function(_get_name,
298                                 type='char',
299                                 store=True,
300                                 string="Nom de la MOA"),
301         "main": fields.boolean(string="MOA principale ?"),
302         "project_id": fields.many2one("cmmi.projet",
303                                       string="Projet",
304                                       required=True),
305         "moa_id": fields.many2one("cmmi.mo.moa",
306                                   string="MOA",
307                                   required=True),
308     }
309
310     def onchange_main(self, cr, uid, ids, project, moa, main, context=None):
311         if not main:
312             return {'value': {'main': True},
313                     'warning': {
314                         'title'   : "Integrity Warning",
315                         'message' : "Une des MOA doit être la MOA principale",
316                     }
317                 }
318         ids = self.search(
319             cr,
320             uid,
321             [
322                 ('project_id', '=', project),
323                 ('moa_id', '!=', moa),
324             ],
325             context=context,
326         )
327         current_id = self.search(
328             cr,
329             uid,
330             [
331                 ('project_id', '=', project),
332                 ('moa_id', '=', moa),
333             ],
334             context=context,
335         )
336         self.write(cr, uid, ids, {'main': False}, context=context)
337         self.write(cr, uid, current_id, {'main': True}, context=context)
338
339         return {'value': {'main': True}}
340
341
342
343 class ProjetPhase(osv.Model):
344     _name = "cmmi.projet.phase"
345
346     _description = "Rattachement des Phases aux projets"
347
348     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
349         if isinstance(ids, (int, long)):
350             ids = [ids]
351         return dict([(i, r.phase_id.name) for i, r in
352                 zip(ids, self.browse(cr, uid, ids, context=context))])
353
354     _columns = {
355         "name": fields.function(_get_name,
356                                 type='char',
357                                 store=True,
358                                 string="Nom de la phase"),
359         "phase_id": fields.many2one("cmmi.phase",
360                                     string="Phase",
361                                     required=True),
362         "projet_id": fields.many2one("cmmi.projet",
363                                      string="Projet",
364                                      required=True),
365         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
366     }
367
368
369
370 class ProjetTeammember(osv.Model):
371     _name = "cmmi.projet.teammember"
372
373     _description = "Rattachement des Team members a un projet."
374
375     def _get_partner_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
376         if isinstance(ids, (int, long)):
377             ids = [ids]
378         return dict([(i, r.partner_id.name) for i, r in
379                 zip(ids, self.browse(cr, uid, ids, context=context))])
380
381     def _get_project_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
382         if isinstance(ids, (int, long)):
383             ids = [ids]
384         return dict([(i, r.projet_id.name) for i, r in
385                 zip(ids, self.browse(cr, uid, ids, context=context))])
386
387     _columns = {
388         "name": fields.function(_get_partner_name,
389                                 type='char',
390                                 store=True,
391                                 string="Nom du partner"),
392         "projet_name": fields.function(_get_project_name,
393                                        type='char',
394                                        store=True,
395                                        string="Nom du projet"),
396         "affecte": fields.integer(string="Affecté à"),
397         "depuis": fields.date(string="Depuis"),
398         "jusqua": fields.date(string="Jusqu'à"),
399         "projet_id": fields.many2one("cmmi.projet",
400                                      string="Projet",
401                                      required=True),
402         "partner_id": fields.many2one("res.partner",
403                                     string="Team Member",
404                                     required=True),
405     }