noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_forecast.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /*!\file
00023  * \brief manage the table forecast
00024  */
00025 /*!
00026  * \brief manage the table forecast
00027  */
00028 class Forecast
00029 {
00030   private static $variable=array ("id"=>"f_id","name"=>"f_name","start_date"=>"f_start_date","end_date"=>"f_end_date");
00031     private $cn;
00032     /**
00033      * @brief constructor
00034      * @param $p_init Database object
00035      */
00036     function __construct ($p_init,$p_id=0)
00037     {
00038         $this->cn=$p_init;
00039         $this->f_id=$p_id;
00040     }
00041     public function get_parameter($p_string)
00042     {
00043         if ( array_key_exists($p_string,self::$variable) )
00044         {
00045             $idx=self::$variable[$p_string];
00046             return $this->$idx;
00047         }
00048         else
00049             exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00050     }
00051     public function set_parameter($p_string,$p_value)
00052     {
00053         if ( array_key_exists($p_string,self::$variable) )
00054         {
00055             $idx=self::$variable[$p_string];
00056             $this->$idx=$p_value;
00057         }
00058         else
00059             exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00060     }
00061     public function get_info()
00062     {
00063         return var_export(self::$variable,true);
00064     }
00065 
00066     public function verify()
00067     {
00068         // Verify that the elt we want to add is correct
00069         // the f_name must be unique (case insensitive)
00070         if ( strlen(trim($this->f_name))==0) throw new Exception(_('Le nom ne peut pas ĂȘtre vide'));
00071 
00072         return 0;
00073     }
00074     public function save()
00075     {
00076         /* please adapt */
00077         if (  $this->get_parameter("id") == 0 )
00078             $this->insert();
00079         else
00080             $this->update();
00081     }
00082 
00083     public function insert()
00084     {
00085         if ( $this->verify() != 0 ) return;
00086         $sql="insert into forecast (f_name,f_start_date,f_end_date) ".
00087              " values ($1,$2,$3)  returning f_id";
00088         $res=$this->cn->exec_sql(
00089                  $sql,
00090                  array($this->f_name,$this->f_start_date,$this->f_end_date)
00091              );
00092         $this->f_id=Database::fetch_result($res,0,0);
00093     }
00094 
00095     /**
00096      *@brief update the forecast table
00097      */
00098     public function update()
00099     {
00100         if ( $this->verify() != 0 ) return;
00101 
00102         $sql="update forecast set f_name=$1,f_start_date=$2,f_end_date=$3 ".
00103              " where f_id = $4";
00104         $res=$this->cn->exec_sql(
00105                  $sql,
00106                  array($this->f_name,$this->f_start_date,$this->f_end_date, $this->f_id)
00107              );
00108 
00109     }
00110     /**
00111      *@brief load all the existing forecast
00112      *@param $p_cn is an Database object
00113      *@return array of f_id and f_name
00114      */
00115     public static function load_all($p_cn)
00116     {
00117         $sql="select f_id, f_name,f_start_date,f_end_date from forecast";
00118         $ret=$p_cn->get_array($sql);
00119         return $ret;
00120     }
00121     public function load()
00122     {
00123         $sql="select f_name,f_start_date ,f_end_date from forecast where f_id=$1";
00124         $res=$this->cn->exec_sql(
00125                  $sql,
00126                  array($this->f_id)
00127              );
00128         if ( Database::num_row($res) == 0 ) return -1;
00129         $row=Database::fetch_array($res,0);
00130         foreach ($row as $idx=>$value)
00131         {
00132             $this->$idx=$value;
00133         }
00134 
00135     }
00136     public function delete()
00137     {
00138         $sql="delete from forecast where f_id=$1";
00139         $res=$this->cn->exec_sql($sql,array($this->f_id));
00140     }
00141     public function object_clone()
00142     {
00143       $this->load();
00144       /* save into the table forecast */
00145       $sql="insert into forecast(f_name,f_start_date,f_end_date) select 'clone '||f_name,f_start_date,f_end_date from forecast where f_id=$1 returning f_id";
00146       $new=$this->cn->get_value($sql,array($this->f_id));
00147 
00148       /* save into forecast_cat */
00149       $sql="insert into forecast_cat(fc_desc,f_id,fc_order)  select fc_desc,$1,fc_order from forecast_cat where f_id=$2 returning fc_id" ;
00150       $array=$this->cn->get_array($sql,array($new,$this->f_id));
00151 
00152       $old=$this->cn->get_array("select fc_id from forecast_cat where f_id=$1",array($this->f_id));
00153       /* save into forecast_item */
00154       for ($i=0;$i<count($array);$i++)
00155         {
00156           $this->cn->exec_sql("insert into forecast_item (fi_text,fi_account,fi_card,fi_order,fc_id,fi_amount,fi_debit,fi_pid) ".
00157                               " select fi_text,fi_account,fi_card,fi_order,$1,fi_amount,fi_debit,fi_pid ".
00158                               " from forecast_item where fc_id=$2",array($array[$i]['fc_id'],$old[$i]['fc_id']));
00159         }
00160     }
00161     /**
00162      * @brief unit test
00163      */
00164     static function test_me()
00165     {}
00166 
00167 }
00168 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations