<?php
//$Id$ 
//gen openMairie le 10/05/2013 09:17

require_once ("../gen/obj/affectation.class.php");

class affectation extends affectation_gen {

    function affectation($id,&$db,$debug) {
        $this->constructeur($id,$db,$debug);
    }// fin constructeur

    function verifier($val,&$db,$DEBUG){
        parent :: verifier($val,$db,$DEBUG);
        if($this->correct==True){
            // affectation obligatoire d un bureau
            if($this->valF['decision']==True
            and $this->valF['bureau']=='T'
            and $this->valF['poste']!='DELEGUE TITULAIRE' and $this->valF['poste']!='DELEGUE SUPPLEANT'){
               $this->correct=false;
               $this->msg= $this->msg._("Choix du poste :  DELEGUE TITULAIRE ou DELEGUE SUPPLEANT pour Tous BUREAUX");
            }
            // verification si le poste est occupe par un autre elu / non bloquant
            //
            if($this->valF['decision'] == 'True' and $this->valF['bureau']!='T'){
                switch ($this->valF['periode']){
                  case "journee" :
                      $sql="select * from  ".DB_PREFIXE."affectation where
                          scrutin ='".$this->valF['scrutin']."'
                          and poste ='".$this->valF['poste']."'
                          and bureau ='".$this->valF['bureau']."'
                          and decision  is True";
                          if($this->valF['affectation']!='') //maj==1
                              $sql=$sql." and affectation !=".$this->valF['affectation'];
                      break;
                  case "matin" :
                      $sql="select * from  ".DB_PREFIXE."affectation where
                          scrutin ='".$this->valF['scrutin']."'
                          and poste ='".$this->valF['poste']."'
                          and bureau ='".$this->valF['bureau']."'
                          and (periode ='journee' or periode='matin')
                          and decision  is True";
                          if($this->valF['affectation']!='') //maj==1
                              $sql=$sql." and affectation !=".$this->valF['affectation'];
                       break;
                  case "apres-midi":
                      $sql="select * from  ".DB_PREFIXE."affectation where
                          scrutin ='".$this->valF['scrutin']."'
                          and poste ='".$this->valF['poste']."'
                          and bureau ='".$this->valF['bureau']."'
                          and (periode ='journee' or periode='apres-midi')
                          and decision  is True";
                          if($this->valF['affectation']!='') //maj==1
                              $sql=$sql." and affectation !=".$this->valF['affectation'];
                      break;
                  default:
                }
                $res = $db->query($sql);
                if($DEBUG==1) echo $sql;
                if (DB :: isError($res))
                   die($res->getMessage(). " => Echec  ".$sql);
                else{
                    $nbligne=$res->numrows();
                    if ($nbligne>0){
                       //$this->correct=false;
                       $this->msg = $this->msg."&nbsp;"._("il_y_a")."&nbsp;".$nbligne.
                       " ".$this->valF['poste']."&nbsp;"._("pour")."&nbsp;"._("le")."&nbsp;"._("bureau")."&nbsp;".$this->valF['bureau'].
                       " - ".$this->valF['periode'].
                       " [ ".$this->valF['scrutin']." ]";
                    }
                }
            }
            // verification si l agent n occupe pas un poste dans la meme periode
            // il peut y avoir plusieurs elections en meme temps
            // non bloquant
             //
            if($this->valF['decision'] == 'True' and $this->valF['bureau']!='T'){

                // date du scrutin
                $sql="select date_scrutin from  ".DB_PREFIXE."scrutin where scrutin = '".
                $this->valF['scrutin']."'";
                $date_scrutin=$db->getOne($sql);
                // test periode
                 $libelle_periode="";
            
                switch ($this->valF['periode']){
                  case "journee" :
                      $sql="select * from  ".DB_PREFIXE."affectation inner join ".DB_PREFIXE."scrutin
                          on scrutin.scrutin=affectation.scrutin
                          where date_scrutin ='".$date_scrutin."'
                          and elu ='".$this->valF['elu']."'
                          and decision  is True";
                          if($this->valF['affectation'])
                              $sql=$sql." and affectation !=".$this->valF['affectation'];
                      break;
                  case "matin" :
                      $sql="select * from  ".DB_PREFIXE."affectation inner join ".DB_PREFIXE."scrutin
                          on scrutin.scrutin=affectation.scrutin
                          where date_scrutin ='".$date_scrutin."'
                          and elu ='".$this->valF['elu']."'
                          and (periode ='journee' or periode='matin')
                          and decision  is True";
                          if($this->valF['affectation'])
                              $sql=$sql." and affectation !=".$this->valF['affectation'];
                       break;
                  case "apres-midi":
                      $sql="select * from  ".DB_PREFIXE."affectation inner join ".DB_PREFIXE."scrutin
                          on scrutin.scrutin=affectation.scrutin
                          where date_scrutin ='".$date_scrutin."'
                          and elu ='".$this->valF['elu']."'
                          and (periode ='journee' or periode='apres-midi')
                          and decision  is True";
                          if($this->valF['affectation'])
                              $sql=$sql." and affectation !=".$this->valF['affectation'];
                      break;
                  default:
                }
                $res = $db->query($sql);
                if($DEBUG==1) echo $sql;
                if (DB :: isError($res))
                   die($res->getMessage(). " => Echec  ".$sql);
                else{
                    $nbligne=$res->numrows();
                    if ($nbligne>0){
                       //$this->correct=false;
                       $this->msg = $this->msg."&nbsp;"._("il_y_a")."&nbsp;".$nbligne.
                       "&nbsp;"._("poste")."&nbsp;"._("occupe")."&nbsp;"._("pour")."&nbsp;"._("elu")."&nbsp;".$this->valF['elu'].
                       " - ".$this->valF['periode'].
                       " [ ".$date_scrutin." ] ";
                    }
                }
            }
            // verification des bureaux Cas des cantonales
            if($this->valF['bureau']!='T'){
                $sql="select canton from  ".DB_PREFIXE."scrutin where scrutin = '".
                $this->valF['scrutin']."'";
                $canton=$db->getOne($sql);
                if($canton!='T'){
                    // selection du canton du bureau
                    $sql="select canton from  ".DB_PREFIXE."bureau where bureau = '".
                    $this->valF['bureau']."'";
                    $canton_bureau=$db->getOne($sql);
                    if($canton != $canton_bureau){
                          $this->correct=false;
                          $this->msg = $this->msg._("BUREAU")."&nbsp;".$this->valF['bureau']."&nbsp;"._("NON")."&nbsp;"._("dans")."&nbsp;"._("canton")."&nbsp;".$canton.
                          "&nbsp;("._("scrutin")."&nbsp;:&nbsp;".$this->valF['scrutin'].")";
                    }
                    
                }
            }
        }
    
    }//verifier
    
    
    
    function setTaille(&$form,$maj){
        parent::setTaille($form,$maj);
        $form->setTaille('note', 80);
    }
    
    function setMax(&$form,$maj){
        parent::setMax($form,$maj);
        $form->setMax('note', 5);
    }
    
    function setGroupe(&$form,$maj){
         parent::setGroupe($form,$maj);
        $form->setGroupe('scrutin','D');
        $form->setGroupe('periode','G');
        $form->setGroupe('poste','F');
    }
    function setValsousformulaire(&$form,$maj,$validation,$idxformulaire,$retourformulaire,$typeformulaire,&$db,$DEBUG=null){
        parent::setValsousformulaire($form,$maj,$validation,$idxformulaire,$retourformulaire,$typeformulaire,$db,$DEBUG=null);
        if ($validation==0) {
          if ($maj == 0){
            if(isset($_SESSION['scrutin']))
                $scrutin=$_SESSION['scrutin'];
            else
                $scrutin="";
            $form->setVal($retourformulaire, $idxformulaire);
            $form->setVal("scrutin", $scrutin);
            $form->setVal("periode", "journee");
        }}
    }
    function setLib(&$form,$maj) {
        parent::setLib($form,$maj);
        $form->setLib('scrutin','');
        $form->setLib('periode','');
        $form->setLib('poste','');
        $form->setLib('bureau','');
    
    }
    function setRegroupe(&$form,$maj){
        parent::setRegroupe($form,$maj);
        $form->setRegroupe('scrutin','D',_("scrutin"));
        $form->setRegroupe('periode','G','');
        $form->setRegroupe('poste','F','');
    }
}// fin classe
?>