OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxpayment.php
Go to the documentation of this file.
1 <?php
2 
8 class oxPayment extends oxI18n
9 {
10 
17 
24 
31 
38 
45 
51  protected $_oGroups = null;
52 
59  protected $_aCountries = null;
60 
66  protected $_sClassName = 'oxpayment';
67 
73  protected $_aDynValues = null;
74 
80  protected $_iPaymentError = null;
81 
87  protected $_blPaymentVatOnTop = false;
88 
94  protected $_oPrice = null;
95 
99  public function __construct()
100  {
101  $this->setPaymentVatOnTop($this->getConfig()->getConfigParam('blPaymentVatOnTop'));
103  $this->init('oxpayments');
104  }
105 
111  public function setPaymentVatOnTop($blOnTop)
112  {
113  $this->_blPaymentVatOnTop = $blOnTop;
114  }
115 
121  public function getGroups()
122  {
123  if ($this->_oGroups == null && ($sOxid = $this->getId())) {
124 
125  // user groups
126  $this->_oGroups = oxNew('oxlist', 'oxgroups');
127  $sViewName = getViewName("oxgroups", $this->getLanguage());
128 
129  // performance
130  $sSelect = "select {$sViewName}.* from {$sViewName}, oxobject2group
131  where oxobject2group.oxobjectid = '{$sOxid}'
132  and oxobject2group.oxgroupsid={$sViewName}.oxid ";
133  $this->_oGroups->selectString($sSelect);
134  }
135 
136  return $this->_oGroups;
137  }
138 
144  public function setDynValues($aDynValues)
145  {
146  $this->_aDynValues = $aDynValues;
147  }
148 
155  public function setDynValue($oKey, $oVal)
156  {
157  $this->_aDynValues[$oKey] = $oVal;
158  }
159 
165  public function getDynValues()
166  {
167  if (!$this->_aDynValues) {
168  $sRawDynValue = null;
169  if (is_object($this->oxpayments__oxvaldesc)) {
170  $sRawDynValue = $this->oxpayments__oxvaldesc->getRawValue();
171  }
172 
173  $this->_aDynValues = oxRegistry::getUtils()->assignValuesFromText($sRawDynValue);
174  }
175 
176  return $this->_aDynValues;
177  }
178 
186  public function getPaymentValue($dBasePrice)
187  {
188  $dRet = 0;
189 
190  if ($this->oxpayments__oxaddsumtype->value == "%") {
191  $dRet = $dBasePrice * $this->oxpayments__oxaddsum->value / 100;
192  } else {
193  $oCur = $this->getConfig()->getActShopCurrencyObject();
194  $dRet = $this->oxpayments__oxaddsum->value * $oCur->rate;
195  }
196 
197  if (($dRet * -1) > $dBasePrice) {
198  $dRet = $dBasePrice;
199  }
200 
201  return $dRet;
202  }
203 
212  public function getBaseBasketPriceForPaymentCostCalc($oBasket)
213  {
214  $dBasketPrice = 0;
215  $iRules = $this->oxpayments__oxaddsumrules->value;
216 
217  // products brutto price
218  if (!$iRules || ($iRules & self::PAYMENT_ADDSUMRULE_ALLGOODS)) {
219  $dBasketPrice += $oBasket->getProductsPrice()->getSum($oBasket->isCalculationModeNetto());
220  }
221 
222  // discounts
223  if ((!$iRules || ($iRules & self::PAYMENT_ADDSUMRULE_DISCOUNTS)) &&
224  ($oCosts = $oBasket->getTotalDiscount())
225  ) {
226  $dBasketPrice -= $oCosts->getPrice();
227  }
228 
229  // vouchers
230  if (!$iRules || ($iRules & self::PAYMENT_ADDSUMRULE_VOUCHERS)) {
231  $dBasketPrice -= $oBasket->getVoucherDiscValue();
232  }
233 
234  // delivery
235  if ((!$iRules || ($iRules & self::PAYMENT_ADDSUMRULE_SHIPCOSTS)) &&
236  ($oCosts = $oBasket->getCosts('oxdelivery'))
237  ) {
238  if ($oBasket->isCalculationModeNetto()) {
239  $dBasketPrice += $oCosts->getNettoPrice();
240  } else {
241  $dBasketPrice += $oCosts->getBruttoPrice();
242  }
243 
244  }
245 
246  // wrapping
247  if (($iRules & self::PAYMENT_ADDSUMRULE_GIFTS) &&
248  ($oCosts = $oBasket->getCosts('oxwrapping'))
249  ) {
250  if ($oBasket->isCalculationModeNetto()) {
251  $dBasketPrice += $oCosts->getNettoPrice();
252  } else {
253  $dBasketPrice += $oCosts->getBruttoPrice();
254  }
255  }
256 
257  // gift card
258  if (($iRules & self::PAYMENT_ADDSUMRULE_GIFTS) &&
259  ($oCosts = $oBasket->getCosts('oxgiftcard'))
260  ) {
261 
262  if ($oBasket->isCalculationModeNetto()) {
263  $dBasketPrice += $oCosts->getNettoPrice();
264  } else {
265  $dBasketPrice += $oCosts->getBruttoPrice();
266  }
267  }
268 
269  return $dBasketPrice;
270  }
271 
277  public function calculate($oBasket)
278  {
279  //getting basket price with applied discounts and vouchers
280  $dPrice = $this->getPaymentValue($this->getBaseBasketPriceForPaymentCostCalc($oBasket));
281 
282  if ($dPrice) {
283  // calculating total price
284  $oPrice = oxNew('oxPrice');
285  $oPrice->setNettoMode($this->_blPaymentVatOnTop);
286 
287  $oPrice->setPrice($dPrice);
288  if ($dPrice > 0) {
289  $oPrice->setVat($oBasket->getAdditionalServicesVatPercent());
290  }
291 
292  $this->_oPrice = $oPrice;
293  }
294 
295  }
296 
302  public function getPrice()
303  {
304  return $this->_oPrice;
305  }
306 
314  public function getFNettoPrice()
315  {
316  if ($this->getPrice()) {
317  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getNettoPrice());
318  }
319  }
320 
328  public function getFBruttoPrice()
329  {
330  if ($this->getPrice()) {
331  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getBruttoPrice());
332  }
333  }
334 
342  public function getFPriceVat()
343  {
344  if ($this->getPrice()) {
345  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getVatValue());
346  }
347  }
348 
354  public function getCountries()
355  {
356  if ($this->_aCountries === null) {
357  $oDb = oxDb::getDb();
358  $this->_aCountries = array();
359  $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid=' . $oDb->quote($this->getId()) . ' and oxtype = "oxcountry" ';
360  $rs = $oDb->getCol($sSelect);
361  $this->_aCountries = $rs;
362  }
363 
364  return $this->_aCountries;
365  }
366 
374  public function delete($sOxId = null)
375  {
376  if (parent::delete($sOxId)) {
377 
378  $sOxId = $sOxId ? $sOxId : $this->getId();
379  $oDb = oxDb::getDb();
380 
381  // deleting payment related data
382  $rs = $oDb->execute("delete from oxobject2payment where oxpaymentid = " . $oDb->quote($sOxId));
383 
384  return $rs->EOF;
385  }
386 
387  return false;
388  }
389 
401  public function isValidPayment($aDynValue, $sShopId, $oUser, $dBasketPrice, $sShipSetId)
402  {
403  $myConfig = $this->getConfig();
404  if ($this->oxpayments__oxid->value == 'oxempty') {
405  // inactive or blOtherCountryOrder is off
406  if (!$this->oxpayments__oxactive->value || !$myConfig->getConfigParam("blOtherCountryOrder")) {
407  $this->_iPaymentError = -2;
408 
409  return false;
410  }
411  if (count(
412  oxRegistry::get("oxDeliverySetList")
413  ->getDeliverySetList(
414  $oUser,
415  $oUser->getActiveCountry()
416  )
417  )
418  ) {
419  $this->_iPaymentError = -3;
420 
421  return false;
422  }
423 
424  return true;
425  }
426 
427  $mxValidationResult = oxRegistry::get("oxInputValidator")->validatePaymentInputData($this->oxpayments__oxid->value, $aDynValue);
428 
429  if (is_integer($mxValidationResult)) {
430  $this->_iPaymentError = $mxValidationResult;
431 
432  return false;
433  } elseif ($mxValidationResult === false) {
434  $this->_iPaymentError = 1;
435 
436  return false;
437  }
438 
439  $oCur = $myConfig->getActShopCurrencyObject();
440  $dBasketPrice = $dBasketPrice / $oCur->rate;
441 
442  if ($sShipSetId) {
443  $aPaymentList = oxRegistry::get("oxPaymentList")->getPaymentList($sShipSetId, $dBasketPrice, $oUser);
444 
445  if (!array_key_exists($this->getId(), $aPaymentList)) {
446  $this->_iPaymentError = -3;
447 
448  return false;
449  }
450  } else {
451  $this->_iPaymentError = -2;
452 
453  return false;
454  }
455 
456  return true;
457  }
458 
464  public function getPaymentErrorNumber()
465  {
466  return $this->_iPaymentError;
467  }
468 }