Credit students for cancelled paid lessons
CI / Tests (PHP 8.1) (pull_request) Successful in 47s
CI / Tests (PHP 8.2) (pull_request) Successful in 47s
CI / PHPStan (pull_request) Successful in 3m12s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m42s
CI / Build Plugin Zip (pull_request) Skipped
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 2m52s

Cancelling a lesson that was already paid for now credits the student
that money instead of leaving it as a manual refund, and the daily
scheduled-billing scan applies any available credit against their due
charges before emailing the notice.

- New us_credits ledger + us_payments.credit_applied column (Payment::netDue).
- PaymentService::creditForCancelledLesson issues a per-lesson share of the
  covering payment's total; wired into all three cancel paths (student
  self-cancel, instructor status update, admin student-detail cancel).
- PaymentService::applyCredits draws credit down FIFO across a run's charges,
  marking a fully-covered charge paid-by-credit; the notice shows the credit
  applied and reduced total, and the admin queue shows net due.
- Student detail page shows a student's credit balance and history.

Ships as part of the unreleased 1.2.0 (same release as scheduled billing).

Tests: composer test (585), composer lint, composer cs all pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
2026-07-24 15:32:20 -03:00
co-authored by Claude Opus 4.8
parent 3f9aef7746
commit e8e66eef3c
30 changed files with 1210 additions and 80 deletions
+36 -1
View File
@@ -5,6 +5,8 @@ namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Mockery;
use Unsupervised\Schedular\Auth\StudentHistory;
use Unsupervised\Schedular\Payment\Credit;
use Unsupervised\Schedular\Payment\CreditRepository;
use Unsupervised\Schedular\Payment\Payment;
use Unsupervised\Schedular\Payment\PaymentRepository;
use Unsupervised\Schedular\Policy\AcceptanceRepository;
@@ -27,6 +29,7 @@ class StudentHistoryTest extends TestCase
private AnswerRepository&Mockery\MockInterface $answers;
private QuestionRepository&Mockery\MockInterface $questions;
private PaymentRepository&Mockery\MockInterface $payments;
private CreditRepository&Mockery\MockInterface $credits;
private StudentHistory $history;
protected function setUp(): void
@@ -39,6 +42,7 @@ class StudentHistoryTest extends TestCase
$this->answers = Mockery::mock(AnswerRepository::class);
$this->questions = Mockery::mock(QuestionRepository::class);
$this->payments = Mockery::mock(PaymentRepository::class);
$this->credits = Mockery::mock(CreditRepository::class);
$this->history = new StudentHistory(
$this->acceptances,
@@ -46,7 +50,8 @@ class StudentHistoryTest extends TestCase
$this->policyVersions,
$this->answers,
$this->questions,
$this->payments
$this->payments,
$this->credits
);
}
@@ -215,4 +220,34 @@ class StudentHistoryTest extends TestCase
self::assertSame('Enrolment #3', $rows[0]['context']);
self::assertSame('—', $rows[0]['receipt']);
}
public function testCreditBalanceDelegatesToRepository(): void
{
$this->credits->shouldReceive('availableBalance')->once()->with(5)->andReturn(45.0);
self::assertSame(45.0, $this->history->creditBalance(5));
}
public function testCreditsBuildDisplayRows(): void
{
$this->credits->shouldReceive('findByStudent')->once()->with(5)->andReturn([
new Credit(5, 33.00, 13.00, 'CAD', 12, 77, 'Credit for cancelled lesson #77', Credit::STATUS_AVAILABLE, '2026-07-01 09:00:00', id: 300),
]);
$rows = $this->history->credits(5);
self::assertSame(
[
[
'created_at' => '2026-07-01 09:00:00',
'amount' => 33.00,
'remaining' => 13.00,
'currency' => 'CAD',
'reason' => 'Credit for cancelled lesson #77',
'status' => Credit::STATUS_AVAILABLE,
],
],
$rows
);
}
}