oxvatselector.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxVatSelector extends oxSuperCfg
00008 {
00009 
00015     protected $_blCatVatSet = null;
00016 
00022     protected static $_aUserVatCache = array();
00023 
00033     public function getUserVat(oxUser $oUser, $blCacheReset = false)
00034     {
00035         if (!$blCacheReset) {
00036             $sId = $oUser->getId();
00037             if (array_key_exists($sId, self::$_aUserVatCache) &&
00038                 self::$_aUserVatCache[$sId] !== null
00039             ) {
00040                 return self::$_aUserVatCache[$sId];
00041             }
00042         }
00043 
00044         $ret = false;
00045 
00046         $sCountryId = $this->_getVatCountry($oUser);
00047 
00048         if ($sCountryId) {
00049             $oCountry = oxNew('oxcountry');
00050             if (!$oCountry->load($sCountryId)) {
00051                 throw oxNew("oxObjectException");
00052             }
00053             if ($oCountry->isForeignCountry()) {
00054                 $ret = $this->_getForeignCountryUserVat($oUser, $oCountry);
00055             }
00056         }
00057 
00058         self::$_aUserVatCache[$oUser->getId()] = $ret;
00059 
00060         return $ret;
00061     }
00062 
00071     protected function _getForeignCountryUserVat(oxUser $oUser, oxCountry $oCountry)
00072     {
00073         if ($oCountry->isInEU()) {
00074             if ($oUser->oxuser__oxustid->value) {
00075                 return 0;
00076             }
00077 
00078             return false;
00079         }
00080 
00081         return 0;
00082     }
00083 
00091     protected function _getVatForArticleCategory(oxArticle $oArticle)
00092     {
00093         $oDb = oxDb::getDb();
00094         $sCatT = getViewName('oxcategories');
00095 
00096         if ($this->_blCatVatSet === null) {
00097             $sSelect = "SELECT oxid FROM $sCatT WHERE oxvat IS NOT NULL LIMIT 1";
00098 
00099             //no category specific vats in shop?
00100             //then for performance reasons we just return false
00101             $this->_blCatVatSet = (bool) $oDb->getOne($sSelect);
00102         }
00103 
00104         if (!$this->_blCatVatSet) {
00105             return false;
00106         }
00107 
00108         $sO2C = getViewName('oxobject2category');
00109         $sSql = "SELECT c.oxvat
00110                  FROM $sCatT AS c, $sO2C AS o2c
00111                  WHERE c.oxid=o2c.oxcatnid AND
00112                        o2c.oxobjectid = " . $oDb->quote($oArticle->getId()) . " AND
00113                        c.oxvat IS NOT NULL
00114                  ORDER BY o2c.oxtime ";
00115 
00116         $fVat = $oDb->getOne($sSql);
00117         if ($fVat !== false && $fVat !== null) {
00118             return $fVat;
00119         }
00120 
00121         return false;
00122     }
00123 
00131     public function getArticleVat(oxArticle $oArticle)
00132     {
00133         startProfile("_assignPriceInternal");
00134         // article has its own VAT ?
00135 
00136         if (($dArticleVat = $oArticle->getCustomVAT()) !== null) {
00137             stopProfile("_assignPriceInternal");
00138 
00139             return $dArticleVat;
00140         }
00141         if (($dArticleVat = $this->_getVatForArticleCategory($oArticle)) !== false) {
00142             stopProfile("_assignPriceInternal");
00143 
00144             return $dArticleVat;
00145         }
00146 
00147         stopProfile("_assignPriceInternal");
00148 
00149         return $this->getConfig()->getConfigParam('dDefaultVAT');
00150     }
00151 
00162     public function getBasketItemVat(oxArticle $oArticle, $oBasket)
00163     {
00164         return $this->getArticleVat($oArticle);
00165     }
00166 
00174     public function getArticleUserVat(oxArticle $oArticle)
00175     {
00176         if (($oUser = $oArticle->getArticleUser())) {
00177             return $this->getUserVat($oUser);
00178         }
00179 
00180         return false;
00181     }
00182 
00183 
00192     protected function _getVatCountry(oxUser $oUser)
00193     {
00194         $blUseShippingCountry = $this->getConfig()->getConfigParam("blShippingCountryVat");
00195 
00196         if ($blUseShippingCountry) {
00197             $aAddresses = $oUser->getUserAddresses($oUser->getId());
00198             $sSelectedAddress = $oUser->getSelectedAddressId();
00199 
00200             if (isset($aAddresses[$sSelectedAddress])) {
00201                 return $aAddresses[$sSelectedAddress]->oxaddress__oxcountryid->value;
00202             }
00203         }
00204 
00205         return $oUser->oxuser__oxcountryid->value;
00206     }
00207 }