[ Index ]
 

Code source de Zen Cart E-Commerce Shopping Cart 1.3.7.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/includes/functions/ -> functions_taxes.php (source)

   1  <?php
   2  /**

   3   * functions_taxes

   4   *

   5   * @package functions

   6   * @copyright Copyright 2003-2006 Zen Cart Development Team

   7   * @copyright Portions Copyright 2003 osCommerce

   8   * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0

   9   * @version $Id: functions_taxes.php 4135 2006-08-14 04:25:02Z drbyte $

  10   */
  11  
  12  ////

  13  // Returns the tax rate for a zone / class

  14  // TABLES: tax_rates, zones_to_geo_zones

  15    function zen_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
  16      global $db;
  17  
  18      if ( ($country_id == -1) && ($zone_id == -1) ) {
  19        if (isset($_SESSION['customer_id'])) {
  20          $country_id = $_SESSION['customer_country_id'];
  21          $zone_id = $_SESSION['customer_zone_id'];
  22        } else {
  23          $country_id = STORE_COUNTRY;
  24          $zone_id = STORE_ZONE;
  25        }
  26      }
  27  
  28      if (STORE_PRODUCT_TAX_BASIS == 'Store') {
  29        if ($zone_id != STORE_ZONE) return 0;
  30      }
  31  
  32      $tax_query = "select sum(tax_rate) as tax_rate
  33                    from (" . TABLE_TAX_RATES . " tr
  34                    left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id)
  35                    left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) )
  36                    where (za.zone_country_id is null
  37                    or za.zone_country_id = 0
  38                    or za.zone_country_id = '" . (int)$country_id . "')
  39                    and (za.zone_id is null
  40                    or za.zone_id = 0
  41                    or za.zone_id = '" . (int)$zone_id . "')
  42                    and tr.tax_class_id = '" . (int)$class_id . "'
  43                    group by tr.tax_priority";
  44  
  45      $tax = $db->Execute($tax_query);
  46  
  47      if ($tax->RecordCount() > 0) {
  48        $tax_multiplier = 1.0;
  49        while (!$tax->EOF) {
  50          $tax_multiplier *= 1.0 + ($tax->fields['tax_rate'] / 100);
  51          $tax->MoveNext();
  52        }
  53        return ($tax_multiplier - 1.0) * 100;
  54      } else {
  55        return 0;
  56      }
  57    }
  58  
  59  ////

  60  // Return the tax description for a zone / class

  61  // TABLES: tax_rates;

  62    function zen_get_tax_description($class_id, $country_id, $zone_id) {
  63      global $db;
  64      $tax_query = "select tax_description
  65                    from (" . TABLE_TAX_RATES . " tr
  66                    left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id)
  67                    left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) )
  68                    where (za.zone_country_id is null or za.zone_country_id = 0
  69                    or za.zone_country_id = '" . (int)$country_id . "')
  70                    and (za.zone_id is null
  71                    or za.zone_id = 0
  72                    or za.zone_id = '" . (int)$zone_id . "')
  73                    and tr.tax_class_id = '" . (int)$class_id . "'
  74                    order by tr.tax_priority";
  75  
  76      $tax = $db->Execute($tax_query);
  77  
  78      if ($tax->RecordCount() > 0) {
  79        $tax_description = '';
  80        while (!$tax->EOF) {
  81          $tax_description .= $tax->fields['tax_description'] . ' + ';
  82          $tax->MoveNext();
  83        }
  84        $tax_description = substr($tax_description, 0, -3);
  85  
  86        return $tax_description;
  87      } else {
  88        return TEXT_UNKNOWN_TAX_RATE;
  89      }
  90    }
  91  
  92  ////

  93  // Add tax to a products price based on whether we are displaying tax "in" the price

  94    function zen_add_tax($price, $tax) {
  95      global $currencies;
  96  
  97      if ( (DISPLAY_PRICE_WITH_TAX == 'true') && ($tax > 0) ) {
  98        return zen_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']) + zen_calculate_tax($price, $tax);
  99      } else {
 100        return zen_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);
 101      }
 102    }
 103  
 104  // Calculates Tax rounding the result

 105    function zen_calculate_tax($price, $tax) {
 106      global $currencies;
 107  //    $result = bcmul($price, $tax, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);

 108  //    $result = bcdiv($result, 100, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);

 109  //    return $result;

 110      return zen_round($price * $tax / 100, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);
 111    }
 112  
 113  
 114  ////

 115  // Output the tax percentage with optional padded decimals

 116    function zen_display_tax_value($value, $padding = TAX_DECIMAL_PLACES) {
 117      if (strpos($value, '.')) {
 118        $loop = true;
 119        while ($loop) {
 120          if (substr($value, -1) == '0') {
 121            $value = substr($value, 0, -1);
 122          } else {
 123            $loop = false;
 124            if (substr($value, -1) == '.') {
 125              $value = substr($value, 0, -1);
 126            }
 127          }
 128        }
 129      }
 130  
 131      if ($padding > 0) {
 132        if ($decimal_pos = strpos($value, '.')) {
 133          $decimals = strlen(substr($value, ($decimal_pos+1)));
 134          for ($i=$decimals; $i<$padding; $i++) {
 135            $value .= '0';
 136          }
 137        } else {
 138          $value .= '.';
 139          for ($i=0; $i<$padding; $i++) {
 140            $value .= '0';
 141          }
 142        }
 143      }
 144  
 145      return $value;
 146    }
 147  
 148  ////

 149  // Get tax rate from tax description

 150   function zen_get_tax_rate_from_desc($tax_desc) {
 151      global $db;
 152      $tax_rate = 0.00;
 153  
 154      $tax_descriptions = explode(' + ', $tax_desc);
 155      foreach ($tax_descriptions as $tax_description) {
 156        $tax_query = "SELECT tax_rate
 157                      FROM " . TABLE_TAX_RATES . "
 158                      WHERE tax_description = :taxDescLookup";
 159        $tax_query = $db->bindVars($tax_query, ':taxDescLookup', $tax_description, 'string'); 
 160  
 161        $tax = $db->Execute($tax_query);
 162  
 163        $tax_rate += $tax->fields['tax_rate'];
 164      }
 165  
 166      return $tax_rate;
 167    }
 168  
 169   function zen_get_tax_locations($store_country = -1, $store_zone = -1) {
 170    global $db;
 171      switch (STORE_PRODUCT_TAX_BASIS) {
 172  
 173        case 'Shipping':
 174          $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
 175                                  from " . TABLE_ADDRESS_BOOK . " ab
 176                                  left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
 177                                  where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
 178                                  and ab.address_book_id = '" . (int)$_SESSION['sendto'] . "'";
 179          $tax_address_result = $db->Execute($tax_address_query);
 180        break;
 181        case 'Billing':
 182  
 183          $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
 184                                  from " . TABLE_ADDRESS_BOOK . " ab
 185                                  left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
 186                                  where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
 187                                  and ab.address_book_id = '" . (int)$_SESSION['billto'] . "'";
 188          $tax_address_result = $db->Execute($tax_address_query);
 189        break;
 190        case 'Store':
 191          $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
 192                                  from " . TABLE_ADDRESS_BOOK . " ab
 193                                  left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
 194                                  where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
 195                                  and ab.address_book_id = '" . (int)$_SESSION['billto'] . "'";
 196          $tax_address_result = $db->Execute($tax_address_query);
 197  
 198          if ($tax_address_result ->fields['entry_zone_id'] == STORE_ZONE) {
 199  
 200          } else {
 201            $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
 202                                    from " . TABLE_ADDRESS_BOOK . " ab
 203                                    left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
 204                                    where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
 205                                    and ab.address_book_id = '" . (int)$_SESSION['sendto'] . "'";
 206          $tax_address_result = $db->Execute($tax_address_query);
 207         }
 208       }
 209       $tax_address['zone_id'] = $tax_address_result->fields['entry_zone_id'];
 210       $tax_address['country_id'] = $tax_address_result->fields['entry_country_id'];
 211       return $tax_address;
 212   }
 213  ?>


Généré le : Mon Nov 26 16:45:43 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics