* * @throws BillingExportNotFound * @throws ExportNotCreated */ public function of(int $exportId, bool $withIdentityCodes): array { $export = $this->exports->findById($exportId) ?? throw new BillingExportNotFound($exportId); if ($export->isDraft()) { throw new ExportNotCreated; } $jobs = $this->jobs->find($export->jobIds()); $rows = []; foreach ($export->invoices() as $index => $invoice) { $customer = $this->customers->billingDetails($invoice->customerId(), $invoice->billingUnitId(), $withIdentityCodes); $reference = $invoice->reference() ?? $customer->billingUnitReference; foreach ($invoice->lines() as $line) { $job = $jobs[$line->jobId] ?? null; $vat = Money::fromCents($line->netCents)->addVat(new VatPercentage($line->vatBasisPoints))->cents - $line->netCents; $rows[] = new ExportRow( $index + 1, $customer, $reference, $job->number ?? '', $job->name ?? '', $line->kind, $line->description, $line->recordedQuarters, $line->quantityQuarters, $line->unitPriceCents, $line->vatBasisPoints, $line->netCents, $vat, ); } } return $rows; } }