[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/kernel/classes/vathandlers/ -> ezdefaultvathandler.php (source)

   1  <?php
   2  //
   3  // Definition of eZDefaultVATHandler class
   4  //
   5  // Created on: <15-Dec-2005 15:42:29 vs>
   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 ezdefaultvathandler.php
  30  */
  31  
  32  /*!
  33    \class eZDefaultVATHandler ezdefaultvathandler.php
  34    \brief Default VAT handler.
  35  
  36    Provides basic VAT charging rules.
  37    Resulting VAT percentage may depend on product category
  38    and country the user is from.
  39  */
  40  
  41  class eZDefaultVATHandler
  42  {
  43      /**
  44       * \public
  45       * \static
  46       */
  47      function getVatPercent( $object, $country )
  48      {
  49          $productCategory = eZDefaultVATHandler::getProductCategory( $object );
  50  
  51          // If product category is not specified
  52          if ( $productCategory === null )
  53          {
  54              require_once ( 'kernel/classes/ezproductcategory.php' );
  55  
  56              // Default to a fake product category (*) that will produce
  57              // weak match on category for any VAT rule.
  58              $productCategory = new eZProductCategory( array( 'id' => -1,
  59                                                               'name' => '*' ) );
  60          }
  61  
  62          // If country is not specified
  63          if ( !$country )
  64          {
  65              // Default to a fake country that will produce
  66              // weak match on country for any VAT rule.
  67              $country = '*';
  68          }
  69  
  70          /*
  71          eZDebug::writeDebug( sprintf( "getVatPercent( country='%s', category='%s' )",
  72                                        $country, $productCategory->attribute( 'name' ) ) );
  73          */
  74  
  75          $vatType = eZDefaultVATHandler::chooseVatType( $productCategory, $country );
  76  
  77          return $vatType->attribute( 'percentage' );
  78      }
  79  
  80      /**
  81       * Determine object's product category.
  82       *
  83       * \private
  84       * \static
  85       */
  86      function getProductCategory( $object )
  87      {
  88          $ini =& eZINI::instance( 'shop.ini' );
  89          if ( !$ini->hasVariable( 'VATSettings', 'ProductCategoryAttribute' ) )
  90          {
  91              eZDebug::writeError( "Cannot find product category: please specify its attribute identifier " .
  92                                   "in the following setting: shop.ini.[VATSettings].ProductCategoryAttribute" );
  93              return null;
  94          }
  95  
  96          $categoryAttributeName = $ini->variable( 'VATSettings', 'ProductCategoryAttribute' );
  97  
  98          if ( !$categoryAttributeName )
  99          {
 100              eZDebug::writeError( "Cannot find product category: empty attribute name specified " .
 101                                   "in the following setting: shop.ini.[VATSettings].ProductCategoryAttribute" );
 102  
 103              return null;
 104          }
 105  
 106          $productDataMap = $object->attribute( 'data_map' );
 107  
 108          if ( !isset( $productDataMap[$categoryAttributeName] ) )
 109          {
 110              eZDebug::writeError( "Cannot find product category: there is no attribute '$categoryAttributeName' in object '" .
 111                                     $object->attribute( 'name' ) .
 112                                     "' of class '" .
 113                                     $object->attribute( 'class_name' ) . "'." );
 114              return null;
 115          }
 116  
 117          $categoryAttribute = $productDataMap[$categoryAttributeName];
 118          $productCategory = $categoryAttribute->attribute( 'content' );
 119  
 120          if ( $productCategory === null )
 121          {
 122              eZDebug::writeNotice( "Product category is not specified in object '" .
 123                                     $object->attribute( 'name' ) .
 124                                     "' of class '" .
 125                                     $object->attribute( 'class_name' ) . "'." );
 126              return null;
 127          }
 128  
 129          return $productCategory;
 130      }
 131  
 132      /**
 133       * Choose the best matching VAT type for given product category and country.
 134       *
 135       * We calculate priority for each VAT type and then choose
 136       * the VAT type having the highest priority
 137       * (or first of those having the highest priority).
 138       *
 139       * VAT type priority is calculated from county match and category match as following:
 140       *
 141       * CountryMatch  = 0
 142       * CategoryMatch = 1
 143       *
 144       * if ( <there is exact match on country> )
 145       *     CountryMatch = 2
 146       * elseif ( <there is weak match on country> )
 147       *     CountryMatch = 1
 148       *
 149       * if ( <there is exact match on product category> )
 150       *     CategoryMatch = 2
 151       * elseif ( <there is weak match on product category> )
 152       *     CategoryMatch = 1
 153       *
 154       * if ( <there is match on both country and category )
 155       *     VatTypePriority = CountryMatch * 2 + CategoryMatch - 2
 156       * else
 157       *     VatTypePriority = 0
 158       *
 159       * \private
 160       * \static
 161       */
 162      function chooseVatType( $productCategory, $country )
 163      {
 164          require_once ( 'kernel/classes/ezvatrule.php' );
 165  
 166          $vatRules = eZVatRule::fetchList();
 167          $catID = $productCategory->attribute( 'id' );
 168  
 169          $vatPriorities = array();
 170          foreach ( $vatRules as $rule )
 171          {
 172              $ruleCountry = $rule->attribute( 'country' );
 173              $ruleCatIDs  = $rule->attribute( 'product_categories_ids' );
 174              $ruleVatID   = $rule->attribute( 'vat_type' );
 175  
 176              $categoryMatch = 0;
 177              $countryMatch  = 0;
 178  
 179              if ( $ruleCountry == '*' )
 180                  $countryMatch = 1;
 181              elseif ( $ruleCountry == $country )
 182                  $countryMatch = 2;
 183  
 184              if ( !$ruleCatIDs )
 185                  $categoryMatch = 1;
 186              elseif ( in_array( $catID, $ruleCatIDs ) )
 187                  $categoryMatch = 2;
 188  
 189              if ( $countryMatch && $categoryMatch )
 190                  $vatPriority = $countryMatch * 2 + $categoryMatch - 2;
 191              else
 192                  $vatPriority = 0;
 193  
 194              /*
 195              eZDebug::writeDebug( sprintf( "Rule: country='%s' categories='%s' => country_match=%d cat_match=%d : pri=%d",
 196                                            $ruleCountry,
 197                                            $rule->attribute( 'product_categories_string' ),
 198                                            $countryMatch, $categoryMatch, $vatPriority ) );
 199              */
 200  
 201              if ( !isset( $vatPriorities[$vatPriority] ) )
 202                  $vatPriorities[$vatPriority] = $ruleVatID;
 203          }
 204  
 205          krsort( $vatPriorities, SORT_NUMERIC );
 206  
 207  
 208          $bestPriority = 0;
 209          if ( $vatPriorities )
 210              $bestPriority = array_shift( array_keys( $vatPriorities ) );
 211  
 212          if ( $bestPriority == 0 )
 213          {
 214              eZDebug::writeError( "Cannot find a suitable VAT type " .
 215                                   "for country '" . $country . "'" .
 216                                   " and category '" . $productCategory->attribute( 'name' ). "'." );
 217  
 218              return new eZVATType(  array( "id" => 0,
 219                                            "name" => ezi18n( 'kernel/shop', 'None' ),
 220                                            "percentage" => 0.0 ) );
 221          }
 222  
 223          $bestVatTypeID = array_shift( $vatPriorities );
 224          $bestVatType = eZVatType::fetch( $bestVatTypeID );
 225  
 226          eZDebug::writeDebug(
 227              sprintf( "Best matching VAT for '%s'/'%s' is '%s' (%d%%)",
 228                       $country,
 229                       $productCategory->attribute( 'name' ),
 230                       $bestVatType->attribute( 'name' ),
 231                       $bestVatType->attribute( 'percentage' ) ) );
 232  
 233          return $bestVatType;
 234      }
 235  }
 236  
 237  ?>


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