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, $dDiscount )
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__oxdiscount->setValue($dDiscount);
00098             $this->oxvouchers__oxdateused->setValue(date( "Y-m-d", oxUtilsDate::getInstance()->getTime() ));
00099             $this->save();
00100         }
00101     }
00102 
00108     public function markAsReserved()
00109     {
00110         //saving oxreserved field
00111         $sVoucherID = $this->oxvouchers__oxid->value;
00112 
00113         if ( $sVoucherID ) {
00114             $sQ = 'update oxvouchers set oxreserved = '.time().' where oxid = "'.$sVoucherID.'"';
00115             oxDb::getDb()->Execute( $sQ );
00116         }
00117     }
00118 
00124     public function unMarkAsReserved()
00125     {
00126         //saving oxreserved field
00127         $sVoucherID = $this->oxvouchers__oxid->value;
00128 
00129         if ( $sVoucherID ) {
00130             $sQ = 'update oxvouchers set oxreserved = 0 where oxid = "'.$sVoucherID.'"';
00131             oxDb::getDb()->Execute($sQ);
00132         }
00133     }
00134 
00144     public function getDiscountValue( $dPrice )
00145     {
00146         $oSerie = $this->getSerie();
00147         if ( $oSerie->oxvoucherseries__oxdiscounttype->value == 'absolute' ) {
00148             $oCur = $this->getConfig()->getActShopCurrencyObject();
00149             $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value * $oCur->rate;
00150         } else {
00151             $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
00152         }
00153 
00154         if ( $dDiscount > $dPrice ) {
00155             $oEx = oxNew( 'oxVoucherException' );
00156             $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00157             $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00158             throw $oEx;
00159         }
00160 
00161         return $dDiscount;
00162     }
00163 
00164     // Checking General Availability
00175     public function checkVoucherAvailability( $aVouchers, $dPrice )
00176     {
00177         $this->_isAvailableWithSameSeries( $aVouchers );
00178         $this->_isAvailableWithOtherSeries( $aVouchers );
00179         $this->_isValidDate();
00180         $this->_isAvailablePrice( $dPrice );
00181         $this->_isNotReserved();
00182 
00183         // returning true - no exception was thrown
00184         return true;
00185     }
00186 
00198     public function checkBasketVoucherAvailability( $aVouchers, $dPrice )
00199     {
00200         $this->_isAvailableWithSameSeries( $aVouchers );
00201         $this->_isAvailableWithOtherSeries( $aVouchers );
00202         $this->_isValidDate();
00203         $this->_isAvailablePrice( $dPrice );
00204 
00205         // returning true - no exception was thrown
00206         return true;
00207     }
00208 
00218     protected function _isAvailablePrice( $dPrice )
00219     {
00220         if ( $this->getDiscountValue( $dPrice ) < 0 ) {
00221             $oEx = oxNew( 'oxVoucherException' );
00222             $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00223             $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00224             throw $oEx;
00225         }
00226         $oSerie = $this->getSerie();
00227         if ( $oSerie->oxvoucherseries__oxminimumvalue->value && $dPrice < $oSerie->oxvoucherseries__oxminimumvalue->value ) {
00228             $oEx = oxNew( 'oxVoucherException' );
00229             $oEx->setMessage('EXCEPTION_VOUCHER_INCORRECTPRICE');
00230             $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00231             throw $oEx;
00232         }
00233 
00234         return true;
00235     }
00236 
00248     protected function _isAvailableWithSameSeries( $aVouchers )
00249     {
00250         if ( is_array( $aVouchers ) ) {
00251             $sId = $this->getId();
00252             if (isset($aVouchers[$sId])) {
00253                 unset($aVouchers[$sId]);
00254             }
00255             $oSerie = $this->getSerie();
00256             if (!$oSerie->oxvoucherseries__oxallowsameseries->value) {
00257                 foreach ( $aVouchers as $voucherId => $voucherNr ) {
00258                     $oVoucher = oxNew( 'oxvoucher' );
00259                     $oVoucher->load($voucherId);
00260                     if ( $this->oxvouchers__oxvoucherserieid->value == $oVoucher->oxvouchers__oxvoucherserieid->value ) {
00261                             $oEx = oxNew( 'oxVoucherException' );
00262                             $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDSAMESERIES');
00263                             throw $oEx;
00264                     }
00265                 }
00266             }
00267         }
00268 
00269         return true;
00270     }
00271 
00282     protected function _isAvailableWithOtherSeries( $aVouchers )
00283     {
00284         if ( is_array( $aVouchers ) && count($aVouchers) ) {
00285             $oSerie = $this->getSerie();
00286             $sIds = '\''.implode('\',\'', array_keys($aVouchers)).'\'';
00287             $blAvailable = true;
00288             if (!$oSerie->oxvoucherseries__oxallowotherseries->value) {
00289                 // just search for vouchers with different series
00290                 $sSql  = "select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
00291                 $sSql .= "oxvouchers.oxvoucherserieid != '{$this->oxvouchers__oxvoucherserieid->value}'";
00292                 $blAvailable &= !oxDb::getDb()->getOne($sSql);
00293             } else {
00294                 // search for vouchers with different series and those vouchers do not allow other series
00295                 $sSql  = "select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
00296                 $sSql .= "where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != '{$this->oxvouchers__oxvoucherserieid->value}' ";
00297                 $sSql .= "and not oxvoucherseries.oxallowotherseries";
00298                 $blAvailable &= !oxDb::getDb()->getOne($sSql);
00299             }
00300             if ( !$blAvailable ) {
00301                     $oEx = oxNew( 'oxVoucherException' );
00302                     $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDOTHERSERIES');
00303                     $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00304                     throw $oEx;
00305             }
00306         }
00307 
00308         return true;
00309     }
00310 
00318     protected function _isValidDate()
00319     {
00320         $oSerie = $this->getSerie();
00321         // if time is not set - keep it as active by default
00322         $sDefTimeStamp = oxUtilsDate::getInstance()->formatDBDate( '-' );
00323         if ( $oSerie->oxvoucherseries__oxbegindate->value == $sDefTimeStamp &&
00324              $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00325             return true;
00326         }
00327 
00328         if ( ( strtotime( $oSerie->oxvoucherseries__oxbegindate->value ) < time() &&
00329               strtotime( $oSerie->oxvoucherseries__oxenddate->value ) > time() ) ||
00330             !$oSerie->oxvoucherseries__oxenddate->value ||
00331             $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00332             return true;
00333         }
00334 
00335         $oEx = oxNew( 'oxVoucherException' );
00336         $oEx->setMessage('EXCEPTION_VOUCHER_ISNOTVALIDDATE');
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         throw $oEx;
00357     }
00358 
00359     // Checking User Availability
00369     public function checkUserAvailability( $oUser )
00370     {
00371 
00372         $this->_isAvailableInOtherOrder( $oUser );
00373         $this->_isValidUserGroup( $oUser );
00374 
00375         // returning true if no exception was thrown
00376         return true;
00377     }
00378 
00388     protected function _isAvailableInOtherOrder( $oUser )
00389     {
00390         $oSerie = $this->getSerie();
00391         if ( !$oSerie->oxvoucherseries__oxallowuseanother->value ) {
00392 
00393             $sSelect  = 'select count(*) from '.$this->getViewName().' where oxuserid = "'.$oUser->oxuser__oxid->value.'" and ';
00394             $sSelect .= 'oxvoucherserieid = "'.$this->oxvouchers__oxvoucherserieid->value.'" and ';
00395             $sSelect .= 'oxorderid is not NULL and oxorderid != "" ';
00396 
00397             if ( oxDb::getDb()->getOne( $sSelect )) {
00398                 $oEx = oxNew( 'oxVoucherException' );
00399                 $oEx->setMessage('EXCEPTION_VOUCHER_NOTAVAILABLEINOTHERORDER');
00400                 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00401                 throw $oEx;
00402             }
00403         }
00404 
00405         return true;
00406     }
00407 
00417     protected function _isValidUserGroup( $oUser )
00418     {
00419         $oVoucherSerie = $this->getSerie();
00420         $oUserGroups = $oVoucherSerie->setUserGroups();
00421 
00422         // dodger Task #1555 R Voucher does not work for not logged user?
00423         if ( !$oUserGroups->count() ) {
00424             return true;
00425         }
00426 
00427         if ( $oUser ) {
00428             foreach ( $oUserGroups as $oGroup ) {
00429                 if ( $oUser->inGroup( $oGroup->getId() ) ) {
00430                     return true;
00431                 }
00432             }
00433         }
00434 
00435         $oEx = oxNew( 'oxVoucherException' );
00436         $oEx->setMessage( 'EXCEPTION_VOUCHER_NOTVALIDUSERGROUP' );
00437         $oEx->setVoucherNr( $this->oxvouchers__vouchernr->value );
00438         throw $oEx;
00439     }
00440 
00446     public function getSimpleVoucher()
00447     {
00448         $oVoucher = new OxstdClass();
00449         $oVoucher->sVoucherId = $this->getId();
00450         $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
00451         // R. setted in oxbasket : $oVoucher->fVoucherdiscount = oxLang::getInstance()->formatCurrency( $this->oxvouchers__oxdiscount->value );
00452 
00453         return $oVoucher;
00454     }
00455 
00461     public function getSerie()
00462     {
00463         if ($this->_oSerie !== null) {
00464             return $this->_oSerie;
00465         }
00466         $oSerie = oxNew('oxvoucherserie');
00467         if (!$oSerie->load($this->oxvouchers__oxvoucherserieid->value)) {
00468             throw new oxObjectException();
00469         }
00470         $this->_oSerie = $oSerie;
00471         return $oSerie;
00472     }
00473 }

Generated on Tue Aug 18 09:21:06 2009 for OXID eShop CE by  doxygen 1.5.5