Otázka 06 / 15
Záhada dvou nedělí
Účetní při kontrole loňského roku porovnává denní tržby z reportu e-shopu s výpisem platební brány a podezírá už bránu, pokladnu i sebe. Celý rok sedí na korunu, až na dvě neděle: 30. března 2025 je v reportu víc a 26. října 2025 méně, pokaždé zhruba o tržbu za jednu hodinu kolem půlnoci. Tady je část modulu, která report počítá. Co se děje a co bys udělal?
Ctrl/Cmd+klik na metodu, třídu nebo konstantu otevře její deklaraci, Alt+← vrátí zpět.
app › Services › Reporting › DailyRevenueReport.php
<?php declare(strict_types=1);
namespace App\Services\Reporting;
use App\Repositories\OrderRepository;
use DateInterval;
use DateTimeImmutable;
use DateTimeZone;
final class DailyRevenueReport
{
private const string SHOP_TIMEZONE = 'Europe/Prague';
private const string DAY_KEY_FORMAT = 'Y-m-d';
private const string DAY_LENGTH = 'PT24H';
public function __construct(
private readonly OrderRepository $orderRepository,
)
{}
/** @return array<string, int> */
public function revenueByDay(DateTimeImmutable $month): array
{
$dayStart = $month->setTimezone(new DateTimeZone(self::SHOP_TIMEZONE))->modify('first day of this month midnight');
$nextMonth = $dayStart->modify('+1 month');
$dayLength = new DateInterval(self::DAY_LENGTH);
$revenueByDay = [];
while ($dayStart < $nextMonth) {
$dayEnd = $dayStart->add($dayLength);
$dayKey = $dayStart->format(self::DAY_KEY_FORMAT);
$revenueByDay[$dayKey] = $this->orderRepository->sumPaidTotalInHellersBetween($dayStart, $dayEnd);
$dayStart = $dayStart->modify('+1 day');
}
return $revenueByDay;
}
}