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:
2026-11-24 13:23:46 +02:00
co-authored by Claude Opus 5.5
parent 97bd23bed5
commit 5f9423a3c3
49 changed files with 799 additions and 82 deletions
@@ -16,6 +16,7 @@ use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\InvalidBillableHours;
use App\Modules\Billing\Domain\Exceptions\InvoicesCannotMerge;
use App\Modules\Billing\Domain\Exceptions\JobAlreadyInDraft;
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
use App\Modules\Billing\Domain\ValueObjects\InvoiceTotals;
@@ -41,7 +42,10 @@ function billableJob(int $id, BillingMode $mode = BillingMode::Hours, array $ove
'customerReference' => null,
'mode' => $mode,
'quotedAmountCents' => null,
'isDone' => true,
'inBillableStatus' => true,
'monthlyFeeCents' => null,
'billingStartsOn' => '2026-09-15',
'billingEndsOn' => null,
], $overrides);
return new BillableJob(...$values);
@@ -150,7 +154,7 @@ it('tells why a job cannot be billed', function (BillableJob $job, JobHours $hou
expect((new BillingRules)->reasonNotBillable($job, $hours, $expenses, $fixedBilled, $rates))->toBe($reason);
})->with([
'billable hours' => [billableJob(1), new JobHours(1, [1 => 4]), [], false, null],
'not done' => [billableJob(1, overrides: ['isDone' => false]), new JobHours(1, [1 => 4]), [], false, NotBillableReason::WrongStatus],
'not done' => [billableJob(1, overrides: ['inBillableStatus' => false]), new JobHours(1, [1 => 4]), [], false, NotBillableReason::WrongStatus],
'billed monthly or not billable' => [billableJob(1, overrides: ['mode' => null]), new JobHours(1, [1 => 4]), [], false, NotBillableReason::NotBillableBasis],
'internal customer' => [billableJob(1, overrides: ['isInternal' => true]), new JobHours(1, [1 => 4]), [], false, NotBillableReason::NotBillableBasis],
'consumer without a code' => [billableJob(1, overrides: ['isConsumer' => true]), new JobHours(1, [1 => 4]), [], false, NotBillableReason::MissingIdentityCode],
@@ -163,6 +167,55 @@ it('tells why a job cannot be billed', function (BillableJob $job, JobHours $hou
'fixed price billed before' => [billableJob(1, BillingMode::FixedPrice, ['quotedAmountCents' => 100000]), new JobHours(1, [1 => 4]), [], true, NotBillableReason::NothingToBill],
]);
it('tells why a monthly fee job cannot be billed', function (array $overrides, array $months, array $expenses, ?NotBillableReason $reason) {
$job = billableJob(1, BillingMode::MonthlyFee, $overrides);
expect((new BillingRules)->reasonNotBillable($job, new JobHours(1, [2 => 8]), $expenses, false, [], $months))->toBe($reason);
})->with([
'months to bill' => [['monthlyFeeCents' => 9900], ['2026-11'], [], null],
'no monthly fee' => [[], ['2026-11'], [], NotBillableReason::MissingMonthlyFee],
'every month billed' => [['monthlyFeeCents' => 9900], [], [], NotBillableReason::NothingToBill],
'only expenses' => [['monthlyFeeCents' => 9900], [], [new BillableExpense(5, 1, 'Paperi', 500)], null],
'not running' => [['monthlyFeeCents' => 9900, 'inBillableStatus' => false], ['2026-11'], [], NotBillableReason::WrongStatus],
]);
it('bills every unbilled month of a monthly fee job and its expenses, not its hours', function () {
$job = billableJob(1, BillingMode::MonthlyFee, ['name' => 'Ylläpito', 'monthlyFeeCents' => 9900]);
$lines = (new BillingRules)->lines($job, $job->toDraftJob(), [1 => 8], [new BillableExpense(5, 1, 'Paperi', 500)], false, [1 => new HourlyRate(1, 'Taitto', 8000)], 2550, ['2026-10', '2026-11']);
expect(array_map(fn (InvoiceLine $line): array => [$line->kind, $line->period, $line->description, $line->netCents], $lines))->toBe([
[LineKind::MonthlyFee, '2026-10', '10/2026 2026-0001 Ylläpito', 9900],
[LineKind::MonthlyFee, '2026-11', '11/2026 2026-0001 Ylläpito', 9900],
[LineKind::Expense, null, 'Paperi', 500],
]);
});
it('counts the months from the start to the material, the last day or completion, leaving out billed ones', function (?string $endsOn, string $period, array $billed, array $months) {
expect(BillingMonths::unbilled('2026-09-15', $endsOn, $period, $billed))->toBe($months);
})->with([
'up to the material' => [null, '2026-11', [], ['2026-09', '2026-10', '2026-11']],
'billed months left out' => [null, '2026-11', ['2026-09', '2026-10'], ['2026-11']],
'ends before the material' => ['2026-10-31', '2026-12', [], ['2026-09', '2026-10']],
'over the year end' => [null, '2027-01', ['2026-09', '2026-10'], ['2026-11', '2026-12', '2027-01']],
'material before the start' => [null, '2026-08', [], []],
]);
it('keeps a monthly fee job on an invoice of its own', function () {
$export = draft();
$export->addJob(new DraftJob(1, 10, null, null));
$export->addJob(new DraftJob(2, 10, null, null, [], ownInvoice: true));
try {
$export->moveJob(2, 1);
$this->fail('The jobs were combined.');
} catch (InvoicesCannotMerge $exception) {
expect($exception->refusal)->toBe(MergeRefusal::MonthlyFeeAlone);
}
$export->moveJob(1, 2);
})->throws(InvoicesCannotMerge::class);
it('forms hour lines by work type name with the billable hours, then the expenses', function () {
$rates = [1 => new HourlyRate(1, 'Taitto', 8000), 2 => new HourlyRate(2, 'Kuvaus', 10000)];
$draftJob = (new DraftJob(1, 1, null, null))->withBillableQuarters(1, 6);