<?php
class PRODUCT{

  var $id, $name, $ename, $photo, $price, $stock, $pstatus;

  function __construct(){
	  $this->id = 0;
	  $this->name = "";
	  $this->ename = "";
	  $this->photo = "";
	  $this->price = "";
	  $this->stock = "";
	  $this->pstatus = "";
	  $this->tb_order = 0;
  }
  
  function setProduct($pfield,$pval){
    global $conn;
	$query = "SELECT * FROM tb_product WHERE $pfield='$pval'";
	$result = mysqli_query($conn,$query);
	$num = mysqli_num_rows($result);
	if($num>0){
	while($myrow = mysqli_fetch_array($result)){
	  $this->id = $myrow["id"];
	  $this->name = $myrow["name"];
	  $this->ename = $myrow["ename"];
	  $this->photo = $myrow["photo"];
	  $this->price = $myrow["price"];
	  $this->stock = $myrow["stock"];
	  $this->pstatus = $myrow["pstatus"];
	  $this->tb_order = $myrow["tb_order"];
	}
	}
  }
  
  function updateProduct(){
    global $conn;
	$query = "UPDATE tb_product SET name='".$this->name."', ename='".$this->ename."', photo='".$this->photo."', price='".$this->price."', stock='".$this->stock."', pstatus='".$this->pstatus."', tb_order='".$this->tb_order."' WHERE id='".$this->id."'";
	//echo $query . "<br>";
	mysqli_query($conn,$query);
  }
  
  function saveProduct(){
    global $conn;
	$query = "INSERT INTO tb_product VALUES (null,'".$this->name."','".$this->ename."','".$this->photo."','".$this->price."','".$this->stock."','".$this->pstatus."','".$this->tb_order."')";
	mysqli_query($conn,$query);
  }
  
  function deleteProduct($pid){
    global $conn;
	$query = "DELETE FROM tb_product WHERE id='$pid'";
	mysqli_query($conn,$query);
  }
  
  function getProducts($pval){
    global $conn;
	$query = "SELECT * FROM tb_product $pval";
	$result = mysqli_query($conn,$query);
	$num = mysqli_num_rows($result);
	if($num>0){
	while($myrow = mysqli_fetch_array($result)){
	  $tid = $myrow["id"];
	  $output[$tid]["name"] = $myrow["name"];
	  $output[$tid]["ename"] = $myrow["ename"];
	  $output[$tid]["photo"] = $myrow["photo"];
	  $output[$tid]["price"] = $myrow["price"];
	  $output[$tid]["stock"] = $myrow["stock"];
	  $output[$tid]["pstatus"] = $myrow["pstatus"];
	  $output[$tid]["tb_order"] = $myrow["tb_order"];
	}
	}
	return $output;
  }
    
  function getRinggit($pval){
      return "RM " . sprintf("%.2f",$pval);
  }
  
  function getInvoiceNum(){
  	global $conn;
	$query = "SELECT MAX(billno) FROM tb_order";
	$result = mysqli_query($conn,$query);
	while($myrow = mysqli_fetch_array($result)){
	  $output = $myrow["MAX(billno)"];
	}
	return ($output+1);
  }
  
  function deductProduct($pid,$pval){
    global $conn;
	
	$this->stock = $this->stock - $pval;
	$this->updateProduct();
  }
	
}
?>