kylekatarnls / business-time

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

[Possible bug] Cannot sub open hours in some scenario #55

Closed Naghal closed 1 year ago

Naghal commented 1 year ago

Hi!

Context I have the following use case: When a ticket is assigned in a new section, I want to recalculate its new service level agreement. Sometime, because the grace period of the new section is shorter than the grace period of the previous ticket, I need to remove open hours instead of adding hours. I saw that there is a method for that subOpenHours.

Error Unfortunately, using local mode with, it throws the exception InvalidArgumentException with the message Opening hours have not been set..

I have tried addOpenHours (-1), subOpenHours(1) and subOpenHours(-1), they all throw the exception.

This might be related to https://github.com/kylekatarnls/business-time/issues/31

Code reproduce:

// $c would be the current due date
 $c = Carbon::parse('2022-10-21 06:40:00'); // Changing this date to '2022-10-21 09:40:00' will make it work without exception
 $c->setOpeningHours([
         'monday' => ['09:00-12:00', '13:00-18:00'],
         'tuesday' => ['09:00-12:00', '13:00-18:00'],
         'wednesday' => ['09:00-12:00'],
         'thursday' => ['09:00-12:00', '13:00-18:00'],
         'friday' => ['09:00-12:00', '13:00-20:00'],
 ]);
$c->subOpenHours(1) // This would be the new due date

Is this the intended behaviour or maybe this is a bug?

kylekatarnls commented 1 year ago

Hello,

I'm so sorry for the delay, It's not the indented behavior, it's a bug and not yet clear to me. I will work on this.

Meanwhile if it helps, I noted that it does not happen using the enable():


BusinessTime::enable(Carbon::class, [
    'monday' => ['09:00-12:00', '13:00-18:00'],
    'tuesday' => ['09:00-12:00', '13:00-18:00'],
    'wednesday' => ['09:00-12:00'],
    'thursday' => ['09:00-12:00', '13:00-18:00'],
    'friday' => ['09:00-12:00', '13:00-20:00'],
]);

$c = Carbon::parse('2022-10-21 06:40:00');
echo $c->subOpenHours(1);
kylekatarnls commented 1 year ago

It should now be fixed in 1.11.3

Thanks for the report 🙏