mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2026-03-03 03:07:01 +00:00
Fixes for no late invoice notifications
This commit is contained in:
@@ -37,6 +37,7 @@ use App\Jobs\EDocument\EInvoicePullDocs;
|
||||
use App\Jobs\Cron\InvoiceTaxSummary;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use App\Jobs\Invoice\InvoiceCheckLateWebhook;
|
||||
use App\Jobs\Invoice\InvoiceCheckOverdue;
|
||||
use App\Jobs\Subscription\CleanStaleInvoiceOrder;
|
||||
use App\PaymentDrivers\Rotessa\Jobs\TransactionReport;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
@@ -130,7 +131,10 @@ class Kernel extends ConsoleKernel
|
||||
$schedule->job(new AutoBillCron())->dailyAt('06:20')->withoutOverlapping()->name('auto-bill-job')->onOneServer();
|
||||
|
||||
/* Fires webhooks for overdue Invoice */
|
||||
$schedule->job(new InvoiceCheckLateWebhook())->dailyAt('07:00')->withoutOverlapping()->name('invoice-overdue-job')->onOneServer();
|
||||
$schedule->job(new InvoiceCheckLateWebhook())->dailyAt('07:00')->withoutOverlapping()->name('invoice-overdue-webhook-job')->onOneServer();
|
||||
|
||||
/* Fires notifications for overdue Invoice (respects company timezone) */
|
||||
$schedule->job(new InvoiceCheckOverdue())->hourly()->withoutOverlapping()->name('invoice-overdue-notification-job')->onOneServer();
|
||||
|
||||
/* Pulls in bank transactions from third party services */
|
||||
$schedule->job(new BankTransactionSync())->twiceDaily(1, 13)->withoutOverlapping()->name('bank-trans-sync-job')->onOneServer();
|
||||
|
||||
191
app/Jobs/Invoice/InvoiceCheckOverdue.php
Normal file
191
app/Jobs/Invoice/InvoiceCheckOverdue.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2025. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Jobs\Invoice;
|
||||
|
||||
use App\Jobs\Mail\NinjaMailer;
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\Admin\InvoiceOverdueObject;
|
||||
use App\Models\Company;
|
||||
use App\Models\Invoice;
|
||||
use App\Utils\Traits\Notifications\UserNotifies;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Carbon;
|
||||
use App\Utils\Ninja;
|
||||
|
||||
class InvoiceCheckOverdue implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
use SerializesModels;
|
||||
use UserNotifies;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (! config('ninja.db.multi_db_enabled')) {
|
||||
$this->processOverdueInvoices();
|
||||
} else {
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
MultiDB::setDB($db);
|
||||
$this->processOverdueInvoices();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process overdue invoices for the current database connection.
|
||||
* We check each company's timezone to ensure the invoice is truly overdue
|
||||
* based on the company's local time.
|
||||
*/
|
||||
private function processOverdueInvoices(): void
|
||||
{
|
||||
// Get all companies that are not disabled
|
||||
Company::query()
|
||||
->where('is_disabled', false)
|
||||
->when(Ninja::isHosted(), function ($query) {
|
||||
$query->whereHas('account', function ($q) {
|
||||
$q->where('is_flagged', false)
|
||||
->whereIn('plan', ['enterprise', 'pro'])
|
||||
->where('plan_expires', '>', now()->subHours(12));
|
||||
});
|
||||
})
|
||||
->cursor()
|
||||
->each(function (Company $company) {
|
||||
$this->checkCompanyOverdueInvoices($company);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for overdue invoices for a specific company,
|
||||
* using the company's timezone to determine if the invoice is overdue.
|
||||
*
|
||||
* Two scenarios trigger an overdue notification:
|
||||
* 1. partial > 0 && partial_due_date was yesterday (partial payment is overdue)
|
||||
* 2. partial == 0 && balance > 0 && due_date was yesterday (full invoice is overdue)
|
||||
*
|
||||
* To prevent duplicate notifications when running hourly, we only process
|
||||
* a company when it's currently between midnight and 1am in their timezone.
|
||||
* This ensures each company is only checked once per day.
|
||||
*/
|
||||
private function checkCompanyOverdueInvoices(Company $company): void
|
||||
{
|
||||
// Get the company's timezone
|
||||
$timezone = $company->timezone();
|
||||
$timezone_name = $timezone ? $timezone->name : 'UTC';
|
||||
|
||||
// Get the current hour in the company's timezone
|
||||
$now_in_company_tz = Carbon::now($timezone_name);
|
||||
|
||||
// Only process this company if it's currently between midnight and 1am in their timezone
|
||||
// This prevents duplicate notifications when running hourly across all timezones
|
||||
if ($now_in_company_tz->hour !== 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate the date range for "just became overdue" in the company's timezone
|
||||
// We check for invoices whose due date was yesterday in the company's timezone
|
||||
$yesterday_start = $now_in_company_tz->copy()->subDay()->startOfDay()->format('Y-m-d');
|
||||
$yesterday_end = $now_in_company_tz->copy()->startOfDay()->subSecond()->format('Y-m-d');
|
||||
|
||||
Invoice::query()
|
||||
->where('company_id', $company->id)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->where('is_deleted', false)
|
||||
->whereNull('deleted_at')
|
||||
->where('balance', '>', 0)
|
||||
->whereHas('client', function ($query) {
|
||||
$query->where('is_deleted', 0)
|
||||
->whereNull('deleted_at');
|
||||
})
|
||||
// Check for overdue conditions based on partial or full invoice
|
||||
->where(function ($query) use ($yesterday_start, $yesterday_end) {
|
||||
// Case 1: Partial payment is overdue (partial > 0 and partial_due_date was yesterday)
|
||||
$query->where(function ($q) use ($yesterday_start, $yesterday_end) {
|
||||
$q->where('partial', '>', 0)
|
||||
->whereNotNull('partial_due_date')
|
||||
->whereBetween('partial_due_date', [$yesterday_start, $yesterday_end]);
|
||||
})
|
||||
// Case 2: Full invoice is overdue (partial == 0 and due_date was yesterday)
|
||||
->orWhere(function ($q) use ($yesterday_start, $yesterday_end) {
|
||||
$q->where(function ($subq) {
|
||||
$subq->where('partial', '=', 0)
|
||||
->orWhereNull('partial');
|
||||
})
|
||||
->whereNotNull('due_date')
|
||||
->whereBetween('due_date', [$yesterday_start, $yesterday_end]);
|
||||
});
|
||||
})
|
||||
->cursor()
|
||||
->each(function ($invoice) {
|
||||
$this->notifyOverdueInvoice($invoice);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send notifications for an overdue invoice to all relevant company users.
|
||||
*/
|
||||
private function notifyOverdueInvoice(Invoice $invoice): void
|
||||
{
|
||||
$nmo = new NinjaMailerObject();
|
||||
$nmo->company = $invoice->company;
|
||||
$nmo->settings = $invoice->company->settings;
|
||||
|
||||
/* We loop through each user and determine whether they need to be notified */
|
||||
foreach ($invoice->company->company_users as $company_user) {
|
||||
/* The User */
|
||||
$user = $company_user->user;
|
||||
|
||||
if (! $user) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$nmo->mailable = new NinjaMailer((new InvoiceOverdueObject($invoice, $invoice->company, $company_user->portalType()))->build());
|
||||
|
||||
/* Returns an array of notification methods */
|
||||
$methods = $this->findUserNotificationTypes(
|
||||
$invoice->invitations()->first(),
|
||||
$company_user,
|
||||
'invoice',
|
||||
['all_notifications', 'invoice_late', 'invoice_late_all', 'invoice_late_user']
|
||||
);
|
||||
|
||||
/* If one of the methods is email then we fire the mailer */
|
||||
if (($key = array_search('mail', $methods)) !== false) {
|
||||
unset($methods[$key]);
|
||||
|
||||
$nmo->to_user = $user;
|
||||
|
||||
NinjaMailerJob::dispatch($nmo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
102
app/Mail/Admin/InvoiceOverdueObject.php
Normal file
102
app/Mail/Admin/InvoiceOverdueObject.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2025. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Mail\Admin;
|
||||
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Models\Company;
|
||||
use App\Models\Invoice;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Number;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use stdClass;
|
||||
|
||||
class InvoiceOverdueObject
|
||||
{
|
||||
public function __construct(public Invoice $invoice, public Company $company, public bool $use_react_url)
|
||||
{
|
||||
}
|
||||
|
||||
public function build()
|
||||
{
|
||||
MultiDB::setDb($this->company->db);
|
||||
|
||||
if (! $this->invoice) {
|
||||
return;
|
||||
}
|
||||
|
||||
App::forgetInstance('translator');
|
||||
/* Init a new copy of the translator */
|
||||
$t = app('translator');
|
||||
/* Set the locale */
|
||||
App::setLocale($this->company->getLocale());
|
||||
/* Set customized translations _NOW_ */
|
||||
$t->replace(Ninja::transformTranslations($this->company->settings));
|
||||
|
||||
$mail_obj = new stdClass();
|
||||
$mail_obj->amount = $this->getAmount();
|
||||
$mail_obj->subject = $this->getSubject();
|
||||
$mail_obj->data = $this->getData();
|
||||
$mail_obj->markdown = 'email.admin.generic';
|
||||
$mail_obj->tag = $this->company->company_key;
|
||||
$mail_obj->text_view = 'email.template.text';
|
||||
|
||||
return $mail_obj;
|
||||
}
|
||||
|
||||
private function getAmount()
|
||||
{
|
||||
return Number::formatMoney($this->invoice->balance, $this->invoice->client);
|
||||
}
|
||||
|
||||
private function getSubject()
|
||||
{
|
||||
return
|
||||
ctrans(
|
||||
'texts.notification_invoice_overdue_subject',
|
||||
[
|
||||
'client' => $this->invoice->client->present()->name(),
|
||||
'invoice' => $this->invoice->number,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function getData()
|
||||
{
|
||||
$settings = $this->invoice->client->getMergedSettings();
|
||||
$content = ctrans(
|
||||
'texts.notification_invoice_overdue',
|
||||
[
|
||||
'amount' => $this->getAmount(),
|
||||
'client' => $this->invoice->client->present()->name(),
|
||||
'invoice' => $this->invoice->number,
|
||||
]
|
||||
);
|
||||
|
||||
$data = [
|
||||
'title' => $this->getSubject(),
|
||||
'content' => $content,
|
||||
'url' => $this->invoice->invitations->first()->getAdminLink($this->use_react_url),
|
||||
'button' => $this->use_react_url ? ctrans('texts.view_invoice') : ctrans('texts.login'),
|
||||
'signature' => $settings->email_signature,
|
||||
'logo' => $this->company->present()->logo(),
|
||||
'settings' => $settings,
|
||||
'whitelabel' => $this->company->account->isPaid() ? true : false,
|
||||
'text_body' => $content,
|
||||
'template' => $this->company->account->isPremium() ? 'email.template.admin_premium' : 'email.template.admin',
|
||||
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4691,6 +4691,8 @@ $lang = array(
|
||||
'show_tasks_in_client_portal' => 'Show Tasks in Client Portal',
|
||||
'notification_quote_expired_subject' => 'Quote :invoice has expired for :client',
|
||||
'notification_quote_expired' => 'The following Quote :invoice for client :client and :amount has now expired.',
|
||||
'notification_invoice_overdue_subject' => 'Invoice :invoice is overdue for :client',
|
||||
'notification_invoice_overdue' => 'The following Invoice :invoice for client :client and :amount is now overdue.',
|
||||
'auto_sync' => 'Auto Sync',
|
||||
'refresh_accounts' => 'Refresh Accounts',
|
||||
'upgrade_to_connect_bank_account' => 'Upgrade to Enterprise to connect your bank account',
|
||||
|
||||
Reference in New Issue
Block a user