33         $this->
init(
'oxvouchers');
 
   47     public function getVoucherByNr($sVoucherNr, $aVouchers = array(), $blCheckavalability = 
false)
 
   50         if (!is_null($sVoucherNr)) {
 
   56             $sQ = 
"select {$sViewName}.* from {$sViewName}, {$sSeriesViewName} where 
   57                         {$sSeriesViewName}.oxid = {$sViewName}.oxvoucherserieid and 
   58                         {$sViewName}.oxvouchernr = " . $oDb->quote($sVoucherNr) . 
" and ";
 
   60             if (is_array($aVouchers)) {
 
   61                 foreach ($aVouchers as $sVoucherId => $sSkipVoucherNr) {
 
   62                     $sQ .= 
"{$sViewName}.oxid != " . $oDb->quote($sVoucherId) . 
" and ";
 
   65             $sQ .= 
"( {$sViewName}.oxorderid is NULL || {$sViewName}.oxorderid = '' ) ";
 
   66             $sQ .= 
" and ( {$sViewName}.oxdateused is NULL || {$sViewName}.oxdateused = 0 ) ";
 
   69             if ($blCheckavalability) {
 
   71                 $sQ .= 
" and {$sViewName}.oxreserved < '{$iTime}' ";
 
   77                 $oEx = 
oxNew(
'oxVoucherException');
 
   78                 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOVOUCHER');
 
   79                 $oEx->setVoucherNr($sVoucherNr);
 
   94     public function markAsUsed($sOrderId, $sUserId, $dDiscount)
 
   97         if ($this->oxvouchers__oxid->value) {
 
   98             $this->oxvouchers__oxorderid->setValue($sOrderId);
 
   99             $this->oxvouchers__oxuserid->setValue($sUserId);
 
  100             $this->oxvouchers__oxdiscount->setValue($dDiscount);
 
  101             $this->oxvouchers__oxdateused->setValue(date(
"Y-m-d", 
oxRegistry::get(
"oxUtilsDate")->getTime()));
 
  112         $sVoucherID = $this->oxvouchers__oxid->value;
 
  116             $sQ = 
"update oxvouchers set oxreserved = " . time() . 
" where oxid = " . $oDb->quote($sVoucherID);
 
  127         $sVoucherID = $this->oxvouchers__oxid->value;
 
  131             $sQ = 
"update oxvouchers set oxreserved = 0 where oxid = " . $oDb->quote($sVoucherID);
 
  213         $oCur = $this->
getConfig()->getActShopCurrencyObject();
 
  214         if ($oSeries->oxvoucherseries__oxminimumvalue->value && $dPrice < ($oSeries->oxvoucherseries__oxminimumvalue->value * $oCur->rate)) {
 
  215             $oEx = 
oxNew(
'oxVoucherException');
 
  216             $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_INCORRECTPRICE');
 
  217             $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
 
  237         if (is_array($aVouchers)) {
 
  238             $sId = $this->
getId();
 
  239             if (isset($aVouchers[$sId])) {
 
  240                 unset($aVouchers[$sId]);
 
  243             if (!$oSeries->oxvoucherseries__oxallowsameseries->value) {
 
  244                 foreach ($aVouchers as $voucherId => $voucherNr) {
 
  245                     $oVoucher = 
oxNew(
'oxVoucher');
 
  246                     $oVoucher->load($voucherId);
 
  247                     if ($this->oxvouchers__oxvoucherserieid->value == $oVoucher->oxvouchers__oxvoucherserieid->value) {
 
  248                         $oEx = 
oxNew(
'oxVoucherException');
 
  249                         $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTALLOWEDSAMESERIES');
 
  250                         $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
 
  272         if (is_array($aVouchers) && count($aVouchers)) {
 
  277             if (!$oSeries->oxvoucherseries__oxallowotherseries->value) {
 
  279                 $sSql = 
"select 1 from oxvouchers where oxvouchers.oxid in ($sIds) and ";
 
  280                 $sSql .= 
"oxvouchers.oxvoucherserieid != " . $oDb->quote($this->oxvouchers__oxvoucherserieid->value);
 
  281                 $blAvailable &= !$oDb->getOne($sSql);
 
  284                 $sSql = 
"select 1 from oxvouchers left join oxvoucherseries on oxvouchers.oxvoucherserieid=oxvoucherseries.oxid ";
 
  285                 $sSql .= 
"where oxvouchers.oxid in ($sIds) and oxvouchers.oxvoucherserieid != " . $oDb->quote($this->oxvouchers__oxvoucherserieid->value);
 
  286                 $sSql .= 
"and not oxvoucherseries.oxallowotherseries";
 
  287                 $blAvailable &= !$oDb->getOne($sSql);
 
  290                 $oEx = 
oxNew(
'oxVoucherException');
 
  291                 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTALLOWEDOTHERSERIES');
 
  292                 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
 
  313         $iTomorrow = mktime(0, 0, 0, date(
"m"), date(
"d") + 1, date(
"Y"));
 
  314         $iYesterday = mktime(0, 0, 0, date(
"m"), date(
"d") - 1, date(
"Y"));
 
  317         $iFrom = ((int) $oSeries->oxvoucherseries__oxbegindate->value) ?
 
  318             strtotime($oSeries->oxvoucherseries__oxbegindate->value) : $iYesterday;
 
  321         $iTo = ((int) $oSeries->oxvoucherseries__oxenddate->value) ?
 
  322             strtotime($oSeries->oxvoucherseries__oxenddate->value) : $iTomorrow;
 
  324         if ($iFrom < $iTime && $iTo > $iTime) {
 
  328         $oEx = 
oxNew(
'oxVoucherException');
 
  329         $oEx->setMessage(
'MESSAGE_COUPON_EXPIRED');
 
  330         if ($iFrom > $iTime && $iTo > $iTime) {
 
  331             $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOVOUCHER');
 
  333         $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
 
  351         $oEx = 
oxNew(
'oxVoucherException');
 
  352         $oEx->setMessage(
'EXCEPTION_VOUCHER_ISRESERVED');
 
  353         $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
 
  389         if (!$oSeries->oxvoucherseries__oxallowuseanother->value) {
 
  392             $sSelect = 
'select count(*) from ' . $this->
getViewName() . 
' where oxuserid = ' . $oDb->quote($oUser->oxuser__oxid->value) . 
' and ';
 
  393             $sSelect .= 
'oxvoucherserieid = ' . $oDb->quote($this->oxvouchers__oxvoucherserieid->value) . 
' and ';
 
  394             $sSelect .= 
'((oxorderid is not NULL and oxorderid != "") or (oxdateused is not NULL and oxdateused != 0)) ';
 
  396             if ($oDb->getOne($sSelect)) {
 
  397                 $oEx = 
oxNew(
'oxVoucherException');
 
  398                 $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTALLOWEDSAMESERIES');
 
  399                 $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
 
  418         $oVoucherSeries = $this->
getSerie();
 
  419         $oUserGroups = $oVoucherSeries->setUserGroups();
 
  421         if (!$oUserGroups->count()) {
 
  426             foreach ($oUserGroups as $oGroup) {
 
  427                 if ($oUser->inGroup($oGroup->getId())) {
 
  433         $oEx = 
oxNew(
'oxVoucherException');
 
  434         $oEx->setMessage(
'ERROR_MESSAGE_VOUCHER_NOTVALIDUSERGROUP');
 
  435         $oEx->setVoucherNr($this->oxvouchers__oxvouchernr->value);
 
  446         $oVoucher = 
new stdClass();
 
  447         $oVoucher->sVoucherId = $this->
getId();
 
  448         $oVoucher->sVoucherNr = $this->oxvouchers__oxvouchernr->value;
 
  462         if ($this->_oSerie !== null) {
 
  465         $oSeries = 
oxNew(
'oxVoucherSerie');
 
  466         if (!$oSeries->load($this->oxvouchers__oxvoucherserieid->value)) {
 
  467             throw oxNew(
"oxObjectException");
 
  469         $this->_oSerie = $oSeries;
 
  483         $sSelect = 
"select 1 from oxobject2discount where oxdiscountid = " . $oDb->quote($oSeries->getId()) . 
" and oxtype = 'oxarticles'";
 
  484         $blOk = ( bool ) $oDb->getOne($sSelect);
 
  498         $sSelect = 
"select 1 from oxobject2discount where oxdiscountid = " . $oDb->quote($oSeries->getId()) . 
" and oxtype = 'oxcategories'";
 
  499         $blOk = ( bool ) $oDb->getOne($sSelect);
 
  512         $oDiscount = 
oxNew(
'oxDiscount');
 
  514         $oDiscount->setId($oSeries->getId());
 
  515         $oDiscount->oxdiscount__oxshopid = 
new oxField($oSeries->oxvoucherseries__oxshopid->value);
 
  516         $oDiscount->oxdiscount__oxactive = 
new oxField(
true);
 
  517         $oDiscount->oxdiscount__oxactivefrom = 
new oxField($oSeries->oxvoucherseries__oxbegindate->value);
 
  518         $oDiscount->oxdiscount__oxactiveto = 
new oxField($oSeries->oxvoucherseries__oxenddate->value);
 
  519         $oDiscount->oxdiscount__oxtitle = 
new oxField($oSeries->oxvoucherseries__oxserienr->value);
 
  520         $oDiscount->oxdiscount__oxamount = 
new oxField(1);
 
  522         $oDiscount->oxdiscount__oxprice = 
new oxField(0);
 
  524         $oDiscount->oxdiscount__oxaddsumtype = 
new oxField($oSeries->oxvoucherseries__oxdiscounttype->value == 
'percent' ? 
'%' : 
'abs');
 
  525         $oDiscount->oxdiscount__oxaddsum = 
new oxField($oSeries->oxvoucherseries__oxdiscount->value);
 
  526         $oDiscount->oxdiscount__oxitmartid = 
new oxField();
 
  527         $oDiscount->oxdiscount__oxitmamount = 
new oxField();
 
  528         $oDiscount->oxdiscount__oxitmmultiple = 
new oxField();
 
  542         if ($this->oxvouchers__oxorderid->value) {
 
  560         if (is_null($oDiscount)) {
 
  564         $oOrder = 
oxNew(
'oxOrder');
 
  565         $oOrder->load($this->oxvouchers__oxorderid->value);
 
  570         foreach ($oOrder->getOrderArticles(
true) as $oOrderArticle) {
 
  571             if (!$oOrderArticle->skipDiscounts() && $oDiscount->isForBasketItem($oOrderArticle)) {
 
  572                 $aItems[$iCount] = array(
 
  573                     'oxid'     => $oOrderArticle->getProductId(),
 
  574                     'price'    => $oOrderArticle->oxorderarticles__oxbprice->value,
 
  575                     'discount' => $oDiscount->getAbsValue($oOrderArticle->oxorderarticles__oxbprice->value),
 
  576                     'amount'   => $oOrderArticle->oxorderarticles__oxamount->value,
 
  594         if (is_null($oDiscount)) {
 
  602         foreach ($oBasket->getContents() as $oBasketItem) {
 
  603             if (!$oBasketItem->isDiscountArticle() && ($oArticle = $oBasketItem->getArticle()) && !$oArticle->skipDiscounts() && $oDiscount->isForBasketItem($oArticle)) {
 
  605                 $aItems[$iCount] = array(
 
  606                     'oxid'     => $oArticle->getId(),
 
  607                     'price'    => $oArticle->getBasketPrice($oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket)->getPrice(),
 
  608                     'discount' => $oDiscount->getAbsValue($oArticle->getBasketPrice($oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket)->getPrice()),
 
  609                     'amount'   => $oBasketItem->getAmount(),
 
  631         if ($oSeries->oxvoucherseries__oxdiscounttype->value == 
'absolute') {
 
  632             $oCur = $this->
getConfig()->getActShopCurrencyObject();
 
  633             $dDiscount = $oSeries->oxvoucherseries__oxdiscount->value * $oCur->rate;
 
  635             $dDiscount = $oSeries->oxvoucherseries__oxdiscount->value / 100 * $dPrice;
 
  638         if ($dDiscount > $dPrice) {
 
  639             $dDiscount = $dPrice;
 
  655         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);
 
  771         return ($dVoucher > $dProduct) ? $dProduct : $dVoucher;
 
  787                 return $this->
getId();
 
  790                 return $this->oxvouchers__oxvouchernr;
 
  792             case 'fVoucherdiscount':
 
  793                 return $this->oxvouchers__oxdiscount;
 
  811         return $iVoucherTimeout;