eZ Publish  [trunk]
ezsimpleprice.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZSimplePrice class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZSimplePrice ezsimpleprice.php
00013   \brief Handles prices with VAT and discounts.
00014 
00015 
00016 */
00017 /*!
00018   \class eZSimplePrice ezsimpleprice.php
00019 
00020 The available attributes are:
00021   - vat_type
00022   - current_user
00023   - is_vat_included
00024   - selected_vat_type
00025   - vat_percent
00026   - inc_vat_price
00027   - ex_vat_price
00028   - discount_percent
00029   - discount_price_inc_vat
00030   - discount_price_ex_vat
00031   - has_discount
00032   - price
00033 */
00034 
00035 
00036 class eZSimplePrice
00037 {
00038     function eZSimplePrice( $classAttribute, $contentObjectAttribute, $storedPrice = null )
00039     {
00040         $this->setVATIncluded( false );
00041 
00042         $price = 0.0;
00043         if ( isset( $storedPrice ) )
00044         {
00045             $price = $storedPrice;
00046         }
00047         $this->setPrice( $price );
00048 
00049         $discountPercent = 0.0;
00050         if ( $contentObjectAttribute instanceof eZContentObjectAttribute )
00051         {
00052             $object = $contentObjectAttribute->object();
00053             $this->ContentObject = $object;
00054             $discountPercent = eZDiscount::discountPercent( eZUser::currentUser(),
00055                                                             array( 'contentclass_id' => $object->attribute( 'contentclass_id'),
00056                                                                    'contentobject_id' => $object->attribute( 'id' ),
00057                                                                    'section_id' => $object->attribute( 'section_id') ) );
00058         }
00059         $this->setDiscountPercent( $discountPercent );
00060     }
00061 
00062     function attributes()
00063     {
00064         return array( 'price',
00065                       'currency',
00066                       'selected_vat_type',
00067                       'vat_type',
00068                       'vat_percent',
00069                       'is_vat_included',
00070                       'inc_vat_price',
00071                       'ex_vat_price',
00072                       'discount_percent',
00073                       'discount_price_inc_vat',
00074                       'discount_price_ex_vat',
00075                       'has_discount',
00076                       'current_user'            // for backward compatibility
00077                     );
00078     }
00079 
00080     /*!
00081      \return \c true if the attribute named \a $attr exists.
00082     */
00083     function hasAttribute( $attr )
00084     {
00085         return in_array( $attr, $this->attributes() );
00086     }
00087 
00088     function setAttribute( $attr, $value )
00089     {
00090         switch ( $attr )
00091         {
00092             case 'selected_vat_type':
00093             {
00094                 $this->setVATType( $value );
00095             } break;
00096 
00097             case 'is_vat_included':
00098             {
00099                 $this->setVATIncluded( $value == '1' );
00100             } break;
00101 
00102             default:
00103             {
00104                 eZDebug::writeError( "Unspecified attribute: " . $attr, __METHOD__ );
00105             } break;
00106         }
00107     }
00108 
00109     function attribute( $attr )
00110     {
00111         switch ( $attr )
00112         {
00113             case 'price' :
00114             {
00115                 return $this->price();
00116             } break;
00117 
00118             case 'currency' :
00119             {
00120                 return $this->currency();
00121             } break;
00122 
00123             case 'selected_vat_type':
00124             {
00125                 return $this->VATType();
00126             } break;
00127 
00128             case 'vat_type' :
00129             {
00130                 return $this->VATType()->VATTypeList();
00131             } break;
00132 
00133             case 'vat_percent' :
00134             {
00135                 return $this->VATPercent();
00136             } break;
00137 
00138             case 'is_vat_included':
00139             {
00140                 return $this->VATIncluded();
00141             } break;
00142 
00143             case 'inc_vat_price' :
00144             {
00145                 return $this->incVATPrice();
00146             } break;
00147 
00148             case 'ex_vat_price' :
00149             {
00150                return $this->exVATPrice();
00151             } break;
00152 
00153             case 'discount_percent' :
00154             {
00155                 return $this->discountPercent();
00156             } break;
00157 
00158             case 'discount_price_inc_vat' :
00159             {
00160                 return $this->discountIncVATPrice();
00161             } break;
00162 
00163             case 'discount_price_ex_vat' :
00164             {
00165                 return $this->discountExVATPrice();
00166             } break;
00167 
00168             case 'has_discount' :
00169             {
00170                 return $this->hasDiscount();
00171             } break;
00172 
00173             case 'current_user':
00174             {
00175                 return eZUser::currentUser();
00176             }
00177 
00178             default :
00179             {
00180                 eZDebug::writeError( "Attribute '$attr' does not exist", __METHOD__ );
00181                 return null;
00182             } break;
00183         }
00184     }
00185 
00186     function VATType()
00187     {
00188         if ( !$this->VATType )
00189         {
00190             $this->VATType = eZVatType::create();
00191         }
00192 
00193         return $this->VATType;
00194     }
00195 
00196     function setVATType( $VATID )
00197     {
00198         $this->VATType = eZVatType::fetch( $VATID );
00199         if ( !$this->VATType )
00200         {
00201             eZDebug::writeDebug( "VAT type with id '$VATID' is unavailable", __METHOD__ );
00202             $this->VATType = eZVatType::create();
00203         }
00204     }
00205 
00206     /**
00207      * Can return dynamic percentage depending on product and country the user is from.
00208      */
00209     function VATPercent( $object = false, $country = false )
00210     {
00211         $VATType = $this->VATType();
00212 
00213         if ( $object === false )
00214         {
00215             if ( $this->ContentObject === null )
00216                 return $VATType->attribute( 'percentage' );
00217 
00218             $object = $this->ContentObject;
00219         }
00220 
00221         return $VATType->getPercentage( $object, $country );
00222     }
00223 
00224     function VATIncluded()
00225     {
00226         return $this->IsVATIncluded;
00227     }
00228 
00229     function setVATIncluded( $VATIncluded )
00230     {
00231         $this->IsVATIncluded = $VATIncluded ;
00232     }
00233 
00234     function price()
00235     {
00236         return $this->Price;
00237     }
00238 
00239     function setPrice( $value )
00240     {
00241         $this->Price = $value;
00242     }
00243 
00244     function incVATPrice()
00245     {
00246         return $this->calcIncVATPrice( $this->price() );
00247     }
00248 
00249     function exVATPrice()
00250     {
00251         return $this->calcExVATPrice( $this->price() );
00252     }
00253 
00254     function discountPercent()
00255     {
00256         return $this->DiscountPercent;
00257     }
00258 
00259     function setDiscountPercent( $percent )
00260     {
00261         $this->DiscountPercent = $percent;
00262     }
00263 
00264     function hasDiscount()
00265     {
00266         return ( $this->discountPercent() != 0 );
00267     }
00268 
00269     function discountIncVATPrice()
00270     {
00271         return $this->calcDiscountIncVATPrice( $this->price() );
00272     }
00273 
00274     function discountExVATPrice()
00275     {
00276         return $this->calcDiscountExVATPrice( $this->price() );
00277     }
00278 
00279     /*!
00280      \returns discount percentage. Backward compatibility.
00281     */
00282     function discount()
00283     {
00284         return $this->discountPercent();
00285     }
00286 
00287     function calcDiscountIncVATPrice( $priceValue )
00288     {
00289         $discountPercent = $this->discountPercent();
00290         $incVATPrice = $this->calcIncVATPrice( $priceValue );
00291         return $incVATPrice * ( 100 - $discountPercent ) / 100;
00292     }
00293 
00294     function calcDiscountExVATPrice( $priceValue )
00295     {
00296         $discountPercent = $this->discountPercent();
00297         $exVATPrice = $this->calcExVATPrice( $priceValue );
00298         return $exVATPrice * ( 100 - $discountPercent ) / 100;
00299     }
00300 
00301     function calcIncVATPrice( $priceValue )
00302     {
00303         $incVATPrice = $priceValue;
00304         if ( !$this->VATIncluded() )
00305         {
00306             $VATPercent = $this->VATPercent();
00307             // If VAT is unknown yet then we use zero VAT percentage for price calculation.
00308             if ( $VATPercent == -1 )
00309                 $VATPercent = 0;
00310             $incVATPrice = $priceValue * ( $VATPercent + 100 ) / 100;
00311         }
00312 
00313         return $incVATPrice;
00314     }
00315 
00316     function calcExVATPrice( $priceValue )
00317     {
00318         $exVATPrice = $priceValue;
00319         if ( $this->VATIncluded() )
00320         {
00321             $VATPercent = $this->VATPercent();
00322             // If VAT is unknown yet then we use zero VAT percentage for price calculation.
00323             if ( $VATPercent == -1 )
00324                 $VATPercent = 0;
00325             $exVATPrice = $priceValue / ( $VATPercent + 100 ) * 100;
00326         }
00327 
00328         return $exVATPrice;
00329     }
00330     /*!
00331      Return the currency for the datatype.
00332     */
00333     function currency()
00334     {
00335         $locale = eZLocale::instance();
00336         $currencyCode = $locale->currencyShortName();
00337         return $currencyCode;
00338     }
00339 
00340     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00341     {
00342         $price = $classAttribute->content();
00343         if ( $price )
00344         {
00345             $vatIncluded = $price->attribute( 'is_vat_included' );
00346             $vatTypes = $price->attribute( 'vat_type' );
00347             $dom = $attributeParametersNode->ownerDocument;
00348             $vatIncludedNode = $dom->createElement( 'vat-included' );
00349             $vatIncludedNode->setAttribute( 'is-set', $vatIncluded ? 'true' : 'false' );
00350             $attributeParametersNode->appendChild( $vatIncludedNode );
00351             $vatTypeNode = $dom->createElement( 'vat-type' );
00352             $chosenVatType = $classAttribute->attribute( 'data_float1' );
00353             $gotVat = false;
00354             foreach ( $vatTypes as $vatType )
00355             {
00356                 $id = $vatType->attribute( 'id' );
00357                 if ( $id == $chosenVatType )
00358                 {
00359                     $vatTypeNode->setAttribute( 'name', $vatType->attribute( 'name' ) );
00360                     $vatTypeNode->setAttribute( 'percentage', $vatType->attribute( 'percentage' ) );
00361                     $gotVat = true;
00362                     break;
00363                 }
00364             }
00365             if ( $gotVat )
00366                 $attributeParametersNode->appendChild( $vatTypeNode );
00367         }
00368     }
00369 
00370     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00371     {
00372         $vatNode = $attributeParametersNode->getElementsByTagName( 'vat-included' )->item( 0 );
00373         $vatIncluded = strtolower( $vatNode->getAttribute( 'is-set' ) ) == 'true';
00374         $classAttribute->setAttribute( eZPriceType::INCLUDE_VAT_FIELD, $vatIncluded );
00375         $vatTypeNode = $attributeParametersNode->getElementsByTagName( 'vat-type' )->item( 0 );
00376         $vatName = $vatTypeNode->getAttribute( 'name' );
00377         $vatPercentage = $vatTypeNode->getAttribute( 'percentage' );
00378         $vatID = false;
00379         $vatTypes = eZVatType::fetchList();
00380         foreach ( $vatTypes as $vatType )
00381         {
00382             if ( $vatType->attribute( 'name' ) == $vatName and
00383                  $vatType->attribute( 'percentage' ) == $vatPercentage )
00384             {
00385                 $vatID = $vatType->attribute( 'id' );
00386                 break;
00387             }
00388         }
00389         if ( !$vatID )
00390         {
00391             $vatType = eZVatType::create();
00392             $vatType->setAttribute( 'name', $vatName );
00393             $vatType->setAttribute( 'percentage', $vatPercentage );
00394             $vatType->store();
00395             $vatID = $vatType->attribute( 'id' );
00396         }
00397         $classAttribute->setAttribute( eZPriceType::VAT_ID_FIELD, $vatID );
00398     }
00399 
00400     /// \privatesection
00401     public $Price;
00402     public $VATType;
00403     public $IsVATIncluded;
00404     public $DiscountPercent;
00405     public $ContentObject;
00406 }
00407 
00408 
00409 ?>