Ajout d'un champ charge_reel dans palier
[OpenERP/cmmi.git] / axes.py
diff --git a/axes.py b/axes.py
index 5ef14ed..aa5a83f 100644 (file)
--- a/axes.py
+++ b/axes.py
@@ -15,6 +15,7 @@ class Mesurable(osv.Model):
                 ("termine", "Terminé"), ("abandonne", "Abandonné"),
                 ("suspendu", "Suspendu"), ("generique", "Générique")]
 
+
     def _nb_jours_init(self, cr, uid, ids, field, arg, context=None):
         result = {}
         for m in self.browse(cr, uid, ids, context=context):
@@ -26,6 +27,31 @@ class Mesurable(osv.Model):
                         datetime.strptime(m.date_init_fin, "%Y-%m-%d").date())
         return result
 
+
+    def _nb_jours_plan(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for m in self.browse(cr, uid, ids, context=context):
+            if not m.date_plan_deb or not m.date_plan_fin:
+                result[m.id] = 0
+                continue
+            result[m.id] = Mesurable._nb_jours_ouvre_entre_2_dates(
+                        datetime.strptime(m.date_plan_deb, "%Y-%m-%d").date(),
+                        datetime.strptime(m.date_plan_fin, "%Y-%m-%d").date())
+        return result
+
+
+    def _nb_jours_reel(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for m in self.browse(cr, uid, ids, context=context):
+            if not m.date_reel_deb or not m.date_reel_fin:
+                result[m.id] = 0
+                continue
+            result[m.id] = Mesurable._nb_jours_ouvre_entre_2_dates(
+                        datetime.strptime(m.date_reel_deb, "%Y-%m-%d").date(),
+                        datetime.strptime(m.date_reel_fin, "%Y-%m-%d").date())
+        return result
+
+
     _columns = {
         "name": fields.char(string="Title", size=64, required=True),
         "description": fields.text(string="Description"),
@@ -39,9 +65,15 @@ class Mesurable(osv.Model):
         "date_plan_fin": fields.date(string="Plan fin"),
         "date_reel_deb": fields.date(string="Réel début"),
         "date_reel_fin": fields.date(string="Réel fin"),
-        "nb_jours_projets": fields.function(_nb_jours_init,
-                                            type="integer",
-                                            string="Nombre de jour"),
+        "nb_jours_init": fields.function(_nb_jours_init,
+                                         type="integer",
+                                         string="Nombre de jours initials"),
+        "nb_jours_plan": fields.function(_nb_jours_plan,
+                                         type="integer",
+                                         string="Nombre de jours planifiés"),
+        "nb_jours_reel": fields.function(_nb_jours_reel,
+                                         type="integer",
+                                         string="Nombre de jours réels"),
     }
 
     _defaults = {
@@ -67,10 +99,10 @@ class Mesurable(osv.Model):
     ]
 
 
-    def commencer(self, cr, uid, ids, context=None):
+    def action_commencer(self, cr, uid, ids, context=None):
         if type(ids) == list:
             if len(ids) != 1:
-                return # TODO: message d'avertissement
+                return
             ids = ids[0]
 
         palier = self.read(cr, uid, ids, ['date_plan_deb', 'date_plan_fin', 'state'], context)
@@ -90,13 +122,13 @@ class Mesurable(osv.Model):
         return self
 
 
-    def suspendre(self, cr, uid, ids, context=None):
+    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]
 
-        mesurable = self.read(cr, uid, ids, ['state'], context, context)
+        mesurable = self.read(cr, uid, ids, ['state'], context)
         if mesurable['state'] != 'encours':
             return
         self.write(
@@ -108,13 +140,13 @@ class Mesurable(osv.Model):
         )
         return self
 
-    def terminer(self, cr, uid, ids, context=None):
+    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]
 
-        mesurable = self.read(cr, uid, ids, ['state'], context, context)
+        mesurable = self.read(cr, uid, ids, ['state'], context)
         if mesurable['state'] != 'encours':
             return
         self.write(
@@ -126,7 +158,41 @@ class Mesurable(osv.Model):
         )
         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]
 
+        mesurable = self.read(cr, uid, ids, ['state'], context)
+        if not ('encours', 'cree').__contains__(mesurable['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]
+
+        mesurable = self.read(cr, uid, ids, ['state'], context)
+        if mesurable['state'] != 'suspendu':
+            return
+        self.write(
+            cr,
+            uid,
+            ids,
+            {'state': 'encours'},
+            context,
+        )
+        return self
 
 #------------ TRAVAIL CALCUL JOURS OUVRES ------------
     @staticmethod
@@ -211,6 +277,28 @@ class Palier(osv.Model):
     _types_palier = [("normal", "Normal"), ("exceptionnel", "Exceptionnel"),
                      ("correctif", "Correctif"), ("autre", "Autre")]
 
+
+    def _get_charge_init(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for palier in self.browse(cr, uid, ids, context=context):
+            result[palier.id] = sum([e.charge_init for e in palier.evolutions])
+        return result
+
+
+    def _get_charge_plan(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for palier in self.browse(cr, uid, ids, context=context):
+            result[palier.id] = sum([e.charge_plan for e in palier.evolutions])
+        return result
+
+
+    def _get_charge_reel(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for p in self.browse(cr, uid, ids, context=context):
+            result[p.id] = sum([e.charge_reel for e in p.evolutions])
+        return result
+
+
     _columns = {
         "type_palier": fields.selection(_types_palier, string="Type"),
         "projet_id": fields.many2one("cmmi.projet",
@@ -222,6 +310,15 @@ class Palier(osv.Model):
         "phases": fields.one2many("cmmi.axes.palier.phase",
                                   "palier_id",
                                   string="Phases"),
+        "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 = {
@@ -236,6 +333,9 @@ class Palier(osv.Model):
         phase_model = self.pool.get("cmmi.projet.phase")
         phases_ids = phase_model.search(cr, uid, [('selectionne', '=', True)])
 
+        palier_model = self.pool.get("cmmi.axes.palier")
+        palier = palier_model.read(cr, uid, palier_id, ['date_plan_deb', 'date_plan_fin'])
+
         # Création des PalierPhase
         palier_phase_model = self.pool.get("cmmi.axes.palier.phase")
         for phase_id in phases_ids:
@@ -245,6 +345,8 @@ class Palier(osv.Model):
                 {
                     'phase_id': phase_id,
                     'palier_id': palier_id,
+                    'date_plan_deb': palier['date_plan_deb'],
+                    'date_plan_fin': palier['date_plan_fin'],
                 }
             )
         return palier_id
@@ -266,6 +368,26 @@ class PalierPhase(osv.Model):
                 zip(ids, self.browse(cr, uid, ids, context=context))])
 
 
+    def _get_charge_init(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for pp in self.browse(cr, uid, ids, context=context):
+            result[pp.id] = sum([p.charge_init for p in pp.phases])
+        return result
+
+
+    def _get_charge_plan(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for pp in self.browse(cr, uid, ids, context=context):
+            result[pp.id] = sum([p.charge_plan for p in pp.phases])
+        return result
+
+    def _get_charge_reel(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for pp in self.browse(cr, uid, ids, context=context):
+            result[pp.id] = sum([c.quantite for c in pp.charges])
+        return result
+
+
     _columns = {
         "name": fields.function(_get_name,
                                 type='char',
@@ -275,10 +397,22 @@ class PalierPhase(osv.Model):
                                     string="Phase du projet"),
         "palier_id": fields.many2one("cmmi.axes.palier",
                                      string="Palier"),
+        "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 plannifiée"),
         # backrefs
         "charges": fields.one2many("cmmi.evolution.charge",
                                    "phase_id",
                                    string="Charges"),
+        "phases": fields.one2many("cmmi.evolution.phase",
+                                  "phase_id",
+                                  string="Phases"),
 #        "evolutions": fields.one2many("cmmi.evolution", #Supprimé !
 #                                      "phase_id",
 #                                      string="Evolutions"),
@@ -295,7 +429,6 @@ class PalierPhase(osv.Model):
         return osv.Model.create(self, cr, uid, vals, context=context)
 
 
-
 # ================================ CHANTIER ================================= #
 class Chantier(osv.Model):
     _name = "cmmi.axes.chantier"
@@ -304,6 +437,28 @@ class Chantier(osv.Model):
 
     _inherit = "cmmi.axes.mesurable"
 
+
+    def _get_charge_init(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for chantier in self.browse(cr, uid, ids, context=context):
+            result[chantier.id] = sum([e.charge_init for e in chantier.evolutions])
+        return result
+
+
+    def _get_charge_plan(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for chantier in self.browse(cr, uid, ids, context=context):
+            result[chantier.id] = sum([e.charge_plan for e in chantier.evolutions])
+        return result
+
+
+    def _get_charge_reel(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for c in self.browse(cr, uid, ids, context=context):
+            result[c.id] = sum([e.charge_reel for e in c.evolutions])
+        return result
+
+
     _columns = {
         "projet_id": fields.many2one("cmmi.projet",
                                      string="Projet",
@@ -316,4 +471,13 @@ class Chantier(osv.Model):
         "evolutions": fields.one2many("cmmi.evolution",
                                       "chantier_id",
                                       string="Evolutions"),
+        "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"),
     }