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,40 @@
<?php
namespace App\Modules\Reports\Presentation\Csv;
use App\Modules\Reports\Application\UseCases\QuickSalesReport;
use Illuminate\Http\Request;
/**
* Quick sales as CSV: a row per cash day, amounts with VAT in cents.
*/
class SalesCsv implements ReportCsv
{
public function __construct(private QuickSalesReport $report) {}
public function fileName(Request $request): string
{
$period = CsvFilters::period($request);
return __('reports.sales.file').'-'.$period->from.'-'.$period->to;
}
public function headers(): array
{
return array_values(__('reports.sales.csv'));
}
public function rows(Request $request): iterable
{
foreach ($this->report->execute(CsvFilters::period($request))->days as $day) {
yield [
$day['day'],
$day['count'],
$day['cashCents'],
$day['cardCents'],
$day['totalCents'],
__($day['isClosed'] ? 'reports.yes' : 'reports.no'),
];
}
}
}
@@ -5,6 +5,7 @@ namespace App\Modules\Reports\Presentation\Http\Controllers;
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
use App\Modules\Reports\Presentation\Csv\HoursCsv;
use App\Modules\Reports\Presentation\Csv\ReportCsv;
use App\Modules\Reports\Presentation\Csv\SalesCsv;
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
@@ -19,7 +20,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
class DownloadReport
{
/** The reports that have a CSV, by their URL key. */
public const array REPORTS = ['hours', 'unbilled'];
public const array REPORTS = ['hours', 'unbilled', 'sales'];
public function __invoke(string $report, Request $request): StreamedResponse
{
@@ -73,6 +74,7 @@ class DownloadReport
return match ($report) {
'hours' => app(HoursCsv::class),
'unbilled' => app(UnbilledCsv::class),
'sales' => app(SalesCsv::class),
default => abort(404),
};
}
@@ -89,7 +89,7 @@ trait ReportPage
*
* @param array<string, string> $filters
*/
protected function csvUrl(string $report, array $filters = []): string
public function csvUrl(string $report, array $filters = []): string
{
return route('reports.csv', array_filter(['report' => $report, 'alku' => $this->from, 'loppu' => $this->to, ...$filters], fn (string $value): bool => $value !== ''));
}
@@ -0,0 +1,219 @@
<?php
use App\Modules\Reports\Application\DTOs\SalesReport;
use App\Modules\Reports\Application\UseCases\QuickSalesReport;
use App\Modules\Reports\Presentation\Livewire\Concerns\ReportPage;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
use App\Modules\Users\Application\Contracts\Clock;
use Livewire\Attributes\Computed;
use Livewire\Component;
/**
* Quick sales in a period (spec 7, 13): by cash day, product, seller and
* VAT rate. They are counter sales apart from the billing material.
*/
new class extends Component
{
use ReportPage;
public function mount(Clock $clock): void
{
$this->startPeriod($clock);
}
#[Computed]
public function report(): ?SalesReport
{
$period = $this->period();
return $period === null ? null : app(QuickSalesReport::class)->execute($period);
}
public function updated(): void
{
$this->authorize('reports.view');
unset($this->report);
}
public function vatRate(int $basisPoints): string
{
return (new VatPercentage($basisPoints))->format();
}
public function seriesLabel(string $key): string
{
return strlen($key) === 7
? ((int) substr($key, 5, 2)).'/'.substr($key, 0, 4)
: $this->dateLabel($key);
}
public function render(): mixed
{
return $this->view()->title(__('reports.sales.title'));
}
};
?>
<div class="mx-auto max-w-6xl space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">{{ __('reports.sales.title') }}</h1>
<x-reports.period-filter :presets="$this->periodPresets()" :valid="$this->period !== null" />
@php($report = $this->report)
@if ($report !== null)
<div class="flex flex-wrap items-center justify-between gap-3">
<h2 class="text-xl font-semibold" role="status" aria-live="polite">{{ $this->periodLabel() }}</h2>
<x-button :href="$this->csvUrl('sales')" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
</div>
@if ($report->days === [])
<p>{{ __('reports.sales.none') }}</p>
@else
@if ($report->openDays > 0)
<p class="flex items-start gap-2 text-sm">
<x-icon name="exclamation-triangle" class="size-5 shrink-0" aria-hidden="true" />
{{ trans_choice('reports.sales.open_days', $report->openDays, ['count' => $report->openDays]) }}
</p>
@endif
<div class="grid gap-6 md:grid-cols-2">
<table class="w-full text-sm">
<caption class="mb-2 text-left text-base font-semibold">{{ __('reports.sales.totals') }}</caption>
<thead class="sr-only">
<tr>
<th scope="col">{{ __('reports.sales.item') }}</th>
<th scope="col">{{ __('reports.sales.amount') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
<tr>
<th scope="row" class="py-1 text-left font-normal">{{ __('reports.sales.count') }}</th>
<td class="py-1 text-right">{{ $report->count }}</td>
</tr>
<tr>
<th scope="row" class="py-1 text-left font-normal">{{ __('reports.sales.cash') }}</th>
<td class="py-1 text-right whitespace-nowrap">{{ $this->money($report->cashCents) }}</td>
</tr>
<tr>
<th scope="row" class="py-1 text-left font-normal">{{ __('reports.sales.card') }}</th>
<td class="py-1 text-right whitespace-nowrap">{{ $this->money($report->cardCents) }}</td>
</tr>
<tr>
<th scope="row" class="py-1 text-left font-semibold">{{ __('reports.sales.total') }}</th>
<td class="py-1 text-right font-semibold whitespace-nowrap">{{ $this->money($report->totalCents()) }}</td>
</tr>
</tbody>
</table>
<table class="w-full text-sm">
<caption class="mb-2 text-left text-base font-semibold">{{ __('reports.sales.by_vat') }}</caption>
<thead>
<tr class="text-left">
<th scope="col" class="py-1">{{ __('reports.sales.vat_rate') }}</th>
<th scope="col" class="py-1 text-right">{{ __('reports.sales.gross') }}</th>
<th scope="col" class="py-1 text-right">{{ __('reports.sales.net') }}</th>
<th scope="col" class="py-1 text-right">{{ __('reports.sales.vat') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->vatTotals as $vat)
<tr wire:key="vat-{{ $vat['vatBasisPoints'] }}">
<th scope="row" class="py-1 text-left font-normal">{{ $this->vatRate($vat['vatBasisPoints']) }}</th>
<td class="py-1 text-right whitespace-nowrap">{{ $this->money($vat['grossCents']) }}</td>
<td class="py-1 text-right whitespace-nowrap">{{ $this->money($vat['netCents']) }}</td>
<td class="py-1 text-right whitespace-nowrap">{{ $this->money($vat['vatCents']) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<x-reports.chart
:title="__($report->seriesByMonth ? 'reports.sales.chart_months' : 'reports.sales.chart_days')"
:summary="implode(', ', array_map(fn ($key, $cents) => $this->seriesLabel($key).' '.$this->money($cents), array_keys($report->series), $report->series))"
:labels="array_map(fn ($key) => $this->seriesLabel((string) $key), array_keys($report->series))"
:values="array_map(fn ($cents) => $cents / 100, array_values($report->series))"
:formatter="fn (float $value) => number_format($value, 0, ',', ' ').' €'"
/>
<div class="grid gap-6 md:grid-cols-2">
<table class="w-full text-sm">
<caption class="mb-2 text-left text-base font-semibold">{{ __('reports.sales.by_product') }}</caption>
<thead>
<tr class="text-left">
<th scope="col" class="py-1">{{ __('reports.sales.product') }}</th>
<th scope="col" class="py-1 text-right">{{ __('reports.sales.quantity') }}</th>
<th scope="col" class="py-1 text-right">{{ __('reports.sales.gross') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->products as $product)
<tr>
<th scope="row" class="py-1 text-left font-normal">{{ $product['name'] }}</th>
<td class="py-1 text-right">{{ $product['quantity'] }}</td>
<td class="py-1 text-right whitespace-nowrap">{{ $this->money($product['grossCents']) }}</td>
</tr>
@endforeach
</tbody>
</table>
<table class="w-full text-sm">
<caption class="mb-2 text-left text-base font-semibold">{{ __('reports.sales.by_seller') }}</caption>
<thead>
<tr class="text-left">
<th scope="col" class="py-1">{{ __('reports.sales.seller') }}</th>
<th scope="col" class="py-1 text-right">{{ __('reports.sales.count') }}</th>
<th scope="col" class="py-1 text-right">{{ __('reports.sales.gross') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->sellers as $seller)
<tr>
<th scope="row" class="py-1 text-left font-normal">{{ $seller['name'] }}</th>
<td class="py-1 text-right">{{ $seller['count'] }}</td>
<td class="py-1 text-right whitespace-nowrap">{{ $this->money($seller['grossCents']) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<caption class="mb-2 text-left text-base font-semibold">{{ __('reports.sales.by_day') }}</caption>
<thead>
<tr class="border-b border-zinc-300 text-left dark:border-zinc-600">
<th scope="col" class="py-2 pr-3">{{ __('reports.sales.day') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.sales.count') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.sales.cash') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.sales.card') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.sales.total') }}</th>
<th scope="col" class="py-2">{{ __('reports.sales.cash_day') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->days as $day)
<tr wire:key="day-{{ $day['day'] }}">
<th scope="row" class="py-2 pr-3 text-left font-normal">
<x-link :href="route('sales.index', ['paiva' => $day['day']])" navigate>{{ $this->dateLabel($day['day']) }}</x-link>
</th>
<td class="py-2 pr-3 text-right">{{ $day['count'] }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($day['cashCents']) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($day['cardCents']) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($day['totalCents']) }}</td>
<td class="py-2">
@if ($day['isClosed'])
<span class="inline-flex items-center gap-1"><x-icon name="lock-closed" class="size-4" aria-hidden="true" />{{ __('reports.sales.closed') }}</span>
@else
<span class="inline-flex items-center gap-1"><x-icon name="exclamation-triangle" class="size-4" aria-hidden="true" />{{ __('reports.sales.open') }}</span>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
@endif
</div>
@@ -9,5 +9,6 @@ Route::middleware(['web', 'auth', 'can:reports.view'])
->group(function () {
Route::livewire('/hours', 'reports::hours')->name('hours');
Route::livewire('/unbilled', 'reports::unbilled')->name('unbilled');
Route::livewire('/sales', 'reports::sales')->name('sales');
Route::get('/{report}/csv', DownloadReport::class)->whereIn('report', DownloadReport::REPORTS)->name('csv');
});