Ajout d'un champ charge_reel dans palier
[OpenERP/cmmi.git] / evolution.py
index c999249..13917c3 100644 (file)
@@ -5,9 +5,16 @@
 from openerp.osv import osv, fields
 
 
+# ================================ EVOLUTION ================================ #
 class Evolution(osv.Model):
     _name = "cmmi.evolution"
 
+    _description = "Table de reference des evolutions."
+
+    _domains = {
+        'human': [('is_company', '=', "False")],
+    }
+
     _priorites = [("incontournable", "Incontournable"),
                   ("necessaire", "Nécéssaire"),
                   ("utile", "Utile")]
@@ -16,6 +23,27 @@ class Evolution(osv.Model):
                 ("termine", "Terminé"), ("abandonne", "Abandonné"),
                 ("suspendu", "Suspendu")]
 
+
+    def _get_charge_init(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for evo in self.browse(cr, uid, ids, context=context):
+            result[evo.id] = sum([p.charge_init for p in evo.phases])
+        return result
+
+
+    def _get_charge_plan(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for evo in self.browse(cr, uid, ids, context=context):
+            result[evo.id] = sum([p.charge_plan for p in evo.phases])
+        return result
+
+    def _get_charge_reel(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for e in self.browse(cr, uid, ids, context=context):
+            result[e.id] = sum([p.quantite for p in e.charge_reel])
+        return result
+
+
     _columns = {
         "pid": fields.integer(string="PID"),
         "name": fields.char(string="Title", size=64, required=True),
@@ -24,38 +52,220 @@ class Evolution(osv.Model):
         "commentaire": fields.text(string="Commentaire"),
         "keywords": fields.text(string="Mots clés"),
         "priorite": fields.selection(_priorites, string="Priorité"),
-        "statut": fields.selection(_statuts, string="Statut"),
-        "charges": fields.one2many("projet.charge",
-                                   "evolution_id",
-                                   string="Charges"),
-        "module_id": fields.many2one("projet.module",
+        "state": fields.selection(_statuts, string="Statut"),
+        "phases": fields.one2many("cmmi.evolution.phase",
+                                  "evolution_id",
+                                  string="Phases"),
+        # Backrefs
+        "module_id": fields.many2one("cmmi.description.module",
                                      string="Modules"),
-        "chantier_id": fields.many2one("projet.chantier",
-                                    string="Chantier"),
-        "palier_id": fields.many2one("projet.palier",
+        "chantier_id": fields.many2one("cmmi.axes.chantier",
+                                       string="Chantier"),
+        "palier_id": fields.many2one("cmmi.axes.palier",
                                      string="Palier"),
-        "phase_id": fields.many2one("projet.phase",
-                                    string="Phase"),
-        "projet_id": fields.many2one("projet.projet",
+        "projet_id": fields.many2one("cmmi.projet",
                                      string="Projet"),
+        "demandeur": fields.many2one("res.partner",
+                                     string="Demandeur",
+                                     domain=_domains['human']),
+        # Functions
+        "charge_init": fields.function(_get_charge_init,
+                                       type="integer",
+                                       string="Charge initiale"),
+        "charge_plan": fields.function(_get_charge_plan,
+                                       type="integer",
+                                       string="Charge plannifiée"),
+        "charge_reel": fields.function(_get_charge_reel,
+                                       type="integer",
+                                       string="Charge réelle"),
     }
 
+    _defaults = {
+        "state": "cree",
+    }
+
+    def action_commencer(self, cr, uid, ids, context=None):
+        if type(ids) == list:
+            if len(ids) != 1:
+                return # TODO: message d'avertissement
+            ids = ids[0]
+
+        evo = self.read(cr, uid, ids, ['state'], context)
+
+        if evo['state'] != 'cree':
+            return
+        self.write(
+            cr,
+            uid,
+            ids,
+            {'state': 'encours'},
+            context,
+        )
+        return self
+
+    def action_suspendre(self, cr, uid, ids, context=None):
+        if type(ids) == list:
+            if len(ids) != 1:
+                return # TODO: message d'avertissement
+            ids = ids[0]
+
+        evo = self.read(cr, uid, ids, ['state'], context)
+        if evo['state'] != 'encours':
+            return
+        self.write(
+            cr,
+            uid,
+            ids,
+            {'state': 'suspendu'},
+            context,
+        )
+        return self
+
+    def action_terminer(self, cr, uid, ids, context=None):
+        if type(ids) == list:
+            if len(ids) != 1:
+                return # TODO: message d'avertissement
+            ids = ids[0]
+
+        evo = self.read(cr, uid, ids, ['state'], context)
+        if evo['state'] != 'encours':
+            return
+        self.write(
+            cr,
+            uid,
+            ids,
+            {'state': 'termine'},
+            context,
+        )
+        return self
+
+    def action_abandonner(self, cr, uid, ids, context=None):
+        if type(ids) == list:
+            if len(ids) != 1:
+                return # TODO: message d'avertissement
+            ids = ids[0]
+
+        evo = self.read(cr, uid, ids, ['state'], context)
+
+        if not ('encours', 'cree').__contains__(evo['state']):
+            return
+        self.write(
+            cr,
+            uid,
+            ids,
+            {'state': 'abandonne'},
+            context,
+        )
+        return self
+
+    def action_reprendre(self, cr, uid, ids, context=None):
+        if type(ids) == list:
+            if len(ids) != 1:
+                return # TODO: message d'avertissement
+            ids = ids[0]
+
+        evo = self.read(cr, uid, ids, ['state'], context)
+
+        if evo['state'] != 'suspendu':
+            return
+        self.write(
+            cr,
+            uid,
+            ids,
+            {'state': 'encours'},
+            context,
+        )
+        return self
+
 
+# =========================== EVOLUTION PHASE =============================== #
+class Phase(osv.Model):
+    _name = "cmmi.evolution.phase"
+
+    _description = "Phase d'une evolution."
+
+    def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        return dict([(i, r.phase_id.name) for i, r in
+                zip(ids, self.browse(cr, uid, ids, context=context))])
+
+
+    _columns = {
+        "name": fields.function(_get_name,
+                                type='char',
+                                store=True,
+                                string="Nom de la phase"),
+        "description": fields.text(string="Description"),
+        "charge_init": fields.integer(string="Charge initiale"),
+        "charge_plan": fields.integer(string="Charge plannifiée"),
+        "phase_id": fields.many2one("cmmi.axes.palier.phase",
+                                    string="Phase"),
+        "evolution_id": fields.many2one("cmmi.evolution",
+                                        string="Evolution"),
+    }
+
+    def create(self, cr, uid, vals, context=None):
+        # TODO: gérer la création d'une phase d'évolution.
+        # Vérifier les valeurs contenues dans vals et les modifier / rajouter si nécessaire selon les cas suivants
+
+        # 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).
+        # Si commentaire est vide, alors par défaut, recopie du commentaire de ?
+        # Si version est vide, alors par dégaut, recopie de la version de ?
+#        phase_model = self.pool.get("cmmi.axes.palier.phase")
+#        evolution_model = self.pool.get("cmmi.evolution")
+#
+#        phase = phase_model.read(cr, vals["phase_id"], fields=None,context=None)
+#        evolution = evolution_model.read(cr, vals["evolution_id"], fields=None,context=None)
+
+#        if vals["description"] == "":
+#            vals["description"] = "" + evolution["description"] + "\n" + phase["description"]
+
+#        if vals["commentaire"] == "" # cmmi.evolution.phase n'a pas de commentaire
+#            vals["commentaire"] = evolution["commentaire"]
+
+#        if vals["version"] == "" # cmmi.evolution.phase n'a pas de version
+#            vals["version"] = phase["version"]
+
+        return osv.Model.create(self, cr, uid, vals, context=context)
+
+    def commencer(self, cr, uid, ids, context=None):
+        if type(ids) == list:
+            if len(ids) != 1:
+                return
+            ids = ids[0]
+
+        phase = self.read(cr, uid, ids, ['charge_plan'], context)
+
+        self.write(
+            cr,
+            uid,
+            ids, {
+                'charge_init' : phase['charge_plan'],
+            },
+            context)
+        return self
+
+
+# =========================== EVOLUTION CHARGE ============================== #
 class Charge(osv.Model):
     _name = "cmmi.evolution.charge"
 
+    _description = "Charge d'une evolution."
+
+    _selection_qte = [("0.25", "1/4"),
+                      ("0.5", "1/2"),
+                      ("0.75", "3/4")]
+
     _columns = {
         "name": fields.char(string="Title", size=64, required=True),
         "description": fields.text(string="Description"),
-        "teammember_id": fields.many2one("projet.teammember",
+        "date": fields.date(string="Date"),
+        "quantite": fields.selection(_selection_qte, string="Quantité"),
+        "phase_id": fields.many2one("cmmi.evolution.phase",
+                                    string="Phase de l'évolution",
+                                    required=True),
+        "teammember_id": fields.many2one("res.partner", # TODO: Vers l'association teammember MO plutôt que MO.
                                          string="Team Member",
                                          required=True),
-        "phase_id": fields.many2one("projet.phase",
-                                    string="Phase",
-                                    required=True),
-        "evolution_id": fields.many2one("projet.evolution",
-                                    string="Evolution",
-                                    required=True),
-        "mo_id": fields.many2one("projet.mo",
-                                 string="Mo"),
     }