ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilShopVats.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Payment/exceptions/class.ilShopException.php';
5 include_once 'Services/Payment/classes/class.ilShopUtils.php';
6 
17 {
18  private $id = 0;
19  private $title = '';
20  private $rate = 0;
21 
29  public function __construct($a_vat_id = 0)
30  {
31  global $ilDB, $lng;
32 
33  $this->db = $ilDB;
34  $this->lng = $lng;
35 
36  if((int)$a_vat_id && $a_vat_id > 0)
37  {
38  $this->id = $a_vat_id;
39  $this->read();
40  }
41  }
42 
49  private function read()
50  {
51  if((int)$this->id)
52  {
53 
54  $res = $this->db->queryf('SELECT * FROM payment_vats
55  WHERE vat_id = %s',
56  array('integer'), array($this->id) );
57 
58  while($row = $this->db->fetchObject($res))
59  {
60  $this->setTitle($row->vat_title);
61  $this->setRate($row->vat_rate);
62 
63  return true;
64  }
65 
66  throw new ilShopException($this->lng->txt('payment_cannot_find_vat'));
67  }
68 
69  throw new ilShopException($this->lng->txt('payment_cannot_read_nonexisting_vat'));
70  }
71 
80  public function reloadFromDatabase()
81  {
82  $this->read();
83 
84  return $this;
85  }
86 
94  public function update()
95  {
96  if((int)$this->id)
97  {
98  if(ilShopVatsList::_isVATAlreadyCreated($this->rate, $this->id))
99  {
100  throw new ilShopException($this->lng->txt('payment_vat_already_created'));
101  }
102 
103 
104  $this->db->manipulatef('
105  UPDATE payment_vats
106  SET vat_title = %s,
107  vat_rate = %s
108  WHERE vat_id = %s',
109  array('text', 'float', 'integer'),
110  array($this->getTitle(),$this->getRate(),$this->getId())
111  );
112 
113  return true;
114  }
115 
116  throw new ilShopException($this->lng->txt('payment_cannot_update_nonexisting_vat'));
117  }
118 
126  public function save()
127  {
128  if(!(int)$this->id)
129  {
130  if(ilShopVatsList::_isVATAlreadyCreated($this->rate))
131  {
132  throw new ilShopException($this->lng->txt('payment_vat_already_created'));
133  }
134 
135  $next_id = $this->db->nextId('payment_vats');
136 
137  $this->db->manipulateF('
138  INSERT INTO payment_vats
139  (vat_id, vat_title, vat_rate)
140  VALUES (%s,%s,%s)',
141  array('integer', 'text', 'float'),
142  array($next_id, $this->getTitle(), $this->getRate())
143  );
144  return true;
145  }
146 
147  throw new ilShopException($this->lng->txt('payment_cannot_save_existing_vat'));
148  }
149 
157  public function delete()
158  {
159  if((int)$this->id)
160  {
161  $result = $this->db->queryF('
162  SELECT * FROM payment_objects
163  WHERE vat_id = %s',
164  array('integer'),
165  array($this->getId())
166  );
167 
168  while($row = $this->db->fetchObject($result))
169  {
170  throw new ilShopException(sprintf($this->lng->txt('paya_vat_not_deleted'), $this->title));
171  }
172 
173  $this->db->manipulateF('
174  DELETE FROM payment_vats
175  WHERE vat_id = %s',
176  array('integer'),
177  array($this->getId())
178 
179  );
180  return true;
181  }
182 
183  throw new ilShopException($this->lng->txt('payment_cannot_delete_nonexisting_vat'));
184  }
185 
193  public function setTitle($a_title)
194  {
195  $this->title = $a_title;
196 
197  return $this;
198  }
199  public function getTitle()
200  {
201  return $this->title;
202  }
210  public function setId($a_id)
211  {
212  $this->id = $a_id;
213 
214  return $this;
215  }
216  public function getId()
217  {
218  return $this->id;
219  }
227  public function setRate($a_rate)
228  {
229  $this->rate = $a_rate;
230 
231  return $this;
232  }
233  public function getRate()
234  {
235  return $this->rate;
236  }
237 
238  public static function _readAllVats()
239  {
240  global $ilDB;
241 
242  $vats = array();
243  $res = $ilDB->query('SELECT * FROM payment_vats');
244  while($row = $ilDB->fetchAssoc($res))
245  {
246  $vats[] = $row;
247  }
248  return $vats;
249  }
250 
251 }
252 
253 ?>