<?php
class ProductData {
	public static $tablename = "product";

	public function ProductData(){
		$this->name = "";
		$this->image = "";
		$this->price_in = "";
		$this->price_out = "";
		$this->unit = "";
		$this->user_id = "";
		$this->presentation = "0";
		$this->created_at = "NOW()";
	}

	public function getCategory(){ return CategoryData::getById($this->category_id);}

	public function add(){
		$sql = "insert into ".self::$tablename." (image,section_id,duration,barcode,name,description,price_in,price_out,user_id,presentation_id,unit_id,category_id,inventary_min,is_favorite,use_ingredients,use_inventory,created_at) ";
		$sql .= "value (\"$this->image\",$this->section_id,\"$this->duration\",\"$this->barcode\",\"$this->name\",\"$this->description\",\"$this->price_in\",\"$this->price_out\",$this->user_id,$this->presentation_id,$this->unit_id,$this->category_id,$this->inventary_min,$this->is_favorite,$this->use_ingredients,$this->use_inventory,NOW())";
		return Executor::doit($sql);
	}

	public function add_ingredient(){
		$sql = "insert into ".self::$tablename." (image,barcode,name,description,price_in,price_out,user_id,presentation_id,unit_id,category2_id,inventary_min,is_ingredient,created_at) ";
		 $sql .= "value (\"$this->image\",\"$this->barcode\",\"$this->name\",\"$this->description\",\"$this->price_in\",\"$this->price_out\",$this->user_id,$this->presentation_id,$this->unit_id,$this->category2_id,$this->inventary_min,1,NOW())";
		return Executor::doit($sql);
	}




	public static function delById($id){
		$sql = "delete from ".self::$tablename." where id=$id";
		Executor::doit($sql);
	}
	public function del(){
		$sql = "delete from ".self::$tablename." where id=$this->id";
		Executor::doit($sql);
	}

// partiendo de que ya tenemos creado un objecto ProductData previamente utilizamos el contexto
	public function update(){
		$sql = "update ".self::$tablename." set barcode=\"$this->barcode\",name=\"$this->name\",price_in=\"$this->price_in\",price_out=\"$this->price_out\",section_id=$this->section_id,unit_id=$this->unit_id,presentation_id=$this->presentation_id,category_id=$this->category_id,inventary_min=\"$this->inventary_min\",description=\"$this->description\",is_active=\"$this->is_active\",is_favorite=\"$this->is_favorite\",use_ingredients=\"$this->use_ingredients\",use_inventory=\"$this->use_inventory\",duration=\"$this->duration\" where id=$this->id";
		Executor::doit($sql);
	}

	public function update_ingredient(){
		$sql = "update ".self::$tablename." set barcode=\"$this->barcode\",name=\"$this->name\",price_in=\"$this->price_in\",price_out=\"$this->price_out\",unit_id=$this->unit_id,presentation_id=$this->presentation_id,category2_id=$this->category2_id,inventary_min=\"$this->inventary_min\",description=\"$this->description\",is_active=\"$this->is_active\",is_favorite=\"$this->is_favorite\",use_ingredients=\"$this->use_ingredients\",use_inventory=\"$this->use_inventory\",duration=\"$this->duration\" where id=$this->id";
		Executor::doit($sql);
	}
	public function del_category(){
		$sql = "update ".self::$tablename." set category_id=NULL where id=$this->id";
		Executor::doit($sql);
	}


	public function update_image(){
		$sql = "update ".self::$tablename." set image=\"$this->image\" where id=$this->id";
		Executor::doit($sql);
	}

	public static function getById($id){
		$sql = "select * from ".self::$tablename." where id=$id";
		$query = Executor::doit($sql);
		return Model::one($query[0],new ProductData());

	}



	public static function getAll(){
		$sql = "select * from ".self::$tablename." where is_ingredient=0";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getAllActive(){
		$sql = "select * from ".self::$tablename." where is_ingredient=0 and is_active=1";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getInventory(){
		$sql = "select * from ".self::$tablename." where is_ingredient=0 and use_inventory=1";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getInventoryIng(){
		$sql = "select * from ".self::$tablename." where is_ingredient=1";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getFavorites(){
		$sql = "select * from ".self::$tablename." where is_ingredient=0 and is_favorite=1";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getIngredients(){
		$sql = "select * from ".self::$tablename." where is_ingredient=1";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getAllByCategoryId($id){
		$sql = "select * from ".self::$tablename." where category_id=$id";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getAllByPage($start_from,$limit){
		$sql = "select * from ".self::$tablename." where id>=$start_from and is_ingredient=0 limit $limit";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getInventoryByPage($start_from,$limit){
		$sql = "select * from ".self::$tablename." where id>=$start_from and is_ingredient=0 and use_inventory=1 limit $limit";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getInventoryIngByPage($start_from,$limit){
		$sql = "select * from ".self::$tablename." where id>=$start_from and is_ingredient=1 limit $limit";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getLike($p){
		$sql = "select * from ".self::$tablename." where (barcode like '%$p%' or name like '%$p%' or id like '%$p%') and is_ingredient=0";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

	public static function getReLike($p){
		$sql = "select * from ".self::$tablename." where (barcode like '%$p%' or name like '%$p%' or id like '%$p%') and (use_inventory=1 or is_ingredient=1)";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}


	public static function getAllByUserId($user_id){
		$sql = "select * from ".self::$tablename." where user_id=$user_id order by created_at desc";
		$query = Executor::doit($sql);
		return Model::many($query[0],new ProductData());
	}

}

?>