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         $oBaseObject = $this->getBaseObject();
00127 
00128         $sTable = $oBaseObject->getViewName();
00129         $sQ  = "select ".$oBaseObject->getSelectFields()." from $sTable ";
00130         $sQ .= "where ".$oBaseObject->getSqlActiveSnippet().' ';
00131 
00132 
00133         // defining initial filter parameters
00134         $sUserId    = null;
00135         $sGroupIds  = null;
00136         $sCountryId = $this->getCountryId( $oUser );
00137         $oDb = oxDb::getDb();
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 .= $oDb->quote( $oGroup->getId() );
00151             }
00152         }
00153 
00154         $sUserTable    = getViewName( 'oxuser' );
00155         $sGroupTable   = getViewName( 'oxgroups' );
00156         $sCountryTable = getViewName( 'oxcountry' );
00157 
00158         $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sCountryId ).")":'0';
00159         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID=".$oDb->quote( $sUserId ). ")":'0';
00160         $sGroupSql   = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
00161 
00162         $sQ .= "and (
00163             select
00164                 if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00165                         $sCountrySql,
00166                         1) &&
00167                 if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00168                         $sUserSql,
00169                         1) &&
00170                 if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00171                         $sGroupSql,
00172                         1)
00173             )";
00174 
00175         return $sQ;
00176     }
00177 
00186     public function getArticleDiscounts( $oArticle, $oUser = null )
00187     {
00188         $aList = array();
00189         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00190             if ( $oDiscount->isForArticle( $oArticle ) ) {
00191                 $aList[$oDiscount->getId()] = $oDiscount;
00192             }
00193         }
00194 
00195         return $aList;
00196     }
00197 
00207     public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
00208     {
00209         $aList = array();
00210         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00211             if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00212                 $aList[$oDiscount->getId()] = $oDiscount;
00213             }
00214         }
00215 
00216         return $aList;
00217     }
00218 
00227     public function getBasketDiscounts( $oBasket, $oUser = null )
00228     {
00229         $aList = array();
00230         $oList = $this->_getList( $oUser );
00231         foreach ( $oList->getArray() as $oDiscount ) {
00232             if ( $oDiscount->isForBasket( $oBasket ) ) {
00233                 $aList[$oDiscount->getId()] = $oDiscount;
00234             }
00235         }
00236 
00237         return $aList;
00238     }
00239 
00249     public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00250     {
00251         $aList = array();
00252         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00253             if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00254                 $aList[$oDiscount->getId()] = $oDiscount;
00255             }
00256         }
00257 
00258         return $aList;
00259     }
00260 
00269     public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00270     {
00271         $aList = array();
00272         $aDiscList = $this->_getList( $oUser )->getArray();
00273         foreach ( $aDiscList as $oDiscount ) {
00274             if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00275                 $aList[$oDiscount->getId()] = $oDiscount;
00276             }
00277         }
00278 
00279         return $aList;
00280     }
00281 
00290     public function applyDiscounts( $oPrice, $aDiscounts )
00291     {
00292         reset( $aDiscounts );
00293         while ( list( , $oDiscount ) = each( $aDiscounts ) ) {
00294             $oDiscount->applyDiscount( $oPrice );
00295         }
00296     }
00297 
00308     public function applyBasketDiscounts( oxPrice $oPrice, $aDiscounts, $dAmount = 1 )
00309     {
00310         $aDiscLog = array();
00311         reset( $aDiscounts );
00312 
00313         // price object to correctly perform calculations
00314         $dOldPrice = $oPrice->getBruttoPrice();
00315 
00316         while (list( , $oDiscount ) = each( $aDiscounts ) ) {
00317             $oDiscount->applyDiscount( $oPrice );
00318             $dNewPrice = $oPrice->getBruttoPrice();
00319 
00320             if ( !isset( $aDiscLog[$oDiscount->getId()] ) ) {
00321                 $aDiscLog[$oDiscount->getId()] = $oDiscount->getSimpleDiscount();
00322             }
00323 
00324             $aDiscLog[$oDiscount->getId()]->dDiscount += $dOldPrice - $dNewPrice;
00325             $aDiscLog[$oDiscount->getId()]->dDiscount *= $dAmount;
00326             $dOldPrice = $dNewPrice;
00327         }
00328         return $aDiscLog;
00329     }
00330 }