【问题标题】:Calculate SUM of profits in FPDF计算 FPDF 中的利润总和
【发布时间】:2016-12-09 11:46:04
【问题描述】:

我有一个包含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)

更新代码

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

【问题讨论】:

  • 您的代码向我们展示了您到目前为止所尝试的内容?
  • 啊抱歉我的错……现在可以看到了。
  • 我认为您确实按 item_id 分组,因此您将根据该特定项目获得一条记录的结果,因此我认为您应该在此之后使用 array_sum() 来计算全天利润
  • @jilesh 感谢您的解决方案。感谢您是否可以帮助我构建代码。

标签: php html mysql report fpdf


【解决方案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-&gt;Cell 行的格式,因为您包含的源代码在包含这些时并不完整。

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

【讨论】:

  • 感谢伟大的代码。我将它应用到我的代码中。现在,我检索一条记录(第一条记录),并获取报告中未显示的记录的每日总利润。我该如何解决这个问题?请在我的原始帖子中找到更新后的代码和打印屏幕。
  • 看起来最终的总利润是正确的。您是否确保添加行行的代码行位于增加总利润的同一循环内?即您在更新的代码中注释掉了//$pdf-&gt;Cell(...) 行的位置?在foreach 循环内,我希望看到您的所有列行,例如我在示例中包含的行。我已经用更完整的代码 sn-p 更新了我的答案来帮助你。
猜你喜欢
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2021-10-11
  • 2013-08-30
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多