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 
00053             $sQ  = "select {$sViewName}.* from {$sViewName}, {$sSeriesViewName} where
00054                         {$sSeriesViewName}.oxid = {$sViewName}.oxvoucherserieid and
00055                         {$sViewName}.oxvouchernr = '{$sVoucherNr}' and ";
00056 
00057             if ( is_array( $aVouchers ) ) {
00058                 foreach ( $aVouchers as $sVoucherId => $sSkipVoucherNr ) {
00059                     $sQ .= "{$sViewName}.oxid != '$sVoucherId' and ";
00060                 }
00061             }
00062             $sQ .= "( {$sViewName}.oxorderid is NULL || {$sViewName}.oxorderid = '' ) ";
00063 
00064             //voucher timeout for 3 hours
00065             if ( $blCheckavalability ) {
00066                 $iTime = time() - 3600 * 3;
00067                 $sQ .= " and {$sViewName}.oxreserved < '{$iTime}' ";
00068             }
00069 
00070             $sQ .= " limit 1";
00071 
00072             if ( ! ( $oRet = $this->assignRecord( $sQ ) ) ) {
00073                 $oEx = oxNew( 'oxVoucherException' );
00074                 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOVOUCHER' );
00075                 $oEx->setVoucherNr( $sVoucherNr );
00076                 throw $oEx;
00077             }
00078         }
00079 
00080         return $oRet;
00081     }
00082 
00091     public function markAsUsed( $sOrderId, $sUserId )
00092     {
00093         //saving oxreserved field
00094         if ( $this->oxvouchers__oxid->value ) {
00095             $this->oxvouchers__oxorderid->setValue($sOrderId);
00096             $this->oxvouchers__oxuserid->setValue($sUserId);
00097             $this->oxvouchers__oxdateused->setValue(date( "Y-m-d", oxUtilsDate::getInstance()->getTime() ));
00098             $this->save();
00099         }
00100     }
00101 
00107     public function markAsReserved()
00108     {
00109         //saving oxreserved field
00110         $sVoucherID = $this->oxvouchers__oxid->value;
00111 
00112         if ( $sVoucherID ) {
00113             $sQ = 'update oxvouchers set oxreserved = '.time().' where oxid = "'.$sVoucherID.'"';
00114             oxDb::getDb()->Execute( $sQ );
00115         }
00116     }
00117 
00123     public function unMarkAsReserved()
00124     {
00125         //saving oxreserved field
00126         $sVoucherID = $this->oxvouchers__oxid->value;
00127 
00128         if ( $sVoucherID ) {
00129             $sQ = 'update oxvouchers set oxreserved = 0 where oxid = "'.$sVoucherID.'"';
00130             oxDb::getDb()->Execute($sQ);
00131         }
00132     }
00133 
00143     public function getDiscountValue( $dPrice )
00144     {
00145         $oSerie = $this->getSerie();
00146         if ( $oSerie->oxvoucherseries__oxdiscounttype->value == 'absolute' ) {
00147             $oCur = $this->getConfig()->getActShopCurrencyObject();
00148             $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value * $oCur->rate;
00149         } else {
00150             $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
00151         }
00152 
00153         if ( $dDiscount > $dPrice ) {
00154             $oEx = oxNew( 'oxVoucherException' );
00155             $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00156             $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00157             throw $oEx;
00158         }
00159 
00160         return $dDiscount;
00161     }
00162 
00163     // Checking General Availability
00174     public function checkVoucherAvailability( $aVouchers, $dPrice )
00175     {
00176         $this->_isAvailableWithSameSeries( $aVouchers );
00177         $this->_isAvailableWithOtherSeries( $aVouchers );
00178         $this->_isValidDate();
00179         $this->_isAvailablePrice( $dPrice );
00180         $this->_isNotReserved();
00181 
00182         // returning true - no exception was thrown
00183         return true;
00184     }
00185 
00197     public function checkBasketVoucherAvailability( $aVouchers, $dPrice )
00198     {
00199         $this->_isAvailableWithSameSeries( $aVouchers );
00200         $this->_isAvailableWithOtherSeries( $aVouchers );
00201         $this->_isValidDate();
00202         $this->_isAvailablePrice( $dPrice );
00203 
00204         // returning true - no exception was thrown
00205         return true;
00206     }
00207 
00217     protected function _isAvailablePrice( $dPrice )
00218     {
00219         if ( $this->getDiscountValue( $dPrice ) < 0 ) {
00220             $oEx = oxNew( 'oxVoucherException' );
00221             $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00222             $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00223             throw $oEx;
00224         }
00225         $oSerie = $this->getSerie();
00226         if ( $oSerie->oxvoucherseries__oxminimumvalue->value && $dPrice < $oSerie->oxvoucherseries__oxminimumvalue->value ) {
00227             $oEx = oxNew( 'oxVoucherException' );
00228             $oEx->setMessage('EXCEPTION_VOUCHER_INCORRECTPRICE');
00229             $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00230             throw $oEx;
00231         }
00232 
00233         return true;
00234     }
00235 
00247     protected function _isAvailableWithSameSeries( $aVouchers )
00248     {
00249         if ( is_array( $aVouchers ) ) {
00250             $sId = $this->getId();
00251             if (isset($aVouchers[$sId])) {
00252                 unset($aVouchers[$sId]);
00253             }
00254             $oSerie = $this->getSerie();
00255             if (!$oSerie->oxvoucherseries__oxallowsameseries->value) {
00256                 foreach ( $aVouchers as $voucherId => $voucherNr ) {
00257                     $oVoucher = oxNew( 'oxvoucher' );
00258                     $oVoucher->load($voucherId);
00259                     if ( $this->oxvouchers__oxvoucherserieid->value == $oVoucher->oxvouchers__oxvoucherserieid->value ) {
00260                             $oEx = oxNew( 'oxVoucherException' );
00261                             $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDSAMESERIES');
00262                             throw $oEx;
00263                     }
00264                 }
00265             }
00266         }
00267 
00268         return true;
00269     }
00270 
00281     protected function _isAvailableWithOtherSeries( $aVouchers )
00282     {
00283         if ( is_array( $aVouchers ) && count($aVouchers) ) {
00284             $oSerie = $this->getSerie();
00285             $sIds = '\''.implode('\',\'', array_keys($aVouchers)).'\'';
00286             $blAvailable = true;
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 != '{$this->oxvouchers__oxvoucherserieid->value}'";
00291                 $blAvailable &= !oxDb::getDb()->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 != '{$this->oxvouchers__oxvoucherserieid->value}' ";
00296                 $sSql .= "and not oxvoucherseries.oxallowotherseries";
00297                 $blAvailable &= !oxDb::getDb()->getOne($sSql);
00298             }
00299             if ( !$blAvailable ) {
00300                     $oEx = oxNew( 'oxVoucherException' );
00301                     $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDOTHERSERIES');
00302                     $oEx->setVoucherNr($this->oxVouchers__voucherNr->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         throw $oEx;
00337     }
00338 
00346     protected function _isNotReserved()
00347     {
00348 
00349         if ( $this->oxvouchers__oxreserved->value < time() - 3600 * 3 ) {
00350             return true;
00351         }
00352 
00353         $oEx = oxNew( 'oxVoucherException' );
00354         $oEx->setMessage('EXCEPTION_VOUCHER_ISRESERVED');
00355         throw $oEx;
00356     }
00357 
00358     // Checking User Availability
00368     public function checkUserAvailability( $oUser )
00369     {
00370 
00371         $this->_isAvailableInOtherOrder( $oUser );
00372         $this->_isValidUserGroup( $oUser );
00373 
00374         // returning true if no exception was thrown
00375         return true;
00376     }
00377 
00387     protected function _isAvailableInOtherOrder( $oUser )
00388     {
00389         $oSerie = $this->getSerie();
00390         if ( !$oSerie->oxvoucherseries__oxallowuseanother->value ) {
00391 
00392             $sSelect  = 'select count(*) from '.$this->getViewName().' where oxuserid = "'.$oUser->oxuser__oxid->value.'" and ';
00393             $sSelect .= 'oxvoucherserieid = "'.$this->oxvouchers__oxvoucherserieid->value.'" and ';
00394             $sSelect .= 'oxorderid is not NULL and oxorderid != "" ';
00395 
00396             if ( oxDb::getDb()->getOne( $sSelect )) {
00397                 $oEx = oxNew( 'oxVoucherException' );
00398                 $oEx->setMessage('EXCEPTION_VOUCHER_NOTAVAILABLEINOTHERORDER');
00399                 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00400                 throw $oEx;
00401             }
00402         }
00403 
00404         return true;
00405     }
00406 
00416     protected function _isValidUserGroup( $oUser )
00417     {
00418         $oVoucherSerie = $this->getSerie();
00419         $oUserGroups = $oVoucherSerie->setUserGroups();
00420 
00421         // dodger Task #1555 R Voucher does not work for not logged user?
00422         if ( !$oUserGroups->count() ) {
00423             return true;
00424         }
00425 
00426         if ( $oUser ) {
00427             foreach ( $oUserGroups as $oGroup ) {
00428                 if ( $oUser->inGroup( $oGroup->getId() ) ) {
00429                     return true;
00430                 }
00431             }
00432         }
00433 
00434         $oEx = oxNew( 'oxVoucherException' );
00435         $oEx->setMessage( 'EXCEPTION_VOUCHER_NOTVALIDUSERGROUP' );
00436         $oEx->setVoucherNr( $this->oxvouchers__vouchernr->value );
00437         throw $oEx;
00438     }
00439 
00445     public function getSimpleVoucher()
00446     {
00447         $oVoucher = new OxstdClass();
00448         $oVoucher->sVoucherId = $this->getId();
00449         $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
00450         // R. setted in oxbasket : $oVoucher->fVoucherdiscount = oxLang::getInstance()->formatCurrency( $this->oxvouchers__oxdiscount->value );
00451 
00452         return $oVoucher;
00453     }
00454 
00460     public function getSerie()
00461     {
00462         if ($this->_oSerie !== null) {
00463             return $this->_oSerie;
00464         }
00465         $oSerie = oxNew('oxvoucherserie');
00466         if (!$oSerie->load($this->oxvouchers__oxvoucherserieid->value)) {
00467             throw new oxObjectException();
00468         }
00469         $this->_oSerie = $oSerie;
00470         return $oSerie;
00471     }
00472 }

Generated on Wed May 13 13:25:52 2009 for OXID eShop CE by  doxygen 1.5.5