[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

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

   1  <?php
   2  //
   3  // Definition of  class
   4  //
   5  // Created on: <25-Nov-2002 15:40:10 wy>
   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  include_once ( "kernel/common/template.php" );
  30  include_once ( "kernel/classes/ezvattype.php" );
  31  include_once ( "lib/ezutils/classes/ezhttppersistence.php" );
  32  
  33  $module =& $Params["Module"];
  34  $http =& eZHttpTool::instance();
  35  $tpl =& templateInit();
  36  $errors = false;
  37  
  38  /**
  39   * Apply changes made to VAT types' names and/or percentages.
  40   */
  41  function applyChanges( $module, $http, &$errors, $vatTypeArray = false )
  42  {
  43      if ( $vatTypeArray === false )
  44          $vatTypeArray = eZVatType::fetchList( true, true );
  45  
  46      $db =& eZDB::instance();
  47      $db->begin();
  48      foreach ( $vatTypeArray as $vatType )
  49      {
  50          $id = $vatType->attribute( 'id' );
  51  
  52          if ( $id == -1 ) // avoid storing changes to the "fake" dynamic VAT type
  53              continue;
  54  
  55          if ( $http->hasPostVariable( "vattype_name_" . $id ) )
  56          {
  57              $name = $http->postVariable( "vattype_name_" . $id );
  58          }
  59          if ( $http->hasPostVariable( "vattype_percentage_" . $id ) )
  60          {
  61              $percentage = $http->postVariable( "vattype_percentage_" . $id );
  62          }
  63  
  64          if ( !$name || $percentage < 0 || $percentage > 100 )
  65          {
  66              if ( !$name )
  67                  $errors[] = ezi18n( 'kernel/shop/vattype', 'Empty VAT type names are not allowed (corrected).' );
  68              else
  69                  $errors[] = ezi18n( 'kernel/shop/vattype', 'Wrong VAT percentage (corrected).' );
  70  
  71              continue;
  72          }
  73  
  74          $vatType->setAttribute( 'name', $name );
  75          $vatType->setAttribute( 'percentage', $percentage );
  76          $vatType->store();
  77      }
  78      $db->commit();
  79  }
  80  
  81  /**
  82   * Generate a unique VAT type name.
  83   *
  84   * The generated name looks like "VAT type X"
  85   * where X is a unique number.
  86   */
  87  function generateUniqueVatTypeName( $vatTypes )
  88  {
  89      $commonPart = ezi18n( 'kernel/shop', 'VAT type' );
  90      $maxNumber = 0;
  91      foreach ( $vatTypes as $type )
  92      {
  93          $typeName = $type->attribute( 'name' );
  94  
  95          if ( !preg_match( "/^$commonPart (\d+)/", $typeName, $matches ) )
  96              continue;
  97  
  98          $curNumber = $matches[1];
  99          if ( $curNumber > $maxNumber )
 100              $maxNumber = $curNumber;
 101      }
 102  
 103      $maxNumber++;
 104      return "$commonPart $maxNumber";
 105  }
 106  
 107  /**
 108   * Determine dependent VAT rules and products for the given VAT types.
 109   *
 110   * \private
 111   */
 112  function findDependencies( $vatTypeIDList, &$deps, &$haveDeps, &$canRemove )
 113  {
 114      // Find dependencies (products and/or VAT rules).
 115      require_once ( 'kernel/classes/ezvatrule.php' );
 116      $deps = array();
 117      $haveDeps = false;
 118      $canRemove = true;
 119      foreach ( $vatTypeIDList as $vatID )
 120      {
 121          $vatType = eZVatType::fetch( $vatID );
 122          $vatName = $vatType->attribute( 'name' );
 123  
 124          // Find dependent VAT rules.
 125          $nRules = eZVatrule::fetchCountByVatType( $vatID );
 126  
 127          // Find dependent products.
 128          $nProducts = eZVatType::fetchDependentProductsCount( $vatID );
 129  
 130          // Find product classes having this VAT type set as default.
 131          $nClasses = eZVatType::fetchDependentClassesCount( $vatID );
 132  
 133          if ( $nClasses )
 134              $canRemove = false;
 135  
 136          $deps[$vatID] = array( 'name' => $vatName,
 137                                 'affected_rules_count' => $nRules,
 138                                 'affected_products_count' => $nProducts,
 139                                 'affected_classes_count' => $nClasses );
 140  
 141          if ( !$haveDeps && ( $nRules > 0 || $nProducts > 0 ) )
 142              $haveDeps = true;
 143      }
 144  }
 145  
 146  // Add new VAT type.
 147  if ( $module->isCurrentAction( 'Add' ) )
 148  {
 149      $vatTypeArray = eZVatType::fetchList( true, true );
 150      applyChanges( $module, $http, $errors, $vatTypeArray );
 151  
 152      $vatType = eZVatType::create();
 153      $vatType->setAttribute( 'name', generateUniqueVatTypeName( $vatTypeArray ) );
 154      $vatType->store();
 155      $tpl->setVariable( 'last_added_id', $vatType->attribute( 'id' ) );
 156  }
 157  // Save changes made to names and percentages.
 158  elseif ( $module->isCurrentAction( 'SaveChanges' ) )
 159  {
 160      applyChanges( $module, $http, $errors );
 161  }
 162  // Remove checked VAT types [with or without confirmation].
 163  elseif ( $module->isCurrentAction( 'Remove' ) )
 164  {
 165      $vatIDsToRemove = $module->actionParameter( 'vatTypeIDList' );
 166      $deps = array();
 167      $haveDeps = false;
 168      $canRemove = true;
 169      findDependencies( $vatIDsToRemove, $deps, $haveDeps, $canRemove );
 170  
 171      $errors = false;
 172      $showDeps = true;
 173      // If there are dependendant rules and/or products
 174      // then show confifmation dialog.
 175      if ( $haveDeps )
 176      {
 177          // Let the user choose another VAT to set
 178  
 179          $allVatTypes = eZVatType::fetchList( true, true );
 180  
 181          if ( ( count( $allVatTypes ) - count( $vatIDsToRemove ) ) == 0 )
 182          {
 183              $errorMsg = 'You cannot remove all VAT types. ' .
 184                          'If you do not neet to charge any VAT for your ' .
 185                          'products then just leave one VAT type and set ' .
 186                          'its percentage to zero.';
 187              $errors[] = ezi18n( 'kernel/shop/vattype', $errorMsg );
 188              $showDeps = false;
 189          }
 190  
 191          $tpl->setVariable( 'can_remove', $canRemove ); // true if we allow the removal
 192          $tpl->setVariable( 'show_dependencies', $showDeps ); // true if we'll show the VAT types' dependencies
 193          $tpl->setVariable( 'errors', $errors ); // array of error messages, false if there are no errors
 194          $tpl->setVariable( 'dependencies', $deps );
 195          $tpl->setVariable( 'vat_type_ids', join( ',', $vatIDsToRemove ) );
 196          $path = array( array( 'text' => ezi18n( 'kernel/shop', 'VAT types' ),
 197                                'url'  => false ) );
 198  
 199          $Result = array();
 200          $Result['path'] =& $path;
 201          $Result['content'] =& $tpl->fetch( "design:shop/removevattypes.tpl" );
 202          return;
 203      }
 204      else // otherwise just silently remove the VAT types.
 205      {
 206          $module->setCurrentAction( 'ConfirmRemoval' );
 207          // pass through
 208      }
 209  }
 210  // Do actually remove checked VAT types.
 211  if ( $module->isCurrentAction( 'ConfirmRemoval' ) )
 212  {
 213      $afterConfirmation = false;
 214  
 215      $vatIDsToRemove = $module->actionParameter( 'vatTypeIDList' );
 216  
 217      // The list of VAT types to remove is a string
 218      // if passed from the confirmation dialog
 219      // and an array if passed from another action.
 220      if ( is_string( $vatIDsToRemove ) )
 221      {
 222          $vatIDsToRemove = explode( ',', $vatIDsToRemove );
 223          $afterConfirmation = true;
 224      }
 225  
 226      if ( !$afterConfirmation )
 227          applyChanges( $module, $http, $errors );
 228  
 229      $db =& eZDB::instance();
 230      $db->begin();
 231      foreach ( $vatIDsToRemove as $vatID )
 232      {
 233          eZVatType::remove( $vatID );
 234      }
 235      $db->commit();
 236  }
 237  
 238  $vatTypeArray = eZVatType::fetchList( true, true );
 239  
 240  if ( is_array( $errors ) )
 241      $errors = array_unique( $errors );
 242  
 243  $tpl->setVariable( "vattype_array", $vatTypeArray );
 244  $tpl->setVariable( "module", $module );
 245  $tpl->setVariable( 'errors', $errors );
 246  
 247  $path = array();
 248  $path[] = array( 'text' => ezi18n( 'kernel/shop', 'VAT types' ),
 249                   'url' => false );
 250  
 251  
 252  $Result = array();
 253  $Result['path'] =& $path;
 254  $Result['content'] =& $tpl->fetch( "design:shop/vattype.tpl" );
 255  
 256  ?>


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