Prise en compte de la MOA principale pour les projets
[OpenERP/cmmi.git] / projet.py
index d312223..153a64d 100644 (file)
--- a/projet.py
+++ b/projet.py
@@ -28,6 +28,25 @@ 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
+
+
     _columns = {
         "name": fields.char(string="Title", size=64, required=True),
         "description": fields.text(string="Description"),
@@ -64,15 +83,15 @@ class Projet(osv.Model):
                                   "project_id",
                                   string="MOE principale",
                                   domaine=[('main', '=', True)]),
-        "moa_id": fields.one2many("cmmi.projet.moa",
-                                  "project_id",
-                                  string="MOA principale",
-                                  domaine=[('main', '=', True)]),
+        "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)
 
@@ -213,7 +232,35 @@ 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):