Add the quick sales report

Quick sales of a period by cash day, product, seller and VAT rate, with
credits, open cash days flagged, a chart by day or month and CSV. VAT is
taken out of each day's total of a rate, as on the cash day balance.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 08:44:20 +02:00
co-authored by Claude Sonnet 5.5
parent a57b383e4d
commit 568871c9dc
19 changed files with 748 additions and 2 deletions
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\Contracts;
use App\Modules\Reports\Application\DTOs\SalesData;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
/**
* Quick sales summed by day, product, seller and VAT rate, from the Sales module.
*/
interface ReportSales
{
public function between(ReportPeriod $period): SalesData;
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
/**
* Quick sales of a period as the Sales module sums them, amounts with VAT
* in cents and credits negative.
*/
final readonly class SalesData
{
/**
* @param list<array{day: string, count: int, cashCents: int, cardCents: int, isClosed: bool}> $days
* @param list<array{name: string, quantity: int, grossCents: int}> $products
* @param list<array{sellerId: int, count: int, grossCents: int}> $sellers
* @param list<array{day: string, vatBasisPoints: int, grossCents: int}> $dayVatTotals
*/
public function __construct(
public array $days,
public array $products,
public array $sellers,
public array $dayVatTotals,
) {}
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
/**
* Quick sales of a period (spec 13), amounts with VAT in cents and
* credits negative: by cash day, product, seller (by name) and VAT rate,
* the totals, and a chart series by day or, for longer periods, by month
* (label Y-m-d or Y-m → cents).
*/
final readonly class SalesReport
{
/**
* @param list<array{day: string, count: int, cashCents: int, cardCents: int, totalCents: int, isClosed: bool}> $days
* @param list<array{name: string, quantity: int, grossCents: int}> $products
* @param list<array{name: string, count: int, grossCents: int}> $sellers
* @param list<array{vatBasisPoints: int, grossCents: int, netCents: int, vatCents: int}> $vatTotals
* @param array<string, int> $series
*/
public function __construct(
public array $days,
public array $products,
public array $sellers,
public array $vatTotals,
public int $count,
public int $cashCents,
public int $cardCents,
public int $openDays,
public array $series,
public bool $seriesByMonth,
) {}
public function totalCents(): int
{
return $this->cashCents + $this->cardCents;
}
}
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\UseCases;
use App\Modules\Reports\Application\Contracts\ReportPeople;
use App\Modules\Reports\Application\Contracts\ReportSales;
use App\Modules\Reports\Application\DTOs\SalesReport;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
use App\Modules\Shared\Domain\ValueObjects\Money;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
/**
* Quick sales in a period (spec 7, 13). The VAT of a rate is taken out of
* each day's total of the rate, as on the cash day balance, and summed
* over the days, so the report matches the closed days' balances.
*/
final readonly class QuickSalesReport
{
/** Periods longer than this are charted by month. */
private const int DAILY_CHART_DAYS = 62;
public function __construct(
private ReportSales $sales,
private ReportPeople $people,
) {}
public function execute(ReportPeriod $period): SalesReport
{
$data = $this->sales->between($period);
$days = array_map(fn (array $day): array => [...$day, 'totalCents' => $day['cashCents'] + $day['cardCents']], $data->days);
$names = $this->people->namesByIds(array_map(fn (array $seller): int => $seller['sellerId'], $data->sellers));
$sellers = array_map(fn (array $seller): array => [
'name' => $names[$seller['sellerId']] ?? '',
'count' => $seller['count'],
'grossCents' => $seller['grossCents'],
], $data->sellers);
usort($sellers, fn (array $a, array $b): int => strnatcasecmp($a['name'], $b['name']));
$byRate = [];
foreach ($data->dayVatTotals as $total) {
$gross = $total['grossCents'];
$net = Money::fromCents(abs($gross))->removeVat(new VatPercentage($total['vatBasisPoints']))->cents;
$net = $gross < 0 ? -$net : $net;
$rate = $total['vatBasisPoints'];
$byRate[$rate] ??= ['vatBasisPoints' => $rate, 'grossCents' => 0, 'netCents' => 0, 'vatCents' => 0];
$byRate[$rate]['grossCents'] += $gross;
$byRate[$rate]['netCents'] += $net;
$byRate[$rate]['vatCents'] += $gross - $net;
}
krsort($byRate);
$byMonth = $period->dayCount() > self::DAILY_CHART_DAYS;
$series = [];
foreach ($days as $day) {
$key = $byMonth ? substr($day['day'], 0, 7) : $day['day'];
$series[$key] = ($series[$key] ?? 0) + $day['totalCents'];
}
return new SalesReport(
$days,
$data->products,
$sellers,
array_values($byRate),
array_sum(array_column($days, 'count')),
array_sum(array_column($days, 'cashCents')),
array_sum(array_column($days, 'cardCents')),
count(array_filter($days, fn (array $day): bool => ! $day['isClosed'])),
$series,
$byMonth,
);
}
}