• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

payment/classes/class.ilPaymentPrices.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00031 class ilPaymentPrices
00032 {
00033         var $ilDB;
00034 
00035         var $pobject_id;
00036         var $unit_value;
00037         var $sub_unit_value;
00038         var $currency;
00039         var $duration;
00040 
00041         var $prices;
00042         
00043         function ilPaymentPrices($a_pobject_id = 0)
00044         {
00045                 global $ilDB;
00046 
00047                 $this->db =& $ilDB;
00048 
00049                 $this->pobject_id = $a_pobject_id;
00050 
00051                 $this->__read();
00052         }
00053 
00054         // SET GET
00055         function getPobjectId()
00056         {
00057                 return $this->pobject_id;
00058         }
00059 
00060         function getPrices()
00061         {
00062                 return $this->prices ? $this->prices : array();
00063         }
00064         function getPrice($a_price_id)
00065         {
00066                 return $this->prices[$a_price_id] ? $this->prices[$a_price_id] : array();
00067         }
00068 
00069         // STATIC
00070         function _getPrice($a_price_id)
00071         {
00072                 global $ilDB;
00073 
00074                 $query = "SELECT * FROM payment_prices ".
00075                         "WHERE price_id = '".$a_price_id."'";
00076 
00077                 $res = $ilDB->query($query);
00078 
00079                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00080                 {
00081                         $price['duration'] = $row->duration;
00082                         $price['currency'] = $row->currency;
00083                         $price['unit_value'] = $row->unit_value;
00084                         $price['sub_unit_value'] = $row->sub_unit_value;
00085                 }
00086                 return count($price) ? $price : array();
00087         }
00088 
00089         function _countPrices($a_pobject_id)
00090         {
00091                 $query = "SELECT count(price_id) FROM payment_prices ".
00092                         "WHERE pobject_id = '".$a_pobject_id."'";
00093 
00094                 $res = $this->db->query($query);
00095                 $row = $res->fetchRow(DB_FETCHMODE_ARRAY);
00096 
00097                 return ($row[0]);
00098         }
00099 
00100         function _getPriceString($a_price_id)
00101         {
00102                 include_once './payment/classes/class.ilPaymentCurrency.php';
00103                 include_once './payment/classes/class.ilGeneralSettings.php';
00104 
00105                 global $lng;
00106 
00107                 $genSet = new ilGeneralSettings();
00108                 $unit_string = $genSet->get("currency_unit");
00109 
00110                 $pr_str = '';
00111                 $price = ilPaymentPrices::_getPrice($a_price_id);
00112 
00113                 $pr_str = number_format( ((int) $price["unit_value"]) . "." . sprintf("%02d", ((int) $price["sub_unit_value"])) , 2, ",", ".");
00114                 return $pr_str . " " . $unit_string;
00115 
00116 /*              $unit_string = $lng->txt('currency_'.ilPaymentCurrency::_getUnit($price['currency']));
00117                 $subunit_string = $lng->txt('currency_'.ilPaymentCurrency::_getSubUnit($price['currency']));
00118 
00119                 if((int) $price['unit_value'])
00120                 {
00121                         $pr_str .= $price['unit_value'].' '.$unit_string.' ';
00122                 }
00123                 if((int) $price['sub_unit_value'])
00124                 {
00125                         $pr_str .= $price['sub_unit_value'].' '.$subunit_string;
00126                 }
00127                 return $pr_str; */
00128         }
00129         
00130         function _getPriceStringFromAmount($a_price)
00131         {
00132                 include_once './payment/classes/class.ilPaymentCurrency.php';
00133                 include_once './payment/classes/class.ilGeneralSettings.php';
00134 
00135                 global $lng;
00136 
00137                 $genSet = new ilGeneralSettings();
00138                 $unit_string = $genSet->get("currency_unit");
00139 
00140                 $pr_str = '';           
00141 
00142                 $pr_str = number_format($a_price , 2, ",", ".");
00143                 return $pr_str . " " . $unit_string;            
00144         }
00145         
00146         function _getPriceFromArray($a_price)
00147         {               
00148                 return (float) (((int) $a_price["unit_value"]) . "." . sprintf("%02d", ((int) $a_price["sub_unit_value"])));
00149         }
00150                         
00151         function _getTotalAmount($a_price_ids)
00152         {
00153                 include_once './payment/classes/class.ilPaymentPrices.php';
00154 #               include_once './payment/classes/class.ilPaymentCurrency.php';
00155                 include_once './payment/classes/class.ilGeneralSettings.php';
00156 
00157                 global $ilDB,$lng;
00158 
00159                 $genSet = new ilGeneralSettings();
00160                 $unit_string = $genSet->get("currency_unit");
00161 
00162                 $amount = array();
00163 
00164                 if (is_array($a_price_ids))
00165                 {
00166                         for ($i = 0; $i < count($a_price_ids); $i++)
00167                         {
00168                                 $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
00169 
00170                                 $price = ((int) $price_data["unit_value"]) . "." . sprintf("%02d", ((int) $price_data["sub_unit_value"]));
00171                                 $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
00172                         }
00173                 }
00174 
00175                 return $amount;
00176 
00177 /*              foreach($a_price_ids as $id)
00178                 {
00179                         $price_data = ilPaymentPrices::_getPrice($id);
00180 
00181                         $price_arr["$price_data[currency]"]['unit'] += (int) $price_data['unit_value'];
00182                         $price_arr["$price_data[currency]"]['subunit'] += (int) $price_data['sub_unit_value'];
00183                 }
00184 
00185                 if(is_array($price_arr))
00186                 {
00187                         foreach($price_arr as $key => $value)
00188                         {
00189                                 // CHECK cent bigger 100
00190                                 $value['unit'] += (int) ($value['subunit'] / 100);
00191                                 $value['subunit'] = (int) ($value['subunit'] % 100);
00192 
00193                                 $unit_string = $lng->txt('currency_'.ilPaymentCurrency::_getUnit($key));
00194                                 $subunit_string = $lng->txt('currency_'.ilPaymentCurrency::_getSubUnit($key));
00195 
00196                                 if((int) $value['unit'])
00197                                 {
00198                                         $pr_str .= $value['unit'].' '.$unit_string.' ';
00199                                 }
00200                                 if((int) $value['subunit'])
00201                                 {
00202                                         $pr_str .= $value['subunit'].' '.$subunit_string;
00203                                 }
00204 
00205                                 // in the moment only one price
00206                                 return $pr_str;
00207                         }
00208                 }
00209                 return 0;*/
00210         }
00211                 
00212 
00213         function setUnitValue($a_value = 0)
00214         {
00215                 // substitute leading zeros with ''
00216                 $this->unit_value = preg_replace('/^0+/','',$a_value);
00217         }
00218         function setSubUnitValue($a_value = 0)
00219         {
00220                 $this->sub_unit_value = $a_value;
00221         }
00222         function setCurrency($a_currency_id)
00223         {
00224                 $this->currency = $a_currency_id;
00225         }
00226         function setDuration($a_duration)
00227         {
00228                 $this->duration = $a_duration;
00229         }
00230 
00231         function add()
00232         {
00233                 $query = "INSERT INTO payment_prices SET ".
00234                         "pobject_id = '".$this->getPobjectId()."', ".
00235                         "currency = '".$this->__getCurrency()."', ".
00236                         "duration = '".$this->__getDuration()."', ".
00237                         "unit_value = '".$this->__getUnitValue()."', ".
00238                         "sub_unit_value = '".$this->__getSubUnitValue()."'";
00239 
00240                 $res = $this->db->query($query);
00241 
00242                 $this->__read();
00243                 
00244                 return true;
00245         }
00246         function update($a_price_id)
00247         {
00248 
00249                 $query = "UPDATE payment_prices SET ".
00250                         "currency = '".$this->__getCurrency()."', ".
00251                         "duration = '".$this->__getDuration()."', ".
00252                         "unit_value = '".$this->__getUnitValue()."', ".
00253                         "sub_unit_value = '".$this->__getSubUnitValue()."' ".
00254                         "WHERE price_id = '".$a_price_id."'";
00255 
00256                 $res = $this->db->query($query);
00257 
00258                 $this->__read();
00259 
00260                 return true;
00261         }
00262         function delete($a_price_id)
00263         {
00264                 $query = "DELETE FROM payment_prices ".
00265                         "WHERE price_id = '".$a_price_id."'";
00266 
00267                 $res = $this->db->query($query);
00268                 
00269 
00270                 $this->__read();
00271 
00272                 return true;
00273         }
00274         function deleteAllPrices()
00275         {
00276                 $query = "DELETE FROM payment_prices ".
00277                         "WHERE pobject_id = '".$this->getPobjectId()."'";
00278 
00279                 $res = $this->db->query($query);
00280                 
00281                 $this->__read();
00282 
00283                 return true;
00284         }
00285 
00286         function validate()
00287         {
00288                 $duration_valid = false;
00289                 $price_valid = false;
00290 
00291                 if(preg_match('/^[1-9][0-9]{0,1}$/',$this->__getDuration()))
00292                 {
00293                         $duration_valid = true;
00294                 }
00295                 
00296                 if(preg_match('/^[1-9]\d{0,4}$/',$this->__getUnitValue()) and
00297                    preg_match('/^\d{0,2}$/',$this->__getSubUnitValue()))
00298                 {
00299                         $price_valid = true;
00300                 }
00301                 else if(preg_match('/^\d{0,5}$/',$this->__getUnitValue()) and
00302                                 preg_match('/[1-9]/',$this->__getSubUnitValue()))
00303                 {
00304                         return true;
00305                 }
00306                 return $duration_valid and $price_valid;
00307         }
00308         // STATIC
00309         function _priceExists($a_price_id,$a_pobject_id)
00310         {
00311                 global $ilDB;
00312 
00313                 $query = "SELECT * FROM payment_prices ".
00314                         "WHERE price_id = '".$a_price_id."' ".
00315                         "AND pobject_id = '".$a_pobject_id."'";
00316 
00317                 $res = $ilDB->query($query);
00318 
00319                 return $res->numRows() ? true : false;
00320         }
00321 
00322 
00323                                   
00324         // PRIVATE
00325         function __getUnitValue()
00326         {
00327                 return $this->unit_value;
00328         }
00329         function __getSubUnitValue()
00330         {
00331                 return $this->sub_unit_value;
00332         }
00333         function __getCurrency()
00334         {
00335                 return $this->currency;
00336         }
00337         function __getDuration()
00338         {
00339                 return $this->duration;
00340         }
00341 
00342         function __read()
00343         {
00344                 $this->prices = array();
00345 
00346                 $query = "SELECT * FROM payment_prices ".
00347                         "WHERE pobject_id = '".$this->getPobjectId()."' ".
00348                         "ORDER BY duration";
00349 
00350                 $res = $this->db->query($query);
00351                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00352                 {
00353                         $this->prices[$row->price_id]['pobject_id'] = $row->pobject_id;
00354                         $this->prices[$row->price_id]['price_id'] = $row->price_id;
00355                         $this->prices[$row->price_id]['currency'] = $row->currency;
00356                         $this->prices[$row->price_id]['duration'] = $row->duration;
00357                         $this->prices[$row->price_id]['unit_value'] = $row->unit_value;
00358                         $this->prices[$row->price_id]['sub_unit_value'] = $row->sub_unit_value;
00359                 }
00360         }
00361 }
00362 ?>

Generated on Fri Dec 13 2013 17:56:54 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1