32 $this->
init(
'oxvouchers' );
46 public function getVoucherByNr( $sVoucherNr, $aVouchers = array(), $blCheckavalability =
false )
49 if ( !is_null( $sVoucherNr ) ) {
52 $sSeriesViewName =
getViewName(
'oxvoucherseries' );
55 $sQ =
"select {$sViewName}.* from {$sViewName}, {$sSeriesViewName} where
56 {$sSeriesViewName}.oxid = {$sViewName}.oxvoucherserieid and
57 {$sViewName}.oxvouchernr = " . $oDb->quote( $sVoucherNr ) .
" and ";
59 if ( is_array( $aVouchers ) ) {
60 foreach ( $aVouchers as $sVoucherId => $sSkipVoucherNr ) {
61 $sQ .=
"{$sViewName}.oxid != " . $oDb->quote( $sVoucherId ) .
" and ";
64 $sQ .=
"( {$sViewName}.oxorderid is NULL || {$sViewName}.oxorderid = '' ) ";
65 $sQ .=
" and ( {$sViewName}.oxdateused is NULL || {$sViewName}.oxdateused = 0 ) ";
68 if ( $blCheckavalability ) {
70 $sQ .=
" and {$sViewName}.oxreserved < '{$iTime}' ";
76 $oEx =
oxNew(
'oxVoucherException' );
77 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOVOUCHER' );
78 $oEx->setVoucherNr( $sVoucherNr );
95 public function markAsUsed( $sOrderId, $sUserId, $dDiscount )
98 if ( $this->oxvouchers__oxid->value ) {
99 $this->oxvouchers__oxorderid->setValue($sOrderId);
100 $this->oxvouchers__oxuserid->setValue($sUserId);
101 $this->oxvouchers__oxdiscount->setValue($dDiscount);
102 $this->oxvouchers__oxdateused->setValue(date(
"Y-m-d",
oxRegistry::get(
"oxUtilsDate")->getTime() ));
115 $sVoucherID = $this->oxvouchers__oxid->value;
119 $sQ =
"update oxvouchers set oxreserved = " . time() .
" where oxid = " . $oDb->quote( $sVoucherID );
120 $oDb->Execute( $sQ );
132 $sVoucherID = $this->oxvouchers__oxid->value;
136 $sQ =
"update oxvouchers set oxreserved = 0 where oxid = " . $oDb->quote( $sVoucherID );
218 $oCur = $this->
getConfig()->getActShopCurrencyObject();
219 if ( $oSeries->oxvoucherseries__oxminimumvalue->value && $dPrice < ($oSeries->oxvoucherseries__oxminimumvalue->value*$oCur->rate) ) {
220 $oEx =
oxNew(
'oxVoucherException' );
221 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_INCORRECTPRICE');
222 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
242 if ( is_array( $aVouchers ) ) {
243 $sId = $this->
getId();
244 if (isset($aVouchers[$sId])) {
245 unset($aVouchers[$sId]);
248 if (!$oSeries->oxvoucherseries__oxallowsameseries->value) {
249 foreach ( $aVouchers as $voucherId => $voucherNr ) {
250 $oVoucher =
oxNew(
'oxVoucher' );
251 $oVoucher->load($voucherId);
252 if ( $this->oxvouchers__oxvoucherserieid->value == $oVoucher->oxvouchers__oxvoucherserieid->value ) {
253 $oEx =
oxNew(
'oxVoucherException' );
254 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTALLOWEDSAMESERIES');
255 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
277 if ( is_array( $aVouchers ) && count($aVouchers) ) {
279 $sIds = implode(
',',
oxDb::getInstance()->quoteArray( array_keys( $aVouchers ) ) );
282 if (!$oSeries->oxvoucherseries__oxallowotherseries->value) {
284 $sSql =
"select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
285 $sSql .=
"oxvouchers.oxvoucherserieid != " . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value ) ;
286 $blAvailable &= !$oDb->getOne($sSql);
289 $sSql =
"select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
290 $sSql .=
"where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != " . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value );
291 $sSql .=
"and not oxvoucherseries.oxallowotherseries";
292 $blAvailable &= !$oDb->getOne($sSql);
294 if ( !$blAvailable ) {
295 $oEx =
oxNew(
'oxVoucherException' );
296 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTALLOWEDOTHERSERIES');
297 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
317 $iTomorrow = mktime( 0, 0, 0, date(
"m" ), date(
"d" )+1, date(
"Y" ) );
318 $iYesterday = mktime( 0, 0, 0, date(
"m" ), date(
"d" )-1, date(
"Y" ) );
321 $iFrom = ( (int)$oSeries->oxvoucherseries__oxbegindate->value ) ?
322 strtotime( $oSeries->oxvoucherseries__oxbegindate->value ) : $iYesterday;
325 $iTo = ( (int)$oSeries->oxvoucherseries__oxenddate->value ) ?
326 strtotime( $oSeries->oxvoucherseries__oxenddate->value ) : $iTomorrow;
328 if ( $iFrom < time() && $iTo > time() ) {
332 $oEx =
oxNew(
'oxVoucherException' );
333 $oEx->setMessage(
'MESSAGE_COUPON_EXPIRED');
334 if ( $iFrom > time() && $iTo > time() ) {
335 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOVOUCHER');
337 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
355 $oEx =
oxNew(
'oxVoucherException' );
356 $oEx->setMessage(
'EXCEPTION_VOUCHER_ISRESERVED');
357 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
393 if ( !$oSeries->oxvoucherseries__oxallowuseanother->value ) {
396 $sSelect =
'select count(*) from '.$this->getViewName().
' where oxuserid = '. $oDb->quote( $oUser->oxuser__oxid->value ) .
' and ';
397 $sSelect .=
'oxvoucherserieid = ' . $oDb->quote( $this->oxvouchers__oxvoucherserieid->value ) .
' and ';
398 $sSelect .=
'((oxorderid is not NULL and oxorderid != "") or (oxdateused is not NULL and oxdateused != 0)) ';
400 if ( $oDb->getOne( $sSelect )) {
401 $oEx =
oxNew(
'oxVoucherException' );
402 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTALLOWEDSAMESERIES');
403 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
422 $oVoucherSeries = $this->
getSerie();
423 $oUserGroups = $oVoucherSeries->setUserGroups();
425 if ( !$oUserGroups->count() ) {
430 foreach ( $oUserGroups as $oGroup ) {
431 if ( $oUser->inGroup( $oGroup->getId() ) ) {
437 $oEx =
oxNew(
'oxVoucherException' );
438 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTVALIDUSERGROUP' );
439 $oEx->setVoucherNr( $this->oxvouchers__oxvouchernr->value );
450 $oVoucher =
new stdClass();
451 $oVoucher->sVoucherId = $this->
getId();
452 $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
465 if ($this->_oSerie !== null) {
468 $oSeries =
oxNew(
'oxVoucherSerie');
469 if (!$oSeries->load($this->oxvouchers__oxvoucherserieid->value)) {
470 throw oxNew(
"oxObjectException" );
472 $this->_oSerie = $oSeries;
485 $sSelect =
"select 1 from oxobject2discount where oxdiscountid = ".$oDb->quote( $oSeries->getId() ).
" and oxtype = 'oxarticles'";
486 $blOk = ( bool ) $oDb->getOne( $sSelect );
500 $sSelect =
"select 1 from oxobject2discount where oxdiscountid = ". $oDb->quote( $oSeries->getId() ).
" and oxtype = 'oxcategories'";
501 $blOk = ( bool ) $oDb->getOne( $sSelect );
514 $oDiscount =
oxNew(
'oxDiscount');
516 $oDiscount->setId($oSeries->getId());
517 $oDiscount->oxdiscount__oxshopid =
new oxField($oSeries->oxvoucherseries__oxshopid->value);
518 $oDiscount->oxdiscount__oxactive =
new oxField(
true);
519 $oDiscount->oxdiscount__oxactivefrom =
new oxField($oSeries->oxvoucherseries__oxbegindate->value);
520 $oDiscount->oxdiscount__oxactiveto =
new oxField($oSeries->oxvoucherseries__oxenddate->value);
521 $oDiscount->oxdiscount__oxtitle =
new oxField($oSeries->oxvoucherseries__oxserienr->value);
522 $oDiscount->oxdiscount__oxamount =
new oxField(1);
524 $oDiscount->oxdiscount__oxprice =
new oxField(0);
526 $oDiscount->oxdiscount__oxaddsumtype =
new oxField($oSeries->oxvoucherseries__oxdiscounttype->value==
'percent'?
'%':
'abs');
527 $oDiscount->oxdiscount__oxaddsum =
new oxField($oSeries->oxvoucherseries__oxdiscount->value);
528 $oDiscount->oxdiscount__oxitmartid =
new oxField();
529 $oDiscount->oxdiscount__oxitmamount =
new oxField();
530 $oDiscount->oxdiscount__oxitmmultiple =
new oxField();
544 if ($this->oxvouchers__oxorderid->value) {
546 } elseif ( $this->
getSession()->getBasket() ) {
562 if (is_null($oDiscount)) {
566 $oOrder =
oxNew(
'oxOrder');
567 $oOrder->load($this->oxvouchers__oxorderid->value);
572 foreach ( $oOrder->getOrderArticles(
true) as $oOrderArticle ) {
573 if (!$oOrderArticle->skipDiscounts() && $oDiscount->isForBasketItem($oOrderArticle)) {
574 $aItems[$iCount] = array(
575 'oxid' => $oOrderArticle->getProductId(),
576 'price' => $oOrderArticle->oxorderarticles__oxbprice->value,
577 'discount' => $oDiscount->getAbsValue($oOrderArticle->oxorderarticles__oxbprice->value),
578 'amount' => $oOrderArticle->oxorderarticles__oxamount->value,
596 if (is_null($oDiscount)) {
604 foreach ( $oBasket->getContents() as $oBasketItem ) {
605 if ( !$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle() ) && !$oArticle->skipDiscounts() && $oDiscount->isForBasketItem($oArticle) ) {
607 $aItems[$iCount] = array(
608 'oxid' => $oArticle->getId(),
609 'price' => $oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getPrice(),
610 'discount' => $oDiscount->getAbsValue($oArticle->getBasketPrice( $oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket )->getPrice()),
611 'amount' => $oBasketItem->getAmount(),
633 if ( $oSeries->oxvoucherseries__oxdiscounttype->value ==
'absolute' ) {
634 $oCur = $this->
getConfig()->getActShopCurrencyObject();
635 $dDiscount = $oSeries->oxvoucherseries__oxdiscount->value * $oCur->rate;
637 $dDiscount = $oSeries->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
640 if ( $dDiscount > $dPrice ) {
641 $dDiscount = $dPrice;
656 return $oSeries->oxvoucherseries__oxdiscount->value;
667 return $oSeries->oxvoucherseries__oxdiscounttype->value;
686 if (!count($aBasketItems) && !$this->
isAdmin() ) {
687 $oEx =
oxNew(
'oxVoucherException' );
688 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOVOUCHER');
689 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
695 $oVoucherPrice =
oxNew(
'oxPrice');
696 $oDiscountPrice =
oxNew(
'oxPrice');
697 $oProductPrice =
oxNew(
'oxPrice');
698 $oProductTotal =
oxNew(
'oxPrice');
701 $blDiscountApplied =
false;
703 foreach ( $aBasketItems as $aBasketItem ) {
706 if ( $blDiscountApplied and !empty( $oSeries->oxvoucherseries__oxcalculateonce->value ) ) {
710 $oDiscountPrice->setPrice($aBasketItem[
'discount']);
711 $oProductPrice->setPrice($aBasketItem[
'price']);
714 if (!$oSeries->oxvoucherseries__oxcalculateonce->value) {
715 $oDiscountPrice->multiply($aBasketItem[
'amount']);
716 $oProductPrice->multiply($aBasketItem[
'amount']);
719 $oVoucherPrice->add($oDiscountPrice->getBruttoPrice());
720 $oProductTotal->add($oProductPrice->getBruttoPrice());
722 if ( !empty( $aBasketItem[
'discount'] ) ) {
723 $blDiscountApplied =
true;
727 $dVoucher = $oVoucherPrice->getBruttoPrice();
728 $dProduct = $oProductTotal->getBruttoPrice();
730 if ( $dVoucher > $dProduct ) {
752 if ( !count( $aBasketItems ) && !$this->
isAdmin() ) {
753 $oEx =
oxNew(
'oxVoucherException' );
754 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOVOUCHER');
755 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
759 $oProductPrice =
oxNew(
'oxPrice');
760 $oProductTotal =
oxNew(
'oxPrice');
762 foreach ( $aBasketItems as $aBasketItem ) {
763 $oProductPrice->setPrice( $aBasketItem[
'price'] );
764 $oProductPrice->multiply( $aBasketItem[
'amount'] );
765 $oProductTotal->add( $oProductPrice->getBruttoPrice() );
768 $dProduct = $oProductTotal->getBruttoPrice();
769 $dVoucher = $oDiscount->getAbsValue( $dProduct );
770 return ( $dVoucher > $dProduct ) ? $dProduct : $dVoucher;
786 return $this->
getId();
789 return $this->oxvouchers__oxvouchernr;
791 case 'fVoucherdiscount':
792 return $this->oxvouchers__oxdiscount;
809 return $iVoucherTimeout;