GenreService, ArtistService, ArtistEngagementService (state machine), StageService, StageDayService, PerformanceService, LaneCascadeService under app/Services/Artist/. Plain final classes with constructor injection — matches FormSubmissionService convention. ArtistEngagementService implements the RFC §10.1 booking_status state machine: terminal Cancelled/Rejected/Declined, Option requires future option_expires_at, Contracted requires fee_amount > 0. transitionStatus is the focused entry point; update() routes through it whenever the payload mutates booking_status. cancel() composes transitionStatus + soft delete in one transaction so the existing ArtistEngagementObserver cascade fires. LaneCascadeService is the D18 transactional move algorithm. Locks the dragged Performance row FOR UPDATE, validates client version against the persisted version (D14), then either parks (stage_id=null, no cascade) or places onto (stage, event, lane) with single-level cascade-bump of any time-overlapping rows on the target lane. Returns a MoveResult value object carrying the moved + cascaded performances so the controller maps them to API resources without a second query. StageDayService implements the §10.5 atomic matrix replace. Detects non-cancelled performances on event_ids about to be removed; throws StageDaysOrphanedPerformancesException unless force_orphan=true. The orphans are not deleted — they persist with the same stage_id so they re-appear when the day re-activates (D5/D27 retention). ArtistService.create raises DuplicateArtistException carrying the existing master so the controller can offer a "use existing" choice instead of forcing the booker to abandon their dialog. ArtistEngagement defaults buma_handled_by based on artist.agent_company.handles_buma per RFC D26. GenreService.delete is hard-blocked (GenreInUseException) when artists still reference the genre via default_genre_id; the frontend rebinds those artists first. StageService.delete cascade-parks performances (stage_id → null, lane preserved) and returns the parked count for the activity-log entry the controller writes in Step 9. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
215 lines
7.5 KiB
PHP
215 lines
7.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Artist;
|
|
|
|
use App\Enums\Artist\ArtistEngagementStatus;
|
|
use App\Enums\Artist\BumaHandledBy;
|
|
use App\Exceptions\Artist\InvalidStatusTransitionException;
|
|
use App\Models\Artist;
|
|
use App\Models\ArtistEngagement;
|
|
use App\Models\Event;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Heart of the artist booking domain. Owns the booking_status state
|
|
* machine, the deal-info defaults, and the cancellation cascade hook
|
|
* that the ArtistEngagementObserver relies on (the observer turns a
|
|
* soft-delete into a performances + advance_sections cascade; this
|
|
* service composes cancel + delete in one transaction).
|
|
*/
|
|
final class ArtistEngagementService
|
|
{
|
|
/**
|
|
* Terminal statuses — no transitions out except an explicit
|
|
* `allowReopen` flag (currently unused by callers, reserved for
|
|
* superadmin tooling).
|
|
*/
|
|
private const TERMINAL = [
|
|
ArtistEngagementStatus::Cancelled,
|
|
ArtistEngagementStatus::Rejected,
|
|
ArtistEngagementStatus::Declined,
|
|
];
|
|
|
|
/**
|
|
* @param array<string, mixed> $attributes
|
|
*/
|
|
public function create(Event $event, Artist $artist, array $attributes): ArtistEngagement
|
|
{
|
|
return DB::transaction(function () use ($event, $artist, $attributes): ArtistEngagement {
|
|
$engagement = new ArtistEngagement($attributes);
|
|
$engagement->artist_id = $artist->id;
|
|
$engagement->event_id = $event->id;
|
|
|
|
// D26 — Buma auto-flip. If artist's agent_company handles
|
|
// BUMA reporting itself, default the engagement to
|
|
// BookingAgency; otherwise default to Organisation. Caller
|
|
// may override via $attributes.
|
|
if (! array_key_exists('buma_handled_by', $attributes)) {
|
|
$artist->loadMissing('agentCompany');
|
|
$engagement->buma_handled_by = ($artist->agentCompany?->handles_buma === true)
|
|
? BumaHandledBy::BookingAgency
|
|
: BumaHandledBy::Organisation;
|
|
}
|
|
|
|
$status = $this->coerceStatus($engagement->booking_status ?? ArtistEngagementStatus::Draft);
|
|
$this->validateInitialStatus($status, $engagement);
|
|
$engagement->booking_status = $status;
|
|
|
|
$engagement->save();
|
|
|
|
return $engagement->refresh();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Partial update. Status changes route through transitionStatus so
|
|
* the state machine fires on every status change regardless of the
|
|
* other fields in the payload.
|
|
*
|
|
* @param array<string, mixed> $attributes
|
|
*/
|
|
public function update(ArtistEngagement $engagement, array $attributes): ArtistEngagement
|
|
{
|
|
return DB::transaction(function () use ($engagement, $attributes): ArtistEngagement {
|
|
unset($attributes['organisation_id'], $attributes['artist_id'], $attributes['event_id']);
|
|
|
|
if (array_key_exists('booking_status', $attributes)) {
|
|
$newStatus = $this->coerceStatus($attributes['booking_status']);
|
|
unset($attributes['booking_status']);
|
|
$engagement->fill($attributes);
|
|
$this->transitionStatus($engagement, $newStatus, $attributes);
|
|
$engagement->save();
|
|
} else {
|
|
$engagement->fill($attributes);
|
|
$engagement->save();
|
|
}
|
|
|
|
return $engagement->refresh();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Atomic transition with full state-machine validation. Throws
|
|
* InvalidStatusTransitionException on illegal moves.
|
|
*
|
|
* @param array<string, mixed> $contextAttributes fields already filled on $engagement
|
|
*/
|
|
public function transitionStatus(
|
|
ArtistEngagement $engagement,
|
|
ArtistEngagementStatus $to,
|
|
array $contextAttributes = [],
|
|
): ArtistEngagement {
|
|
$from = $this->coerceStatus($engagement->booking_status);
|
|
|
|
$this->validateTransition($from, $to, $engagement);
|
|
|
|
$engagement->booking_status = $to;
|
|
$engagement->save();
|
|
|
|
return $engagement;
|
|
}
|
|
|
|
/**
|
|
* Cancel = transition to Cancelled, then soft-delete. Observer
|
|
* cascades to performances + advance_sections per RFC §5.4.
|
|
*/
|
|
public function cancel(ArtistEngagement $engagement): ArtistEngagement
|
|
{
|
|
return DB::transaction(function () use ($engagement): ArtistEngagement {
|
|
$from = $this->coerceStatus($engagement->booking_status);
|
|
if ($from !== ArtistEngagementStatus::Cancelled) {
|
|
$this->transitionStatus($engagement, ArtistEngagementStatus::Cancelled);
|
|
}
|
|
$engagement->delete();
|
|
|
|
return $engagement->refresh();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Soft delete only. Observer cascades to children. Use cancel() if
|
|
* the booking_status should also flip — the API destroy endpoint
|
|
* routes here directly to keep status untouched (§6.2 — DELETE
|
|
* /engagements/{id} is "remove the booking", semantically distinct
|
|
* from "cancel").
|
|
*/
|
|
public function softDelete(ArtistEngagement $engagement): void
|
|
{
|
|
$engagement->delete();
|
|
}
|
|
|
|
private function validateInitialStatus(ArtistEngagementStatus $status, ArtistEngagement $engagement): void
|
|
{
|
|
if ($status === ArtistEngagementStatus::Option) {
|
|
$expires = $engagement->option_expires_at;
|
|
if ($expires === null || $expires->isPast()) {
|
|
throw new InvalidStatusTransitionException(
|
|
ArtistEngagementStatus::Draft,
|
|
$status,
|
|
'Option requires option_expires_at in the future.',
|
|
);
|
|
}
|
|
}
|
|
|
|
if ($status === ArtistEngagementStatus::Contracted) {
|
|
$fee = $engagement->fee_amount;
|
|
if ($fee === null || (float) $fee <= 0) {
|
|
throw new InvalidStatusTransitionException(
|
|
ArtistEngagementStatus::Draft,
|
|
$status,
|
|
'Contracted requires fee_amount.',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function validateTransition(
|
|
ArtistEngagementStatus $from,
|
|
ArtistEngagementStatus $to,
|
|
ArtistEngagement $engagement,
|
|
): void {
|
|
if ($from === $to) {
|
|
return;
|
|
}
|
|
|
|
if (in_array($from, self::TERMINAL, true)) {
|
|
throw new InvalidStatusTransitionException(
|
|
$from,
|
|
$to,
|
|
sprintf('%s is terminal — re-booking creates a new engagement.', $from->value),
|
|
);
|
|
}
|
|
|
|
if ($to === ArtistEngagementStatus::Option) {
|
|
$expires = $engagement->option_expires_at;
|
|
if ($expires === null || $expires->isPast()) {
|
|
throw new InvalidStatusTransitionException(
|
|
$from,
|
|
$to,
|
|
'Option requires option_expires_at in the future.',
|
|
);
|
|
}
|
|
}
|
|
|
|
if ($to === ArtistEngagementStatus::Contracted) {
|
|
$fee = $engagement->fee_amount;
|
|
if ($fee === null || (float) $fee <= 0) {
|
|
throw new InvalidStatusTransitionException(
|
|
$from,
|
|
$to,
|
|
'Contracted requires fee_amount.',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function coerceStatus(ArtistEngagementStatus|string $value): ArtistEngagementStatus
|
|
{
|
|
return $value instanceof ArtistEngagementStatus
|
|
? $value
|
|
: ArtistEngagementStatus::from($value);
|
|
}
|
|
}
|