Files
petriandClaude Sonnet 5.5 7d7b72989d Add an editable menu name and reserve billing material numbers on creation
- Company details get an optional "Valikon nimi", shown at the left of the
  main menu; it falls back to the application name when empty.
- A billing material gets its running number (billing_exports.number) only
  when it is created, so discarded drafts leave no gaps. Drafts have no
  number; existing created materials are numbered by the migration, and the
  CSV file name uses the number.
- TaskAssigneeMigrationTest rolls back from its own migration onward instead
  of assuming it is the last one.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-24 19:41:57 +02:00

186 lines
6.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\Services;
use App\Modules\Billing\Application\Contracts\BillingJobs;
use App\Modules\Billing\Application\DTOs\AvailableJob;
use App\Modules\Billing\Application\DTOs\BillableJob;
use App\Modules\Billing\Application\DTOs\ExportView;
use App\Modules\Billing\Application\DTOs\HoursRow;
use App\Modules\Billing\Application\DTOs\InvoiceJobView;
use App\Modules\Billing\Application\DTOs\InvoiceView;
use App\Modules\Billing\Application\DTOs\MergeTarget;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Entities\BillingInvoice;
use App\Modules\Billing\Domain\Enums\BillingMode;
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
use App\Modules\Billing\Domain\ValueObjects\InvoiceTotals;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
/**
* Builds a billing material's page: a draft is previewed with the hours
* and expenses the jobs have now, and a created material shows its lines.
*/
final class ExportViews
{
public function __construct(
private BillingExportRepository $exports,
private BillingJobs $jobs,
private BillingSnapshot $snapshots,
private UserAccountQueries $users,
) {}
public function of(int $exportId): ?ExportView
{
$export = $this->exports->findById($exportId);
if ($export === null) {
return null;
}
$candidates = $export->isDraft() ? $this->jobs->candidates() : [];
$snapshot = $this->snapshots->load($export->period(), $export->jobIds(), $candidates);
$onDraft = array_flip($export->jobIds());
$invoices = [];
foreach ($export->invoices() as $invoice) {
$invoices[] = $this->invoiceView($export, $invoice, $snapshot);
}
$available = [];
foreach ($candidates as $job) {
if (! isset($onDraft[$job->id])) {
$hours = $snapshot->hoursOf($job->id);
$available[] = new AvailableJob(
$job,
$hours->approvedQuarters(),
$hours->unapprovedQuarters,
count($snapshot->expensesOf($job->id)),
$snapshot->reasonNotBillable($job),
$job->mode === BillingMode::Hours ? $snapshot->missingRateNames($job) : [],
);
}
}
$names = $this->users->namesByIds(array_values(array_filter([$export->createdBy(), $export->finalizedBy()])));
return new ExportView(
(int) $export->id(),
$export->number(),
$export->status(),
$export->period(),
$export->createdAt(),
$names[$export->createdBy()] ?? '',
$export->finalizedAt(),
$export->finalizedBy() === null ? null : ($names[$export->finalizedBy()] ?? ''),
$invoices,
InvoiceTotals::sum(array_map(fn (InvoiceView $invoice): InvoiceTotals => $invoice->totals, $invoices)),
$available,
);
}
private function invoiceView(BillingExport $export, BillingInvoice $invoice, BillingSnapshot $snapshot): InvoiceView
{
$jobViews = [];
$lines = [];
$mergeTargets = [];
foreach ($invoice->jobs() as $draftJob) {
$job = $snapshot->jobs[$draftJob->jobId] ?? null;
$view = $export->isDraft()
? $this->draftJobView($draftJob, $job, $snapshot)
: $this->createdJobView($draftJob, $job, $invoice->lines());
$jobViews[] = $view;
array_push($lines, ...$view->lines);
if ($export->isDraft()) {
$mergeTargets[$draftJob->jobId] = $this->mergeTargets($export, $invoice, $draftJob, $snapshot);
}
}
$first = $snapshot->jobs[$invoice->key()] ?? null;
return new InvoiceView(
$invoice->key(),
$first->customerName ?? '',
$first?->billingUnitName,
$invoice->reference(),
$jobViews,
InvoiceTotals::of($lines),
$mergeTargets,
);
}
private function draftJobView(DraftJob $draftJob, ?BillableJob $job, BillingSnapshot $snapshot): InvoiceJobView
{
if ($job === null) {
return new InvoiceJobView($draftJob->jobId, '', '', null, [], [], 0, null);
}
$hours = $snapshot->hoursOf($job->id);
$lines = $snapshot->linesOf($job, $draftJob, $hours->approvedByWorkType, $snapshot->expensesOf($job->id));
$rows = [];
foreach ($snapshot->rules->sortedByName($hours->approvedByWorkType, $snapshot->rates) as $workTypeId => $approved) {
$rate = $snapshot->rates[$workTypeId] ?? null;
$billable = $draftJob->billableOf($workTypeId, $approved);
$line = $rate?->rateCents === null ? null : InvoiceLine::hours($job->id, $workTypeId, $rate->name, $approved, $billable, $rate->rateCents, $snapshot->vatBasisPoints);
$rows[] = new HoursRow($workTypeId, $rate->name ?? '', $approved, $billable, isset($draftJob->billableQuarters[$workTypeId]), $rate?->rateCents, $line->netCents ?? 0);
}
return new InvoiceJobView(
$job->id,
$job->number,
$job->name,
$job->mode,
$rows,
$lines,
$hours->unapprovedQuarters,
$snapshot->reasonNotBillable($job),
$job->mode === BillingMode::Hours ? $snapshot->missingRateNames($job) : [],
);
}
/**
* @param list<InvoiceLine> $invoiceLines
*/
private function createdJobView(DraftJob $draftJob, ?BillableJob $job, array $invoiceLines): InvoiceJobView
{
return new InvoiceJobView(
$draftJob->jobId,
$job->number ?? '',
$job->name ?? '',
$job?->mode,
[],
array_values(array_filter($invoiceLines, fn (InvoiceLine $line): bool => $line->jobId === $draftJob->jobId)),
0,
null,
);
}
/**
* @return list<MergeTarget>
*/
private function mergeTargets(BillingExport $export, BillingInvoice $current, DraftJob $job, BillingSnapshot $snapshot): array
{
$targets = [];
foreach ($export->invoices() as $invoice) {
if ($invoice === $current || $invoice->refusalFor($job) !== null) {
continue;
}
$labels = array_map(fn (DraftJob $other): string => ($snapshot->jobs[$other->jobId] ?? null)?->label() ?? (string) $other->jobId, $invoice->jobs());
$targets[] = new MergeTarget($invoice->key(), implode(', ', $labels));
}
return $targets;
}
}