Files
petriandClaude Opus 5.5 5f9423a3c3 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]>
2026-11-24 13:23:46 +02:00

144 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\Services;
use App\Modules\Billing\Application\DTOs\BillableExpense;
use App\Modules\Billing\Application\DTOs\BillableJob;
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, 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, array $unbilledMonths = []): ?NotBillableReason
{
if (! $job->inBillableStatus) {
return NotBillableReason::WrongStatus;
}
if ($job->mode === null || $job->isInternal) {
return NotBillableReason::NotBillableBasis;
}
if ($job->isConsumer && ! $job->hasIdentityCode) {
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;
}
return $fixedPriceBilled && $expenses === [] ? NotBillableReason::NothingToBill : null;
}
if ($this->missingRateNames($hours, $rates) !== []) {
return NotBillableReason::MissingHourlyRate;
}
return $hours->approvedQuarters() === 0 && $expenses === [] ? NotBillableReason::NothingToBill : null;
}
/**
* Work types with approved hours but no hourly rate, for an hours job.
*
* @param array<int, HourlyRate> $rates
* @return list<string>
*/
public function missingRateNames(JobHours $hours, array $rates): array
{
$names = [];
foreach ($hours->approvedByWorkType as $workTypeId => $quarters) {
if ($quarters > 0 && ($rates[$workTypeId]->rateCents ?? null) === null) {
$names[] = $rates[$workTypeId]->name ?? (string) $workTypeId;
}
}
sort($names);
return $names;
}
/**
* The job's lines: hours per work type by name (management's billable
* 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 $unbilledMonths = []): array
{
$lines = [];
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);
}
} else {
foreach ($this->sortedByName($approvedByWorkType, $rates) as $workTypeId => $approved) {
$rate = $rates[$workTypeId] ?? null;
if ($approved <= 0 || $rate?->rateCents === null) {
continue;
}
$lines[] = InvoiceLine::hours($job->id, $workTypeId, $rate->name, $approved, $draftJob->billableOf($workTypeId, $approved), $rate->rateCents, $vatBasisPoints);
}
}
foreach ($expenses as $expense) {
$lines[] = InvoiceLine::expense($job->id, $expense->id, $expense->description, $expense->amountCents, $vatBasisPoints);
}
return $lines;
}
/**
* @param array<int, int> $quarters
* @param array<int, HourlyRate> $rates
* @return array<int, int>
*/
public function sortedByName(array $quarters, array $rates): array
{
uksort($quarters, fn (int $a, int $b): int => strcmp($rates[$a]->name ?? '', $rates[$b]->name ?? '') ?: $a <=> $b);
return $quarters;
}
}