Add monthly fee billing lines to the billing material
Jobs billed by a fixed monthly fee get a monthly fee field, kept behind the quotes permission like the quoted amount. A billing material now has a billing month: a new draft adds every monthly fee job with something to bill, each on an invoice of its own, and bills one line for every unbilled month from the job's start up to the billing month (or its end or completion), so skipped months are caught up and no month is billed twice. Their expenses are billed alongside, their hours are covered by the fee and marked billed, and the job keeps running. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -225,3 +225,73 @@ it('keeps everyone but management out', function (string $role) {
|
||||
|
||||
$this->get(route('billing.index'))->assertForbidden();
|
||||
})->with(['employee', 'intern', 'subcontractor']);
|
||||
|
||||
it('bills every unbilled month of a monthly fee job on an invoice of its own and keeps the job running', function () {
|
||||
$this->travelTo(now()->setDate(2026, 11, 20)->setTime(12, 0));
|
||||
|
||||
$maintenance = doneJob([
|
||||
'name' => 'Ylläpito',
|
||||
'status' => JobStatus::InProduction,
|
||||
'payment_basis' => 'monthly_fee',
|
||||
'monthly_fee_cents' => 9900,
|
||||
'starts_on' => '2026-09-10',
|
||||
], [$this->layout->id => 2]);
|
||||
ExpenseModel::factory()->create(['customer_job_id' => $maintenance->id, 'description' => 'Lisenssi', 'billed_amount_cents' => 1500]);
|
||||
$other = doneJob([], [$this->layout->id => 1]);
|
||||
|
||||
$exportId = openDraft();
|
||||
|
||||
$page = Livewire::test('billing::export', ['export' => $exportId])
|
||||
->assertSee('9/2026')
|
||||
->assertSee('Hyväksytyt tunnit 2 h kuuluvat kuukausihintaan')
|
||||
->call('savePeriod', '2026-10')
|
||||
->assertSet('statusMessage', 'Laskutuskuukausi on nyt 10/2026.')
|
||||
->assertDontSee('11/2026')
|
||||
->call('savePeriod', '2026-13')
|
||||
->assertHasErrors(['period'])
|
||||
->call('savePeriod', '2026-11')
|
||||
->call('add', $other->id)
|
||||
->set("moveTargets.{$other->id}", (string) $maintenance->id)
|
||||
->call('move', $other->id)
|
||||
->assertSet('errorMessage', 'Kuukausilaskutuksen työ laskutetaan omalla laskullaan.');
|
||||
|
||||
expect($page->get('export')->invoices)->toHaveCount(2)
|
||||
->and($page->get('export')->invoices[0]->mergeTargets[$maintenance->id])->toBe([]);
|
||||
|
||||
$page->call('finalize')->assertSet('errorMessage', '');
|
||||
|
||||
$monthly = BillingLineModel::query()->where('customer_job_id', $maintenance->id)->orderBy('sort_order')->get();
|
||||
expect($monthly->map->only(['kind', 'period', 'net_cents'])->all())->toBe([
|
||||
['kind' => LineKind::MonthlyFee, 'period' => '2026-09', 'net_cents' => 9900],
|
||||
['kind' => LineKind::MonthlyFee, 'period' => '2026-10', 'net_cents' => 9900],
|
||||
['kind' => LineKind::MonthlyFee, 'period' => '2026-11', 'net_cents' => 9900],
|
||||
['kind' => LineKind::Expense, 'period' => null, 'net_cents' => 1500],
|
||||
])
|
||||
->and($maintenance->refresh()->status)->toBe(JobStatus::InProduction)
|
||||
->and($other->refresh()->status)->toBe(JobStatus::Invoiced)
|
||||
->and(TimeEntryModel::query()->where('customer_job_id', $maintenance->id)->value('billing_export_id'))->toBe($exportId);
|
||||
|
||||
// The same month again bills nothing; the next month bills one row.
|
||||
$next = openDraft();
|
||||
expect(DB::table('billing_invoice_jobs')->count())->toBe(2);
|
||||
|
||||
$this->travelTo(now()->setDate(2026, 12, 5));
|
||||
Livewire::test('billing::export', ['export' => $next])
|
||||
->assertSee('Ei laskutettavaa')
|
||||
->call('savePeriod', '2026-12')
|
||||
->call('add', $maintenance->id)
|
||||
->assertSet('errorMessage', '')
|
||||
->call('finalize');
|
||||
|
||||
expect(BillingLineModel::query()->where('customer_job_id', $maintenance->id)->where('kind', LineKind::MonthlyFee)->pluck('period')->all())
|
||||
->toBe(['2026-09', '2026-10', '2026-11', '2026-12']);
|
||||
});
|
||||
|
||||
it('does not bill a monthly fee job without a monthly fee', function () {
|
||||
$job = doneJob(['status' => JobStatus::InProduction, 'payment_basis' => 'monthly_fee']);
|
||||
|
||||
Livewire::test('billing::export', ['export' => openDraft()])
|
||||
->assertSee('Kuukausilaskutuksen työltä puuttuu kuukausihinta.')
|
||||
->call('add', $job->id)
|
||||
->assertSet('errorMessage', fn (string $message) => str_contains($message, 'kuukausihinta'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user