kylekatarnls / business-time

Carbon mixin to handle business days and opening hours
MIT License
297 stars 14 forks source link

Bug: isOpenOn always return true now it seem #77

Closed ziming closed 3 weeks ago

ziming commented 3 months ago

I'm not exactly sure when it started happening but it was previous working but now it just seem to return true all the time.

Example code:

1st in a laravel codebase i have something like this in my AppServiceProvider boot method

BusinessTime::enable(Carbon::class, [
            'holidays' => [
                'region' => 'sg-national',
            ],
        ]);

Then this example code:

$scheduleConfig = [
    "friday" => [
      "10:00-18:00",
    ],
    "monday" => [
      "10:00-18:00",
    ],
    "sunday" => [],
    "tuesday" => [
            "10:00-18:00",
    ],
    "saturday" => [],
    "thursday" => [
      "10:00-18:00",
    ],
    "wednesday" => [
      "10:00-18:00",
    ],
    "exceptions" => [],
    "holidaysAreClosed" => true,
  ];

$schedule = Schedule::create($scheduleConfig);

 // return true. In the past it return false. This is the bug. 
// It should be false because it is christmas and the schedule config "holidaysAreClosed" key is true too
$schedule->isOpenOn('2024-12-25'); // return true when it should be false

$christmas = Carbon::parse('2024-12-25');
$christmas->isHoliday(); // return true
kylekatarnls commented 3 months ago

Hello, BusinessTime::enable set the default config, but Schedule::create() is a way to isolate a config, so both won't merge.

You can either include 'region' => 'sg-national' in your Schedule:

$schedule = Schedule::create([
    'friday' => [
        '10:00-18:00',
    ],
    'monday' => [
        '10:00-18:00',
    ],
    'sunday' => [],
    'tuesday' => [
        '10:00-18:00',
    ],
    'saturday' => [],
    'thursday' => [
        '10:00-18:00',
    ],
    'wednesday' => [
        '10:00-18:00',
    ],
    'exceptions' => [],
    'holidaysAreClosed' => true,
    'holidays' => [
        'region' => 'sg-national',
    ],
]);

var_dump($schedule->isOpenOn('2024-12-25')); // false

Or you pass the planning in default settings and use Carbon:

BusinessTime::enable(Carbon::class, [
    'friday' => [
        '10:00-18:00',
    ],
    'monday' => [
        '10:00-18:00',
    ],
    'sunday' => [],
    'tuesday' => [
        '10:00-18:00',
    ],
    'saturday' => [],
    'thursday' => [
        '10:00-18:00',
    ],
    'wednesday' => [
        '10:00-18:00',
    ],
    'exceptions' => [],
    'holidaysAreClosed' => true,
    'holidays' => [
        'region' => 'sg-national',
    ],
]);

var_dump(Carbon::isOpenOn('2024-12-25'));