Show a monthly fee job's billed periods on a page of its own

The new billing::job-months page lists the months that created billing
materials have billed for a monthly fee job, with links to the materials
and the total, and the months still to bill up to the current month,
counted the same way as a new material counts them. The job page links
to it next to the payment basis for those who manage billing.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 15:56:15 +02:00
co-authored by Claude Sonnet 5.5
parent e817cca329
commit bc3dbf16a4
16 changed files with 470 additions and 3 deletions
@@ -0,0 +1,80 @@
<?php
use App\Models\User;
use App\Modules\Billing\Domain\Enums\ExportStatus;
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingExportModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->manager = User::factory()->management()->create();
$this->actingAs($this->manager);
$this->travelTo(now()->setDate(2026, 11, 20)->setTime(12, 0));
$this->maintenance = CustomerJobModel::factory()->create([
'name' => 'Somen ylläpito',
'customer_id' => CustomerModel::factory()->company()->create(['name' => 'Kahvila Oy'])->id,
'status' => JobStatus::InProduction,
'payment_basis' => 'monthly_fee',
'monthly_fee_cents' => 39000,
'starts_on' => '2026-09-10',
]);
});
/**
* Creates a billing material whose billing month is the given month, with the monthly fee jobs on it.
*/
function createMonthlyMaterial(string $period): void
{
Livewire::test('billing::index')->call('openDraftNow');
$exportId = (int) BillingExportModel::query()->where('status', ExportStatus::Draft)->value('id');
Livewire::test('billing::export', ['export' => $exportId])
->call('savePeriod', $period)
->call('finalize')
->assertSet('errorMessage', '');
}
it('lists the billed months with their billing material and the months still to bill', function () {
createMonthlyMaterial('2026-10');
$this->travelTo(now()->setDate(2026, 12, 5));
Livewire::test('billing::job-months', ['job' => $this->maintenance->id])
->assertSeeInOrder(['Laskutetut kuukaudet (2)', '9/2026', 'Aineisto 1', '390,00', '10/2026', 'Aineisto 1', 'Laskutettu yhteensä 780,00'])
->assertSeeInOrder(['Laskuttamatta kuukauteen 12/2026 mennessä', '11/2026', '12/2026']);
});
it('shows a job with every month billed and one with none billed', function () {
Livewire::test('billing::job-months', ['job' => $this->maintenance->id])
->assertSee('Mitään kuukautta ei ole vielä laskutettu.')
->assertSeeInOrder(['9/2026', '10/2026', '11/2026']);
createMonthlyMaterial('2026-11');
Livewire::test('billing::job-months', ['job' => $this->maintenance->id])
->assertSee('Kaikki kuukaudet tähän kuukauteen asti on laskutettu.');
});
it('links a monthly fee job to its billed periods for management only', function () {
$hourly = CustomerJobModel::factory()->create(['payment_basis' => 'hourly']);
$link = route('billing.job-months', $this->maintenance->id);
$this->get(route('jobs.show', $this->maintenance))->assertSee($link, false)->assertSee('Laskutetut kaudet');
$this->get(route('jobs.show', $hourly))->assertDontSee('Laskutetut kaudet');
$this->actingAs(User::factory()->employee()->create());
$this->get(route('jobs.show', $this->maintenance))->assertOk()->assertDontSee($link, false);
$this->get($link)->assertForbidden();
});
it('has no billed periods page for a job that is not billed by a monthly fee', function () {
$hourly = CustomerJobModel::factory()->create(['payment_basis' => 'hourly']);
$this->get(route('billing.job-months', $hourly))->assertNotFound();
$this->get(route('billing.job-months', 999999))->assertNotFound();
});