oxvatselector.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxVatSelector extends oxSuperCfg
00008 {
00009 
00010 
00016     protected static $_instance = null;
00017 
00023     public static function getInstance()
00024     {
00025         if ( !self::$_instance instanceof oxVatSelector ) {
00026                 self::$_instance = oxNew('oxVatSelector');
00027         }
00028         return self::$_instance;
00029     }
00030 
00036     protected static $_aUserVatCache = array();
00037 
00047     public function getUserVat(oxUser $oUser, $blCacheReset = false)
00048     {
00049         if (!$blCacheReset) {
00050             if (self::$_aUserVatCache[$oUser->getId()] !== null) {
00051                 return self::$_aUserVatCache[$oUser->getId()];
00052             }
00053         }
00054 
00055         $ret = false;
00056 
00057         $sCountryId = $this->_getVatCountry($oUser);
00058 
00059         if ($sCountryId) {
00060             $oCountry = oxNew('oxcountry');
00061             if (!$oCountry->load($sCountryId)) {
00062                 throw new oxObjectException();
00063             }
00064             if ($oCountry->isForeignCountry()) {
00065                 $ret = $this->_getForeignCountryUserVat($oUser, $oCountry);
00066             }
00067         }
00068 
00069         self::$_aUserVatCache[$oUser->getId()] = $ret;
00070         return $ret;
00071     }
00072 
00081     protected function _getForeignCountryUserVat(oxUser $oUser, oxCountry $oCountry )
00082     {
00083         if ($oCountry->isInEU()) {
00084             if ($oUser->oxuser__oxustid->value) {
00085                 return 0;
00086             }
00087             return false;
00088         }
00089 
00090         return 0;
00091     }
00092 
00100     protected function _getVatForArticleCategory(oxArticle $oArticle)
00101     {
00102         //return false;
00103         //if (count($aCats)) {
00104         //$sMainCat  = $aCats[0];
00105         //$aCats = $oArticle->getCategoryIds();
00106 
00107         $oDb = oxDb::getDb();
00108         $sCatT = getViewName('oxcategories');
00109         $sSelect = "SELECT oxid
00110                     FROM $sCatT
00111                     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         $iCount = $oDb->getOne($sSelect);
00116         if (!$iCount) {
00117             return false;
00118         }
00119 
00120         $sO2C = getViewName('oxobject2category');
00121         $sSql = "SELECT c.oxvat
00122                  FROM $sCatT AS c, $sO2C AS o2c
00123                  WHERE c.oxid=o2c.oxcatnid AND
00124                        o2c.oxobjectid = '".$oArticle->getId()."' AND
00125                        c.oxvat IS NOT NULL
00126                  ORDER BY o2c.oxtime ";
00127 
00128         //echo $sSql."<br>";
00129 
00130         $fVat = $oDb->getOne($sSql);
00131         if ($fVat !== false && $fVat !== null) {
00132             return $fVat;
00133         }
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 }