oxprice.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxPrice
00007 {
00013     protected $_dBrutto = 0.0;
00014 
00020     protected $_dNetto = 0.0;
00021 
00027     protected $_dVat = 0.0;
00028 
00029 
00035     protected $_aDiscounts = null;
00036 
00037 
00046     protected $_blNetPriceMode;
00047 
00055     public function __construct($dInitPrice = null)
00056     {
00057         $this->_blNetPriceMode = oxRegistry::getConfig()->getConfigParam( 'blEnterNetPrice' );
00058 
00059         if ($dInitPrice) {
00060             $this->setPrice($dInitPrice);
00061         }
00062     }
00063 
00069     public function setNettoPriceMode()
00070     {
00071         $this->_blNetPriceMode = true;
00072     }
00073 
00079     public function setBruttoPriceMode()
00080     {
00081         $this->_blNetPriceMode = false;
00082     }
00083 
00091     public function setVat($newVat)
00092     {
00093         $this->_dVat = (double) $newVat;
00094     }
00095 
00110     public function setUserVat($newVat)
00111     {
00112         if (!$this->_blNetPriceMode && $newVat != $this->_dVat) {
00113             $this->_dBrutto = self::Netto2Brutto(self::Brutto2Netto($this->_dBrutto, $this->_dVat), (double) $newVat);
00114         }
00115         $this->_dVat = (double) $newVat;
00116     }
00117 
00123     public function getVat()
00124     {
00125         return $this->_dVat;
00126     }
00127 
00137     public function setPrice($dPrice, $dVat = null)
00138     {
00139         if (isset($dVat)) {
00140             $this->_dVat = (double) $dVat;
00141         }
00142 
00143         if ($this->_blNetPriceMode) {
00144             $this->_dNetto = $dPrice;
00145         } else {
00146             $this->_dBrutto = $dPrice;
00147         }
00148     }
00149 
00155     public function getPrice()
00156     {
00157         if ( $this->_blNetPriceMode ) {
00158             return $this->getNettoPrice();
00159         } else {
00160             return $this->getBruttoPrice();
00161         }
00162     }
00163 
00169     public function getBruttoPrice()
00170     {
00171         if ( $this->_blNetPriceMode ) {
00172             return $this->getNettoPrice() + $this->getVatValue();
00173         } else {
00174             return oxRegistry::getUtils()->fRound($this->_dBrutto);
00175         }
00176     }
00177 
00183     public function getNettoPrice()
00184     {
00185         if ( $this->_blNetPriceMode ) {
00186             return oxRegistry::getUtils()->fRound($this->_dNetto);
00187         } else {
00188             return $this->getBruttoPrice() - $this->getVatValue();
00189         }
00190     }
00191 
00197     public function getVatValue()
00198     {
00199         if ( $this->_blNetPriceMode ) {
00200             $dVatValue = $this->getNettoPrice() * $this->getVat() / 100 ;
00201         } else {
00202             $dVatValue = $this->getBruttoPrice() * $this->getVat() / ( 100 + $this->getVat());
00203         }
00204 
00205         return oxRegistry::getUtils()->fRound( $dVatValue );
00206     }
00207 
00216     public function subtractPercent( $dValue )
00217     {
00218         $dPrice = $this->getPrice();
00219         $this->setPrice( $dPrice - self::percent( $dPrice, $dValue) );
00220     }
00221 
00230     public function addPercent($dValue)
00231     {
00232         $this->subtractPercent(-$dValue);
00233     }
00234 
00242     public function addPrice( oxPrice $oPrice )
00243     {
00244         if ($this->_blNetPriceMode) {
00245             $this->add( $oPrice->getNettoPrice() );
00246         } else {
00247             $this->add( $oPrice->getBruttoPrice() );
00248         }
00249     }
00250 
00259     public function add($dValue)
00260     {
00261         $dPrice = $this->getPrice();
00262         $this->setPrice( $dPrice + $dValue );
00263     }
00264 
00273     public function subtract($dValue)
00274     {
00275         $this->add(-$dValue);
00276     }
00277 
00286     public function multiply($dValue)
00287     {
00288         $dPrice = $this->getPrice();
00289         $this->setPrice( $dPrice * $dValue );
00290     }
00291 
00300     public function divide($dValue)
00301     {
00302         $dPrice = $this->getPrice();
00303         $this->setPrice( $dPrice / $dValue );
00304     }
00305 
00317     public function compare(oxPrice $oPrice)
00318     {
00319         $dBruttoPrice1 = $this->getBruttoPrice();
00320         $dBruttoPrice2 = $oPrice->getBruttoPrice();
00321 
00322         if ($dBruttoPrice1 == $dBruttoPrice2) {
00323             $iRes = 0;
00324         } elseif ($dBruttoPrice1 > $dBruttoPrice2) {
00325             $iRes = 1;
00326         } else {
00327             $iRes = -1;
00328         }
00329 
00330         return $iRes;
00331     }
00332 
00341     public static function percent($dValue, $dPercent)
00342     {
00343         return ((double) $dValue * (double) $dPercent)/100.0;
00344     }
00345 
00358     public static function brutto2Netto($dBrutto, $dVat)
00359     {
00360         // if VAT = -100% Return 0 because we subtract all what we have.
00361         // made to avoid division by zero in formula.
00362         if ($dVat == -100) {
00363             return 0;
00364         }
00365 
00366         return (double) ((double) $dBrutto*100.0)/(100.0 + (double) $dVat);
00367     }
00368 
00379     public static function netto2Brutto($dNetto, $dVat)
00380     {
00381         return (double) $dNetto + self::percent($dNetto, $dVat);
00382     }
00383 
00396     protected function _recalculate()
00397     {
00398         if ( $this->_blNetPriceMode ) {
00399             $this->_dBrutto = self::netto2Brutto($this->_dNetto, $this->_dVat);
00400             $this->_dNetto = oxRegistry::getUtils()->fRound($this->_dNetto);
00401         } else {
00402             $this->_dNetto  = self::brutto2Netto($this->_dBrutto, $this->_dVat);
00403             $this->_dBrutto = oxRegistry::getUtils()->fRound($this->_dBrutto);
00404         }
00405     }
00406 
00414     public static function getPriceInActCurrency( $dPrice )
00415     {
00416         $oCur = oxRegistry::getConfig()->getActShopCurrencyObject();
00417         return ( ( double ) $dPrice ) * $oCur->rate;
00418     }
00419 
00420 
00429     public function setDiscount( $dValue, $sType )
00430     {
00431         $this->_aDiscounts[] = array( 'value' => $dValue, 'type' => $sType );
00432     }
00433 
00439     public function getDiscounts()
00440     {
00441         return $this->_aDiscounts;
00442     }
00443 
00449     protected function _flushDiscounts()
00450     {
00451         $this->_aDiscounts = null;
00452     }
00453 
00459     public function calculateDiscount()
00460     {
00461         $dPrice = $this->getPrice();
00462         $aDiscounts = $this->getDiscounts();
00463 
00464         if ($aDiscounts) {
00465             foreach ($aDiscounts as $aDiscount) {
00466 
00467                 if ($aDiscount['type'] == 'abs') {
00468                     $dPrice = $dPrice - $aDiscount['value'];
00469                 } else {
00470                     $dPrice = $dPrice * (100 - $aDiscount['value']) / 100;
00471                 }
00472             }
00473             if ($dPrice < 0 ) {
00474                 $this->setPrice( 0 );
00475             } else {
00476                 $this->setPrice( $dPrice );
00477             }
00478 
00479             $this->_flushDiscounts();
00480         }
00481     }
00482 
00483 }