105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\ApprovalStatus;
|
|
use App\Enums\CooperationStatus;
|
|
use App\Models\Cooperation;
|
|
use App\Models\CooperationMedia;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class UpdateCooperationStatusCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'cooperation:update-status';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Manually trigger the update of cooperation statuses based on deadlines';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Updating cooperation statuses...');
|
|
|
|
$today = Carbon::today();
|
|
|
|
Log::info('Running UpdateCooperationStatusJob at '.now());
|
|
|
|
// 1. PENDING -> ASSIGNMENT (final_submission_date)
|
|
// Automatically reject media that haven't responded
|
|
$rejectedPendingMedia = CooperationMedia::whereHas('cooperation', function ($query) use ($today) {
|
|
$query->pending()->whereDate('final_submission_date', '<=', $today);
|
|
})
|
|
->pending()
|
|
->update(['status' => ApprovalStatus::REJECTED]);
|
|
|
|
if ($rejectedPendingMedia > 0) {
|
|
Log::info("Updated {$rejectedPendingMedia} media in PENDING cooperations to REJECTED because they didn't respond before final_submission_date.");
|
|
}
|
|
|
|
$updatedPending = Cooperation::pending()
|
|
->whereDate('final_submission_date', '<=', $today)
|
|
->update(['status' => CooperationStatus::ASSIGNMENT]);
|
|
|
|
if ($updatedPending > 0) {
|
|
Log::info("Updated {$updatedPending} cooperations from PENDING to ASSIGNMENT.");
|
|
}
|
|
|
|
// 2. ASSIGNMENT -> VERIFICATION (assignment_deadline)
|
|
// Automatically reject media that haven't responded
|
|
$rejectedMedia = CooperationMedia::whereHas('cooperation', function ($query) use ($today) {
|
|
$query->assignment()->whereDate('assignment_deadline', '<=', $today);
|
|
})
|
|
->pending()
|
|
->update(['status' => ApprovalStatus::REJECTED]);
|
|
|
|
if ($rejectedMedia > 0) {
|
|
Log::info("Updated {$rejectedMedia} media assignments to REJECTED because they didn't respond before the deadline.");
|
|
}
|
|
|
|
$updatedAssignment = Cooperation::assignment()
|
|
->whereDate('assignment_deadline', '<=', $today)
|
|
->update(['status' => CooperationStatus::VERIFICATION]);
|
|
|
|
if ($updatedAssignment > 0) {
|
|
Log::info("Updated {$updatedAssignment} cooperations from ASSIGNMENT to VERIFICATION.");
|
|
}
|
|
|
|
// 3. VERIFICATION -> PAYMENT (verification_deadline)
|
|
// Automatically reject media that haven't responded
|
|
$rejectedVerificationMedia = CooperationMedia::whereHas('cooperation', function ($query) use ($today) {
|
|
$query->verification()->whereDate('verification_deadline', '<=', $today);
|
|
})
|
|
->pending()
|
|
->update(['status' => ApprovalStatus::REJECTED]);
|
|
|
|
if ($rejectedVerificationMedia > 0) {
|
|
Log::info("Updated {$rejectedVerificationMedia} media in VERIFICATION cooperations to REJECTED because they didn't respond before verification_deadline.");
|
|
}
|
|
|
|
$updatedVerification = Cooperation::verification()
|
|
->whereDate('verification_deadline', '<=', $today)
|
|
->update(['status' => CooperationStatus::PAYMENT]);
|
|
|
|
if ($updatedVerification > 0) {
|
|
Log::info("Updated {$updatedVerification} cooperations from VERIFICATION to PAYMENT.");
|
|
}
|
|
|
|
$this->info('Cooperation statuses updated successfully.');
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|