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

102 lines
3.7 KiB
PHP

<?php
namespace App\Modules\Expenses\Infrastructure\Persistence\Repositories;
use App\Modules\Expenses\Domain\Contracts\ExpenseRepository;
use App\Modules\Expenses\Domain\Entities\Expense;
use App\Modules\Expenses\Domain\ValueObjects\ExpenseBilling;
use App\Modules\Expenses\Domain\ValueObjects\ExpenseDetails;
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
use App\Modules\Expenses\Domain\ValueObjects\MarkupPercentage;
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
use App\Modules\Shared\Domain\ValueObjects\Money;
use DateTimeImmutable;
class EloquentExpenseRepository implements ExpenseRepository
{
public function findById(int $id): ?Expense
{
$model = ExpenseModel::query()->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function save(Expense $expense): Expense
{
$model = $expense->id() === null ? new ExpenseModel : ExpenseModel::query()->findOrFail($expense->id());
$details = $expense->details();
$model->fill([
'customer_job_id' => $expense->jobId(),
'user_id' => $expense->userId(),
'recorded_by' => $expense->recordedBy(),
'category' => $details->category,
'paid_by' => $details->paidBy,
'spent_on' => $details->spentOn->format('Y-m-d'),
'description' => $details->description,
'cost_cents' => $details->cost->cents,
'kilometers' => $details->kilometers === null ? null : $details->kilometers->tenths / 10,
'destination' => $details->destination,
'purpose' => $details->purpose,
'is_billable' => $details->isBillable(),
'markup_basis_points' => $details->billing?->markup?->basisPoints,
'billed_amount_cents' => $details->billing?->amount->cents,
])->save();
return $this->toEntity($model->refresh());
}
public function delete(Expense $expense): void
{
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
? ExpenseBilling::for(
Money::fromCents($model->cost_cents),
$model->markup_basis_points === null ? null : new MarkupPercentage($model->markup_basis_points),
Money::fromCents($model->billed_amount_cents),
)
: null;
return Expense::reconstitute(
$model->id,
$model->customer_job_id,
$model->user_id,
$model->recorded_by,
ExpenseDetails::reconstitute(
$model->category,
$model->paid_by,
DateTimeImmutable::createFromInterface($model->spent_on),
$model->description,
Money::fromCents($model->cost_cents),
$model->kilometers === null ? null : new Kilometers((int) round((float) $model->kilometers * 10)),
$model->destination,
$model->purpose,
$billing,
),
$model->billing_export_id,
);
}
}