Php 如何使用此脚本计算增值税?
我正在使用前一段时间遇到的教程中的购物车脚本,我需要修改它以计算我所在州(宾夕法尼亚州)的销售税。我想让它提取该州,将其与宾夕法尼亚州或宾夕法尼亚州进行比较,以防有人编写该州的速记,然后将小计(对于项目..在添加装运之前)乘以该州的销售税,并将其添加到小计中。我该怎么做呢?下面是计算总计的脚本Php 如何使用此脚本计算增值税?,php,math,shopping-cart,Php,Math,Shopping Cart,我正在使用前一段时间遇到的教程中的购物车脚本,我需要修改它以计算我所在州(宾夕法尼亚州)的销售税。我想让它提取该州,将其与宾夕法尼亚州或宾夕法尼亚州进行比较,以防有人编写该州的速记,然后将小计(对于项目..在添加装运之前)乘以该州的销售税,并将其添加到小计中。我该怎么做呢?下面是计算总计的脚本 <?php require_once 'config.php'; /********************************************************* *
<?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;
}
?>
您不必处理每个市的不同税率?通常情况下,这将通过邮政编码从税务表中查找,甚至可能是街道地址+邮政编码(在某些州)
它看起来是这样的:
// 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);
//是宾夕法尼亚州的邮政编码吗?
如果($zipcode<15000 | |$zipcode>19699)//需要验证实际范围
$taxrate=0;
其他的
{
$rs=mysql_查询(“从taxtable中选择rate,其中zipcode='$zipcode'”,$db);
如果(!$rs)
{
//查找速率时出错,是否应用默认值?
}
其他的
{
$row=mysql\u fetch\u assoc($rs);
$taxrate=$row['rate'];
}
}
$amount=$amount*(1+taxrate/100.0);
编辑您的问题,并在每行代码前放置4个空格,即使原始代码中已缩进。这会将其格式化为代码并保留原始缩进。您也可以使用文本编辑器工具栏中的“101\n010”按钮。这听起来似乎比我准备要讨论的要复杂得多,但根据维基百科:宾夕法尼亚州的销售税率为6%。阿勒格尼县的销售税率为7%,费城的销售税率为8%。如果只加8%就可以覆盖整个范围,会更简单吗?我不知道该如何比较PA中的每一个邮政编码。我正试图重写您提供的用于我的脚本的代码,我想知道您为什么选择“rs”作为特定变量名。它应该代表什么?我知道这无关紧要,我可以将它重命名为我想要的任何名称,但我想了解您的思路。$rs
代表“结果”或“结果集”。PHP文档()中的示例使用$result,但为了简洁起见,我经常使用$rs
。您需要注意在货币上进行浮点计算的陷阱。看见