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