class_forecast_cat.php

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