oxpayment.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxPayment extends oxI18n
00008 {
00013     const PAYMENT_ADDSUMRULE_ALLGOODS = 1;
00014 
00019     const PAYMENT_ADDSUMRULE_DISCOUNTS = 2;
00020 
00025     const PAYMENT_ADDSUMRULE_VOUCHERS = 4;
00026 
00031     const PAYMENT_ADDSUMRULE_SHIPCOSTS = 8;
00032 
00037     const PAYMENT_ADDSUMRULE_GIFTS = 16;
00038 
00044     protected $_oGroups = null;
00045 
00052     protected $_aCountries = null;
00053 
00059     protected $_sClassName = 'oxpayment';
00060 
00066     protected $_aDynValues = null;
00067 
00073     protected $_iPaymentError = null;
00074 
00078     public function __construct()
00079     {
00080         parent::__construct();
00081         $this->init( 'oxpayments' );
00082     }
00083 
00089     public function getGroups()
00090     {
00091         if ( $this->_oGroups == null && ( $sOxid = $this->getId() ) ) {
00092 
00093             // usergroups
00094             $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00095             $sViewName = getViewName( "oxgroups", $this->getLanguage() );
00096 
00097             // performance
00098             $sSelect = "select {$sViewName}.* from {$sViewName}, oxobject2group
00099                         where oxobject2group.oxobjectid = '{$sOxid}'
00100                         and oxobject2group.oxgroupsid={$sViewName}.oxid ";
00101             $this->_oGroups->selectString( $sSelect );
00102         }
00103 
00104         return $this->_oGroups;
00105     }
00106 
00114     public function setDynValues( $aDynValues )
00115     {
00116         $this->_aDynValues = $aDynValues;
00117     }
00118 
00127     public function setDynValue( $oKey, $oVal )
00128     {
00129         $this->_aDynValues[$oKey] = $oVal;
00130     }
00131 
00137     public function getDynValues()
00138     {
00139         if ( !$this->_aDynValues ) {
00140             $sRawDynValue = null;
00141             if ( is_object($this->oxpayments__oxvaldesc ) ) {
00142                 $sRawDynValue = $this->oxpayments__oxvaldesc->getRawValue();
00143             }
00144 
00145             $this->_aDynValues = oxUtils::getInstance()->assignValuesFromText( $sRawDynValue );
00146         }
00147         return $this->_aDynValues;
00148     }
00149 
00157     public function getPaymentValue( $dBaseprice )
00158     {
00159         $dRet = 0;
00160 
00161         if ( $this->oxpayments__oxaddsumtype->value == "%") {
00162             $dRet = $dBaseprice * $this->oxpayments__oxaddsum->value/100;
00163         } else {
00164             $oCur = $this->getConfig()->getActShopCurrencyObject();
00165             $dRet = $this->oxpayments__oxaddsum->value * $oCur->rate;
00166         }
00167 
00168         if ( ($dRet * -1 ) > $dBaseprice ) {
00169             $dRet = $dBaseprice;
00170         }
00171 
00172         return $dRet;
00173     }
00174 
00183     public function getBaseBasketPriceForPaymentCostCalc( $oBasket )
00184     {
00185         $dBasketPrice = 0;
00186         $iRules = $this->oxpayments__oxaddsumrules->value;
00187 
00188         // products brutto price
00189         if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_ALLGOODS ) ) {
00190             $dBasketPrice += $oBasket->getProductsPrice()->getBruttoSum();
00191         }
00192 
00193         // discounts
00194         if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_DISCOUNTS ) ) &&
00195              ( $oCosts = $oBasket->getTotalDiscount() ) ) {
00196             $dBasketPrice -= $oCosts->getBruttoPrice();
00197         }
00198 
00199         // vouchers
00200         if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_VOUCHERS ) ) {
00201             $dBasketPrice -= $oBasket->getVoucherDiscValue();
00202         }
00203 
00204         // delivery
00205         if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_SHIPCOSTS ) ) &&
00206              ( $oCosts = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00207             $dBasketPrice += $oCosts->getBruttoPrice();
00208         }
00209 
00210             // wrapping
00211         if ( ( $iRules & self::PAYMENT_ADDSUMRULE_GIFTS ) &&
00212              ( $oCosts = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00213             $dBasketPrice += $oCosts->getBruttoPrice();
00214         }
00215 
00216         return $dBasketPrice;
00217     }
00218 
00226     public function getPaymentPrice( $oBasket )
00227     {
00228         //getting basket price with applied discounts and vouchers
00229         $dPrice = $this->getPaymentValue( $this->getBaseBasketPriceForPaymentCostCalc( $oBasket ) );
00230 
00231         // calculating total price
00232         $oPrice = oxNew( 'oxPrice' );
00233         $oPrice->setBruttoPriceMode();
00234         $oPrice->setPrice( $dPrice );
00235 
00236         if ( $this->getConfig()->getConfigParam( 'blCalcVATForPayCharge' ) && $dPrice > 0 ) {
00237             $oPrice->setVat( $oBasket->getMostUsedVatPercent() );
00238         }
00239 
00240         return $oPrice;
00241     }
00242 
00248     public function getCountries()
00249     {
00250         if ( $this->_aCountries === null ) {
00251             $oDb = oxDb::getDb();
00252             $this->_aCountries = array();
00253             $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid='.$oDb->quote( $this->getId() ).' and oxtype = "oxcountry" ';
00254             $rs = $oDb->execute( $sSelect );
00255             if ( $rs && $rs->recordCount()) {
00256                 while ( !$rs->EOF ) {
00257                     $this->_aCountries[] = $rs->fields[0];
00258                     $rs->moveNext();
00259                 }
00260             }
00261         }
00262         return $this->_aCountries;
00263     }
00264 
00272     public function delete( $sOXID = null )
00273     {
00274         if ( parent::delete( $sOXID ) ) {
00275 
00276             $sOXID = $sOXID?$sOXID:$this->getId();
00277             $oDb = oxDb::getDb();
00278 
00279             // deleting payment related data
00280             $rs = $oDb->execute( "delete from oxobject2payment where oxpaymentid = ".$oDb->quote( $sOXID ) );
00281             return $rs->EOF;
00282         }
00283 
00284         return false;
00285     }
00286 
00298     public function isValidPayment( $aDynvalue, $sShopId, $oUser, $dBasketPrice, $sShipSetId )
00299     {
00300         $myConfig = $this->getConfig();
00301         if ( $this->oxpayments__oxid->value == 'oxempty' ) {
00302             // inactive or blOtherCountryOrder is off
00303             if ( !$this->oxpayments__oxactive->value || !$myConfig->getConfigParam( "blOtherCountryOrder" ) ) {
00304                 $this->_iPaymentError = -2;
00305                 return false;
00306             }
00307             if (count(oxDeliverySetList::getInstance()
00308                             ->getDeliverySetList(
00309                                         $oUser,
00310                                         $oUser->getActiveCountry()
00311                                 )
00312                     )) {
00313                 $this->_iPaymentError = -3;
00314                 return false;
00315             }
00316             return true;
00317         }
00318 
00319         if ( !oxInputValidator::getInstance()->validatePaymentInputData( $this->oxpayments__oxid->value, $aDynvalue ) ) {
00320             $this->_iPaymentError = 1;
00321             return false;
00322         }
00323 
00324         $oCur = $myConfig->getActShopCurrencyObject();
00325         $dBasketPrice = $dBasketPrice / $oCur->rate;
00326 
00327         if ( $sShipSetId ) {
00328             $aPaymentList = oxPaymentList::getInstance()->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00329 
00330             if ( !array_key_exists( $this->getId(), $aPaymentList ) ) {
00331                 $this->_iPaymentError = -3;
00332                 return false;
00333             }
00334         } else {
00335             $this->_iPaymentError = -2;
00336             return false;
00337         }
00338 
00339         return true;
00340     }
00341 
00347     public function getPaymentErrorNumber()
00348     {
00349         return $this->_iPaymentError;
00350     }
00351 
00352 }