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->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 $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 */ 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; } }