72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Holiday;
|
|
use Illuminate\Console\Command;
|
|
use Morilog\Jalali\Jalalian;
|
|
|
|
class SeedIranianHolidays extends Command
|
|
{
|
|
protected $signature = 'holidays:seed {--year= : فقط یک سال شمسی مشخص}';
|
|
|
|
protected $description = 'ثبت/بهروزرسانی تعطیلات رسمی ایران از فایل تقویم پروژه';
|
|
|
|
public function handle(): int
|
|
{
|
|
$path = database_path('data/iranian_official_holidays.php');
|
|
if (!is_file($path)) {
|
|
$this->error('فایل تعطیلات یافت نشد: ' . $path);
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
/** @var array<int, list<array{month:int, day:int, title:string}>> $years */
|
|
$years = require $path;
|
|
$onlyYear = $this->option('year') !== null ? (int) $this->option('year') : null;
|
|
|
|
$count = 0;
|
|
foreach ($years as $jalaliYear => $days) {
|
|
if ($onlyYear !== null && (int) $jalaliYear !== $onlyYear) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($days as $day) {
|
|
try {
|
|
$gregorian = (new Jalalian((int) $jalaliYear, (int) $day['month'], (int) $day['day']))
|
|
->toCarbon()
|
|
->format('Y-m-d');
|
|
} catch (\Throwable $e) {
|
|
$this->warn("رد شد: {$jalaliYear}/{$day['month']}/{$day['day']} — {$e->getMessage()}");
|
|
continue;
|
|
}
|
|
|
|
Holiday::updateOrCreate(
|
|
['date' => $gregorian],
|
|
['title' => $day['title']]
|
|
);
|
|
$count++;
|
|
}
|
|
|
|
$this->info("سال {$jalaliYear}: " . count($days) . ' روز');
|
|
}
|
|
|
|
$this->info("جمع: {$count} تعطیل رسمی ثبت/بهروز شد.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
/** آخرین سال شمسی پوششدادهشده در فایل داده */
|
|
public static function coveredUntilYear(): int
|
|
{
|
|
$path = database_path('data/iranian_official_holidays.php');
|
|
if (!is_file($path)) {
|
|
return 0;
|
|
}
|
|
|
|
$years = array_keys(require $path);
|
|
|
|
return $years ? (int) max($years) : 0;
|
|
}
|
|
}
|