Add backend check for due invoices, #54
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use App\Models\Invoice;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CheckInvoiceDueDatesJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
try {
|
||||
$today = Carbon::today();
|
||||
|
||||
// Find invoices with payment_status 'issued' and due_date <= today
|
||||
$invoices = Invoice::where('payment_status', 'issued')
|
||||
->whereDate('due_date', '<=', $today)
|
||||
->get();
|
||||
|
||||
foreach ($invoices as $invoice) {
|
||||
$invoice->update(['payment_status' => 'due']);
|
||||
Log::info("Updated invoice {$invoice->nr} to 'due' status");
|
||||
}
|
||||
|
||||
Log::info("Checked {$invoices->count()} invoices for due dates.");
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error in CheckInvoiceDueDatesJob: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Jobs\CheckInvoiceDueDatesJob;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CheckInvoiceDueDatesListener
|
||||
{
|
||||
public function handle($event)
|
||||
{
|
||||
try {
|
||||
$today = now()->toDateString();
|
||||
$cacheKey = 'invoice_due_dates_checked_' . $today;
|
||||
|
||||
if (!Cache::has($cacheKey)) {
|
||||
// Cache a timestamped key, so we only check once a day
|
||||
Cache::put($cacheKey, true, now()->endOfDay());
|
||||
Log::info("Starting daily job to check for due invoices.");
|
||||
(new CheckInvoiceDueDatesJob())->handle();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error in CheckInvoiceDueDatesListener: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected $listen = [
|
||||
'Illuminate\Routing\Events\RouteMatched' => [
|
||||
\App\Listeners\CheckInvoiceDueDatesListener::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user