Feature: Add Income Bulk Actions (Account, Payment Method and category

This commit is contained in:
johnnyq
2026-08-03 15:23:50 -04:00
parent a313278623
commit abd79c0aed
6 changed files with 634 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
<?php
require_once '../../../includes/modal_header.php';
// Income rows are a UNION of payments and revenues, so each checkbox carries a composite
// reference ("Payment-12" / "Revenue-7"). Whitelist the shape before echoing it back.
$income_ids = preg_grep('/^(Payment|Revenue)-[1-9][0-9]*$/', array_filter((array) ($_GET['income_ids'] ?? []), 'is_string'));
$count = count($income_ids);
// Generate the HTML form content using output buffering.
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fa fa-fw fa-piggy-bank mr-2"></i>Set Account for <strong><?= $count ?></strong> Income Record<?= $count == 1 ? '' : 's' ?></h5>
<button type="button" class="close text-white" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<?php foreach ($income_ids as $income_id) { ?> <input type="hidden" name="income_ids[]" value="<?= $income_id ?>"><?php } ?>
<div class="modal-body">
<div class="form-group">
<label>Account <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-piggy-bank"></i></span>
</div>
<select class="form-control select2" name="bulk_account_id" data-placeholder="- Select an Account -" required>
<option></option>
<?php
$sql = mysqli_query($mysqli, "SELECT account_id, account_name, opening_balance FROM accounts WHERE account_archived_at IS NULL ORDER BY account_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$account_id = intval($row['account_id']);
$account_name = escapeHtml($row['account_name']);
$opening_balance = floatval($row['opening_balance']);
$sql_payments = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS total_payments FROM payments WHERE payment_account_id = $account_id");
$row = mysqli_fetch_assoc($sql_payments);
$total_payments = floatval($row['total_payments']);
$sql_revenues = mysqli_query($mysqli, "SELECT SUM(revenue_amount) AS total_revenues FROM revenues WHERE revenue_account_id = $account_id");
$row = mysqli_fetch_assoc($sql_revenues);
$total_revenues = floatval($row['total_revenues']);
$sql_expenses = mysqli_query($mysqli, "SELECT SUM(expense_amount) AS total_expenses FROM expenses WHERE expense_account_id = $account_id");
$row = mysqli_fetch_assoc($sql_expenses);
$total_expenses = floatval($row['total_expenses']);
$balance = $opening_balance + $total_payments + $total_revenues - $total_expenses;
?>
<option value="<?= $account_id ?>"><div class="float-left"><?= $account_name ?></div><div class="float-right"> [$<?= number_format($balance, 2) ?>]</div></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="bulk_edit_income_account" class="btn btn-primary text-bold"><i class="fa fa-fw fa-check mr-2"></i>Set</button>
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fa fa-times mr-2"></i>Cancel</button>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';

View File

@@ -0,0 +1,119 @@
<?php
require_once '../../../includes/modal_header.php';
// Income rows are a UNION of payments and revenues, so each checkbox carries a composite
// reference ("Payment-12" / "Revenue-7"). Whitelist the shape before echoing it back.
$income_ids = preg_grep('/^(Payment|Revenue)-[1-9][0-9]*$/', array_filter((array) ($_GET['income_ids'] ?? []), 'is_string'));
$count = count($income_ids);
// A revenue carries its own category, but a payment does not - it inherits the category from the
// invoice it was paid against, so categorising a payment really means categorising that invoice.
// Work out up front what that will drag along, and say so.
$selected_payment_ids = [];
foreach ($income_ids as $income_ref) {
if (str_starts_with($income_ref, 'Payment-')) {
$selected_payment_ids[] = intval(substr($income_ref, strlen('Payment-')));
}
}
$affected_invoice_count = 0;
$orphan_payment_count = 0;
$sibling_payment_count = 0;
if ($selected_payment_ids) {
$selected_payment_ids_sql = implode(',', $selected_payment_ids);
$sql = mysqli_query($mysqli, "SELECT
SUM(CASE WHEN payment_invoice_id = 0 THEN 1 ELSE 0 END) AS orphan_count,
COUNT(DISTINCT CASE WHEN payment_invoice_id != 0 THEN payment_invoice_id END) AS invoice_count
FROM payments
WHERE payment_id IN ($selected_payment_ids_sql)");
$row = mysqli_fetch_assoc($sql);
$orphan_payment_count = intval($row['orphan_count']);
$affected_invoice_count = intval($row['invoice_count']);
// Other payments sitting on those same invoices - not selected, but they will show the new
// category too, because they read it from the invoice as well
$sql = mysqli_query($mysqli, "SELECT COUNT(*) AS sibling_count FROM payments
WHERE payment_archived_at IS NULL
AND payment_id NOT IN ($selected_payment_ids_sql)
AND payment_invoice_id != 0
AND payment_invoice_id IN (SELECT payment_invoice_id FROM payments WHERE payment_id IN ($selected_payment_ids_sql) AND payment_invoice_id != 0)");
$row = mysqli_fetch_assoc($sql);
$sibling_payment_count = intval($row['sibling_count']);
}
// Generate the HTML form content using output buffering.
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fa fa-fw fa-list mr-2"></i>Set Category for <strong><?= $count ?></strong> Income Record<?= $count == 1 ? '' : 's' ?></h5>
<button type="button" class="close text-white" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<?php foreach ($income_ids as $income_id) { ?> <input type="hidden" name="income_ids[]" value="<?= $income_id ?>"><?php } ?>
<div class="modal-body">
<?php if ($affected_invoice_count || $orphan_payment_count) { ?>
<div class="alert alert-warning">
<?php if ($affected_invoice_count) { ?>
A payment has no category of its own - it takes the one on the invoice it was paid against.
This will set the category on <strong><?= $affected_invoice_count ?></strong> invoice<?= $affected_invoice_count == 1 ? '' : 's' ?>,
which changes how <?= $affected_invoice_count == 1 ? 'that invoice' : 'those invoices' ?> categorise<?= $affected_invoice_count == 1 ? 's' : '' ?> everywhere else too.
<?php if ($sibling_payment_count) { ?>
<br><br><strong><?= $sibling_payment_count ?></strong> further payment<?= $sibling_payment_count == 1 ? '' : 's' ?> against
<?= $affected_invoice_count == 1 ? 'that invoice' : 'those invoices' ?> <?= $sibling_payment_count == 1 ? 'is' : 'are' ?> not selected,
but will show the new category as well.
<?php } ?>
<?php } ?>
<?php if ($orphan_payment_count) { ?>
<?php if ($affected_invoice_count) { ?><br><br><?php } ?>
<strong><?= $orphan_payment_count ?></strong> selected payment<?= $orphan_payment_count == 1 ? '' : 's' ?>
<?= $orphan_payment_count == 1 ? 'has' : 'have' ?> no linked invoice and will be skipped.
<?php } ?>
</div>
<?php } ?>
<div class="form-group">
<label>Category <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-list"></i></span>
</div>
<select class="form-control select2" name="bulk_category_id" data-placeholder="- Select a Category -" required>
<option></option>
<?php
$sql = mysqli_query($mysqli, "SELECT category_id, category_name FROM categories WHERE category_type = 'Income' AND category_archived_at IS NULL ORDER BY category_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$category_id = intval($row['category_id']);
$category_name = escapeHtml($row['category_name']);
?>
<option value="<?= $category_id ?>"><?= $category_name ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="bulk_edit_income_category" class="btn btn-primary text-bold"><i class="fa fa-fw fa-check mr-2"></i>Set</button>
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fa fa-times mr-2"></i>Cancel</button>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';

View File

@@ -0,0 +1,58 @@
<?php
require_once '../../../includes/modal_header.php';
// Income rows are a UNION of payments and revenues, so each checkbox carries a composite
// reference ("Payment-12" / "Revenue-7"). Whitelist the shape before echoing it back.
$income_ids = preg_grep('/^(Payment|Revenue)-[1-9][0-9]*$/', array_filter((array) ($_GET['income_ids'] ?? []), 'is_string'));
$count = count($income_ids);
// Generate the HTML form content using output buffering.
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fa fa-fw fa-money-check-alt mr-2"></i>Set Payment Method for <strong><?= $count ?></strong> Income Record<?= $count == 1 ? '' : 's' ?></h5>
<button type="button" class="close text-white" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<?php foreach ($income_ids as $income_id) { ?> <input type="hidden" name="income_ids[]" value="<?= $income_id ?>"><?php } ?>
<div class="modal-body">
<div class="form-group">
<label>Payment Method <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-money-check-alt"></i></span>
</div>
<select class="form-control select2" name="bulk_payment_method" data-placeholder="- Select a Method of Payment -" required>
<option></option>
<?php
$sql = mysqli_query($mysqli, "SELECT payment_method_name FROM payment_methods ORDER BY payment_method_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$payment_method_name = escapeHtml($row['payment_method_name']);
?>
<option><?= $payment_method_name ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="bulk_edit_income_method" class="btn btn-primary text-bold"><i class="fa fa-fw fa-check mr-2"></i>Set</button>
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fa fa-times mr-2"></i>Cancel</button>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';