This commit is contained in:
2026-06-14 21:42:21 +03:30
parent b84b2df01a
commit 0c4cbd7c12
9 changed files with 80 additions and 23 deletions
+66
View File
@@ -7,6 +7,8 @@ use App\Models\Price;
use App\Models\PriceCandle;
use App\Models\Product;
use App\Models\Setting;
use Carbon\Carbon;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class ConfigService
{
@@ -397,4 +399,68 @@ class ConfigService
'max' => $max,
];
}
public function isClosePriceEnabled(): bool
{
return $this->getBool('close_price', false);
}
/** @return array{start: string, end: string} */
public function tradingHourBounds(): array
{
return [
'start' => $this->normalizeTradingTime($this->getString('startDay', '00:00'), '00:00'),
'end' => $this->normalizeTradingTime($this->getString('endDay', '23:59'), '23:59'),
];
}
private function normalizeTradingTime(string $value, string $default): string
{
$value = trim($value);
if ($value === '') {
$value = $default;
}
if (preg_match('/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/', $value, $matches)) {
return sprintf('%02d:%02d', (int) $matches[1], (int) $matches[2]);
}
return $default;
}
public function isOutsideTradingHours(?Carbon $now = null): bool
{
if (!$this->isClosePriceEnabled()) {
return false;
}
$now = $now ?? Carbon::now();
$bounds = $this->tradingHourBounds();
[$startHour, $startMinute] = array_map('intval', explode(':', $bounds['start']));
[$endHour, $endMinute] = array_map('intval', explode(':', $bounds['end']));
$start = $now->copy()->startOfDay()->setTime($startHour, $startMinute);
$end = $now->copy()->startOfDay()->setTime($endHour, $endMinute);
return !$now->between($start, $end);
}
public function syncAppClosedFlag(?Carbon $now = null): bool
{
$closed = $this->isOutsideTradingHours($now);
Cnt::where('key', 'app_closed')->update(['value' => $closed ? 'true' : 'false']);
if ($closed) {
$this->UpdateConfig('montzar_closed', 'true');
}
return $closed;
}
public function assertTradingOpen(?Carbon $now = null): void
{
if ($this->isOutsideTradingHours($now)) {
throw new UnprocessableEntityHttpException('فروشگاه در حال حاضر بسته است.');
}
}
}