ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentPrices.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
32 {
33  var $ilDB;
34 
36 
37  var $price;
38  var $currency;
39  var $duration;
41 
42 
43  private $prices = array();
44 
45  function ilPaymentPrices($a_pobject_id = 0)
46  {
47  global $ilDB;
48 
49  $this->db =& $ilDB;
50 
51  $this->pobject_id = $a_pobject_id;
52 
53  $this->__read();
54  }
55 
56  // SET GET
57  function getPobjectId()
58  {
59  return $this->pobject_id;
60  }
61 
62  function getPrices()
63  {
64  return $this->prices ? $this->prices : array();
65  }
66  function getPrice($a_price_id)
67  {
68  return $this->prices[$a_price_id] ? $this->prices[$a_price_id] : array();
69  }
70 
71 
72  // STATIC
73  function _getPrice($a_price_id)
74  {
75  global $ilDB, $ilSettings;
76 
77  $res = $ilDB->queryf('
78  SELECT * FROM payment_prices
79  WHERE price_id = %s',
80  array('integer'), array($a_price_id));
81 
82  while($row = $ilDB->fetchObject($res))
83  {
84  $price['duration'] = $row->duration;
85  $price['unlimited_duration'] = $row->unlimited_duration;
86  $price['currency'] = $row->currency;
87  $price['price'] = $row->price;
88 
89  }
90  return count($price) ? $price : array();
91  }
92 
93  function _countPrices($a_pobject_id)
94  {
95  global $ilDB;
96 
97  $res = $ilDB->queryf('
98  SELECT count(price_id) FROM payment_prices
99  WHERE pobject_id = %s',
100  array('integer'),
101  array($a_pobject_id));
102 
103  $row = $res->fetchRow(DB_FETCHMODE_ARRAY);
104 
105  return ($row[0]);
106  }
107 
108  function _getPriceString($a_price_id)
109  {
110  include_once './payment/classes/class.ilPaymentCurrency.php';
111 
112  global $lng;
113 
114  $price = ilPaymentPrices::_getPrice($a_price_id);
115 
116 
117  return self::_formatPriceToString($price['price']);
118 
119  }
120 
121 
122 // public static function _formatPriceToString($unit_value, $subunit_value)
123  public static function _formatPriceToString($a_price)
124  {
125  include_once './payment/classes/class.ilGeneralSettings.php';
126 
127  $genSet = new ilGeneralSettings();
128  $currency_unit = $genSet->get('currency_unit');
129 
130  return $a_price . ' ' . $currency_unit;
131  }
132 
133 
134  function _getPriceStringFromAmount($a_price)
135  {
136  include_once './payment/classes/class.ilPaymentCurrency.php';
137  include_once './payment/classes/class.ilGeneralSettings.php';
138 
139  global $lng;
140 
141  $genSet = new ilGeneralSettings();
142  $currency_unit = $genSet->get("currency_unit");
143 
144  $pr_str = '';
145 
146  $pr_str = number_format($a_price , 2, ",", ".");
147  return $pr_str . " " . $currency_unit;
148  }
149 
150 
151  function _getTotalAmount($a_price_ids)
152  {
153 
154  include_once './payment/classes/class.ilPaymentPrices.php';
155 # include_once './payment/classes/class.ilPaymentCurrency.php';
156  include_once './payment/classes/class.ilGeneralSettings.php';
157 
158  global $ilDB,$lng;
159 
160  $genSet = new ilGeneralSettings();
161  $currency_unit = $genSet->get("currency_unit");
162 
163  $amount = array();
164 
165  if (is_array($a_price_ids))
166  {
167  for ($i = 0; $i < count($a_price_ids); $i++)
168  {
169  $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
170 
171  $price = (float) $price_data["price"];
172  $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
173  }
174  }
175 
176  return $amount;
177  }
178 
179 
180 /* function setUnitValue($a_value = 0)
181  {
182  // substitute leading zeros with ''
183  $this->unit_value = preg_replace('/^0+/','',$a_value);
184  }
185  function setSubUnitValue($a_value = 0)
186  {
187  $this->sub_unit_value = $a_value;
188  }
189 */
190  function setPrice($a_price = 0)
191  {
192  $this->price = preg_replace('/^0+/','',$a_price);
193 
194  $this->price = $a_price;
195  }
196 
197  function setCurrency($a_currency_id)
198  {
199  $this->currency = $a_currency_id;
200  }
201  function setDuration($a_duration)
202  {
203  if($this->unlimited_duration == '1' && ($a_duration == '' || null))
204  $a_duration = 0;
205 
206  $this->duration = (int)$a_duration;
207  }
208 
209  function setUnlimitedDuration($a_unlimited_duration)
210  {
211  if($a_unlimited_duration)
212  $this->unlimited_duration = (int)$a_unlimited_duration;
213  else
214  $this->unlimited_duration = 0;
215  }
216 
217  function add()
218  {
219  $next_id = $this->db->nextId('payment_prices');
220 
221  $res = $this->db->manipulateF('
222  INSERT INTO payment_prices
223  ( price_id,
224  pobject_id,
225  currency,
226  duration,
227  unlimited_duration,
228  price
229  )
230  VALUES (%s, %s, %s, %s, %s, %s)',
231 
232  array('integer','integer', 'integer', 'integer', 'integer', 'float'),
233  array( $next_id,
234  $this->getPobjectId(),
235  $this->__getCurrency(),
236  $this->__getDuration(),
237  $this->__getUnlimitedDuration(),
238  $this->__getPrice()
239  ));
240 
241  $this->__read();
242 
243  return true;
244  }
245  function update($a_price_id)
246  {
247  $res = $this->db->manipulateF('
248  UPDATE payment_prices SET
249  currency = %s,
250  duration = %s,
251  unlimited_duration = %s,
252  price = %s
253  WHERE price_id = %s',
254 
255  array('integer', 'integer','integer', 'float', 'integer'),
256  array( $this->__getCurrency(),
257  $this->__getDuration(),
258  $this->__getUnlimitedDuration(),
259  $this->__getPrice(),
260  $a_price_id
261  ));
262 
263  $this->__read();
264 
265  return true;
266  }
267  function delete($a_price_id)
268  {
269  $statement = $this->db->manipulateF('
270  DELETE FROM payment_prices
271  WHERE price_id = %s',
272  array('integer'), array($a_price_id));
273 
274  $this->__read();
275 
276  return true;
277  }
278  function deleteAllPrices()
279  {
280  $statement = $this->db->manipulateF('
281  DELETE FROM payment_prices
282  WHERE pobject_id = %s',
283  array('integer'),
284  array($this->getPobjectId()));
285 
286  $this->__read();
287 
288  return true;
289  }
290 
291  function validate()
292  {
293 
294  $duration_valid = false;
295  $price_valid = false;
296 
297  if(preg_match('/^(([1-9][0-9]{0,1})|[0])?$/',$this->__getDuration())
298  || ((int)$this->__getDuration() == 0 && $this->__getUnlimitedDuration() == 1))
299  {
300  $duration_valid = true;
301  }
302 
303  if(preg_match('/[0-9]/',$this->__getPrice()))
304  {
305 
306  $price_valid = true;
307  }
308 
309  if($duration_valid == true && $price_valid == true)
310  {
311  return true;
312 
313  }
314 
315  else return false;
316 
317  }
318  // STATIC
319  function _priceExists($a_price_id,$a_pobject_id)
320  {
321  global $ilDB;
322 
323  $res = $ilDB->queryf('
324  SELECT * FROM payment_prices
325  WHERE price_id = %s
326  AND pobject_id = %s',
327  array('integer', 'integer'),
328  array($a_price_id, $a_pobject_id));
329 
330  return $res->numRows() ? true : false;
331  }
332 
333  // PRIVATE
334 
335  function __getPrice()
336  {
337  return $this->price;
338  }
339  function __getCurrency()
340  {
341  return $this->currency;
342  }
343  function __getDuration()
344  {
345  return $this->duration;
346  }
348  {
350  }
351 
352  function __read()
353  {
354  $this->prices = array();
355 
356  $res = $this->db->queryf('
357  SELECT * FROM payment_prices
358  WHERE pobject_id = %s
359  ORDER BY duration',
360  array('integer'),
361  array($this->getPobjectId()));
362 
363 
364  while($row = $this->db->fetchObject($res))
365  {
366  $this->prices[$row->price_id]['pobject_id'] = $row->pobject_id;
367  $this->prices[$row->price_id]['price_id'] = $row->price_id;
368  $this->prices[$row->price_id]['currency'] = $row->currency;
369  $this->prices[$row->price_id]['duration'] = $row->duration;
370  $this->prices[$row->price_id]['unlimited_duration'] = $row->unlimited_duration;
371  $this->prices[$row->price_id]['price'] = $row->price;
372  }
373  }
374 
375  public function getNumberOfPrices()
376  {
377  return count($this->prices);
378  }
379 
380  public function getLowestPrice()
381  {
382  $lowest_price_id = 0;
383  $lowest_price = 0;
384 
385  foreach ($this->prices as $price_id => $data)
386  {
387  $current_price = $data['price'];
388 
389  if($lowest_price == 0||
390  $lowest_price > (float)$current_price)
391  {
392  $lowest_price = (float)$current_price;
393  $lowest_price_id = $price_id;
394  }
395  }
396 
397  return is_array($this->prices[$lowest_price_id]) ? $this->prices[$lowest_price_id] : array();
398  }
399 }
400 ?>