Added Recurring Payment Creation and Deletion and display in the recurring Invoice sections, still not operational need to add to cron

This commit is contained in:
johnnyq 2024-12-21 18:49:42 -05:00
parent 63015ab22d
commit 87a86803ee
4 changed files with 187 additions and 5 deletions

View File

@ -16,6 +16,7 @@ $sql = mysqli_query(
$mysqli,
"SELECT * FROM recurring
LEFT JOIN categories ON recurring_category_id = category_id
LEFT JOIN recurring_payments ON recurring_payment_recurring_invoice_id = recurring_id
WHERE recurring_client_id = $client_id
AND (CONCAT(recurring_prefix,recurring_number) LIKE '%$q%' OR recurring_frequency LIKE '%$q%' OR recurring_scope LIKE '%$q%' OR category_name LIKE '%$q%')
ORDER BY $sort $order LIMIT $record_from, $record_to");
@ -104,6 +105,11 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
Category <?php if ($sort == 'category_name') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-secondary" href="?<?php echo $url_query_strings_sort; ?>&sort=recurring_payment_recurring_invoice_id&order=<?php echo $disp; ?>">
Auto Pay <?php if ($sort == 'recurring_payment_recurring_invoice_id') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-secondary" href="?<?php echo $url_query_strings_sort; ?>&sort=recurring_status&order=<?php echo $disp; ?>">
Status <?php if ($sort == 'recurring_status') { echo $order_icon; } ?>
@ -140,6 +146,22 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$status = "Inactive";
$status_badge_color = "secondary";
}
$recurring_payment_recurring_invoice_id = intval($row['recurring_payment_recurring_invoice_id']);
if ($recurring_payment_recurring_invoice_id) {
$auto_pay_display = "
Yes
<a href='post.php?delete_recurring_payment=$recurring_payment_id' title='Remove'>
<i class='fas fa-fw fa-times-circle'></i>
</a>
";
} else {
$auto_pay_display = "
<a href='#' data-toggle='modal' data-target='#addRecurringPaymentModal$recurring_id'>
Create
</a>
";
require "recurring_payment_add_modal.php";
}
?>
@ -151,10 +173,11 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
<td><?php echo $recurring_last_sent; ?></td>
<td><?php echo $recurring_next_date; ?></td>
<td><?php echo $category_name; ?></td>
<td><?php echo $auto_pay_display; ?></td>
<td>
<span class="p-2 badge badge-<?php echo $status_badge_color; ?>">
<?php echo $status; ?>
</span>
<span class="p-2 badge badge-<?php echo $status_badge_color; ?>">
<?php echo $status; ?>
</span>
</td>
<td>
<div class="dropdown dropleft text-center">
@ -177,7 +200,8 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
</tr>
<?php
require "recurring_invoice_edit_modal.php";
//require "recurring_invoice_edit_modal.php";
}

View File

@ -1192,6 +1192,64 @@ if (isset($_GET['email_invoice'])) {
}
if (isset($_POST['add_recurring_payment'])) {
$recurring_id = intval($_POST['recurring_id']);
$account = intval($_POST['account']);
$currency_code = sanitizeInput($_POST['currency_code']);
$payment_method = sanitizeInput($_POST['payment_method']);
// Get Recurring Info for logging and alerting
$sql = mysqli_query($mysqli, "SELECT * FROM recurring WHERE recurring_id = $recurring_id");
$row = mysqli_fetch_array($sql);
$recurring_prefix = nullable_htmlentities($row['recurring_prefix']);
$recurring_number = intval($row['recurring_number']);
$recurring_amount = floatval($row['recurring_amount']);
$client_id = intval($row['recurring_client_id']);
mysqli_query($mysqli,"INSERT INTO recurring_payments SET recurring_payment_amount = $recurring_amount, recurring_payment_currency_code = '$currency_code', recurring_payment_account_id = $account, recurring_payment_method = '$payment_method', recurring_payment_recurring_invoice_id = $recurring_id");
// Get Payment ID for reference
$recurring_payment_id = mysqli_insert_id($mysqli);
// Logging
logAction("Recurring Invoice", "Auto Payment", "$session_name created Auto Pay for Recurring Invoice $recurring_prefix$recurring_number in the amount of " . numfmt_format_currency($currency_format, $recurring_amount, $currency_code), $client_id, $recurring_id);
$_SESSION['alert_message'] = "Automatic Payment created for <strong>$recurring_prefix$recurring_number</strong>";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if (isset($_GET['delete_recurring_payment'])) {
$recurring_payment_id = intval($_GET['delete_recurring_payment']);
$sql = mysqli_query($mysqli,"SELECT * FROM recurring_payments WHERE recurring_payment_id = $recurring_payment_id");
$row = mysqli_fetch_array($sql);
$recurring_invoice_id = intval($row['recurring_payment_recurring_invoice_id']);
// Get the invoice total and details
$sql = mysqli_query($mysqli,"SELECT * FROM recurring WHERE recurring_id = $recurring_invoice_id");
$row = mysqli_fetch_array($sql);
$recurring_prefix = sanitizeInput($row['recurring_prefix']);
$recurring_number = intval($row['recurring_number']);
$client_id = intval($row['recurring_client_id']);
mysqli_query($mysqli,"DELETE FROM recurring_payments WHERE recurring_payment_id = $recurring_payment_id");
// Logging
logAction("Recurring Invoice", "Auto Payment", "$session_name removed auto Pay from Recurring Invoice $recurring_prefix$recurring_number", $client_id, $recurring_invoice_id);
$_SESSION['alert_type'] = "error";
$_SESSION['alert_message'] = "Auto Payment Removed for Recurring Invoice <strong>$recurring_prefix$recurring_number</strong>";
if ($config_stripe_enable) {
$_SESSION['alert_message'] = "Auto Payment Removed - Stripe Auto payments must be manually removed in Stripe";
}
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if (isset($_GET['force_recurring'])) {
$recurring_id = intval($_GET['force_recurring']);

View File

@ -17,6 +17,7 @@ $sql = mysqli_query(
"SELECT SQL_CALC_FOUND_ROWS * FROM recurring
LEFT JOIN clients ON recurring_client_id = client_id
LEFT JOIN categories ON recurring_category_id = category_id
LEFT JOIN recurring_payments ON recurring_payment_recurring_invoice_id = recurring_id
WHERE (CONCAT(recurring_prefix,recurring_number) LIKE '%$q%' OR recurring_frequency LIKE '%$q%' OR recurring_scope LIKE '%$q%' OR client_name LIKE '%$q%' OR category_name LIKE '%$q%')
AND DATE(recurring_created_at) BETWEEN '$dtf' AND '$dtt'
ORDER BY $sort $order LIMIT $record_from, $record_to");
@ -123,12 +124,16 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
Last Sent <?php if ($sort == 'recurring_last_sent') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=category_name&order=<?php echo $disp; ?>">
Category <?php if ($sort == 'category_name') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=recurring_payment_recurring_invoice_id&order=<?php echo $disp; ?>">
Auto Pay <?php if ($sort == 'recurring_payment_recurring_invoice_id') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?php echo $url_query_strings_sort; ?>&sort=recurring_status&order=<?php echo $disp; ?>">
Status <?php if ($sort == 'recurring_status') { echo $order_icon; } ?>
@ -168,6 +173,23 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$status = "Inactive";
$status_badge_color = "secondary";
}
$recurring_payment_id = intval($row['recurring_payment_id']);
$recurring_payment_recurring_invoice_id = intval($row['recurring_payment_recurring_invoice_id']);
if ($recurring_payment_recurring_invoice_id) {
$auto_pay_display = "
Yes
<a href='post.php?delete_recurring_payment=$recurring_payment_id' title='Remove'>
<i class='fas fa-fw fa-times-circle'></i>
</a>
";
} else {
$auto_pay_display = "
<a href='#' data-toggle='modal' data-target='#addRecurringPaymentModal$recurring_id'>
Create
</a>
";
require "recurring_payment_add_modal.php";
}
?>
@ -182,6 +204,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
<td><?php echo ucwords($recurring_frequency); ?>ly</td>
<td><?php echo $recurring_last_sent; ?></td>
<td><?php echo $category_name; ?></td>
<td><?php echo $auto_pay_display; ?></td>
<td>
<span class="p-2 badge badge-<?php echo $status_badge_color; ?>">
<?php echo $status; ?>

View File

@ -0,0 +1,77 @@
<div class="modal" id="addRecurringPaymentModal<?php echo $recurring_id; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content bg-dark">
<div class="modal-header">
<h5 class="modal-title"><i class="fa fa-fw fa-credit-card mr-2"></i><?php echo "$recurring_prefix$recurring_number"; ?>: Create Recurring Payment</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="recurring_id" value="<?php echo $recurring_id; ?>">
<input type="hidden" name="currency_code" value="<?php echo $recurring_currency_code; ?>">
<div class="modal-body bg-white">
<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="account" required>
<option value="">- Select an Account -</option>
<?php
$sql_account_select = mysqli_query($mysqli, "SELECT * FROM accounts WHERE account_archived_at IS NULL ORDER BY account_name ASC");
while ($row = mysqli_fetch_array($sql_account_select)) {
$account_id_select = intval($row['account_id']);
$account_name_select = nullable_htmlentities($row['account_name']);
?>
<option <?php if ($config_default_payment_account == $account_id_select) { echo "selected"; } ?>
value="<?php echo $account_id_select; ?>">
<?php echo $account_name_select; ?>
</option>
<?php
}
?>
</select>
</div>
</div>
<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="payment_method" required>
<option value="">- Method of Payment -</option>
<?php
$sql_payment_method_select = mysqli_query($mysqli, "SELECT * FROM categories WHERE category_type = 'Payment Method' AND category_archived_at IS NULL ORDER BY category_name ASC");
while ($row = mysqli_fetch_array($sql_payment_method_select)) {
$category_name_select = nullable_htmlentities($row['category_name']);
?>
<option <?php if ($config_default_payment_method == $category_name_select) {
echo "selected";
} ?>><?php echo $category_name_select; ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="modal-footer bg-white">
<button type="submit" name="add_recurring_payment" class="btn btn-primary text-bold"><i class="fas fa-check mr-2"></i>Create</button>
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fas fa-times mr-2"></i>Cancel</button>
</div>
</form>
</div>
</div>
</div>