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