diff --git a/cron/cron.php b/cron/cron.php
index 1d51e39e..7a76b333 100644
--- a/cron/cron.php
+++ b/cron/cron.php
@@ -524,12 +524,23 @@ if ($config_send_invoice_reminders == 1) {
$contact_name = sanitizeInput($row['contact_name']);
$contact_email = sanitizeInput($row['contact_email']);
- // Late Charges
+ // Sum payments already applied, derive the real balance owed
+ $sql_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments WHERE payment_invoice_id = $invoice_id");
+ $paid_row = mysqli_fetch_assoc($sql_paid);
+ $amount_paid = floatval($paid_row['amount_paid']);
+ $invoice_balance = $invoice_amount - $amount_paid;
+
+ // Nothing actually owed (e.g. paid in full but status lagging) - skip
+ if ($invoice_balance <= 0) {
+ continue;
+ }
+
+ // Late Charges
if ($config_invoice_late_fee_enable == 1 && $day > 1) {
$todays_date = date('Y-m-d');
- $late_fee_amount = ($invoice_amount * $config_invoice_late_fee_percent) / 100;
+ $late_fee_amount = ($invoice_balance * $config_invoice_late_fee_percent) / 100;
$new_invoice_amount = $invoice_amount + $late_fee_amount;
mysqli_query($mysqli, "UPDATE invoices SET invoice_amount = $new_invoice_amount WHERE invoice_id = $invoice_id");
@@ -541,14 +552,22 @@ if ($config_send_invoice_reminders == 1) {
appNotify("Invoice Late Charge", "Invoice $invoice_prefix$invoice_number for $client_name in the amount of $invoice_amount was charged a late fee of $late_fee_amount", "/agent/invoice.php?invoice_id=$invoice_id", $client_id);
+ // Roll the fee into the balance and total we report below
+ $invoice_amount = $new_invoice_amount;
+ $invoice_balance = $invoice_balance + $late_fee_amount;
+
}
- appNotify("Invoice Overdue", "Invoice $invoice_prefix$invoice_number for $client_name in the amount of $invoice_amount is overdue by $day days", "/agent/invoice.php?invoice_id=$invoice_id", $client_id);
+ appNotify("Invoice Overdue", "Invoice $invoice_prefix$invoice_number for $client_name with a balance of " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . " is overdue by $day days", "/agent/invoice.php?invoice_id=$invoice_id", $client_id);
$subject = "Overdue Invoice $invoice_prefix$invoice_number";
- $body = "Hello $contact_name,
Our records indicate that we have not yet received payment for the invoice $invoice_prefix$invoice_number. We kindly request that you submit your payment as soon as possible. If you have any questions or concerns, please do not hesitate to contact us at $company_email or $company_phone.
+
+ // Only show the paid line if a payment has actually been applied
+ $paid_line = $amount_paid > 0 ? "Amount Paid: " . numfmt_format_currency($currency_format, $amount_paid, $invoice_currency_code) . "
" : "";
+
+ $body = "Hello $contact_name,
Our records indicate that we have not yet received payment in full for the invoice $invoice_prefix$invoice_number. We kindly request that you submit your payment as soon as possible. If you have any questions or concerns, please do not hesitate to contact us at $company_email or $company_phone.
- Kindly review the invoice details mentioned below.
Invoice: $invoice_prefix$invoice_number
Issue Date: $invoice_date
Total: " . numfmt_format_currency($currency_format, $invoice_amount, $invoice_currency_code) . "
Due Date: $invoice_due
Over Due By: $day Days
To view your invoice, please click here.
--
$company_name - Billing
$config_invoice_from_email
$company_phone";
+ Kindly review the invoice details mentioned below.
Invoice: $invoice_prefix$invoice_number
Issue Date: $invoice_date
Invoice Total: " . numfmt_format_currency($currency_format, $invoice_amount, $invoice_currency_code) . "
$paid_line" . "Balance Due: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "
Due Date: $invoice_due
Over Due By: $day Days
To view your invoice, please click here.
--
$company_name - Billing
$config_invoice_from_email
$company_phone";
$mail = addToMailQueue([
[