php 想要计算codeigniter 3中每个房间的价格

9rbhqvlz  于 9个月前  发布在  PHP
关注(0)|答案(1)|浏览(81)

我创建了一个寄宿家庭网站的预订系统,我使用codeigniter 3为这个项目,但客户给了我一些复杂的条件,以实现预订,并需要计算价格房间明智的,并在最后这些房间的总数。
我很困惑写代码为以下情况下,我需要显示价格为每个房间根据以下条件:
我将数据存储为会话中的杂项数组
用于房间

[selectedRoom] => Array
        (
            [0] => Mud House (Neel Kutir)
            [1] => Mud House (Lal Kutir)
            [2] => Standard AC Room
            [3] => Deluxe AC Room
        )

字符串
成人(下划线前为房间名称,下划线后为成人人数)

[adults] => Array
        (
            [0] => Mud House (Neel Kutir)_1
            [1] => Mud House (Lal Kutir)_1
            [2] => Standard AC Room_1
            [3] => Deluxe AC Room_1
        )


在下面的数组中,我存储了

[dateRange] => Array
        (
            [0] => 2023-12-29,
            [1] => 2023-12-30,
            [2] => 2023-12-31
        )


在下面的数组中,我将数据存储为
数据的格式为{房间名称}{孩子总数}{孩子数系列}_{年龄组如斜线前是开始年份,斜线后是结束年份}

[kids_age] => Array
        (
            [0] => Mud House (Neel Kutir)_2_1_5/8
            [1] => Mud House (Neel Kutir)_2_2_8/12
            [2] => Mud House (Lal Kutir)_2_1_1/4
            [3] => Mud House (Lal Kutir)_2_2_8/12
            [4] => Standard AC Room_2_1_1/4
            [5] => Standard AC Room_2_2_5/8
            [6] => Deluxe AC Room_2_1_8/12
            [7] => Deluxe AC Room_2_2_5/8
        )


以下是需要实现的条件
如果房间是“泥屋(尼尔库蒂尔)”或“泥屋(尼尔库蒂尔)”,那么价格计算将是这样的

如果预订是一晚

for adult price will be like 
adult  =  1850  (for  1 Night  Per  Person )
kids = 1850 (for  1 Night  Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  1850)
or if kid age is (between 8 to 12 years then the price is 75% of  1850)
or if kid age is (above 12 years then the price is 1850)

如果预订是2晚f

or adult price will be like 
adult  =  1500  (for  2 Nights  Per  Person )
kids = 1500 (for  2 Nights Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  1500)
or if kid age is (between 8 to 12 years then the price is 75% of  1500)
or if kid age is (above 12 years then the price is 1850)

如果预订3晚或以上

adult price will be like 
adult  =  1350  (for  3 Nights or above  Per  Person )
kids = 1350 (for  3 Nights or above Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  1350)
or if kid age is (between 8 to 12 years then the price is 75% of  1350)
or if kid age is (above 12 years then the price is 1350)


如果房间是“标准空调房”,那么价格计算将像

如果预订是一晚

for adult price will be like 
adult  =  2050  (for  1 Night  Per  Person )
kids = 2050 (for  1 Night  Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  2050)
or if kid age is (between 8 to 12 years then the price is 75% of  2050)
or if kid age is (above 12 years then the price is 2050)

如果预订是2晚

for adult price will be like 
adult  =  1700  (for  2 Nights  Per  Person )
kids = 1700 (for  2 Nights Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  1700)
or if kid age is (between 8 to 12 years then the price is 75% of  1700)
or if kid age is (above 12 years then the price is 1700)

如果预订3晚或以上

for adult price will be like 
adult  =  1550  (for  3 Nights or above  Per  Person )
kids = 1550 (for  3 Nights or above Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  1550)
or if kid age is (between 8 to 12 years then the price is 75% of  1550)
or if kid age is (above 12 years then the price is 1550)


如果房间是“豪华空调房”,那么价格计算将像

如果预订是一晚

for adult price will be like 
adult  =  3350  (for  1 Night  Per  Person )
kids = 3350 (for  1 Night  Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  3350)
or if kid age is (between 8 to 12 years then the price is 75% of  3350)
or if kid age is (above 12 years then the price is 3350)

如果预订是2晚

for adult price will be like 
adult  =  3000  (for  2 Nights  Per  Person )
kids = 3000 (for  2 Nights Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  3000)
or if kid age is (between 8 to 12 years then the price is 75% of  3000)
or if kid age is (above 12 years then the price is 3000)

如果预订3晚或以上

for adult price will be like 
adult  =  2850  (for  3 Nights or above  Per  Person )
kids = 2850 (for  3 Nights or above Per  Person ) 
and 
if kid age is (between 1 to 4 years then the price is 0)
or if kid age is (between 5 to 8 years then the price is 50% of  2850)
or if kid age is (between 8 to 12 years then the price is 75% of  2850)
or if kid age is (above 12 years then the price is 2850)
c0vxltue

c0vxltue1#

首先创建一个数组,将每个房间的成人数量和孩子的年龄分组:

foreach ($array['adults'] as $room_adults) {
    $room_adults_parts = explode('_', $room_adults);
    $guests_per_room[$room_adults_parts[0]]['adults'] = $room_adults_parts[1];
}

foreach ($array['kids_age'] as $room_kids_age) {
    $room_kids_age_parts = explode('_', $room_kids_age);
    $guests_per_room[$room_kids_age_parts[0]]['kids_age'][] = $room_kids_age_parts[3];
}

字符串
这给出了一个$guests_per_room数组,看起来像这样:

array(4) {
  ["Mud House (Neel Kutir)"]=>
  array(2) {
    ["adults"]=>
    string(1) "1"
    ["kids_age"]=>
    array(2) {
      [0]=>
      string(3) "5/8"
      [1]=>
      string(4) "8/12"
    }
  }
  ["Mud House (Lal Kutir)"]=>
  array(2) {
    ["adults"]=>
    string(1) "1"
    ["kids_age"]=>
    array(2) {
      [0]=>
      string(3) "1/4"
      [1]=>
      string(4) "8/12"
    }
  }
  ["Standard AC Room"]=>
  array(2) {
    ["adults"]=>
    string(1) "1"
    ["kids_age"]=>
    array(2) {
      [0]=>
      string(3) "1/4"
      [1]=>
      string(3) "5/8"
    }
  }
  ["Deluxe AC Room"]=>
  array(2) {
    ["adults"]=>
    string(1) "1"
    ["kids_age"]=>
    array(2) {
      [0]=>
      string(4) "8/12"
      [1]=>
      string(3) "5/8"
    }
  }
}


然后用这个来计算每个房间的总数和最终总数:

$total = 0;
$number_of_nights = count($array['dateRange']);

foreach ($guests_per_room as $room => $guests) {

    // Start with the adult price for 1 night
    if (strpos($room, 'Mud House') === 0) {
        $adult_price = 1850;
    } else if ($room == 'Deluxe AC Room') {
        $adult_price = 3350;
    } else { // Standard AC Room
        $adult_price = 2050;
    }
    
    // If more than 1 night has been booked, subtract discount
    // (all rooms seem to have the same discount)
    if ($number_of_nights == 2) {
        $adult_price -= 350;
    } else if ($number_of_nights >= 3) {
        $adult_price -= 500;
    }

    $room_total = 0;
    
    // Add to the room total: the adult price multiplied by the number of adults
    if (isset($guests['adults'])) {
        $room_total += $adult_price * $guests['adults'];
    }
    
    // Add to the room total: the discounted price for each of the kids
    if (isset($guests['kids_age'])) {
        foreach ($guests['kids_age'] as $age) {
            if ($age == '5/8') {
                $room_total += 0.5 * $adult_price;
            } else if ($age == '8/12') {
                $room_total += 0.75 * $adult_price;
            } else if ($age != '1/4') { // Children older than 12
                $room_total += $adult_price;
            }
        }
    }    
    
    // Add the room total to the final total
    $total += $room_total;
    
    echo $room . ' total : ' . $room_total . "\n";
}

echo 'Final total : ' . $total;


您的问题不清楚多晚房价是否也应该乘以天数。如果需要,请调整。

相关问题