oxprice.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxPrice extends oxSuperCfg
00008 {
00014     protected $_dBrutto = 0.0;
00015 
00021     protected $_dNetto = 0.0;
00022 
00028     protected $_dVat = 0.0;
00029 
00038     protected $_blNetPriceMode;
00039 
00047     public function __construct($dInitPrice = null)
00048     {
00049         $this->_blNetPriceMode = $this->getConfig()->getConfigParam( 'blEnterNetPrice' );
00050 
00051         if ($dInitPrice) {
00052             $this->setPrice($dInitPrice);
00053         }
00054     }
00055 
00061     public function setNettoPriceMode()
00062     {
00063         $this->_blNetPriceMode = true;
00064     }
00065 
00071     public function setBruttoPriceMode()
00072     {
00073         $this->_blNetPriceMode = false;
00074     }
00075 
00083     public function setVat($newVat)
00084     {
00085         $this->_dVat = (double) $newVat;
00086         $this->_recalculate();
00087     }
00088 
00101     public function setUserVat($newVat)
00102     {
00103         if (!$this->_blNetPriceMode && $newVat != $this->_dVat) {
00104             $this->_dBrutto = self::Netto2Brutto(self::Brutto2Netto($this->_dBrutto, $this->_dVat), (double) $newVat);
00105         }
00106         $this->_dVat = (double) $newVat;
00107         $this->_recalculate();
00108     }
00109 
00115     public function getVat()
00116     {
00117         return $this->_dVat;
00118     }
00119 
00129     public function setPrice($newPrice, $dVat = null)
00130     {
00131         if (isset($dVat)) {
00132             $this->_dVat = (double) $dVat;
00133         }
00134 
00135         if ($this->_blNetPriceMode) {
00136             $this->_dNetto = $newPrice;
00137         } else {
00138             $this->_dBrutto = $newPrice;
00139         }
00140         $this->_recalculate();
00141     }
00142 
00148     public function getBruttoPrice()
00149     {
00150         if (!$this->_blNetPriceMode) {
00151             return oxUtils::getInstance()->fRound($this->_dBrutto);
00152         }
00153         return $this->_dBrutto;
00154     }
00155 
00161     public function getNettoPrice()
00162     {
00163         if ($this->_blNetPriceMode) {
00164             return oxUtils::getInstance()->fRound($this->_dNetto);
00165         }
00166         return $this->_dNetto;
00167     }
00168 
00174     public function getVatValue()
00175     {
00176         return (double) $this->getBruttoPrice() - $this->getNettoPrice();
00177     }
00178 
00187     public function subtractPercent($dValue)
00188     {
00189         if ($this->_blNetPriceMode) {
00190             $this->_dNetto = $this->_dNetto - self::percent($this->_dNetto, $dValue);
00191         } else {
00192             $this->_dBrutto = $this->_dBrutto - self::percent($this->_dBrutto, $dValue);
00193         }
00194         $this->_recalculate();
00195 
00196     }
00197 
00206     public function addPercent($dValue)
00207     {
00208         $this->subtractPercent(-$dValue);
00209     }
00210 
00218     public function addPrice( oxPrice $oPrice )
00219     {
00220         if ($this->_blNetPriceMode) {
00221             $this->add( $oPrice->getNettoPrice() );
00222         } else {
00223             $this->add( $oPrice->getBruttoPrice() );
00224         }
00225     }
00226 
00235     public function add($dValue)
00236     {
00237         if ($this->_blNetPriceMode) {
00238             $this->_dNetto += $dValue;
00239         } else {
00240             $this->_dBrutto += $dValue;
00241         }
00242         $this->_recalculate();
00243     }
00244 
00253     public function subtract($dValue)
00254     {
00255         $this->add(-$dValue);
00256     }
00257 
00266     public function multiply($dValue)
00267     {
00268         if ($this->_blNetPriceMode) {
00269             $this->_dNetto = $this->_dNetto * $dValue;
00270         } else {
00271             $this->_dBrutto = $this->_dBrutto * $dValue;
00272         }
00273         $this->_recalculate();
00274     }
00275 
00284     public function divide($dValue)
00285     {
00286         if ($this->_blNetPriceMode) {
00287             $this->_dNetto = $this->_dNetto / $dValue;
00288         } else {
00289             $this->_dBrutto = $this->_dBrutto / $dValue;
00290         }
00291         $this->_recalculate();
00292     }
00293 
00305     public function compare(oxPrice $oPrice)
00306     {
00307         $dBruttoPrice1 = $this->getBruttoPrice();
00308         $dBruttoPrice2 = $oPrice->getBruttoPrice();
00309 
00310         if ($dBruttoPrice1 == $dBruttoPrice2) {
00311             $iRes = 0;
00312         } elseif ($dBruttoPrice1 > $dBruttoPrice2) {
00313             $iRes = 1;
00314         } else {
00315             $iRes = -1;
00316         }
00317 
00318         return $iRes;
00319     }
00320 
00329     public static function percent($dValue, $dPercent)
00330     {
00331         return ((double) $dValue * (double) $dPercent)/100.0;
00332     }
00333 
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         $dBrutto = oxUtils::getInstance()->fRound($dBrutto);
00356         return (double) ((double) $dBrutto*100.0)/(100.0 + (double) $dVat);
00357 
00358         // Old (alternative) formulas
00359         // return $dPrice / ( 1 + ( $dVat/100) );
00360         // return $dPrice / ( 1 + ((1/100) * $myConfig->dDefaultVAT));
00361 
00362     }
00363 
00375     public static function netto2Brutto($dNetto, $dVat)
00376     {
00377         $dNetto = oxUtils::getInstance()->fRound($dNetto);
00378         return (double) $dNetto + self::percent($dNetto, $dVat);
00379     }
00380 
00388     protected function _recalculate()
00389     {
00390         if ( $this->_blNetPriceMode ) {
00391             // rounding base price in netto mode
00392             $this->_dNetto  = oxUtils::getInstance()->fRound( $this->_dNetto );
00393             $this->_dBrutto = self::netto2Brutto($this->_dNetto, $this->_dVat);
00394         } else {
00395             $this->_dNetto  = self::brutto2Netto($this->_dBrutto, $this->_dVat);
00396         }
00397 
00398         // rounding brutto price value after each operation
00399         $this->_dBrutto = oxUtils::getInstance()->fRound( $this->_dBrutto );
00400     }
00401 }

Generated on Thu Dec 4 12:04:56 2008 for OXID eShop CE by  doxygen 1.5.5