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 
00102     public function setUserVat($newVat)
00103     {
00104         if (!$this->_blNetPriceMode && $newVat != $this->_dVat) {
00105             $this->_dBrutto = self::Netto2Brutto(self::Brutto2Netto($this->_dBrutto, $this->_dVat), (double) $newVat);
00106         }
00107         $this->_dVat = (double) $newVat;
00108         $this->_recalculate();
00109     }
00110 
00116     public function getVat()
00117     {
00118         return $this->_dVat;
00119     }
00120 
00130     public function setPrice($newPrice, $dVat = null)
00131     {
00132         if (isset($dVat)) {
00133             $this->_dVat = (double) $dVat;
00134         }
00135 
00136         if ($this->_blNetPriceMode) {
00137             $this->_dNetto = $newPrice;
00138         } else {
00139             $this->_dBrutto = $newPrice;
00140         }
00141         $this->_recalculate();
00142     }
00143 
00149     public function getBruttoPrice()
00150     {
00151         if (!$this->_blNetPriceMode) {
00152             return oxUtils::getInstance()->fRound($this->_dBrutto);
00153         }
00154         return $this->_dBrutto;
00155     }
00156 
00162     public function getNettoPrice()
00163     {
00164         if ($this->_blNetPriceMode) {
00165             return oxUtils::getInstance()->fRound($this->_dNetto);
00166         }
00167         return $this->_dNetto;
00168     }
00169 
00175     public function getVatValue()
00176     {
00177         return (double) $this->getBruttoPrice() - $this->getNettoPrice();
00178     }
00179 
00188     public function subtractPercent($dValue)
00189     {
00190         if ($this->_blNetPriceMode) {
00191             $this->_dNetto = $this->_dNetto - self::percent($this->_dNetto, $dValue);
00192         } else {
00193             $this->_dBrutto = $this->_dBrutto - self::percent($this->_dBrutto, $dValue);
00194         }
00195         $this->_recalculate();
00196 
00197     }
00198 
00207     public function addPercent($dValue)
00208     {
00209         $this->subtractPercent(-$dValue);
00210     }
00211 
00219     public function addPrice( oxPrice $oPrice )
00220     {
00221         if ($this->_blNetPriceMode) {
00222             $this->add( $oPrice->getNettoPrice() );
00223         } else {
00224             $this->add( $oPrice->getBruttoPrice() );
00225         }
00226     }
00227 
00236     public function add($dValue)
00237     {
00238         if ($this->_blNetPriceMode) {
00239             $this->_dNetto += $dValue;
00240         } else {
00241             $this->_dBrutto += $dValue;
00242         }
00243         $this->_recalculate();
00244     }
00245 
00254     public function subtract($dValue)
00255     {
00256         $this->add(-$dValue);
00257     }
00258 
00267     public function multiply($dValue)
00268     {
00269         if ($this->_blNetPriceMode) {
00270             $this->_dNetto = $this->_dNetto * $dValue;
00271         } else {
00272             $this->_dBrutto = $this->_dBrutto * $dValue;
00273         }
00274         $this->_recalculate();
00275     }
00276 
00285     public function divide($dValue)
00286     {
00287         if ($this->_blNetPriceMode) {
00288             $this->_dNetto = $this->_dNetto / $dValue;
00289         } else {
00290             $this->_dBrutto = $this->_dBrutto / $dValue;
00291         }
00292         $this->_recalculate();
00293     }
00294 
00306     public function compare(oxPrice $oPrice)
00307     {
00308         $dBruttoPrice1 = $this->getBruttoPrice();
00309         $dBruttoPrice2 = $oPrice->getBruttoPrice();
00310 
00311         if ($dBruttoPrice1 == $dBruttoPrice2) {
00312             $iRes = 0;
00313         } elseif ($dBruttoPrice1 > $dBruttoPrice2) {
00314             $iRes = 1;
00315         } else {
00316             $iRes = -1;
00317         }
00318 
00319         return $iRes;
00320     }
00321 
00330     public static function percent($dValue, $dPercent)
00331     {
00332         return ((double) $dValue * (double) $dPercent)/100.0;
00333     }
00334 
00347     public static function brutto2Netto($dBrutto, $dVat)
00348     {
00349         // if VAT = -100% Return 0 because we subtract all what we have.
00350         // made to avoid division by zero in formula.
00351         if ($dVat == -100) {
00352             return 0;
00353         }
00354 
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 
00373     public static function netto2Brutto($dNetto, $dVat)
00374     {
00375         return (double) $dNetto + self::percent($dNetto, $dVat);
00376     }
00377 
00385     protected function _recalculate()
00386     {
00387         if ( $this->_blNetPriceMode ) {
00388             // rounding base price in netto mode
00389             $this->_dNetto  = oxUtils::getInstance()->fRound( $this->_dNetto );
00390             $this->_dBrutto = self::netto2Brutto($this->_dNetto, $this->_dVat);
00391 
00392             // rounding brutto price value after each operation
00393             $this->_dBrutto = oxUtils::getInstance()->fRound( $this->_dBrutto );
00394         } else {
00395             $this->_dBrutto = oxUtils::getInstance()->fRound( $this->_dBrutto );
00396             $this->_dNetto  = self::brutto2Netto($this->_dBrutto, $this->_dVat);
00397         }
00398     }
00399 
00407     public static function getPriceInActCurrency( $dPrice )
00408     {
00409         $oCur = oxConfig::getInstance()->getActShopCurrencyObject();
00410         return ( ( double ) $dPrice ) * $oCur->rate;
00411     }
00412 }