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
@@ -25,4 +25,12 @@ interface ExpenseQueries
public function forUserInMonth(int $userId, string $month): array;
public function find(int $id): ?ExpenseSummary;
/**
* The jobs' billable expenses that are not billed yet, oldest first.
*
* @param list<int> $jobIds
* @return list<ExpenseSummary>
*/
public function unbilledBillable(array $jobIds): array;
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Modules\Expenses\Application\UseCases;
use App\Modules\Expenses\Domain\Contracts\ExpenseRepository;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Marks a job's billable, unbilled expenses billed on a billing material,
* so they are never billed twice and can no longer be changed. The caller
* (billing) checks who may do this.
*/
final class MarkJobExpensesBilled
{
public function __construct(
private ExpenseRepository $expenses,
private TransactionManager $transactions,
) {}
/**
* @return list<int> Ids of the expenses marked billed.
*/
public function execute(int $jobId, int $billingExportId): array
{
return $this->transactions->run(fn (): array => $this->expenses->markBillableAsBilled($jobId, $billingExportId));
}
}
@@ -30,7 +30,7 @@ final class RemoveExpense
$this->transactions->run(function () use ($expenseId): void {
$expense = $this->expenses->findById($expenseId) ?? throw new ExpenseNotFound($expenseId);
if ($this->jobs->find($expense->jobId())->isLocked ?? true) {
if ($expense->isBilled() || ($this->jobs->find($expense->jobId())->isLocked ?? true)) {
throw new ExpenseLocked;
}
@@ -36,7 +36,7 @@ final class UpdateExpense
$this->transactions->run(function () use ($expenseId, $data, $details): void {
$expense = $this->expenses->findById($expenseId) ?? throw new ExpenseNotFound($expenseId);
if ($this->jobs->find($expense->jobId())->isLocked ?? true) {
if ($expense->isBilled() || ($this->jobs->find($expense->jobId())->isLocked ?? true)) {
throw new ExpenseLocked;
}
@@ -16,4 +16,11 @@ interface ExpenseRepository
public function save(Expense $expense): Expense;
public function delete(Expense $expense): void;
/**
* Marks the job's billable, unbilled expenses billed on a billing material.
*
* @return list<int> Ids of the expenses marked.
*/
public function markBillableAsBilled(int $jobId, int $billingExportId): array;
}
@@ -8,7 +8,8 @@ use App\Modules\Expenses\Domain\ValueObjects\ExpenseDetails;
/**
* An expense on a customer job: whose it is, who recorded it (management
* records subcontractors' expenses) and what it is.
* records subcontractors' expenses) and what it is. A billed expense (on a
* billing material) can no longer be changed or removed.
*/
final class Expense
{
@@ -18,6 +19,7 @@ final class Expense
private int $userId,
private int $recordedBy,
private ExpenseDetails $details,
private ?int $billingExportId = null,
) {}
public static function record(int $jobId, int $userId, int $recordedBy, ExpenseDetails $details): self
@@ -25,9 +27,19 @@ final class Expense
return new self(null, $jobId, $userId, $recordedBy, $details);
}
public static function reconstitute(int $id, int $jobId, int $userId, int $recordedBy, ExpenseDetails $details): self
public static function reconstitute(int $id, int $jobId, int $userId, int $recordedBy, ExpenseDetails $details, ?int $billingExportId = null): self
{
return new self($id, $jobId, $userId, $recordedBy, $details);
return new self($id, $jobId, $userId, $recordedBy, $details, $billingExportId);
}
public function isBilled(): bool
{
return $this->billingExportId !== null;
}
public function billingExportId(): ?int
{
return $this->billingExportId;
}
public function change(int $userId, ExpenseDetails $details): void
@@ -30,6 +30,7 @@ use Illuminate\Support\Carbon;
* @property bool $is_billable
* @property int|null $markup_basis_points
* @property int|null $billed_amount_cents
* @property int|null $billing_export_id
*/
#[Table('expenses')]
#[Fillable([
@@ -42,6 +42,19 @@ class EloquentExpenseQueries implements ExpenseQueries
return $this->summaries(ExpenseModel::query()->whereKey($id))[0] ?? null;
}
public function unbilledBillable(array $jobIds): array
{
if ($jobIds === []) {
return [];
}
return array_reverse($this->summaries(ExpenseModel::query()
->whereIn('customer_job_id', $jobIds)
->where('is_billable', true)
->whereNotNull('billed_amount_cents')
->whereNull('billing_export_id')));
}
/**
* @param Builder<ExpenseModel> $query
* @return list<ExpenseSummary>
@@ -51,6 +51,24 @@ class EloquentExpenseRepository implements ExpenseRepository
ExpenseModel::query()->whereKey($expense->id())->delete();
}
public function markBillableAsBilled(int $jobId, int $billingExportId): array
{
$ids = ExpenseModel::query()
->where('customer_job_id', $jobId)
->where('is_billable', true)
->whereNotNull('billed_amount_cents')
->whereNull('billing_export_id')
->orderBy('spent_on')
->orderBy('id')
->pluck('id')
->map(fn ($id): int => (int) $id)
->all();
ExpenseModel::query()->whereKey($ids)->update(['billing_export_id' => $billingExportId]);
return $ids;
}
private function toEntity(ExpenseModel $model): Expense
{
$billing = $model->is_billable && $model->billed_amount_cents !== null
@@ -77,6 +95,7 @@ class EloquentExpenseRepository implements ExpenseRepository
$model->purpose,
$billing,
),
$model->billing_export_id,
);
}
}