Files
petriandClaude Opus 5.5 97bd23bed5 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]>
2026-11-24 13:08:24 +02:00

78 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Billing\Domain\ValueObjects;
use App\Modules\Shared\Domain\ValueObjects\Money;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
/**
* An invoice's sum without VAT, its VAT per rate (counted from the rate's
* total, rounded half up) and the sum with VAT.
*/
final readonly class InvoiceTotals
{
/**
* @param array<int, array{net: int, vat: int}> $byVatRate Keyed by VAT basis points, highest first.
*/
public function __construct(
public int $netCents,
public int $vatCents,
public array $byVatRate,
) {}
/**
* @param list<InvoiceLine> $lines
*/
public static function of(array $lines): self
{
$netByRate = [];
foreach ($lines as $line) {
$netByRate[$line->vatBasisPoints] = ($netByRate[$line->vatBasisPoints] ?? 0) + $line->netCents;
}
krsort($netByRate);
$byRate = [];
$net = 0;
$vat = 0;
foreach ($netByRate as $basisPoints => $rateNet) {
$rateVat = Money::fromCents($rateNet)->addVat(new VatPercentage($basisPoints))->cents - $rateNet;
$byRate[$basisPoints] = ['net' => $rateNet, 'vat' => $rateVat];
$net += $rateNet;
$vat += $rateVat;
}
return new self($net, $vat, $byRate);
}
/**
* @param list<self> $totals
*/
public static function sum(array $totals): self
{
$byRate = [];
foreach ($totals as $total) {
foreach ($total->byVatRate as $basisPoints => $amounts) {
$byRate[$basisPoints] = [
'net' => ($byRate[$basisPoints]['net'] ?? 0) + $amounts['net'],
'vat' => ($byRate[$basisPoints]['vat'] ?? 0) + $amounts['vat'],
];
}
}
krsort($byRate);
return new self(
array_sum(array_column($byRate, 'net')),
array_sum(array_column($byRate, 'vat')),
$byRate,
);
}
public function grossCents(): int
{
return $this->netCents + $this->vatCents;
}
}