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