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

class OrdersExtra extends DatabaseObject {
	
  protected static $table_name="OrdersExtra";
 
  
  //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);
    
  }

  //we may need to create an special function 
  ///that will change ItemType_id to id


  public static function saveOrderExtra ($orderID="",$object="",$dishinfosaved="") {
    $extra = new self ("Blank");
    //error_log(print_r("seeee waaaa" ,true));

    //error_log(print_r($object ,true));

    
         foreach(  $object as $attribute =>$value){

            if(property_exists($extra,$attribute) && $attribute != "id" ) {
              $extra->$attribute = $value;
            }  
             
            
             
            //  if($attribute == "checked" && $attribute != "true" ) {
            //   $extra->checked = "false";
            //   error_log(print_r("setting false" ,true));
            // } 
            

          }

          $extra->Orders_ID =  $orderID  ;
          $extra->OrderDishInfo_id = $dishinfosaved;
          
          if ($object->StoringAppOrder == true) {
            $extra->Extra_id =   $object->Extra_id;
           // unset($extra->OrdersExtraI_id);
          }else {
            $extra->Extra_id =  $object->id;
          }
  
       //unset( $extra->OrdersExtraI_id);

      $extra->save();
      //return  $extra;
    

}


public static function TotalExtrasPrice ($orderID="") {
  //error_reporting(-1);
  global $database;
 
  
  $sql = "SELECT `OrderDishInfo_id`,`Price` FROM `OrdersExtra` WHERE `Orders_ID`='".$orderID."' AND `checked`='1' ";
  $ExtrasForOrder =  $database->query($sql) ;

  //  foreach (mysqli_fetch_all($ExtrasForOrder,MYSQLI_ASSOC) as $key=>$value) {
     
  //   $Total = $value['Price'] + $Total;

  //  }
   //error_log(print_r('Total Price',true));

   
    return mysqli_fetch_all($ExtrasForOrder,MYSQLI_ASSOC);

 


   

}


public static function GetDishExtras_TOTAL ($OrderDishInfo_id="", $OrderID="") {
  global $database;
  $sql = "SELECT `Price` FROM `OrdersExtra` WHERE `checked`=1 AND `Orders_ID`='".$OrderID."' AND `OrderDishInfo_id`= '".$OrderDishInfo_id."' ";

  $TOTAL_EXTRA_ForDish = mysqli_fetch_all( $database->query($sql),MYSQLI_ASSOC);
   
  $FINAL_TOTAL=0.00;

  foreach ($TOTAL_EXTRA_ForDish  as $key => $value) {
    //error_log(print_r("SHOW ME THE value",true));
    //error_log(print_r($value['Price']  ,true));
   $FINAL_TOTAL= $FINAL_TOTAL+$value['Price'];
  }

  //  error_log(print_r("SHOW ME THE total",true));
  //    error_log(print_r(number_format(round((float)$FINAL_TOTAL,2),2) ,true));
 return number_format(round((float)$FINAL_TOTAL,2),2) ;

  
}


public static function FindOrderExtra ($orderID="", $OrderDishInfo_id="") {
  //error_log(print_r( "OrderDishInfo_id fron inside extra"  ,true));
 // error_log(print_r($OrderDishInfo_id,true));
  $extras = self::find_by_sql(' SELECT * FROM `OrdersExtra` WHERE `Orders_ID` = "'.$orderID.'" AND `OrderDishInfo_id` = "'.$OrderDishInfo_id.'" ' ,"" )  ;
  foreach(  $extras as $attribute =>$value){
    $value ->id =  $value ->Extra_id;
  }

  return  $extras ;


}


  public static function createProperty( $name, $value ){
     $this->{$name} = $value;
}

  public static function setGetField ($idf) {
    return self::$db_fields['id'] = $idf;
  }
	
  public function full_name() {
    if(isset($this->First_Name) && isset($this->Last_Name)) {
      return $this->First_Name . " " . $this->Last_Name;
    } else {
      return "";
    }
  }

  



}

?>