oxprice.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxPrice extends oxSuperCfg
00007 {
00013     protected $_dBrutto = 0.0;
00014 
00020     protected $_dNetto = 0.0;
00021 
00027     protected $_dVat = 0.0;
00028 
00037     protected $_blNetPriceMode;
00038 
00046     public function __construct($dInitPrice = null)
00047     {
00048         $this->_blNetPriceMode = $this->getConfig()->getConfigParam( 'blEnterNetPrice' );
00049 
00050         if ($dInitPrice) {
00051             $this->setPrice($dInitPrice);
00052         }
00053     }
00054 
00060     public function setNettoPriceMode()
00061     {
00062         $this->_blNetPriceMode = true;
00063     }
00064 
00070     public function setBruttoPriceMode()
00071     {
00072         $this->_blNetPriceMode = false;
00073     }
00074 
00082     public function setVat($newVat)
00083     {
00084         $this->_dVat = (double) $newVat;
00085         $this->_recalculate();
00086     }
00087 
00100     public function setUserVat($newVat)
00101     {
00102         if (!$this->_blNetPriceMode && $newVat != $this->_dVat) {
00103             $this->_dBrutto = self::Netto2Brutto(self::Brutto2Netto($this->_dBrutto, $this->_dVat), (double) $newVat);
00104         }
00105         $this->_dVat = (double) $newVat;
00106         $this->_recalculate();
00107     }
00108 
00114     public function getVat()
00115     {
00116         return $this->_dVat;
00117     }
00118 
00128     public function setPrice($newPrice, $dVat = null)
00129     {
00130         if (isset($dVat)) {
00131             $this->_dVat = (double) $dVat;
00132         }
00133 
00134         if ($this->_blNetPriceMode) {
00135             $this->_dNetto = $newPrice;
00136         } else {
00137             $this->_dBrutto = $newPrice;
00138         }
00139         $this->_recalculate();
00140     }
00141 
00147     public function getBruttoPrice()
00148     {
00149         if (!$this->_blNetPriceMode) {
00150             return oxUtils::getInstance()->fRound($this->_dBrutto);
00151         }
00152         return $this->_dBrutto;
00153     }
00154 
00160     public function getNettoPrice()
00161     {
00162         if ($this->_blNetPriceMode) {
00163             return oxUtils::getInstance()->fRound($this->_dNetto);
00164         }
00165         return $this->_dNetto;
00166     }
00167 
00173     public function getVatValue()
00174     {
00175         return (double) $this->getBruttoPrice() - $this->getNettoPrice();
00176     }
00177 
00186     public function subtractPercent($dValue)
00187     {
00188         if ($this->_blNetPriceMode) {
00189             $this->_dNetto = $this->_dNetto - self::percent($this->_dNetto, $dValue);
00190         } else {
00191             $this->_dBrutto = $this->_dBrutto - self::percent($this->_dBrutto, $dValue);
00192         }
00193         $this->_recalculate();
00194 
00195     }
00196 
00205     public function addPercent($dValue)
00206     {
00207         $this->subtractPercent(-$dValue);
00208     }
00209 
00217     public function addPrice( oxPrice $oPrice )
00218     {
00219         if ($this->_blNetPriceMode) {
00220             $this->add( $oPrice->getNettoPrice() );
00221         } else {
00222             $this->add( $oPrice->getBruttoPrice() );
00223         }
00224     }
00225 
00234     public function add($dValue)
00235     {
00236         if ($this->_blNetPriceMode) {
00237             $this->_dNetto += $dValue;
00238         } else {
00239             $this->_dBrutto += $dValue;
00240         }
00241         $this->_recalculate();
00242     }
00243 
00252     public function subtract($dValue)
00253     {
00254         $this->add(-$dValue);
00255     }
00256 
00265     public function multiply($dValue)
00266     {
00267         if ($this->_blNetPriceMode) {
00268             $this->_dNetto = $this->_dNetto * $dValue;
00269         } else {
00270             $this->_dBrutto = $this->_dBrutto * $dValue;
00271         }
00272         $this->_recalculate();
00273     }
00274 
00283     public function divide($dValue)
00284     {
00285         if ($this->_blNetPriceMode) {
00286             $this->_dNetto = $this->_dNetto / $dValue;
00287         } else {
00288             $this->_dBrutto = $this->_dBrutto / $dValue;
00289         }
00290         $this->_recalculate();
00291     }
00292 
00304     public function compare(oxPrice $oPrice)
00305     {
00306         $dBruttoPrice1 = $this->getBruttoPrice();
00307         $dBruttoPrice2 = $oPrice->getBruttoPrice();
00308 
00309         if ($dBruttoPrice1 == $dBruttoPrice2) {
00310             $iRes = 0;
00311         } elseif ($dBruttoPrice1 > $dBruttoPrice2) {
00312             $iRes = 1;
00313         } else {
00314             $iRes = -1;
00315         }
00316 
00317         return $iRes;
00318     }
00319 
00328     public static function percent($dValue, $dPercent)
00329     {
00330         return ((double) $dValue * (double) $dPercent)/100.0;
00331     }
00332 
00346     public static function brutto2Netto($dBrutto, $dVat)
00347     {
00348         // if VAT = -100% Return 0 because we subtract all what we have.
00349         // made to avoid division by zero in formula.
00350         if ($dVat == -100) {
00351             return 0;
00352         }
00353 
00354         $dBrutto = oxUtils::getInstance()->fRound($dBrutto);
00355         return (double) ((double) $dBrutto*100.0)/(100.0 + (double) $dVat);
00356 
00357         // Old (alternative) formulas
00358         // return $dPrice / ( 1 + ( $dVat/100) );
00359         // return $dPrice / ( 1 + ((1/100) * $myConfig->dDefaultVAT));
00360 
00361     }
00362 
00374     public static function netto2Brutto($dNetto, $dVat)
00375     {
00376         $dNetto = oxUtils::getInstance()->fRound($dNetto);
00377         return (double) $dNetto + self::percent($dNetto, $dVat);
00378     }
00379 
00387     protected function _recalculate()
00388     {
00389         if ( $this->_blNetPriceMode ) {
00390             // rounding base price in netto mode
00391             $this->_dNetto  = oxUtils::getInstance()->fRound( $this->_dNetto );
00392             $this->_dBrutto = self::netto2Brutto($this->_dNetto, $this->_dVat);
00393         } else {
00394             $this->_dNetto  = self::brutto2Netto($this->_dBrutto, $this->_dVat);
00395         }
00396 
00397         // rounding brutto price value after each operation
00398         $this->_dBrutto = oxUtils::getInstance()->fRound( $this->_dBrutto );
00399     }
00400 
00408     public static function getPriceInActCurrency( $dPrice )
00409     {
00410         $oCur = oxConfig::getInstance()->getActShopCurrencyObject();
00411         return ( ( double ) $dPrice ) * $oCur->rate;
00412     }
00413 }