Invoicing - bug: fix not being able to add tickets to invoice; feature: add ability to remove tickets from invoice

This commit is contained in:
Wrongecho
2026-09-11 11:56:40 +00:00
parent f558414346
commit cb262139ae
3 changed files with 43 additions and 7 deletions

View File

@@ -141,7 +141,7 @@ if (isset($_GET['invoice_id'])) {
//Get billable, and unbilled tickets to add to invoice
$sql_tickets_billable = mysqli_query(
$mysqli, "
SELECT 1
SELECT ticket_id, ticket_subject, ticket_number, ticket_prefix, ticket_status
FROM
tickets
WHERE
@@ -151,10 +151,9 @@ if (isset($_GET['invoice_id'])) {
AND
ticket_invoice_id = 0
AND
ticket_status = 5;
ticket_status IN (4, 5);
");
//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments WHERE payment_invoice_id = $invoice_id");
$row = mysqli_fetch_assoc($sql_amount_paid);
@@ -697,7 +696,7 @@ if (isset($_GET['invoice_id'])) {
</div>
</div>
</div>
<div class="col-sm d-print-none <?php if (mysqli_num_rows($sql_tickets) == 0) { echo "d-none"; } ?>">
<div class="col-sm d-print-none <?php if (mysqli_num_rows($sql_tickets) == 0 && mysqli_num_rows($sql_tickets_billable) == 0) { echo "d-none"; } ?>">
<div class="card">
<div class="card-header text-bold">
<i class="fa fa-life-ring me-2"></i>Tickets
@@ -728,15 +727,19 @@ if (isset($_GET['invoice_id'])) {
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Subject</th>
<th class="text-end">Time Worked</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($sql_tickets)) {
$ticket_prefix = escapeHtml($row['ticket_prefix']);
$ticket_number = escapeHtml($row['ticket_number']);
$ticket_id = intval($row['ticket_id']);
$ticket_created_at = escapeHtml($row['ticket_created_at']);
$ticket_subject = escapeHtml($row['ticket_subject']);
@@ -744,9 +747,12 @@ if (isset($_GET['invoice_id'])) {
?>
<tr>
<td><a href="ticket.php?ticket_id=<?= $ticket_id ?>"><?= "$ticket_prefix$ticket_number" ?></a></td>
<td><?= $ticket_created_at ?></td>
<td><?= $ticket_subject ?></td>
<td class="text-end"><?= $ticket_total_time_worked ?></td>
<td align="right"><a class="btn btn-light text-danger confirm-link" title="Remove" href="post.php?remove_ticket_from_invoice&invoice_id=<?= $invoice_id ?>&ticket_id=<?= $ticket_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>"><i class="fa fa-times"></i></a></td>
</tr>
<?php
}

View File

@@ -44,8 +44,16 @@
</a>
</td>
<td><?= $ticket_subject ?></td>
<td><a href='ticket.php?ticket_id=<?= $ticket_id ?>&invoice_id=<?= $invoice_id ?>#addInvoiceFromTicketModal'>
<i class="fas fa-fw fa-plus-circle"></i></td>
<td>
<form action="post.php" method="post">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="invoice_id" value="<?= $invoice_id ?>">
<input type="hidden" name="ticket_id" value="<?= $ticket_id ?>">
<button class="btn btn-link p-0" type="submit" name="add_ticket_to_invoice" title="Add ticket to invoice">
<i class="fas fa-fw fa-plus-circle"></i>
</button>
</form>
</td>
</tr>
<?php } ?>
</table>

View File

@@ -1003,6 +1003,7 @@ if (isExportRequest('export_invoices')) {
}
// TODO: This should probably be removed as we now allow multiple invoices to be linked to a single ticket
if (isset($_POST['link_invoice_to_ticket'])) {
validateCSRFToken();
@@ -1041,7 +1042,28 @@ if (isset($_POST['add_ticket_to_invoice'])) {
flashAlert("Ticket linked to invoice");
redirect("post.php?add_ticket_to_invoice=$invoice_id");
redirect("invoice.php?invoice_id=$invoice_id");
}
if (isset($_GET['remove_ticket_from_invoice'])) {
validateCSRFToken();
enforceUserPermission('module_sales', 2);
$invoice_id = intval($_GET['invoice_id']);
$ticket_id = intval($_GET['ticket_id']);
$client_id = intval(getFieldById('tickets', $ticket_id, 'ticket_client_id'));
enforceClientAccess();
mysqli_query($mysqli,"UPDATE tickets SET ticket_invoice_id = 0 WHERE ticket_id = $ticket_id");
flashAlert("Ticket unlinked from invoice");
redirect("invoice.php?invoice_id=$invoice_id");
}