recompute($section); } public function updated(AdvanceSection $section): void { if (! $section->wasChanged('submission_status')) { return; } $this->recompute($section); } public function deleted(AdvanceSection $section): void { $this->recompute($section); } private function recompute(AdvanceSection $section): void { $engagementId = $section->engagement_id; DB::transaction(function () use ($engagementId): void { $engagement = ArtistEngagement::query() ->withoutGlobalScope(OrganisationScope::class) ->whereKey($engagementId) ->lockForUpdate() ->first(); if ($engagement === null) { return; } $rows = AdvanceSection::query() ->withoutGlobalScope(OrganisationScope::class) ->where('engagement_id', $engagementId) ->lockForUpdate() ->get(['submission_status']); $total = $rows->count(); $completed = $rows ->where('submission_status', AdvanceSectionSubmissionStatus::Approved) ->count(); $engagement->disableLogging(); $engagement->update([ 'advancing_completed_count' => $completed, 'advancing_total_count' => $total, ]); }); } }