<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'test2.php';
/* ---------------------------------------- */
$room = "1";
$start_date = "2019-08-26"; // data di arrivo
$end_date = "2019-08-30"; // data di partenza
roomPriceCalc($room, $start_date, $end_date);
echo "<h3>TOTAL PRICE : ".$totPrice."</h3>";
/* ---------------------------------------- */
function roomPriceCalc($room, $start_date, $end_date)
{
global $totPrice, $roomPrices;
$date_in = new DateTime($start_date); // data arrivo
$date_out = new DateTime($end_date); // data partenza
$nights = $date_out->diff($date_in); // numero di pernottamenti
$nights = $nights->d;
echo "<br />data di arrivo : " .$start_date
. "<br />data di partenza : ".$end_date
. "<br />pernottamenti : " .$nights
. "<br /><br />";
/* ---------------------------------------- connessione al db */
$hostname = "HOST";
$pass = "PASSWORD";
$muser = "USERDB";
$db = "DB";
$mysqli = new mysqli($hostname, $muser, $pass, $db);
if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error() ); }
/* ---------------------------------------- legge il prezzo standard della camera */
$query = "SELECT price_sun, price_mon, price_tue, price_wed, price_thu, price_fri, price_sat"
. " FROM room"
. " WHERE id = ".$room;
$result = $mysqli->query($query);// prezzi elencati da domenica = 0 a sabato = 6
while($row = $result->fetch_assoc()) {
extract($row);
$stdPrices = array($row["price_sun"],$row["price_mon"],$row["price_tue"],$row["price_wed"],$row["price_thu"],$row["price_fri"],$row["price_sat"]);
}
echo "<h3>Room standard prices</h3>".show_var( $stdPrices )."<br />";
/* ---------------------------------------- legge lo sconto standard della camera */
$query2 = "SELECT rebate_sun, rebate_mon, rebate_tue, rebate_wed, rebate_thu, rebate_fri, rebate_sat FROM room WHERE id =$room";
$result2 = $mysqli->query($query2);// prezzi elencati da domenica = 0 a sabato = 6
while($row2 = $result2->fetch_assoc()) {
extract($row2);
$stdRebates = array($row2["rebate_sun"],$row2["rebate_mon"],$row2["rebate_tue"],$row2["rebate_wed"],$row2["rebate_thu"],$row2["rebate_fri"],$row2["rebate_sat"]);
}
//$stdRebates = array(9, 0, 0, 0, 4, 5, 6);
echo "<h3>Room standard rebates</h3>".show_var( $stdRebates )."<br />";
/* ---------------------------------------- crea la tabella dei prezzi e sconti per periodo */
$dtAdd = 0;
$roomPrices = array();
for ($dt=0; $dt<$nights; $dt++)
{
$day = $date_in->modify("+".$dtAdd." day");
$df = $day->format("Y-m-d");
$num = $day->format('w');
$roomPrices[$df]["date"] = $df; // giorno esaminato
$roomPrices[$df]["label"] = $day->format('l'); // giorno
$roomPrices[$df]["wDay"] = $num; // numero del giorno, da domenica = 0 a sabato = 6
$roomPrices[$df]["stdPrice"] = $stdPrices[$num]; // prezzo standard dalla tabella "room"
$roomPrices[$df]["stdRebate"] = $stdRebates[$num]; // sconto standard dalla tabella "room"
$roomPrices[$df]["cusPrice"] = 0; // prezzo custom dalla tabella "room_custom_price"
$roomPrices[$df]["cusRebate"] = 0; // sconto custom dalla tabella "room_custom_price"
$dtAdd = 1;
}
echo "<h3>Room prices - step 1</h3>".show_var( $roomPrices )."<br />";
/* ---------------------------------------- legge il prezzo e lo sconto custom del periodo per la camera */
$query3 = "SELECT certain_date, price, rebate"
. " FROM room_custom_price"
. " WHERE room_id = ".$room
. " AND (certain_date BETWEEN '".$start_date."' AND '".$end_date."')"
. " ORDER BY certain_date ASC";
$result3 = $mysqli->query($query3);// prezzi elencati da domenica = 0 a sabato = 6
while($row3 = $result3->fetch_assoc()) {
extract($row3);
$cusPrices[] = array("certain_date" => $row3["certain_date"],"price" => $row3["price"] ,"rebate" => $row3["rebate"]);
}
echo "<h3>Custom prices</h3>".show_var( $cusPrices )."<br />";
/* ---------------------------------------- aggiorna la tabella con il prezzo e lo sconto custom */
foreach ($cusPrices as $k=>$items)
{
$df = $items["certain_date"]; // riprende la data per aggiornare la tabella
if ( !empty( $roomPrices[$df] ) )
{
$roomPrices[$df]["cusPrice"] = $items["price"]; // prezzo custom dalla tabella "room_custom_price"
$roomPrices[$df]["cusRebate"] = $items["rebate"]; // sconto custom dalla tabella "room_custom_price"
}
}
echo "<h3>Room prices - step 2</h3>".show_var( $roomPrices )."<br />";
/* ---------------------------------------- calcola il prezzo finale */
$totPrice = 0;
foreach ($roomPrices as $df=>$items)
{
$Rebate = $Rebate = $items["stdRebate"]; // sceglie lo sconto da applicare
if ( !empty( $items["cusRebate"] ) ) $Rebate = $items["cusRebate"]; // prevale lo sconto custom
if ( empty( $Rebate ) ) { $roomPrices[$df]["stdFinal"] = $items["stdPrice"]; }
else { $roomPrices[$df]["stdFinal"] = round( $items["stdPrice"] * (1 - $Rebate / 100), 2 ); }
$Rebate = 0;
if ( !empty( $items["cusRebate"] ) ) $Rebate = $items["cusRebate"]; // applica lo sconto custom sul prezzo custom
if ( empty( $Rebate ) ) { $roomPrices[$df]["cusFinal"] = $items["cusPrice"]; }
else { $roomPrices[$df]["cusFinal"] = round( $items["cusPrice"] * (1 - $Rebate / 100), 2 ); }
$roomPrices[$df]["totFinal"] = $roomPrices[$df]["stdFinal"]; // sceglie il prezzp da applicare, prevale custom
if ( !empty( $roomPrices[$df]["cusFinal"] ) ) { $roomPrices[$df]["totFinal"] = $roomPrices[$df]["cusFinal"]; }
$totPrice += $roomPrices[$df]["totFinal"];
}
echo "<h3>Room prices - step 3</h3>".show_var( $roomPrices )."<br />";
return;
}
?>