tsompanis tsompanis - 6 months ago 13
PHP Question

Finding extra persons and in which category they belong

I'm making a script for a hotel with my little knowledge in PHP where the user (me) selects how many persons are about to rent a room.

All of my rooms are for 2 persons. Depending on how many there are, I'm charging an extra price/day, per person. More can stay, but those "extras" will have to pay depending on the age.

function calc()
{
$data['msg'] = "";
$data['from_date'] = $this->input->post('from_date');
$data['to_date'] = $this->input->post('to_date');
$data['category'] = $this->input->post('category');
$data['guestid'] = $this->input->post('guestid');
$data['idroom'] = $this->input->post('room');

$data['nguests'] = $this->input->post('nguests');
$data['nchilds'] = $this->input->post('nchilds');
$data['lchilds'] = $this->input->post('lchilds');

$data['greek'] = $this->input->post('greek');
$data['continental'] = $this->input->post('continental');
$data['halfboard'] = $this->input->post('halfboard');
$data['whitewine'] = $this->input->post('whitewine');
$data['redwine'] = $this->input->post('redwine');

$data['preferences'] = $this->input->post('preferences');
$data['mandiscount'] = $this->input->post('mandiscount');

$data['booking'] = $this->input->post('booking');
$data['airport'] = $this->input->post('airport');
$data['annotation'] = $this->input->post('annotation');

$data['is_manual'] = $this->input->post('is_manual');

if($data['is_manual'] == "manual")
{
$data['price_per_day'] = $this->input->post('price_per_day');
}

$room_avail = $this->kratiseis->getRoomPrice($data['idroom'], $data['from_date'], $data['to_date']);
$category_details = $this->admin->getSingleRoomCategory($data['category']);
$extras = $this->admin->getExtras();
$client = $this->guests->details($data['guestid']);
$data['room'] = $this->details->getSingleRoom($data['idroom']);

//$stay_days = date_diff($data['from_date'], $data['to_date']) - 1;

$from = new DateTime($this->input->post('from_date'));
$to = new DateTime($this->input->post('to_date'));
$stay_days = $from->diff($to);
$data['stay_for'] = $stay_days->format('%d');

$data['total_guests'] = $data['nguests'] + $data['nchilds'] + $data['lchilds'];

/*
* We only accept 2 persons in any room. If there are more than 2, we charge extra
*/

$extra_adults_price = 0;
$extra_childs_price = 0;
$extra_adults = 0;
$extra_childs = 0;
$we_have_extras = false;

if($data['total_guests'] > 2)
{
/*
* Find how many adults extra we have.
*/
if($data['nguests'] > 2)
{
$we_have_extras = true;
$extra_adults = $data['nguests'] - 2;
$extra_adults_price = ($extra_adults * $data['stay_for']) * 10;
}

/*
* Find how many extra children do we have.
*/
if( $data['nchilds'] > 2)
{
$we_have_extras = true;
$extra_childs = $data['nchilds'] - 2;
$extra_childs_price = ($extra_childs * $data['stay_for']) * 5;
}
}


Here's the view, from where I get the data:

<div class="column-container">
<div class="column column-1" style="background-color: #097ea1; color: #fff;">Adults:</div>
<div class="column column-2" style="background-color: #cee8ef">
<select name="nguests">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div class="column column-3" style="background-color: #097ea1; color: #fff;">Children 0-5:</div>
<div class="column column-4" style="background-color: #cee8ef">
<select name="nchilds">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div class="column column-5" style="background-color: #097ea1; color: #fff;">Children 6-12:</div>
<div class="column column-6" style="background-color: #cee8ef">
<select name="lchilds">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div class="column column-7"></div>
<div class="column column-8"></div>
</div>


I also have some code in the controller.

Now, lets say we have a reservation of 3 adults + 1 child = total of 4 persons. The extra cost is $10, but it should have been $15 (10 for the adult and 5 for the child)

How can I distinguish those values of extra persons so I can get my desired result?

Answer

Try this code, I think the problem is in your logic

Function  CountExtras($data) {      
if($data['total_guests'] <= 2) return 0;

$extra_adults_price = 0;
$extra_childs_price = 0;
$extra_adults = 0;
$extra_childs = 0;

$data['nguests'] = $data['total_guests'] - $data['nchilds'];

If( $data['nguests'] >= 2  )  $data['nguests'] = $data['nguests'] - 2; 
else {
    $data['nguests'] = 0;
    --$data['nchilds'];
}

return $data['nguests']*10 + $data['nchilds']*5;
}

echo CountExtras(array ( 'total_guests' => 1, 'nchilds' => 0 )).'<br>'; // 0
echo CountExtras(array ( 'total_guests' => 2, 'nchilds' => 0 )).'<br>'; // 0
echo CountExtras(array ( 'total_guests' => 3, 'nchilds' => 1 )).'<br>'; // 5 
echo CountExtras(array ( 'total_guests' => 3, 'nchilds' => 0 )).'<br>'; // 10
echo CountExtras(array ( 'total_guests' => 4, 'nchilds' => 1 )).'<br>'; // 15