Go to the documentation of this file.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( $sVoucherNr, $aVouchers = array(), $blCheckavalability = false )
00047 {
00048 $oRet = null;
00049 if ( !is_null( $sVoucherNr ) ) {
00050
00051 $sViewName = $this->getViewName();
00052 $sSeriesViewName = getViewName( 'oxvoucherseries' );
00053 $oDb = oxDb::getDb();
00054
00055 $sQ = "select {$sViewName}.* from {$sViewName}, {$sSeriesViewName} where
00056 {$sSeriesViewName}.oxid = {$sViewName}.oxvoucherserieid and
00057 {$sViewName}.oxvouchernr = " . $oDb->quote( $sVoucherNr ) . " and ";
00058
00059 if ( is_array( $aVouchers ) ) {
00060 foreach ( $aVouchers as $sVoucherId => $sSkipVoucherNr ) {
00061 $sQ .= "{$sViewName}.oxid != " . $oDb->quote( $sVoucherId ) . " and ";
00062 }
00063 }
00064 $sQ .= "( {$sViewName}.oxorderid is NULL || {$sViewName}.oxorderid = '' ) ";
00065 $sQ .= " and ( {$sViewName}.oxdateused is NULL || {$sViewName}.oxdateused = 0 ) ";
00066
00067
00068 if ( $blCheckavalability ) {
00069 $iTime = time() - 3600 * 3;
00070 $sQ .= " and {$sViewName}.oxreserved < '{$iTime}' ";
00071 }
00072
00073 $sQ .= " limit 1";
00074
00075 if ( ! ( $oRet = $this->assignRecord( $sQ ) ) ) {
00076 $oEx = oxNew( 'oxVoucherException' );
00077 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOVOUCHER' );
00078 $oEx->setVoucherNr( $sVoucherNr );
00079 throw $oEx;
00080 }
00081 }
00082
00083 return $oRet;
00084 }
00085
00095 public function markAsUsed( $sOrderId, $sUserId, $dDiscount )
00096 {
00097
00098 if ( $this->oxvouchers__oxid->value ) {
00099 $this->oxvouchers__oxorderid->setValue($sOrderId);
00100 $this->oxvouchers__oxuserid->setValue($sUserId);
00101 $this->oxvouchers__oxdiscount->setValue($dDiscount);
00102 $this->oxvouchers__oxdateused->setValue(date( "Y-m-d", oxRegistry::get("oxUtilsDate")->getTime() ));
00103 $this->save();
00104 }
00105 }
00106
00112 public function markAsReserved()
00113 {
00114
00115 $sVoucherID = $this->oxvouchers__oxid->value;
00116
00117 if ( $sVoucherID ) {
00118 $oDb = oxDb::getDb();
00119 $sQ = "update oxvouchers set oxreserved = " . time() . " where oxid = " . $oDb->quote( $sVoucherID );
00120 $oDb->Execute( $sQ );
00121 }
00122 }
00123
00129 public function unMarkAsReserved()
00130 {
00131
00132 $sVoucherID = $this->oxvouchers__oxid->value;
00133
00134 if ( $sVoucherID ) {
00135 $oDb = oxDb::getDb();
00136 $sQ = "update oxvouchers set oxreserved = 0 where oxid = " . $oDb->quote( $sVoucherID );
00137 $oDb->Execute($sQ);
00138 }
00139 }
00140
00150 public function getDiscountValue( $dPrice )
00151 {
00152 if ($this->_isProductVoucher()) {
00153 return $this->_getProductDiscoutValue( (double) $dPrice );
00154 } elseif ($this->_isCategoryVoucher()) {
00155 return $this->_getCategoryDiscoutValue( (double) $dPrice );
00156 } else {
00157 return $this->_getGenericDiscoutValue( (double) $dPrice );
00158 }
00159 }
00160
00161
00172 public function checkVoucherAvailability( $aVouchers, $dPrice )
00173 {
00174 $this->_isAvailableWithSameSeries( $aVouchers );
00175 $this->_isAvailableWithOtherSeries( $aVouchers );
00176 $this->_isValidDate();
00177 $this->_isAvailablePrice( $dPrice );
00178 $this->_isNotReserved();
00179
00180
00181 return true;
00182 }
00183
00195 public function checkBasketVoucherAvailability( $aVouchers, $dPrice )
00196 {
00197 $this->_isAvailableWithSameSeries( $aVouchers );
00198 $this->_isAvailableWithOtherSeries( $aVouchers );
00199 $this->_isValidDate();
00200 $this->_isAvailablePrice( $dPrice );
00201
00202
00203 return true;
00204 }
00205
00215 protected function _isAvailablePrice( $dPrice )
00216 {
00217 $oSerie = $this->getSerie();
00218 $oCur = $this->getConfig()->getActShopCurrencyObject();
00219 if ( $oSerie->oxvoucherseries__oxminimumvalue->value && $dPrice < ($oSerie->oxvoucherseries__oxminimumvalue->value*$oCur->rate) ) {
00220 $oEx = oxNew( 'oxVoucherException' );
00221 $oEx->setMessage('EXCEPTION_VOUCHER_INCORRECTPRICE');
00222 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00223 throw $oEx;
00224 }
00225
00226 return true;
00227 }
00228
00240 protected function _isAvailableWithSameSeries( $aVouchers )
00241 {
00242 if ( is_array( $aVouchers ) ) {
00243 $sId = $this->getId();
00244 if (isset($aVouchers[$sId])) {
00245 unset($aVouchers[$sId]);
00246 }
00247 $oSerie = $this->getSerie();
00248 if (!$oSerie->oxvoucherseries__oxallowsameseries->value) {
00249 foreach ( $aVouchers as $voucherId => $voucherNr ) {
00250 $oVoucher = oxNew( 'oxvoucher' );
00251 $oVoucher->load($voucherId);
00252 if ( $this->oxvouchers__oxvoucherserieid->value == $oVoucher->oxvouchers__oxvoucherserieid->value ) {
00253 $oEx = oxNew( 'oxVoucherException' );
00254 $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDSAMESERIES');
00255 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00256 throw $oEx;
00257 }
00258 }
00259 }
00260 }
00261
00262 return true;
00263 }
00264
00275 protected function _isAvailableWithOtherSeries( $aVouchers )
00276 {
00277 if ( is_array( $aVouchers ) && count($aVouchers) ) {
00278 $oSerie = $this->getSerie();
00279 $sIds = implode(',', oxDb::getInstance()->quoteArray( array_keys( $aVouchers ) ) );
00280 $blAvailable = true;
00281 $oDb = oxDb::getDb();
00282 if (!$oSerie->oxvoucherseries__oxallowotherseries->value) {
00283
00284 $sSql = "select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
00285 $sSql .= "oxvouchers.oxvoucherserieid != " . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value ) ;
00286 $blAvailable &= !$oDb->getOne($sSql);
00287 } else {
00288
00289 $sSql = "select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
00290 $sSql .= "where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != " . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value );
00291 $sSql .= "and not oxvoucherseries.oxallowotherseries";
00292 $blAvailable &= !$oDb->getOne($sSql);
00293 }
00294 if ( !$blAvailable ) {
00295 $oEx = oxNew( 'oxVoucherException' );
00296 $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDOTHERSERIES');
00297 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00298 throw $oEx;
00299 }
00300 }
00301
00302 return true;
00303 }
00304
00312 protected function _isValidDate()
00313 {
00314 $oSerie = $this->getSerie();
00315
00316
00317 $iTomorrow = mktime( 0, 0, 0, date( "m" ), date( "d" )+1, date( "Y" ) );
00318 $iYesterday = mktime( 0, 0, 0, date( "m" ), date( "d" )-1, date( "Y" ) );
00319
00320
00321 $iFrom = ( (int)$oSerie->oxvoucherseries__oxbegindate->value ) ?
00322 strtotime( $oSerie->oxvoucherseries__oxbegindate->value ) : $iYesterday;
00323
00324
00325 $iTo = ( (int)$oSerie->oxvoucherseries__oxenddate->value ) ?
00326 strtotime( $oSerie->oxvoucherseries__oxenddate->value ) : $iTomorrow;
00327
00328 if ( $iFrom < time() && $iTo > time() ) {
00329 return true;
00330 }
00331
00332 $oEx = oxNew( 'oxVoucherException' );
00333 $oEx->setMessage('EXCEPTION_VOUCHER_ISNOTVALIDDATE');
00334 if ( $iFrom > time() && $iTo > time() ) {
00335 $oEx->setMessage('ERROR_MESSAGE_VOUCHER_NOVOUCHER');
00336 }
00337 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
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 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00358 throw $oEx;
00359 }
00360
00361
00371 public function checkUserAvailability( $oUser )
00372 {
00373
00374 $this->_isAvailableInOtherOrder( $oUser );
00375 $this->_isValidUserGroup( $oUser );
00376
00377
00378 return true;
00379 }
00380
00390 protected function _isAvailableInOtherOrder( $oUser )
00391 {
00392 $oSerie = $this->getSerie();
00393 if ( !$oSerie->oxvoucherseries__oxallowuseanother->value ) {
00394
00395 $oDb = oxDb::getDb();
00396 $sSelect = 'select count(*) from '.$this->getViewName().' where oxuserid = '. $oDb->quote( $oUser->oxuser__oxid->value ) . ' and ';
00397 $sSelect .= 'oxvoucherserieid = ' . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value ) . ' and ';
00398 $sSelect .= '((oxorderid is not NULL and oxorderid != "") or (oxdateused is not NULL and oxdateused != 0)) ';
00399
00400 if ( $oDb->getOne( $sSelect )) {
00401 $oEx = oxNew( 'oxVoucherException' );
00402 $oEx->setMessage('EXCEPTION_VOUCHER_NOTAVAILABLEINOTHERORDER');
00403 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00404 throw $oEx;
00405 }
00406 }
00407
00408 return true;
00409 }
00410
00420 protected function _isValidUserGroup( $oUser )
00421 {
00422 $oVoucherSerie = $this->getSerie();
00423 $oUserGroups = $oVoucherSerie->setUserGroups();
00424
00425
00426 if ( !$oUserGroups->count() ) {
00427 return true;
00428 }
00429
00430 if ( $oUser ) {
00431 foreach ( $oUserGroups as $oGroup ) {
00432 if ( $oUser->inGroup( $oGroup->getId() ) ) {
00433 return true;
00434 }
00435 }
00436 }
00437
00438 $oEx = oxNew( 'oxVoucherException' );
00439 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOTVALIDUSERGROUP' );
00440 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00441 throw $oEx;
00442 }
00443
00449 public function getSimpleVoucher()
00450 {
00451 $oVoucher = new stdClass();
00452 $oVoucher->sVoucherId = $this->getId();
00453 $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
00454
00455
00456 return $oVoucher;
00457 }
00458
00464 public function getSerie()
00465 {
00466 if ($this->_oSerie !== null) {
00467 return $this->_oSerie;
00468 }
00469 $oSerie = oxNew('oxvoucherserie');
00470 if (!$oSerie->load($this->oxvouchers__oxvoucherserieid->value)) {
00471 throw oxNew( "oxObjectException" );
00472 }
00473 $this->_oSerie = $oSerie;
00474 return $oSerie;
00475 }
00476
00482 protected function _isProductVoucher()
00483 {
00484 $oDb = oxDb::getDb();
00485 $oSerie = $this->getSerie();
00486 $sSelect = "select 1 from oxobject2discount where oxdiscountid = ".$oDb->quote( $oSerie->getId() )." and oxtype = 'oxarticles'";
00487 $blOk = ( bool ) $oDb->getOne( $sSelect );
00488
00489 return $blOk;
00490 }
00491
00497 protected function _isCategoryVoucher()
00498 {
00499 $oDb = oxDb::getDb();
00500 $oSerie = $this->getSerie();
00501 $sSelect = "select 1 from oxobject2discount where oxdiscountid = ". $oDb->quote( $oSerie->getId() )." and oxtype = 'oxcategories'";
00502 $blOk = ( bool ) $oDb->getOne( $sSelect );
00503
00504 return $blOk;
00505 }
00506
00512 protected function _getSerieDiscount( )
00513 {
00514 $oSerie = $this->getSerie();
00515 $oDiscount = oxNew('oxDiscount');
00516
00517 $oDiscount->setId($oSerie->getId());
00518 $oDiscount->oxdiscount__oxshopid = new oxField($oSerie->oxvoucherseries__oxshopid->value);
00519 $oDiscount->oxdiscount__oxactive = new oxField(true);
00520 $oDiscount->oxdiscount__oxactivefrom = new oxField($oSerie->oxvoucherseries__oxbegindate->value);
00521 $oDiscount->oxdiscount__oxactiveto = new oxField($oSerie->oxvoucherseries__oxenddate->value);
00522 $oDiscount->oxdiscount__oxtitle = new oxField($oSerie->oxvoucherseries__oxserienr->value);
00523 $oDiscount->oxdiscount__oxamount = new oxField(1);
00524 $oDiscount->oxdiscount__oxamountto = new oxField(MAX_64BIT_INTEGER);
00525 $oDiscount->oxdiscount__oxprice = new oxField(0);
00526 $oDiscount->oxdiscount__oxpriceto = new oxField(MAX_64BIT_INTEGER);
00527 $oDiscount->oxdiscount__oxaddsumtype = new oxField($oSerie->oxvoucherseries__oxdiscounttype->value=='percent'?'%':'abs');
00528 $oDiscount->oxdiscount__oxaddsum = new oxField($oSerie->oxvoucherseries__oxdiscount->value);
00529 $oDiscount->oxdiscount__oxitmartid = new oxField();
00530 $oDiscount->oxdiscount__oxitmamount = new oxField();
00531 $oDiscount->oxdiscount__oxitmmultiple = new oxField();
00532
00533 return $oDiscount;
00534 }
00535
00543 protected function _getBasketItems($oDiscount = null)
00544 {
00545 if ($this->oxvouchers__oxorderid->value) {
00546 return $this->_getOrderBasketItems($oDiscount);
00547 } elseif ( $this->getSession()->getBasket() ) {
00548 return $this->_getSessionBasketItems($oDiscount);
00549 } else {
00550 return array();
00551 }
00552 }
00553
00561 protected function _getOrderBasketItems($oDiscount = null)
00562 {
00563 if (is_null($oDiscount)) {
00564 $oDiscount = $this->_getSerieDiscount();
00565 }
00566
00567 $oOrder = oxNew('oxorder');
00568 $oOrder->load($this->oxvouchers__oxorderid->value);
00569
00570 $aItems = array();
00571 $iCount = 0;
00572
00573 foreach ( $oOrder->getOrderArticles(true) as $oOrderArticle ) {
00574 if (!$oOrderArticle->skipDiscounts() && $oDiscount->isForBasketItem($oOrderArticle)) {
00575 $aItems[$iCount] = array(
00576 'oxid' => $oOrderArticle->getProductId(),
00577 'price' => $oOrderArticle->oxorderarticles__oxprice->value,
00578 'discount' => $oDiscount->getAbsValue($oOrderArticle->oxorderarticles__oxprice->value),
00579 'amount' => $oOrderArticle->oxorderarticles__oxamount->value,
00580 );
00581 $iCount ++;
00582 }
00583 }
00584
00585 return $aItems;
00586 }
00587
00595 protected function _getSessionBasketItems($oDiscount = null)
00596 {
00597 if (is_null($oDiscount)) {
00598 $oDiscount = $this->_getSerieDiscount();
00599 }
00600
00601 $oBasket = $this->getSession()->getBasket();
00602 $aItems = array();
00603 $iCount = 0;
00604
00605 foreach ( $oBasket->getContents() as $oBasketItem ) {
00606 if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) && !$oArticle->skipDiscounts() && $oDiscount->isForBasketItem($oArticle) ) {
00607
00608 $aItems[$iCount] = array(
00609 'oxid' => $oArticle->getId(),
00610 'price' => $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getPrice(),
00611 'discount' => $oDiscount->getAbsValue($oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getPrice()),
00612 'amount' => $oBasketItem->getAmount(),
00613 );
00614
00615 $iCount ++;
00616 }
00617 }
00618
00619 return $aItems;
00620 }
00621
00631 protected function _getGenericDiscoutValue( $dPrice )
00632 {
00633 $oSerie = $this->getSerie();
00634 if ( $oSerie->oxvoucherseries__oxdiscounttype->value == 'absolute' ) {
00635 $oCur = $this->getConfig()->getActShopCurrencyObject();
00636 $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value * $oCur->rate;
00637 } else {
00638 $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
00639 }
00640
00641 if ( $dDiscount > $dPrice ) {
00642 $dDiscount = $dPrice;
00643 }
00644
00645 return $dDiscount;
00646 }
00647
00648
00654 public function getDiscount()
00655 {
00656 $oSerie = $this->getSerie();
00657 return $oSerie->oxvoucherseries__oxdiscount->value;
00658 }
00659
00665 public function getDiscountType()
00666 {
00667 $oSerie = $this->getSerie();
00668 return $oSerie->oxvoucherseries__oxdiscounttype->value;
00669 }
00670
00671
00681 protected function _getProductDiscoutValue( $dPrice )
00682 {
00683 $oDiscount = $this->_getSerieDiscount();
00684 $aBasketItems = $this->_getBasketItems($oDiscount);
00685
00686
00687 if (!count($aBasketItems) && !$this->isAdmin() ) {
00688 $oEx = oxNew( 'oxVoucherException' );
00689 $oEx->setMessage('EXCEPTION_VOUCHER_NOVOUCHER');
00690 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00691 throw $oEx;
00692 }
00693
00694 $oSerie = $this->getSerie();
00695
00696 $oVoucherPrice = oxNew('oxPrice');
00697 $oDiscountPrice = oxNew('oxPrice');
00698 $oProductPrice = oxNew('oxPrice');
00699 $oProductTotal = oxNew('oxPrice');
00700
00701 foreach ( $aBasketItems as $aBasketItem ) {
00702
00703 $oDiscountPrice->setPrice($aBasketItem['discount']);
00704 $oProductPrice->setPrice($aBasketItem['price']);
00705
00706
00707 if (!$oSerie->oxvoucherseries__oxcalculateonce->value) {
00708 $oDiscountPrice->multiply($aBasketItem['amount']);
00709 $oProductPrice->multiply($aBasketItem['amount']);
00710 }
00711
00712 $oVoucherPrice->add($oDiscountPrice->getBruttoPrice());
00713 $oProductTotal->add($oProductPrice->getBruttoPrice());
00714 }
00715
00716 $dVoucher = $oVoucherPrice->getBruttoPrice();
00717 $dProduct = $oProductTotal->getBruttoPrice();
00718
00719 if ( $dVoucher > $dProduct ) {
00720 return $dProduct;
00721 }
00722
00723 return $dVoucher;
00724 }
00725
00735 protected function _getCategoryDiscoutValue( $dPrice )
00736 {
00737 $oDiscount = $this->_getSerieDiscount();
00738 $aBasketItems = $this->_getBasketItems( $oDiscount );
00739
00740
00741 if ( !count( $aBasketItems ) && !$this->isAdmin() ) {
00742 $oEx = oxNew( 'oxVoucherException' );
00743 $oEx->setMessage('EXCEPTION_VOUCHER_NOVOUCHER');
00744 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00745 throw $oEx;
00746 }
00747
00748 $oProductPrice = oxNew('oxPrice');
00749 $oProductTotal = oxNew('oxPrice');
00750
00751 foreach ( $aBasketItems as $aBasketItem ) {
00752 $oProductPrice->setPrice( $aBasketItem['price'] );
00753 $oProductPrice->multiply( $aBasketItem['amount'] );
00754 $oProductTotal->add( $oProductPrice->getBruttoPrice() );
00755 }
00756
00757 $dProduct = $oProductTotal->getBruttoPrice();
00758 $dVoucher = $oDiscount->getAbsValue( $dProduct );
00759 return ( $dVoucher > $dProduct ) ? $dProduct : $dVoucher;
00760 }
00761
00769 public function __get( $sName )
00770 {
00771 switch ( $sName ) {
00772
00773
00774 case 'sVoucherId':
00775 return $this->getId();
00776 break;
00777 case 'sVoucherNr':
00778 return $this->oxvouchers__oxvouchernr;
00779 break;
00780 case 'fVoucherdiscount':
00781 return $this->oxvouchers__oxdiscount;
00782 break;
00783 }
00784 return parent::__get($sName);
00785 }
00786 }