ILIAS  Release_3_10_x_branch Revision 61812
 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 
38  var $currency;
39  var $duration;
40 
41  private $prices = array();
42 
43  function ilPaymentPrices($a_pobject_id = 0)
44  {
45  global $ilDB;
46 
47  $this->db =& $ilDB;
48 
49  $this->pobject_id = $a_pobject_id;
50 
51  $this->__read();
52  }
53 
54  // SET GET
55  function getPobjectId()
56  {
57  return $this->pobject_id;
58  }
59 
60  function getPrices()
61  {
62  return $this->prices ? $this->prices : array();
63  }
64  function getPrice($a_price_id)
65  {
66  return $this->prices[$a_price_id] ? $this->prices[$a_price_id] : array();
67  }
68 
69  // STATIC
70  function _getPrice($a_price_id)
71  {
72  global $ilDB;
73 
74  $query = "SELECT * FROM payment_prices ".
75  "WHERE price_id = '".$a_price_id."'";
76 
77  $res = $ilDB->query($query);
78 
79  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
80  {
81  $price['duration'] = $row->duration;
82  $price['currency'] = $row->currency;
83  $price['unit_value'] = $row->unit_value;
84  $price['sub_unit_value'] = $row->sub_unit_value;
85  }
86  return count($price) ? $price : array();
87  }
88 
89  function _countPrices($a_pobject_id)
90  {
91  $query = "SELECT count(price_id) FROM payment_prices ".
92  "WHERE pobject_id = '".$a_pobject_id."'";
93 
94  $res = $this->db->query($query);
95  $row = $res->fetchRow(DB_FETCHMODE_ARRAY);
96 
97  return ($row[0]);
98  }
99 
100  function _getPriceString($a_price_id)
101  {
102  include_once './payment/classes/class.ilPaymentCurrency.php';
103 
104  global $lng;
105 
106  $price = ilPaymentPrices::_getPrice($a_price_id);
107 
108  return self::_formatPriceToString($price['unit_value'], $price['sub_unit_value']);
109  }
110 
111  public static function _formatPriceToString($unit_value, $subunit_value)
112  {
113  include_once './payment/classes/class.ilGeneralSettings.php';
114 
115  $genSet = new ilGeneralSettings();
116  $unit_string = $genSet->get('currency_unit');
117 
118  $pr_str = number_format( ((int) $unit_value) . '.' . sprintf('%02d', ((int) $subunit_value)), 2, ',', '.');
119  return $pr_str . ' ' . $unit_string;
120  }
121 
122  public static function _formatPriceToFloat($unit_value, $subunit_value)
123  {
124  return (float) number_format(((int) $unit_value).'.'.sprintf('%02d', ((int) $subunit_value)), 2, '.', '');
125  }
126 
127  function _getPriceStringFromAmount($a_price)
128  {
129  include_once './payment/classes/class.ilPaymentCurrency.php';
130  include_once './payment/classes/class.ilGeneralSettings.php';
131 
132  global $lng;
133 
134  $genSet = new ilGeneralSettings();
135  $unit_string = $genSet->get("currency_unit");
136 
137  $pr_str = '';
138 
139  $pr_str = number_format($a_price , 2, ",", ".");
140  return $pr_str . " " . $unit_string;
141  }
142 
143  function _getPriceFromArray($a_price)
144  {
145  return (float) (((int) $a_price["unit_value"]) . "." . sprintf("%02d", ((int) $a_price["sub_unit_value"])));
146  }
147 
148  function _getTotalAmount($a_price_ids)
149  {
150  include_once './payment/classes/class.ilPaymentPrices.php';
151 # include_once './payment/classes/class.ilPaymentCurrency.php';
152  include_once './payment/classes/class.ilGeneralSettings.php';
153 
154  global $ilDB,$lng;
155 
156  $genSet = new ilGeneralSettings();
157  $unit_string = $genSet->get("currency_unit");
158 
159  $amount = array();
160 
161  if (is_array($a_price_ids))
162  {
163  for ($i = 0; $i < count($a_price_ids); $i++)
164  {
165  $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
166 
167  $price = ((int) $price_data["unit_value"]) . "." . sprintf("%02d", ((int) $price_data["sub_unit_value"]));
168  $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
169  }
170  }
171 
172  return $amount;
173 
174 /* foreach($a_price_ids as $id)
175  {
176  $price_data = ilPaymentPrices::_getPrice($id);
177 
178  $price_arr["$price_data[currency]"]['unit'] += (int) $price_data['unit_value'];
179  $price_arr["$price_data[currency]"]['subunit'] += (int) $price_data['sub_unit_value'];
180  }
181 
182  if(is_array($price_arr))
183  {
184  foreach($price_arr as $key => $value)
185  {
186  // CHECK cent bigger 100
187  $value['unit'] += (int) ($value['subunit'] / 100);
188  $value['subunit'] = (int) ($value['subunit'] % 100);
189 
190  $unit_string = $lng->txt('currency_'.ilPaymentCurrency::_getUnit($key));
191  $subunit_string = $lng->txt('currency_'.ilPaymentCurrency::_getSubUnit($key));
192 
193  if((int) $value['unit'])
194  {
195  $pr_str .= $value['unit'].' '.$unit_string.' ';
196  }
197  if((int) $value['subunit'])
198  {
199  $pr_str .= $value['subunit'].' '.$subunit_string;
200  }
201 
202  // in the moment only one price
203  return $pr_str;
204  }
205  }
206  return 0;*/
207  }
208 
209 
210  function setUnitValue($a_value = 0)
211  {
212  // substitute leading zeros with ''
213  $this->unit_value = preg_replace('/^0+/','',$a_value);
214  }
215  function setSubUnitValue($a_value = 0)
216  {
217  $this->sub_unit_value = $a_value;
218  }
219  function setCurrency($a_currency_id)
220  {
221  $this->currency = $a_currency_id;
222  }
223  function setDuration($a_duration)
224  {
225  $this->duration = $a_duration;
226  }
227 
228  function add()
229  {
230  $query = "INSERT INTO payment_prices SET ".
231  "pobject_id = '".$this->getPobjectId()."', ".
232  "currency = '".$this->__getCurrency()."', ".
233  "duration = '".$this->__getDuration()."', ".
234  "unit_value = '".$this->__getUnitValue()."', ".
235  "sub_unit_value = '".$this->__getSubUnitValue()."'";
236 
237  $res = $this->db->query($query);
238 
239  $this->__read();
240 
241  return true;
242  }
243  function update($a_price_id)
244  {
245 
246  $query = "UPDATE payment_prices SET ".
247  "currency = '".$this->__getCurrency()."', ".
248  "duration = '".$this->__getDuration()."', ".
249  "unit_value = '".$this->__getUnitValue()."', ".
250  "sub_unit_value = '".$this->__getSubUnitValue()."' ".
251  "WHERE price_id = '".$a_price_id."'";
252 
253  $res = $this->db->query($query);
254 
255  $this->__read();
256 
257  return true;
258  }
259  function delete($a_price_id)
260  {
261  $query = "DELETE FROM payment_prices ".
262  "WHERE price_id = '".$a_price_id."'";
263 
264  $res = $this->db->query($query);
265 
266 
267  $this->__read();
268 
269  return true;
270  }
271  function deleteAllPrices()
272  {
273  $query = "DELETE FROM payment_prices ".
274  "WHERE pobject_id = '".$this->getPobjectId()."'";
275 
276  $res = $this->db->query($query);
277 
278  $this->__read();
279 
280  return true;
281  }
282 
283  function validate()
284  {
285  $duration_valid = false;
286  $price_valid = false;
287 
288  if(preg_match('/^[1-9][0-9]{0,1}$/',$this->__getDuration()))
289  {
290  $duration_valid = true;
291  }
292 
293  if(preg_match('/^[1-9]\d{0,4}$/',$this->__getUnitValue()) and
294  preg_match('/^\d{0,2}$/',$this->__getSubUnitValue()))
295  {
296  $price_valid = true;
297  }
298  else if(preg_match('/^\d{0,5}$/',$this->__getUnitValue()) and
299  preg_match('/[1-9]/',$this->__getSubUnitValue()))
300  {
301  return true;
302  }
303  return $duration_valid and $price_valid;
304  }
305  // STATIC
306  function _priceExists($a_price_id,$a_pobject_id)
307  {
308  global $ilDB;
309 
310  $query = "SELECT * FROM payment_prices ".
311  "WHERE price_id = '".$a_price_id."' ".
312  "AND pobject_id = '".$a_pobject_id."'";
313 
314  $res = $ilDB->query($query);
315 
316  return $res->numRows() ? true : false;
317  }
318 
319 
320 
321  // PRIVATE
322  function __getUnitValue()
323  {
324  return $this->unit_value;
325  }
326  function __getSubUnitValue()
327  {
328  return $this->sub_unit_value;
329  }
330  function __getCurrency()
331  {
332  return $this->currency;
333  }
334  function __getDuration()
335  {
336  return $this->duration;
337  }
338 
339  function __read()
340  {
341  $this->prices = array();
342 
343  $query = "SELECT * FROM payment_prices ".
344  "WHERE pobject_id = '".$this->getPobjectId()."' ".
345  "ORDER BY duration";
346 
347  $res = $this->db->query($query);
348  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
349  {
350  $this->prices[$row->price_id]['pobject_id'] = $row->pobject_id;
351  $this->prices[$row->price_id]['price_id'] = $row->price_id;
352  $this->prices[$row->price_id]['currency'] = $row->currency;
353  $this->prices[$row->price_id]['duration'] = $row->duration;
354  $this->prices[$row->price_id]['unit_value'] = $row->unit_value;
355  $this->prices[$row->price_id]['sub_unit_value'] = $row->sub_unit_value;
356  }
357  }
358 
359  public function getNumberOfPrices()
360  {
361  return count($this->prices);
362  }
363 
364  public function getLowestPrice()
365  {
366  $lowest_price_id = 0;
367  $lowest_price = 0;
368 
369  foreach ($this->prices as $price_id => $data)
370  {
371  $current_price = self::_formatPriceToFloat($data['unit_value'], $data['sub_unit_value']);
372 
373  if($lowest_price == 0||
374  $lowest_price > (float)$current_price)
375  {
376  $lowest_price = (float)$current_price;
377  $lowest_price_id = $price_id;
378  }
379  }
380 
381  return is_array($this->prices[$lowest_price_id]) ? $this->prices[$lowest_price_id] : array();
382  }
383 }
384 ?>