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
@@ -10,23 +10,26 @@ use App\Modules\Billing\Application\DTOs\HourlyRate;
use App\Modules\Billing\Application\DTOs\JobHours;
use App\Modules\Billing\Domain\Enums\BillingMode;
use App\Modules\Billing\Domain\Enums\NotBillableReason;
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
/**
* What a job may bill and its invoice lines (spec 12): hours per work type
* at the hourly rates, or the quoted fixed price once, and the billable
* expenses separately. Only approved hours are billed.
* at the hourly rates, the quoted fixed price once, or the monthly fee of
* every unbilled month (spec 5.5), and the billable expenses separately.
* Only approved hours are billed; a fixed price or a monthly fee covers them.
*/
final class BillingRules
{
/**
* @param list<BillableExpense> $expenses
* @param array<int, HourlyRate> $rates
* @param list<string> $unbilledMonths For a monthly fee job, as Y-m.
*/
public function reasonNotBillable(BillableJob $job, JobHours $hours, array $expenses, bool $fixedPriceBilled, array $rates): ?NotBillableReason
public function reasonNotBillable(BillableJob $job, JobHours $hours, array $expenses, bool $fixedPriceBilled, array $rates, array $unbilledMonths = []): ?NotBillableReason
{
if (! $job->isDone) {
if (! $job->inBillableStatus) {
return NotBillableReason::WrongStatus;
}
@@ -38,6 +41,14 @@ final class BillingRules
return NotBillableReason::MissingIdentityCode;
}
if ($job->mode === BillingMode::MonthlyFee) {
if ($job->monthlyFeeCents === null) {
return NotBillableReason::MissingMonthlyFee;
}
return $unbilledMonths === [] && $expenses === [] ? NotBillableReason::NothingToBill : null;
}
if ($job->mode === BillingMode::FixedPrice) {
if (! $fixedPriceBilled && $job->quotedAmountCents === null) {
return NotBillableReason::MissingQuotedAmount;
@@ -76,18 +87,26 @@ final class BillingRules
/**
* The job's lines: hours per work type by name (management's billable
* hours or the approved ones) or the fixed price, then the expenses.
* hours or the approved ones), the fixed price or the monthly fees,
* then the expenses.
*
* @param array<int, int> $approvedByWorkType
* @param list<BillableExpense> $expenses
* @param array<int, HourlyRate> $rates
* @param list<string> $unbilledMonths For a monthly fee job, as Y-m.
* @return list<InvoiceLine>
*/
public function lines(BillableJob $job, DraftJob $draftJob, array $approvedByWorkType, array $expenses, bool $fixedPriceBilled, array $rates, int $vatBasisPoints): array
public function lines(BillableJob $job, DraftJob $draftJob, array $approvedByWorkType, array $expenses, bool $fixedPriceBilled, array $rates, int $vatBasisPoints, array $unbilledMonths = []): array
{
$lines = [];
if ($job->mode === BillingMode::FixedPrice) {
if ($job->mode === BillingMode::MonthlyFee) {
if ($job->monthlyFeeCents !== null) {
foreach ($unbilledMonths as $month) {
$lines[] = InvoiceLine::monthlyFee($job->id, $month, BillingMonths::label($month).' '.$job->label(), $job->monthlyFeeCents, $vatBasisPoints);
}
}
} elseif ($job->mode === BillingMode::FixedPrice) {
if (! $fixedPriceBilled && $job->quotedAmountCents !== null) {
$lines[] = InvoiceLine::fixedPrice($job->id, $job->label(), $job->quotedAmountCents, $vatBasisPoints);
}