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