<?php
// If it's going to need the database, then it's 
// probably smart to require it before we start.
require_once('initializer.php');

class Reservation extends DatabaseObject {
	
  protected static $table_name="Reservation";
  
  //we will populate this array of attribure dynamically
	//protected static $db_fields = array('id','Username','Privilege','First_Name','Last_Name','Password');
  
  protected static $db_fields;
  protected static $exclu_fields = array ();

	// public  $id;
	// public $Username;
	// public $Privilege;
	// public $Last_Name;
  // public $First_Name;
  // public $Password;

  public function __construct($options=""){
    if ($options=="Blank") {
      $myFields = static::Get_Object_Fields ();
      foreach($myFields as $field) {
        $this->{$field} = "";
      }
    }

   // unset ($this->id);
    
  }

  public static function Mark_Sent_To_POS ($ONLINE_id=""){ 
    global $database;
 
    $sql = "UPDATE `Reservation` SET `Sent_to_POS`='YES' WHERE `ONLINE_id`='{$ONLINE_id}' " ;
    $database->query($sql);

  }

  public static function getReservation_by_ONLINE_ID($ONLINE_ID="") {
    //self::Get_Object_Fields ()
    $result_array = self::find_by_sql("SELECT * FROM `Reservation` WHERE `ONLINE_id`='{$ONLINE_ID}' LIMIT 1","");
        return !empty($result_array) ? array_shift($result_array) : false;
  }



  public static function GetOnlineReservations () {

    $sql = "SELECT * FROM `Reservation` WHERE `DatePassed` >= CURDATE() AND `Sent_to_POS`='No'";

    $Reservations=  self::find_by_sql($sql,"Reservations");

    return  $Reservations;

  }


  public static function GetReservations () {

    $sql = "SELECT * FROM `Reservation` WHERE `DatePassed` >= CURDATE()";

    $Reservations=  self::find_by_sql($sql,"Reservations");

    return  $Reservations;

  }

  public static function  getUserReservation ($customer_id=""){

    $sql = "SELECT * FROM `Reservation` WHERE `DatePassed` >= DATE_ADD(CURDATE(), INTERVAL -10 DAY) AND`Customer_id`='".$customer_id."' ORDER BY  FIELD(`Status`, 'CONFIRMED') DESC, `Time` ASC ";

    $status=  self::find_by_sql($sql,"");

  return $status;

  }




  public static function  checkUserReservation ($customer_id="", $date=""){

    $sql = "SELECT `Status` FROM `Reservation` WHERE `Customer_id`='".$customer_id."' AND DATE (`DatePassed`) = DATE ('".$date."') AND `Status`!='CANCELLED' LIMIT 1";

    $status=  self::find_by_sql($sql,"");
    //  error_log(print_r( $status[0]->Status ,true));

     if (isset($status[0]->Status) && !empty($status[0]->Status)) {
      error_log(print_r( "return value",true));
       return $status[0]->Status;
       
      } else {
        error_log(print_r( "return false",true));
        return false;
     }
     


  }



}

?>