noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_forecast_cat.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 /**
00023 * @file
00024  * @brief  manage the table forecast_cat, this table contains the categories of forecast
00025  *  as expense, asset, sales...
00026  */
00027 /**
00028  *@brief this class is called normally from forecast, a forecast contains category
00029  * like sales, expenses, each category contains items
00030  *@see Forecast
00031  *
00032  */
00033 class Forecast_Cat
00034 {
00035     /* example private $variable=array("val1"=>1,"val2"=>"Seconde valeur","val3"=>0); */
00036     private static $variable=array ("id"=>"fc_id","order"=>"fc_order","desc"=>"fc_desc","forecast"=>"f_id");
00037     private $cn;
00038     /**
00039      * @brief constructor
00040      * @param $p_init Database object
00041      */
00042     function __construct ($p_init,$p_id=0)
00043     {
00044         $this->cn=$p_init;
00045         $this->fc_id=$p_id;
00046     }
00047     public function get_parameter($p_string)
00048     {
00049         if ( array_key_exists($p_string,self::$variable) )
00050         {
00051             $idx=self::$variable[$p_string];
00052             return $this->$idx;
00053         }
00054         else
00055             exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00056     }
00057     public function set_parameter($p_string,$p_value)
00058     {
00059         if ( array_key_exists($p_string,self::$variable) )
00060         {
00061             $idx=self::$variable[$p_string];
00062             $this->$idx=$p_value;
00063         }
00064         else
00065             exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00066 
00067 
00068     }
00069     public function get_info()
00070     {
00071         return var_export(self::$variable,true);
00072     }
00073     public function verify()
00074     {
00075         if (strlen(trim($this->fc_order))==0)
00076         {
00077             $this->fc_order="1";
00078         }
00079         // Verify that the elt we want to add is correct
00080         // the f_name must be unique (case insensitive)
00081         return 0;
00082     }
00083     public function save()
00084     {
00085         if (  $this->get_parameter("id") == 0 )
00086             $this->insert();
00087         else
00088             $this->update();
00089     }
00090 
00091     public function insert()
00092     {
00093         if ( $this->verify() != 0 ) return;
00094 
00095         $sql="insert into forecast_cat (fc_desc,fc_order,f_id) ".
00096              " values ($1,$2,$3)  returning fc_id";
00097         $res=$this->cn->exec_sql(
00098                  $sql,
00099                  array($this->fc_desc,$this->fc_order,$this->f_id)
00100              );
00101         $this->fc_id=Database::fetch_result($res,0,0);
00102     }
00103 
00104     public function update()
00105     {
00106         if ( $this->verify() != 0 ) return;
00107 
00108         $sql="update forecast_cat set fc_desc=$1,f_id=$2,fc_order=$3 ".
00109              " where fc_id = $4";
00110         $res=$this->cn->exec_sql(
00111                  $sql,
00112                  array($this->fc_desc,$this->f_id, $this->fc_order,$this->fc_id)
00113              );
00114     }
00115     /**
00116      *@brief Load all the cat. for a given forecast and return them into a array
00117      *@param $p_cn database connx
00118      *@param $p_id is the forecast id (f_id)
00119      *@return an array with all the data
00120      */
00121     public static function load_all($p_cn,$p_id)
00122     {
00123         $sql="select fc_id,fc_desc,fc_order from forecast_cat where f_id=$1";
00124 
00125         $res=$p_cn->get_array($sql,array($p_id));
00126 
00127         return $res;
00128     }
00129     public function load()
00130     {
00131 
00132         $sql="select fc_desc, f_id,fc_order from forecast_cat where fc_id=$1";
00133 
00134         $res=$this->cn->exec_sql(
00135                  $sql,
00136                  array($this->fc_id)
00137              );
00138 
00139         if ( Database::num_row($res) == 0 ) return;
00140         $row=Database::fetch_array($res,0);
00141         foreach ($row as $idx=>$value)
00142         {
00143             $this->$idx=$value;
00144         }
00145 
00146     }
00147     /**
00148      *@brief Make a array for a ISelect of the available cat
00149      *@param $id is forecast::f_id
00150      *@return array for ISelect
00151      *@see ISelect
00152      */
00153     public function make_array($id)
00154     {
00155         $sql="select fc_id,fc_desc from forecast_cat where f_id=$id";
00156         $ret=$this->cn->make_array($sql);
00157         return $ret;
00158     }
00159     public function delete()
00160     {
00161         $sql="delete from forecast_cat where fc_id=$1";
00162         $res=$this->cn->exec_sql($sql,array($this->fc_id));
00163     }
00164     /**
00165      * @brief unit test
00166      */
00167     static function test_me()
00168     {}
00169 
00170 }
00171 
00172 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations