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