Modification de la relation projet-phase en projet-phase.type
[OpenERP/cmmi.git] / projet.py
index 8967371..9381694 100644 (file)
--- a/projet.py
+++ b/projet.py
@@ -28,13 +28,51 @@ 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("cmmi.partner.teammember",
+        "team_members": fields.many2many("res.partner",
                                          "cmmi_projet_teammember_rel",
                                          "projet_id",
                                          "partner_id",
@@ -60,19 +98,41 @@ 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)
+
+        # Récupération des ids de toutes les phases
+        phase_model = self.pool.get("cmmi.phase.type")
+        phases_ids = phase_model.search(cr, uid, [])
+
+        # Création des relations
+        projet_phase_model = self.pool.get("cmmi.projet.phase")
+        for phase_id in phases_ids:
+            projet_phase_model.create(
+                cr,
+                uid,
+                {
+                    'projet_id': project_id,
+                    'phase_id': phase_id,
+                    'selectionne': False,
+                }
+            )
+
+        return project_id
+
+
     def action_add_domain(self, cr, uid, ids, context=None):
         pass
 
@@ -155,13 +215,43 @@ class ProjetMoe(osv.Model):
                                 string="Nom de la MOE"),
         "main": fields.boolean(string="MOE principale ?"),
         "project_id": fields.many2one("cmmi.projet",
-                                      string="Projet"),
+                                      string="Projet",
+                                      required=True),
         "moe_id": fields.many2one("cmmi.mo.moe",
-                                      string="MOE"),
+                                  string="MOE",
+                                  required=True),
     }
 
     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):
@@ -180,13 +270,43 @@ class ProjetMoa(osv.Model):
                                 string="Nom de la MOA"),
         "main": fields.boolean(string="MOA principale ?"),
         "project_id": fields.many2one("cmmi.projet",
-                                      string="Projet"),
+                                      string="Projet",
+                                      required=True),
         "moa_id": fields.many2one("cmmi.mo.moa",
-                                      string="MOA"),
+                                  string="MOA",
+                                  required=True),
     }
 
     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):
@@ -205,7 +325,9 @@ class ProjetPhase(osv.Model):
                                 string="Nom de la phase"),
         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
         "projet_id": fields.many2one("cmmi.projet",
-                                     string="Projet"),
-        "phase_id": fields.many2one("cmmi.phase",
-                                    string="Phase"),
+                                     string="Projet",
+                                     required=True),
+        "phase_id": fields.many2one("cmmi.phase.type",
+                                    string="Phase",
+                                    required=True),
     }
\ No newline at end of file