计算FPDF中的利润总和

时间:2016-12-09 11:46:05

标签: php html mysql report fpdf

我的报告中有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);

这只会检索上一项profit的一条记录,应如何更正以获取每日sum(profit)

enter image description here

更新了代码

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

enter image description here

1 个答案:

答案 0 :(得分:3)

您不能将SQL函数和表达式应用于PHP变量,这些仅在作为SQL查询执行时有效。为了帮助减少混淆并使代码尽可能可读,我建议您为计算值指定列名或别名,例如:

abc.com/user123

然后您可以使用以下方式访问值:

<?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']; 

然后在PDF的底部,您可以使用$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); ... } 输出每日总利润额。

总结然后应用上述所有技术:

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); 行的格式,因为包含这些内容的源代码不完整。

另外如果您不熟悉前缀,我在答案源代码中使用匈牙利表示法,即$ oQuery中的o表示对象,$ aRows中的a表示数组,d表示$ dTotalProfit是for double,我可能已经将i用于整数,b用于布尔值等,如果需要的话。如果您可以从名称中识别变量类型,这只会有助于代码可读性。