<?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 Stock extends DatabaseObject {
	
  protected static $table_name="Stock";
 
  
  //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 testGetField () {
    return self::$db_fields;
  }

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

  public static function setGetField ($idf) {
    return self::$db_fields['id'] = $idf;
  }

  public static function GetmyLowStock (){
    global $database;
    $sql = "SELECT `Availability` FROM `Stock` WHERE `Availability`<=10";
     $OrderReadyCount =   $database->query($sql);
    
     return $OrderReadyCount->num_rows;

  }

  public static function ReduceStock($OrderDishInfo_id=""){
    global $database;
    //  serror_log(print_r( "ReduceStock",true));
  
   $dishInfo =  mysqli_fetch_all(OrdersInfo::FindDishItemType ($OrderDishInfo_id),MYSQLI_ASSOC) ;
   //error_log(print_r( $dishInfo,true));
   
   if ($dishInfo['0']['ItemType'] == "SideDish" ) {
    SideDishes::Last_Consumed ($dishInfo['0']['ItemType_id'] );
   } else if ($dishInfo['0']['ItemType'] == "Menu_Dishes" ){
    MenuDishes::Last_Consumed ($dishInfo['0']['ItemType_id'] );
   }
   
   //since we're reducing stock
   // this mean is the last_consumed
 //$retVal = (condition) ? a : b ;

 
   $stockInfo =  mysqli_fetch_all(Consumables::GetStockToReduce ( $dishInfo['0']['ItemType']=='Menu_Dishes' ? 'MenuDish':'SideDish' , $dishInfo['0']['ItemType_id']) ,MYSQLI_ASSOC);

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

    $result;
    if (!empty($stockInfo)) {
      foreach ($stockInfo as $key => $value) {
        // error_log(print_r( "stock in look",true));
        // error_log(print_r( $value,true));
        $Stock_id= $value['Stock_id'];
        $Amount=$value['Amount'];
  
        $sql = "UPDATE `Stock` SET `Available_Qt`=`Available_Qt`-$Amount, `Last_Consumed`=NOW(),`Availability`=(`Available_Qt`/`Original_Qt`)*100 WHERE `id`='".$Stock_id."' ";
        error_log(print_r( "reducing stock ".$sql,true));
        $result =$database->query($sql);
      }
  
    } else {
      error_log(print_r("Dish not Linked to consumable",true));
    }
    
    

    //$sql = "UPDATE `Stock` SET `Available_Qt`=`Available_Qt`-'".$Amount."', `Last_Consumed`=NOW(),`Availability`=(`Available_Qt`/`Original_Qt`)*100 WHERE `id`='".$Stock_id."' ";
    return $result;
    
}


	
  public function full_name() {
    if(isset($this->First_Name) && isset($this->Last_Name)) {
      return $this->First_Name . " " . $this->Last_Name;
    } else {
      return "";
    }
  }

  



}

?>