rewrite the Recurring Incpome by Client report into 1 Query instead of multiple

This commit is contained in:
johnnyq
2023-02-28 22:41:24 -05:00
parent 94c020f689
commit 47dbd46bf3

View File

@@ -3,7 +3,18 @@
require_once("inc_all_reports.php"); require_once("inc_all_reports.php");
validateAccountantRole(); validateAccountantRole();
$sql_clients = mysqli_query($mysqli, "SELECT * FROM clients WHERE company_id = $session_company_id"); $sql = mysqli_query($mysqli, "
SELECT clients.client_name,
SUM(CASE WHEN recurring.recurring_frequency = 'month' THEN recurring.recurring_amount
WHEN recurring.recurring_frequency = 'year' THEN recurring.recurring_amount / 12 END) AS recurring_monthly_total
FROM clients
LEFT JOIN recurring ON clients.client_id = recurring.recurring_client_id
WHERE clients.company_id = $session_company_id
AND recurring.recurring_status = 1
GROUP BY clients.client_id
HAVING recurring_monthly_total > 0
ORDER BY recurring_monthly_total DESC
");
?> ?>
@@ -26,41 +37,26 @@ $sql_clients = mysqli_query($mysqli, "SELECT * FROM clients WHERE company_id = $
<tbody> <tbody>
<?php <?php
while ($row = mysqli_fetch_array($sql_clients)) { while ($row = mysqli_fetch_array($sql)) {
$client_id = intval($row['client_id']); $client_id = intval($_row['client_id']);
$client_name = htmlentities($row['client_name']); $client_name = htmlentities($row['client_name']);
//Get Monthly Recurring Total
$sql_recurring_monthly_total = mysqli_query($mysqli, "SELECT SUM(recurring_amount) AS recurring_monthly_total FROM recurring WHERE recurring_status = 1 AND recurring_frequency = 'month' AND recurring_client_id = $client_id AND company_id = $session_company_id");
$row = mysqli_fetch_array($sql_recurring_monthly_total);
$recurring_monthly_total = floatval($row['recurring_monthly_total']); $recurring_monthly_total = floatval($row['recurring_monthly_total']);
$recurring_total = $recurring_total + $recurring_monthly_total;
//Get Yearly Recurring Total
$sql_recurring_yearly_total = mysqli_query($mysqli, "SELECT SUM(recurring_amount) AS recurring_yearly_total FROM recurring WHERE recurring_status = 1 AND recurring_frequency = 'year' AND recurring_client_id = $client_id AND company_id = $session_company_id");
$row = mysqli_fetch_array($sql_recurring_yearly_total);
$recurring_yearly_total = floatval($row['recurring_yearly_total']) / 12;
$recurring_monthly = $recurring_monthly_total + $recurring_yearly_total;
$recurring_total = $recurring_total + $recurring_monthly;
if ($recurring_monthly > 0) {
?>
<tr>
<td><?php echo $client_name; ?></td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $recurring_monthly, $session_company_currency); ?></td>
</tr>
<?php
}
}
?> ?>
<tr>
<th>Total</th>
<th class="text-right"><?php echo numfmt_format_currency($currency_format, $recurring_total, $session_company_currency); ?></th> <tr>
</tr> <td><?php echo $client_name; ?></td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $recurring_monthly_total, $session_company_currency); ?></td>
</tr>
<?php
}
?>
<tr>
<th>Total</th>
<th class="text-right"><?php echo numfmt_format_currency($currency_format, $recurring_total, $session_company_currency); ?></th>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>