我有一个包含 profit
列的报告。我需要添加利润列的所有值并获得当天的总利润。感谢任何帮助,因为我已经完成了 SO,但没有找到解决方案。
$result = mysqli_query($connection, "SELECT S.sales_id, I.item_name, ST.stock_code,
S.date, S.unit_price, SUM(S.qty), (SUM(S.qty)*S.unit_price),
((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)) FROM sales S
INNER JOIN items I ON S.item_id=I.item_id
INNER JOIN stock ST ON S.stock_id=ST.stock_id
INNER JOIN purchase_items P ON S.purchase_id=P.purchase_id
WHERE S.date BETWEEN '$date 00:00:00' AND '$date 23:59:59' GROUP BY S.item_id");
$qty = $row['SUM(S.qty)'];
$unit_price = $row['unit_price'];
$amount = $row['(SUM(S.qty)*S.unit_price)'];
$profit = $row['((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost))'];
$dailyProfit = $row['(SUM(SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)))'];
$pdf->Cell(25,10,$dailyProfit,'B',0,'J',1);
这只检索了一条包含最后一项的利润
的记录,应该如何更正以获得每日的sum(profit)
?
更新代码
$result = mysqli_query($connection, "SELECT S.sales_id, I.item_name, ST.stock_code, S.date,
S.unit_price, SUM(S.qty) AS line_qty, (SUM(S.qty)*S.unit_price) AS line_total,
((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)) AS line_profit
FROM sales S
INNER JOIN items I ON S.item_id=I.item_id
INNER JOIN stock ST ON S.stock_id=ST.stock_id
INNER JOIN purchase_items P ON S.purchase_id=P.purchase_id
WHERE S.date BETWEEN '$date 00:00:00' AND '$date 23:59:59' GROUP BY S.item_id");
$qty = $row['line_qty'];
$unit_price = $row['unit_price'];
$amount = $row['line_total'];
$profit = $row['line_profit'];
$aRows = array();
while ($oRow = mysqli_fetch_assoc($result)) $aRows[] = $oRow;
mysqli_free_result($result);
$dTotalProfit = 0;
if (count($aRows) > 0) foreach ($aRows as $aRow) {
$dTotalProfit += $aRow['line_profit'];
//$pdf->Cell(25, 10, $aRow['line_qty'], 'B', 0, 'J', 1);
}
$totalProfit = number_format($dTotalProfit, 2);
$pdf->Cell(220, 10, 'Daily Total Profit:', 'B', 0, 'R', 1);
$pdf->Cell(25, 10, $totalProfit, 'B', 0, 'J', 1);
最佳答案
您不能将 SQL 函数和表达式应用于 PHP 变量,它们仅在作为 SQL 查询执行时有效。为了帮助减少混淆并使您的代码尽可能可读,我建议您为计算值分配列名或别名,例如:
<?php
$sSQL = "SELECT S.sales_id, I.item_name, ST.stock_code, S.date, S.unit_price,
SUM(S.qty) AS line_qty, (SUM(S.qty)*S.unit_price) AS line_total,
((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)) AS line_profit
FROM sales S
INNER JOIN items I ON S.item_id=I.item_id
INNER JOIN stock ST ON S.stock_id=ST.stock_id
INNER JOIN purchase_items P ON S.purchase_id=P.purchase_id
WHERE S.date BETWEEN '$date 00:00:00' AND '$date 23:59:59' GROUP BY S.item_id";
然后您可以使用以下方法访问这些值:
$qty = $row['line_qty'];
$unit_price = $row['unit_price'];
$amount = $row['line_total'];
$profit = $row['line_profit'];
要获得每日总利润,您需要对所有返回行的值求和,在此上下文中,这很容易在循环中完成:
$oConnection = mysqli_connect($sHostname, $sUsername, $sPassword);
mysqli_select_db($oConnection, $sDatabaseName);
$oResult = mysqli_query($oConnection, $sSQL);
$aRows = array();
while ($oRow = mysqli_fetch_assoc($oResult)) $aRows[] = $oRow;
mysqli_free_result($result);
$dTotalProfit = 0;
if (count($aRows) > 0) foreach ($aRows as $aRow) {
$dTotalProfit += $aRow['line_profit'];
$pdf->Cell(25, 10, $aRow['line_qty'], 'B', 0, 'J', 1);
...
}
然后在您的 PDF 底部,您可以使用 number_format($dTotalProfit, 2)
输出您的每日总利润金额。
然后总结并应用以上所有技术:
$oQuery = mysqli_query($connection, "SELECT S.sales_id, I.item_name, ST.stock_code, S.date,
S.unit_price, SUM(S.qty) AS line_qty, (SUM(S.qty)*S.unit_price) AS line_total,
((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)) AS line_profit
FROM sales S
INNER JOIN items I ON S.item_id=I.item_id
INNER JOIN stock ST ON S.stock_id=ST.stock_id
INNER JOIN purchase_items P ON S.purchase_id=P.purchase_id
WHERE S.date BETWEEN '$date 00:00:00' AND '$date 23:59:59' GROUP BY S.item_id");
$aRows = array();
while ($oRow = mysqli_fetch_assoc($oQuery)) $aRows[] = $oRow;
mysqli_free_result($oQuery);
$aColumnWidths = array(25, 20, 20, 100, 20, 20, 20, 20);
$dTotalProfit = 0;
if (count($aRows) > 0) foreach ($aRows as $aRow) {
$dTotalProfit += $aRow['line_profit'];
$pdf->Cell($aColumnWidths[0], 10, $aRow['date'], 'B', 0, 'J', 1);
$pdf->Cell($aColumnWidths[1], 10, $aRow['sales_id'], 'B', 0, 'J', 1);
$pdf->Cell($aColumnWidths[2], 10, $aRow['stock_code'], 'B', 0, 'J', 1);
$pdf->Cell($aColumnWidths[3], 10, $aRow['item_name'], 'B', 0, 'L', 1);
$pdf->Cell($aColumnWidths[4], 10, $aRow['line_qty'], 'B', 0, 'R', 1);
$pdf->Cell($aColumnWidths[5], 10, $aRow['unit_price'], 'B', 0, 'R', 1);
$pdf->Cell($aColumnWidths[6], 10, $aRow['line_total'], 'B', 0, 'R', 1);
$pdf->Cell($aColumnWidths[7], 10, $aRow['line_profit'], 'B', 0, 'R', 1);
$pdf->Ln();
}
$pdf->Ln();
$pdf->Cell($aColumnWidths[0] + $aColumnWidths[1] + $aColumnWidths[2] + $aColumnWidths[3] +
$aColumnWidths[4] + $aColumnWidths[5] + $aColumnWidths[6], 10,
'Daily Total Profit:', 'B', 0, 'R', 1);
$pdf->Cell($aColumnWidths[7], 10, number_format($dTotalProfit, 2), 'B', 0, 'R', 1);
在此代码中,您还会注意到我已将日期列移至开头,因为通常在财务报告中,日期会显示在左侧,然后最右侧的利润列将在正下方显示总计。我已经猜测了您的 $pdf->Cell
行的格式,因为您包含的源代码在包含这些行时并不完整。
此外,如果您不熟悉前缀,我在我的答案源代码中使用了匈牙利表示法,即 $oQuery 中的 o 表示对象,$aRows 中的 a 表示数组,$dTotalProfit 中的 d是 double 的,如果需要的话,我可能用 i 表示整数,b 表示 bool 值等。如果您可以从名称中识别变量类型,这将有助于提高代码可读性。
关于php - 计算 FPDF 中的利润总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41059761/