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]>
77 lines
2.9 KiB
PHP
77 lines
2.9 KiB
PHP
<?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,
|
|
);
|
|
}
|
|
}
|