diff --git a/cron/nightly_tasks.php b/cron/nightly_tasks.php
index a69312fef..de23b7b4d 100644
--- a/cron/nightly_tasks.php
+++ b/cron/nightly_tasks.php
@@ -674,7 +674,7 @@ if ($config_send_invoice_reminders == 1) {
// Logging
// logAudit("Cron", "Task", "Cron created notifications for past due invoices and sent out notifications to the primary and billing contacts email");
-// Send Recurring Invoices that match todays date and are active
+// Generate & Send Recurring Invoices that match todays date and are active
//Loop through all recurring that match today's date and is active
$sql_recurring_invoices = mysqli_query($mysqli, "SELECT client_name, client_net_terms, recurring_invoice_amount, recurring_invoice_category_id,
@@ -682,7 +682,7 @@ $sql_recurring_invoices = mysqli_query($mysqli, "SELECT client_name, client_net_
recurring_invoice_discount_amount, recurring_invoice_email_notify,
recurring_invoice_frequency, recurring_invoice_id, recurring_invoice_last_sent,
recurring_invoice_next_date, recurring_invoice_note, recurring_invoice_scope,
- recurring_invoice_status, recurring_payment_account_id, recurring_payment_currency_code,
+ recurring_invoice_status, recurring_invoice_auto_send, recurring_payment_account_id, recurring_payment_currency_code,
recurring_payment_method, recurring_payment_recurring_invoice_id FROM recurring_invoices
LEFT JOIN recurring_payments ON recurring_invoice_id = recurring_payment_recurring_invoice_id
LEFT JOIN clients ON client_id = recurring_invoice_client_id
@@ -701,6 +701,7 @@ while ($row = mysqli_fetch_assoc($sql_recurring_invoices)) {
$recurring_invoice_amount = floatval($row['recurring_invoice_amount']);
$recurring_invoice_currency_code = escapeSql($row['recurring_invoice_currency_code']);
$recurring_invoice_note = escapeSql($row['recurring_invoice_note']);
+ $recurring_invoice_auto_send = intval($row['recurring_invoice_auto_send']);
$recurring_invoice_email_notify = intval($row['recurring_invoice_email_notify']);
$category_id = intval($row['recurring_invoice_category_id']);
$client_id = intval($row['recurring_invoice_client_id']);
@@ -726,7 +727,7 @@ while ($row = mysqli_fetch_assoc($sql_recurring_invoices)) {
//Generate a unique URL key for clients to access
$url_key = randomString(32);
- mysqli_query($mysqli, "INSERT INTO invoices SET invoice_prefix = '$config_invoice_prefix', invoice_number = $new_invoice_number, invoice_scope = '$recurring_invoice_scope', invoice_date = CURDATE(), invoice_due = DATE_ADD(CURDATE(), INTERVAL $client_net_terms day), invoice_discount_amount = $recurring_invoice_discount_amount, invoice_amount = $recurring_invoice_amount, invoice_currency_code = '$recurring_invoice_currency_code', invoice_note = '$recurring_invoice_note', invoice_category_id = $category_id, invoice_status = 'Sent', invoice_url_key = '$url_key', invoice_recurring_invoice_id = $recurring_invoice_id, invoice_client_id = $client_id");
+ mysqli_query($mysqli, "INSERT INTO invoices SET invoice_prefix = '$config_invoice_prefix', invoice_number = $new_invoice_number, invoice_scope = '$recurring_invoice_scope', invoice_date = CURDATE(), invoice_due = DATE_ADD(CURDATE(), INTERVAL $client_net_terms day), invoice_discount_amount = $recurring_invoice_discount_amount, invoice_amount = $recurring_invoice_amount, invoice_currency_code = '$recurring_invoice_currency_code', invoice_note = '$recurring_invoice_note', invoice_category_id = $category_id, invoice_status = 'Draft', invoice_url_key = '$url_key', invoice_recurring_invoice_id = $recurring_invoice_id, invoice_client_id = $client_id");
$new_invoice_id = mysqli_insert_id($mysqli);
@@ -751,13 +752,17 @@ while ($row = mysqli_fetch_assoc($sql_recurring_invoices)) {
}
- mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Sent', history_description = 'Invoice Generated from Recurring!', history_invoice_id = $new_invoice_id");
+ mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Draft', history_description = 'Invoice Generated from Recurring!', history_invoice_id = $new_invoice_id");
- appNotify("Recurring Sent", "Recurring Invoice $config_invoice_prefix$new_invoice_number for $client_name Sent", "/agent/invoice.php?invoice_id=$new_invoice_id", $client_id);
+ if ($recurring_invoice_auto_send == 1) {
+ appNotify("Recurring Sent", "Recurring Invoice $config_invoice_prefix$new_invoice_number for $client_name Sent", "/agent/invoice.php?invoice_id=$new_invoice_id", $client_id);
+ } else {
+ appNotify("Recurring Generated", "Recurring Invoice $config_invoice_prefix$new_invoice_number for $client_name Generated", "/agent/invoice.php?invoice_id=$new_invoice_id", $client_id);
+ }
triggerCustomAction('invoice_create', $new_invoice_id);
- //Update recurring dates
+ // Update recurring dates
mysqli_query($mysqli, "UPDATE recurring_invoices SET recurring_invoice_last_sent = CURDATE(), recurring_invoice_next_date = DATE_ADD(CURDATE(), INTERVAL 1 $recurring_invoice_frequency) WHERE recurring_invoice_id = $recurring_invoice_id");
@@ -783,7 +788,8 @@ while ($row = mysqli_fetch_assoc($sql_recurring_invoices)) {
$contact_name = escapeSql($row['contact_name']);
$contact_email = escapeSql($row['contact_email']);
- if ($config_recurring_auto_send_invoice == 1 && $recurring_invoice_email_notify == 1) {
+ // Send invoice email if: (1) Global recurring auto-send is on, (2) the recurring invoice is set to auto-send (than just be a draft), and (3) the recurring invoice is opted in for the client to be notified
+ if ($config_recurring_auto_send_invoice == 1 && $recurring_invoice_auto_send == 1 && $recurring_invoice_email_notify == 1) {
$subject = "Invoice $invoice_prefix$invoice_number";
$body = "Hello $contact_name,
An invoice regarding \"$invoice_scope\" has been generated. Please view the details below.
Invoice: $invoice_prefix$invoice_number
Issue Date: $invoice_date
Total: " . numfmt_format_currency($currency_format, $invoice_amount, $recurring_invoice_currency_code) . "
Due Date: $invoice_due
To view your invoice, please click
here.
--
$company_name - Billing
$config_invoice_from_email
$company_phone";
@@ -851,6 +857,17 @@ while ($row = mysqli_fetch_assoc($sql_invalid_recurring_invoices)) {
}
// End Flag any active recurring "next run" dates that are in the past
+// Start Notify draft invoices needing review
+$sql_invoices_pending_send = mysqli_query($mysqli,"SELECT invoice_id FROM invoices WHERE invoice_status = 'Draft'");
+$invoices_pending_send = mysqli_num_rows($sql_invoices_pending_send);
+
+if ($invoices_pending_send > 0) {
+
+ appNotify("Draft Invoices", "There are $invoices_pending_send draft invoices pending review", "/agent/invoices.php?&status=Draft");
+}
+// End Notify draft invoices needing review
+
+
// Start Recurring Payments
$sql_recurring_payments = mysqli_query($mysqli, "
diff --git a/db.sql b/db.sql
index 580621a9f..0186e7561 100644
--- a/db.sql
+++ b/db.sql
@@ -2002,6 +2002,7 @@ CREATE TABLE `recurring_invoices` (
`recurring_invoice_amount` decimal(15,2) NOT NULL DEFAULT 0.00,
`recurring_invoice_currency_code` varchar(200) NOT NULL,
`recurring_invoice_note` text DEFAULT NULL,
+ `recurring_invoice_auto_send` tinyint(1) NOT NULL DEFAULT 1,
`recurring_invoice_email_notify` tinyint(1) NOT NULL DEFAULT 1,
`recurring_invoice_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`recurring_invoice_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),