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 $oDb = oxDb::getDb();
00053
00054 $sQ = "select {$sViewName}.* from {$sViewName}, {$sSeriesViewName} where
00055 {$sSeriesViewName}.oxid = {$sViewName}.oxvoucherserieid and
00056 {$sViewName}.oxvouchernr = " . $oDb->quote( $sVoucherNr ) . " and ";
00057
00058 if ( is_array( $aVouchers ) ) {
00059 foreach ( $aVouchers as $sVoucherId => $sSkipVoucherNr ) {
00060 $sQ .= "{$sViewName}.oxid != " . $oDb->quote( $sVoucherId ) . " and ";
00061 }
00062 }
00063 $sQ .= "( {$sViewName}.oxorderid is NULL || {$sViewName}.oxorderid = '' ) ";
00064 $sQ .= " and ( {$sViewName}.oxdateused is NULL || {$sViewName}.oxdateused = 0 ) ";
00065
00066
00067 if ( $blCheckavalability ) {
00068 $iTime = time() - 3600 * 3;
00069 $sQ .= " and {$sViewName}.oxreserved < '{$iTime}' ";
00070 }
00071
00072 $sQ .= " limit 1";
00073
00074 if ( ! ( $oRet = $this->assignRecord( $sQ ) ) ) {
00075 $oEx = oxNew( 'oxVoucherException' );
00076 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOVOUCHER' );
00077 $oEx->setVoucherNr( $sVoucherNr );
00078 throw $oEx;
00079 }
00080 }
00081
00082 return $oRet;
00083 }
00084
00094 public function markAsUsed( $sOrderId, $sUserId, $dDiscount )
00095 {
00096
00097 if ( $this->oxvouchers__oxid->value ) {
00098 $this->oxvouchers__oxorderid->setValue($sOrderId);
00099 $this->oxvouchers__oxuserid->setValue($sUserId);
00100 $this->oxvouchers__oxdiscount->setValue($dDiscount);
00101 $this->oxvouchers__oxdateused->setValue(date( "Y-m-d", oxUtilsDate::getInstance()->getTime() ));
00102 $this->save();
00103 }
00104 }
00105
00111 public function markAsReserved()
00112 {
00113
00114 $sVoucherID = $this->oxvouchers__oxid->value;
00115
00116 if ( $sVoucherID ) {
00117 $oDb = oxDb::getDb();
00118 $sQ = "update oxvouchers set oxreserved = " . time() . " where oxid = " . $oDb->quote( $sVoucherID );
00119 $oDb->Execute( $sQ );
00120 }
00121 }
00122
00128 public function unMarkAsReserved()
00129 {
00130
00131 $sVoucherID = $this->oxvouchers__oxid->value;
00132
00133 if ( $sVoucherID ) {
00134 $oDb = oxDb::getDb();
00135 $sQ = "update oxvouchers set oxreserved = 0 where oxid = " . $oDb->quote( $sVoucherID );
00136 $oDb->Execute($sQ);
00137 }
00138 }
00139
00149 public function getDiscountValue( $dPrice )
00150 {
00151 if ($this->_isProductVoucher()) {
00152 return $this->_getProductDiscoutValue( (double) $dPrice );
00153 } elseif ($this->_isCategoryVoucher()) {
00154 return $this->_getCategoryDiscoutValue( (double) $dPrice );
00155 } else {
00156 return $this->_getGenericDiscoutValue( (double) $dPrice );
00157 }
00158 }
00159
00160
00171 public function checkVoucherAvailability( $aVouchers, $dPrice )
00172 {
00173 $this->_isAvailableWithSameSeries( $aVouchers );
00174 $this->_isAvailableWithOtherSeries( $aVouchers );
00175 $this->_isValidDate();
00176 $this->_isAvailablePrice( $dPrice );
00177 $this->_isNotReserved();
00178
00179
00180 return true;
00181 }
00182
00194 public function checkBasketVoucherAvailability( $aVouchers, $dPrice )
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 if ( $this->getDiscountValue( $dPrice ) < 0 ) {
00217 $oEx = oxNew( 'oxVoucherException' );
00218 $oEx->setMessage('EXCEPTION_VOUCHER_TOTALBELOWZERO');
00219 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00220 throw $oEx;
00221 }
00222 $oSerie = $this->getSerie();
00223 $oCur = $this->getConfig()->getActShopCurrencyObject();
00224 if ( $oSerie->oxvoucherseries__oxminimumvalue->value && $dPrice < ($oSerie->oxvoucherseries__oxminimumvalue->value*$oCur->rate) ) {
00225 $oEx = oxNew( 'oxVoucherException' );
00226 $oEx->setMessage('EXCEPTION_VOUCHER_INCORRECTPRICE');
00227 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->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 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00261 throw $oEx;
00262 }
00263 }
00264 }
00265 }
00266
00267 return true;
00268 }
00269
00280 protected function _isAvailableWithOtherSeries( $aVouchers )
00281 {
00282 if ( is_array( $aVouchers ) && count($aVouchers) ) {
00283 $oSerie = $this->getSerie();
00284 $sIds = implode(',', oxDb::getInstance()->quoteArray( array_keys( $aVouchers ) ) );
00285 $blAvailable = true;
00286 $oDb = oxDb::getDb();
00287 if (!$oSerie->oxvoucherseries__oxallowotherseries->value) {
00288
00289 $sSql = "select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
00290 $sSql .= "oxvouchers.oxvoucherserieid != " . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value ) ;
00291 $blAvailable &= !$oDb->getOne($sSql);
00292 } else {
00293
00294 $sSql = "select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
00295 $sSql .= "where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != " . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value );
00296 $sSql .= "and not oxvoucherseries.oxallowotherseries";
00297 $blAvailable &= !$oDb->getOne($sSql);
00298 }
00299 if ( !$blAvailable ) {
00300 $oEx = oxNew( 'oxVoucherException' );
00301 $oEx->setMessage('EXCEPTION_VOUCHER_NOTALLOWEDOTHERSERIES');
00302 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00303 throw $oEx;
00304 }
00305 }
00306
00307 return true;
00308 }
00309
00317 protected function _isValidDate()
00318 {
00319 $oSerie = $this->getSerie();
00320
00321
00322 $iTomorrow = mktime( 0, 0, 0, date( "m" ), date( "d" )+1, date( "Y" ) );
00323 $iYesterday = mktime( 0, 0, 0, date( "m" ), date( "d" )-1, date( "Y" ) );
00324
00325
00326 $iFrom = ( (int)$oSerie->oxvoucherseries__oxbegindate->value ) ?
00327 strtotime( $oSerie->oxvoucherseries__oxbegindate->value ) : $iYesterday;
00328
00329
00330 $iTo = ( (int)$oSerie->oxvoucherseries__oxenddate->value ) ?
00331 strtotime( $oSerie->oxvoucherseries__oxenddate->value ) : $iTomorrow;
00332
00333 if ( $iFrom < time() && $iTo > time() ) {
00334 return true;
00335 }
00336
00337 $oEx = oxNew( 'oxVoucherException' );
00338 $oEx->setMessage('EXCEPTION_VOUCHER_ISNOTVALIDDATE');
00339 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00340 throw $oEx;
00341 }
00342
00350 protected function _isNotReserved()
00351 {
00352
00353 if ( $this->oxvouchers__oxreserved->value < time() - 3600 * 3 ) {
00354 return true;
00355 }
00356
00357 $oEx = oxNew( 'oxVoucherException' );
00358 $oEx->setMessage('EXCEPTION_VOUCHER_ISRESERVED');
00359 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00360 throw $oEx;
00361 }
00362
00363
00373 public function checkUserAvailability( $oUser )
00374 {
00375
00376 $this->_isAvailableInOtherOrder( $oUser );
00377 $this->_isValidUserGroup( $oUser );
00378
00379
00380 return true;
00381 }
00382
00392 protected function _isAvailableInOtherOrder( $oUser )
00393 {
00394 $oSerie = $this->getSerie();
00395 if ( !$oSerie->oxvoucherseries__oxallowuseanother->value ) {
00396
00397 $oDb = oxDb::getDb();
00398 $sSelect = 'select count(*) from '.$this->getViewName().' where oxuserid = '. $oDb->quote( $oUser->oxuser__oxid->value ) . ' and ';
00399 $sSelect .= 'oxvoucherserieid = ' . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value ) . ' and ';
00400 $sSelect .= '((oxorderid is not NULL and oxorderid != "") or (oxdateused is not NULL and oxdateused != 0)) ';
00401
00402 if ( $oDb->getOne( $sSelect )) {
00403 $oEx = oxNew( 'oxVoucherException' );
00404 $oEx->setMessage('EXCEPTION_VOUCHER_NOTAVAILABLEINOTHERORDER');
00405 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00406 throw $oEx;
00407 }
00408 }
00409
00410 return true;
00411 }
00412
00422 protected function _isValidUserGroup( $oUser )
00423 {
00424 $oVoucherSerie = $this->getSerie();
00425 $oUserGroups = $oVoucherSerie->setUserGroups();
00426
00427
00428 if ( !$oUserGroups->count() ) {
00429 return true;
00430 }
00431
00432 if ( $oUser ) {
00433 foreach ( $oUserGroups as $oGroup ) {
00434 if ( $oUser->inGroup( $oGroup->getId() ) ) {
00435 return true;
00436 }
00437 }
00438 }
00439
00440 $oEx = oxNew( 'oxVoucherException' );
00441 $oEx->setMessage( 'EXCEPTION_VOUCHER_NOTVALIDUSERGROUP' );
00442 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
00443 throw $oEx;
00444 }
00445
00451 public function getSimpleVoucher()
00452 {
00453 $oVoucher = new oxStdClass();
00454 $oVoucher->sVoucherId = $this->getId();
00455 $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
00456
00457
00458 return $oVoucher;
00459 }
00460
00466 public function getSerie()
00467 {
00468 if ($this->_oSerie !== null) {
00469 return $this->_oSerie;
00470 }
00471 $oSerie = oxNew('oxvoucherserie');
00472 if (!$oSerie->load($this->oxvouchers__oxvoucherserieid->value)) {
00473 throw oxNew( "oxObjectException" );
00474 }
00475 $this->_oSerie = $oSerie;
00476 return $oSerie;
00477 }
00478
00484 protected function _isProductVoucher()
00485 {
00486 $oDb = oxDb::getDb();
00487 $oSerie = $this->getSerie();
00488 $sSelect = "select 1 from oxobject2discount where oxdiscountid = ".$oDb->quote( $oSerie->getId() )." and oxtype = 'oxarticles'";
00489 $blOk = ( bool ) $oDb->getOne( $sSelect );
00490
00491 return $blOk;
00492 }
00493
00499 protected function _isCategoryVoucher()
00500 {
00501 $oDb = oxDb::getDb();
00502 $oSerie = $this->getSerie();
00503 $sSelect = "select 1 from oxobject2discount where oxdiscountid = ". $oDb->quote( $oSerie->getId() )." and oxtype = 'oxcategories'";
00504 $blOk = ( bool ) $oDb->getOne( $sSelect );
00505
00506 return $blOk;
00507 }
00508
00514 protected function _getSerieDiscount( )
00515 {
00516 $oSerie = $this->getSerie();
00517 $oDiscount = oxNew('oxDiscount');
00518
00519 $oDiscount->setId($oSerie->getId());
00520 $oDiscount->oxdiscount__oxshopid = new oxField($oSerie->oxvoucherseries__oxshopid->value);
00521 $oDiscount->oxdiscount__oxactive = new oxField(true);
00522 $oDiscount->oxdiscount__oxactivefrom = new oxField($oSerie->oxvoucherseries__oxbegindate->value);
00523 $oDiscount->oxdiscount__oxactiveto = new oxField($oSerie->oxvoucherseries__oxenddate->value);
00524 $oDiscount->oxdiscount__oxtitle = new oxField($oSerie->oxvoucherseries__oxserienr->value);
00525 $oDiscount->oxdiscount__oxamount = new oxField(1);
00526 $oDiscount->oxdiscount__oxamountto = new oxField(MAX_64BIT_INTEGER);
00527 $oDiscount->oxdiscount__oxprice = new oxField(0);
00528 $oDiscount->oxdiscount__oxpriceto = new oxField(MAX_64BIT_INTEGER);
00529 $oDiscount->oxdiscount__oxaddsumtype = new oxField($oSerie->oxvoucherseries__oxdiscounttype->value=='percent'?'%':'abs');
00530 $oDiscount->oxdiscount__oxaddsum = new oxField($oSerie->oxvoucherseries__oxdiscount->value);
00531 $oDiscount->oxdiscount__oxitmartid = new oxField();
00532 $oDiscount->oxdiscount__oxitmamount = new oxField();
00533 $oDiscount->oxdiscount__oxitmmultiple = new oxField();
00534
00535 return $oDiscount;
00536 }
00537
00545 protected function _getBasketItems($oDiscount = null)
00546 {
00547 if ($this->oxvouchers__oxorderid->value) {
00548 return $this->_getOrderBasketItems($oDiscount);
00549 } elseif ( $this->getSession()->getBasket() ) {
00550 return $this->_getSessionBasketItems($oDiscount);
00551 } else {
00552 return array();
00553 }
00554 }
00555
00563 protected function _getOrderBasketItems($oDiscount = null)
00564 {
00565 if (is_null($oDiscount)) {
00566 $oDiscount = $this->_getSerieDiscount();
00567 }
00568
00569 $oOrder = oxNew('oxorder');
00570 $oOrder->load($this->oxvouchers__oxorderid->value);
00571
00572 $aItems = array();
00573 $iCount = 0;
00574
00575 foreach ( $oOrder->getOrderArticles(true) as $oOrderArticle ) {
00576 if (!$oOrderArticle->skipDiscounts() && $oDiscount->isForBasketItem($oOrderArticle)) {
00577 $aItems[$iCount] = array(
00578 'oxid' => $oOrderArticle->getProductId(),
00579 'price' => $oOrderArticle->oxorderarticles__oxprice->value,
00580 'discount' => $oDiscount->getAbsValue($oOrderArticle->oxorderarticles__oxprice->value),
00581 'amount' => $oOrderArticle->oxorderarticles__oxamount->value,
00582 );
00583 $iCount ++;
00584 }
00585 }
00586
00587 return $aItems;
00588 }
00589
00597 protected function _getSessionBasketItems($oDiscount = null)
00598 {
00599 if (is_null($oDiscount)) {
00600 $oDiscount = $this->_getSerieDiscount();
00601 }
00602
00603 $oBasket = $this->getSession()->getBasket();
00604 $aItems = array();
00605 $iCount = 0;
00606
00607 foreach ( $oBasket->getContents() as $oBasketItem ) {
00608 if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) && !$oArticle->skipDiscounts() && $oDiscount->isForBasketItem($oArticle) ) {
00609
00610 $aItems[$iCount] = array(
00611 'oxid' => $oArticle->getId(),
00612 'price' => $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getBruttoPrice(),
00613 'discount' => $oDiscount->getAbsValue($oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getBruttoPrice()),
00614 'amount' => $oBasketItem->getAmount(),
00615 );
00616
00617 $iCount ++;
00618 }
00619 }
00620
00621 return $aItems;
00622 }
00623
00633 protected function _getGenericDiscoutValue( $dPrice )
00634 {
00635 $oSerie = $this->getSerie();
00636 if ( $oSerie->oxvoucherseries__oxdiscounttype->value == 'absolute' ) {
00637 $oCur = $this->getConfig()->getActShopCurrencyObject();
00638 $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value * $oCur->rate;
00639 } else {
00640 $dDiscount = $oSerie->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
00641 }
00642
00643 if ( $dDiscount > $dPrice ) {
00644 $dDiscount = $dPrice;
00645 }
00646
00647 return $dDiscount;
00648 }
00649
00659 protected function _getProductDiscoutValue( $dPrice )
00660 {
00661 $oDiscount = $this->_getSerieDiscount();
00662 $aBasketItems = $this->_getBasketItems($oDiscount);
00663
00664
00665 if (!count($aBasketItems) && !$this->isAdmin() ) {
00666 $oEx = oxNew( 'oxVoucherException' );
00667 $oEx->setMessage('EXCEPTION_VOUCHER_NOVOUCHER');
00668 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00669 throw $oEx;
00670 }
00671
00672 $oSerie = $this->getSerie();
00673
00674 $oVoucherPrice = oxNew('oxPrice');
00675 $oDiscountPrice = oxNew('oxPrice');
00676 $oProductPrice = oxNew('oxPrice');
00677 $oProductTotal = oxNew('oxPrice');
00678
00679 foreach ( $aBasketItems as $aBasketItem ) {
00680
00681 $oDiscountPrice->setPrice($aBasketItem['discount']);
00682 $oProductPrice->setPrice($aBasketItem['price']);
00683
00684
00685 if (!$oSerie->oxvoucherseries__oxcalculateonce->value) {
00686 $oDiscountPrice->multiply($aBasketItem['amount']);
00687 $oProductPrice->multiply($aBasketItem['amount']);
00688 }
00689
00690 $oVoucherPrice->add($oDiscountPrice->getBruttoPrice());
00691 $oProductTotal->add($oProductPrice->getBruttoPrice());
00692 }
00693
00694 $dVoucher = $oVoucherPrice->getBruttoPrice();
00695 $dProduct = $oProductTotal->getBruttoPrice();
00696
00697 if ( $dVoucher > $dProduct ) {
00698 return $dProduct;
00699 }
00700
00701 return $dVoucher;
00702 }
00703
00713 protected function _getCategoryDiscoutValue( $dPrice )
00714 {
00715 $oDiscount = $this->_getSerieDiscount();
00716 $aBasketItems = $this->_getBasketItems( $oDiscount );
00717
00718
00719 if ( !count( $aBasketItems ) && !$this->isAdmin() ) {
00720 $oEx = oxNew( 'oxVoucherException' );
00721 $oEx->setMessage('EXCEPTION_VOUCHER_NOVOUCHER');
00722 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
00723 throw $oEx;
00724 }
00725
00726 $oProductPrice = oxNew('oxPrice');
00727 $oProductTotal = oxNew('oxPrice');
00728
00729 foreach ( $aBasketItems as $aBasketItem ) {
00730 $oProductPrice->setPrice( $aBasketItem['price'] );
00731 $oProductPrice->multiply( $aBasketItem['amount'] );
00732 $oProductTotal->add( $oProductPrice->getBruttoPrice() );
00733 }
00734
00735 $dProduct = $oProductTotal->getBruttoPrice();
00736 $dVoucher = $oDiscount->getAbsValue( $dProduct );
00737 return ( $dVoucher > $dProduct ) ? $dProduct : $dVoucher;
00738 }
00739
00747 public function __get( $sName )
00748 {
00749 switch ( $sName ) {
00750
00751
00752 case 'sVoucherId':
00753 return $this->getId();
00754 break;
00755 case 'sVoucherNr':
00756 return $this->oxvouchers__oxvouchernr;
00757 break;
00758 case 'fVoucherdiscount':
00759 return $this->oxvouchers__oxdiscount;
00760 break;
00761 }
00762 return parent::__get($sName);
00763 }
00764 }