00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 require_once("class_ihidden.php");
00030 require_once("class_itext.php");
00031 require_once("class_iselect.php");
00032 require_once("class_ispan.php");
00033 require_once('class_database.php');
00034 require_once("class_anc_plan.php");
00035
00036 class Anc_Account
00037 {
00038 var $id;
00039 var $name;
00040 var $pa_id;
00041 var $amount;
00042 var $description;
00043 var $db;
00044 var $ga_id;
00045 function Anc_Account($p_db,$p_id=0)
00046 {
00047 $this->db=$p_db;
00048 $this->id=$p_id;
00049 $this->ga_id=null;
00050 }
00051
00052
00053
00054
00055 private function fetch_from_db($p_where)
00056 {
00057 $sql="select po_id,
00058 po_name ,
00059 pa_id,
00060 po_amount,
00061 po_description,
00062 ga_id
00063 from poste_analytique
00064 where ".
00065 $p_where;
00066
00067 $ret=$this->db->exec_sql($sql);
00068 if ( Database::num_row($ret) == 0 )return null;
00069 $line=Database::fetch_array($ret);
00070
00071 $this->id=$line['po_id'];
00072 $this->name=$line['po_name'];
00073 $this->pa_id=$line['pa_id'];
00074 $this->amount=$line['po_amount'];
00075 $this->description=$line['po_description'];
00076 $this->ga_id=$line['ga_id'];
00077
00078
00079 }
00080 function get_by_id()
00081 {
00082 $this->fetch_from_db("po_id=".$this->id);
00083 }
00084
00085
00086
00087
00088
00089 function get_by_name($p_name)
00090 {
00091 $p_name=sql_string($p_name);
00092 if ( $p_name == null )
00093 $p_name=$this->name;
00094
00095 $this->fetch_from_db("po_name='".$p_name."'");
00096 echo "id = ".$this->id;
00097 }
00098 function add()
00099 {
00100 $this->format_data();
00101 if ( strlen($this->name) == 0)
00102 return;
00103 if ( $this->ga_id == null || strlen(trim($this->ga_id)) == 0 )
00104 $ga_id="NULL";
00105 else
00106 $ga_id="'".$this->ga_id."'";
00107 $sql="insert into poste_analytique (
00108 po_name ,
00109 pa_id,
00110 po_amount,
00111 po_description,
00112 ga_id
00113 ) values ($1,$2,$3,$4,$5)";
00114
00115 try
00116 {
00117 $this->db->exec_sql($sql,array($this->name,$this->pa_id,$this->amount,$this->description,$ga_id));
00118
00119 }
00120 catch (Exception $e)
00121 {
00122 if ( DEBUG ) print_r($e);
00123 echo "<p class=\"notice\">Doublon : l'enregistrement n'est pas sauve</p>";
00124 }
00125
00126 }
00127 static function make_array_name($cn,$pa_id)
00128 {
00129 $a=$cn->make_array("select po_name,po_name from poste_analytique ".
00130 " where ".
00131 " pa_id =".$pa_id." order by po_name ");
00132 return $a;
00133 }
00134 function update()
00135 {
00136 $this->format_data();
00137 if ( strlen($this->name) == 0)
00138 return;
00139 $sql="update poste_analytique ".
00140 " set po_name=$1".
00141 " ,pa_id=$2".
00142 " ,po_amount=$3".
00143 " ,po_description=$4".
00144 " ,ga_id=$5".
00145 " where po_id=$6";
00146 try
00147 {
00148 $this->db->exec_sql($sql,array($this->name,$this->pa_id,$this->amount,
00149 $this->description,$this->ga_id,$this->id));
00150 }
00151 catch (Exception $e)
00152 {
00153 echo "<p class=\"notice\">Doublon : l'enregistrement n'est pas sauve</p>";
00154 }
00155
00156 }
00157 private function format_data()
00158 {
00159
00160 $this->name=$this->name;
00161 $this->pa_id=$this->pa_id;
00162 $this->amount=$this->amount;
00163 if (strlen($this->amount) == 0 )
00164 $this->amount=0.0;
00165 if ( isNumber($this->amount) ==0 )
00166 $this->amount=0;
00167
00168 $this->description=$this->description;
00169 }
00170 function delete()
00171 {
00172 $sql="delete from poste_analytique where po_id=".$this->id;
00173 $this->db->exec_sql($sql);
00174 }
00175
00176
00177
00178 function get_list()
00179 {
00180 $sql="select po_id,
00181 po_name ,
00182 pa_id,
00183 po_amount,
00184 po_description,
00185 ga_id
00186 from poste_analytique ".
00187 " order by po_name";
00188
00189 $ex=$this->db->exec_sql($sql);
00190 $ret=Database::fetch_all($ex);
00191 if ( $ret == null )
00192 return null;
00193
00194 $array=array();
00195 foreach ($ret as $line)
00196 {
00197 $object=new Anc_Account($this->db);
00198
00199 $object->id=$line['po_id'];
00200 $object->name=$line['po_name'];
00201 $object->pa_id=$line['pa_id'];
00202 $object->amount=$line['po_amount'];
00203 $object->description=$line['po_description'];
00204 $object->ga_id=$line['ga_id'];
00205 $array[]=clone $object;
00206 }
00207
00208 return $array;
00209 }
00210 function display_list()
00211 {
00212 $array=$this->get_list();
00213 if ( empty($array) )
00214 {
00215 echo "Vide";
00216 return;
00217 }
00218 foreach ($array as $line)
00219 {
00220 echo $line->id." / ".$line->name." / ".$line->description."/".
00221 $line->amount." / ".$line->pa_id."/".$line->ga_id."<br>";
00222 }
00223 }
00224 function debug()
00225 {
00226 echo "id ".$this->id."<br>";
00227 echo "name ".$this->name."<br>";
00228 echo "pa_id ".$this->pa_id."<br>";
00229 echo "amount ".$this->amount."<br>";
00230 echo "description ".$this->description."<br>";
00231 echo "ga_id ".$this->ga_id."<br>";
00232 }
00233 function form()
00234 {
00235 $r='';
00236 $wName=new IText("po_name",$this->name);
00237 $wAmount=new INum("po_amount",$this->amount);
00238 $wDescription=new IText("po_description",$this->description);
00239 $aGroup_analytic=$this->db->make_array("select ga_id,ga_id from groupe_analytique where pa_id=".$this->pa_id,1);
00240 if ( count($aGroup_analytic) > 1 )
00241 {
00242 $wGa_id=new ISelect("ga_id");
00243 $wGa_id->value=$aGroup_analytic;
00244 $wGa_id->selected=$this->ga_id;
00245 $wGa_id->table=1;
00246 }
00247 else
00248 {
00249 $wGa_id=new ISpan();
00250 }
00251 $pa=new Anc_Plan($this->db,$this->pa_id);
00252 $pa->get();
00253 $wPaName=new IText("",$pa->name);
00254 $wPaName->table=1;
00255 $wPaName->readOnly=true;
00256
00257 $wName->table=1;
00258 $wAmount->table=1;
00259 $wDescription->table=1;
00260 $r.=HtmlInput::hidden("pa_id",$this->pa_id);
00261 $r.=HtmlInput::hidden("po_id",$this->id);
00262
00263 $r.="<table>";
00264
00265 $r.="<tr>";
00266 $r.=td(_('Nom'));
00267 $r.=$wName->input();
00268 $r.="</tr>";
00269
00270 $r.="<tr>";
00271 $r.=td(_('Montant'));
00272 $r.=$wAmount->input();
00273 $r.="</tr>";
00274
00275
00276 $r.="<tr>";
00277 $r.=td(_('Description'));
00278 $r.=$wDescription->input();
00279 $r.="</tr>";
00280
00281 $r.="<tr>";
00282 $r.=td(_('Plan Analytique'));
00283 $r.=$wPaName->input();
00284 $r.="</tr>";
00285
00286 $r.="<tr>";
00287 $r.=td(_('Groupe'));
00288 $r.=$wGa_id->input();
00289 $r.="</tr>";
00290
00291 $r.="</table>";
00292 return $r;
00293
00294 }
00295 function get_from_array($p_array)
00296 {
00297 $this->name=(isset ($p_array['po_name']))?$p_array['po_name']:"";
00298 $this->description=(isset ($p_array['po_description']))?$p_array['po_description']:"";
00299 $this->pa_id=(isset ($p_array['pa_id']))?$p_array['pa_id']:"";
00300 $this->amount=(isset ($p_array['po_amount']))?$p_array['po_amount']:0;
00301 $this->id=(isset ($p_array['po_id']))?$p_array['po_id']:-1;
00302
00303 $this->ga_id=(isset($p_array['ga_id']) && $p_array['ga_id'] != "-1" )?$p_array['ga_id']:null;
00304 }
00305 static function test_me()
00306 {
00307 $cn=new Database(dossier::id());
00308 $pa_id=$cn->get_value("select max(pa_id) from plan_analytique");
00309 $o=new Anc_Account($cn);
00310 echo "<h1>Poste_Analytique</h1>";
00311 echo "<h2>get_list</h2>";
00312 $ee=$o->get_list();
00313 print_r($ee);
00314
00315
00316 echo "<h2>Add some </h2>";
00317 $o->pa_id=$pa_id;
00318 $o->name="test1";
00319 $o->add();
00320
00321
00322 $o->name="test2";
00323 $o->add();
00324
00325 $o->name="test3";
00326 $o->add();
00327
00328 $o->name="test4";
00329 $o->add();
00330
00331 $o->name="test5";
00332 $o->add();
00333
00334 echo "<h2> remove test1</h2>";
00335 $o->get_by_name("test1");
00336 $o->delete();
00337 $o->display_list();
00338
00339 $o->get_by_name("test4");
00340 echo "<hr>".$o->id."<hr>";
00341 $o->name="Test Four";
00342 $o->update();
00343 $o->display_list();
00344 $o->delete();
00345 $o->display_list();
00346 }
00347 }
00348 ?>