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:
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\Contracts;
|
||||
|
||||
use App\Modules\Billing\Application\DTOs\CustomerBillingDetails;
|
||||
|
||||
/**
|
||||
* Customers' invoicing details for billing files, from the Customers module.
|
||||
*/
|
||||
interface BillingCustomers
|
||||
{
|
||||
/**
|
||||
* @param bool $withIdentityCode Whether a consumer's full personal identity code is included; the caller checks the permission.
|
||||
*/
|
||||
public function billingDetails(int $customerId, ?int $billingUnitId, bool $withIdentityCode): CustomerBillingDetails;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\DTOs;
|
||||
|
||||
/**
|
||||
* Who an invoice goes to and where. The personal identity code of a
|
||||
* consumer is sensitive: it is filled in only when the caller may see it,
|
||||
* and never logged or shown on a page.
|
||||
*/
|
||||
final readonly class CustomerBillingDetails
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public bool $isConsumer,
|
||||
public ?string $businessId,
|
||||
public ?string $personalIdentityCode,
|
||||
public ?string $billingUnitName,
|
||||
public ?string $billingAddress,
|
||||
public ?string $eInvoiceAddress,
|
||||
public ?string $eInvoiceOperator,
|
||||
public ?string $billingUnitReference,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\DTOs;
|
||||
|
||||
use App\Modules\Billing\Domain\Enums\LineKind;
|
||||
|
||||
/**
|
||||
* One invoice line of a created billing material for a billing file, with
|
||||
* its invoice's customer details. Amounts are in cents; the quantity is in
|
||||
* quarters of an hour for hours, otherwise in pieces (a month is one).
|
||||
*/
|
||||
final readonly class ExportRow
|
||||
{
|
||||
public function __construct(
|
||||
public int $invoiceNumber,
|
||||
public CustomerBillingDetails $customer,
|
||||
public ?string $reference,
|
||||
public string $jobNumber,
|
||||
public string $jobName,
|
||||
public LineKind $kind,
|
||||
public string $description,
|
||||
public ?int $recordedQuarters,
|
||||
public ?int $quantityQuarters,
|
||||
public int $unitPriceCents,
|
||||
public int $vatBasisPoints,
|
||||
public int $netCents,
|
||||
public int $vatCents,
|
||||
) {}
|
||||
|
||||
public function grossCents(): int
|
||||
{
|
||||
return $this->netCents + $this->vatCents;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\Services;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingCustomers;
|
||||
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
||||
use App\Modules\Billing\Application\DTOs\ExportRow;
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
|
||||
use App\Modules\Billing\Domain\Exceptions\ExportNotCreated;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
|
||||
|
||||
/**
|
||||
* The lines of a created billing material for a billing file, in the
|
||||
* order of its invoices, each with its invoice's customer details. A
|
||||
* line's VAT is counted from the line (rounded half up), so an invoice's
|
||||
* VAT summed from its lines may differ by a cent from the page, which
|
||||
* counts it from each rate's total.
|
||||
*/
|
||||
final class ExportRows
|
||||
{
|
||||
public function __construct(
|
||||
private BillingExportRepository $exports,
|
||||
private BillingJobs $jobs,
|
||||
private BillingCustomers $customers,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param bool $withIdentityCodes Whether consumers' personal identity codes are included; the caller checks the permission.
|
||||
* @return list<ExportRow>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ExportNotCreated extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The billing material is still a draft.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingCustomers;
|
||||
use App\Modules\Billing\Application\DTOs\CustomerBillingDetails;
|
||||
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
||||
use App\Modules\Customers\Domain\Enums\CustomerType;
|
||||
|
||||
/**
|
||||
* Customers' invoicing details through the Customers module's queries.
|
||||
*/
|
||||
class CustomersForBilling implements BillingCustomers
|
||||
{
|
||||
public function __construct(private CustomerQueries $customers) {}
|
||||
|
||||
public function billingDetails(int $customerId, ?int $billingUnitId, bool $withIdentityCode): CustomerBillingDetails
|
||||
{
|
||||
$customer = $this->customers->findSummary($customerId);
|
||||
$isConsumer = $customer?->type === CustomerType::Consumer;
|
||||
$unit = $billingUnitId === null ? null : $this->customers->findBillingUnit($customerId, $billingUnitId);
|
||||
|
||||
return new CustomerBillingDetails(
|
||||
$customer->name ?? '',
|
||||
$isConsumer,
|
||||
$customer?->businessId,
|
||||
$isConsumer && $withIdentityCode ? $this->customers->personalIdentityCodeOf($customerId) : null,
|
||||
$unit?->name,
|
||||
$unit?->billingAddress,
|
||||
$unit?->eInvoiceAddress,
|
||||
$unit?->eInvoiceOperator,
|
||||
$unit?->reference,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,14 @@
|
||||
namespace App\Modules\Billing\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Billing\Application\Contracts\BillingCustomers;
|
||||
use App\Modules\Billing\Application\Contracts\BillingExpenses;
|
||||
use App\Modules\Billing\Application\Contracts\BillingHours;
|
||||
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
||||
use App\Modules\Billing\Application\Contracts\BillingPrices;
|
||||
use App\Modules\Billing\Application\Contracts\BillingQueries;
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Infrastructure\CustomersForBilling;
|
||||
use App\Modules\Billing\Infrastructure\ExpensesForBilling;
|
||||
use App\Modules\Billing\Infrastructure\HoursForBilling;
|
||||
use App\Modules\Billing\Infrastructure\JobsForBilling;
|
||||
@@ -36,6 +38,7 @@ class BillingServiceProvider extends ServiceProvider
|
||||
BillingHours::class => HoursForBilling::class,
|
||||
BillingExpenses::class => ExpensesForBilling::class,
|
||||
BillingPrices::class => PricesForBilling::class,
|
||||
BillingCustomers::class => CustomersForBilling::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
|
||||
@@ -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'), ',');
|
||||
}
|
||||
}
|
||||
@@ -321,6 +321,13 @@ new class extends Component
|
||||
<p class="text-sm font-medium">
|
||||
{{ __('billing.export.summary', ['invoices' => count($export->invoices), 'jobs' => $export->jobCount(), 'net' => $this->money($export->totals->netCents)]) }}
|
||||
</p>
|
||||
|
||||
@if (! $export->isDraft())
|
||||
<div class="space-y-2">
|
||||
<x-button :href="route('billing.csv', $export->id)" icon="arrow-down-tray" :text="__('billing.csv.download')" />
|
||||
<p class="max-w-3xl text-sm">{{ __('billing.csv.sensitive') }}</p>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<p id="billing-status" tabindex="-1" role="status" aria-live="polite" class="text-sm font-medium focus:outline-none">{{ $statusMessage }}</p>
|
||||
|
||||
@@ -74,6 +74,7 @@ new class extends Component
|
||||
['index' => 'invoices', 'label' => __('billing.index.columns.invoices'), 'sortable' => false, 'align' => 'right'],
|
||||
['index' => 'jobs', 'label' => __('billing.index.columns.jobs'), 'sortable' => false, 'align' => 'right'],
|
||||
['index' => 'net', 'label' => __('billing.index.columns.net'), 'sortable' => false, 'align' => 'right'],
|
||||
['index' => 'file', 'label' => __('billing.index.columns.file'), 'sortable' => false],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -138,6 +139,12 @@ new class extends Component
|
||||
@interact('column_net', $export)
|
||||
<span class="whitespace-nowrap">{{ $export->netCents === null ? '' : $this->money($export->netCents) }}</span>
|
||||
@endinteract
|
||||
|
||||
@interact('column_file', $export)
|
||||
@if ($export->status->value === 'created')
|
||||
<x-link :href="route('billing.csv', $export->id)" icon="arrow-down-tray" :aria-label="__('billing.csv.download_named', ['number' => $export->id])">{{ __('billing.csv.download_short') }}</x-link>
|
||||
@endif
|
||||
@endinteract
|
||||
</x-table>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Billing\Presentation\Http\Controllers\DownloadBillingCsv;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::middleware(['web', 'auth', 'can:billing.manage'])
|
||||
@@ -8,4 +9,5 @@ Route::middleware(['web', 'auth', 'can:billing.manage'])
|
||||
->group(function () {
|
||||
Route::livewire('/', 'billing::index')->name('index');
|
||||
Route::livewire('/{export}', 'billing::export')->whereNumber('export')->name('export');
|
||||
Route::get('/{export}/csv', DownloadBillingCsv::class)->whereNumber('export')->name('csv');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user