Reservation system pricing conditions refactoring - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, February 21, 2022

Reservation system pricing conditions refactoring

I got to work on a project with bookings and reservations that a client had a previous platform done in Laravel/PHP and asked us to do some modifications. This was my first time working with algorithms for calculating prices in a date range, so I am trying to guess what would be the most efficient way of doing it.

Right now, the client has 4 different types of prices:

  • Low Season (1st of November - 31st of March)
  • Medium Season (1st of April - 30th of June and 1st of October - 31st of October)
  • High Season (1st of July - 15th of July and 16th of August - 30th of September)
  • Peak Season (16th of July - 15th of August)

They have different groups of cars to rent within these seasons, but the previous code is so buggy that it doesn't calculate the right price. Here is a short excerpt:

$startDate = str_replace("/", "-", explode(' ', $request->startDate)[0]);
$endDate = str_replace("/", "-", explode(' ', $request->endDate)[0]);

$begin = new DateTime($startDate);
$end = new DateTime($endDate);
if ($startDate == $endDate) {
    $end->setTime(0,0,1);
}

$startYear = intval($begin->format('Y'));
$endYear = intval($end->format('Y'));

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

$groupPrices = DB::table("Group")->select("*")->get();
$totalGroupPrices = [];
$totalGroupPricesWithInsurance = [];

foreach ($groupPrices as $group)
        {
            foreach($daterange as $date){
                if ($date->format('d-m-Y') >= "1/11/".strval($startYear) and $date->format('d-m-Y') <= "31/3/".strval(($endYear+1)))
                {
                    (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->lowSeasonPrice : $totalGroupPrices[$group->id] = $group->lowSeasonPrice);
                    (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->lowSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->lowSeasonPriceWithInsurance);
                } elseif ($date->format('d-m-Y') >= "1/1/".strval($startYear) and $date->format('d-m-Y') <= "31/3/".strval(($endYear)))
                {
                    (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->lowSeasonPrice : $totalGroupPrices[$group->id] = $group->lowSeasonPrice);
                    (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->lowSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->lowSeasonPriceWithInsurance);
                } elseif (($date->format('d-m') >= "1/4" and $date->format('d-m') <= "30/6") or ($date->format('d-m') >= "1/10" and $date->format('d-m') <= "31/10"))
                {
                    (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->mediumSeasonPrice : $totalGroupPrices[$group->id] = $group->mediumSeasonPrice);
                    (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->mediumSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->mediumSeasonPriceWithInsurance);
                } elseif (($date->format('d-m') >= "1/7" and $date->format('d-m') <= "15/7") or ($date->format('d-m') >= "16/8" and $date->format('d-m') <= "30/9"))
                {
                    (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->highSeasonPrice : $totalGroupPrices[$group->id] = $group->highSeasonPrice);
                    (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->highSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->highSeasonPriceWithInsurance);
                } elseif ($date->format('d-m') >= "16/7" and $date->format('d-m') <= "15/8")
                {
                    (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->peakSeasonPrice : $totalGroupPrices[$group->id] = $group->peakSeasonPrice);
                    (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->peakSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->peakSeasonPriceWithInsurance);
                }
            }
        }

I'm sure this must have a very simple approach, or at least with a lot less code to make it less buggy. I just would like to know how would you design this algorithm.

Thanks in advance.



source https://stackoverflow.com/questions/71197190/reservation-system-pricing-conditions-refactoring

No comments:

Post a Comment