OXID eShop CE  4.10.7
 All Classes Namespaces 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  } else {
294  // no payment costs
295  $oPrice = oxNew('oxPrice');
296  $oPrice->setPrice(0);
297  $this->_oPrice = $oPrice;
298  }
299 
300  }
301 
307  public function getPrice()
308  {
309  return $this->_oPrice;
310  }
311 
319  public function getFNettoPrice()
320  {
321  if ($this->getPrice()) {
322  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getNettoPrice());
323  }
324  }
325 
333  public function getFBruttoPrice()
334  {
335  if ($this->getPrice()) {
336  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getBruttoPrice());
337  }
338  }
339 
347  public function getFPriceVat()
348  {
349  if ($this->getPrice()) {
350  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getVatValue());
351  }
352  }
353 
359  public function getCountries()
360  {
361  if ($this->_aCountries === null) {
362  $oDb = oxDb::getDb();
363  $this->_aCountries = array();
364  $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid=' . $oDb->quote($this->getId()) . ' and oxtype = "oxcountry" ';
365  $rs = $oDb->getCol($sSelect);
366  $this->_aCountries = $rs;
367  }
368 
369  return $this->_aCountries;
370  }
371 
379  public function delete($sOxId = null)
380  {
381  if (parent::delete($sOxId)) {
382 
383  $sOxId = $sOxId ? $sOxId : $this->getId();
384  $oDb = oxDb::getDb();
385 
386  // deleting payment related data
387  $rs = $oDb->execute("delete from oxobject2payment where oxpaymentid = " . $oDb->quote($sOxId));
388 
389  return $rs->EOF;
390  }
391 
392  return false;
393  }
394 
406  public function isValidPayment($aDynValue, $sShopId, $oUser, $dBasketPrice, $sShipSetId)
407  {
408  $myConfig = $this->getConfig();
409  if ($this->oxpayments__oxid->value == 'oxempty') {
410  // inactive or blOtherCountryOrder is off
411  if (!$this->oxpayments__oxactive->value || !$myConfig->getConfigParam("blOtherCountryOrder")) {
412  $this->_iPaymentError = -2;
413 
414  return false;
415  }
416  if (count(
417  oxRegistry::get("oxDeliverySetList")
418  ->getDeliverySetList(
419  $oUser,
420  $oUser->getActiveCountry()
421  )
422  )
423  ) {
424  $this->_iPaymentError = -3;
425 
426  return false;
427  }
428 
429  return true;
430  }
431 
432  $mxValidationResult = oxRegistry::get("oxInputValidator")->validatePaymentInputData($this->oxpayments__oxid->value, $aDynValue);
433 
434  if (is_integer($mxValidationResult)) {
435  $this->_iPaymentError = $mxValidationResult;
436 
437  return false;
438  } elseif ($mxValidationResult === false) {
439  $this->_iPaymentError = 1;
440 
441  return false;
442  }
443 
444  $oCur = $myConfig->getActShopCurrencyObject();
445  $dBasketPrice = $dBasketPrice / $oCur->rate;
446 
447  if ($sShipSetId) {
448  $aPaymentList = oxRegistry::get("oxPaymentList")->getPaymentList($sShipSetId, $dBasketPrice, $oUser);
449 
450  if (!array_key_exists($this->getId(), $aPaymentList)) {
451  $this->_iPaymentError = -3;
452 
453  return false;
454  }
455  } else {
456  $this->_iPaymentError = -2;
457 
458  return false;
459  }
460 
461  return true;
462  }
463 
469  public function getPaymentErrorNumber()
470  {
471  return $this->_iPaymentError;
472  }
473 }