[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/kernel/shop/ -> vatrules.php (source)

   1  <?php
   2  //
   3  // Created on: <17-Feb-2006 16:58:41 vs>
   4  //
   5  // SOFTWARE NAME: eZ publish
   6  // SOFTWARE RELEASE: 3.9.0
   7  // BUILD VERSION: 17785
   8  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
   9  // SOFTWARE LICENSE: GNU General Public License v2.0
  10  // NOTICE: >
  11  //   This program is free software; you can redistribute it and/or
  12  //   modify it under the terms of version 2.0  of the GNU General
  13  //   Public License as published by the Free Software Foundation.
  14  //
  15  //   This program is distributed in the hope that it will be useful,
  16  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18  //   GNU General Public License for more details.
  19  //
  20  //   You should have received a copy of version 2.0 of the GNU General
  21  //   Public License along with this program; if not, write to the Free
  22  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23  //   MA 02110-1301, USA.
  24  //
  25  //
  26  
  27  include_once ( "kernel/common/template.php" );
  28  include_once ( "lib/ezutils/classes/ezhttppersistence.php" );
  29  include_once ( "kernel/classes/ezvatrule.php" );
  30  
  31  /**
  32   * Find errors in VAT charging rules.
  33   *
  34   * \return list of errors, or false if no errors found.
  35   */
  36  function findErrors( $vatRules )
  37  {
  38      $errors = false;
  39  
  40      // 1. Check if default rule exists.
  41      $defaultRuleExists = false;
  42      foreach ( $vatRules as $rule )
  43      {
  44          if ( $rule->attribute( 'country' ) == '*' &&
  45               !$rule->attribute( 'product_categories' ) )
  46          {
  47              $defaultRuleExists = true;
  48              break;
  49          }
  50      }
  51  
  52      if ( !$defaultRuleExists && count( $vatRules ) > 0 )
  53          $errors[] = ezi18n( 'kernel/shop/vatrules', 'No default rule found. ' .
  54                              'Please add rule having "Any" country and "Any" category.' );
  55  
  56      // 2. Check for conflicting rules.
  57      // Conflicting rules are those having the same country and equal or intersecting categories sets.
  58      $vatRulesCount = count( $vatRules );
  59      for ( $i=0; $i < $vatRulesCount; $i++ )
  60      {
  61          $iRule       = $vatRules[$i];
  62          $iCountry    = $iRule->attribute( 'country' );
  63          $iCategories = $iRule->attribute( 'product_categories_names' );
  64  
  65          for ( $j=$i+1; $j < $vatRulesCount; $j++ )
  66          {
  67              $jRule       = $vatRules[$j];
  68              $jCountry    = $jRule->attribute( 'country' );
  69  
  70              if ( $iCountry != $jCountry )
  71                  continue;
  72  
  73              $jCategories = $jRule->attribute( 'product_categories_names' );
  74  
  75              // Multiple default rules.
  76              if ( !$iCategories && !$jCategories )
  77              {
  78                  if ( $iCountry == '*' )
  79                  {
  80                      $errorMessage = "Conflict: There are multiple default rules.";
  81                      $errors[] = ezi18n( 'kernel/shop/vatrules', $errorMessage );
  82                  }
  83                  else
  84                  {
  85                      $errorMessage = "Conflict: There are multiple default rules for country '%1'.";
  86                      $errors[] = ezi18n( 'kernel/shop/vatrules', $errorMessage, null, array( $iCountry ) );
  87                  }
  88              }
  89              // Intersecting rules.
  90              elseif ( $iCategories && $jCategories && $commonCategories = array_intersect( $iCategories, $jCategories ) )
  91              {
  92                  if ( $iCountry == '*' )
  93                  {
  94                      $errorMessage = "Conflict: The following categories for any country are mentioned in multiple rules: %2.";
  95                      $errors[] = ezi18n( 'kernel/shop/vatrules', $errorMessage, null, array( $iCountry, join( ',', $commonCategories ) ) );
  96                  }
  97                  else
  98                  {
  99                      $errorMessage = "Conflict: The following categories for country '%1' are mentioned in multiple rules: %2.";
 100                      $errors[] = ezi18n( 'kernel/shop/vatrules', $errorMessage, null, array( $iCountry, join( ',', $commonCategories ) ) );
 101                  }
 102              }
 103          }
 104      }
 105  
 106      if ( is_array( $errors ) )
 107      {
 108          // Remove duplicated error messages.
 109          $errors = array_unique( $errors );
 110          sort( $errors );
 111      }
 112  
 113      return $errors;
 114  }
 115  
 116  /**
 117   * Auxiliary function used to sort VAT rules.
 118   *
 119   * Rules are sorted by country and categories.
 120   * Any specific categories list or country is considered less than '*' (Any).
 121   */
 122  function compareVatRules($a, $b)
 123  {
 124      // Compare countries.
 125  
 126      $aCountry = $a->attribute( 'country' );
 127      $bCountry = $b->attribute( 'country' );
 128  
 129      if ( $aCountry != $bCountry )
 130      {
 131          if ( $aCountry == '*' )
 132              return 1;
 133          if ( $bCountry == '*' )
 134              return -1;
 135  
 136          return ( $aCountry < $bCountry ? -1 : 1 );
 137      }
 138  
 139      // Ok, countries are equal. Let's compare categories then.
 140  
 141      if ( $a->attribute( 'product_categories' ) )
 142          $aCategory = $a->attribute( 'product_categories_string' );
 143      else
 144          $aCategory = '*';
 145  
 146      if ( $b->attribute( 'product_categories' ) )
 147          $bCategory = $b->attribute( 'product_categories_string' );
 148      else
 149          $bCategory = '*';
 150  
 151      if ( $aCategory != $bCategory )
 152      {
 153          if ( $aCategory == '*' )
 154              return 1;
 155          if ( $bCategory == '*' )
 156              return -1;
 157  
 158          return ( $aCategory < $bCategory ? -1 : 1 );
 159      }
 160  
 161      return 0;
 162  }
 163  
 164  $module =& $Params["Module"];
 165  $http   =& eZHttpTool::instance();
 166  $tpl =& templateInit();
 167  
 168  if ( $http->hasPostVariable( "AddRuleButton" ) )
 169  {
 170      return $module->redirectTo( $module->functionURI( "editvatrule" ) );
 171  }
 172  
 173  if ( $http->hasPostVariable( "RemoveRuleButton" ) )
 174  {
 175      if ( !$http->hasPostVariable( "RuleIDList" ) )
 176          $ruleIDList = array();
 177      else
 178          $ruleIDList = $http->postVariable( "RuleIDList" );
 179  
 180      $db =& eZDB::instance();
 181      $db->begin();
 182      foreach ( $ruleIDList as $ruleID )
 183          eZVatRule::remove( $ruleID );
 184      $db->commit();
 185  }
 186  
 187  if ( $http->hasPostVariable( "SaveCategoriesButton" ) )
 188  {
 189      $db =& eZDB::instance();
 190      $db->begin();
 191      foreach ( $productCategories as $cat )
 192      {
 193          $id = $cat->attribute( 'id' );
 194  
 195          if ( !$http->hasPostVariable( "category_name_" . $id ) )
 196              continue;
 197  
 198          $name = $http->postVariable( "category_name_" . $id );
 199          $cat->setAttribute( 'name', $name );
 200          $cat->store();
 201      }
 202      $db->commit();
 203      return $module->redirectTo( $module->functionURI( "productcategories" ) );
 204  }
 205  
 206  $vatRules = eZVatRule::fetchList();
 207  $errors = findErrors( $vatRules );
 208  usort( $vatRules, 'compareVatRules' );
 209  
 210  $tpl->setVariable( 'rules', $vatRules );
 211  $tpl->setVariable( 'errors', $errors );
 212  
 213  $path = array();
 214  $path[] = array( 'text' => ezi18n( 'kernel/shop/vatrules', 'VAT rules' ),
 215                   'url' => false );
 216  
 217  $Result = array();
 218  $Result['path'] =& $path;
 219  $Result['content'] =& $tpl->fetch( "design:shop/vatrules.tpl" );
 220  
 221  ?>


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