2

目前我已经编写了一个函数来检查订单是否已完全支付。在函数中,第二个函数用于确定总订单价格。在第一个函数中,将总订单价格与订单的所有交易进行比较,并以此为基础确定是否支付订单。

下面提到的功能。我正在寻找一种可以将函数更改为单个查询的方法。就像是:

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_*已弃用,但这是下一个问题。

4

1 回答 1

0

这似乎是一个 SQL 问题。看起来您想要获取订单的总额,然后减去交易总额,以确定他们是否已全额付款。你能做这个吗:

$id = 您的订单编号...

SELECT 
sum(op.price * op.quantity) as order_products_total,
sum(rma.price * rma.quantity) as rma_total,
FROM order 
left join order_products op on ordering_id = order.order_id 
left join rma_products rma on ordering_id  = order.order_id 
WHERE
order.order_id = $id 

您可以对所有left join表执行此操作(我仅以 order_products 和 rma_products 为例),但您可以对 order_shipping 和其他表执行相同操作。

在您的 PHP 中,您将取回所有总数,并且您可以在那里进行比较语句(仅来自一个查询)。

于 2013-03-20T13:22:09.213 回答