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,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\Contracts;
use App\Modules\Sales\Application\DTOs\QuickSalesInPeriod;
/**
* Quick sales summed for management's reports. Credits are included with
* negative amounts.
*/
interface QuickSaleReportQueries
{
/**
* Sales of the cash days from the first to the last (Y-m-d, both included).
*/
public function between(string $from, string $to): QuickSalesInPeriod;
}
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\DTOs;
/**
* Quick sales of a period, amounts with VAT in cents:
*
* - days: every cash day with sales, oldest first;
* - products: by product name, most sold (in euros) first;
* - sellers: by seller id;
* - dayVatTotals: each day's sales by VAT rate, for VAT taken out of the
* day's total of a rate as on the cash day balance.
*/
final readonly class QuickSalesInPeriod
{
/**
* @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,96 @@
<?php
namespace App\Modules\Sales\Infrastructure\Persistence\Repositories;
use App\Modules\Sales\Application\Contracts\QuickSaleReportQueries;
use App\Modules\Sales\Application\DTOs\QuickSalesInPeriod;
use DateTimeImmutable;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
/**
* Sums quick sales and their lines in the database.
*/
class EloquentQuickSaleReportQueries implements QuickSaleReportQueries
{
public function between(string $from, string $to): QuickSalesInPeriod
{
$until = (new DateTimeImmutable($to))->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);
}
}
@@ -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,
];