153a64d80e0659ab1f347fe1bbabc9628f66e6a4
[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     _columns = {
51         "name": fields.char(string="Title", size=64, required=True),
52         "description": fields.text(string="Description"),
53         "domains": fields.one2many("cmmi.projet.domaine",
54                                     "project_id",
55                                     string="Domaines"),
56         "team_members": fields.many2many("res.partner",
57                                          "cmmi_projet_teammember_rel",
58                                          "projet_id",
59                                          "partner_id",
60                                          string="Team Members"),
61         "modules": fields.one2many("cmmi.description.module",
62                                    "projet_id",
63                                    string="Modules"),
64         "chantiers": fields.one2many("cmmi.axes.chantier",
65                                      "projet_id",
66                                      string="Chantiers"),
67         "paliers": fields.one2many("cmmi.axes.palier",
68                                    "projet_id",
69                                    string="Paliers"),
70         "phases": fields.one2many("cmmi.projet.phase",
71                                   "projet_id",
72                                   string="Phases"),
73         "evolutions": fields.one2many("cmmi.evolution",
74                                       "projet_id",
75                                       string="Evolutions"),
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         "moe_id": fields.one2many("cmmi.projet.moe",
83                                   "project_id",
84                                   string="MOE principale",
85                                   domaine=[('main', '=', True)]),
86         "moa_id": fields.function(_get_main_moa,
87                                   type="string",
88                                   string="MOA principale"),
89         "main_domain": fields.function(_get_main_domain,
90                                        type="string",
91                                        string="Domaine principal"),
92     }
93
94
95     def create(self, cr, uid, vals, context=None):
96         project_id = osv.Model.create(self, cr, uid, vals, context=context)
97
98         # Récupération des ids de toutes les phases
99         phase_model = self.pool.get("cmmi.phase")
100         phases_ids = phase_model.search(cr, uid, [])
101
102         # Création des relations
103         projet_phase_model = self.pool.get("cmmi.projet.phase")
104         for phase_id in phases_ids:
105             projet_phase_model.create(
106                 cr,
107                 uid,
108                 {
109                     'projet_id': project_id,
110                     'phase_id': phase_id,
111                     'selectionne': False,
112                 }
113             )
114
115         return project_id
116
117
118     def action_add_domain(self, cr, uid, ids, context=None):
119         pass
120
121     def action_add_moe(self, cr, uid, ids, context=None):
122         pass
123
124     def action_add_moa(self, cr, uid, ids, context=None):
125         pass
126
127 class ProjetDomaine(osv.Model):
128
129     _name = "cmmi.projet.domaine"
130
131     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
132         if isinstance(ids, (int, long)):
133             ids = [ids]
134         #return {i: r.domaine_id.name for i, r in
135         #        zip(ids, self.browse(cr, uid, ids, context=context))}
136         return dict([(i, r.domaine_id.name) for i, r in
137                 zip(ids, self.browse(cr, uid, ids, context=context))])
138
139     _columns = {
140         "name": fields.function(_get_name,
141                                 type='char',
142                                 store=True,
143                                 string="Nom du domaine"),
144         "main": fields.boolean(string="Domaine principal ?"),
145         "project_id": fields.many2one("cmmi.projet",
146                                       string="Projet"),
147         "domaine_id": fields.many2one("cmmi.description.domaine",
148                                       string="Domaine"),
149     }
150
151     def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
152         if not main:
153             return {'value': {'main': True},
154                     'warning': {
155                         'title'   : "Integrity Warning",
156                         'message' : "One of the domains should be the main domain",
157                     }
158                 }
159         ids = self.search(
160             cr,
161             uid,
162             [
163                 ('project_id', '=', project),
164                 ('domaine_id', '!=', domaine),
165             ],
166             context=context,
167         )
168         current_id = self.search(
169             cr,
170             uid,
171             [
172                 ('project_id', '=', project),
173                 ('domaine_id', '=', domaine),
174             ],
175             context=context,
176         )
177         self.write(cr, uid, ids, {'main': False}, context=context)
178         self.write(cr, uid, current_id, {'main': True}, context=context)
179
180         return {'value': {'main': True}}
181
182
183
184 class ProjetMoe(osv.Model):
185     _name = "cmmi.projet.moe"
186
187     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
188         if isinstance(ids, (int, long)):
189             ids = [ids]
190         return dict([(i, r.moe_id.name) for i, r in
191                 zip(ids, self.browse(cr, uid, ids, context=context))])
192
193     _columns = {
194         "name": fields.function(_get_name,
195                                 type='char',
196                                 store=True, # Permet d'enregistrer le champ.
197                                 string="Nom de la MOE"),
198         "main": fields.boolean(string="MOE principale ?"),
199         "project_id": fields.many2one("cmmi.projet",
200                                       string="Projet",
201                                       required=True),
202         "moe_id": fields.many2one("cmmi.mo.moe",
203                                   string="MOE",
204                                   required=True),
205     }
206
207     def onchange_main(self, cr, uid, ids, project, moe, main, context=None):
208         pass
209
210
211 class ProjetMoa(osv.Model):
212     _name = "cmmi.projet.moa"
213
214     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
215         if isinstance(ids, (int, long)):
216             ids = [ids]
217         return dict([(i, r.moa_id.name) for i, r in
218                 zip(ids, self.browse(cr, uid, ids, context=context))])
219
220     _columns = {
221         "name": fields.function(_get_name,
222                                 type='char',
223                                 store=True,
224                                 string="Nom de la MOA"),
225         "main": fields.boolean(string="MOA principale ?"),
226         "project_id": fields.many2one("cmmi.projet",
227                                       string="Projet",
228                                       required=True),
229         "moa_id": fields.many2one("cmmi.mo.moa",
230                                   string="MOA",
231                                   required=True),
232     }
233
234     def onchange_main(self, cr, uid, ids, project, moa, main, context=None):
235         if not main:
236             return {'value': {'main': True},
237                     'warning': {
238                         'title'   : "Integrity Warning",
239                         'message' : "Une des MOA doit être la MOA principale",
240                     }
241                 }
242         ids = self.search(
243             cr,
244             uid,
245             [
246                 ('project_id', '=', project),
247                 ('moa_id', '!=', moa),
248             ],
249             context=context,
250         )
251         current_id = self.search(
252             cr,
253             uid,
254             [
255                 ('project_id', '=', project),
256                 ('moa_id', '=', moa),
257             ],
258             context=context,
259         )
260         self.write(cr, uid, ids, {'main': False}, context=context)
261         self.write(cr, uid, current_id, {'main': True}, context=context)
262
263         return {'value': {'main': True}}
264
265
266 class ProjetPhase(osv.Model):
267     _name = "cmmi.projet.phase"
268
269     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
270         if isinstance(ids, (int, long)):
271             ids = [ids]
272         return dict([(i, r.phase_id.name) for i, r in
273                 zip(ids, self.browse(cr, uid, ids, context=context))])
274
275     _columns = {
276         "name": fields.function(_get_name,
277                                 type='char',
278                                 store=True,
279                                 string="Nom de la phase"),
280         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
281         "projet_id": fields.many2one("cmmi.projet",
282                                      string="Projet",
283                                      required=True),
284         "phase_id": fields.many2one("cmmi.phase",
285                                     string="Phase",
286                                     required=True),
287     }