OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxvatselector.php
Go to the documentation of this file.
1 <?php
2 
7 class oxVatSelector extends oxSuperCfg
8 {
13  protected $_blCatVatSet = null;
14 
20  protected static $_instance = null;
21 
29  public static function getInstance()
30  {
31  return oxRegistry::get("oxVatSelector");
32  }
33 
39  protected static $_aUserVatCache = array();
40 
50  public function getUserVat( oxUser $oUser, $blCacheReset = false )
51  {
52  if (!$blCacheReset) {
53  $sId = $oUser->getId();
54  if ( array_key_exists( $sId, self::$_aUserVatCache ) &&
55  self::$_aUserVatCache[$sId] !== null) {
56  return self::$_aUserVatCache[$sId];
57  }
58  }
59 
60  $ret = false;
61 
62  $sCountryId = $this->_getVatCountry($oUser);
63 
64  if ($sCountryId) {
65  $oCountry = oxNew('oxcountry');
66  if (!$oCountry->load($sCountryId)) {
67  throw oxNew( "oxObjectException" );
68  }
69  if ($oCountry->isForeignCountry()) {
70  $ret = $this->_getForeignCountryUserVat($oUser, $oCountry);
71  }
72  }
73 
74  self::$_aUserVatCache[$oUser->getId()] = $ret;
75  return $ret;
76  }
77 
86  protected function _getForeignCountryUserVat(oxUser $oUser, oxCountry $oCountry )
87  {
88  if ($oCountry->isInEU()) {
89  if ($oUser->oxuser__oxustid->value) {
90  return 0;
91  }
92  return false;
93  }
94 
95  return 0;
96  }
97 
105  protected function _getVatForArticleCategory(oxArticle $oArticle)
106  {
107  $oDb = oxDb::getDb();
108  $sCatT = getViewName('oxcategories');
109 
110  if ( $this->_blCatVatSet === null ) {
111  $sSelect = "SELECT oxid FROM $sCatT WHERE oxvat IS NOT NULL LIMIT 1";
112 
113  //no category specific vats in shop?
114  //then for performance reasons we just return false
115  $this->_blCatVatSet = (bool) $oDb->getOne( $sSelect );
116  }
117 
118  if ( !$this->_blCatVatSet ) {
119  return false;
120  }
121 
122  $sO2C = getViewName('oxobject2category');
123  $sSql = "SELECT c.oxvat
124  FROM $sCatT AS c, $sO2C AS o2c
125  WHERE c.oxid=o2c.oxcatnid AND
126  o2c.oxobjectid = ".$oDb->quote( $oArticle->getId() )." AND
127  c.oxvat IS NOT NULL
128  ORDER BY o2c.oxtime ";
129 
130  $fVat = $oDb->getOne($sSql);
131  if ($fVat !== false && $fVat !== null) {
132  return $fVat;
133  }
134 
135  return false;
136  }
137 
145  public function getArticleVat(oxArticle $oArticle)
146  {
147  startProfile("_assignPriceInternal");
148  // article has its own VAT ?
149 
150  if ( ( $dArticleVat = $oArticle->getCustomVAT() ) !== null ) {
151  stopProfile("_assignPriceInternal");
152  return $dArticleVat;
153  }
154  if ( ( $dArticleVat = $this->_getVatForArticleCategory($oArticle) ) !== false ) {
155  stopProfile("_assignPriceInternal");
156  return $dArticleVat;
157  }
158 
159  stopProfile("_assignPriceInternal");
160  return $this->getConfig()->getConfigParam( 'dDefaultVAT' );
161  }
162 
173  public function getBasketItemVat(oxArticle $oArticle, $oBasket )
174  {
175  return $this->getArticleVat( $oArticle );
176  }
177 
185  public function getArticleUserVat(oxArticle $oArticle)
186  {
187  if ( ( $oUser = $oArticle->getArticleUser() ) ) {
188  return $this->getUserVat( $oUser );
189  }
190  return false;
191  }
192 
193 
202  protected function _getVatCountry(oxUser $oUser = null)
203  {
204  $blUseShippingCountry = $this->getConfig()->getConfigParam("blShippingCountryVat");
205 
206  if ($blUseShippingCountry) {
207  $aAddresses = $oUser->getUserAddresses($oUser->getId());
208  $sSelectedAddress = $oUser->getSelectedAddressId();
209 
210  if (isset($aAddresses[$sSelectedAddress])) {
211  return $aAddresses[$sSelectedAddress]->oxaddress__oxcountryid->value;
212  }
213  }
214 
215  return $oUser->oxuser__oxcountryid->value;
216  }
217 
218 }