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             // user groups
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 
00318     public function getPrice()
00319     {
00320         return $this->_oPrice;
00321     }
00322 
00330     public function getFNettoPrice()
00331     {
00332         if ( $this->getPrice() ) {
00333             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getNettoPrice() );
00334         }
00335     }
00336 
00344     public function getFBruttoPrice()
00345     {
00346         if ( $this->getPrice() ) {
00347             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
00348         }
00349     }
00350 
00358     public function getFPriceVat()
00359     {
00360         if ( $this->getPrice() ) {
00361             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getVatValue() );
00362         }
00363     }
00364 
00370     public function getCountries()
00371     {
00372         if ( $this->_aCountries === null ) {
00373             $oDb = oxDb::getDb();
00374             $this->_aCountries = array();
00375             $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid='.$oDb->quote( $this->getId() ).' and oxtype = "oxcountry" ';
00376             $rs = $oDb->select( $sSelect );
00377             if ( $rs && $rs->recordCount()) {
00378                 while ( !$rs->EOF ) {
00379                     $this->_aCountries[] = $rs->fields[0];
00380                     $rs->moveNext();
00381                 }
00382             }
00383         }
00384         return $this->_aCountries;
00385     }
00386 
00394     public function delete( $sOXID = null )
00395     {
00396         if ( parent::delete( $sOXID ) ) {
00397 
00398             $sOXID = $sOXID?$sOXID:$this->getId();
00399             $oDb = oxDb::getDb();
00400 
00401             // deleting payment related data
00402             $rs = $oDb->execute( "delete from oxobject2payment where oxpaymentid = ".$oDb->quote( $sOXID ) );
00403             return $rs->EOF;
00404         }
00405 
00406         return false;
00407     }
00408 
00420     public function isValidPayment( $aDynValue, $sShopId, $oUser, $dBasketPrice, $sShipSetId )
00421     {
00422         $myConfig = $this->getConfig();
00423         if ( $this->oxpayments__oxid->value == 'oxempty' ) {
00424             // inactive or blOtherCountryOrder is off
00425             if ( !$this->oxpayments__oxactive->value || !$myConfig->getConfigParam( "blOtherCountryOrder" ) ) {
00426                 $this->_iPaymentError = -2;
00427                 return false;
00428             }
00429             if (count(oxRegistry::get("oxDeliverySetList")
00430                             ->getDeliverySetList(
00431                                         $oUser,
00432                                         $oUser->getActiveCountry()
00433                                 )
00434                     )) {
00435                 $this->_iPaymentError = -3;
00436                 return false;
00437             }
00438             return true;
00439         }
00440 
00441         $mxValidationResult = oxRegistry::get("oxInputValidator")->validatePaymentInputData( $this->oxpayments__oxid->value, $aDynValue );
00442 
00443         if ( is_integer($mxValidationResult) ) {
00444             $this->_iPaymentError = $mxValidationResult;
00445             return false;
00446         } elseif ($mxValidationResult === false) {
00447             $this->_iPaymentError = 1;
00448             return false;
00449         }
00450 
00451         $oCur = $myConfig->getActShopCurrencyObject();
00452         $dBasketPrice = $dBasketPrice / $oCur->rate;
00453 
00454         if ( $sShipSetId ) {
00455             $aPaymentList = oxRegistry::get("oxPaymentList")->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00456 
00457             if ( !array_key_exists( $this->getId(), $aPaymentList ) ) {
00458                 $this->_iPaymentError = -3;
00459                 return false;
00460             }
00461         } else {
00462             $this->_iPaymentError = -2;
00463             return false;
00464         }
00465 
00466         return true;
00467     }
00468 
00474     public function getPaymentErrorNumber()
00475     {
00476         return $this->_iPaymentError;
00477     }
00478 
00479 }