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 if ($sCountryId = $oUser->oxuser__oxcountryid->value) {
00058 $oCountry = oxNew('oxcountry');
00059 if (!$oCountry->load($sCountryId)) {
00060 throw new oxObjectException();
00061 }
00062 if ($oCountry->isForeignCountry()) {
00063 $ret = $this->_getForeignCountryUserVat($oUser, $oCountry);
00064 }
00065 }
00066
00067 self::$_aUserVatCache[$oUser->getId()] = $ret;
00068 return $ret;
00069 }
00070
00079 protected function _getForeignCountryUserVat(oxUser $oUser, oxCountry $oCountry )
00080 {
00081 if ($oCountry->isInEU()) {
00082 if ($oUser->oxuser__oxustid->value && $oUser->oxuser__oxcompany->value) {
00083 return 0;
00084 }
00085 return false;
00086 }
00087
00088 return 0;
00089 }
00090
00098 protected function _getVatForArticleCategory(oxArticle $oArticle)
00099 {
00100
00101
00102
00103
00104
00105 $oDb = oxDb::getDb();
00106 $sCatT = getViewName('oxcategories');
00107 $sSelect = "SELECT oxid
00108 FROM $sCatT
00109 WHERE oxvat IS NOT NULL LIMIT 1";
00110
00111
00112
00113 $iCount = $oDb->getOne($sSelect);
00114 if (!$iCount) {
00115 return false;
00116 }
00117
00118 $sO2C = getViewName('oxobject2category');
00119 $sSql = "SELECT c.oxvat
00120 FROM $sCatT AS c, $sO2C AS o2c
00121 WHERE c.oxid=o2c.oxcatnid AND
00122 o2c.oxobjectid = '".$oArticle->getId()."' AND
00123 c.oxvat IS NOT NULL
00124 ORDER BY o2c.oxtime ";
00125
00126
00127
00128 $fVat = $oDb->getOne($sSql);
00129 if ($fVat !== false && $fVat !== null) {
00130 return $fVat;
00131 }
00132
00133
00134 return false;
00135 }
00136
00144 public function getArticleVat(oxArticle $oArticle)
00145 {
00146 startProfile("_assignPriceInternal");
00147
00148
00149 if ( ( $dArticleVat = $oArticle->getCustomVAT() ) !== null ) {
00150 stopProfile("_assignPriceInternal");
00151 return $dArticleVat;
00152 }
00153 if ( ( $dArticleVat = $this->_getVatForArticleCategory($oArticle) ) !== false ) {
00154 stopProfile("_assignPriceInternal");
00155 return $dArticleVat;
00156 }
00157
00158 stopProfile("_assignPriceInternal");
00159 return $this->getConfig()->getConfigParam( 'dDefaultVAT' );
00160 }
00161
00172 public function getBasketItemVat(oxArticle $oArticle, $oBasket )
00173 {
00174 return $this->getArticleVat( $oArticle );
00175 }
00176
00184 public function getArticleUserVat(oxArticle $oArticle)
00185 {
00186 if ( ( $oUser = $oArticle->getArticleUser() ) ) {
00187 return $this->getUserVat( $oUser );
00188 }
00189 return false;
00190 }
00191
00192 }