<?php
class COA{

	private $conn;
	private $table_name = "coa";

	public $id;	
	public $type_of_expense;	
	public $account_type;	
	public $account_code;	
	public $account_head;
	public $debit;	
	public $credit;		
	public $entry_by;
	public $entry_date_time;

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

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET type_of_expense=:type_of_expense, account_type=:account_type, account_code=:account_code, account_head=:account_head, debit=:debit, credit=:credit, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":type_of_expense", $this->type_of_expense);
		$stmt->bindParam(":account_type", $this->account_type);
		$stmt->bindParam(":account_code", $this->account_code);
		$stmt->bindParam(":account_head", $this->account_head);
		$stmt->bindParam(":debit", $this->debit);
		$stmt->bindParam(":credit", $this->credit);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);

		if($stmt->execute()){
			return true;
		}	 
		return false;
	}

	function update(){
		$query = "UPDATE " . $this->table_name . " SET type_of_expense=:type_of_expense, account_head=:account_head, debit=:debit, credit=:credit, entry_by=:entry_by, entry_date_time=:entry_date_time where id=:id";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":id", $this->id);
		$stmt->bindParam(":type_of_expense", $this->type_of_expense);
		$stmt->bindParam(":account_head", $this->account_head);
		$stmt->bindParam(":debit", $this->debit);
		$stmt->bindParam(":credit", $this->credit);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);
		
		if($stmt->execute()){
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;
	}

	function exist(){
		$query = "SELECT id FROM " . $this->table_name . " WHERE  type_of_expense= :type_of_expense and account_type= :account_type and account_head= :account_head LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );	
	 
		$stmt->bindParam(":type_of_expense", $this->type_of_expense);
		$stmt->bindParam(":account_type", $this->account_type);
		$stmt->bindParam(":account_head", $this->account_head);
		
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}

	function getAllRows(){
		$condition = "";
		if($this->account_type){
			$condition = "where account_type=:account_type";
		}
	
		$query = "SELECT * FROM coa " . $condition . " order by account_code";
		$stmt = $this->conn->prepare($query);
		if($this->account_type) $stmt->bindParam(":account_type", $this->account_type);
		$stmt->execute();
		return $stmt;
	}

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

// 	function SearchGeneralLedger(){
// 				$query = "select coa.*,cc.debit,cc.credit from coa left join (SELECT account_type,account_code,account_head,total*debit AS debit,total*credit AS credit FROM 
// 				(SELECT ifnull(account_type,'') AS account_type,a.account_code,ifnull(a.account_head,'') AS account_head,
// 				total,ifnull(debit,0) AS debit,IFNULL(credit,0) AS credit FROM 
// 				(SELECT account_code,account_head, SUM(quantity*unit_price) AS total
// 				FROM expense
// 				WHERE `date`>=:df AND `date`<=:dt
// 				GROUP BY account_code
// 				UNION
// 				SELECT account_code,account_head, SUM(quantity*unit_price) AS total
// 				FROM income
// 				WHERE `date`>=:df AND `date`<=:dt
// 				GROUP BY account_code) AS a LEFT JOIN coa ON a.account_code=coa.account_code) AS b
// 				UNION
// 				SELECT coa.account_type,journal_detail.account_code,coa.account_head,SUM(journal_detail.debit) AS debit,SUM(journal_detail.credit) AS credit
// 				FROM (select * from journal_detail where is_deleted=0) as journal_detail
// 				LEFT JOIN coa ON journal_detail.account_code=coa.account_code
// 				WHERE `journal_date`>=:df AND `journal_date`<=:dt
// 				GROUP BY account_code ORDER BY account_code) as cc on coa.account_code=cc.account_code order by coa.account_code


// ";

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

}
?>
