[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

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

   1  <?php
   2  //
   3  // Created on: <18-Feb-2006 13:11:18 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  // Parameters: ruleID (optional)
  28  
  29  $module =& $Params['Module'];
  30  
  31  require_once ( 'kernel/classes/ezvatrule.php' );
  32  require_once ( 'kernel/classes/ezproductcategory.php' );
  33  require_once ( 'kernel/classes/ezvattype.php' );
  34  
  35  $errors = false;
  36  $errorHeader = false;
  37  $productCategories = eZProductCategory::fetchList();
  38  
  39  /**
  40   * Check entered data.
  41   *
  42   * \return list of errors found, or false if the data is ok.
  43   */
  44  function checkEnteredData( $country, $categories, $vatType, $productCategories, $ruleID )
  45  {
  46      $errors = false;
  47      $errorHeader = '';
  48  
  49      /*
  50       * Check if the data was entered correctly.
  51       */
  52  
  53      if ( !$country || !is_numeric( $vatType ) )
  54      {
  55          $errors = array();
  56          $errorHeader = ezi18n( 'kernel/shop/editvatrule', 'Invalid data entered' );
  57  
  58          if ( !$country )
  59              $errors[] = ezi18n( 'kernel/shop/editvatrule', 'Choose a country.' );
  60          if ( !is_numeric( $vatType ) )
  61              $errors[] = ezi18n( 'kernel/shop/editvatrule', 'Choose a VAT type.' );
  62  
  63          return array( $errorHeader, $errors );
  64      }
  65  
  66      /*
  67       * Check if the rule we're about to create
  68       * conflicts with existing ones.
  69       */
  70  
  71      $errorHeader = ezi18n( 'kernel/shop/editvatrule', 'Conflicting rule' );
  72      $vatRules = eZVatRule::fetchList();
  73  
  74      // If the rule is default one
  75      if ( !$categories )
  76      {
  77          // check if default rule already exists.
  78          foreach ( $vatRules as $i )
  79          {
  80              if ( $i->attribute( 'id' ) == $ruleID ||
  81                   $i->attribute( 'country' ) != $country ||
  82                   $i->attribute( 'product_categories' ) )
  83                  continue;
  84  
  85              if ( $country == '*' )
  86                  $errors[] = ezi18n( 'kernel/shop/editvatrule', 'Default rule for any country already exists.' );
  87              else
  88              {
  89                  $errorMessage = "Default rule for country '%1' already exists.";
  90                  $errors[] = ezi18n( 'kernel/shop/editvatrule', $errorMessage, null, array( $country ) );
  91              }
  92  
  93              break;
  94          }
  95      }
  96  
  97      // If the rule contains some categories
  98      else
  99      {
 100          // check if there are already rules defined for the same country
 101          // containing some of the categories.
 102  
 103          foreach ( $vatRules as $i )
 104          {
 105              if ( $i->attribute( 'id' ) == $ruleID ||
 106                   $i->attribute( 'country' ) != $country ||
 107                   !$i->attribute( 'product_categories' ) )
 108                  continue;
 109  
 110              $intersection = array_intersect( $categories, $i->attribute( 'product_categories_ids' ) );
 111              if ( !$intersection )
 112                  continue;
 113  
 114              // Construct string containing name of all the conflicting categories.
 115              $intersectingCategories = array();
 116              foreach ( $productCategories as $cat )
 117              {
 118                  if ( array_search( $cat->attribute( 'id' ), $intersection ) !== false )
 119                       $intersectingCategories[] = $cat->attribute( 'name' );
 120              }
 121              $intersectingCategories = join( ', ', $intersectingCategories );
 122  
 123              if ( $country == '*' )
 124                  $errorMessage = "There is already a rule defined for any country containing the following categories: %2.";
 125              else
 126                  $errorMessage = "There is already a rule defined for country '%1' containing the following categories: %2.";
 127  
 128              $errors[] = ezi18n( 'kernel/shop/editvatrule', $errorMessage, null, array( $country, $intersectingCategories ) );
 129          }
 130      }
 131  
 132      return array( $errorHeader, $errors );
 133  }
 134  
 135  
 136  if ( $module->isCurrentAction( 'Cancel' ) )
 137  {
 138      return $module->redirectTo( $module->functionURI( 'vatrules' ) );
 139  }
 140  else if ( in_array( $module->currentAction(), array(  'Create', 'StoreChanges' ) ) )
 141  {
 142      if ( $module->isCurrentAction( 'StoreChanges' ) )
 143          $ruleID = $module->actionParameter( 'RuleID' );
 144  
 145      $chosenCountry = $module->actionParameter( 'Country' );
 146      $chosenCategories = $module->hasActionParameter( 'Categories' ) ? $module->actionParameter( 'Categories' ) : array();
 147      $chosenVatType = $module->actionParameter( 'VatType' );
 148  
 149      // normalize data
 150      if ( in_array( '*', $chosenCategories ) )
 151          $chosenCategories = array();
 152  
 153      list( $errorHeader, $errors ) = checkEnteredData( $chosenCountry, $chosenCategories, $chosenVatType,
 154                                                        $productCategories, $ruleID );
 155  
 156      // save rule (creating it if not exists)
 157      do // one-iteration loop to prevent high nesting levels
 158      {
 159          if ( $errors !== false )
 160              break;
 161  
 162          // The entered data is ok.
 163          if ( $module->isCurrentAction( 'StoreChanges' ) )
 164          {
 165              // Store changes made to the VAT rule.
 166              $vatRule = eZVatRule::fetch( $ruleID );
 167  
 168              if ( !is_object( $vatRule ) )
 169              {
 170                  //$ruleID = null;
 171                  $errors[] = ezi18n( 'kernel/shop/editvatrule', 'Rule not found' );
 172                  break;
 173              }
 174          }
 175          else
 176          {
 177              // Create a new VAT rule...
 178              $vatRule = eZVatRule::create();
 179          }
 180  
 181          // Modify chosen categories array
 182          // so that it can be saved into the VAT rule.
 183          $addID = create_function('$i', "return array( 'id' => \$i ) ;" );
 184          $chosenCategories = array_map( $addID, $chosenCategories );
 185  
 186          $vatRule->setAttribute( 'country', $chosenCountry );
 187          $vatRule->setAttribute( 'product_categories', $chosenCategories );
 188          $vatRule->setAttribute( 'vat_type', $chosenVatType );
 189          $vatRule->store();
 190  
 191          return $module->redirectTo( $module->functionURI( 'vatrules' ) );
 192  
 193      } while ( false );
 194  }
 195  
 196  if ( is_numeric( $ruleID ) )
 197  {
 198      $tplVatRule      = eZVatRule::fetch( $ruleID );
 199      $tplCountry      = $tplVatRule->attribute( 'country' );
 200      $tplCategoryIDs  = $tplVatRule->attribute( 'product_categories_ids' );
 201      $tplVatTypeID    = $tplVatRule->attribute( 'vat_type' );
 202  
 203      $pathText = ezi18n( 'kernel/shop/editvatrule', 'Edit VAT charging rule' );
 204  }
 205  else
 206  {
 207      $tplVatRule = null;
 208      $tplCountry = false;
 209      $tplVatTypeID = false;
 210      $tplCategoryIDs = array();
 211  
 212      $pathText = ezi18n( 'kernel/shop/editvatrule', 'Create new VAT charging rule' );
 213  }
 214  
 215  if ( $errors !== false )
 216  {
 217      $tplCountry     = $chosenCountry;
 218      $tplCategoryIDs = $chosenCategories;
 219      $tplVatTypeID   = $chosenVatType;
 220  }
 221  
 222  $vatTypes = eZVatType::fetchList( true, true );
 223  
 224  include_once ( 'kernel/common/template.php' );
 225  $tpl =& templateInit();
 226  
 227  $tpl->setVariable( 'error_header', $errorHeader );
 228  $tpl->setVariable( 'errors', $errors );
 229  $tpl->setVariable( 'all_vat_types', $vatTypes );
 230  $tpl->setVariable( 'all_product_categories', $productCategories );
 231  
 232  $tpl->setVariable( 'rule',         $tplVatRule );
 233  $tpl->setVariable( 'country',      $tplCountry );
 234  $tpl->setVariable( 'category_ids', $tplCategoryIDs );
 235  $tpl->setVariable( 'vat_type_id',  $tplVatTypeID );
 236  
 237  $Result = array();
 238  $Result['content'] =& $tpl->fetch( "design:shop/editvatrule.tpl" );
 239  $Result['path'] = array( array( 'text' => $pathText,
 240                                  'url' => false ) );
 241  
 242  ?>


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