oxpayment.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxPayment extends oxI18n
00009 {
00014     const PAYMENT_ADDSUMRULE_ALLGOODS = 1;
00015 
00020     const PAYMENT_ADDSUMRULE_DISCOUNTS = 2;
00021 
00026     const PAYMENT_ADDSUMRULE_VOUCHERS = 4;
00027 
00032     const PAYMENT_ADDSUMRULE_SHIPCOSTS = 8;
00033 
00038     const PAYMENT_ADDSUMRULE_GIFTS = 16;
00039 
00045     protected $_oGroups = null;
00046 
00053     protected $_aCountries = null;
00054 
00060     protected $_sClassName = 'oxpayment';
00061 
00067     protected $_aDynValues = null;
00068 
00074     protected $_iPaymentError = null;
00075 
00081     protected $_blPaymentVatOnTop = false;
00082 
00088     protected $_oPrice = null;
00089 
00093     public function __construct()
00094     {
00095         $this->setPaymentVatOnTop( $this->getConfig()->getConfigParam( 'blPaymentVatOnTop' ) );
00096         parent::__construct();
00097         $this->init( 'oxpayments' );
00098     }
00099 
00107     public function setPaymentVatOnTop( $blOnTop )
00108     {
00109         $this->_blPaymentVatOnTop = $blOnTop;
00110     }
00111 
00117     public function getGroups()
00118     {
00119         if ( $this->_oGroups == null && ( $sOxid = $this->getId() ) ) {
00120 
00121             // usergroups
00122             $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00123             $sViewName = getViewName( "oxgroups", $this->getLanguage() );
00124 
00125             // performance
00126             $sSelect = "select {$sViewName}.* from {$sViewName}, oxobject2group
00127                         where oxobject2group.oxobjectid = '{$sOxid}'
00128                         and oxobject2group.oxgroupsid={$sViewName}.oxid ";
00129             $this->_oGroups->selectString( $sSelect );
00130         }
00131 
00132         return $this->_oGroups;
00133     }
00134 
00142     public function setDynValues( $aDynValues )
00143     {
00144         $this->_aDynValues = $aDynValues;
00145     }
00146 
00155     public function setDynValue( $oKey, $oVal )
00156     {
00157         $this->_aDynValues[$oKey] = $oVal;
00158     }
00159 
00165     public function getDynValues()
00166     {
00167         if ( !$this->_aDynValues ) {
00168             $sRawDynValue = null;
00169             if ( is_object($this->oxpayments__oxvaldesc ) ) {
00170                 $sRawDynValue = $this->oxpayments__oxvaldesc->getRawValue();
00171             }
00172 
00173             $this->_aDynValues = oxRegistry::getUtils()->assignValuesFromText( $sRawDynValue );
00174         }
00175         return $this->_aDynValues;
00176     }
00177 
00185     public function getPaymentValue( $dBaseprice )
00186     {
00187         $dRet = 0;
00188 
00189         if ( $this->oxpayments__oxaddsumtype->value == "%") {
00190             $dRet = $dBaseprice * $this->oxpayments__oxaddsum->value/100;
00191         } else {
00192             $oCur = $this->getConfig()->getActShopCurrencyObject();
00193             $dRet = $this->oxpayments__oxaddsum->value * $oCur->rate;
00194         }
00195 
00196         if ( ($dRet * -1 ) > $dBaseprice ) {
00197             $dRet = $dBaseprice;
00198         }
00199 
00200         return $dRet;
00201     }
00202 
00211     public function getBaseBasketPriceForPaymentCostCalc( $oBasket )
00212     {
00213         $dBasketPrice = 0;
00214         $iRules = $this->oxpayments__oxaddsumrules->value;
00215 
00216         // products brutto price
00217         if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_ALLGOODS ) ) {
00218             $dBasketPrice += $oBasket->getProductsPrice()->getSum( $oBasket->isCalculationModeNetto() );
00219         }
00220 
00221         // discounts
00222         if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_DISCOUNTS ) ) &&
00223              ( $oCosts = $oBasket->getTotalDiscount() ) ) {
00224             $dBasketPrice -= $oCosts->getPrice();
00225         }
00226 
00227         // vouchers
00228         if ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_VOUCHERS ) ) {
00229             $dBasketPrice -= $oBasket->getVoucherDiscValue();
00230         }
00231 
00232         // delivery
00233         if ( ( !$iRules || ( $iRules & self::PAYMENT_ADDSUMRULE_SHIPCOSTS ) ) &&
00234              ( $oCosts = $oBasket->getCosts( 'oxdelivery' ) ) ) {
00235             if ($oBasket->isCalculationModeNetto()) {
00236                 $dBasketPrice += $oCosts->getNettoPrice();
00237             } else {
00238                 $dBasketPrice += $oCosts->getBruttoPrice();
00239             }
00240 
00241         }
00242 
00243         // wrapping
00244         if ( ( $iRules & self::PAYMENT_ADDSUMRULE_GIFTS ) &&
00245              ( $oCosts = $oBasket->getCosts( 'oxwrapping' ) ) ) {
00246             if ($oBasket->isCalculationModeNetto()) {
00247                 $dBasketPrice += $oCosts->getNettoPrice();
00248             } else {
00249                 $dBasketPrice += $oCosts->getBruttoPrice();
00250             }
00251         }
00252 
00253         // gift card
00254         if ( ( $iRules & self::PAYMENT_ADDSUMRULE_GIFTS ) &&
00255              ( $oCosts = $oBasket->getCosts( 'oxgiftcard' ) ) ) {
00256 
00257             if ($oBasket->isCalculationModeNetto()) {
00258                 $dBasketPrice += $oCosts->getNettoPrice();
00259             } else {
00260                 $dBasketPrice += $oCosts->getBruttoPrice();
00261             }
00262         }
00263 
00264         return $dBasketPrice;
00265     }
00266 
00276     public function getPaymentPrice( $oBasket )
00277     {
00278         $this->calculate( $oBasket );
00279     }
00280 
00281 
00289     public function calculate( $oBasket )
00290     {
00291         //getting basket price with applied discounts and vouchers
00292         $dPrice = $this->getPaymentValue( $this->getBaseBasketPriceForPaymentCostCalc( $oBasket ) );
00293 
00294         if ( $dPrice ) {
00295             // calculating total price
00296             $oPrice = oxNew( 'oxPrice' );
00297             if ( !$this->_blPaymentVatOnTop ) {
00298                 $oPrice->setBruttoPriceMode();
00299             } else {
00300                 $oPrice->setNettoPriceMode();
00301             }
00302 
00303             $oPrice->setPrice( $dPrice );
00304             if ( $dPrice > 0 ) {
00305                 $oPrice->setVat( $oBasket->getAdditionalServicesVatPercent() );
00306             }
00307 
00308             $this->_oPrice = $oPrice;
00309         }
00310 
00311     }
00312 
00313 
00319     public function getPrice()
00320     {
00321         return $this->_oPrice;
00322     }
00323 
00329     public function getFNettoPrice()
00330     {
00331         if ( $this->getPrice() ) {
00332             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getNettoPrice() );
00333         }
00334     }
00335 
00341     public function getFBruttoPrice()
00342     {
00343         if ( $this->getPrice() ) {
00344             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
00345         }
00346     }
00347 
00353     public function getFPriceVat()
00354     {
00355         if ( $this->getPrice() ) {
00356             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getVatValue() );
00357         }
00358     }
00359 
00365     public function getCountries()
00366     {
00367         if ( $this->_aCountries === null ) {
00368             $oDb = oxDb::getDb();
00369             $this->_aCountries = array();
00370             $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid='.$oDb->quote( $this->getId() ).' and oxtype = "oxcountry" ';
00371             $rs = $oDb->select( $sSelect );
00372             if ( $rs && $rs->recordCount()) {
00373                 while ( !$rs->EOF ) {
00374                     $this->_aCountries[] = $rs->fields[0];
00375                     $rs->moveNext();
00376                 }
00377             }
00378         }
00379         return $this->_aCountries;
00380     }
00381 
00389     public function delete( $sOXID = null )
00390     {
00391         if ( parent::delete( $sOXID ) ) {
00392 
00393             $sOXID = $sOXID?$sOXID:$this->getId();
00394             $oDb = oxDb::getDb();
00395 
00396             // deleting payment related data
00397             $rs = $oDb->execute( "delete from oxobject2payment where oxpaymentid = ".$oDb->quote( $sOXID ) );
00398             return $rs->EOF;
00399         }
00400 
00401         return false;
00402     }
00403 
00415     public function isValidPayment( $aDynvalue, $sShopId, $oUser, $dBasketPrice, $sShipSetId )
00416     {
00417         $myConfig = $this->getConfig();
00418         if ( $this->oxpayments__oxid->value == 'oxempty' ) {
00419             // inactive or blOtherCountryOrder is off
00420             if ( !$this->oxpayments__oxactive->value || !$myConfig->getConfigParam( "blOtherCountryOrder" ) ) {
00421                 $this->_iPaymentError = -2;
00422                 return false;
00423             }
00424             if (count(oxRegistry::get("oxDeliverySetList")
00425                             ->getDeliverySetList(
00426                                         $oUser,
00427                                         $oUser->getActiveCountry()
00428                                 )
00429                     )) {
00430                 $this->_iPaymentError = -3;
00431                 return false;
00432             }
00433             return true;
00434         }
00435 
00436         if ( !oxRegistry::get("oxInputValidator")->validatePaymentInputData( $this->oxpayments__oxid->value, $aDynvalue ) ) {
00437             $this->_iPaymentError = 1;
00438             return false;
00439         }
00440 
00441         $oCur = $myConfig->getActShopCurrencyObject();
00442         $dBasketPrice = $dBasketPrice / $oCur->rate;
00443 
00444         if ( $sShipSetId ) {
00445             $aPaymentList = oxRegistry::get("oxPaymentList")->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00446 
00447             if ( !array_key_exists( $this->getId(), $aPaymentList ) ) {
00448                 $this->_iPaymentError = -3;
00449                 return false;
00450             }
00451         } else {
00452             $this->_iPaymentError = -2;
00453             return false;
00454         }
00455 
00456         return true;
00457     }
00458 
00464     public function getPaymentErrorNumber()
00465     {
00466         return $this->_iPaymentError;
00467     }
00468 
00469 }