如何用这个脚本计算销售税?

发布于 2024-08-15 12:01:49 字数 4324 浏览 2 评论 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;    
}

?>

I'm using a shopping cart script from a tutorial I came across a while ago, and I need to modify it to calculate sales tax for my state (Pennsylvania). I want it to extract the state, compare it to either Pennsylvania or PA incase someone writes the state shorthand and then multiply the subtotal (for the items.. before shipping is added) by the sales tax for this state and add that to the subtotal. How should I go about doing this? Here is the script that does the calculation for grand total.

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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红玫瑰 2024-08-22 12:01:50

您不必处理每个城市不同的税率?通常,这可以通过邮政编码从税表中查找,甚至可以通过街道地址+邮政编码(在某些州)查找。

它看起来像这样:

// 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);

You don't have to handle differing tax rates per municipality? Normally this would be looked up from a tax table by zip code, and maybe even for the street address + zip code (in some states).

It would look like this:

// 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文