OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxpayment.php
Go to the documentation of this file.
1 <?php
2 
8 class oxPayment extends oxI18n
9 {
15 
21 
27 
33 
39 
45  protected $_oGroups = null;
46 
53  protected $_aCountries = null;
54 
60  protected $_sClassName = 'oxpayment';
61 
67  protected $_aDynValues = null;
68 
74  protected $_iPaymentError = null;
75 
81  protected $_blPaymentVatOnTop = false;
82 
88  protected $_oPrice = null;
89 
93  public function __construct()
94  {
95  $this->setPaymentVatOnTop( $this->getConfig()->getConfigParam( 'blPaymentVatOnTop' ) );
97  $this->init( 'oxpayments' );
98  }
99 
107  public function setPaymentVatOnTop( $blOnTop )
108  {
109  $this->_blPaymentVatOnTop = $blOnTop;
110  }
111 
117  public function getGroups()
118  {
119  if ( $this->_oGroups == null && ( $sOxid = $this->getId() ) ) {
120 
121  // user groups
122  $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
123  $sViewName = getViewName( "oxgroups", $this->getLanguage() );
124 
125  // performance
126  $sSelect = "select {$sViewName}.* from {$sViewName}, oxobject2group
127  where oxobject2group.oxobjectid = '{$sOxid}'
128  and oxobject2group.oxgroupsid={$sViewName}.oxid ";
129  $this->_oGroups->selectString( $sSelect );
130  }
131 
132  return $this->_oGroups;
133  }
134 
142  public function setDynValues( $aDynValues )
143  {
144  $this->_aDynValues = $aDynValues;
145  }
146 
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  return $this->_aDynValues;
176  }
177 
185  public function getPaymentValue( $dBasePrice )
186  {
187  $dRet = 0;
188 
189  if ( $this->oxpayments__oxaddsumtype->value == "%") {
190  $dRet = $dBasePrice * $this->oxpayments__oxaddsum->value/100;
191  } else {
192  $oCur = $this->getConfig()->getActShopCurrencyObject();
193  $dRet = $this->oxpayments__oxaddsum->value * $oCur->rate;
194  }
195 
196  if ( ($dRet * -1 ) > $dBasePrice ) {
197  $dRet = $dBasePrice;
198  }
199 
200  return $dRet;
201  }
202 
211  public function getBaseBasketPriceForPaymentCostCalc( $oBasket )
212  {
213  $dBasketPrice = 0;
214  $iRules = $this->oxpayments__oxaddsumrules->value;
215 
216  // products brutto price
217  if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_ALLGOODS ) ) {
218  $dBasketPrice += $oBasket->getProductsPrice()->getSum( $oBasket->isCalculationModeNetto() );
219  }
220 
221  // discounts
222  if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_DISCOUNTS ) ) &&
223  ( $oCosts = $oBasket->getTotalDiscount() ) ) {
224  $dBasketPrice -= $oCosts->getPrice();
225  }
226 
227  // vouchers
228  if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_VOUCHERS ) ) {
229  $dBasketPrice -= $oBasket->getVoucherDiscValue();
230  }
231 
232  // delivery
233  if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_SHIPCOSTS ) ) &&
234  ( $oCosts = $oBasket->getCosts( 'oxdelivery' ) ) ) {
235  if ($oBasket->isCalculationModeNetto()) {
236  $dBasketPrice += $oCosts->getNettoPrice();
237  } else {
238  $dBasketPrice += $oCosts->getBruttoPrice();
239  }
240 
241  }
242 
243  // wrapping
244  if ( ( $iRules & self::PAYMENT_ADDSUMRULE_GIFTS ) &&
245  ( $oCosts = $oBasket->getCosts( 'oxwrapping' ) ) ) {
246  if ($oBasket->isCalculationModeNetto()) {
247  $dBasketPrice += $oCosts->getNettoPrice();
248  } else {
249  $dBasketPrice += $oCosts->getBruttoPrice();
250  }
251  }
252 
253  // gift card
254  if ( ( $iRules & self::PAYMENT_ADDSUMRULE_GIFTS ) &&
255  ( $oCosts = $oBasket->getCosts( 'oxgiftcard' ) ) ) {
256 
257  if ($oBasket->isCalculationModeNetto()) {
258  $dBasketPrice += $oCosts->getNettoPrice();
259  } else {
260  $dBasketPrice += $oCosts->getBruttoPrice();
261  }
262  }
263 
264  return $dBasketPrice;
265  }
266 
276  public function getPaymentPrice( $oBasket )
277  {
278  $this->calculate( $oBasket );
279  }
280 
281 
289  public function calculate( $oBasket )
290  {
291  //getting basket price with applied discounts and vouchers
292  $dPrice = $this->getPaymentValue( $this->getBaseBasketPriceForPaymentCostCalc( $oBasket ) );
293 
294  if ( $dPrice ) {
295  // calculating total price
296  $oPrice = oxNew( 'oxPrice' );
297  if ( !$this->_blPaymentVatOnTop ) {
298  $oPrice->setBruttoPriceMode();
299  } else {
300  $oPrice->setNettoPriceMode();
301  }
302 
303  $oPrice->setPrice( $dPrice );
304  if ( $dPrice > 0 ) {
305  $oPrice->setVat( $oBasket->getAdditionalServicesVatPercent() );
306  }
307 
308  $this->_oPrice = $oPrice;
309  }
310 
311  }
312 
318  public function getPrice()
319  {
320  return $this->_oPrice;
321  }
322 
330  public function getFNettoPrice()
331  {
332  if ( $this->getPrice() ) {
333  return oxLang::getInstance()->formatCurrency( $this->getPrice()->getNettoPrice() );
334  }
335  }
336 
344  public function getFBruttoPrice()
345  {
346  if ( $this->getPrice() ) {
347  return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
348  }
349  }
350 
358  public function getFPriceVat()
359  {
360  if ( $this->getPrice() ) {
361  return oxLang::getInstance()->formatCurrency( $this->getPrice()->getVatValue() );
362  }
363  }
364 
370  public function getCountries()
371  {
372  if ( $this->_aCountries === null ) {
373  $oDb = oxDb::getDb();
374  $this->_aCountries = array();
375  $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid='.$oDb->quote( $this->getId() ).' and oxtype = "oxcountry" ';
376  $rs = $oDb->select( $sSelect );
377  if ( $rs && $rs->recordCount()) {
378  while ( !$rs->EOF ) {
379  $this->_aCountries[] = $rs->fields[0];
380  $rs->moveNext();
381  }
382  }
383  }
384  return $this->_aCountries;
385  }
386 
394  public function delete( $sOXID = null )
395  {
396  if ( parent::delete( $sOXID ) ) {
397 
398  $sOXID = $sOXID?$sOXID:$this->getId();
399  $oDb = oxDb::getDb();
400 
401  // deleting payment related data
402  $rs = $oDb->execute( "delete from oxobject2payment where oxpaymentid = ".$oDb->quote( $sOXID ) );
403  return $rs->EOF;
404  }
405 
406  return false;
407  }
408 
420  public function isValidPayment( $aDynValue, $sShopId, $oUser, $dBasketPrice, $sShipSetId )
421  {
422  $myConfig = $this->getConfig();
423  if ( $this->oxpayments__oxid->value == 'oxempty' ) {
424  // inactive or blOtherCountryOrder is off
425  if ( !$this->oxpayments__oxactive->value || !$myConfig->getConfigParam( "blOtherCountryOrder" ) ) {
426  $this->_iPaymentError = -2;
427  return false;
428  }
429  if (count(oxRegistry::get("oxDeliverySetList")
430  ->getDeliverySetList(
431  $oUser,
432  $oUser->getActiveCountry()
433  )
434  )) {
435  $this->_iPaymentError = -3;
436  return false;
437  }
438  return true;
439  }
440 
441  $mxValidationResult = oxRegistry::get("oxInputValidator")->validatePaymentInputData( $this->oxpayments__oxid->value, $aDynValue );
442 
443  if ( is_integer($mxValidationResult) ) {
444  $this->_iPaymentError = $mxValidationResult;
445  return false;
446  } elseif ($mxValidationResult === false) {
447  $this->_iPaymentError = 1;
448  return false;
449  }
450 
451  $oCur = $myConfig->getActShopCurrencyObject();
452  $dBasketPrice = $dBasketPrice / $oCur->rate;
453 
454  if ( $sShipSetId ) {
455  $aPaymentList = oxRegistry::get("oxPaymentList")->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
456 
457  if ( !array_key_exists( $this->getId(), $aPaymentList ) ) {
458  $this->_iPaymentError = -3;
459  return false;
460  }
461  } else {
462  $this->_iPaymentError = -2;
463  return false;
464  }
465 
466  return true;
467  }
468 
474  public function getPaymentErrorNumber()
475  {
476  return $this->_iPaymentError;
477  }
478 
479 }