#native_company# #native_desc#
#native_cta#

PipePrice

By Anon
on June 28, 2000

Version: 1.0

Type: Class

Category: Shopping Carts

License: GNU General Public License

Description: this class works as a pipe:
the number is pushed at the top
it flows down through any operation added
and then out in its final value

u can choose to add reductions of price if a quantity is reached
or add costs to it if there are special taxes
and so on…

<?
/*
PipePrice for the calculation of prices in shoppingcharts
rel. 1.0 x PHP4.0
by Riccardo Pasquini
[email protected]
*/

/*
NOTES:
the methods marked with PHP4 only work with such version
for PHP3 the class works with only one single instance of the class Operation done in the constructor

this class works as a pipe:
the number is pushed at the top
it flows down through any operation added
and then out in its final value

u can choose to add reductions of price if a quantity is reached
or add costs to it if there are special taxes
and so on...

*/

define("__PIPE_PRICE__",1);

//base class from which deriving and redefining Operate($nCurValue)
class Operation    //or which works as a NullOperation
{
      function Operate($nCurValue)
      {
               return $nCurValue;
      }
}

//class of example to apply to PipePrice
class Sconto extends Operation
{
      var $m_bIsPerc;
      var $m_nOperatore;

      function Sconto($value, $bIsPerc=0)
      {
               $this->m_bIsPerc=$bIsPerc;
               $this->m_nOperatore=$value;
      }

      function Operate($nCurValue)
      {
               if($this->m_bIsPerc)
               {
                   $this->m_nOperatore=$nCurValue*$this->m_nOperatore/100;   //from percentage to value
               }

               //applies the reduction in price
               return ($nCurValue-$this->m_nOperatore);
      }
}

// main class
class PipePrice
{
      var $m_nStartValue;
      var $m_nCurValue;
      var $m_nEndValue;
      var $m_aOperations;

      //constructors
      function PipePrice($nStartValue=0, $operation=NULL)
      {
               $this->m_nStartValue=$nStartValue;
               $this->m_aOperations=array($operation);
               $this->m_nCurValue=0;
               $this->m_nEndValue=0;
               $this->compute();
      }

      // methods to add operations to the pipe
      //PHP4
      function addHeadOperation($operation)        //$operation is of Operation type
      {
               return array_unshift($this->m_aOperations,$operation);
      }

      //PHP4
      function removeHeadOperation()
      {
               return array_shift($this->m_aOperations);
      }

      //PHP4
      function addTailOperation($operation)        //$operation as above
      {
               return array_push($this->m_aOperations,$operation);
      }

      //PHP4
      function removeTailOperation()
      {
               return array_pop($this->m_aOperations);
      }

      function setStartValue($nStartValue)
      {
               $this->m_nStartValue=$nStartValue;
               $this->compute();
      }

      function getStartValue()
      {
               return $this->m_nStartValue;
      }

      function getEndValue()
      {
               $this->compute();
               return $this->m_nEndValue;
      }

      function compute()
      {
               $this->m_nCurValue=$this->m_nStartValue;
               reset($this->m_aOperations);
               for($op=current($this->m_aOperations);$op;$op=next($this->m_aOperations))
                      $this->m_nCurValue=$op->Operate($this->m_nCurValue);

               $this->m_nEndValue=$this->m_nCurValue;
      }
};
?>