目前我已经编写了一个函数来检查订单是否已完全支付。在函数中,第二个函数用于确定总订单价格。在第一个函数中,将总订单价格与订单的所有交易进行比较,并以此为基础确定是否支付订单。
下面提到的功能。我正在寻找一种可以将函数更改为单个查询的方法。就像是:
SELECT *
FROM order
WHERE
((Grandtotal - TransactionTotal) <= 0)
这样查询返回,例如,如果已付款,则返回 true,如果未付款,则返回 false。
功能 IsOrderPaid:
function IsOrderPaid($order_id){
$returnvalue = false;
$ordertotal = CalculateOrderTotal($order_id);
$sql = "SELECT SUM(amount) AS TransactionTotal FROM order_transaction WHERE order_id='".mysql_real_escape_string($order_id)."' AND status='9'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($result);
if(($ordertotal - $row->TransactionTotal) <= 0 ){
$returnvalue = true;
}
return $returnvalue;
}
函数CalculateOrderTotal
function CalculateOrderTotal($order_id){
$sql = "SELECT
order_id, SUM(total) GrandTotal
FROM
(
SELECT ordering_id AS order_id, SUM(price * quantity) AS total
FROM order_products
WHERE order_id = ".mysql_real_escape_string($order_id)."
UNION ALL
SELECT ordering_id AS order_id, SUM(price * quantity * -1) AS total
FROM rma_products
WHERE bestelling_id = ".mysql_real_escape_string($order_id)."
UNION ALL
SELECT order_id, amount AS total
FROM order_shipping
WHERE order_id = ".mysql_real_escape_string($order_id)."
UNION ALL
SELECT order_id, amount AS total
FROM order_service
WHERE order_id = ".mysql_real_escape_string($order_id)."
UNION ALL
SELECT order_id, SUM(amount * -1) AS total
FROM order_gift
WHERE order_id = ".mysql_real_escape_string($order_id)."
)subTable
WHERE order_id <> 0
GROUP BY order_id";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($result);
return $row->GrandTotal;
}
我知道mysql_*
已弃用,但这是下一个问题。