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:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Billing\Domain\Exceptions\InvalidBillableHours;
|
||||
|
||||
/**
|
||||
* A job on a draft invoice: who it bills (customer, billing unit, the
|
||||
* customer's reference) and management's billable hours per work type.
|
||||
* A work type without its own billable hours bills its approved hours.
|
||||
*/
|
||||
final readonly class DraftJob
|
||||
{
|
||||
/** 9 999 hours. */
|
||||
public const int MAX_QUARTERS = 39_996;
|
||||
|
||||
/**
|
||||
* @param array<int, int> $billableQuarters Keyed by work type id.
|
||||
*/
|
||||
public function __construct(
|
||||
public int $jobId,
|
||||
public int $customerId,
|
||||
public ?int $billingUnitId,
|
||||
public ?string $reference,
|
||||
public array $billableQuarters = [],
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws InvalidBillableHours
|
||||
*/
|
||||
public function withBillableQuarters(int $workTypeId, ?int $quarters): self
|
||||
{
|
||||
$billable = $this->billableQuarters;
|
||||
|
||||
if ($quarters === null) {
|
||||
unset($billable[$workTypeId]);
|
||||
} elseif ($quarters < 0 || $quarters > self::MAX_QUARTERS) {
|
||||
throw new InvalidBillableHours;
|
||||
} else {
|
||||
$billable[$workTypeId] = $quarters;
|
||||
}
|
||||
|
||||
return new self($this->jobId, $this->customerId, $this->billingUnitId, $this->reference, $billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* The billable quarters of a work type: management's own, or the approved ones.
|
||||
*/
|
||||
public function billableOf(int $workTypeId, int $approvedQuarters): int
|
||||
{
|
||||
return $this->billableQuarters[$workTypeId] ?? $approvedQuarters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses billable hours such as "2,5", "2.5" or "2" to quarters of an
|
||||
* hour; null for an empty value (back to the approved hours).
|
||||
*
|
||||
* @throws InvalidBillableHours
|
||||
*/
|
||||
public static function parseHours(string $value): ?int
|
||||
{
|
||||
$value = str_replace(',', '.', trim($value));
|
||||
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (preg_match('/^\d{1,4}(\.\d{1,2})?$/', $value) !== 1) {
|
||||
throw new InvalidBillableHours;
|
||||
}
|
||||
|
||||
$quarters = (float) $value * 4;
|
||||
|
||||
if (abs($quarters - round($quarters)) > 0.0001) {
|
||||
throw new InvalidBillableHours;
|
||||
}
|
||||
|
||||
return (int) round($quarters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Billing\Domain\Enums\LineKind;
|
||||
|
||||
/**
|
||||
* A line of an invoice, without VAT: a work type's billable hours at its
|
||||
* hourly rate, a job's fixed price, or an expense. Prices and descriptions
|
||||
* are snapshots taken when the material is formed.
|
||||
*/
|
||||
final readonly class InvoiceLine
|
||||
{
|
||||
public function __construct(
|
||||
public int $jobId,
|
||||
public LineKind $kind,
|
||||
public ?int $workTypeId,
|
||||
public ?int $expenseId,
|
||||
public string $description,
|
||||
public ?int $recordedQuarters,
|
||||
public ?int $quantityQuarters,
|
||||
public int $unitPriceCents,
|
||||
public int $vatBasisPoints,
|
||||
public int $netCents,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Billable hours × the hourly rate, rounded half up to the cent. The
|
||||
* approved (recorded) hours are kept beside them for reports.
|
||||
*/
|
||||
public static function hours(int $jobId, int $workTypeId, string $workTypeName, int $recordedQuarters, int $billableQuarters, int $hourlyRateCents, int $vatBasisPoints): self
|
||||
{
|
||||
return new self(
|
||||
$jobId,
|
||||
LineKind::Hours,
|
||||
$workTypeId,
|
||||
null,
|
||||
$workTypeName,
|
||||
$recordedQuarters,
|
||||
$billableQuarters,
|
||||
$hourlyRateCents,
|
||||
$vatBasisPoints,
|
||||
intdiv($billableQuarters * $hourlyRateCents * 2 + 4, 8),
|
||||
);
|
||||
}
|
||||
|
||||
public static function fixedPrice(int $jobId, string $description, int $amountCents, int $vatBasisPoints): self
|
||||
{
|
||||
return new self($jobId, LineKind::FixedPrice, null, null, $description, null, null, $amountCents, $vatBasisPoints, $amountCents);
|
||||
}
|
||||
|
||||
public static function expense(int $jobId, int $expenseId, string $description, int $amountCents, int $vatBasisPoints): self
|
||||
{
|
||||
return new self($jobId, LineKind::Expense, null, $expenseId, $description, null, null, $amountCents, $vatBasisPoints, $amountCents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user