Add backend check for due invoices, #54

This commit is contained in:
2025-11-11 21:20:15 +01:00
parent b81c6d71f4
commit c682d0caf6
15 changed files with 112 additions and 23 deletions
+41
View File
@@ -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());
}
}
}
+22
View File
@@ -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();
}
}
+1
View File
@@ -2,5 +2,6 @@
return [
App\Providers\AppServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\FortifyServiceProvider::class,
];
+20 -19
View File
@@ -62,6 +62,7 @@
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
// 'stack' => [
// 'driver' => 'stack',
// 'channels' => explode(',', env('LOG_STACK', 'single')),
@@ -84,26 +85,26 @@
'replace_placeholders' => true,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => env('LOG_SLACK_USERNAME', 'Laravel Log'),
'emoji' => env('LOG_SLACK_EMOJI', ':boom:'),
'level' => env('LOG_LEVEL', 'critical'),
'replace_placeholders' => true,
],
// 'slack' => [
// 'driver' => 'slack',
// 'url' => env('LOG_SLACK_WEBHOOK_URL'),
// 'username' => env('LOG_SLACK_USERNAME', 'Laravel Log'),
// 'emoji' => env('LOG_SLACK_EMOJI', ':boom:'),
// 'level' => env('LOG_LEVEL', 'critical'),
// 'replace_placeholders' => true,
// ],
'papertrail' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class),
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
'connectionString' => 'tls://' . env('PAPERTRAIL_URL') . ':' . env('PAPERTRAIL_PORT'),
],
'processors' => [PsrLogMessageProcessor::class],
],
// 'papertrail' => [
// 'driver' => 'monolog',
// 'level' => env('LOG_LEVEL', 'debug'),
// 'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class),
// 'handler_with' => [
// 'host' => env('PAPERTRAIL_URL'),
// 'port' => env('PAPERTRAIL_PORT'),
// 'connectionString' => 'tls://' . env('PAPERTRAIL_URL') . ':' . env('PAPERTRAIL_PORT'),
// ],
// 'processors' => [PsrLogMessageProcessor::class],
// ],
'stderr' => [
'driver' => 'monolog',
+1 -4
View File
@@ -3,10 +3,7 @@
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use App\Http\Controllers\InvoiceController;
Route::get('/welcome', function () {
return Inertia::render('Welcome');
})->name('welcome');
use Illuminate\Support\Facades\Log;
Route::get('/', function () {
return Inertia::render('Dashboard');
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
View File
View File
View File
View File
View File