[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/shop/classes/ -> ezsimpleprice.php (source)

   1  <?php
   2  //
   3  // Definition of eZSimplePrice class
   4  //
   5  // Created on: <28-Nov-2005 12:26:52 dl>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezsimpleprice.php
  30  */
  31  
  32  /*!
  33    \class eZSimplePrice ezsimpleprice.php
  34    \brief Handles prices with VAT and discounts.
  35  
  36  
  37  */
  38  /*!
  39    \class eZSimplePrice ezsimpleprice.php
  40  
  41  The available attributes are:
  42    - vat_type
  43    - current_user
  44    - is_vat_included
  45    - selected_vat_type
  46    - vat_percent
  47    - inc_vat_price
  48    - ex_vat_price
  49    - discount_percent
  50    - discount_price_inc_vat
  51    - discount_price_ex_vat
  52    - has_discount
  53    - price
  54  */
  55  
  56  include_once ( 'kernel/classes/datatypes/ezuser/ezuser.php' );
  57  include_once ( 'kernel/classes/ezcontentobjecttreenode.php' );
  58  include_once ( 'kernel/classes/ezvattype.php' );
  59  include_once ( 'kernel/classes/ezdiscount.php' );
  60  
  61  
  62  class eZSimplePrice
  63  {
  64      function eZSimplePrice( &$classAttribute, &$contentObjectAttribute, $storedPrice = null )
  65      {
  66          $this->setVATIncluded( false );
  67  
  68          $price = 0.0;
  69          if ( isset( $storedPrice ) )
  70          {
  71              $price = $storedPrice;
  72          }
  73          $this->setPrice( $price );
  74  
  75          $discountPercent = 0.0;
  76          if ( get_class( $contentObjectAttribute ) == 'ezcontentobjectattribute' )
  77          {
  78              $object =& $contentObjectAttribute->object();
  79              $this->ContentObject = $object;
  80              $discountPercent = eZDiscount::discountPercent( eZUser::currentUser(),
  81                                                              array( 'contentclass_id' => $object->attribute( 'contentclass_id'),
  82                                                                     'contentobject_id' => $object->attribute( 'id' ),
  83                                                                     'section_id' => $object->attribute( 'section_id') ) );
  84          }
  85          $this->setDiscountPercent( $discountPercent );
  86      }
  87  
  88      function attributes()
  89      {
  90          return array( 'price',
  91                        'currency',
  92                        'selected_vat_type',
  93                        'vat_type',
  94                        'vat_percent',
  95                        'is_vat_included',
  96                        'inc_vat_price',
  97                        'ex_vat_price',
  98                        'discount_percent',
  99                        'discount_price_inc_vat',
 100                        'discount_price_ex_vat',
 101                        'has_discount',
 102                        'current_user'            // for backward compatibility
 103                      );
 104      }
 105  
 106      /*!
 107       \return \c true if the attribute named \a $attr exists.
 108      */
 109      function hasAttribute( $attr )
 110      {
 111          return in_array( $attr, $this->attributes() );
 112      }
 113  
 114      function setAttribute( $attr, $value )
 115      {
 116          switch ( $attr )
 117          {
 118              case 'selected_vat_type':
 119              {
 120                  $this->setVATType( $value );
 121              } break;
 122  
 123              case 'is_vat_included':
 124              {
 125                  $this->setVATIncluded( $value == '1' );
 126              } break;
 127  
 128              default:
 129              {
 130                  eZDebug::writeError( "Unspecified attribute: " . $attr, 'eZSimplePrice::setAttribute' );
 131              } break;
 132          }
 133      }
 134  
 135      function &attribute( $attr )
 136      {
 137          switch ( $attr )
 138          {
 139              case 'price' :
 140              {
 141                  return $this->price();
 142              } break;
 143  
 144              case 'currency' :
 145              {
 146                  return $this->currency();
 147              } break;
 148  
 149              case 'selected_vat_type':
 150              {
 151                  return $this->VATType();
 152              } break;
 153  
 154              case 'vat_type' :
 155              {
 156                  $VATType =& $this->VATType();
 157                  return $VATType->VATTypeList();
 158  
 159              } break;
 160  
 161              case 'vat_percent' :
 162              {
 163                  $vatPercent = $this->VATPercent();
 164                  return $vatPercent;
 165              } break;
 166  
 167              case 'is_vat_included':
 168              {
 169                  return $this->VATIncluded();
 170              } break;
 171  
 172              case 'inc_vat_price' :
 173              {
 174                  return $this->incVATPrice();
 175              } break;
 176  
 177              case 'ex_vat_price' :
 178              {
 179                 return $this->exVATPrice();
 180              } break;
 181  
 182              case 'discount_percent' :
 183              {
 184                  return $this->discountPercent();
 185              } break;
 186  
 187              case 'discount_price_inc_vat' :
 188              {
 189                  return $this->discountIncVATPrice();
 190              } break;
 191  
 192              case 'discount_price_ex_vat' :
 193              {
 194                  return $this->discountExVATPrice();
 195              } break;
 196  
 197              case 'has_discount' :
 198              {
 199                  return $this->hasDiscount();
 200              } break;
 201  
 202              case 'current_user':
 203              {
 204                  return eZUser::currentUser();
 205              }
 206  
 207              default :
 208              {
 209                  eZDebug::writeError( "Attribute '$attr' does not exist", 'eZSimplePrice::attribute' );
 210                  $retValue = null;
 211                  return $retValue;
 212              } break;
 213          }
 214      }
 215  
 216      function &VATType()
 217      {
 218          if ( !$this->VATType )
 219              $this->VATType = eZVatType::create();
 220  
 221          return $this->VATType;
 222      }
 223  
 224      function setVATType( $VATID )
 225      {
 226          $this->VATType = eZVatType::fetch( $VATID );
 227          if ( !$this->VATType )
 228          {
 229              eZDebug::writeDebug( "VAT type with id '$VATID' is unavailable", 'eZSimplePrice::setVATType');
 230              $this->VATType = eZVatType::create();
 231          }
 232      }
 233  
 234      /**
 235       * Can return dynamic percentage depending on product and country the user is from.
 236       */
 237      function VATPercent( $object = false, $country = false )
 238      {
 239          $VATType =& $this->VATType();
 240  
 241          if ( $object === false )
 242          {
 243              if ( $this->ContentObject === null )
 244                  return $VATType->attribute( 'percentage' );
 245  
 246              $object = $this->ContentObject;
 247          }
 248  
 249          return $VATType->getPercentage( $object, $country );
 250      }
 251  
 252      function &VATIncluded()
 253      {
 254          return $this->IsVATIncluded;
 255      }
 256  
 257      function setVATIncluded( $VATIncluded )
 258      {
 259          $this->IsVATIncluded = $VATIncluded ;
 260      }
 261  
 262      function &price()
 263      {
 264          return $this->Price;
 265      }
 266  
 267      function setPrice( $value )
 268      {
 269          $this->Price = $value;
 270      }
 271  
 272      function &incVATPrice()
 273      {
 274          return $this->calcIncVATPrice( $this->price() );
 275      }
 276  
 277      function &exVATPrice()
 278      {
 279          return $this->calcExVATPrice( $this->price() );
 280      }
 281  
 282      function &discountPercent()
 283      {
 284          return $this->DiscountPercent;
 285      }
 286  
 287      function setDiscountPercent( $percent )
 288      {
 289          $this->DiscountPercent = $percent;
 290      }
 291  
 292      function &hasDiscount()
 293      {
 294          $hasDiscount = false;
 295          $discountPercent = $this->discountPercent();
 296          if ( $discountPercent != 0 )
 297              $hasDiscount = true;
 298  
 299          return $hasDiscount;
 300      }
 301  
 302      function &discountIncVATPrice()
 303      {
 304          return $this->calcDiscountIncVATPrice( $this->price() );
 305      }
 306  
 307      function &discountExVATPrice()
 308      {
 309          return $this->calcDiscountExVATPrice( $this->price() );
 310      }
 311  
 312      /*!
 313       \returns discount percentage. Backward compatibility.
 314      */
 315      function discount()
 316      {
 317          return $this->discountPercent();
 318      }
 319  
 320      function &calcDiscountIncVATPrice( $priceValue )
 321      {
 322          $discountPercent =& $this->discountPercent();
 323          $incVATPrice =& $this->calcIncVATPrice( $priceValue );
 324          $discountPrice = $incVATPrice * ( 100 - $discountPercent ) / 100;
 325          return $discountPrice;
 326      }
 327  
 328      function &calcDiscountExVATPrice( $priceValue )
 329      {
 330          $discountPercent =& $this->discountPercent();
 331          $exVATPrice =& $this->calcExVATPrice( $priceValue );
 332          $discountPrice = $exVATPrice * ( 100 - $discountPercent ) / 100;
 333          return $discountPrice;
 334      }
 335  
 336      function &calcIncVATPrice( $priceValue )
 337      {
 338          $incVATPrice = $priceValue;
 339          if ( !$this->VATIncluded() )
 340          {
 341              $VATPercent = $this->VATPercent();
 342              // If VAT is unknown yet then we use zero VAT percentage for price calculation.
 343              if ( $VATPercent == -1 )
 344                  $VATPercent = 0;
 345              $incVATPrice = $priceValue * ( $VATPercent + 100 ) / 100;
 346          }
 347  
 348          return $incVATPrice;
 349      }
 350  
 351      function &calcExVATPrice( $priceValue )
 352      {
 353          $exVATPrice = $priceValue;
 354          if ( $this->VATIncluded() )
 355          {
 356              $VATPercent = $this->VATPercent();
 357              // If VAT is unknown yet then we use zero VAT percentage for price calculation.
 358              if ( $VATPercent == -1 )
 359                  $VATPercent = 0;
 360              $exVATPrice = $priceValue / ( $VATPercent + 100 ) * 100;
 361          }
 362  
 363          return $exVATPrice;
 364      }
 365  
 366      function &currency()
 367      {
 368          $currencyCode = '';
 369          return $currencyCode;
 370      }
 371  
 372      /*!
 373       \reimp
 374      */
 375      function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 376      {
 377          $price =& $classAttribute->content();
 378          if ( $price )
 379          {
 380              $vatIncluded = $price->attribute( 'is_vat_included' );
 381              $vatTypes = $price->attribute( 'vat_type' );
 382              $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'vat-included',
 383                                                                                       array( 'is-set' => $vatIncluded ? 'true' : 'false' ) ) );
 384              $vatTypeNode = eZDOMDocument::createElementNode( 'vat-type' );
 385              $chosenVatType = $classAttribute->attribute( 'data_float1' );
 386              $gotVat = false;
 387              foreach ( $vatTypes as $vatType )
 388              {
 389                  $id = $vatType->attribute( 'id' );
 390                  if ( $id == $chosenVatType )
 391                  {
 392                      $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'name', $vatType->attribute( 'name' ) ) );
 393                      $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'percentage', $vatType->attribute( 'percentage' ) ) );
 394                      $gotVat = true;
 395                      break;
 396                  }
 397              }
 398              if ( $gotVat )
 399                  $attributeParametersNode->appendChild( $vatTypeNode );
 400          }
 401      }
 402  
 403      /*!
 404       \reimp
 405      */
 406      function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 407      {
 408          $vatNode =& $attributeParametersNode->elementByName( 'vat-included' );
 409          $vatIncluded = strtolower( $vatNode->attributeValue( 'is-set' ) ) == 'true';
 410          $classAttribute->setAttribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD, $vatIncluded );
 411          $vatTypeNode =& $attributeParametersNode->elementByName( 'vat-type' );
 412          $vatName = $vatTypeNode->attributeValue( 'name' );
 413          $vatPercentage = $vatTypeNode->attributeValue( 'percentage' );
 414          $vatID = false;
 415          $vatTypes = eZVATType::fetchList();
 416          foreach ( array_keys( $vatTypes ) as $vatTypeKey )
 417          {
 418              $vatType =& $vatTypes[$vatTypeKey];
 419              if ( $vatType->attribute( 'name' ) == $vatName and
 420                   $vatType->attribute( 'percentage' ) == $vatPercentage )
 421              {
 422                  $vatID = $vatType->attribute( 'id' );
 423                  break;
 424              }
 425          }
 426          if ( !$vatID )
 427          {
 428              $vatType = eZVATType::create();
 429              $vatType->setAttribute( 'name', $vatName );
 430              $vatType->setAttribute( 'percentage', $vatPercentage );
 431              $vatType->store();
 432              $vatID = $vatType->attribute( 'id' );
 433          }
 434          $classAttribute->setAttribute( EZ_DATATYPESTRING_VAT_ID_FIELD, $vatID );
 435      }
 436  
 437      /// \privatesection
 438      var $Price;
 439      var $VATType;
 440      var $IsVATIncluded;
 441      var $DiscountPercent;
 442      var $ContentObject;
 443  }
 444  
 445  
 446  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7