Ajout d'un champ charge_reel dans palier
[OpenERP/cmmi.git] / projet.py
index e1858da..f37b957 100644 (file)
--- a/projet.py
+++ b/projet.py
 '''
 
 from openerp.osv import osv, fields
-from gtk import TRUE
 
+
+# ================================== PROJET ================================= #
 class Projet(osv.Model):
 
-    _name = "projet.projet"
+    _name = "cmmi.projet"
 
-    _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "description": fields.text(string="Description"),
-        "domaines": fields.many2many("projet.domaine",
-                                     "projet_projet_domaine_rel",
-                                     "projets",
-                                     string="Domaines"),
-        "structures": fields.many2many("projet.structure",
-                                       "projet_projet_structure_rel",
-                                       "projets",
-                                       string="Structures"),
-        "structures_moe": fields.many2many("projet.structure",
-                                           "projet_projet_structure_rel",
-                                           "projets",
-                                           string="Structures",
-                                           domain=[('role_mo_id.type_mo', '=', "MOE")]),
-        "structures_moa": fields.many2many("projet.structure",
-                                           "projet_projet_structure_rel",
-                                           "projets",
-                                           string="Structures",
-                                           domain=[('role_mo_id.type_mo', '=', "MOA")]),
-        "team_members": fields.many2many("projet.teammember",
-                                         "projet_projet_teammember_rel",
-                                         "projets",
-                                         string="Team Members"),
-        "modules": fields.one2many("projet.module",
-                                   "projet_id",
-                                   string="Modules"),
-        "chantiers": fields.one2many("projet.chantier",
-                                     "projet_id",
-                                     string="Chantiers"),
-        "paliers": fields.one2many("projet.palier",
-                                   "projet_id",
-                                   string="Paliers"),
-        "phases": fields.one2many("projet.phase",
-                                   "projet_id",
-                                   string="Phases"),
-        "evolutions":fields.one2many("projet.evolution",
-                                     "projet_id",
-                                     string="Evolutions"),
-        "moe_id": fields.many2one("projet.moe", string="MoE", required=True),
-        "moa_id": fields.many2one("projet.moa", string="MoA", required=True),
-        "main_domain": fields.many2one("projet.domaine",
-                                       string="Domaine principal"),
-        "main_structure": fields.many2one("projet.structure",
-                                          string="Structure principale"),
-    }
+    _description = "Table des projets."
 
+    def _get_main_domain(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if type(ids) in (int, float):
+            ids = [ids]
 
-class Evolution(osv.Model):
-    _name = "projet.evolution"
+        projects = self.browse(cr, uid, ids, context=context)
 
-    _priorites = [("incontournable", "Incontournable"),
-                  ("necessaire", "Nécéssaire"),
-                  ("utile", "Utile")]
+        result = {}
 
-    _statuts = [("cree", "Crée"), ("encours", "En cours"),
-                ("termine", "Terminé"), ("abandonne", "Abandonné"),
-                ("suspendu", "Suspendu")]
-
-    _columns = {
-        "pid": fields.integer(string="PID"),
-        "name": fields.char(string="Title", size=64, required=True),
-        "description": fields.text(string="Description"),
-        "objectif": fields.text(string="Objectif"),
-        "commentaire": fields.text(string="Commentaire"),
-        "keywords": fields.text(string="Mots clés"),
-        "priorite": fields.selection(_priorites, string="Priorité"),
-        "statut": fields.selection(_statuts, string="Statut"),
-        "charges": fields.one2many("projet.charge",
-                                   "evolution_id",
-                                   string="Charges"),
-        "module_id": fields.many2one("projet.module",
-                                     string="Modules"),
-        "chantier_id": fields.many2one("projet.chantier",
-                                    string="Chantier"),
-        "palier_id": fields.many2one("projet.palier",
-                                     string="Palier"),
-        "phase_id": fields.many2one("projet.phase",
-                                    string="Phase"),
-        "projet_id": fields.many2one("projet.projet",
-                                     string="Projet"),
-    }
+        for project in projects:
+            for domain in project.domains:
+                if domain.main:
+                    result[project.id] = domain.name
+                    break
+            else:
+                result[project.id] = "Any domain is selected as main domain yet"
 
+        return result
 
-class Structure(osv.Model):
 
-    _name = "projet.structure"
+    def _get_main_moa(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if type(ids) in (int, float):
+            ids = [ids]
 
-    _statuts = [("actif", "Actif"), ("inactif", "Inactif")]
+        projects = self.browse(cr, uid, ids, context=context)
 
-    _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "code": fields.char(string="Code", size=8, required=True),
-        "description": fields.text(string="Description"),
-        "parent_id": fields.many2one("projet.structure", string="Parent_id"),
-        "statut": fields.selection(_statuts, string="Statut"),
-        "role_mo_id":fields.many2one("projet.role_mo", string="Role MO"),
-        "projets": fields.many2many("projet.projet",
-                                    "projet_projet_structure_rel",
-                                    "structures",
-                                    string="Projets"),
-    }
+        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"
 
-class Role_MO(osv.Model):
-    _name = "projet.role_mo"
+        return result
 
-    _statuts = [("actif", "Actif"), ("inactif", "Inactif")]
 
-    _types_mo = [("MOE", "MOE"), ("MOA", "MOA")]
+    def _get_main_moe(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if type(ids) in (int, float):
+            ids = [ids]
 
-    _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "code": fields.char(string="Code", size=8, required=True),
-        "description": fields.text(string="Description"),
-        "statut": fields.selection(_statuts, string="Statut"),
-        "type_mo": fields.selection(_types_mo, string="Type de MO", required=True),
-        "structures": fields.one2many("projet.structure",
-                                   "role_mo_id",
-                                   string="MOs"),
-        "mo_ids": fields.one2many("projet.mo",
-                                   "role_mo_id",
-                                   string="MOs"),
-    }
+        projects = self.browse(cr, uid, ids, context=context)
 
-class Module(osv.Model):
-    _name = "projet.module"
+        result = {}
 
-    _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "description": fields.text(string="Description"),
-        "projet_id": fields.many2one("projet.projet",
-                                     string="Projet",
-                                     required=True),
-        "evolutions": fields.one2many("projet.evolution",
-                                      "module_id",
-                                      string="Evolutions")
-    }
+        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
 
 
-class Domaine(osv.Model):
-    _name = "projet.domaine"
+    def _get_phases_selectionnees(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        result = {}
+        for projet in self.browse(cr, uid, ids, context=context):
+            res = []
+            for phase in projet.phases:
+                #import pdb; pdb.set_trace()
+                if phase.selectionne:
+                    res.append(phase.id)
+            result[projet.id] = res
+        return result
 
-    _statuts = [("actif", "Actif"), ("inactif", "Inactif")]
 
     _columns = {
         "name": fields.char(string="Title", size=64, required=True),
-        "code": fields.char(string="Code", size=8),
         "description": fields.text(string="Description"),
-        "ordre": fields.integer(string="Ordre"),
-        "parent_id": fields.many2one("projet.domaine", string="Parent_id"),
-        "statut": fields.selection(_statuts, string="Statut"),
-        "projets": fields.many2many("projet.projet",
-                                    "projet_projet_structure_rel",
-                                    "domaines",
-                                    string="Projets"),
+        "use_chantier": fields.boolean(string="Utilisation de la notion de chantier ?"),
+        "use_palier": fields.boolean(string="Utilisation de la notion de palier ?"),
+
+        # Backrefs
+        "domains": fields.one2many("cmmi.projet.domaine",
+                                    "project_id",
+                                    string="Domaines"),
+        "moe_ids": fields.one2many("cmmi.projet.moe",
+                                   "project_id",
+                                   string="MOEs"),
+        "moa_ids": fields.one2many("cmmi.projet.moa",
+                                   "project_id",
+                                   string="MOAs"),
+        "team_members": fields.one2many("cmmi.projet.teammember",
+                                         "projet_id",
+                                         string="Team Members"),
+        "modules": fields.one2many("cmmi.description.module",
+                                   "projet_id",
+                                   string="Modules"),
+        "chantiers": fields.one2many("cmmi.axes.chantier",
+                                     "projet_id",
+                                     string="Chantiers"),
+        "paliers": fields.one2many("cmmi.axes.palier",
+                                   "projet_id",
+                                   string="Paliers"),
+        "phases": fields.one2many("cmmi.projet.phase",
+                                  "projet_id",
+                                  string="Phases"),
+        "evolutions": fields.one2many("cmmi.evolution",
+                                      "projet_id",
+                                      string="Evolutions"),
+
+        # Champs fonction rapatriant les mo ou domaine principaux
+        "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"),
+        "phases_selectionnees": fields.function(_get_phases_selectionnees,
+                                                type="one2many",
+                                                obj="cmmi.projet.phase",
+                                                string="Phases sélectionnées"),
     }
 
+    _defaults = {
+        "use_chantier": True,
+        "use_palier": True,
+    }
 
-class Teammember(osv.Model):
-    _name = "projet.teammember"
 
-    _inherit = "res.partner"
+    def create(self, cr, uid, vals, context=None):
+        project_id = osv.Model.create(self, cr, uid, vals, context=context)
 
-    _columns = {
-        "projets": fields.many2many("projet.projet",
-                                    "projet_projet_teammember_rel",
-                                    "team_members",
-                                    string="Projets"),
-        "charges": fields.one2many("projet.projet",
-                                   "team_members",
-                                   string="Charges"),
-    }
+        # Récupération des ids de toutes les phases
+        phase_model = self.pool.get("cmmi.phase")
+        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,
+                }
+            )
 
-class Type_Phase(osv.Model):
-    _name = "projet.type_phase"
+        return project_id
 
-    _statuts = [("actif", "Actif"), ("inactif", "Inactif")]
 
-    _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "description": fields.text(string="Description"),
-        "statut": fields.selection(_statuts, string="Statut"),
-    }
+    def action_add_domain(self, cr, uid, ids, context=None):
+        pass
 
+    def action_add_moe(self, cr, uid, ids, context=None):
+        pass
 
-class Phase(osv.Model):
-    _name = "projet.phase"
+    def action_add_moa(self, cr, uid, ids, context=None):
+        pass
 
-    _statuts = [("actif", "Actif"), ("inactif", "Inactif")]
 
 
-    _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "description": fields.text(string="Description"),
-        "statut": fields.selection(_statuts, string="Statut"),
-        "type_phase_id": fields.many2one("projet.type_phase",
-                                         string="Type phase"),
-        "projet_id": fields.many2one("projet.projet",
-                                     string="Projet",
-                                     required=True),
-        "charges": fields.one2many("projet.charge",
-                                   "phase_id",
-                                   string="Charges"),
-        "evolutions": fields.one2many("projet.evolution",
-                                      "phase_id",
-                                      string="Evolutions"),
-        "palier_id": fields.many2one("projet.palier",
-                                     string="Palier"),
-    }
+# ============================= PROJET DOMAINE ============================== #
+class ProjetDomaine(osv.Model):
+
+    _name = "cmmi.projet.domaine"
 
-#TODO trouver un nom a cette chose
-class qqch(osv.Model):
-    _name = "projet.qqch"
+    _description = "Rattachement des domaines a un projet."
 
-    _statuts = [("cree", "Crée"), ("encours", "En cours"),
-                ("termine", "Terminé"), ("abandonne", "Abandonné"),
-                ("suspendu", "Suspendu"), ("generique", "Générique")]
+    def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        #return {i: r.domaine_id.name for i, r in
+        #        zip(ids, self.browse(cr, uid, ids, context=context))}
+        return dict([(i, r.domaine_id.name) for i, r in
+                zip(ids, self.browse(cr, uid, ids, context=context))])
 
     _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "description": fields.text(string="Description"),
-        "commentaire": fields.text(string="Commentaire"),
-        "statut": fields.selection(_statuts, string="Statut"),
-        "version": fields.char(string="Version", size=16),
-        "date_jalon": fields.date(sring="Jalon"),
-        "date_init_deb": fields.date(string="Date initiale début"),
-        "date_init_fin": fields.date(string="Date initiale de fin"),
-        "date_plan_deb": fields.date(string="Date plannifiée début"),
-        "date_plan_fin": fields.date(string="Date plannifiée de fin"),
-        "date_reel_deb": fields.date(string="Data réelle début"),
-        "date_reel_fin": fields.date(string="Data réelle fin"),
+        "name": fields.function(_get_name,
+                                type='char',
+                                store=True,
+                                string="Nom du domaine"),
+        "main": fields.boolean(string="Domaine principal ?"),
+        "project_id": fields.many2one("cmmi.projet",
+                                      string="Projet"),
+        "domaine_id": fields.many2one("cmmi.description.domaine",
+                                      string="Domaine"),
     }
 
-    _sql_constraints = [
-        (
-            "date_init_deb_before_date_init_fin",
-            "CHECK(date_init_deb<> date_init_fin)",
-            "The date_init_deb should be previous date_init_fin",
-        ),
-        (
-            "date_plan_deb_before_date_plan_fin",
-            "CHECK(date_plan_deb <> date_plan_fin)",
-            "The date_plan_deb should be previous date_plan_fin",
-        ),
-        (
-            "date_reel_deb_before_date_reel_fin",
-            "CHECK(date_reel_deb<> date_reel_fin)",
-            "The date_reel_deb should be previous date_reel_fin",
-        ),
-    ]
-
-
-class Chantier(osv.Model):
-    _name = "projet.chantier"
-
-    _inherit = "projet.qqch"
+    def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
+        if not main:
+            return {'value': {'main': True},
+                    'warning': {
+                        'title'   : "Integrity Warning",
+                        'message' : "One of the domains should be the main domain",
+                    }
+                }
+        ids = self.search(
+            cr,
+            uid,
+            [
+                ('project_id', '=', project),
+                ('domaine_id', '!=', domaine),
+            ],
+            context=context,
+        )
+        current_id = self.search(
+            cr,
+            uid,
+            [
+                ('project_id', '=', project),
+                ('domaine_id', '=', domaine),
+            ],
+            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}}
+
+
+
+# =============================== PROJET MOE ================================ #
+class ProjetMoe(osv.Model):
+    _name = "cmmi.projet.moe"
+
+    _description = "Rattachement des MOEs au projet."
+
+    def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        return dict([(i, r.moe_id.name) for i, r in
+                zip(ids, self.browse(cr, uid, ids, context=context))])
 
     _columns = {
-        "projet_id": fields.many2one("projet.projet",
-                                     string="Projet",
-                                     required=True),
-        "evolutions": fields.one2many("projet.evolution",
-                                      "chantier_id",
-                                      string="Evolutions"),
+        "name": fields.function(_get_name,
+                                type='char',
+                                store=True, # Permet d'enregistrer le champ.
+                                string="Nom de la MOE"),
+        "main": fields.boolean(string="MOE principale ?"),
+        "project_id": fields.many2one("cmmi.projet",
+                                      string="Projet",
+                                      required=True),
+        "moe_id": fields.many2one("cmmi.mo.moe",
+                                  string="MOE",
+                                  required=True),
     }
 
-
-class Palier(osv.Model):
-    _name = "projet.palier"
-
-    _types_palier = [("normal", "Normal"), ("exceptionnel", "Exceptionnel"),
-                     ("correctif", "Correctif"), ("autre", "Autre")]
-
-    _inherit = "projet.qqch"
+    def onchange_main(self, cr, uid, ids, project, moe, main, context=None):
+        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}}
+
+
+
+# =============================== PROJET MOA ================================ #
+class ProjetMoa(osv.Model):
+    _name = "cmmi.projet.moa"
+
+    _description = "Rattachement des MOAs a une projet."
+
+    def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        return dict([(i, r.moa_id.name) for i, r in
+                zip(ids, self.browse(cr, uid, ids, context=context))])
 
     _columns = {
-        "type_palier": fields.selection(_types_palier, string="Type"),
-        "projet_id": fields.many2one("projet.projet",
-                                     string="Projet",
-                                     required=True),
-        "evolutions": fields.one2many("projet.evolution",
-                                      "palier_id",
-                                      string="Evolutions"),
-        "phases": fields.one2many("projet.phase",
-                                  "palier_id",
-                                  string="Phases"),
+        "name": fields.function(_get_name,
+                                type='char',
+                                store=True,
+                                string="Nom de la MOA"),
+        "main": fields.boolean(string="MOA principale ?"),
+        "project_id": fields.many2one("cmmi.projet",
+                                      string="Projet",
+                                      required=True),
+        "moa_id": fields.many2one("cmmi.mo.moa",
+                                  string="MOA",
+                                  required=True),
     }
 
-
-class Charge(osv.Model):
-    _name = "projet.charge"
+    def onchange_main(self, cr, uid, ids, project, moa, main, context=None):
+        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}}
+
+
+
+# =============================== PROJET PHASE ============================== #
+class ProjetPhase(osv.Model):
+    _name = "cmmi.projet.phase"
+
+    _description = "Rattachement des Phases aux projets"
 
     _columns = {
-        "name": fields.char(string="Title", size=64, required=True),
-        "description": fields.text(string="Description"),
-        "teammember_id": fields.many2one("projet.teammember",
-                                         string="Team Member",
-                                         required=True),
-        "phase_id": fields.many2one("projet.phase",
+        "name": fields.related("phase_id",
+                                "name",
+                                read_only=True,
+                                type="char",
+                                relation="cmmi.phase",
+                                string="Nom de la phase"),
+        "phase_id": fields.many2one("cmmi.phase",
                                     string="Phase",
                                     required=True),
-        "evolution_id": fields.many2one("projet.evolution",
-                                    string="Evolution",
-                                    required=True),
-        "mo_id": fields.many2one("projet.mo",
-                                 string="Mo"),
-    }
-
-class mo(osv.Model):
-    _name = "projet.mo"
-
-    _choses = [("primaire", "Primaire"),
-               ("secondaire", "Secondaire"),
-               ("generique", "Générique")]
-
-    _columns = {
-        "name": fields.char(string="Title"),
-        "description": fields.text(string="Description"),
-        "chose": fields.selection(_choses, string="Chose", required=True),
-        "role_mo_id": fields.many2one("projet.role_mo", string="Role"),
-        "charges": fields.one2many("projet.charge",
-                                   "mo_id",
-                                   string="Charges"),
+        "projet_id": fields.many2one("cmmi.projet",
+                                     string="Projet",
+                                     required=True),
+        "selectionne": fields.boolean(string="Phase sélectionnée ?"),
     }
 
-    _defaults = {
-        "chose": "generique"}
 
 
-class moe(osv.Model):
-    _name = "projet.moe"
-    _inherit = "projet.mo"
+# ============================ PROJET TEAM MEMBER =========================== #
+class ProjetTeammember(osv.Model):
+    _name = "cmmi.projet.teammember"
 
-    _columns = {
-        "projets": fields.one2many("projet.projet",
-                                     "moe_id",
-                                     string="Projets"),
-    }
+    _description = "Rattachement des Team members a un projet."
 
+    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))])
 
-class moa(osv.Model):
-    _name= "projet.moa"
-    _inherit = "projet.mo"
+    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 = {
-        "projets": fields.one2many("projet.projet",
-                                     "moa_id",
-                                     string="Projets"),
+        "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),
+        "partner_id": fields.many2one("res.partner",
+                                    string="Team Member",
+                                    required=True),
     }