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