Let parents register once and book for their children
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]>
This commit is contained in:
@@ -16,6 +16,7 @@ class CreditRepository {
|
||||
$this->table,
|
||||
[
|
||||
'student_id' => $credit->studentId,
|
||||
'payer_id' => $credit->payerOrStudent(),
|
||||
'amount' => $credit->amount,
|
||||
'remaining' => $credit->remaining,
|
||||
'currency' => $credit->currency,
|
||||
@@ -25,7 +26,7 @@ class CreditRepository {
|
||||
'status' => $credit->status,
|
||||
'created_at' => current_time( 'mysql' ),
|
||||
],
|
||||
[ '%d', '%f', '%f', '%s', '%d', '%d', '%s', '%s', '%s' ]
|
||||
[ '%d', '%d', '%f', '%f', '%s', '%d', '%d', '%s', '%s', '%s' ]
|
||||
);
|
||||
|
||||
return $this->db->insert_id;
|
||||
@@ -56,15 +57,16 @@ class CreditRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* A student's total unused credit balance (sum of the remaining amounts of every
|
||||
* still-available credit).
|
||||
* A payer's total unused credit balance (sum of the remaining amounts of every
|
||||
* still-available credit). Keyed on the payer, so a guardian's balance covers
|
||||
* credits earned by any of their children — one family, one balance.
|
||||
*/
|
||||
public function availableBalance( int $studentId ): float {
|
||||
public function availableBalance( int $payerId ): float {
|
||||
$total = $this->db->get_var(
|
||||
$this->db->prepare(
|
||||
'SELECT COALESCE( SUM( remaining ), 0 ) FROM %i WHERE student_id = %d AND status = %s',
|
||||
'SELECT COALESCE( SUM( remaining ), 0 ) FROM %i WHERE payer_id = %d AND status = %s',
|
||||
$this->table,
|
||||
$studentId,
|
||||
$payerId,
|
||||
Credit::STATUS_AVAILABLE
|
||||
)
|
||||
);
|
||||
@@ -73,17 +75,17 @@ class CreditRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* A student's still-available credits, oldest first — the FIFO order they are
|
||||
* A payer's still-available credits, oldest first — the FIFO order they are
|
||||
* consumed in.
|
||||
*
|
||||
* @return list<Credit>
|
||||
*/
|
||||
public function findAvailableByStudent( int $studentId ): array {
|
||||
public function findAvailableByPayer( int $payerId ): array {
|
||||
$rows = $this->db->get_results(
|
||||
$this->db->prepare(
|
||||
'SELECT * FROM %i WHERE student_id = %d AND status = %s AND remaining > 0 ORDER BY created_at ASC, id ASC',
|
||||
'SELECT * FROM %i WHERE payer_id = %d AND status = %s AND remaining > 0 ORDER BY created_at ASC, id ASC',
|
||||
$this->table,
|
||||
$studentId,
|
||||
$payerId,
|
||||
Credit::STATUS_AVAILABLE
|
||||
)
|
||||
);
|
||||
@@ -92,7 +94,10 @@ class CreditRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* Every credit for a student, newest first (admin history).
|
||||
* Every credit earned by a student, newest first — the admin history on their
|
||||
* own screen. Unlike the balance this is keyed on the student, so a child's
|
||||
* screen shows the credits their cancellations produced even though the
|
||||
* balance itself sits with their guardian.
|
||||
*
|
||||
* @return list<Credit>
|
||||
*/
|
||||
@@ -109,17 +114,30 @@ class CreditRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw down a student's credit balance by $amount, consuming their available
|
||||
* Backfill `payer_id` on credits written before guardian accounts existed,
|
||||
* where the student was always the payer. Run once from the installer so the
|
||||
* payer-keyed balance queries see those rows.
|
||||
*/
|
||||
public function backfillPayerIds(): void {
|
||||
$sql = $this->db->prepare( 'UPDATE %i SET payer_id = student_id WHERE payer_id = 0', $this->table );
|
||||
|
||||
if ( null !== $sql ) {
|
||||
$this->db->query( $sql );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw down a payer's credit balance by $amount, consuming their available
|
||||
* credits oldest first and marking each fully-spent credit `consumed`. Stops once
|
||||
* the amount is exhausted; a balance shorter than $amount simply drains to zero.
|
||||
*/
|
||||
public function consume( int $studentId, float $amount ): void {
|
||||
public function consume( int $payerId, float $amount ): void {
|
||||
$remaining = round( $amount, 2 );
|
||||
if ( $remaining <= 0.0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $this->findAvailableByStudent( $studentId ) as $credit ) {
|
||||
foreach ( $this->findAvailableByPayer( $payerId ) as $credit ) {
|
||||
if ( $remaining <= 0.0 ) {
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user