Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_amortissement_detail_sql.php
Go to the documentation of this file.
1 <?php
2 /*
3  * This file is part of NOALYSS.
4  *
5  * NOALYSS is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * NOALYSS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with NOALYSS; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19  /**
20  *@file
21  *@brief Manage the table amortissement.amortissement_detail
22  *
23  *
24  Example
25  @code
26 
27  @endcode
28  */
29 require_once NOALYSS_INCLUDE.'/lib/class_database.php';
30 require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
31 
32 
33 /**
34  *@brief Manage the table amortissement.amortissement_detail
35  */
37 {
38  /* example private $variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0); */
39 
40  protected $variable=array("ad_id"=>"ad_id","ad_percentage"=>"ad_percentage"
41  ,"ad_year"=>"ad_year"
42  ,"ad_amount"=>"ad_amount"
43  ,"a_id"=>"a_id"
44  );
45  function __construct ( & $p_cn,$p_id=-1)
46  {
47  $this->cn=$p_cn;
48  $this->ad_id=$p_id;
49 
50  if ( $p_id == -1 )
51  {
52  /* Initialize an empty object */
53  foreach ($this->variable as $key=>$value) $this->$value='';
54  $this->ad_id=$p_id;
55  }
56  else
57  {
58  /* load it */
59 
60  $this->load();
61  }
62  }
63  public function get_parameter($p_string)
64  {
65  if ( array_key_exists($p_string,$this->variable) )
66  {
67  $idx=$this->variable[$p_string];
68  return $this->$idx;
69  }
70  else
71  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
72  }
73  public function set_parameter($p_string,$p_value)
74  {
75  if ( array_key_exists($p_string,$this->variable) )
76  {
77  $idx=$this->variable[$p_string];
78  $this->$idx=$p_value;
79  }
80  else
81  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
82  }
83  public function get_info()
84  {
85  return var_export($this,true);
86  }
87  public function verify()
88  {
89  // Verify that the elt we want to add is correct
90  /* verify only the datatype */
91  if ( settype($this->ad_percentage,'float') == false )
92  throw new Exception('DATATYPE ad_percentage $this->ad_percentage non numerique');
93  if ( settype($this->ad_year,'float') == false )
94  throw new Exception('DATATYPE ad_year $this->ad_year non numerique');
95  if ( settype($this->ad_amount,'float') == false )
96  throw new Exception('DATATYPE ad_amount $this->ad_amount non numerique');
97  if ( settype($this->a_id,'float') == false )
98  throw new Exception('DATATYPE a_id $this->a_id non numerique');
99 
100 
101  }
102  public function save()
103  {
104  /* please adapt */
105  if ( $this->ad_id == -1 )
106  $this->insert();
107  else
108  $this->update();
109  }
110  /**
111  *@brief retrieve array of object thanks a condition
112  *@param $cond condition (where clause) (optional by default all the rows are fetched)
113  * you can use this parameter for the order or subselect
114  *@param $p_array array for the SQL stmt
115  *@see Database::get_array
116  *@return an empty array if nothing is found
117  */
118  public function seek($cond='',$p_array=null)
119  {
120  $sql="select * from amortissement.amortissement_detail $cond";
121  $aobj=array();
122  $array= $this->cn->get_array($sql,$p_array);
123  // map each row in a object
124  $size=$this->cn->count();
125  if ( $size == 0 ) return $aobj;
126  for ($i=0; $i<$size; $i++)
127  {
128  $oobj=new Amortissement_Detail_Sql ($this->cn);
129  foreach ($array[$i] as $idx=>$value)
130  {
131  $oobj->$idx=$value;
132  }
133  $aobj[]=clone $oobj;
134  }
135  return $aobj;
136  }
137  public function insert()
138  {
139  if ( $this->verify() != 0 ) return;
140  if ( $this->ad_id==-1 )
141  {
142  /* please adapt */
143  $sql="insert into amortissement.amortissement_detail(ad_percentage
144  ,ad_year
145  ,ad_amount
146  ,a_id
147  ) values ($1
148  ,$2
149  ,$3
150  ,$4
151  ) returning ad_id";
152 
153  $this->ad_id=$this->cn->get_value(
154  $sql,
155  array( $this->ad_percentage
156  ,$this->ad_year
157  ,$this->ad_amount
158  ,$this->a_id
159  )
160  );
161  }
162  else
163  {
164  $sql="insert into amortissement.amortissement_detail(ad_percentage
165  ,ad_year
166  ,ad_amount
167  ,a_id
168  ,ad_id) values ($1
169  ,$2
170  ,$3
171  ,$4
172  ,$5
173  ) returning ad_id";
174 
175  $this->ad_id=$this->cn->get_value(
176  $sql,
177  array( $this->ad_percentage
178  ,$this->ad_year
179  ,$this->ad_amount
180  ,$this->a_id
181  ,$this->ad_id)
182  );
183 
184  }
185 
186  }
187 
188  public function update()
189  {
190  if ( $this->verify() != 0 ) return;
191  /* please adapt */
192  $sql=" update amortissement.amortissement_detail set ad_percentage = $1
193  ,ad_year = $2
194  ,ad_amount = $3
195  ,a_id = $4
196  where ad_id= $5";
197  $res=$this->cn->exec_sql(
198  $sql,
199  array($this->ad_percentage
200  ,$this->ad_year
201  ,$this->ad_amount
202  ,$this->a_id
203  ,$this->ad_id)
204  );
205 
206  }
207  /**
208  *@brief load a object
209  *@return 0 on success -1 the object is not found
210  */
211  public function load()
212  {
213 
214  $sql="select ad_percentage
215  ,ad_year
216  ,ad_amount
217  ,a_id
218  from amortissement.amortissement_detail where ad_id=$1";
219  /* please adapt */
220  $res=$this->cn->get_array(
221  $sql,
222  array($this->ad_id)
223  );
224 
225  if ( count($res) == 0 )
226  {
227  /* Initialize an empty object */
228  foreach ($this->variable as $key=>$value) $this->$key='';
229 
230  return -1;
231  }
232  foreach ($res[0] as $idx=>$value)
233  {
234  $this->$idx=$value;
235  }
236  return 0;
237  }
238 
239  public function delete()
240  {
241  $sql="delete from amortissement.amortissement_detail where ad_id=$1";
242  $res=$this->cn->exec_sql($sql,array($this->ad_id));
243  }
244  /**
245  * Unit test for the class
246  */
247  static function test_me()
248  {
249 
250  }
251 
252 }
253 // Amortissement_Detail_Sql::test_me();
254 ?>
Manage the table amortissement.amortissement_detail.
if(isset($_POST['remove'])) $array
$key
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
$res
$sql
if($p_number->value==0) l les annuités seront recalculées et l pct[] ad_year[] select h_amount from amortissement amortissement_histo where a_id
static test_me()
Unit test for the class.