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 
00080     protected $_blPaymentVatOnTop = false;
00081 
00085     public function __construct()
00086     {
00087         $this->setPaymentVatOnTop( $this->getConfig()->getConfigParam( 'blPaymentVatOnTop' ) );
00088         parent::__construct();
00089         $this->init( 'oxpayments' );
00090     }
00091 
00099     public function setPaymentVatOnTop( $blOnTop )
00100     {
00101         $this->_blPaymentVatOnTop = $blOnTop;
00102     }
00103 
00109     public function getGroups()
00110     {
00111         if ( $this->_oGroups == null && ( $sOxid = $this->getId() ) ) {
00112 
00113             // usergroups
00114             $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00115             $sViewName = getViewName( "oxgroups", $this->getLanguage() );
00116 
00117             // performance
00118             $sSelect = "select {$sViewName}.* from {$sViewName}, oxobject2group
00119                         where oxobject2group.oxobjectid = '{$sOxid}'
00120                         and oxobject2group.oxgroupsid={$sViewName}.oxid ";
00121             $this->_oGroups->selectString( $sSelect );
00122         }
00123 
00124         return $this->_oGroups;
00125     }
00126 
00134     public function setDynValues( $aDynValues )
00135     {
00136         $this->_aDynValues = $aDynValues;
00137     }
00138 
00147     public function setDynValue( $oKey, $oVal )
00148     {
00149         $this->_aDynValues[$oKey] = $oVal;
00150     }
00151 
00157     public function getDynValues()
00158     {
00159         if ( !$this->_aDynValues ) {
00160             $sRawDynValue = null;
00161             if ( is_object($this->oxpayments__oxvaldesc ) ) {
00162                 $sRawDynValue = $this->oxpayments__oxvaldesc->getRawValue();
00163             }
00164 
00165             $this->_aDynValues = oxUtils::getInstance()->assignValuesFromText( $sRawDynValue );
00166         }
00167         return $this->_aDynValues;
00168     }
00169 
00177     public function getPaymentValue( $dBaseprice )
00178     {
00179         $dRet = 0;
00180 
00181         if ( $this->oxpayments__oxaddsumtype->value == "%") {
00182             $dRet = $dBaseprice * $this->oxpayments__oxaddsum->value/100;
00183         } else {
00184             $oCur = $this->getConfig()->getActShopCurrencyObject();
00185             $dRet = $this->oxpayments__oxaddsum->value * $oCur->rate;
00186         }
00187 
00188         if ( ($dRet * -1 ) > $dBaseprice ) {
00189             $dRet = $dBaseprice;
00190         }
00191 
00192         return $dRet;
00193     }
00194 
00203     public function getBaseBasketPriceForPaymentCostCalc( $oBasket )
00204     {
00205         $dBasketPrice = 0;
00206         $iRules = $this->oxpayments__oxaddsumrules->value;
00207 
00208         // products brutto price
00209         if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_ALLGOODS ) ) {
00210             $dBasketPrice += $oBasket->getProductsPrice()->getBruttoSum();
00211         }
00212 
00213         // discounts
00214         if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_DISCOUNTS ) ) &&
00215              ( $oCosts = $oBasket->getTotalDiscount() ) ) {
00216             $dBasketPrice -= $oCosts->getBruttoPrice();
00217         }
00218 
00219         // vouchers
00220         if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_VOUCHERS ) ) {
00221             $dBasketPrice -= $oBasket->getVoucherDiscValue();
00222         }
00223 
00224         // delivery
00225         if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_SHIPCOSTS ) ) &&
00226              ( $oCosts = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00227             $dBasketPrice += $oCosts->getBruttoPrice();
00228         }
00229 
00230             // wrapping
00231         if ( ( $iRules & self::PAYMENT_ADDSUMRULE_GIFTS ) &&
00232              ( $oCosts = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00233             $dBasketPrice += $oCosts->getBruttoPrice();
00234         }
00235 
00236         return $dBasketPrice;
00237     }
00238 
00246     public function getPaymentPrice( $oBasket )
00247     {
00248         //getting basket price with applied discounts and vouchers
00249         $dPrice = $this->getPaymentValue( $this->getBaseBasketPriceForPaymentCostCalc( $oBasket ) );
00250 
00251         // calculating total price
00252         $oPrice = oxNew( 'oxPrice' );
00253         if ( !$this->_blPaymentVatOnTop ) {
00254             $oPrice->setBruttoPriceMode();
00255         } else {
00256             $oPrice->setNettoPriceMode();
00257         }
00258 
00259         $oPrice->setPrice( $dPrice );
00260 
00261         // VAT will be always calculated(#3757)
00262         // blCalcVATForPayCharge option is @deprecated since 2012-03-23 in version 4.6
00263         // blShowVATForPayCharge option will be used only for displaying
00264         if ( $dPrice > 0 ) {
00265             $oPrice->setVat( $oBasket->getMostUsedVatPercent() );
00266         }
00267 
00268         return $oPrice;
00269     }
00270 
00276     public function getCountries()
00277     {
00278         if ( $this->_aCountries === null ) {
00279             $oDb = oxDb::getDb();
00280             $this->_aCountries = array();
00281             $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid='.$oDb->quote( $this->getId() ).' and oxtype = "oxcountry" ';
00282             $rs = $oDb->select( $sSelect );
00283             if ( $rs && $rs->recordCount()) {
00284                 while ( !$rs->EOF ) {
00285                     $this->_aCountries[] = $rs->fields[0];
00286                     $rs->moveNext();
00287                 }
00288             }
00289         }
00290         return $this->_aCountries;
00291     }
00292 
00300     public function delete( $sOXID = null )
00301     {
00302         if ( parent::delete( $sOXID ) ) {
00303 
00304             $sOXID = $sOXID?$sOXID:$this->getId();
00305             $oDb = oxDb::getDb();
00306 
00307             // deleting payment related data
00308             $rs = $oDb->execute( "delete from oxobject2payment where oxpaymentid = ".$oDb->quote( $sOXID ) );
00309             return $rs->EOF;
00310         }
00311 
00312         return false;
00313     }
00314 
00326     public function isValidPayment( $aDynvalue, $sShopId, $oUser, $dBasketPrice, $sShipSetId )
00327     {
00328         $myConfig = $this->getConfig();
00329         if ( $this->oxpayments__oxid->value == 'oxempty' ) {
00330             // inactive or blOtherCountryOrder is off
00331             if ( !$this->oxpayments__oxactive->value || !$myConfig->getConfigParam( "blOtherCountryOrder" ) ) {
00332                 $this->_iPaymentError = -2;
00333                 return false;
00334             }
00335             if (count(oxDeliverySetList::getInstance()
00336                             ->getDeliverySetList(
00337                                         $oUser,
00338                                         $oUser->getActiveCountry()
00339                                 )
00340                     )) {
00341                 $this->_iPaymentError = -3;
00342                 return false;
00343             }
00344             return true;
00345         }
00346 
00347         if ( !oxInputValidator::getInstance()->validatePaymentInputData( $this->oxpayments__oxid->value, $aDynvalue ) ) {
00348             $this->_iPaymentError = 1;
00349             return false;
00350         }
00351 
00352         $oCur = $myConfig->getActShopCurrencyObject();
00353         $dBasketPrice = $dBasketPrice / $oCur->rate;
00354 
00355         if ( $sShipSetId ) {
00356             $aPaymentList = oxPaymentList::getInstance()->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00357 
00358             if ( !array_key_exists( $this->getId(), $aPaymentList ) ) {
00359                 $this->_iPaymentError = -3;
00360                 return false;
00361             }
00362         } else {
00363             $this->_iPaymentError = -2;
00364             return false;
00365         }
00366 
00367         return true;
00368     }
00369 
00375     public function getPaymentErrorNumber()
00376     {
00377         return $this->_iPaymentError;
00378     }
00379 
00380 }