Add the billing material with drafts, combined invoices and billed marks

Management builds the monthly billing material (laskuaineisto) as a
draft: done jobs are added as invoices of their own, combined when the
customer, billing unit and reference allow it, and their billable hours
are set per work type. Creating the material checks the jobs again,
marks the approved hours and billable expenses billed, forms invoice
lines with price and VAT snapshots and moves the jobs to "Lasku luotu".
Fixed price jobs bill their quoted amount once. Billed hours keep their
approval and billed expenses can no longer change. The Fennoa export
and monthly fee lines come later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 13:08:24 +02:00
co-authored by Claude Opus 5.5
parent 6de5d9bef1
commit 97bd23bed5
98 changed files with 4887 additions and 22 deletions
@@ -0,0 +1,124 @@
<?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\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.
*/
final class BillingRules
{
/**
* @param list<BillableExpense> $expenses
* @param array<int, HourlyRate> $rates
*/
public function reasonNotBillable(BillableJob $job, JobHours $hours, array $expenses, bool $fixedPriceBilled, array $rates): ?NotBillableReason
{
if (! $job->isDone) {
return NotBillableReason::WrongStatus;
}
if ($job->mode === null || $job->isInternal) {
return NotBillableReason::NotBillableBasis;
}
if ($job->isConsumer && ! $job->hasIdentityCode) {
return NotBillableReason::MissingIdentityCode;
}
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) or the fixed price, then the expenses.
*
* @param array<int, int> $approvedByWorkType
* @param list<BillableExpense> $expenses
* @param array<int, HourlyRate> $rates
* @return list<InvoiceLine>
*/
public function lines(BillableJob $job, DraftJob $draftJob, array $approvedByWorkType, array $expenses, bool $fixedPriceBilled, array $rates, int $vatBasisPoints): array
{
$lines = [];
if ($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;
}
}