2009-12-18 4 views
0

나는 잠시 전에 나왔던 자습서의 장바구니 스크립트를 사용하고 있으며, 주 (펜실베니아)의 판매 세를 계산하기 위해 그것을 수정해야합니다. 상태를 추출하고 펜실베니아 또는 PA와 비교하여 누군가가 국가 약어를 쓰고이 품목의 판매 세가 소계 (배송 전에 추가됨)를 곱하여 소계에 추가합니다 . 어떻게해야합니까? 다음은 총계에 대한 계산을 수행하는 스크립트입니다.이 스크립트로 판매 세를 계산하는 방법은 무엇입니까?

<?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

답변

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에서 모든 단일 우편 번호를 비교하는 방법을 알지 못합니다. –

+0

스크립트로 작업하기 위해 제공 한이 코드를 다시 작성하려고하고 있는데 왜 특정 변수 이름에 "rs"를 선택했는지 궁금합니다. 그것은 무엇을 의미할까요? –

+0

나는 그것이 중요하지 않다는 것을 안다. 그리고 나는 그것을 내가 원했던 것으로 이름을 바꿀 수 있었다. 그러나 당신이 올 생각의 기차를 이해하고 싶다. –

관련 문제