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,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
use App\Modules\Billing\Domain\Enums\NotBillableReason;
/**
* A done job that is not on the draft: what it has to bill, and why it
* cannot be added, if it cannot.
*/
final readonly class AvailableJob
{
/**
* @param list<string> $missingRateNames
*/
public function __construct(
public BillableJob $job,
public int $approvedQuarters,
public int $unapprovedQuarters,
public int $expenseCount,
public ?NotBillableReason $reason,
public array $missingRateNames = [],
) {}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
/**
* A billable, unbilled expense of a job, with its amount to bill without VAT.
*/
final readonly class BillableExpense
{
public function __construct(
public int $id,
public int $jobId,
public string $description,
public int $amountCents,
) {}
}
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
use App\Modules\Billing\Domain\Enums\BillingMode;
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
/**
* A customer job as billing sees it. A null mode means the job is not
* billed this way (not billable, or billed monthly).
*/
final readonly class BillableJob
{
public function __construct(
public int $id,
public string $number,
public string $name,
public int $customerId,
public string $customerName,
public bool $isConsumer,
public bool $isInternal,
public bool $hasIdentityCode,
public ?int $billingUnitId,
public ?string $billingUnitName,
public ?string $customerReference,
public ?BillingMode $mode,
public ?int $quotedAmountCents,
public bool $isDone,
) {}
/**
* E.g. "2026-0005 Messuvideo".
*/
public function label(): string
{
return $this->number.' '.$this->name;
}
public function toDraftJob(): DraftJob
{
return new DraftJob($this->id, $this->customerId, $this->billingUnitId, $this->customerReference);
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
use App\Modules\Billing\Domain\Enums\ExportStatus;
use DateTimeImmutable;
/**
* A billing material on the list. The sum without VAT is known once it is created.
*/
final readonly class ExportSummary
{
public function __construct(
public int $id,
public ExportStatus $status,
public DateTimeImmutable $createdAt,
public string $createdByName,
public ?DateTimeImmutable $finalizedAt,
public ?string $finalizedByName,
public int $invoiceCount,
public int $jobCount,
public ?int $netCents,
) {}
}
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
use App\Modules\Billing\Domain\Enums\ExportStatus;
use App\Modules\Billing\Domain\ValueObjects\InvoiceTotals;
use DateTimeImmutable;
/**
* A billing material for its page: a draft with its preview and the done
* jobs that may be added, or a created material with its lines.
*/
final readonly class ExportView
{
/**
* @param list<InvoiceView> $invoices
* @param list<AvailableJob> $availableJobs
*/
public function __construct(
public int $id,
public ExportStatus $status,
public DateTimeImmutable $createdAt,
public string $createdByName,
public ?DateTimeImmutable $finalizedAt,
public ?string $finalizedByName,
public array $invoices,
public InvoiceTotals $totals,
public array $availableJobs = [],
) {}
public function isDraft(): bool
{
return $this->status === ExportStatus::Draft;
}
public function jobCount(): int
{
return array_sum(array_map(fn (InvoiceView $invoice): int => count($invoice->jobs), $this->invoices));
}
/**
* Whether some job on the draft cannot be billed as it is.
*/
public function hasProblems(): bool
{
foreach ($this->invoices as $invoice) {
foreach ($invoice->jobs as $job) {
if ($job->problem !== null) {
return true;
}
}
}
return false;
}
}
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
/**
* A work type's name and hourly rate without VAT, if it has one.
*/
final readonly class HourlyRate
{
public function __construct(
public int $workTypeId,
public string $name,
public ?int $rateCents,
) {}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
/**
* A work type's hours on a draft invoice: approved and billable quarters,
* and the line's sum without VAT.
*/
final readonly class HoursRow
{
public function __construct(
public int $workTypeId,
public string $name,
public int $approvedQuarters,
public int $billableQuarters,
public bool $isAdjusted,
public ?int $rateCents,
public int $netCents,
) {}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
use App\Modules\Billing\Domain\Enums\BillingMode;
use App\Modules\Billing\Domain\Enums\NotBillableReason;
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
/**
* A job on an invoice of a billing material: its hours per work type (on a
* draft), its lines, and on a draft what may stop it from being billed.
*/
final readonly class InvoiceJobView
{
/**
* @param list<HoursRow> $hours
* @param list<InvoiceLine> $lines
* @param list<string> $missingRateNames
*/
public function __construct(
public int $jobId,
public string $number,
public string $name,
public ?BillingMode $mode,
public array $hours,
public array $lines,
public int $unapprovedQuarters,
public ?NotBillableReason $problem,
public array $missingRateNames = [],
) {}
public function label(): string
{
return $this->number.' '.$this->name;
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
use App\Modules\Billing\Domain\ValueObjects\InvoiceTotals;
/**
* An invoice of a billing material for its page.
*/
final readonly class InvoiceView
{
/**
* @param list<InvoiceJobView> $jobs
* @param array<int, list<MergeTarget>> $mergeTargets Invoices each job may move to, keyed by job id.
*/
public function __construct(
public int $key,
public string $customerName,
public ?string $billingUnitName,
public ?string $reference,
public array $jobs,
public InvoiceTotals $totals,
public array $mergeTargets = [],
) {}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
/**
* A job's unbilled hours in quarters of an hour: approved ones by work
* type, and those still waiting for approval (never billed).
*/
final readonly class JobHours
{
/**
* @param array<int, int> $approvedByWorkType Keyed by work type id.
*/
public function __construct(
public int $jobId,
public array $approvedByWorkType = [],
public int $unapprovedQuarters = 0,
) {}
public function approvedQuarters(): int
{
return array_sum($this->approvedByWorkType);
}
}
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\DTOs;
/**
* An invoice a job may be moved to, named by its jobs.
*/
final readonly class MergeTarget
{
public function __construct(
public int $invoiceKey,
public string $label,
) {}
}