auditionadmin/app/Actions/Tabulation/EnterNoShow.php

66 lines
2.6 KiB
PHP

<?php
namespace App\Actions\Tabulation;
use App\Exceptions\AuditionAdminException;
use App\Models\BonusScore;
use App\Models\Entry;
use App\Models\EntryTotalScore;
use App\Models\ScoreSheet;
use Illuminate\Support\Facades\DB;
use function auditionLog;
class EnterNoShow
{
/**
* Handles the no-show or failed-prelim flagging for a given entry.
*
* This method ensures the specified flag type is valid and validates
* that the action can be performed based on the associated audition's state.
* Deletes related score records and applies the specified flag ('no_show'
* or 'failed_prelim') to the entry, returning a success message.
*
* @param Entry $entry The entry being flagged.
* @param string $flagType The type of flag to apply ('no-show' or 'failed-prelim').
* @return string A confirmation message about the flagging operation.
*
* @throws AuditionAdminException If an invalid flag type is provided,
* or the action violates business rules.
*/
public function __invoke(Entry $entry, string $flagType = 'noshow'): string
{
if ($flagType !== 'noshow' && $flagType !== 'failprelim') {
throw new AuditionAdminException('Invalid flag type');
}
if ($entry->audition->hasFlag('seats_published')) {
throw new AuditionAdminException('Cannot enter a no-show for an entry in an audition where seats are published');
}
if ($entry->audition->hasFlag('advancement_published')) {
throw new AuditionAdminException('Cannot enter a no-show for an entry in an audition where advancement is published');
}
DB::table('score_sheets')->where('entry_id', $entry->id)->delete();
ScoreSheet::where('entry_id', $entry->id)->delete();
BonusScore::where('entry_id', $entry->id)->delete();
EntryTotalScore::where('entry_id', $entry->id)->delete();
if ($flagType == 'failprelim') {
$msg = 'Failed prelim has been entered for '.$entry->audition->name.' #'.$entry->draw_number.' (ID: '.$entry->id.').';
$entry->addFlag('failed_prelim');
} else {
$entry->addFlag('no_show');
$msg = 'No Show has been entered for '.$entry->audition->name.' #'.$entry->draw_number.' (ID: '.$entry->id.').';
}
$affected = [
['entries', [$entry->id]],
['auditions', [$entry->audition_id]],
['students', [$entry->student_id]],
];
auditionLog($msg, $affected);
return $msg;
}
}