Add a CSV download of created billing materials

Management downloads a created billing material as a semicolon CSV with
decimal commas and a UTF-8 BOM for Excel: one row per invoice line with
the invoice's customer, billing unit, e-invoice details and reference,
the job, the line and its net, VAT and gross amounts. Consumers' full
personal identity codes are included only for those who may see them,
the response is not cached, and cells Excel would read as formulas are
escaped. The columns follow Fennoa's import format once a sample exists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 13:32:46 +02:00
co-authored by Claude Opus 5.5
parent 5f9423a3c3
commit ae7b611126
16 changed files with 520 additions and 3 deletions
@@ -0,0 +1,117 @@
<?php
namespace App\Modules\Billing\Presentation\Http\Controllers;
use App\Modules\Billing\Application\DTOs\ExportRow;
use App\Modules\Billing\Application\Services\ExportRows;
use App\Modules\Billing\Domain\Enums\LineKind;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotCreated;
use Illuminate\Support\Facades\Gate;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Downloads a created billing material as a CSV file for Excel and the
* invoicing program: one row per invoice line, semicolons, decimal
* commas and a UTF-8 BOM. Consumers' personal identity codes are included
* for those who may see them, so the response is never cached.
*/
class DownloadBillingCsv
{
public function __invoke(int $export, ExportRows $exportRows): StreamedResponse
{
Gate::authorize('billing.manage');
try {
$rows = $exportRows->of($export, Gate::allows('customers.view-personal-identity-code'));
} catch (BillingExportNotFound|ExportNotCreated) {
abort(404);
}
return response()->streamDownload(function () use ($rows): void {
$out = fopen('php://output', 'w');
if ($out === false) {
return;
}
fwrite($out, "\u{FEFF}");
fputcsv($out, array_values(__('billing.csv.columns')), ';', '"', '');
foreach ($rows as $row) {
fputcsv($out, array_map(self::cell(...), $this->cells($row)), ';', '"', '');
}
fclose($out);
}, __('billing.csv.file', ['number' => $export]), [
'Content-Type' => 'text/csv; charset=UTF-8',
'Cache-Control' => 'no-store, private',
]);
}
/**
* @return list<string>
*/
private function cells(ExportRow $row): array
{
$customer = $row->customer;
return [
(string) $row->invoiceNumber,
$customer->name,
__($customer->isConsumer ? 'billing.csv.consumer' : 'billing.csv.company'),
(string) $customer->businessId,
(string) $customer->personalIdentityCode,
(string) $customer->billingUnitName,
(string) $customer->billingAddress,
(string) $customer->eInvoiceAddress,
(string) $customer->eInvoiceOperator,
(string) $row->reference,
$row->jobNumber,
$row->jobName,
__('billing.kinds.'.$row->kind->value),
$row->description,
$row->recordedQuarters === null ? '' : self::hours($row->recordedQuarters),
$row->quantityQuarters === null ? '1' : self::hours($row->quantityQuarters),
__('billing.csv.units.'.match ($row->kind) {
LineKind::Hours => 'hours',
LineKind::MonthlyFee => 'month',
default => 'piece',
}),
self::money($row->unitPriceCents),
self::decimal($row->vatBasisPoints, 100),
self::money($row->netCents),
self::money($row->vatCents),
self::money($row->grossCents()),
];
}
/**
* Guards against formula injection: a cell that Excel would read as a
* formula gets a leading apostrophe.
*/
public static function cell(string $value): string
{
return preg_match('/^[=+\-@\t\r\n]/', $value) === 1 ? "'".$value : $value;
}
private static function money(int $cents): string
{
return ($cents < 0 ? '-' : '').intdiv(abs($cents), 100).','.str_pad((string) (abs($cents) % 100), 2, '0', STR_PAD_LEFT);
}
private static function hours(int $quarters): string
{
return self::decimal($quarters * 25, 100);
}
/**
* E.g. 2550 / 100 → "25,5", 1000 / 100 → "10".
*/
private static function decimal(int $value, int $divisor): string
{
$text = number_format($value / $divisor, 2, ',', '');
return rtrim(rtrim($text, '0'), ',');
}
}