c470240dc0
Made it a command so it runs with the scheduler
55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Carbon\Carbon;
|
|
use App\Models\Invoice;
|
|
|
|
class CheckInvoiceDueDatesCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'invoices:check-due';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Check invoices for due dates (runs synchronously)';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
$this->info('Check invoice due dates');
|
|
Log::info('Check invoice due dates');
|
|
|
|
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']);
|
|
$this->info("Updated invoice {$invoice->nr} to 'due' status");
|
|
}
|
|
|
|
$this->info("Checked {$invoices->count()} invoices for due dates.");
|
|
} catch (\Exception $e) {
|
|
$this->error("Error in CheckInvoiceDueDatesJob: " . $e->getMessage());
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|