noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_acc_tva.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 this class is used for the table tva_rate
00024  */
00025 require_once('class_dossier.php');
00026 require_once('class_database.php');
00027 
00028 /*!\brief Acc_Tva is used for to map the table tva_rate
00029  * parameter are
00030 - private static $cn;   database connection
00031 - private static $variable=array("id"=>"tva_id",
00032                  "label"=>"tva_label",
00033                  "rate"=>"tva_rate",
00034                  "comment"=>"tva_comment",
00035                  "account"=>"tva_poste");
00036 
00037 */
00038 class Acc_Tva
00039 {
00040     private  $cn;               /*!< $cn database connection */
00041     private static $variable=array("id"=>"tva_id",
00042                                    "label"=>"tva_label",
00043                                    "rate"=>"tva_rate",
00044                                    "comment"=>"tva_comment",
00045                                    "account"=>"tva_poste",
00046                                     "both_side"=>'tva_both_side');
00047 
00048     function __construct ($p_init,$p_tva_id=0)
00049     {
00050         $this->cn=$p_init;
00051         $this->tva_id=$p_tva_id;
00052         $this->poste="";
00053     }
00054     public function get_parameter($p_string)
00055     {
00056         if ( array_key_exists($p_string,self::$variable) )
00057         {
00058             $idx=self::$variable[$p_string];
00059             return $this->$idx;
00060         }
00061 
00062         echo  (__FILE__.":".__LINE__.'Erreur attribut inexistant');
00063     }
00064     public function set_parameter($p_string,$p_value)
00065     {
00066         if ( array_key_exists($p_string,self::$variable) )
00067         {
00068             $idx=self::$variable[$p_string];
00069             $this->$idx=$p_value;
00070         }
00071         else
00072             exit (__FILE__.":".__LINE__.'Erreur attribut inexistant');
00073 
00074 
00075     }
00076     public function get_info()
00077     {
00078         return var_export(self::$variable,true);
00079     }
00080 
00081     public function verify()
00082     {
00083         // Verify that the elt we want to add is correct
00084     }
00085     public function save()
00086     {
00087 
00088         if (  $this->tva_id == 0 )
00089             $this->insert();
00090         else
00091             $this->update();
00092     }
00093 
00094     public function insert()
00095     {
00096         if ( $this->verify() != 0 ) return;
00097         $sql="select tva_insert($1,$2,$3,$4,$5)";
00098 
00099         $res=$this->cn->exec_sql(
00100                  $sql,
00101                  array($this->tva_label,
00102                        $this->tva_rate,
00103                        $this->tva_comment,
00104                        $this->tva_poste,
00105                         $this->tva_both_side)
00106              );
00107         $this->tva_id=$this->cn->get_current_seq('s_tva');
00108         $err=Database::fetch_result($res);
00109     }
00110 
00111     public function update()
00112     {
00113         if ( $this->verify() != 0 ) return;
00114         $sql="update tva_rate set tva_label=$1,tva_rate=$2,tva_comment=$3,tva_poste=$4,tva_both_side=$5 ".
00115              " where tva_id = $6";
00116         $res=$this->cn->exec_sql(
00117                  $sql,
00118                  array($this->tva_label,
00119                        $this->tva_rate,
00120                        $this->tva_comment,
00121                        $this->tva_poste,
00122                        $this->tva_both_side,
00123                        $this->tva_id)
00124              );
00125 
00126     }
00127     /**
00128      *Load the VAT,
00129      *@note if the label is not found then we get an message error, so the best is probably
00130      *to initialize the VAT object with default value
00131      */
00132     public function load()
00133     {
00134         $sql="select tva_id,tva_label,tva_rate, tva_comment,tva_poste,tva_both_side from tva_rate where tva_id=$1";
00135         $res=$this->cn->exec_sql(
00136                  $sql,
00137                  array($this->tva_id)
00138              );
00139 
00140         if ( $this->cn->size() == 0 ) return -1;
00141 
00142         $row=Database::fetch_array($res,0);
00143         foreach ($row as $idx=>$value)
00144         {
00145             $this->$idx=$value;
00146         }
00147         return 0;
00148     }
00149     /*!\brief get the account of the side (debit or credit)
00150      *\param $p_side is d or C
00151      *\return the account to use
00152      *\note call first load if tva_poste is empty
00153      */
00154     public function get_side($p_side)
00155     {
00156         if ( strlen($this->tva_poste) == 0 ) $this->load();
00157         list($deb,$cred)=explode(",",$this->tva_poste);
00158         switch ($p_side)
00159         {
00160         case 'd':
00161                 return $deb;
00162             break;
00163         case 'c':
00164             return $cred;
00165             break;
00166         default:
00167             throw (new Exception (__FILE__.':'.__LINE__." param est d ou c, on a recu [ $p_side ]"));
00168         }
00169     }
00170     public function delete()
00171     {
00172         $sql="delete from tva_rate where tva_id=$1";
00173         $res=$this->cn->exec_sql($sql,array($this->tva_id));
00174     }
00175     /*!\brief
00176      * Test function
00177      */
00178     static function test_me()
00179     {
00180         $cn=new Database(dossier::id());
00181         $a=new Acc_Tva($cn);
00182         echo $a->get_info();
00183         $a->set_parameter("id",1);
00184         $a->load();
00185         $a->set_parameter("id",0);
00186         $a->set_parameter("rate","0.2222");
00187         $a->set_parameter("label","test");
00188         $a->save();
00189         $a->load();
00190         print_r($a);
00191 
00192         $a->set_parameter("comment","un cht'it test");
00193         $a->save();
00194         $a->load();
00195         print_r($a);
00196 
00197         $a->delete();
00198     }
00199 
00200 }
00201 
00202 /* test::test_me(); */
 All Data Structures Namespaces Files Functions Variables Enumerations