2009-12-18 91 views
0

我使用的是前段時間我遇到的教程中的購物車腳本,我需要修改它來計算我的州(賓夕法尼亞州)的銷售稅。我希望它提取狀態,將它與賓夕法尼亞州或賓夕法尼亞州進行比較,因爲有人寫狀態簡寫,然後將小計(對於項目......在發貨前添加)乘以該州的銷售稅並將其添加到小計。我應該如何去做這件事?這是用於總計算的腳本。如何使用此腳本計算銷售稅?

<?php 
require_once 'config.php'; 

/********************************************************* 
*     CHECKOUT FUNCTIONS 
*********************************************************/ 
function saveOrder() 
{ 
    $orderId  = 0; 
    $shippingCost = 5; 
    $requiredField = array('hidShippingFirstName', 'hidShippingLastName', 'hidShippingAddress1', 'hidShippingCity', 'hidShippingPostalCode', 
          'hidPaymentFirstName', 'hidPaymentLastName', 'hidPaymentAddress1', 'hidPaymentCity', 'hidPaymentPostalCode'); 

    if (checkRequiredPost($requiredField)) { 
     extract($_POST); 

     // make sure the first character in the 
     // customer and city name are properly upper cased 
     $hidShippingFirstName = ucwords($hidShippingFirstName); 
     $hidShippingLastName = ucwords($hidShippingLastName); 
     $hidPaymentFirstName = ucwords($hidPaymentFirstName); 
     $hidPaymentLastName = ucwords($hidPaymentLastName); 
     $hidShippingCity  = ucwords($hidShippingCity); 
     $hidPaymentCity  = ucwords($hidPaymentCity); 

     $cartContent = getCartContent(); 
     $numItem  = count($cartContent); 

     // save order & get order id 
     $sql = "INSERT INTO tbl_order(od_date, od_last_update, od_shipping_first_name, od_shipping_last_name, od_shipping_address1, 
             od_shipping_address2, od_shipping_phone, od_shipping_state, od_shipping_city, od_shipping_postal_code, od_shipping_cost, 
             od_payment_first_name, od_payment_last_name, od_payment_address1, od_payment_address2, 
             od_payment_phone, od_payment_state, od_payment_city, od_payment_postal_code) 
       VALUES (NOW(), NOW(), '$hidShippingFirstName', '$hidShippingLastName', '$hidShippingAddress1', 
         '$hidShippingAddress2', '$hidShippingPhone', '$hidShippingState', '$hidShippingCity', '$hidShippingPostalCode', '$shippingCost', 
         '$hidPaymentFirstName', '$hidPaymentLastName', '$hidPaymentAddress1', 
         '$hidPaymentAddress2', '$hidPaymentPhone', '$hidPaymentState', '$hidPaymentCity', '$hidPaymentPostalCode')"; 
     $result = dbQuery($sql); 

     // get the order id 
     $orderId = dbInsertId(); 

     if ($orderId) { 
      // save order items 
      for ($i = 0; $i < $numItem; $i++) { 
       $sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty) 
         VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})"; 
       $result = dbQuery($sql);      
      } 


      // update product stock 
      for ($i = 0; $i < $numItem; $i++) { 
       $sql = "UPDATE tbl_product 
         SET pd_qty = pd_qty - {$cartContent[$i]['ct_qty']} 
         WHERE pd_id = {$cartContent[$i]['pd_id']}"; 
       $result = dbQuery($sql);      
      } 


      // then remove the ordered items from cart 
      for ($i = 0; $i < $numItem; $i++) { 
       $sql = "DELETE FROM tbl_cart 
         WHERE ct_id = {$cartContent[$i]['ct_id']}"; 
       $result = dbQuery($sql);      
      }       
     }     
    } 

    return $orderId; 
} 

/* 
    Get order total amount (total purchase + shipping cost) 
*/ 
function getOrderAmount($orderId) 
{ 
    $orderAmount = 0; 

    $sql = "SELECT SUM(pd_price * od_qty) 
      FROM tbl_order_item oi, tbl_product p 
      WHERE oi.pd_id = p.pd_id and oi.od_id = $orderId 

      UNION 

      SELECT od_shipping_cost 
      FROM tbl_order 
      WHERE od_id = $orderId"; 
    $result = dbQuery($sql); 

    if (dbNumRows($result) == 2) { 
     $row = dbFetchRow($result); 
     $totalPurchase = $row[0]; 

     $row = dbFetchRow($result); 
     $shippingCost = $row[0]; 

     $orderAmount = $totalPurchase + $shippingCost; 
    } 

    return $orderAmount;  
} 

?> 
+1

編輯你的問題,並把4位的每一行代碼之前,即使它在原來已經縮進。這會將其格式化爲代碼並保留原始縮進。您也可以使用文本編輯器工具欄中的「101 \ n010」按鈕。 – outis 2009-12-18 04:41:11

回答

0

您不必爲每個城市處理不同的稅率?通常這可以通過郵政編碼在稅表中查找,甚至可以用於街道地址+郵政編碼(在某些州)。

它應該是這樣的:

// is it a Pennsylvania zip code? 
if ($zipcode < 15000 || $zipcode > 19699) // needs verification of the actual range 
    $taxrate = 0; 
else 
{ 
    $rs = mysql_query ("select rate from taxtable where zipcode = '$zipcode'", $db); 
    if (!$rs) 
    { 
     // error looking up rate, maybe apply a default? 
    } 
    else 
    { 
     $row = mysql_fetch_assoc($rs); 
     $taxrate = $row['rate']; 
    } 
} 

$amount = $amount * (1 + $taxrate/100.0); 
+0

聽起來這樣會讓它變得比我準備好要複雜得多,但根據維基百科: 賓夕法尼亞州的銷售稅率爲6%。阿勒格尼縣的銷售稅率爲7%,費城的銷售稅率爲8%。只用8%來覆蓋整個範圍會更簡單嗎?我不知道如何比較PA中的每個郵政編碼。 – 2009-12-18 05:22:51

+0

我試圖重寫你提供的代碼來處理我的腳本,我想知道爲什麼你爲那個特定的變量名選擇「rs」。它應該代表什麼? – 2009-12-19 05:47:30

+0

我知道這並不重要,我可以將它重命名爲任何我想要的,但是我想了解您來自哪裏的思路。 – 2009-12-19 05:50:35