<?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 Consumables extends DatabaseObject {
	
  protected static $table_name="Consumables";
  
  //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 = "";

	// 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 GetStockToReduce (	$ItemType="", $ItemType_id ="") {
    global $database;

    $sql = "SELECT `Stock_id`,`Amount` FROM `Consumables` WHERE `Dish_Type`='".$ItemType."' AND `Dish_id`='".$ItemType_id."' ";

    return  $database->query($sql);
    //we need to pass to stock class
  }

	

  public static function  SaveConsumables ($Consumable="",$Dish_Type="",$id="" ) {
    
        foreach($Consumable as $attribute){
            $newConsumable = new self ("Blank");
        
            // if(isset($Consumable["id"])){
            //     error_log(print_r("We have id of consumable",true));
            //     $newConsumable->id=$Consumable["id"];
            // }
        
            $newConsumable->Dish_Type=$Dish_Type;
            $newConsumable->Dish_id=$id;
            //$newConsumable->Dish_id= "1";
            $newConsumable->id=$attribute->id;
            $newConsumable->Stock_id=$attribute->Stock_id;
         
            $newConsumable->Amount= $attribute->Amount;
            $newConsumable->Unit= $attribute->Unit;
            $newConsumable->Name= $attribute->Name;
        
            $newConsumable->save();
             //we need to increase the used_by field on stock
            //to match the number of entries in the consumable table
            Stock::Increase_Used_by($attribute->Stock_id);
        }
    
    
    
    }

}

?>