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 > 0 ) {
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             $oPrice->setVat( $oBasket->getAdditionalServicesVatPercent() );
00305 
00306             $this->_oPrice = $oPrice;
00307         }
00308     }
00309 
00310 
00316     public function getPrice()
00317     {
00318         return $this->_oPrice;
00319     }
00320 
00326     public function getFNettoPrice()
00327     {
00328         if ( $this->getPrice() ) {
00329             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getNettoPrice() );
00330         }
00331     }
00332 
00338     public function getFBruttoPrice()
00339     {
00340         if ( $this->getPrice() ) {
00341             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
00342         }
00343     }
00344 
00350     public function getFPriceVat()
00351     {
00352         if ( $this->getPrice() ) {
00353             return oxLang::getInstance()->formatCurrency( $this->getPrice()->getVatValue() );
00354         }
00355     }
00356 
00362     public function getCountries()
00363     {
00364         if ( $this->_aCountries === null ) {
00365             $oDb = oxDb::getDb();
00366             $this->_aCountries = array();
00367             $sSelect = 'select oxobjectid from oxobject2payment where oxpaymentid='.$oDb->quote( $this->getId() ).' and oxtype = "oxcountry" ';
00368             $rs = $oDb->select( $sSelect );
00369             if ( $rs && $rs->recordCount()) {
00370                 while ( !$rs->EOF ) {
00371                     $this->_aCountries[] = $rs->fields[0];
00372                     $rs->moveNext();
00373                 }
00374             }
00375         }
00376         return $this->_aCountries;
00377     }
00378 
00386     public function delete( $sOXID = null )
00387     {
00388         if ( parent::delete( $sOXID ) ) {
00389 
00390             $sOXID = $sOXID?$sOXID:$this->getId();
00391             $oDb = oxDb::getDb();
00392 
00393             // deleting payment related data
00394             $rs = $oDb->execute( "delete from oxobject2payment where oxpaymentid = ".$oDb->quote( $sOXID ) );
00395             return $rs->EOF;
00396         }
00397 
00398         return false;
00399     }
00400 
00412     public function isValidPayment( $aDynvalue, $sShopId, $oUser, $dBasketPrice, $sShipSetId )
00413     {
00414         $myConfig = $this->getConfig();
00415         if ( $this->oxpayments__oxid->value == 'oxempty' ) {
00416             // inactive or blOtherCountryOrder is off
00417             if ( !$this->oxpayments__oxactive->value || !$myConfig->getConfigParam( "blOtherCountryOrder" ) ) {
00418                 $this->_iPaymentError = -2;
00419                 return false;
00420             }
00421             if (count(oxRegistry::get("oxDeliverySetList")
00422                             ->getDeliverySetList(
00423                                         $oUser,
00424                                         $oUser->getActiveCountry()
00425                                 )
00426                     )) {
00427                 $this->_iPaymentError = -3;
00428                 return false;
00429             }
00430             return true;
00431         }
00432 
00433         if ( !oxRegistry::get("oxInputValidator")->validatePaymentInputData( $this->oxpayments__oxid->value, $aDynvalue ) ) {
00434             $this->_iPaymentError = 1;
00435             return false;
00436         }
00437 
00438         $oCur = $myConfig->getActShopCurrencyObject();
00439         $dBasketPrice = $dBasketPrice / $oCur->rate;
00440 
00441         if ( $sShipSetId ) {
00442             $aPaymentList = oxRegistry::get("oxPaymentList")->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00443 
00444             if ( !array_key_exists( $this->getId(), $aPaymentList ) ) {
00445                 $this->_iPaymentError = -3;
00446                 return false;
00447             }
00448         } else {
00449             $this->_iPaymentError = -2;
00450             return false;
00451         }
00452 
00453         return true;
00454     }
00455 
00461     public function getPaymentErrorNumber()
00462     {
00463         return $this->_iPaymentError;
00464     }
00465 
00466 }