oxvoucher.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxVoucher extends oxBase
00009 {
00010 
00011     protected $_oSerie = null;
00012 
00018     protected $_blDisableShopCheck = true;
00019 
00023     protected $_sClassName = 'oxvoucher';
00024 
00028     public function __construct()
00029     {
00030         parent::__construct();
00031         $this->init( 'oxvouchers' );
00032     }
00033 
00045     public function getVoucherByNr( $sVoucherNr, $aVouchers = array(), $blCheckavalability = false )
00046     {
00047         $oRet = null;
00048         if ( !is_null( $sVoucherNr ) ) {
00049 
00050             $sViewName = $this->getViewName();
00051             $sSeriesViewName = getViewName( 'oxvoucherseries' );
00052             $myDB = oxDb::getDb();
00053 
00054             $sQ  = "select {$sViewName}.* from {$sViewName}, {$sSeriesViewName} where
00055                         {$sSeriesViewName}.oxid = {$sViewName}.oxvoucherserieid and
00056                         {$sViewName}.oxvouchernr = " . $myDB->quote( $sVoucherNr ) . " and ";
00057 
00058             if ( is_array( $aVouchers ) ) {
00059                 foreach ( $aVouchers as $sVoucherId => $sSkipVoucherNr ) {
00060                     $sQ .= "{$sViewName}.oxid != " . $myDB->quote( $sVoucherId ) . " and ";
00061                 }
00062             }
00063             $sQ .= "( {$sViewName}.oxorderid is NULL || {$sViewName}.oxorderid = '' ) ";
00064             $sQ .= " and ( {$sViewName}.oxdateused is NULL || {$sViewName}.oxdateused = 0 ) ";
00065 
00066             //voucher timeout for 3 hours
00067             if ( $blCheckavalability ) {
00068                 $iTime = time() - 3600 * 3;
00069                 $sQ .= " and {$sViewName}.oxreserved < '{$iTime}' ";
00070             }
00071 
00072             $sQ .= " limit 1";
00073 
00074             if ( ! ( $oRet = $this->assignRecord( $sQ ) ) ) {
00075                 $oEx = oxNew( 'oxVoucherException' );
00076                 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOVOUCHER' );
00077                 $oEx->setVoucherNr( $sVoucherNr );
00078                 throw $oEx;
00079             }
00080         }
00081 
00082         return $oRet;
00083     }
00084 
00094     public function markAsUsed( $sOrderId, $sUserId, $dDiscount )
00095     {
00096         //saving oxreserved field
00097         if ( $this->oxvouchers__oxid->value ) {
00098             $this->oxvouchers__oxorderid->setValue($sOrderId);
00099             $this->oxvouchers__oxuserid->setValue($sUserId);
00100             $this->oxvouchers__oxdiscount->setValue($dDiscount);
00101             $this->oxvouchers__oxdateused->setValue(date( "Y-m-d", oxUtilsDate::getInstance()->getTime() ));
00102             $this->save();
00103         }
00104     }
00105 
00111     public function markAsReserved()
00112     {
00113         //saving oxreserved field
00114         $sVoucherID = $this->oxvouchers__oxid->value;
00115 
00116         if ( $sVoucherID ) {
00117             $myDB = oxDb::getDb();
00118             $sQ = "update oxvouchers set oxreserved = " . time() . " where oxid = " . $myDB->quote( $sVoucherID );
00119             $myDB->Execute( $sQ );
00120         }
00121     }
00122 
00128     public function unMarkAsReserved()
00129     {
00130         //saving oxreserved field
00131         $sVoucherID = $this->oxvouchers__oxid->value;
00132 
00133         if ( $sVoucherID ) {
00134             $myDB = oxDb::getDb();
00135             $sQ = "update oxvouchers set oxreserved = 0 where oxid = " . $myDB->quote( $sVoucherID );
00136             $myDB->Execute($sQ);
00137         }
00138     }
00139 
00149     public function getDiscountValue( $dPrice )
00150     {
00151         if ($this->_isProductVoucher()) {
00152             return $this->_getProductDiscoutValue( $dPrice );
00153         } elseif ($this->_isCategoryVoucher()) {
00154             return $this->_getCategoryDiscoutValue( $dPrice );
00155         } else {
00156             return $this->_getGenericDiscoutValue( $dPrice );
00157         }
00158     }
00159 
00160     // Checking General Availability
00171     public function checkVoucherAvailability( $aVouchers, $dPrice )
00172     {
00173         $this->_isAvailableWithSameSeries( $aVouchers );
00174         $this->_isAvailableWithOtherSeries( $aVouchers );
00175         $this->_isValidDate();
00176         $this->_isAvailablePrice( $dPrice );
00177         $this->_isNotReserved();
00178 
00179         // returning true - no exception was thrown
00180         return true;
00181     }
00182 
00194     public function checkBasketVoucherAvailability( $aVouchers, $dPrice )
00195     {
00196         $this->_isAvailableWithSameSeries( $aVouchers );
00197         $this->_isAvailableWithOtherSeries( $aVouchers );
00198         $this->_isValidDate();
00199         $this->_isAvailablePrice( $dPrice );
00200 
00201         // returning true - no exception was thrown
00202         return true;
00203     }
00204 
00214     protected function _isAvailablePrice( $dPrice )
00215     {
00216         if ( $this->getDiscountValue( $dPrice ) < 0 ) {
00217             $oEx = oxNew( 'oxVoucherException' );
00218             $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00219             $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00220             throw $oEx;
00221         }
00222         $oSerie = $this->getSerie();
00223         $oCur = $this->getConfig()->getActShopCurrencyObject();
00224         if ( $oSerie->oxvoucherseries__oxminimumvalue->value && $dPrice < ($oSerie->oxvoucherseries__oxminimumvalue->value*$oCur->rate) ) {
00225             $oEx = oxNew( 'oxVoucherException' );
00226             $oEx->setMessage('EXCEPTION_VOUCHER_INCORRECTPRICE');
00227             $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00228             throw $oEx;
00229         }
00230 
00231         return true;
00232     }
00233 
00245     protected function _isAvailableWithSameSeries( $aVouchers )
00246     {
00247         if ( is_array( $aVouchers ) ) {
00248             $sId = $this->getId();
00249             if (isset($aVouchers[$sId])) {
00250                 unset($aVouchers[$sId]);
00251             }
00252             $oSerie = $this->getSerie();
00253             if (!$oSerie->oxvoucherseries__oxallowsameseries->value) {
00254                 foreach ( $aVouchers as $voucherId => $voucherNr ) {
00255                     $oVoucher = oxNew( 'oxvoucher' );
00256                     $oVoucher->load($voucherId);
00257                     if ( $this->oxvouchers__oxvoucherserieid->value == $oVoucher->oxvouchers__oxvoucherserieid->value ) {
00258                             $oEx = oxNew( 'oxVoucherException' );
00259                             $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDSAMESERIES');
00260                             $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00261                             throw $oEx;
00262                     }
00263                 }
00264             }
00265         }
00266 
00267         return true;
00268     }
00269 
00280     protected function _isAvailableWithOtherSeries( $aVouchers )
00281     {
00282         if ( is_array( $aVouchers ) && count($aVouchers) ) {
00283             $oSerie = $this->getSerie();
00284             $sIds = implode(',', oxDb::getInstance()->quoteArray( array_keys( $aVouchers ) ) );
00285             $blAvailable = true;
00286             $myDB = oxDb::getDb();
00287             if (!$oSerie->oxvoucherseries__oxallowotherseries->value) {
00288                 // just search for vouchers with different series
00289                 $sSql  = "select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
00290                 $sSql .= "oxvouchers.oxvoucherserieid != " . $myDB->quote( $this->oxvouchers__oxvoucherserieid->value ) ;
00291                 $blAvailable &= !$myDB->getOne($sSql);
00292             } else {
00293                 // search for vouchers with different series and those vouchers do not allow other series
00294                 $sSql  = "select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
00295                 $sSql .= "where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != " . $myDB->quote( $this->oxvouchers__oxvoucherserieid->value );
00296                 $sSql .= "and not oxvoucherseries.oxallowotherseries";
00297                 $blAvailable &= !$myDB->getOne($sSql);
00298             }
00299             if ( !$blAvailable ) {
00300                     $oEx = oxNew( 'oxVoucherException' );
00301                     $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDOTHERSERIES');
00302                     $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00303                     throw $oEx;
00304             }
00305         }
00306 
00307         return true;
00308     }
00309 
00317     protected function _isValidDate()
00318     {
00319         $oSerie = $this->getSerie();
00320         // if time is not set - keep it as active by default
00321         $sDefTimeStamp = oxUtilsDate::getInstance()->formatDBDate( '-' );
00322         if ( $oSerie->oxvoucherseries__oxbegindate->value == $sDefTimeStamp &&
00323              $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00324             return true;
00325         }
00326 
00327         if ( ( strtotime( $oSerie->oxvoucherseries__oxbegindate->value ) < time() &&
00328               strtotime( $oSerie->oxvoucherseries__oxenddate->value ) > time() ) ||
00329             !$oSerie->oxvoucherseries__oxenddate->value ||
00330             $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00331             return true;
00332         }
00333 
00334         $oEx = oxNew( 'oxVoucherException' );
00335         $oEx->setMessage('EXCEPTION_VOUCHER_ISNOTVALIDDATE');
00336         $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00337         throw $oEx;
00338     }
00339 
00347     protected function _isNotReserved()
00348     {
00349 
00350         if ( $this->oxvouchers__oxreserved->value < time() - 3600 * 3 ) {
00351             return true;
00352         }
00353 
00354         $oEx = oxNew( 'oxVoucherException' );
00355         $oEx->setMessage('EXCEPTION_VOUCHER_ISRESERVED');
00356         $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00357         throw $oEx;
00358     }
00359 
00360     // Checking User Availability
00370     public function checkUserAvailability( $oUser )
00371     {
00372 
00373         $this->_isAvailableInOtherOrder( $oUser );
00374         $this->_isValidUserGroup( $oUser );
00375 
00376         // returning true if no exception was thrown
00377         return true;
00378     }
00379 
00389     protected function _isAvailableInOtherOrder( $oUser )
00390     {
00391         $oSerie = $this->getSerie();
00392         if ( !$oSerie->oxvoucherseries__oxallowuseanother->value ) {
00393 
00394             $myDB = oxDb::getDb();
00395             $sSelect  = 'select count(*) from '.$this->getViewName().' where oxuserid = '. $myDB->quote( $oUser->oxuser__oxid->value ) . ' and ';
00396             $sSelect .= 'oxvoucherserieid = ' . $myDB->quote( $this->oxvouchers__oxvoucherserieid->value ) . ' and ';
00397             $sSelect .= '((oxorderid is not NULL and oxorderid != "") or (oxdateused is not NULL and oxdateused != 0)) ';
00398 
00399             if ( $myDB->getOne( $sSelect )) {
00400                 $oEx = oxNew( 'oxVoucherException' );
00401                 $oEx->setMessage('EXCEPTION_VOUCHER_NOTAVAILABLEINOTHERORDER');
00402                 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00403                 throw $oEx;
00404             }
00405         }
00406 
00407         return true;
00408     }
00409 
00419     protected function _isValidUserGroup( $oUser )
00420     {
00421         $oVoucherSerie = $this->getSerie();
00422         $oUserGroups = $oVoucherSerie->setUserGroups();
00423 
00424         // dodger Task #1555 R Voucher does not work for not logged user?
00425         if ( !$oUserGroups->count() ) {
00426             return true;
00427         }
00428 
00429         if ( $oUser ) {
00430             foreach ( $oUserGroups as $oGroup ) {
00431                 if ( $oUser->inGroup( $oGroup->getId() ) ) {
00432                     return true;
00433                 }
00434             }
00435         }
00436 
00437         $oEx = oxNew( 'oxVoucherException' );
00438         $oEx->setMessage( 'EXCEPTION_VOUCHER_NOTVALIDUSERGROUP' );
00439         $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00440         throw $oEx;
00441     }
00442 
00448     public function getSimpleVoucher()
00449     {
00450         $oVoucher = new oxStdClass();
00451         $oVoucher->sVoucherId = $this->getId();
00452         $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
00453         // R. setted in oxbasket : $oVoucher->fVoucherdiscount = oxLang::getInstance()->formatCurrency( $this->oxvouchers__oxdiscount->value );
00454 
00455         return $oVoucher;
00456     }
00457 
00463     public function getSerie()
00464     {
00465         if ($this->_oSerie !== null) {
00466             return $this->_oSerie;
00467         }
00468         $oSerie = oxNew('oxvoucherserie');
00469         if (!$oSerie->load($this->oxvouchers__oxvoucherserieid->value)) {
00470             throw oxNew( "oxObjectException" );
00471         }
00472         $this->_oSerie = $oSerie;
00473         return $oSerie;
00474     }
00475 
00481     protected function _isProductVoucher()
00482     {
00483         $myDB    = oxDb::getDb();
00484         $oSerie  = $this->getSerie();
00485         $sSelect = "select 1 from oxobject2discount where oxdiscountid = ".$myDB->quote( $oSerie->getId() )." and oxtype = 'oxarticles'";
00486         $blOk    = ( bool ) $myDB->getOne( $sSelect );
00487 
00488         return $blOk;
00489     }
00490 
00496     protected function _isCategoryVoucher()
00497     {
00498         $myDB    = oxDb::getDb();
00499         $oSerie  = $this->getSerie();
00500         $sSelect = "select 1 from oxobject2discount where oxdiscountid = ". $myDB->quote( $oSerie->getId() )." and oxtype = 'oxcategories'";
00501         $blOk    = ( bool ) $myDB->getOne( $sSelect );
00502 
00503         return $blOk;
00504     }
00505 
00511     protected function _getSerieDiscount( )
00512     {
00513         $oSerie    = $this->getSerie();
00514         $oDiscount = oxNew('oxDiscount');
00515 
00516         $oDiscount->setId($oSerie->getId());
00517         $oDiscount->oxdiscount__oxshopid      = new oxField($oSerie->oxvoucherseries__oxshopid->value);
00518         $oDiscount->oxdiscount__oxactive      = new oxField(true);
00519         $oDiscount->oxdiscount__oxactivefrom  = new oxField($oSerie->oxvoucherseries__oxbegindate->value);
00520         $oDiscount->oxdiscount__oxactiveto    = new oxField($oSerie->oxvoucherseries__oxenddate->value);
00521         $oDiscount->oxdiscount__oxtitle       = new oxField($oSerie->oxvoucherseries__oxserienr->value);
00522         $oDiscount->oxdiscount__oxamount      = new oxField(1);
00523         $oDiscount->oxdiscount__oxamountto    = new oxField(MAX_64BIT_INTEGER);
00524         $oDiscount->oxdiscount__oxprice       = new oxField(0);
00525         $oDiscount->oxdiscount__oxpriceto     = new oxField(MAX_64BIT_INTEGER);
00526         $oDiscount->oxdiscount__oxaddsumtype  = new oxField($oSerie->oxvoucherseries__oxdiscounttype->value=='percent'?'%':'abs');
00527         $oDiscount->oxdiscount__oxaddsum      = new oxField($oSerie->oxvoucherseries__oxdiscount->value);
00528         $oDiscount->oxdiscount__oxitmartid    = new oxField();
00529         $oDiscount->oxdiscount__oxitmamount   = new oxField();
00530         $oDiscount->oxdiscount__oxitmmultiple = new oxField();
00531 
00532         return $oDiscount;
00533     }
00534 
00542     protected function _getBasketItems($oDiscount = null)
00543     {
00544         if ($this->oxvouchers__oxorderid->value) {
00545             return $this->_getOrderBasketItems($oDiscount);
00546         } elseif ( $this->getSession()->getBasket() ) {
00547             return $this->_getSessionBasketItems($oDiscount);
00548         } else {
00549             return array();
00550         }
00551     }
00552 
00560     protected function _getOrderBasketItems($oDiscount = null)
00561     {
00562         if (is_null($oDiscount)) {
00563             $oDiscount = $this->_getSerieDiscount();
00564         }
00565 
00566         $oOrder = oxNew('oxorder');
00567         $oOrder->load($this->oxvouchers__oxorderid->value);
00568 
00569         $aItems  = array();
00570         $iCount  = 0;
00571 
00572         foreach ( $oOrder->getOrderArticles(true) as $oOrderArticle ) {
00573             if (!$oOrderArticle->skipDiscounts() && $oDiscount->isForBasketItem($oOrderArticle)) {
00574                 $aItems[$iCount] = array(
00575                     'oxid'     => $oOrderArticle->getProductId(),
00576                     'price'    => $oOrderArticle->oxorderarticles__oxprice->value,
00577                     'discount' => $oDiscount->getAbsValue($oOrderArticle->oxorderarticles__oxprice->value),
00578                     'amount'   => $oOrderArticle->oxorderarticles__oxamount->value,
00579                 );
00580                 $iCount ++;
00581             }
00582         }
00583 
00584         return $aItems;
00585     }
00586 
00594     protected function _getSessionBasketItems($oDiscount = null)
00595     {
00596         if (is_null($oDiscount)) {
00597             $oDiscount = $this->_getSerieDiscount();
00598         }
00599 
00600         $oBasket = $this->getSession()->getBasket();
00601         $aItems  = array();
00602         $iCount  = 0;
00603 
00604         foreach ( $oBasket->getContents() as $oBasketItem ) {
00605             if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) && !$oArticle->skipDiscounts() && $oDiscount->isForBasketItem($oArticle) ) {
00606 
00607                 $aItems[$iCount] = array(
00608                     'oxid'     => $oArticle->getId(),
00609                     'price'    => $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getBruttoPrice(),
00610                     'discount' => $oDiscount->getAbsValue($oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getBruttoPrice()),
00611                     'amount'   => $oBasketItem->getAmount(),
00612                 );
00613 
00614                 $iCount ++;
00615             }
00616         }
00617 
00618         return $aItems;
00619     }
00620 
00630     protected function _getGenericDiscoutValue( $dPrice )
00631     {
00632         $oSerie = $this->getSerie();
00633         if ( $oSerie->oxvoucherseries__oxdiscounttype->value == 'absolute' ) {
00634             $oCur = $this->getConfig()->getActShopCurrencyObject();
00635             $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value * $oCur->rate;
00636         } else {
00637             $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
00638         }
00639 
00640         if ( $dDiscount > $dPrice ) {
00641             $oEx = oxNew( 'oxVoucherException' );
00642             $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00643             $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00644             throw $oEx;
00645         }
00646 
00647         return $dDiscount;
00648     }
00649 
00659     protected function _getProductDiscoutValue( $dPrice )
00660     {
00661         $oDiscount    = $this->_getSerieDiscount();
00662         $aBasketItems = $this->_getBasketItems($oDiscount);
00663 
00664         // Basket Item Count and isAdmin check (unble to access property $oOrder->_getOrderBasket()->_blSkipVouchersAvailabilityChecking)
00665         if (!count($aBasketItems) && !$this->isAdmin() ) {
00666             $oEx = oxNew( 'oxVoucherException' );
00667             $oEx->setMessage('EXCEPTION_VOUCHER_NOVOUCHER');
00668             $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00669             throw $oEx;
00670         }
00671 
00672         $oSerie    = $this->getSerie();
00673 
00674         $oVoucherPrice  = oxNew('oxPrice');
00675         $oDiscountPrice = oxNew('oxPrice');
00676         $oProductPrice  = oxNew('oxPrice');
00677         $oProductTotal  = oxNew('oxPrice');
00678 
00679         foreach ( $aBasketItems as $aBasketItem ) {
00680 
00681             $oDiscountPrice->setPrice($aBasketItem['discount']);
00682             $oProductPrice->setPrice($aBasketItem['price']);
00683 
00684             // Individual voucher is not multiplied by article amount
00685             if (!$oSerie->oxvoucherseries__oxcalculateonce->value) {
00686                 $oDiscountPrice->multiply($aBasketItem['amount']);
00687                 $oProductPrice->multiply($aBasketItem['amount']);
00688             }
00689 
00690             $oVoucherPrice->add($oDiscountPrice->getBruttoPrice());
00691             $oProductTotal->add($oProductPrice->getBruttoPrice());
00692         }
00693 
00694         $dVoucher = $oVoucherPrice->getBruttoPrice();
00695         $dProduct = $oProductTotal->getBruttoPrice();
00696 
00697         if ( $dVoucher > $dProduct ) {
00698             return $dProduct;
00699         }
00700 
00701         return $dVoucher;
00702     }
00703 
00713     protected function _getCategoryDiscoutValue( $dPrice )
00714     {
00715         $oDiscount    = $this->_getSerieDiscount();
00716         $aBasketItems = $this->_getBasketItems( $oDiscount );
00717 
00718         // Basket Item Count and isAdmin check (unble to access property $oOrder->_getOrderBasket()->_blSkipVouchersAvailabilityChecking)
00719         if ( !count( $aBasketItems ) && !$this->isAdmin() ) {
00720             $oEx = oxNew( 'oxVoucherException' );
00721             $oEx->setMessage('EXCEPTION_VOUCHER_NOVOUCHER');
00722             $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00723             throw $oEx;
00724         }
00725 
00726         $oProductPrice = oxNew('oxPrice');
00727         $oProductTotal = oxNew('oxPrice');
00728 
00729         foreach ( $aBasketItems as $aBasketItem ) {
00730             $oProductPrice->setPrice( $aBasketItem['price'] );
00731             $oProductPrice->multiply( $aBasketItem['amount'] );
00732             $oProductTotal->add( $oProductPrice->getBruttoPrice() );
00733         }
00734 
00735         $dProduct = $oProductTotal->getBruttoPrice();
00736         $dVoucher = $oDiscount->getAbsValue( $dProduct );
00737         return ( $dVoucher > $dProduct ) ? $dProduct : $dVoucher;
00738     }
00739 
00747     public function __get( $sName )
00748     {
00749         switch ( $sName ) {
00750 
00751             // simple voucher mapping
00752             case 'sVoucherId':
00753                 return $this->getId();
00754                 break;
00755             case 'sVoucherNr':
00756                 return $this->oxvouchers__oxvouchernr;
00757                 break;
00758             case 'fVoucherdiscount':
00759                 return $this->oxvouchers__oxdiscount;
00760                 break;
00761         }
00762         return parent::__get($sName);
00763     }
00764 }