<?php
class ItemSalesDetail{
	    // database connection and table item_name
	private $conn;
	private $table_name = "item_sales_detail";

	// object properties

	public $id;
	public $sales_date;
	public $voucher_no;
	public $item_code;
	public $item_name;
	public $store;
    public $quantity;
    public $price;


	public function __construct($db){
		$this->conn = $db;
	}

	function create(){
		$query = "INSERT INTO `" . $this->table_name . "` SET sales_date=:sales_date, voucher_no=:voucher_no, item_code=:item_code, item_name=:item_name, store=:store
        , quantity=:quantity, price=:price ";

		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":sales_date", $this->sales_date);
		$stmt->bindParam(":voucher_no", $this->voucher_no);
		$stmt->bindParam(":item_code", $this->item_code);
        $stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":store", $this->store);
        $stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":price", $this->price);


		if($stmt->execute()){	
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;		
    }
    
	function getSalesDetail(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE voucher_no=:voucher_no";

		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":voucher_no", $this->voucher_no);
		$stmt->execute();
		return $stmt;
	}

	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE voucher_no =:voucher_no";
		$stmt = $this->conn->prepare($query);
		$this->voucher_no = htmlspecialchars(strip_tags($this->voucher_no));
		$stmt->bindParam(":voucher_no", $this->voucher_no);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	function search(){
		$condition = "";

		if($this->df){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " sales_date >=:df";
		}
		if($this->dt){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " sales_date <=:dt";
		}
		if($this->category){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " category =:category";
		}
		if($this->brand){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " brand =:brand";
		}
		if($this->item_code){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " sales_detail.item_code =:item_code";
		}
		if($this->item_name){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (sales_detail.item_name LIKE  :item_name '%' or sales_detail.item_name LIKE '%' :item_name '%' or sales_detail.item_name Like '%' :item_name ) ";
		}


		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT sales_date,store,category,brand FROM " . $this->table_name . " left join item on sales_detail.item_code=item.item_code " . $condition . " ORDER BY sales_date,store,category,brand,item_code ";
		$stmt = $this->conn->prepare($query);
		$this->item_code=htmlspecialchars(strip_tags($this->item_code));
		$this->item_name=htmlspecialchars(strip_tags($this->item_name));

		if($this->df) $stmt->bindParam(":df", $this->df);
		if($this->dt) $stmt->bindParam(":dt", $this->dt);
		if($this->category) $stmt->bindParam(":category", $this->category);
		if($this->brand) $stmt->bindParam(":brand", $this->brand);
		if($this->item_code) $stmt->bindParam(":item_code", $this->item_code);
		if($this->item_name) $stmt->bindParam(":item_name", $this->item_name);


		$stmt->execute();
		return $stmt;
	}


function searchItemsales(){
		$condition = "";

		if($this->df){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " sales_detail.sales_date >=:df";
		}
		if($this->dt){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " sales_detail.sales_date <=:dt";
		}
		if($this->category){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " category =:category";
		}
		if($this->brand){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " brand =:brand";
		}
		if($this->item_code){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " sales_detail.item_code =:item_code";
		}
		if($this->item_name){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (sales_detail.item_name LIKE  :item_name '%' or sales_detail.item_name LIKE '%' :item_name '%' or sales_detail.item_name Like '%' :item_name ) ";
		}


		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT sales_detail.*,car_no,NAME,phone,address,total_amount,payment,category,brand FROM sales_detail LEFT JOIN sales ON sales_detail.voucher_no=sales.voucher_no 
		LEFT JOIN item ON sales_detail.item_code=item.item_code " . $condition . " order by sales_detail.sales_date,voucher_no,category,brand,sales_detail.item_code";
		$stmt = $this->conn->prepare($query);

		if($this->df) $stmt->bindParam(":df", $this->df);
		if($this->dt) $stmt->bindParam(":dt", $this->dt);
		if($this->category) $stmt->bindParam(":category", $this->category);
		if($this->brand) $stmt->bindParam(":brand", $this->brand);
		if($this->item_code) $stmt->bindParam(":item_code", $this->item_code);
		if($this->item_name) $stmt->bindParam(":item_name", $this->item_name);


		$stmt->execute();
		return $stmt;
	}

}

?>