diff --git a/app/Modules/Reports/Application/Contracts/ReportSales.php b/app/Modules/Reports/Application/Contracts/ReportSales.php new file mode 100644 index 0000000..b4fefef --- /dev/null +++ b/app/Modules/Reports/Application/Contracts/ReportSales.php @@ -0,0 +1,16 @@ + $days + * @param list $products + * @param list $sellers + * @param list $dayVatTotals + */ + public function __construct( + public array $days, + public array $products, + public array $sellers, + public array $dayVatTotals, + ) {} +} diff --git a/app/Modules/Reports/Application/DTOs/SalesReport.php b/app/Modules/Reports/Application/DTOs/SalesReport.php new file mode 100644 index 0000000..82981a3 --- /dev/null +++ b/app/Modules/Reports/Application/DTOs/SalesReport.php @@ -0,0 +1,39 @@ + $days + * @param list $products + * @param list $sellers + * @param list $vatTotals + * @param array $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; + } +} diff --git a/app/Modules/Reports/Application/UseCases/QuickSalesReport.php b/app/Modules/Reports/Application/UseCases/QuickSalesReport.php new file mode 100644 index 0000000..efdf18f --- /dev/null +++ b/app/Modules/Reports/Application/UseCases/QuickSalesReport.php @@ -0,0 +1,76 @@ +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, + ); + } +} diff --git a/app/Modules/Reports/Infrastructure/Adapters/SalesForReports.php b/app/Modules/Reports/Infrastructure/Adapters/SalesForReports.php new file mode 100644 index 0000000..5b10e69 --- /dev/null +++ b/app/Modules/Reports/Infrastructure/Adapters/SalesForReports.php @@ -0,0 +1,23 @@ +queries->between($period->from, $period->to); + + return new SalesData($sales->days, $sales->products, $sales->sellers, $sales->dayVatTotals); + } +} diff --git a/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php b/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php index 03e84d5..d523a06 100644 --- a/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php +++ b/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php @@ -7,10 +7,12 @@ use App\Modules\Reports\Application\Contracts\ReportCatalog; use App\Modules\Reports\Application\Contracts\ReportHours; use App\Modules\Reports\Application\Contracts\ReportJobs; use App\Modules\Reports\Application\Contracts\ReportPeople; +use App\Modules\Reports\Application\Contracts\ReportSales; use App\Modules\Reports\Infrastructure\Adapters\CatalogForReports; use App\Modules\Reports\Infrastructure\Adapters\HoursForReports; use App\Modules\Reports\Infrastructure\Adapters\JobsForReports; use App\Modules\Reports\Infrastructure\Adapters\PeopleForReports; +use App\Modules\Reports\Infrastructure\Adapters\SalesForReports; use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; use Livewire\Livewire; @@ -30,6 +32,7 @@ class ReportsServiceProvider extends ServiceProvider ReportJobs::class => JobsForReports::class, ReportCatalog::class => CatalogForReports::class, ReportPeople::class => PeopleForReports::class, + ReportSales::class => SalesForReports::class, ]; public function boot(): void diff --git a/app/Modules/Reports/Presentation/Csv/SalesCsv.php b/app/Modules/Reports/Presentation/Csv/SalesCsv.php new file mode 100644 index 0000000..1f8e371 --- /dev/null +++ b/app/Modules/Reports/Presentation/Csv/SalesCsv.php @@ -0,0 +1,40 @@ +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'), + ]; + } + } +} diff --git a/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php b/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php index 034d6b2..187775e 100644 --- a/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php +++ b/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php @@ -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), }; } diff --git a/app/Modules/Reports/Presentation/Livewire/Concerns/ReportPage.php b/app/Modules/Reports/Presentation/Livewire/Concerns/ReportPage.php index 0747f22..f457ad1 100644 --- a/app/Modules/Reports/Presentation/Livewire/Concerns/ReportPage.php +++ b/app/Modules/Reports/Presentation/Livewire/Concerns/ReportPage.php @@ -89,7 +89,7 @@ trait ReportPage * * @param array $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 !== '')); } diff --git a/app/Modules/Reports/Presentation/Livewire/⚡sales.blade.php b/app/Modules/Reports/Presentation/Livewire/⚡sales.blade.php new file mode 100644 index 0000000..d65c327 --- /dev/null +++ b/app/Modules/Reports/Presentation/Livewire/⚡sales.blade.php @@ -0,0 +1,219 @@ +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')); + } +}; +?> + +
+

{{ __('reports.sales.title') }}

+ + + + @php($report = $this->report) + + @if ($report !== null) +
+

{{ $this->periodLabel() }}

+ +
+ + @if ($report->days === []) +

{{ __('reports.sales.none') }}

+ @else + @if ($report->openDays > 0) +

+

+ @endif + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
{{ __('reports.sales.totals') }}
{{ __('reports.sales.item') }}{{ __('reports.sales.amount') }}
{{ __('reports.sales.count') }}{{ $report->count }}
{{ __('reports.sales.cash') }}{{ $this->money($report->cashCents) }}
{{ __('reports.sales.card') }}{{ $this->money($report->cardCents) }}
{{ __('reports.sales.total') }}{{ $this->money($report->totalCents()) }}
+ + + + + + + + + + + + + @foreach ($report->vatTotals as $vat) + + + + + + + @endforeach + +
{{ __('reports.sales.by_vat') }}
{{ __('reports.sales.vat_rate') }}{{ __('reports.sales.gross') }}{{ __('reports.sales.net') }}{{ __('reports.sales.vat') }}
{{ $this->vatRate($vat['vatBasisPoints']) }}{{ $this->money($vat['grossCents']) }}{{ $this->money($vat['netCents']) }}{{ $this->money($vat['vatCents']) }}
+
+ + + +
+ + + + + + + + + + + @foreach ($report->products as $product) + + + + + + @endforeach + +
{{ __('reports.sales.by_product') }}
{{ __('reports.sales.product') }}{{ __('reports.sales.quantity') }}{{ __('reports.sales.gross') }}
{{ $product['name'] }}{{ $product['quantity'] }}{{ $this->money($product['grossCents']) }}
+ + + + + + + + + + + + @foreach ($report->sellers as $seller) + + + + + + @endforeach + +
{{ __('reports.sales.by_seller') }}
{{ __('reports.sales.seller') }}{{ __('reports.sales.count') }}{{ __('reports.sales.gross') }}
{{ $seller['name'] }}{{ $seller['count'] }}{{ $this->money($seller['grossCents']) }}
+
+ +
+ + + + + + + + + + + + + + @foreach ($report->days as $day) + + + + + + + + + @endforeach + +
{{ __('reports.sales.by_day') }}
{{ __('reports.sales.day') }}{{ __('reports.sales.count') }}{{ __('reports.sales.cash') }}{{ __('reports.sales.card') }}{{ __('reports.sales.total') }}{{ __('reports.sales.cash_day') }}
+ {{ $this->dateLabel($day['day']) }} + {{ $day['count'] }}{{ $this->money($day['cashCents']) }}{{ $this->money($day['cardCents']) }}{{ $this->money($day['totalCents']) }} + @if ($day['isClosed']) + + @else + + @endif +
+
+ @endif + @endif +
diff --git a/app/Modules/Reports/Presentation/Routes/web.php b/app/Modules/Reports/Presentation/Routes/web.php index ea61224..df62328 100644 --- a/app/Modules/Reports/Presentation/Routes/web.php +++ b/app/Modules/Reports/Presentation/Routes/web.php @@ -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'); }); diff --git a/app/Modules/Sales/Application/Contracts/QuickSaleReportQueries.php b/app/Modules/Sales/Application/Contracts/QuickSaleReportQueries.php new file mode 100644 index 0000000..c4cb759 --- /dev/null +++ b/app/Modules/Sales/Application/Contracts/QuickSaleReportQueries.php @@ -0,0 +1,19 @@ + $days + * @param list $products + * @param list $sellers + * @param list $dayVatTotals + */ + public function __construct( + public array $days, + public array $products, + public array $sellers, + public array $dayVatTotals, + ) {} +} diff --git a/app/Modules/Sales/Infrastructure/Persistence/Repositories/EloquentQuickSaleReportQueries.php b/app/Modules/Sales/Infrastructure/Persistence/Repositories/EloquentQuickSaleReportQueries.php new file mode 100644 index 0000000..50a5462 --- /dev/null +++ b/app/Modules/Sales/Infrastructure/Persistence/Repositories/EloquentQuickSaleReportQueries.php @@ -0,0 +1,96 @@ +modify('+1 day')->format('Y-m-d'); + $closedDays = DB::table('cash_days') + ->whereNotNull('closed_at') + ->where('day', '>=', $from) + ->where('day', '<', $until) + ->pluck('day') + ->map(fn ($day): string => substr((string) $day, 0, 10)) + ->flip(); + + $days = DB::table('quick_sales') + ->where('sold_on', '>=', $from) + ->where('sold_on', '<', $until) + ->groupBy('sold_on') + ->orderBy('sold_on') + ->select('sold_on') + ->selectRaw('COUNT(*) as sale_count') + ->selectRaw("SUM(CASE WHEN payment_method = 'cash' THEN total_cents ELSE 0 END) as cash_cents") + ->selectRaw("SUM(CASE WHEN payment_method = 'cash' THEN 0 ELSE total_cents END) as card_cents") + ->get() + ->map(function (object $row) use ($closedDays): array { + $day = substr((string) $row->sold_on, 0, 10); + + return [ + 'day' => $day, + 'count' => (int) $row->sale_count, + 'cashCents' => (int) $row->cash_cents, + 'cardCents' => (int) $row->card_cents, + 'isClosed' => $closedDays->has($day), + ]; + }) + ->all(); + + $products = $this->lines($from, $until) + ->groupBy('quick_sale_lines.product_name') + ->select('quick_sale_lines.product_name') + ->selectRaw('SUM(quick_sale_lines.quantity) as quantity') + ->selectRaw('SUM(quick_sale_lines.quantity * quick_sale_lines.unit_price_cents) as gross_cents') + ->orderByDesc('gross_cents') + ->orderBy('quick_sale_lines.product_name') + ->get() + ->map(fn (object $row): array => ['name' => (string) $row->product_name, 'quantity' => (int) $row->quantity, 'grossCents' => (int) $row->gross_cents]) + ->all(); + + $sellers = DB::table('quick_sales') + ->where('sold_on', '>=', $from) + ->where('sold_on', '<', $until) + ->groupBy('user_id') + ->select('user_id') + ->selectRaw('COUNT(*) as sale_count') + ->selectRaw('SUM(total_cents) as gross_cents') + ->get() + ->map(fn (object $row): array => ['sellerId' => (int) $row->user_id, 'count' => (int) $row->sale_count, 'grossCents' => (int) $row->gross_cents]) + ->all(); + + $dayVatTotals = $this->lines($from, $until) + ->groupBy('quick_sales.sold_on', 'quick_sale_lines.vat_basis_points') + ->orderBy('quick_sales.sold_on') + ->orderBy('quick_sale_lines.vat_basis_points') + ->select('quick_sales.sold_on', 'quick_sale_lines.vat_basis_points') + ->selectRaw('SUM(quick_sale_lines.quantity * quick_sale_lines.unit_price_cents) as gross_cents') + ->get() + ->map(fn (object $row): array => [ + 'day' => substr((string) $row->sold_on, 0, 10), + 'vatBasisPoints' => (int) $row->vat_basis_points, + 'grossCents' => (int) $row->gross_cents, + ]) + ->all(); + + return new QuickSalesInPeriod(array_values($days), array_values($products), array_values($sellers), array_values($dayVatTotals)); + } + + private function lines(string $from, string $until): Builder + { + return DB::table('quick_sale_lines') + ->join('quick_sales', 'quick_sales.id', '=', 'quick_sale_lines.quick_sale_id') + ->where('quick_sales.sold_on', '>=', $from) + ->where('quick_sales.sold_on', '<', $until); + } +} diff --git a/app/Modules/Sales/Infrastructure/Providers/SalesServiceProvider.php b/app/Modules/Sales/Infrastructure/Providers/SalesServiceProvider.php index 66699d2..193e250 100644 --- a/app/Modules/Sales/Infrastructure/Providers/SalesServiceProvider.php +++ b/app/Modules/Sales/Infrastructure/Providers/SalesServiceProvider.php @@ -7,12 +7,14 @@ use App\Modules\Customers\Application\Contracts\CustomerReferences; use App\Modules\Sales\Application\Contracts\QuickSaleHours; use App\Modules\Sales\Application\Contracts\QuickSaleProducts; use App\Modules\Sales\Application\Contracts\QuickSaleQueries; +use App\Modules\Sales\Application\Contracts\QuickSaleReportQueries; use App\Modules\Sales\Domain\Contracts\CashDayRepository; use App\Modules\Sales\Domain\Contracts\QuickSaleRepository; use App\Modules\Sales\Infrastructure\CustomerReferencesFromSales; use App\Modules\Sales\Infrastructure\HoursForQuickSales; use App\Modules\Sales\Infrastructure\Persistence\Repositories\EloquentCashDayRepository; use App\Modules\Sales\Infrastructure\Persistence\Repositories\EloquentQuickSaleQueries; +use App\Modules\Sales\Infrastructure\Persistence\Repositories\EloquentQuickSaleReportQueries; use App\Modules\Sales\Infrastructure\Persistence\Repositories\EloquentQuickSaleRepository; use App\Modules\Sales\Infrastructure\ProductsFromPriceList; use App\Modules\Users\Domain\Enums\Role; @@ -34,6 +36,7 @@ class SalesServiceProvider extends ServiceProvider QuickSaleRepository::class => EloquentQuickSaleRepository::class, CashDayRepository::class => EloquentCashDayRepository::class, QuickSaleQueries::class => EloquentQuickSaleQueries::class, + QuickSaleReportQueries::class => EloquentQuickSaleReportQueries::class, QuickSaleProducts::class => ProductsFromPriceList::class, QuickSaleHours::class => HoursForQuickSales::class, ]; diff --git a/app/View/MainMenu.php b/app/View/MainMenu.php index 95fd5d1..34b265f 100644 --- a/app/View/MainMenu.php +++ b/app/View/MainMenu.php @@ -99,6 +99,7 @@ final class MainMenu 'items' => [ ['route' => 'reports.hours', 'pattern' => ['reports.hours'], 'label' => 'layout.report_hours', 'ability' => 'reports.view'], ['route' => 'reports.unbilled', 'pattern' => ['reports.unbilled'], 'label' => 'layout.report_unbilled', 'ability' => 'reports.view'], + ['route' => 'reports.sales', 'pattern' => ['reports.sales'], 'label' => 'layout.report_sales', 'ability' => 'reports.view'], ], ], 'maintenance' => [ diff --git a/lang/en/reports.php b/lang/en/reports.php index 7f72d2a..3931436 100644 --- a/lang/en/reports.php +++ b/lang/en/reports.php @@ -96,4 +96,43 @@ return [ 'oldest' => 'Oldest entry', ], ], + + 'sales' => [ + 'title' => 'Quick sales', + 'file' => 'quick-sales', + 'none' => 'There are no quick sales in the period.', + 'open_days' => '{1} One cash day is not closed, so its figures may still change.|[2,*] :count cash days are not closed, so their figures may still change.', + 'totals' => 'Totals (with VAT)', + 'item' => 'Item', + 'amount' => 'Amount', + 'count' => 'Sales', + 'cash' => 'Cash', + 'card' => 'Debit card', + 'total' => 'Total', + 'by_vat' => 'By VAT rate', + 'vat_rate' => 'VAT rate', + 'gross' => 'With VAT', + 'net' => 'Without VAT', + 'vat' => 'VAT', + 'chart_days' => 'Sales by day (with VAT)', + 'chart_months' => 'Sales by month (with VAT)', + 'by_product' => 'By product', + 'product' => 'Product', + 'quantity' => 'Quantity', + 'by_seller' => 'By seller', + 'seller' => 'Seller', + 'by_day' => 'By cash day', + 'day' => 'Day', + 'cash_day' => 'Cash day', + 'closed' => 'Closed', + 'open' => 'Not closed', + 'csv' => [ + 'day' => 'Day', + 'count' => 'Sales', + 'cash' => 'Cash (cents)', + 'card' => 'Debit card (cents)', + 'total' => 'Total (cents)', + 'closed' => 'Closed', + ], + ], ]; diff --git a/lang/fi/reports.php b/lang/fi/reports.php index 6e2f24c..84cca95 100644 --- a/lang/fi/reports.php +++ b/lang/fi/reports.php @@ -96,4 +96,43 @@ return [ 'oldest' => 'Vanhin kirjaus', ], ], + + 'sales' => [ + 'title' => 'Pikamyynnit', + 'file' => 'pikamyynnit', + 'none' => 'Aikavälillä ei ole pikamyyntejä.', + 'open_days' => '{1} Yksi kassapäivä on päättämättä, joten sen luvut voivat vielä muuttua.|[2,*] :count kassapäivää on päättämättä, joten niiden luvut voivat vielä muuttua.', + 'totals' => 'Yhteensä (verollinen)', + 'item' => 'Erä', + 'amount' => 'Määrä', + 'count' => 'Myyntejä', + 'cash' => 'Käteinen', + 'card' => 'Pankkikortti', + 'total' => 'Yhteensä', + 'by_vat' => 'ALV-kannoittain', + 'vat_rate' => 'ALV-kanta', + 'gross' => 'Verollinen', + 'net' => 'Veroton', + 'vat' => 'Vero', + 'chart_days' => 'Myynti päivittäin (verollinen)', + 'chart_months' => 'Myynti kuukausittain (verollinen)', + 'by_product' => 'Tuotteittain', + 'product' => 'Tuote', + 'quantity' => 'Määrä', + 'by_seller' => 'Myyjittäin', + 'seller' => 'Myyjä', + 'by_day' => 'Kassapäivittäin', + 'day' => 'Päivä', + 'cash_day' => 'Kassapäivä', + 'closed' => 'Päätetty', + 'open' => 'Päättämättä', + 'csv' => [ + 'day' => 'Päivä', + 'count' => 'Myyntejä', + 'cash' => 'Käteinen (snt)', + 'card' => 'Pankkikortti (snt)', + 'total' => 'Yhteensä (snt)', + 'closed' => 'Päätetty', + ], + ], ]; diff --git a/tests/Feature/Modules/Reports/QuickSalesReportTest.php b/tests/Feature/Modules/Reports/QuickSalesReportTest.php new file mode 100644 index 0000000..19473ed --- /dev/null +++ b/tests/Feature/Modules/Reports/QuickSalesReportTest.php @@ -0,0 +1,75 @@ +actingAs(User::factory()->management()->create()); + $this->seller = User::factory()->employee()->create(['name' => 'Myyjä Maija']); + $this->photos = ProductModel::factory()->create(['name' => 'Passikuvat', 'price_cents' => 1990]); + $this->prints = ProductModel::factory()->create(['name' => 'Tuloste', 'price_cents' => 100]); +}); + +function reportSale(string $day, ProductModel $product, int $quantity, PaymentMethod $method = PaymentMethod::Cash, int $vat = 2550): QuickSaleModel +{ + return QuickSaleModel::factory()->withLine($product, $quantity, $vat)->create([ + 'user_id' => test()->seller->id, + 'payment_method' => $method, + 'sold_on' => $day, + 'sold_at' => $day.' 10:00:00', + ]); +} + +it('sums quick sales by day, product, seller and VAT rate with credits', function () { + $first = reportSale('2026-11-02', $this->photos, 2); + reportSale('2026-11-02', $this->prints, 5, PaymentMethod::Card, 1350); + reportSale('2026-11-03', $this->photos, 1, PaymentMethod::Card); + QuickSaleModel::factory()->withLine($this->photos, -2)->create([ + 'user_id' => $this->seller->id, 'sold_on' => '2026-11-03', 'sold_at' => '2026-11-03 12:00:00', 'credits_quick_sale_id' => $first->id, + ]); + reportSale('2026-12-01', $this->photos, 9); + DB::table('cash_days')->insert(['day' => '2026-11-02', 'closed_at' => now(), 'closed_by' => $this->seller->id, 'created_at' => now(), 'updated_at' => now()]); + + $report = Livewire::withQueryParams(['alku' => '2026-11-01', 'loppu' => '2026-11-30']) + ->test('reports::sales') + ->assertOk() + ->assertSee('Passikuvat') + ->assertSee('Myyjä Maija') + ->instance() + ->report(); + + expect($report->count)->toBe(4) + ->and($report->cashCents)->toBe(0) + ->and($report->cardCents)->toBe(2490) + ->and($report->openDays)->toBe(1) + ->and($report->days[0])->toBe(['day' => '2026-11-02', 'count' => 2, 'cashCents' => 3980, 'cardCents' => 500, 'isClosed' => true, 'totalCents' => 4480]) + ->and($report->products[0])->toBe(['name' => 'Passikuvat', 'quantity' => 1, 'grossCents' => 1990]) + ->and($report->sellers)->toBe([['name' => 'Myyjä Maija', 'count' => 4, 'grossCents' => 2490]]) + ->and($report->vatTotals)->toBe([ + ['vatBasisPoints' => 2550, 'grossCents' => 1990, 'netCents' => 1585, 'vatCents' => 405], + ['vatBasisPoints' => 1350, 'grossCents' => 500, 'netCents' => 441, 'vatCents' => 59], + ]) + ->and($report->series)->toBe(['2026-11-02' => 4480, '2026-11-03' => -1990]); +}); + +it('charts a long period by month and downloads the days as CSV', function () { + reportSale('2026-03-02', $this->photos, 1); + reportSale('2026-03-20', $this->photos, 1); + + $report = Livewire::withQueryParams(['alku' => '2026-01-01', 'loppu' => '2026-12-31'])->test('reports::sales')->instance()->report(); + expect($report->seriesByMonth)->toBeTrue() + ->and($report->series)->toBe(['2026-03' => 3980]); + + $response = $this->get(route('reports.csv', ['report' => 'sales', 'alku' => '2026-01-01', 'loppu' => '2026-12-31'])) + ->assertOk() + ->assertDownload('pikamyynnit-2026-01-01-2026-12-31.csv'); + + expect($response->streamedContent())->toContain('2026-03-02;1;1990;0;1990;ei'); +});