<?php
/**
 *
 *
 * @package opencimetiere
 * @version SVN : $Id$
 */

//
require_once "../gen/obj/defunt.class.php";

class defunt extends defunt_gen {

    function defunt($id, &$db, $DEBUG) {
        //
        $this->constructeur($id, $db, $DEBUG);
        // Récupération des paramètres nécessaires aux calculs de taille et de
        // durée
        $this->init_parameters();
    }

    function init_parameters() {
        //
        $default_taille_cercueil = 1;
        $this->taille_cercueil = $this->f->getParameter("taille_cercueil");
        if (is_null($this->taille_cercueil)) {
            $this->taille_cercueil = $default_taille_cercueil;
        }
        //
        $default_taille_urne = 0.1;
        $this->taille_urne = $this->f->getParameter("taille_urne");
        if (is_null($this->taille_urne)) {
            $this->taille_urne = $default_taille_urne;
        }
        //
        $default_taille_reduction = 0.5;
        $this->taille_reduction = $this->f->getParameter("taille_reduction");
        if (is_null($this->taille_reduction)) {
            $this->taille_reduction = $default_taille_reduction;
        }
        //
        $default_temps_reduction = 5;
        $this->temps_reduction = $this->f->getParameter("temps_reduction");
        if (is_null($this->temps_reduction)) {
            $this->temps_reduction = $default_temps_reduction;
        }
    }

   function verifier($val,&$db,$DEBUG){
      $this->correct=True;
      // obligatoire  [Compulsory]
      if ($this->valF['nom']==""){
         $this->correct=false;
         $this->addToMessage( " "._("nom")." "._("obligatoire"));
      }
      // verification de date   [Check of date]
      // coherence des actions   [coherence of the actions]
      if($val['datenaissance']!=""   and $val['datedeces']!="") {
         if($this->valF['datenaissance']>$this->valF['datedeces']){
            $this->correct=false;
            $this->addToMessage(_("datenaissance")." > "._("datedeces")." [".
            $val['datenaissance'].
            " > ".$val['datedeces']."]");
         }
      }
      //  suppression des controle AC2i version 1.07
      if($val['dateinhumation']!=""   and $val['datedeces']!=""
           and $val['dateinhumation']!="00/00/0000"
           and $val['datedeces']!="00/00/0000") {
         if($this->valF['datedeces']>$this->valF['dateinhumation']){
            $this->correct=false;
            $this->addToMessage(_("datedeces")." > "._("dateinhumation")." [".
            $val['datedeces']." > ".$val['dateinhumation']."]");
         }
      }
      // *** 1.07 test null /defaut
      if(($val['dateinhumation']!="" and $val['dateinhumation']!="00/00/0000")
         and ($val['dateexhumation']!="" and $val['dateexhumation']!="00/00/0000")) {
         if($this->valF['dateinhumation']>$this->valF['dateexhumation']){
            $this->correct=false;
            $this->addToMessage(_("dateinhumation")." > "._("dateexhumation")." [".
            $val['dateinhumation']." > ".$val['dateexhumation']."]");
         }
      }
      // regle de date de reduction = $this->temps_reduction  [Rule of date of reduction]
      // exception colombarium  et terraincommunal et depositoire
      // [exception Columbarium and municipal ground and depository]
      if(($val['dateinhumation']!="" and $val['dateinhumation']!="00/00/0000")
         and ($val['datereduction']!="" and $val['datereduction']!="00/00/0000")
         and ($this->retourformulaire != "colombarium"
         and $this->retourformulaire != "terraincommunal"
         and $this->retourformulaire != "depositoire"
         and $this->retourformulaire != "ossuaire")) {
         $temp =explode("-", $this->valF['dateinhumation']);
         $temp[0] = $temp[0]+$this->temps_reduction;
         $datebutoir= $temp[0]."-".$temp[1]."-".$temp[2];
         if($this->valF['datereduction'] < $datebutoir){
            $this->correct=false;
            $this->addToMessage(_("datereduction")." [".
            $val['datereduction']." < ".$this->temps_reduction.
            " "._("ans")." ".$val['dateinhumation']."]");
         }
      }

      if($val['verrou']=="Oui"){
          $this->correct=false;
          $this->addToMessage(_("operation")." "._("active"));
      }
    }//verifier [verify]

    function setType(&$form,$maj) {
        //
        parent::setType($form, $maj);
        //
        $form->setType('emplacement', 'hidden');
        //
        $form->setType('defunt', 'hiddenstatic');
        //
        $form->setType('archivetaille', 'hidden');
        //
        if ($maj == 0 || $maj == 1) {
            $form->setType('taille', 'hiddenstatic');
            $form->setType('exhumation', 'select');
            //
            $form->setType('datedeces', 'date2');
            $form->setType('dateexhumation', 'date2');
            $form->setType('datenaissance','date2');
            $form->setType('dateinhumation','date2');
            //
            if ($this->retourformulaire=='colombarium' or $this->retourformulaire=='terraincommunal'
            or $this->retourformulaire=='ossuaire' or $this->retourformulaire=='depositoire'){
               $form->setType('datereduction','hidden');
               $form->setType('reduction', 'hidden');
            }else{
               $form->setType('datereduction','date2');
               $form->setType('reduction', 'select');
            }
            //
            $form->setType('nature', 'select');
            $form->setType('historique', 'textareahiddenstatic');
            $form->setType('verrou', 'hiddenstatic');
        }
   }

    function setSelect(&$form, $maj,$db,$DEBUG) {
        //
        parent::setSelect($form, $maj, $this->db, false);
        //
      if($maj<2){
         // exhumation inhumation
         $contenu=array();
         $contenu[0]=array('Non','Oui');
         $contenu[1]=array(_('Non'),_('Oui'));
         $form->setSelect("exhumation",$contenu);
         $form->setSelect("reduction",$contenu);
         //nature
         $contenu=array();
         if ($this->retourformulaire=='colombarium'){
            $contenu[0]=array('urne');
            $contenu[1]=array(_("urne"));
         }else{
            if ($this->retourformulaire=='terraincommunal'){
               $contenu[0]=array('cercueil','zinc');
               $contenu[1]=array(_('cercueil'),_("cercueil_zinc"));
            }else{
               $contenu[0]=array('cercueil','zinc','urne','boite');
               $contenu[1]=array(_('cercueil'),_("cercueil_zinc"),_("urne"),_("boite"));
            }
         }
         $form->setSelect("nature",$contenu);
      }
   }

    function setLib(&$form,$maj) {
        //
        parent::setLib($form,$maj);
        //
        $form->setLib('defunt', _("Id"));
   }

   function setOnchange(&$form,$maj){
      parent::setOnchange($form,$maj);
      // * mise en majuscule
      // * Put in capital letter
      $form->setOnchange("nom","this.value=this.value.toUpperCase()");
      $form->setOnchange("prenom","this.value=this.value.toUpperCase()");
      $form->setOnchange("marital","this.value=this.value.toUpperCase()");
      $form->setOnchange("lieudeces","this.value=this.value.toUpperCase()");
   } // fin onChange [end]

    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){
                $form->setVal('defunt', "[...]");
                $form->setVal('emplacement', $idxformulaire);
                $form->setVal('verrou', "Non");
                if ($retourformulaire == 'colombarium') {
                    $form->setVal('taille', $this->taille_urne);
                } else {
                    $form->setVal('taille', $this->taille_cercueil);
                }
            }
        }
    }

   function triggerajouter($id,&$db,$val,$DEBUG) {
      $this->calcultaille();
      $sql ="select placeoccupe from ".DB_PREFIXE."emplacement where emplacement ="
      .$this->valF['emplacement'] ;
      $emplacement=$db->getOne($sql);
      $cle= " emplacement = ".$this->valF['emplacement'];
      $valF['placeoccupe']= $emplacement+$this->valF['taille'];
      $res= $db->autoExecute(DB_PREFIXE.'emplacement',$valF,DB_AUTOQUERY_UPDATE,$cle);
      if (database::isError($res))die($res->getMessage());
      $this->addToLog("maj place occupe dans emplacement :".$emplacement+$this->valF['taille'], VERBOSE_MODE);
      $this->addToMessage(_("enregistrement")." ".
      $this->valF['emplacement']." "._("table")." "._("emplacement").
          " [".$db->affectedRows()." "._("enregistrement")." ".
          _("mis_a_jour")."]") ;
   }

   function triggermodifier($id,&$db,$val,$DEBUG) {
      $this->calcultaille();
      $sql ="select placeoccupe from ".DB_PREFIXE."emplacement where emplacement ="
      .$this->valF['emplacement'] ;
      $emplacement=$db->getOne($sql);
      $cle= " emplacement = ".$this->valF['emplacement'];
      $valF['placeoccupe']= $emplacement+$this->valF['taille']-$val['archivetaille'];
      $res= $db->autoExecute(DB_PREFIXE.'emplacement',$valF,DB_AUTOQUERY_UPDATE,$cle);
      if (database::isError($res))die($res->getMessage());
      $this->addToLog("maj place occupe dans emplacement :".$emplacement+$this->valF['taille'], VERBOSE_MODE);
      $this->addToMessage("Enregistrement ".$this->valF['emplacement']." de la table emplacement"
          ." [".$db->affectedRows()." "._("enregistrement")." ".
             _("mis_a_jour")."]") ;
   }

   function triggersupprimer($id,&$db,$val,$DEBUG) {
     if ($val['taille']<>""){
         $sql ="select placeoccupe from ".DB_PREFIXE."emplacement where emplacement ="
         .$val['emplacement'] ;
         $emplacement=$db->getOne($sql);
         $cle= " emplacement = ".$val['emplacement'];
         $valF['placeoccupe']= $emplacement-$val['taille'];
         $res= $db->autoExecute(DB_PREFIXE.'emplacement',$valF,DB_AUTOQUERY_UPDATE,$cle);
         if (database::isError($res))die($res->getMessage());
         $this->addToLog(_("maj place occupe dans emplacement").":".$emplacement+$this->valF['taille'], VERBOSE_MODE);
         $this->addToMessage(_("enregistrement")." ".$this->valF['emplacement']." "._("table")." "._("emplacement").
             " [".$db->affectedRows()." "._("enregistrement")." ".
             _("mis_a_jour")."]") ;
      }
   }

   function calcultaille() {
        // calcul de la taille occupee par le defunt
        if ($this->valF['exhumation']=="Oui") {
            $this->valF['taille'] = 0;
        } else {
            if ($this->valF['nature']=="urne") {
                $this->valF['taille'] = $this->taille_urne;
            } else {
                if ($this->valF['reduction']=="Oui" or $this->valF['nature']=="boite") {
                    $this->valF['taille'] = $this->taille_reduction;
                } else {
                    $this->valF['taille'] = $this->taille_cercueil;
                }
            }
        }
        //
        $this->addToLog(_("taille")." = ".$this->valF['taille']);
    }

    function setLayout(&$form, $maj) {
        //
        $bloc_global_width_full = "span12";
        $bloc_global_width_half = "span6";

        //// CONTAINER GLOBAL - START
        $this->form->setBloc("defunt", "D", "", "emplacement-form defunt-form form-action-".$maj);

        //// LIGNE ID - START
        $this->form->setBloc("defunt", "D", "", "row-fluid");
            //// BLOC ID - START
            $this->form->setBloc("defunt", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-identification ");
                //// BLOC ID - START
                $this->form->setBloc("defunt", "D", "", "span6");
                $this->form->setBloc("emplacement", "F");
                //// BLOC ID - END
                //// BLOC VERROU - START
                $this->form->setBloc("verrou", "DF", "", "span6 emplacement-field-verrou");
                //// BLOC VERROU - END
            $this->form->setBloc("verrou", "F");
            //// BLOC ID - END
        $this->form->setBloc("verrou", "F");
        //// LIGNE ID - END

        //// LIGNE PERSONNE + INFOS + ADRESSE - START
        $this->form->setBloc("titre", "D", "", "row-fluid");
            //// BLOC PERSONNE + INFOS + ADRESSE - START
            $this->form->setBloc("titre", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
                //// LIGNE PERSONNE + INFOS + ADRESSE - START
                $this->form->setBloc("titre", "D", "", "row-fluid");
                    //// BLOC PERSONNE + INFOS - START
                    $this->form->setBloc("titre", "D", "", $bloc_global_width_half);
                        //// LIGNE PERSONNE - START
                        $this->form->setBloc("titre", "D", "", "row-fluid");
                            //// BLOC PERSONNE - START
                            $this->form->setBloc("titre", "D", _("defunt"), " emplacement-bloc-personne span12");
                                $this->form->setBloc("titre", "D", "", "span12");
                                $this->form->setBloc("lieudeces", "F");
                            $this->form->setBloc("lieudeces", "F");
                            //// BLOC PERSONNE - END
                        $this->form->setBloc("lieudeces", "F");
                        //// LIGNE PERSONNE - END
                    $this->form->setBloc("lieudeces", "F");
                    //// BLOC PERSONNE + INFOS - END
                    //// BLOC ADRESSE - START
                    $this->form->setBloc("nature", "D", "", $bloc_global_width_half);
                        //// LIGNE PERSONNE - START
                        $this->form->setBloc("nature", "D", "", "row-fluid");
                            //// BLOC PERSONNE - START
                            $this->form->setBloc("nature", "D", _("nature"), " emplacement-bloc-personne span12");
                                $this->form->setBloc("nature", "D", "", "span12");
                                $this->form->setBloc("taille", "F");
                            $this->form->setBloc("taille", "F");
                            //// BLOC PERSONNE - END
                        $this->form->setBloc("taille", "F");
                        //// LIGNE PERSONNE - END
                        //// LIGNE ADRESSE - START
                        $this->form->setBloc("dateinhumation", "D", "", "row-fluid");
                            //// BLOC ADRESSE - START
                            $this->form->setBloc("dateinhumation", "D", _("Informations funeraires"), "emplacement-bloc-adresse span12");
                                $this->form->setBloc("dateinhumation", "D", "", "span12");
                                $this->form->setBloc("dateexhumation", "F");
                            $this->form->setBloc("dateexhumation", "F");
                            //// BLOC ADRESSE - END
                        $this->form->setBloc("dateexhumation", "F");
                        //// LIGNE ADRESSE - END
                    $this->form->setBloc("dateexhumation", "F");
                    //// BLOC ADRESSE - END
                $this->form->setBloc("dateexhumation", "F");
                //// LIGNE PERSONNE + INFOS + ADRESSE - END
            $this->form->setBloc("dateexhumation", "F");
            //// BLOC PERSONNE + INFOS + ADRESSE - END
        $this->form->setBloc("dateexhumation", "F");
        //// LIGNE PERSONNE + INFOS + ADRESSE - END

        //// LIGNE HISTORIQUE - START
        $this->form->setBloc("historique", "D", "", "row-fluid");
            //// BLOC HISTORIQUE - START
            $this->form->setBloc("historique", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-historique ");
                $this->form->setBloc("historique", "DF", "", "group span12");
            $this->form->setBloc("historique", "F");
            // BLOC HISTORIQUE - END
        $this->form->setBloc("historique", "F");
        //// LIGNE HISTORIQUE - START

        //// LIGNE OBSERVATION - START
        $this->form->setBloc("observation", "D", "", "row-fluid");
            //// BLOC OBSERVATION - START
            $this->form->setBloc("observation", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-observation ");
                $this->form->setBloc("observation", "DF", "", "group span12");
            $this->form->setBloc("observation", "F");
            // BLOC OBSERVATION - END
        $this->form->setBloc("observation", "F");
        //// LIGNE OBSERVATION - START

        //// CONTAINER GLOBAL - END
        $this->form->setBloc("observation", "F");
    }

}

?>