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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user