05426ed575d94d866b4ded4947f8cb2128b57118
[OpenERP/cmmi.git] / evolution.py
1 #-*- coding: utf8 -*-
2 '''
3 '''
4
5 from openerp.osv import osv, fields
6
7
8 # ================================ EVOLUTION ================================ #
9 class Evolution(osv.Model):
10     _name = "cmmi.evolution"
11
12     _description = "Table de reference des evolutions."
13
14     _domains = {
15         'human': [('is_company', '=', "False")],
16     }
17
18     _priorites = [("incontournable", "Incontournable"),
19                   ("necessaire", "Nécéssaire"),
20                   ("utile", "Utile")]
21
22     _statuts = [("cree", "Crée"), ("encours", "En cours"),
23                 ("termine", "Terminé"), ("abandonne", "Abandonné"),
24                 ("suspendu", "Suspendu")]
25
26
27     def _get_charge_init(self, cr, uid, ids, field, arg, context=None):
28         result = {}
29         for evo in self.browse(cr, uid, context=context):
30             result[evo.id] = sum([p.charge_init for p in evo.phases])
31         return result
32
33
34     _columns = {
35         "pid": fields.integer(string="PID"),
36         "name": fields.char(string="Title", size=64, required=True),
37         "description": fields.text(string="Description"),
38         "objectif": fields.text(string="Objectif"),
39         "commentaire": fields.text(string="Commentaire"),
40         "keywords": fields.text(string="Mots clés"),
41         "priorite": fields.selection(_priorites, string="Priorité"),
42         "state": fields.selection(_statuts, string="Statut"),
43 #        "charges": fields.one2many("cmmi.evolution.charge", # Supprimé !
44 #                                   "evolution_id",
45 #                                   string="Charges"),
46         "phases": fields.one2many("cmmi.evolution.pahse",
47                                   "evolution_id",
48                                   string="Phases"),
49         # Backrefs
50         "module_id": fields.many2one("cmmi.description.module",
51                                      string="Modules"),
52         "chantier_id": fields.many2one("cmmi.axes.chantier",
53                                        string="Chantier"),
54         "palier_id": fields.many2one("cmmi.axes.palier",
55                                      string="Palier"),
56         "projet_id": fields.many2one("cmmi.projet",
57                                      string="Projet"),
58         "demandeur": fields.many2one("res.partner",
59                                      string="Demandeur",
60                                      domain=_domains['human']),
61         # Functions
62         "charge_init": fields.function(_get_charge_init,
63                                        type="integer",
64                                        string="Charge initiale"),
65         "charge_plan": fields.function(_get_charge_plan,
66                                        type="integer",
67                                        string="Charge plannifiée"),
68     }
69
70     _defaults = {
71         "state": "cree",
72     }
73
74     def action_commencer(self, cr, uid, ids, context=None):
75         if type(ids) == list:
76             if len(ids) != 1:
77                 return # TODO: message d'avertissement
78             ids = ids[0]
79
80         evo = self.read(cr, uid, ids, ['state'], context)
81
82         if evo['state'] != 'cree':
83             return
84         self.write(
85             cr,
86             uid,
87             ids,
88             {'state': 'encours'},
89             context,
90         )
91         return self
92
93     def action_suspendre(self, cr, uid, ids, context=None):
94         if type(ids) == list:
95             if len(ids) != 1:
96                 return # TODO: message d'avertissement
97             ids = ids[0]
98
99         evo = self.read(cr, uid, ids, ['state'], context)
100         if evo['state'] != 'encours':
101             return
102         self.write(
103             cr,
104             uid,
105             ids,
106             {'state': 'suspendu'},
107             context,
108         )
109         return self
110
111     def action_terminer(self, cr, uid, ids, context=None):
112         if type(ids) == list:
113             if len(ids) != 1:
114                 return # TODO: message d'avertissement
115             ids = ids[0]
116
117         evo = self.read(cr, uid, ids, ['state'], context)
118         if evo['state'] != 'encours':
119             return
120         self.write(
121             cr,
122             uid,
123             ids,
124             {'state': 'termine'},
125             context,
126         )
127         return self
128
129     def action_abandonner(self, cr, uid, ids, context=None):
130         if type(ids) == list:
131             if len(ids) != 1:
132                 return # TODO: message d'avertissement
133             ids = ids[0]
134
135         evo = self.read(cr, uid, ids, ['state'], context)
136
137         if not ('encours', 'cree').__contains__(evo['state']):
138             return
139         self.write(
140             cr,
141             uid,
142             ids,
143             {'state': 'abandonne'},
144             context,
145         )
146         return self
147
148     def action_reprendre(self, cr, uid, ids, context=None):
149         if type(ids) == list:
150             if len(ids) != 1:
151                 return # TODO: message d'avertissement
152             ids = ids[0]
153
154         evo = self.read(cr, uid, ids, ['state'], context)
155
156         if evo['state'] != 'suspendu':
157             return
158         self.write(
159             cr,
160             uid,
161             ids,
162             {'state': 'encours'},
163             context,
164         )
165         return self
166
167
168 # =========================== EVOLUTION PHASE =============================== #
169 class Phase(osv.Model):
170     _name = "cmmi.evolution.phase"
171
172     _description = "Phase d'une evolution."
173
174     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
175         if isinstance(ids, (int, long)):
176             ids = [ids]
177         return dict([(i, r.phase_id.name) for i, r in
178                 zip(ids, self.browse(cr, uid, ids, context=context))])
179
180
181     _columns = {
182         "name": fields.function(_get_name,
183                                 type='char',
184                                 store=True,
185                                 string="Nom de la phase"),
186         "description": fields.text(string="Description"),
187         "charge_init": fields.integer(string="Charge initiale"),
188         "charge_plan": fields.integer(string="Charge plannifiée"),
189         "phase_id": fields.many2one("cmmi.axes.palier.phase",
190                                     string="Phase"),
191         "evolution_id": fields.many2one("cmmi.evolution",
192                                         string="Evolution"),
193     }
194
195     def create(self, cr, uid, vals, context=None):
196         # TODO: gérer la création d'une phase d'évolution.
197         # Vérifier les valeurs contenues dans vals et les modifier / rajouter si nécessaire selon les cas suivants
198
199         # Si description est vide, alors par défaut, recopie de la description de l'evolution et de la phase (concaténés avec un retour à la ligne entre les deux).
200         # Si commentaire est vide, alors par défaut, recopie du commentaire de ?
201         # Si version est vide, alors par dégaut, recopie de la version de ?
202 #        phase_model = self.pool.get("cmmi.axes.palier.phase")
203 #        evolution_model = self.pool.get("cmmi.evolution")
204 #
205 #        phase = phase_model.read(cr, vals["phase_id"], fields=None,context=None)
206 #        evolution = evolution_model.read(cr, vals["evolution_id"], fields=None,context=None)
207
208 #        if vals["description"] == "":
209 #            vals["description"] = "" + evolution["description"] + "\n" + phase["description"]
210
211 #        if vals["commentaire"] == "" # cmmi.evolution.phase n'a pas de commentaire
212 #            vals["commentaire"] = evolution["commentaire"]
213
214 #        if vals["version"] == "" # cmmi.evolution.phase n'a pas de version
215 #            vals["version"] = phase["version"]
216
217         return osv.Model.create(self, cr, uid, vals, context=context)
218
219     def commencer(self, cr, uid, ids, context=None):
220         if type(ids) == list:
221             if len(ids) != 1:
222                 return
223             ids = ids[0]
224
225         phase = self.read(cr, uid, ids, ['charge_plan'], context)
226
227         self.write(
228             cr,
229             uid,
230             ids, {
231                 'charge_init' : phase['charge_plan'],
232             },
233             context)
234         return self
235
236
237 # =========================== EVOLUTION CHARGE ============================== #
238 class Charge(osv.Model):
239     _name = "cmmi.evolution.charge"
240
241     _description = "Charge d'une evolution."
242
243     _columns = {
244         "name": fields.char(string="Title", size=64, required=True),
245         "description": fields.text(string="Description"),
246         "evolution_id": fields.many2one("cmmi.evolution",
247                                         string="Palier"),
248         "phase_id": fields.many2one("cmmi.evolution.phase",
249                                     string="Phase de l'évolution",
250                                     required=True),
251         "teammember_id": fields.many2one("res.partner", # TODO: Vers l'association teammember MO plutôt que MO.
252                                          string="Team Member",
253                                          required=True),
254     }