Prise en compte de la MOE principale pour les projets
[OpenERP/cmmi.git] / projet.py
index 153a64d..6dc86ed 100644 (file)
--- a/projet.py
+++ b/projet.py
@@ -42,7 +42,26 @@ class Projet(osv.Model):
                     result[project.id] = moa.name
                     break
             else:
-                result[project.id] = "Any moa is selected as main moa yet"
+                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
 
@@ -79,10 +98,9 @@ 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)]),
+        "moe_id": fields.function(_get_main_moe,
+                                  type="string",
+                                  string="MOE principale"),
         "moa_id": fields.function(_get_main_moa,
                                   type="string",
                                   string="MOA principale"),
@@ -205,7 +223,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):