oxvatselector.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxVatSelector extends oxSuperCfg
00008 {
00013     protected $_blCatVatSet = null;
00014 
00020     protected static $_instance = null;
00021 
00027     public static function getInstance()
00028     {
00029         if ( !self::$_instance instanceof oxVatSelector ) {
00030                 self::$_instance = oxNew('oxVatSelector');
00031         }
00032         return self::$_instance;
00033     }
00034 
00040     protected static $_aUserVatCache = array();
00041 
00051     public function getUserVat( oxUser $oUser, $blCacheReset = false )
00052     {
00053         if (!$blCacheReset) {
00054             $sId = $oUser->getId();
00055             if ( array_key_exists( $sId, self::$_aUserVatCache ) &&
00056                  self::$_aUserVatCache[$sId] !== null) {
00057                 return self::$_aUserVatCache[$sId];
00058             }
00059         }
00060 
00061         $ret = false;
00062 
00063         $sCountryId = $this->_getVatCountry($oUser);
00064 
00065         if ($sCountryId) {
00066             $oCountry = oxNew('oxcountry');
00067             if (!$oCountry->load($sCountryId)) {
00068                 throw oxNew( "oxObjectException" );
00069             }
00070             if ($oCountry->isForeignCountry()) {
00071                 $ret = $this->_getForeignCountryUserVat($oUser, $oCountry);
00072             }
00073         }
00074 
00075         self::$_aUserVatCache[$oUser->getId()] = $ret;
00076         return $ret;
00077     }
00078 
00087     protected function _getForeignCountryUserVat(oxUser $oUser, oxCountry $oCountry )
00088     {
00089         if ($oCountry->isInEU()) {
00090             if ($oUser->oxuser__oxustid->value) {
00091                 return 0;
00092             }
00093             return false;
00094         }
00095 
00096         return 0;
00097     }
00098 
00106     protected function _getVatForArticleCategory(oxArticle $oArticle)
00107     {
00108         $oDb = oxDb::getDb();
00109         $sCatT = getViewName('oxcategories');
00110 
00111         if ( $this->_blCatVatSet === null ) {
00112             $sSelect = "SELECT oxid FROM $sCatT WHERE oxvat IS NOT NULL LIMIT 1";
00113 
00114             //no category specific vats in shop?
00115             //then for performance reasons we just return false
00116             $this->_blCatVatSet = (bool) $oDb->getOne( $sSelect );
00117         }
00118 
00119         if ( !$this->_blCatVatSet ) {
00120             return false;
00121         }
00122 
00123         $sO2C = getViewName('oxobject2category');
00124         $sSql = "SELECT c.oxvat
00125                  FROM $sCatT AS c, $sO2C AS o2c
00126                  WHERE c.oxid=o2c.oxcatnid AND
00127                        o2c.oxobjectid = ".$oDb->quote( $oArticle->getId() )." AND
00128                        c.oxvat IS NOT NULL
00129                  ORDER BY o2c.oxtime ";
00130 
00131         $fVat = $oDb->getOne($sSql);
00132         if ($fVat !== false && $fVat !== null) {
00133             return $fVat;
00134         }
00135 
00136         return false;
00137     }
00138 
00146     public function getArticleVat(oxArticle $oArticle)
00147     {
00148         startProfile("_assignPriceInternal");
00149         // article has its own VAT ?
00150 
00151         if ( ( $dArticleVat = $oArticle->getCustomVAT() ) !== null ) {
00152             stopProfile("_assignPriceInternal");
00153             return $dArticleVat;
00154         }
00155         if ( ( $dArticleVat = $this->_getVatForArticleCategory($oArticle) ) !== false ) {
00156             stopProfile("_assignPriceInternal");
00157             return $dArticleVat;
00158         }
00159 
00160         stopProfile("_assignPriceInternal");
00161         return $this->getConfig()->getConfigParam( 'dDefaultVAT' );
00162     }
00163 
00174     public function getBasketItemVat(oxArticle $oArticle, $oBasket )
00175     {
00176         return $this->getArticleVat( $oArticle );
00177     }
00178 
00186     public function getArticleUserVat(oxArticle $oArticle)
00187     {
00188         if ( ( $oUser = $oArticle->getArticleUser() ) ) {
00189             return $this->getUserVat( $oUser );
00190         }
00191         return false;
00192     }
00193 
00194 
00203     protected function _getVatCountry(oxUser $oUser = null)
00204     {
00205         $blUseShippingCountry = $this->getConfig()->getConfigParam("blShippingCountryVat");
00206 
00207         if ($blUseShippingCountry) {
00208             $aAddresses = $oUser->getUserAddresses($oUser->getId());
00209             $sSelectedAddress = $oUser->getSelectedAddressId();
00210 
00211             if (isset($aAddresses[$sSelectedAddress])) {
00212                 return $aAddresses[$sSelectedAddress]->oxaddress__oxcountryid->value;
00213             }
00214         }
00215 
00216         return $oUser->oxuser__oxcountryid->value;
00217     }
00218 
00219 }