format('Y-m-d') !== $next_date) { $error = 'next_date is a date - 2026-11-05.'; } else { // The day it bills on stays put unless the next date moves - set before // next_date, as MySQL reads a column already set in the same UPDATE as its new value $set[] = "recurring_invoice_anchor_day = IF(recurring_invoice_next_date = '$next_date' AND recurring_invoice_anchor_day > 0, recurring_invoice_anchor_day, DAY('$next_date'))"; $set[] = "recurring_invoice_next_date = '$next_date'"; } } if (array_key_exists('scope', $_POST)) { $set[] = "recurring_invoice_scope = '" . escapeSql(mb_substr(trim((string) $_POST['scope']), 0, 255)) . "'"; } if (array_key_exists('note', $_POST)) { $set[] = "recurring_invoice_note = '" . mysqli_real_escape_string($mysqli, (string) $_POST['note']) . "'"; } if (array_key_exists('category_id', $_POST)) { $category_id = intval($_POST['category_id']); if ($category_id && !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM categories WHERE category_id = $category_id AND category_type = 'Income' AND category_archived_at IS NULL"))) { $error = 'category_id is not one of your income categories.'; } $set[] = "recurring_invoice_category_id = $category_id"; } if (array_key_exists('discount', $_POST)) { $discount = billingNumberInput($_POST['discount']); if ($discount === null || $discount < 0) { $error = 'discount is an amount off each invoice, 0 or more.'; } $set[] = "recurring_invoice_discount_amount = " . round(floatval($discount), 2); } if (array_key_exists('auto_send', $_POST)) { $set[] = "recurring_invoice_auto_send = " . (intval($_POST['auto_send']) ? 1 : 0); } if ($error === '' && !$set) { $error = 'Send at least one of frequency, next_date, scope, note, category_id, discount or auto_send.'; } if ($error !== '') { http_response_code(400); $return_arr['success'] = "False"; $return_arr['message'] = $error; echo json_encode($return_arr); exit(); } $update_sql = mysqli_query($mysqli, "UPDATE recurring_invoices SET " . implode(', ', $set) . " WHERE recurring_invoice_id = $recurring_invoice_id AND recurring_invoice_client_id = $client_id LIMIT 1"); if ($update_sql) { // A recurring invoice saved as it was still counts as updated $update_count = 1; billingRecalc('recurring', $recurring_invoice_id); logHistory(intval($row['recurring_invoice_status']) ? 'Active' : 'Paused', "Edited via API ($api_key_name)", 0, 0, $recurring_invoice_id); $reference = escapeSql(billingNumber('recurring', $row)); logAudit("Recurring Invoice", "Edit", "Edited recurring invoice $reference via API ($api_key_name)", $client_id, $recurring_invoice_id); logAudit("API", "Success", "Edited recurring invoice $reference via API ($api_key_name)", $client_id); } } } // Output require_once '../update_output.php';