Delete calculated scores on score sheet changes, deletions, and additions.
Addresses #68
This commit is contained in:
parent
f8d778dbcc
commit
e93ae750e4
|
|
@ -121,6 +121,11 @@ class Entry extends Model
|
|||
return $this->hasOne(Seat::class);
|
||||
}
|
||||
|
||||
public function calculatedScores(): HasMany
|
||||
{
|
||||
return $this->hasMany(CalculatedScore::class);
|
||||
}
|
||||
|
||||
public function scopeForSeating(Builder $query): void
|
||||
{
|
||||
$query->where('for_seating', 1);
|
||||
|
|
|
|||
|
|
@ -16,6 +16,22 @@ class ScoreSheet extends Model
|
|||
|
||||
protected $casts = ['subscores' => 'json'];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
static::created(function ($scoreSheet) {
|
||||
$scoreSheet->deleteRelatedCalculatedScores();
|
||||
});
|
||||
|
||||
static::updated(function ($scoreSheet) {
|
||||
$scoreSheet->deleteRelatedCalculatedScores();
|
||||
});
|
||||
|
||||
static::deleted(function ($scoreSheet) {
|
||||
$scoreSheet->deleteRelatedCalculatedScores();
|
||||
});
|
||||
}
|
||||
|
||||
public function entry(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Entry::class);
|
||||
|
|
@ -37,9 +53,18 @@ class ScoreSheet extends Model
|
|||
'audition_id' // Local key on the intermediate model (Entry)
|
||||
);
|
||||
}
|
||||
|
||||
public function getSubscore($id)
|
||||
{
|
||||
return $this->subscores[$id]['score'] ?? false;
|
||||
// this function is used at resources/views/tabulation/entry_score_sheet.blade.php
|
||||
}
|
||||
|
||||
public function deleteRelatedCalculatedScores(): void
|
||||
{
|
||||
$entry = $this->entry;
|
||||
if ($entry) {
|
||||
$entry->calculatedScores()->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue