Corrections sur la vue phase et simplification
[OpenERP/cmmi.git] / projet.py
index d312223..55e9b2d 100644 (file)
--- a/projet.py
+++ b/projet.py
@@ -28,16 +28,52 @@ class Projet(osv.Model):
         return result
 
 
+    def _get_main_moa(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if type(ids) in (int, float):
+            ids = [ids]
+
+        projects = self.browse(cr, uid, ids, context=context)
+
+        result = {}
+
+        for project in projects:
+            for moa in project.moa_ids:
+                if moa.main:
+                    result[project.id] = moa.name
+                    break
+            else:
+                result[project.id] = "Any MOA is selected as main MOA yet"
+
+        return result
+
+
+    def _get_main_moe(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if type(ids) in (int, float):
+            ids = [ids]
+
+        projects = self.browse(cr, uid, ids, context=context)
+
+        result = {}
+
+        for project in projects:
+            for moe in project.moe_ids:
+                if moe.main:
+                    result[project.id] = moe.name
+                    break
+            else:
+                result[project.id] = "Any MOE is selected as main MOE yet"
+
+        return result
+
+
     _columns = {
         "name": fields.char(string="Title", size=64, required=True),
         "description": fields.text(string="Description"),
         "domains": fields.one2many("cmmi.projet.domaine",
                                     "project_id",
                                     string="Domaines"),
-        "team_members": fields.many2many("res.partner",
-                                         "cmmi_projet_teammember_rel",
+        "team_members": fields.one2many("cmmi.projet.teammember",
                                          "projet_id",
-                                         "partner_id",
                                          string="Team Members"),
         "modules": fields.one2many("cmmi.description.module",
                                    "projet_id",
@@ -60,19 +96,18 @@ class Projet(osv.Model):
         "moa_ids": fields.one2many("cmmi.projet.moa",
                                    "project_id",
                                    string="MOAs"),
-        "moe_id": fields.one2many("cmmi.projet.moe",
-                                  "project_id",
-                                  string="MOE principale",
-                                  domaine=[('main', '=', True)]),
-        "moa_id": fields.one2many("cmmi.projet.moa",
-                                  "project_id",
-                                  string="MOA principale",
-                                  domaine=[('main', '=', True)]),
+        "moe_id": fields.function(_get_main_moe,
+                                  type="string",
+                                  string="MOE principale"),
+        "moa_id": fields.function(_get_main_moa,
+                                  type="string",
+                                  string="MOA principale"),
         "main_domain": fields.function(_get_main_domain,
                                        type="string",
                                        string="Domaine principal"),
     }
 
+
     def create(self, cr, uid, vals, context=None):
         project_id = osv.Model.create(self, cr, uid, vals, context=context)
 
@@ -186,7 +221,35 @@ class ProjetMoe(osv.Model):
     }
 
     def onchange_main(self, cr, uid, ids, project, moe, main, context=None):
-        pass
+        if not main:
+            return {'value': {'main': True},
+                    'warning': {
+                        'title'   : "Integrity Warning",
+                        'message' : "Une des MOE doit être la MOE principale",
+                    }
+                }
+        ids = self.search(
+            cr,
+            uid,
+            [
+                ('project_id', '=', project),
+                ('moe_id', '!=', moe),
+            ],
+            context=context,
+        )
+        current_id = self.search(
+            cr,
+            uid,
+            [
+                ('project_id', '=', project),
+                ('moe_id', '=', moe),
+            ],
+            context=context,
+        )
+        self.write(cr, uid, ids, {'main': False}, context=context)
+        self.write(cr, uid, current_id, {'main': True}, context=context)
+
+        return {'value': {'main': True}}
 
 
 class ProjetMoa(osv.Model):
@@ -213,12 +276,42 @@ class ProjetMoa(osv.Model):
     }
 
     def onchange_main(self, cr, uid, ids, project, moa, main, context=None):
-        pass
+        if not main:
+            return {'value': {'main': True},
+                    'warning': {
+                        'title'   : "Integrity Warning",
+                        'message' : "Une des MOA doit être la MOA principale",
+                    }
+                }
+        ids = self.search(
+            cr,
+            uid,
+            [
+                ('project_id', '=', project),
+                ('moa_id', '!=', moa),
+            ],
+            context=context,
+        )
+        current_id = self.search(
+            cr,
+            uid,
+            [
+                ('project_id', '=', project),
+                ('moa_id', '=', moa),
+            ],
+            context=context,
+        )
+        self.write(cr, uid, ids, {'main': False}, context=context)
+        self.write(cr, uid, current_id, {'main': True}, context=context)
+
+        return {'value': {'main': True}}
 
 
 class ProjetPhase(osv.Model):
     _name = "cmmi.projet.phase"
 
+    _description = "Rattachement des Phases aux projets"
+
     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
         if isinstance(ids, (int, long)):
             ids = [ids]
@@ -230,11 +323,47 @@ class ProjetPhase(osv.Model):
                                 type='char',
                                 store=True,
                                 string="Nom de la phase"),
+        "phase_id": fields.many2one("cmmi.phase",
+                                    string="Phase",
+                                    required=True),
+        "projet_id": fields.many2one("cmmi.projet",
+                                     string="Projet",
+                                     required=True),
         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
+    }
+
+
+class ProjetTeammember(osv.Model):
+    _name = "cmmi.projet.teammember"
+
+    def _get_partner_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        return dict([(i, r.partner_id.name) for i, r in
+                zip(ids, self.browse(cr, uid, ids, context=context))])
+
+    def _get_project_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        return dict([(i, r.projet_id.name) for i, r in
+                zip(ids, self.browse(cr, uid, ids, context=context))])
+
+    _columns = {
+        "name": fields.function(_get_partner_name,
+                                type='char',
+                                store=True,
+                                string="Nom du partner"),
+        "projet_name": fields.function(_get_project_name,
+                                       type='char',
+                                       store=True,
+                                       string="Nom du projet"),
+        "affecte": fields.integer(string="Affecté à"),
+        "depuis": fields.date(string="Depuis"),
+        "jusqua": fields.date(string="Jusqu'à"),
         "projet_id": fields.many2one("cmmi.projet",
                                      string="Projet",
                                      required=True),
-        "phase_id": fields.many2one("cmmi.phase",
-                                    string="Phase",
+        "partner_id": fields.many2one("res.partner",
+                                    string="Team Member",
                                     required=True),
     }
\ No newline at end of file