00001 <?php
00002
00009 class oxVoucher extends oxBase
00010 {
00011
00012 protected $_oSerie = null;
00013
00019 protected $_blDisableShopCheck = true;
00020
00024 protected $_sClassName = 'oxvoucher';
00025
00029 public function __construct()
00030 {
00031 parent::__construct();
00032 $this->init( 'oxvouchers' );
00033 }
00034
00046 public function getVoucherByNr( $voucherNr, $aVouchers = array(), $blCheckavalability = false )
00047 {
00048 if ( is_null($voucherNr) ) {
00049 return;
00050 }
00051
00052
00053 $sQ = 'select * from '.$this->getViewName().' where oxvouchernr = "'.$voucherNr.'" and ';
00054 foreach( $aVouchers as $sVoucherId => $sVoucherNr ) {
00055 $sQ .= 'oxid != "'.$sVoucherId.'" and ';
00056 }
00057 $sQ .= '(oxorderid is NULL || oxorderid = "") ';
00058
00059
00060 if ( $blCheckavalability ) {
00061 $iTime = time() - 3600 * 3;
00062 $sQ .= ' and oxreserved < "'.$iTime.'" ';
00063 }
00064
00065 $sQ .= " limit 1";
00066
00067
00068 if ( ! ( $oRet = $this->assignRecord( $sQ ) ) ) {
00069 $oEx = oxNew('oxVoucherException' );
00070 $oEx->setMessage('EXCEPTION_VOUCHER_NOVOUCHER');
00071 $oEx->setVoucherNr($voucherNr);
00072 throw $oEx;
00073 }
00074
00075 return $oRet;
00076 }
00077
00086 public function markAsUsed( $sOrderId, $sUserId )
00087 {
00088
00089 if ( $this->oxvouchers__oxid->value ) {
00090 $this->oxvouchers__oxorderid->setValue($sOrderId);
00091 $this->oxvouchers__oxuserid->setValue($sUserId);
00092 $this->oxvouchers__oxdateused->setValue(date( "Y-m-d", oxUtilsDate::getInstance()->getTime() ));
00093 $this->save();
00094 }
00095 }
00096
00102 public function markAsReserved()
00103 {
00104
00105 $sVoucherID = $this->oxvouchers__oxid->value;
00106
00107 if ( $sVoucherID ) {
00108 $sQ = 'update oxvouchers set oxreserved = '.time().' where oxid = "'.$sVoucherID.'"';
00109 oxDb::getDb()->Execute( $sQ );
00110 }
00111 }
00112
00118 public function unMarkAsReserved()
00119 {
00120
00121 $sVoucherID = $this->oxvouchers__oxid->value;
00122
00123 if ( $sVoucherID ) {
00124 $sQ = 'update oxvouchers set oxreserved = 0 where oxid = "'.$sVoucherID.'"';
00125 oxDb::getDb()->Execute($sQ);
00126 }
00127 }
00128
00138 public function getDiscountValue( $dPrice )
00139 {
00140 $oSerie = $this->getSerie();
00141 if ( $oSerie->oxvoucherseries__oxdiscounttype->value == 'absolute' ) {
00142 $oCur = $this->getConfig()->getActShopCurrencyObject();
00143 $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value * $oCur->rate;
00144 } else {
00145 $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
00146 }
00147
00148 if ( $dDiscount > $dPrice ) {
00149 $oEx = oxNew( 'oxVoucherException' );
00150 $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00151 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00152 throw $oEx;
00153 }
00154
00155 return $dDiscount;
00156 }
00157
00158
00169 public function checkVoucherAvailability( $aVouchers, $dPrice )
00170 {
00171
00172 $this->_isAvailableWithSameSeries( $aVouchers );
00173 $this->_isAvailableWithOtherSeries( $aVouchers );
00174 $this->_isValidDate();
00175 $this->_isAvailablePrice( $dPrice );
00176 $this->_isNotReserved();
00177
00178
00179 return true;
00180 }
00181
00193 public function checkBasketVoucherAvailability( $aVouchers, $dPrice )
00194 {
00195
00196 $this->_isAvailableWithSameSeries( $aVouchers );
00197 $this->_isAvailableWithOtherSeries( $aVouchers );
00198 $this->_isValidDate();
00199 $this->_isAvailablePrice( $dPrice );
00200
00201
00202 return true;
00203 }
00204
00214 protected function _isAvailablePrice( $dPrice )
00215 {
00216
00217 if ( $this->getDiscountValue( $dPrice ) < 0 ) {
00218 $oEx = oxNew( 'oxVoucherException' );
00219 $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00220 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00221 throw $oEx;
00222 }
00223 $oSerie = $this->getSerie();
00224 if ( $oSerie->oxvoucherseries__oxminimumvalue->value && $dPrice < $oSerie->oxvoucherseries__oxminimumvalue->value ) {
00225 $oEx = oxNew( 'oxVoucherException' );
00226 $oEx->setMessage('EXCEPTION_VOUCHER_INCORRECTPRICE');
00227 $oEx->setVoucherNr($this->oxVouchers__voucherNr->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 throw $oEx;
00261 }
00262 }
00263 }
00264 }
00265
00266 return true;
00267 }
00268
00279 protected function _isAvailableWithOtherSeries( $aVouchers )
00280 {
00281 if ( is_array( $aVouchers ) && count($aVouchers) ) {
00282 $oSerie = $this->getSerie();
00283 $sIds = '\''.implode('\',\'', array_keys($aVouchers)).'\'';
00284 $blAvailable = true;
00285 if (!$oSerie->oxvoucherseries__oxallowotherseries->value) {
00286
00287 $sSql = "select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
00288 $sSql .= "oxvouchers.oxvoucherserieid != '{$this->oxvouchers__oxvoucherserieid->value}'";
00289 $blAvailable &= !oxDb::getDb()->getOne($sSql);
00290 } else {
00291
00292 $sSql = "select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
00293 $sSql .= "where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != '{$this->oxvouchers__oxvoucherserieid->value}' ";
00294 $sSql .= "and not oxvoucherseries.oxallowotherseries";
00295 $blAvailable &= !oxDb::getDb()->getOne($sSql);
00296 }
00297 if ( !$blAvailable ) {
00298 $oEx = oxNew( 'oxVoucherException' );
00299 $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDOTHERSERIES');
00300 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00301 throw $oEx;
00302 }
00303 }
00304
00305 return true;
00306 }
00307
00315 protected function _isValidDate()
00316 {
00317 $oSerie = $this->getSerie();
00318
00319 $sDefTimeStamp = oxUtilsDate::getInstance()->formatDBDate( '-' );
00320 if ( $oSerie->oxvoucherseries__oxbegindate->value == $sDefTimeStamp &&
00321 $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00322 return true;
00323 }
00324
00325 if ( ( strtotime( $oSerie->oxvoucherseries__oxbegindate->value ) < time() &&
00326 strtotime( $oSerie->oxvoucherseries__oxenddate->value ) > time() ) ||
00327 !$oSerie->oxvoucherseries__oxenddate->value ||
00328 $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00329 return true;
00330 }
00331
00332 $oEx = oxNew( 'oxVoucherException' );
00333 $oEx->setMessage('EXCEPTION_VOUCHER_ISNOTVALIDDATE');
00334 throw $oEx;
00335 }
00336
00344 protected function _isNotReserved()
00345 {
00346
00347 if ( $this->oxvouchers__oxreserved->value < time() - 3600 * 3 ) {
00348 return true;
00349 }
00350
00351 $oEx = oxNew( 'oxVoucherException' );
00352 $oEx->setMessage('EXCEPTION_VOUCHER_ISRESERVED');
00353 throw $oEx;
00354 }
00355
00356
00366 public function checkUserAvailability( $oUser )
00367 {
00368
00369 $this->_isAvailableInOtherOrder( $oUser );
00370 $this->_isValidUserGroup( $oUser );
00371
00372
00373 return true;
00374 }
00375
00385 protected function _isAvailableInOtherOrder( $oUser )
00386 {
00387 $oSerie = $this->getSerie();
00388 if ( !$oSerie->oxvoucherseries__oxallowuseanother->value ) {
00389
00390 $sSelect = 'select count(*) from '.$this->getViewName().' where oxuserid = "'.$oUser->oxuser__oxid->value.'" and ';
00391 $sSelect .= 'oxvoucherserieid = "'.$this->oxvouchers__oxvoucherserieid->value.'" and ';
00392 $sSelect .= 'oxorderid is not NULL and oxorderid != "" ';
00393
00394 if ( oxDb::getDb()->getOne( $sSelect )) {
00395 $oEx = oxNew( 'oxVoucherException' );
00396 $oEx->setMessage('EXCEPTION_VOUCHER_NOTAVAILABLEINOTHERORDER');
00397 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00398 throw $oEx;
00399 }
00400 }
00401
00402 return true;
00403 }
00404
00414 protected function _isValidUserGroup( $oUser )
00415 {
00416 $oVoucherSerie = $this->getSerie();
00417 $oUserGroups = $oVoucherSerie->setUserGroups();
00418
00419
00420 if ( !$oUserGroups->count() ) {
00421 return true;
00422 }
00423
00424 if ( $oUser ) {
00425 foreach ( $oUserGroups as $oGroup ) {
00426 if ( $oUser->inGroup( $oGroup->getId() ) ) {
00427 return true;
00428 }
00429 }
00430 }
00431
00432 $oEx = oxNew( 'oxVoucherException' );
00433 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOTVALIDUSERGROUP' );
00434 $oEx->setVoucherNr( $this->oxvouchers__vouchernr->value );
00435 throw $oEx;
00436 }
00437
00443 public function getSimpleVoucher()
00444 {
00445 $oVoucher = new OxstdClass();
00446 $oVoucher->sVoucherId = $this->getId();
00447 $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
00448
00449
00450 return $oVoucher;
00451 }
00452
00458 public function getSerie()
00459 {
00460 if ($this->_oSerie !== null) {
00461 return $this->_oSerie;
00462 }
00463 $oSerie = oxNew('oxvoucherserie');
00464 if (!$oSerie->load($this->oxvouchers__oxvoucherserieid->value)) {
00465 throw new oxObjectException();
00466 }
00467 $this->_oSerie = $oSerie;
00468 return $oSerie;
00469 }
00470 }