[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
Zen Cart 源代码 functions_taxes.php

Zen Cart 源代码 functions_taxes.php




下载文件

文件名: functions_taxes.php
文件类型: PHP文件
文件大小: 11.34 KiB
MD5: 8bd3114ba6e9c76b144c6e42693af808

functions_taxes.php - 关闭高亮
  1. <?php
  2. /**
  3.  * functions_taxes
  4.  *
  5.  * @package functions
  6.  * @copyright Copyright 2003-2010 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 16190 2010-05-03 20:18:57Z wilt $
  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 = -1, $zone_id = -1) {
  63.     global $db;
  64.    
  65.     if ( ($country_id == -1) && ($zone_id == -1) ) {
  66.       if (isset($_SESSION['customer_id'])) {
  67.         $country_id = $_SESSION['customer_country_id'];
  68.         $zone_id = $_SESSION['customer_zone_id'];
  69.       } else {
  70.         $country_id = STORE_COUNTRY;
  71.         $zone_id = STORE_ZONE;
  72.       }
  73.     }
  74.  
  75.     $tax_query = "select tax_description
  76.                  from (" . TABLE_TAX_RATES . " tr
  77.                  left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id)
  78.                  left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) )
  79.                  where (za.zone_country_id is null or za.zone_country_id = 0
  80.                  or za.zone_country_id = '" . (int)$country_id . "')
  81.                  and (za.zone_id is null
  82.                  or za.zone_id = 0
  83.                  or za.zone_id = '" . (int)$zone_id . "')
  84.                  and tr.tax_class_id = '" . (int)$class_id . "'
  85.                  order by tr.tax_priority";
  86.  
  87.     $tax = $db->Execute($tax_query);
  88.  
  89.     if ($tax->RecordCount() > 0) {
  90.       $tax_description = '';
  91.       while (!$tax->EOF) {
  92.         $tax_description .= $tax->fields['tax_description'] . ' + ';
  93.         $tax->MoveNext();
  94.       }
  95.       $tax_description = substr($tax_description, 0, -3);
  96.  
  97.       return $tax_description;
  98.     } else {
  99.       return TEXT_UNKNOWN_TAX_RATE;
  100.     }
  101.   }
  102. ////
  103. // Return the tax rates for each defined tax for the given class and zone
  104. // @returns array(description => tax_rate)
  105.   function zen_get_multiple_tax_rates($class_id, $country_id, $zone_id, $tax_description=array()) {
  106.     global $db;
  107.  
  108.     if ( ($country_id == -1) && ($zone_id == -1) ) {
  109.       if (isset($_SESSION['customer_id'])) {
  110.         $country_id = $_SESSION['customer_country_id'];
  111.         $zone_id = $_SESSION['customer_zone_id'];
  112.       } else {
  113.         $country_id = STORE_COUNTRY;
  114.         $zone_id = STORE_ZONE;
  115.       }
  116.     }
  117.  
  118.     $tax_query = "select tax_description, tax_rate, tax_priority
  119.                  from (" . TABLE_TAX_RATES . " tr
  120.                  left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id)
  121.                  left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) )
  122.                  where (za.zone_country_id is null or za.zone_country_id = 0
  123.                  or za.zone_country_id = '" . (int)$country_id . "')
  124.                  and (za.zone_id is null
  125.                  or za.zone_id = 0
  126.                  or za.zone_id = '" . (int)$zone_id . "')
  127.                  and tr.tax_class_id = '" . (int)$class_id . "'
  128.                  order by tr.tax_priority";
  129.     $tax = $db->Execute($tax_query);
  130.  
  131.     // calculate appropriate tax rate respecting priorities and compounding
  132.     if ($tax->RecordCount() > 0) {
  133.       $tax_aggregate_rate = 1;
  134.       $tax_rate_factor = 1;
  135.       $tax_prior_rate = 1;
  136.       $tax_priority = 0;
  137.       while (!$tax->EOF) {
  138.         if ((int)$tax->fields['tax_priority'] > $tax_priority) {
  139.           $tax_priority = $tax->fields['tax_priority'];
  140.           $tax_prior_rate = $tax_aggregate_rate;
  141.           $tax_rate_factor = 1 + ($tax->fields['tax_rate'] / 100);
  142.           $tax_rate_factor *= $tax_aggregate_rate;
  143.           $tax_aggregate_rate = 1;
  144.         } else {
  145.           $tax_rate_factor = $tax_prior_rate * ( 1 + ($tax->fields['tax_rate'] / 100));
  146.         }
  147.         $rates_array[$tax->fields['tax_description']] = 100 * ($tax_rate_factor - $tax_prior_rate);
  148.         $tax_aggregate_rate += $tax_rate_factor - 1;
  149.         $tax->MoveNext();
  150.       }
  151.     } else {
  152.       // no tax at this level, set rate to 0 and description of unknown
  153.       $rates_array[0] = TEXT_UNKNOWN_TAX_RATE;
  154.     }
  155.     return $rates_array;
  156.   }
  157. ////
  158. // Add tax to a products price based on whether we are displaying tax "in" the price
  159.   function zen_add_tax($price, $tax) {
  160.     global $currencies;
  161.  
  162.     if ( (DISPLAY_PRICE_WITH_TAX == 'true') && ($tax > 0) ) {
  163.       return $price + zen_calculate_tax($price, $tax);
  164.     } else {
  165.       return $price;
  166.     }
  167.   }
  168.  
  169.  // Calculates Tax rounding the result
  170.   function zen_calculate_tax($price, $tax) {
  171.     global $currencies;
  172.     return $price * $tax / 100;
  173.   }
  174. ////
  175. // Output the tax percentage with optional padded decimals
  176.   function zen_display_tax_value($value, $padding = TAX_DECIMAL_PLACES) {
  177.     if (strpos($value, '.')) {
  178.       $loop = true;
  179.       while ($loop) {
  180.         if (substr($value, -1) == '0') {
  181.           $value = substr($value, 0, -1);
  182.         } else {
  183.           $loop = false;
  184.           if (substr($value, -1) == '.') {
  185.             $value = substr($value, 0, -1);
  186.           }
  187.         }
  188.       }
  189.     }
  190.  
  191.     if ($padding > 0) {
  192.       if ($decimal_pos = strpos($value, '.')) {
  193.         $decimals = strlen(substr($value, ($decimal_pos+1)));
  194.         for ($i=$decimals; $i<$padding; $i++) {
  195.           $value .= '0';
  196.         }
  197.       } else {
  198.         $value .= '.';
  199.         for ($i=0; $i<$padding; $i++) {
  200.           $value .= '0';
  201.         }
  202.       }
  203.     }
  204.  
  205.     return $value;
  206.   }
  207.  
  208. ////
  209. // Get tax rate from tax description
  210.  function zen_get_tax_rate_from_desc($tax_desc) {
  211.     global $db;
  212.     $tax_rate = 0.00;
  213.  
  214.     $tax_descriptions = explode(' + ', $tax_desc);
  215.     foreach ($tax_descriptions as $tax_description) {
  216.       $tax_query = "SELECT tax_rate
  217.                    FROM " . TABLE_TAX_RATES . "
  218.                    WHERE tax_description = :taxDescLookup";
  219.       $tax_query = $db->bindVars($tax_query, ':taxDescLookup', $tax_description, 'string');
  220.  
  221.       $tax = $db->Execute($tax_query);
  222.  
  223.       $tax_rate += $tax->fields['tax_rate'];
  224.     }
  225.  
  226.     return $tax_rate;
  227.   }
  228.  
  229.  function zen_get_tax_locations($store_country = -1, $store_zone = -1) {
  230.   global $db;
  231.     switch (STORE_PRODUCT_TAX_BASIS) {
  232.  
  233.       case 'Shipping':
  234.         $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
  235.                                from " . TABLE_ADDRESS_BOOK . " ab
  236.                                left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
  237.                                where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
  238.                                and ab.address_book_id = '" . (int)$_SESSION['sendto'] . "'";
  239.         $tax_address_result = $db->Execute($tax_address_query);
  240.       break;
  241.       case 'Billing':
  242.  
  243.         $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
  244.                                from " . TABLE_ADDRESS_BOOK . " ab
  245.                                left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
  246.                                where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
  247.                                and ab.address_book_id = '" . (int)$_SESSION['billto'] . "'";
  248.         $tax_address_result = $db->Execute($tax_address_query);
  249.       break;
  250.       case 'Store':
  251.         $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
  252.                                from " . TABLE_ADDRESS_BOOK . " ab
  253.                                left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
  254.                                where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
  255.                                and ab.address_book_id = '" . (int)$_SESSION['billto'] . "'";
  256.         $tax_address_result = $db->Execute($tax_address_query);
  257.  
  258.         if ($tax_address_result ->fields['entry_zone_id'] == STORE_ZONE) {
  259.  
  260.         } else {
  261.           $tax_address_query = "select ab.entry_country_id, ab.entry_zone_id
  262.                                  from " . TABLE_ADDRESS_BOOK . " ab
  263.                                  left join " . TABLE_ZONES . " z on (ab.entry_zone_id = z.zone_id)
  264.                                  where ab.customers_id = '" . (int)$_SESSION['customer_id'] . "'
  265.                                  and ab.address_book_id = '" . (int)$_SESSION['sendto'] . "'";
  266.         $tax_address_result = $db->Execute($tax_address_query);
  267.        }
  268.      }
  269.      $tax_address['zone_id'] = $tax_address_result->fields['entry_zone_id'];
  270.      $tax_address['country_id'] = $tax_address_result->fields['entry_country_id'];
  271.      return $tax_address;
  272.  }
  273.  function zen_get_all_tax_descriptions($country_id = -1, $zone_id = -1)
  274.  {
  275.    global $db;
  276.     if ( ($country_id == -1) && ($zone_id == -1) ) {
  277.       if (isset($_SESSION['customer_id'])) {
  278.         $country_id = $_SESSION['customer_country_id'];
  279.         $zone_id = $_SESSION['customer_zone_id'];
  280.       } else {
  281.         $country_id = STORE_COUNTRY;
  282.         $zone_id = STORE_ZONE;
  283.       }
  284.     }
  285.    
  286.    $sql = "select tr.*
  287.           from (" . TABLE_TAX_RATES . " tr
  288.           left join " . TABLE_ZONES_TO_GEO_ZONES . " za on (tr.tax_zone_id = za.geo_zone_id)
  289.           left join " . TABLE_GEO_ZONES . " tz on (tz.geo_zone_id = tr.tax_zone_id) )
  290.           where (za.zone_country_id is null
  291.           or za.zone_country_id = 0
  292.           or za.zone_country_id = '" . (int)$country_id . "')
  293.           and (za.zone_id is null
  294.           or za.zone_id = 0
  295.           or za.zone_id = '" . (int)$zone_id . "')";
  296.    $result = $db->Execute($sql);
  297.    $taxDescriptions =array();
  298.    while (!$result->EOF)
  299.    {
  300.      $taxDescriptions[] = $result->fields['tax_description'];
  301.      $result->moveNext();
  302.    }
  303.    return $taxDescriptions;
  304.  }
  305.