oxprice.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxPrice
00007 {
00008 
00014     protected $_dBrutto = 0.0;
00015 
00021     protected $_dNetto = 0.0;
00022 
00028     protected $_dVat = 0.0;
00029 
00030 
00036     protected $_aDiscounts = null;
00037 
00038 
00047     protected $_blNetPriceMode;
00048 
00056     public function __construct($dPrice = null)
00057     {
00058         $this->setNettoMode(oxRegistry::getConfig()->getConfigParam('blEnterNetPrice'));
00059 
00060         if (!is_null($dPrice)) {
00061             $this->setPrice($dPrice);
00062         }
00063     }
00064 
00070     public function setNettoMode($blNetto = true)
00071     {
00072         $this->_blNetPriceMode = $blNetto;
00073     }
00074 
00080     public function isNettoMode()
00081     {
00082         return $this->_blNetPriceMode;
00083     }
00084 
00088     public function setNettoPriceMode()
00089     {
00090         $this->setNettoMode();
00091     }
00092 
00096     public function setBruttoPriceMode()
00097     {
00098         $this->setNettoMode(false);
00099     }
00100 
00106     public function setVat($dVat)
00107     {
00108         $this->_dVat = (double) $dVat;
00109     }
00110 
00121     public function setUserVat($newVat)
00122     {
00123         if (!$this->isNettoMode() && $newVat != $this->_dVat) {
00124             $this->_dBrutto = self::Netto2Brutto(self::Brutto2Netto($this->_dBrutto, $this->_dVat), (double) $newVat);
00125         }
00126         $this->_dVat = (double) $newVat;
00127     }
00128 
00134     public function getVat()
00135     {
00136         return $this->_dVat;
00137     }
00138 
00146     public function setPrice($dPrice, $dVat = null)
00147     {
00148         if (!is_null($dVat)) {
00149             $this->setVat($dVat);
00150         }
00151 
00152         if ($this->isNettoMode()) {
00153             $this->_dNetto = $dPrice;
00154         } else {
00155             $this->_dBrutto = $dPrice;
00156         }
00157     }
00158 
00164     public function getPrice()
00165     {
00166         if ($this->isNettoMode()) {
00167             return $this->getNettoPrice();
00168         } else {
00169             return $this->getBruttoPrice();
00170         }
00171     }
00172 
00178     public function getBruttoPrice()
00179     {
00180         if ($this->isNettoMode()) {
00181             return $this->getNettoPrice() + $this->getVatValue();
00182         } else {
00183             return oxRegistry::getUtils()->fRound($this->_dBrutto);
00184         }
00185     }
00186 
00192     public function getNettoPrice()
00193     {
00194         if ($this->isNettoMode()) {
00195             return oxRegistry::getUtils()->fRound($this->_dNetto);
00196         } else {
00197             return $this->getBruttoPrice() - $this->getVatValue();
00198         }
00199     }
00200 
00206     public function getVatValue()
00207     {
00208         if ($this->isNettoMode()) {
00209             $dVatValue = $this->getNettoPrice() * $this->getVat() / 100;
00210         } else {
00211             $dVatValue = $this->getBruttoPrice() * $this->getVat() / (100 + $this->getVat());
00212         }
00213 
00214         return oxRegistry::getUtils()->fRound($dVatValue);
00215     }
00216 
00223     public function subtractPercent($dValue)
00224     {
00225         $dPrice = $this->getPrice();
00226         $this->setPrice($dPrice - self::percent($dPrice, $dValue));
00227     }
00228 
00235     public function addPercent($dValue)
00236     {
00237         $this->subtractPercent(-$dValue);
00238     }
00239 
00245     public function addPrice(oxPrice $oPrice)
00246     {
00247         if ($this->isNettoMode()) {
00248             $this->add($oPrice->getNettoPrice());
00249         } else {
00250             $this->add($oPrice->getBruttoPrice());
00251         }
00252     }
00253 
00260     public function add($dValue)
00261     {
00262         $dPrice = $this->getPrice();
00263         $this->setPrice($dPrice + $dValue);
00264     }
00265 
00272     public function subtract($dValue)
00273     {
00274         $this->add(-$dValue);
00275     }
00276 
00283     public function multiply($dValue)
00284     {
00285         $dPrice = $this->getPrice();
00286         $this->setPrice($dPrice * $dValue);
00287     }
00288 
00295     public function divide($dValue)
00296     {
00297         $dPrice = $this->getPrice();
00298         $this->setPrice($dPrice / $dValue);
00299     }
00300 
00312     public function compare(oxPrice $oPrice)
00313     {
00314         $dBruttoPrice1 = $this->getBruttoPrice();
00315         $dBruttoPrice2 = $oPrice->getBruttoPrice();
00316 
00317         if ($dBruttoPrice1 == $dBruttoPrice2) {
00318             $iRes = 0;
00319         } elseif ($dBruttoPrice1 > $dBruttoPrice2) {
00320             $iRes = 1;
00321         } else {
00322             $iRes = -1;
00323         }
00324 
00325         return $iRes;
00326     }
00327 
00336     public static function percent($dValue, $dPercent)
00337     {
00338         return ((double) $dValue * (double) $dPercent) / 100.0;
00339     }
00340 
00353     public static function brutto2Netto($dBrutto, $dVat)
00354     {
00355         // if VAT = -100% Return 0 because we subtract all what we have.
00356         // made to avoid division by zero in formula.
00357         if ($dVat == -100) {
00358             return 0;
00359         }
00360 
00361         return (double) ((double) $dBrutto * 100.0) / (100.0 + (double) $dVat);
00362     }
00363 
00374     public static function netto2Brutto($dNetto, $dVat)
00375     {
00376         return (double) $dNetto + self::percent($dNetto, $dVat);
00377     }
00378 
00386     public static function getPriceInActCurrency($dPrice)
00387     {
00388         $oCur = oxRegistry::getConfig()->getActShopCurrencyObject();
00389 
00390         return (( double ) $dPrice) * $oCur->rate;
00391     }
00392 
00393 
00400     public function setDiscount($dValue, $sType)
00401     {
00402         $this->_aDiscounts[] = array('value' => $dValue, 'type' => $sType);
00403     }
00404 
00410     public function getDiscounts()
00411     {
00412         return $this->_aDiscounts;
00413     }
00414 
00418     protected function _flushDiscounts()
00419     {
00420         $this->_aDiscounts = null;
00421     }
00422 
00426     public function calculateDiscount()
00427     {
00428         $dPrice = $this->getPrice();
00429         $aDiscounts = $this->getDiscounts();
00430 
00431         if ($aDiscounts) {
00432             foreach ($aDiscounts as $aDiscount) {
00433 
00434                 if ($aDiscount['type'] == 'abs') {
00435                     $dPrice = $dPrice - $aDiscount['value'];
00436                 } else {
00437                     $dPrice = $dPrice * (100 - $aDiscount['value']) / 100;
00438                 }
00439             }
00440             if ($dPrice < 0) {
00441                 $this->setPrice(0);
00442             } else {
00443                 $this->setPrice($dPrice);
00444             }
00445 
00446             $this->_flushDiscounts();
00447         }
00448     }
00449 }