OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxvatselector.php
Go to the documentation of this file.
1 <?php
2 
7 class oxVatSelector extends oxSuperCfg
8 {
9 
15  protected $_blCatVatSet = null;
16 
22  protected static $_aUserVatCache = array();
23 
33  public function getUserVat(oxUser $oUser, $blCacheReset = false)
34  {
35  if (!$blCacheReset) {
36  $sId = $oUser->getId();
37  if (array_key_exists($sId, self::$_aUserVatCache) &&
38  self::$_aUserVatCache[$sId] !== null
39  ) {
40  return self::$_aUserVatCache[$sId];
41  }
42  }
43 
44  $ret = false;
45 
46  $sCountryId = $this->_getVatCountry($oUser);
47 
48  if ($sCountryId) {
49  $oCountry = oxNew('oxcountry');
50  if (!$oCountry->load($sCountryId)) {
51  throw oxNew("oxObjectException");
52  }
53  if ($oCountry->isForeignCountry()) {
54  $ret = $this->_getForeignCountryUserVat($oUser, $oCountry);
55  }
56  }
57 
58  self::$_aUserVatCache[$oUser->getId()] = $ret;
59 
60  return $ret;
61  }
62 
71  protected function _getForeignCountryUserVat(oxUser $oUser, oxCountry $oCountry)
72  {
73  if ($oCountry->isInEU()) {
74  if ($oUser->oxuser__oxustid->value) {
75  return 0;
76  }
77 
78  return false;
79  }
80 
81  return 0;
82  }
83 
91  protected function _getVatForArticleCategory(oxArticle $oArticle)
92  {
93  $oDb = oxDb::getDb();
94  $sCatT = getViewName('oxcategories');
95 
96  if ($this->_blCatVatSet === null) {
97  $sSelect = "SELECT oxid FROM $sCatT WHERE oxvat IS NOT NULL LIMIT 1";
98 
99  //no category specific vats in shop?
100  //then for performance reasons we just return false
101  $this->_blCatVatSet = (bool) $oDb->getOne($sSelect);
102  }
103 
104  if (!$this->_blCatVatSet) {
105  return false;
106  }
107 
108  $sO2C = getViewName('oxobject2category');
109  $sSql = "SELECT c.oxvat
110  FROM $sCatT AS c, $sO2C AS o2c
111  WHERE c.oxid=o2c.oxcatnid AND
112  o2c.oxobjectid = " . $oDb->quote($oArticle->getId()) . " AND
113  c.oxvat IS NOT NULL
114  ORDER BY o2c.oxtime ";
115 
116  $fVat = $oDb->getOne($sSql);
117  if ($fVat !== false && $fVat !== null) {
118  return $fVat;
119  }
120 
121  return false;
122  }
123 
131  public function getArticleVat(oxArticle $oArticle)
132  {
133  startProfile("_assignPriceInternal");
134  // article has its own VAT ?
135 
136  if (($dArticleVat = $oArticle->getCustomVAT()) !== null) {
137  stopProfile("_assignPriceInternal");
138 
139  return $dArticleVat;
140  }
141  if (($dArticleVat = $this->_getVatForArticleCategory($oArticle)) !== false) {
142  stopProfile("_assignPriceInternal");
143 
144  return $dArticleVat;
145  }
146 
147  stopProfile("_assignPriceInternal");
148 
149  return $this->getConfig()->getConfigParam('dDefaultVAT');
150  }
151 
162  public function getBasketItemVat(oxArticle $oArticle, $oBasket)
163  {
164  return $this->getArticleVat($oArticle);
165  }
166 
174  public function getArticleUserVat(oxArticle $oArticle)
175  {
176  if (($oUser = $oArticle->getArticleUser())) {
177  return $this->getUserVat($oUser);
178  }
179 
180  return false;
181  }
182 
183 
192  protected function _getVatCountry(oxUser $oUser)
193  {
194  $blUseShippingCountry = $this->getConfig()->getConfigParam("blShippingCountryVat");
195 
196  if ($blUseShippingCountry) {
197  $aAddresses = $oUser->getUserAddresses($oUser->getId());
198  $sSelectedAddress = $oUser->getSelectedAddressId();
199 
200  if (isset($aAddresses[$sSelectedAddress])) {
201  return $aAddresses[$sSelectedAddress]->oxaddress__oxcountryid->value;
202  }
203  }
204 
205  return $oUser->oxuser__oxcountryid->value;
206  }
207 }