| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- declare(strict_types=1);
- namespace App\Services;
- use App\Models\ProductionCalendarDay;
- use Carbon\CarbonImmutable;
- use DateTimeInterface;
- use InvalidArgumentException;
- class ProductionCalendarService
- {
- public function addWorkingDays(DateTimeInterface|string $startDate, int $workingDays): CarbonImmutable
- {
- if ($workingDays < 0) {
- throw new InvalidArgumentException('Количество рабочих дней не может быть отрицательным.');
- }
- $date = CarbonImmutable::parse($startDate)->startOfDay();
- if ($workingDays === 0) {
- return $date;
- }
- $overrides = ProductionCalendarDay::query()
- ->pluck('is_working_day', 'date')
- ->map(static fn (mixed $value): bool => (bool) $value);
- $remaining = $workingDays;
- while ($remaining > 0) {
- $date = $date->addDay();
- $key = $date->toDateString();
- $isWorkingDay = $overrides->has($key)
- ? $overrides->get($key)
- : ! $date->isWeekend();
- if ($isWorkingDay) {
- $remaining--;
- }
- }
- return $date;
- }
- }
|