A parent registers once and manages lessons for one or more children, who need no login of their own. A child is a real wp_users row with the student role but no usable login — so student_id keeps meaning "a WordPress user" on every table, and booking, credits, policies and enrolments work unchanged. A us_guardians link table maps guardian to child. The signup form gains a parent/guardian tick that reveals a block per child, with the account-signup questions asked per child rather than per guardian — they describe the student, not the account holder. Signup policies are recorded once per child with the guardian as the acceptor, which is the record that actually means something. A family that half-creates is rolled back entirely rather than leaving a guardian who cannot re-register. The booking and enrolment forms gain a "Who is this for?" picker listing children first, so the default selection is never the parent — booking for the wrong child is correctable, quietly billing a parent for their kid's lesson is not. POST /bookings and POST /enrollments take an optional student_id honoured only for that child's guardian; anything else is a 403. That check is the authorisation boundary of the feature. Payments and credits gain a payer: the charge names the child it was for and the guardian who owes it, so per-child reporting is unchanged while notices, receipts and the payment step reach the parent. Credit is held by the payer, so one child's cancellation can settle a sibling's charge, and the daily billing scan sends a guardian one notice covering every child. Closes #132 Co-Authored-By: Claude Opus 5 <[email protected]>
97 lines
3.2 KiB
PHP
97 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Payment;
|
|
|
|
use Unsupervised\Schedular\Val;
|
|
|
|
/**
|
|
* A studio credit held on a student's account — money already paid for a lesson
|
|
* that was later cancelled. Credits are consumed against future scheduled-billing
|
|
* charges (weekly / monthly) before the student is asked to pay, oldest first.
|
|
*/
|
|
class Credit {
|
|
|
|
public const STATUS_AVAILABLE = 'available';
|
|
public const STATUS_CONSUMED = 'consumed';
|
|
|
|
/**
|
|
* All valid credit statuses.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
public const VALID_STATUSES = [ self::STATUS_AVAILABLE, self::STATUS_CONSUMED ];
|
|
|
|
public function __construct(
|
|
public readonly int $studentId,
|
|
public readonly float $amount,
|
|
public readonly float $remaining,
|
|
/**
|
|
* The account holding this balance — a child's guardian, or 0 meaning
|
|
* "the student themselves". A family's credits all sit on the guardian,
|
|
* so one child's cancellation can settle a sibling's charge.
|
|
*/
|
|
public readonly int $payerId = 0,
|
|
public readonly string $currency = 'CAD',
|
|
public readonly ?int $sourcePaymentId = null,
|
|
public readonly ?int $sourceLessonId = null,
|
|
public readonly ?string $reason = null,
|
|
public readonly string $status = self::STATUS_AVAILABLE,
|
|
public readonly ?string $createdAt = null,
|
|
public readonly ?string $updatedAt = null,
|
|
public readonly ?int $id = null,
|
|
) {}
|
|
|
|
public static function fromRow( \stdClass $row ): self {
|
|
return new self(
|
|
studentId: Val::int( $row->student_id ),
|
|
amount: Val::float( $row->amount ),
|
|
remaining: Val::float( $row->remaining ),
|
|
payerId: Val::int( $row->payer_id ?? 0 ),
|
|
currency: Val::string( $row->currency ),
|
|
sourcePaymentId: Val::intOrNull( $row->source_payment_id ?? null ),
|
|
sourceLessonId: Val::intOrNull( $row->source_lesson_id ?? null ),
|
|
reason: Val::stringOrNull( $row->reason ?? null ),
|
|
status: Val::string( $row->status ),
|
|
createdAt: Val::stringOrNull( $row->created_at ?? null ),
|
|
updatedAt: Val::stringOrNull( $row->updated_at ?? null ),
|
|
id: Val::int( $row->id ),
|
|
);
|
|
}
|
|
|
|
public function isAvailable(): bool {
|
|
return self::STATUS_AVAILABLE === $this->status && $this->remaining > 0.0;
|
|
}
|
|
|
|
/**
|
|
* Whose balance this credit sits in: the recorded payer, falling back to the
|
|
* student. Callers go through here rather than reading `payerId`, so the `0`
|
|
* default of a pre-guardian credit never leaks out as a user id.
|
|
*/
|
|
public function payerOrStudent(): int {
|
|
return $this->payerId > 0 ? $this->payerId : $this->studentId;
|
|
}
|
|
|
|
/**
|
|
* Returns a plain array representation of the credit.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(): array {
|
|
return [
|
|
'id' => $this->id,
|
|
'student_id' => $this->studentId,
|
|
'payer_id' => $this->payerOrStudent(),
|
|
'amount' => $this->amount,
|
|
'remaining' => $this->remaining,
|
|
'currency' => $this->currency,
|
|
'source_payment_id' => $this->sourcePaymentId,
|
|
'source_lesson_id' => $this->sourceLessonId,
|
|
'reason' => $this->reason,
|
|
'status' => $this->status,
|
|
'created_at' => $this->createdAt,
|
|
'updated_at' => $this->updatedAt,
|
|
];
|
|
}
|
|
}
|