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,54 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Application\Services\BillingSnapshot;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Enums\NotBillableReason;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\JobAlreadyInDraft;
use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Adds a done job to the draft as an invoice of its own, if it has
* something to bill and nothing stops it from being billed.
*/
final class AddJobToDraft
{
use ChangesDraft;
public function __construct(
BillingExportRepository $exports,
TransactionManager $transactions,
private BillingSnapshot $snapshots,
) {
$this->exports = $exports;
$this->transactions = $transactions;
}
/**
* @throws BillingExportNotFound
* @throws ExportNotDraft
* @throws JobAlreadyInDraft
* @throws JobNotBillable
*/
public function execute(int $exportId, int $jobId): void
{
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId): void {
$snapshot = $this->snapshots->load([$jobId]);
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
$reason = $snapshot->reasonNotBillable($job);
if ($reason !== null) {
throw new JobNotBillable($jobId, $reason);
}
$export->addJob($job->toDraftJob());
});
}
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Loads a draft locked, lets the use case change it and saves it.
*/
trait ChangesDraft
{
private BillingExportRepository $exports;
private TransactionManager $transactions;
/**
* @param-immediately-invoked-callable $change
*
* @param callable(BillingExport): void $change
*
* @throws BillingExportNotFound
* @throws ExportNotDraft
*/
private function changeDraft(int $exportId, callable $change): void
{
$this->transactions->run(function () use ($exportId, $change): void {
$export = $this->exports->findById($exportId, lock: true) ?? throw new BillingExportNotFound($exportId);
$export->ensureDraft();
$change($export);
$this->exports->save($export);
});
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Throws a draft away. Nothing was billed yet, so the jobs stay as they are.
*/
final class DiscardDraft
{
public function __construct(
private BillingExportRepository $exports,
private TransactionManager $transactions,
) {}
/**
* @throws BillingExportNotFound
* @throws ExportNotDraft
*/
public function execute(int $exportId): void
{
$this->transactions->run(function () use ($exportId): void {
$export = $this->exports->findById($exportId, lock: true) ?? throw new BillingExportNotFound($exportId);
$export->ensureDraft();
$this->exports->delete($export);
});
}
}
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Application\Contracts\BillingExpenses;
use App\Modules\Billing\Application\Contracts\BillingHours;
use App\Modules\Billing\Application\Contracts\BillingJobs;
use App\Modules\Billing\Application\DTOs\BillableExpense;
use App\Modules\Billing\Application\Services\BillingSnapshot;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Enums\NotBillableReason;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportEmpty;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Creates the billing material from its draft (spec 12), all or nothing:
* checks every job again, marks the approved hours and billable expenses
* billed, forms the invoice lines from what was marked with the current
* prices, and moves the jobs to "Lasku luotu".
*/
final class FinalizeBillingExport
{
public function __construct(
private BillingExportRepository $exports,
private BillingJobs $jobs,
private BillingHours $hours,
private BillingExpenses $expenses,
private BillingSnapshot $snapshots,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @return int How many invoices the material has.
*
* @throws BillingExportNotFound
* @throws ExportNotDraft
* @throws ExportEmpty
* @throws JobNotBillable
*/
public function execute(int $exportId, int $actorUserId): int
{
return $this->transactions->run(function () use ($exportId, $actorUserId): int {
$export = $this->exports->findById($exportId, lock: true) ?? throw new BillingExportNotFound($exportId);
$export->ensureDraft();
$snapshot = $this->snapshots->load($export->jobIds());
foreach ($export->jobIds() as $jobId) {
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
$reason = $snapshot->reasonNotBillable($job);
if ($reason !== null) {
throw new JobNotBillable($jobId, $reason);
}
}
$linesByJob = [];
foreach ($export->jobs() as $draftJob) {
$job = $snapshot->jobs[$draftJob->jobId];
$billedHours = $this->hours->markBilled($job->id, $exportId);
$billedExpenseIds = array_flip($this->expenses->markBilled($job->id, $exportId));
$billedExpenses = array_values(array_filter(
$snapshot->expensesOf($job->id),
fn (BillableExpense $expense): bool => isset($billedExpenseIds[$expense->id]),
));
$linesByJob[$job->id] = $snapshot->rules->lines($job, $draftJob, $billedHours, $billedExpenses, $snapshot->isFixedPriceBilled($job->id), $snapshot->rates, $snapshot->vatBasisPoints);
$this->jobs->markInvoiced($job->id, $actorUserId);
}
$export->finalize($linesByJob, $actorUserId, $this->clock->now());
$this->exports->save($export);
return count($export->invoices());
});
}
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\InvoicesCannotMerge;
use App\Modules\Billing\Domain\Exceptions\JobNotInDraft;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Moves a job to another invoice of the draft, when the jobs may be billed together (same customer and billing unit, no two references).
*/
final class MoveJobToInvoice
{
use ChangesDraft;
public function __construct(BillingExportRepository $exports, TransactionManager $transactions)
{
$this->exports = $exports;
$this->transactions = $transactions;
}
/**
* @throws BillingExportNotFound
* @throws ExportNotDraft
* @throws JobNotInDraft
* @throws InvoicesCannotMerge
*/
public function execute(int $exportId, int $jobId, int $invoiceKey): void
{
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId, $invoiceKey): void {
$export->moveJob($jobId, $invoiceKey);
});
}
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Exceptions\DraftAlreadyOpen;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Opens a new billing material draft. There is one draft at a time, so
* two people never bill the same jobs on separate drafts.
*/
final class OpenBillingDraft
{
public function __construct(
private BillingExportRepository $exports,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @return int The draft's id.
*
* @throws DraftAlreadyOpen
*/
public function execute(int $actorUserId): int
{
return $this->transactions->run(function () use ($actorUserId): int {
if ($this->exports->openDraftId() !== null) {
throw new DraftAlreadyOpen;
}
return (int) $this->exports->save(BillingExport::startDraft($actorUserId, $this->clock->now()))->id();
});
}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\JobNotInDraft;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Takes a job off the draft; an invoice left without jobs goes too.
*/
final class RemoveJobFromDraft
{
use ChangesDraft;
public function __construct(BillingExportRepository $exports, TransactionManager $transactions)
{
$this->exports = $exports;
$this->transactions = $transactions;
}
/**
* @throws BillingExportNotFound
* @throws ExportNotDraft
* @throws JobNotInDraft
*/
public function execute(int $exportId, int $jobId): void
{
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId): void {
$export->removeJob($jobId);
});
}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\JobNotInDraft;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Moves a job off a combined invoice to an invoice of its own.
*/
final class SeparateJob
{
use ChangesDraft;
public function __construct(BillingExportRepository $exports, TransactionManager $transactions)
{
$this->exports = $exports;
$this->transactions = $transactions;
}
/**
* @throws BillingExportNotFound
* @throws ExportNotDraft
* @throws JobNotInDraft
*/
public function execute(int $exportId, int $jobId): void
{
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId): void {
$export->separate($jobId);
});
}
}
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\InvalidBillableHours;
use App\Modules\Billing\Domain\Exceptions\JobNotInDraft;
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Sets the hours management bills for a work type of a job (spec 12); an
* empty value returns to the approved hours. Both are kept on the line.
*/
final class SetBillableHours
{
use ChangesDraft;
public function __construct(BillingExportRepository $exports, TransactionManager $transactions)
{
$this->exports = $exports;
$this->transactions = $transactions;
}
/**
* @param string $hours E.g. "12,5"; empty for the approved hours.
*
* @throws BillingExportNotFound
* @throws ExportNotDraft
* @throws JobNotInDraft
* @throws InvalidBillableHours
*/
public function execute(int $exportId, int $jobId, int $workTypeId, string $hours): void
{
$quarters = DraftJob::parseHours($hours);
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId, $workTypeId, $quarters): void {
$export->setBillableQuarters($jobId, $workTypeId, $quarters);
});
}
}