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

98 lines
3.4 KiB
PHP

<?php
namespace App\Modules\Expenses\Infrastructure\Persistence\Repositories;
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
use App\Modules\Expenses\Application\DTOs\ExpenseJob;
use App\Modules\Expenses\Application\DTOs\ExpenseSummary;
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
class EloquentExpenseQueries implements ExpenseQueries
{
public function __construct(
private ExpenseJobs $jobs,
private UserAccountQueries $users,
) {}
public function forJob(int $jobId): array
{
return $this->summaries(ExpenseModel::query()->where('customer_job_id', $jobId));
}
public function forUserInMonth(int $userId, string $month): array
{
if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1) {
return [];
}
$start = Carbon::createFromFormat('!Y-m', $month);
return $this->summaries(ExpenseModel::query()
->where('user_id', $userId)
->whereBetween('spent_on', [$start->toDateString(), $start->copy()->endOfMonth()->toDateString()]));
}
public function find(int $id): ?ExpenseSummary
{
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>
*/
private function summaries(Builder $query): array
{
/** @var Collection<int, ExpenseModel> $models */
$models = $query->orderByDesc('spent_on')->orderByDesc('id')->get();
if ($models->isEmpty()) {
return [];
}
$jobs = $this->jobs->findMany($models->pluck('customer_job_id')->unique()->values()->all());
$names = $this->users->namesByIds($models->pluck('user_id')->merge($models->pluck('recorded_by'))->unique()->values()->all());
return $models
->map(fn (ExpenseModel $model): ExpenseSummary => new ExpenseSummary(
$model->id,
$jobs[$model->customer_job_id] ?? new ExpenseJob($model->customer_job_id, '', '', true),
$model->user_id,
$names[$model->user_id] ?? '',
$model->recorded_by,
$names[$model->recorded_by] ?? '',
$model->category,
$model->paid_by,
$model->spent_on->toDateString(),
$model->description,
$model->cost_cents,
$model->kilometers === null ? null : (int) round((float) $model->kilometers * 10),
$model->destination,
$model->purpose,
$model->is_billable,
$model->markup_basis_points,
$model->billed_amount_cents,
))
->values()
->all();
}
}