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 = " . oxDb::getDb()->quote( $sVoucherNr ) . " and ";
00056
00057 if ( is_array( $aVouchers ) ) {
00058 foreach ( $aVouchers as $sVoucherId => $sSkipVoucherNr ) {
00059 $sQ .= "{$sViewName}.oxid != " . oxDb::getDb()->quote( $sVoucherId ) . " and ";
00060 }
00061 }
00062 $sQ .= "( {$sViewName}.oxorderid is NULL || {$sViewName}.oxorderid = '' ) ";
00063
00064
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
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
00111 $sVoucherID = $this->oxvouchers__oxid->value;
00112
00113 if ( $sVoucherID ) {
00114 $sQ = "update oxvouchers set oxreserved = " . time() . " where oxid = " . oxDb::getDb()->quote( $sVoucherID );
00115 oxDb::getDb()->Execute( $sQ );
00116 }
00117 }
00118
00124 public function unMarkAsReserved()
00125 {
00126
00127 $sVoucherID = $this->oxvouchers__oxid->value;
00128
00129 if ( $sVoucherID ) {
00130 $sQ = "update oxvouchers set oxreserved = 0 where oxid = " . oxDb::getDb()->quote( $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
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
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
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 $oCur = $this->getConfig()->getActShopCurrencyObject();
00228 if ( $oSerie->oxvoucherseries__oxminimumvalue->value && $dPrice < ($oSerie->oxvoucherseries__oxminimumvalue->value*$oCur->rate) ) {
00229 $oEx = oxNew( 'oxVoucherException' );
00230 $oEx->setMessage('EXCEPTION_VOUCHER_INCORRECTPRICE');
00231 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00232 throw $oEx;
00233 }
00234
00235 return true;
00236 }
00237
00249 protected function _isAvailableWithSameSeries( $aVouchers )
00250 {
00251 if ( is_array( $aVouchers ) ) {
00252 $sId = $this->getId();
00253 if (isset($aVouchers[$sId])) {
00254 unset($aVouchers[$sId]);
00255 }
00256 $oSerie = $this->getSerie();
00257 if (!$oSerie->oxvoucherseries__oxallowsameseries->value) {
00258 foreach ( $aVouchers as $voucherId => $voucherNr ) {
00259 $oVoucher = oxNew( 'oxvoucher' );
00260 $oVoucher->load($voucherId);
00261 if ( $this->oxvouchers__oxvoucherserieid->value == $oVoucher->oxvouchers__oxvoucherserieid->value ) {
00262 $oEx = oxNew( 'oxVoucherException' );
00263 $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDSAMESERIES');
00264 throw $oEx;
00265 }
00266 }
00267 }
00268 }
00269
00270 return true;
00271 }
00272
00283 protected function _isAvailableWithOtherSeries( $aVouchers )
00284 {
00285 if ( is_array( $aVouchers ) && count($aVouchers) ) {
00286 $oSerie = $this->getSerie();
00287 $sIds = '\''.implode('\',\'', array_keys($aVouchers)).'\'';
00288 $blAvailable = true;
00289 if (!$oSerie->oxvoucherseries__oxallowotherseries->value) {
00290
00291 $sSql = "select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
00292 $sSql .= "oxvouchers.oxvoucherserieid != " . oxDb::getDb()->quote( $this->oxvouchers__oxvoucherserieid->value ) ;
00293 $blAvailable &= !oxDb::getDb()->getOne($sSql);
00294 } else {
00295
00296 $sSql = "select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
00297 $sSql .= "where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != " . oxDb::getDb()->quote( $this->oxvouchers__oxvoucherserieid->value );
00298 $sSql .= "and not oxvoucherseries.oxallowotherseries";
00299 $blAvailable &= !oxDb::getDb()->getOne($sSql);
00300 }
00301 if ( !$blAvailable ) {
00302 $oEx = oxNew( 'oxVoucherException' );
00303 $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDOTHERSERIES');
00304 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00305 throw $oEx;
00306 }
00307 }
00308
00309 return true;
00310 }
00311
00319 protected function _isValidDate()
00320 {
00321 $oSerie = $this->getSerie();
00322
00323 $sDefTimeStamp = oxUtilsDate::getInstance()->formatDBDate( '-' );
00324 if ( $oSerie->oxvoucherseries__oxbegindate->value == $sDefTimeStamp &&
00325 $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00326 return true;
00327 }
00328
00329 if ( ( strtotime( $oSerie->oxvoucherseries__oxbegindate->value ) < time() &&
00330 strtotime( $oSerie->oxvoucherseries__oxenddate->value ) > time() ) ||
00331 !$oSerie->oxvoucherseries__oxenddate->value ||
00332 $oSerie->oxvoucherseries__oxenddate->value == $sDefTimeStamp ) {
00333 return true;
00334 }
00335
00336 $oEx = oxNew( 'oxVoucherException' );
00337 $oEx->setMessage('EXCEPTION_VOUCHER_ISNOTVALIDDATE');
00338 throw $oEx;
00339 }
00340
00348 protected function _isNotReserved()
00349 {
00350
00351 if ( $this->oxvouchers__oxreserved->value < time() - 3600 * 3 ) {
00352 return true;
00353 }
00354
00355 $oEx = oxNew( 'oxVoucherException' );
00356 $oEx->setMessage('EXCEPTION_VOUCHER_ISRESERVED');
00357 throw $oEx;
00358 }
00359
00360
00370 public function checkUserAvailability( $oUser )
00371 {
00372
00373 $this->_isAvailableInOtherOrder( $oUser );
00374 $this->_isValidUserGroup( $oUser );
00375
00376
00377 return true;
00378 }
00379
00389 protected function _isAvailableInOtherOrder( $oUser )
00390 {
00391 $oSerie = $this->getSerie();
00392 if ( !$oSerie->oxvoucherseries__oxallowuseanother->value ) {
00393
00394 $sSelect = 'select count(*) from '.$this->getViewName().' where oxuserid = '. oxDb::getDb()->quote( $oUser->oxuser__oxid->value ) . ' and ';
00395 $sSelect .= 'oxvoucherserieid = ' . oxDb::getDb()->quote( $this->oxvouchers__oxvoucherserieid->value ) . ' and ';
00396 $sSelect .= 'oxorderid is not NULL and oxorderid != "" ';
00397
00398 if ( oxDb::getDb()->getOne( $sSelect )) {
00399 $oEx = oxNew( 'oxVoucherException' );
00400 $oEx->setMessage('EXCEPTION_VOUCHER_NOTAVAILABLEINOTHERORDER');
00401 $oEx->setVoucherNr($this->oxVouchers__voucherNr->value);
00402 throw $oEx;
00403 }
00404 }
00405
00406 return true;
00407 }
00408
00418 protected function _isValidUserGroup( $oUser )
00419 {
00420 $oVoucherSerie = $this->getSerie();
00421 $oUserGroups = $oVoucherSerie->setUserGroups();
00422
00423
00424 if ( !$oUserGroups->count() ) {
00425 return true;
00426 }
00427
00428 if ( $oUser ) {
00429 foreach ( $oUserGroups as $oGroup ) {
00430 if ( $oUser->inGroup( $oGroup->getId() ) ) {
00431 return true;
00432 }
00433 }
00434 }
00435
00436 $oEx = oxNew( 'oxVoucherException' );
00437 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOTVALIDUSERGROUP' );
00438 $oEx->setVoucherNr( $this->oxvouchers__vouchernr->value );
00439 throw $oEx;
00440 }
00441
00447 public function getSimpleVoucher()
00448 {
00449 $oVoucher = new OxstdClass();
00450 $oVoucher->sVoucherId = $this->getId();
00451 $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
00452
00453
00454 return $oVoucher;
00455 }
00456
00462 public function getSerie()
00463 {
00464 if ($this->_oSerie !== null) {
00465 return $this->_oSerie;
00466 }
00467 $oSerie = oxNew('oxvoucherserie');
00468 if (!$oSerie->load($this->oxvouchers__oxvoucherserieid->value)) {
00469 throw new oxObjectException();
00470 }
00471 $this->_oSerie = $oSerie;
00472 return $oSerie;
00473 }
00474 }