<?php



// Check if a file was uploaded

if(isset($_FILES["fileToUpload"])) {

    // Get the temporary filename of the uploaded file

    $tmpFilePath = $_FILES["fileToUpload"]["tmp_name"];

  

    // Get the temporary directory path

    $tempDirPath = sys_get_temp_dir();

  

    // Get the full temporary file path

    $tempFilePath = $tempDirPath . '/' . basename($tmpFilePath);

  

    // Move the uploaded file to the temporary directory

    if(move_uploaded_file($tmpFilePath, $tempFilePath)) {

      echo "File uploaded to temporary directory: " . $tempFilePath;

    } else {

      echo "Error moving file to temporary directory.";

    }

  }



  if($_POST['platform'] == 2 || $_POST['platform'] == 3){

    }else{

        echo "<script>alert('Platform type not supported yet!');</script>";

        exit;

    }



$conn = mysqli_connect("localhost","dzvisual_admin","Farhan@123456", "dzvisual_homestay");

if($conn){echo "database connection ok<br>";}else{echo "database connection error<br>";}



error_reporting(E_ALL);

set_time_limit(0);



date_default_timezone_set('Europe/London');



?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />



<title>PHPExcel Reader Example #13</title>



</head>

<body>



<h1>PHPExcel Reader Example #13</h1>

<h2>Simple File Reader for Multiple CSV Files</h2>

<?php



/** Include path **/

set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');



/** PHPExcel_IOFactory */

include 'PHPExcel/IOFactory.php';





$inputFileType = 'CSV';

$inputFileNames = array($tempFilePath);



$objReader = PHPExcel_IOFactory::createReader($inputFileType);

$inputFileName = array_shift($inputFileNames);



echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #1 using IOFactory with a defined reader type of ',$inputFileType,'<br />';

$objPHPExcel = $objReader->load($inputFileName);

$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));

foreach($inputFileNames as $sheet => $inputFileName) {

	echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #',($sheet+2),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';

	$objReader->setSheetIndex($sheet+1);

	$objReader->loadIntoExisting($inputFileName,$objPHPExcel);

	$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));

}





echo '<hr />';



echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';



$loadedSheetNames = $objPHPExcel->getSheetNames();



foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {

	echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'</b><br />';

	$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);

	$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);



	// var_dump($sheetData);

    $first_loop = true; $no = 0;

    foreach($sheetData as $data){

        if ($first_loop) {

            $first_loop = false;

            continue;

        }



        //for bookingcom

        if($_POST['platform'] == 2){

            $rvNo = $data['B']; //reservation no

            $guestName = $data['E']; //guest name

            $importerPrice = $data['M']; //price

        }



        //for airbnb

        if($_POST['platform'] == 3){

            if (preg_match('/^.{10}$/',$data['C'])) {

                $rvNo = $data['C']; //reservation no

                $guestName = $data['F']; //guest name

                $importerPrice = $data['K']; //price

            }



            if (preg_match('/^.{10}$/',$data['E'])) {

                $rvNo = $data['E']; //reservation no

                $guestName = $data['H']; //guest name

                $importerPrice = $data['O']; //price

            }

        }



        echo ++$no; echo " - ";

        echo $rvNo; 

        echo " - ";

        echo $guestName; 

        echo "<br>";

        if($dataSQL = getDetailByMysqliWhere('reservation','rv_no',$rvNo)){



            $sqlPrice = $dataSQL['rv_total'];

            

            echo "- price : ".$sqlPrice; echo "<br>";

            echo "- importer price : ".$importerPrice; echo "<br>";

            if($sqlPrice == $importerPrice){

                $counterMatch++;

                echo "- MATCH OK"; echo "<br>";

            }else{

                $counterNotMatch++;

                echo "- NOT MATCH"; echo "<br>";

            }



            if(mysqli_query($conn,"UPDATE reservation SET rv_total_by_import=$importerPrice WHERE rv_no='$rvNo'")){

                echo "- PRICE ADDED TO rv_total_by_import"; echo "<br>";

            }else{

                echo "- PRICE NOT ABLE TO ADD"; echo "<br>";

            }



            $counterFound++;

        }else{

            echo "- no found";

            $counterNoFound++;

        }

        echo "<br>";

        echo "<br>";

    }



	echo '<br /><br />';

}



echo "Total found: ".$counterFound; echo "<br>";

echo "Total no found: ".$counterNoFound; echo "<br>";

echo "Total price match: ".$counterMatch; echo "<br>";

echo "Total price not match: ".$counterNotMatch; echo "<br>";





?>

<body>

</html>



<?php

    function getDetailByTable($tableName,$rowID){

        global $conn;

        

        $result = mysqli_query($conn, "SHOW COLUMNS FROM $tableName");

        while($row = mysqli_fetch_assoc($result)){

            $column[] = $row['Field'];

        } 

        

        $sql = "SELECT * FROM $tableName WHERE $column[0]=$rowID";

        $result = mysqli_query($conn,$sql);

        $array = mysqli_fetch_array($result);

        

        return $array;

    }



	function getDetailByMysqliWhere($table,$where,$id){

		global $conn;



		$result = mysqli_query($conn,"SELECT * FROM $table WHERE $where='$id'");

		$array = mysqli_fetch_array($result);

		return $array;

	}

?>