oxdiscountlist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDiscountList extends oxList
00008 {
00014     static protected $_instance = null;
00015 
00021     protected $_sUserId = null;
00022 
00028     protected $_blReload = true;
00029 
00035     public function __construct( $sObjectsInListName = 'oxdiscount' )
00036     {
00037         parent::__construct( 'oxdiscount' );
00038     }
00039 
00045     static public function getInstance()
00046     {
00047         // disable cashing for test modules
00048         if ( defined( 'OXID_PHP_UNIT' ) ) {
00049             self::$_instance = modInstances::getMod( __CLASS__ );
00050         }
00051 
00052         if ( !isset( self::$_instance ) ) {
00053             // allow modules
00054             self::$_instance = oxNew( 'oxDiscountList' );
00055 
00056             if ( defined( 'OXID_PHP_UNIT' ) ) {
00057                 modInstances::addMod( __CLASS__, self::$_instance);
00058             }
00059         }
00060 
00061         return self::$_instance;
00062     }
00063 
00071     protected function _getList( $oUser = null )
00072     {
00073         $sUserId = $oUser?$oUser->getId():'';
00074 
00075         if ( $this->_blReload || $sUserId !== $this->_sUserId ) {
00076             // loading list
00077             $this->selectString( $this->_getFilterSelect( $oUser ) );
00078 
00079             // setting list proterties
00080             $this->_blReload = false;    // reload marker
00081             $this->_sUserId  = $sUserId; // discount list user id
00082         }
00083 
00084         // resetting array pointer
00085         $this->rewind();
00086 
00087         return $this;
00088     }
00089 
00097     public function getCountryId( $oUser )
00098     {
00099         $sCountryId = null;
00100         if ( $oUser ) {
00101             $sCountryId = $oUser->getActiveCountry();
00102         }
00103 
00104         return $sCountryId;
00105     }
00106 
00112     public function forceReload()
00113     {
00114         $this->_blReload = true;
00115     }
00116 
00124     protected function _getFilterSelect( $oUser )
00125     {
00126 
00127         $sTable = getViewName( 'oxdiscount' );
00128         $oBaseObject = $this->getBaseObject();
00129 
00130         $sQ  = "select ".$oBaseObject->getSelectFields()." from $sTable ";
00131         $sQ .= "where ".$oBaseObject->getSqlActiveSnippet().' ';
00132 
00133 
00134         // defining initial filter parameters
00135         $sUserId    = null;
00136         $sGroupIds  = null;
00137         $sCountryId = $this->getCountryId( $oUser );
00138 
00139         // checking for current session user which gives additional restrictions for user itself, users group and country
00140         if ( $oUser ) {
00141 
00142             // user ID
00143             $sUserId = $oUser->getId();
00144 
00145             // user group ids
00146             foreach ( $oUser->getUserGroups() as $oGroup ) {
00147                 if ( $sGroupIds ) {
00148                     $sGroupIds .= ', ';
00149                 }
00150                 $sGroupIds .= "'".$oGroup->getId()."'";
00151             }
00152         }
00153 
00154         $sUserTable    = getViewName( 'oxuser' );
00155         $sGroupTable   = getViewName( 'oxgroups' );
00156         $sCountryTable = getViewName( 'oxcountry' );
00157 
00158         $oDb = oxDb::getDb();
00159         $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sCountryId ).")":'0';
00160         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID='$sUserId')":'0';
00161         $sGroupSql   = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
00162 
00163         $sQ .= "and (
00164             select
00165                 if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00166                         $sCountrySql,
00167                         1) &&
00168                 if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00169                         $sUserSql,
00170                         1) &&
00171                 if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00172                         $sGroupSql,
00173                         1)
00174             )";
00175 
00176         return $sQ;
00177     }
00178 
00187     public function getArticleDiscounts( $oArticle, $oUser = null )
00188     {
00189         $aList = array();
00190         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00191             if ( $oDiscount->isForArticle( $oArticle ) ) {
00192                 $aList[$oDiscount->getId()] = $oDiscount;
00193             }
00194         }
00195 
00196         return $aList;
00197     }
00198 
00208     public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
00209     {
00210         $aList = array();
00211         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00212             if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00213                 $aList[$oDiscount->getId()] = $oDiscount;
00214             }
00215         }
00216 
00217         return $aList;
00218     }
00219 
00228     public function getBasketDiscounts( $oBasket, $oUser = null )
00229     {
00230         $aList = array();
00231         $oList = $this->_getList( $oUser );
00232         foreach ( $oList->getArray() as $oDiscount ) {
00233             if ( $oDiscount->isForBasket( $oBasket ) ) {
00234                 $aList[$oDiscount->getId()] = $oDiscount;
00235             }
00236         }
00237 
00238         return $aList;
00239     }
00240 
00250     public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00251     {
00252         $aList = array();
00253         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00254             if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00255                 $aList[$oDiscount->getId()] = $oDiscount;
00256             }
00257         }
00258 
00259         return $aList;
00260     }
00261 
00270     public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00271     {
00272         $aList = array();
00273         $aDiscList = $this->_getList( $oUser )->getArray();
00274         foreach ( $aDiscList as $oDiscount ) {
00275             if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00276                 $aList[$oDiscount->getId()] = $oDiscount;
00277             }
00278         }
00279 
00280         return $aList;
00281     }
00282 
00291     public function applyDiscounts( $oPrice, $aDiscounts )
00292     {
00293         reset( $aDiscounts );
00294         while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00295             $oDiscount->applyDiscount( $oPrice );
00296         }
00297     }
00298 
00309     public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00310     {
00311         $aDiscLog = array();
00312         reset( $aDiscounts );
00313 
00314         // price object to correctly perform calculations
00315         $dOldPrice = $oPrice->getBruttoPrice();
00316 
00317         while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00318             $oDiscount->applyDiscount( $oPrice );
00319             $dNewPrice = $oPrice->getBruttoPrice();
00320 
00321             if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00322                 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00323             }
00324 
00325             $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00326             $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00327             $dOldPrice = $dNewPrice;
00328         }
00329         return $aDiscLog;
00330     }
00331 }