<?php
/**
 * Model for Payroll
 *
 * @author Mills ABeiku Godfred
 */
class Payroll
{
	
	public $conn;
	

	
	function __construct()
	{
		$this->conn = Database::connect();
    }
    
    public function add_payroll(array $data){
		$sql = "INSERT INTO payroll ";
		$fields = array_keys($data);
		$vals = array_values($data);

		$sql .= "(".implode(",", $fields).")";
		$arr = array();
		foreach ($fields as $f) {
			$arr[] = "?";
		}
		$sql .= " VALUES(".implode(",", $arr).")";

		$statement = $this->conn->prepare($sql);

		foreach ($vals as $i => $v) {
			$statement->bindValue($i + 1, $v);
		}

		return $statement->execute();

    }
    
    public function update_payroll(array $key_col_vals, array $values) {
        
        $sql = "UPDATE payroll SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $key_cols = array_keys($key_col_vals);
        $key_vals = array_values($key_col_vals);
        foreach ($fields as $i=>$f) {
            $fields[$i] .= " = ? ";
        }

        $sql .= implode(",", $fields);
        $sql .= " WHERE " ;
        
        foreach ($key_cols as $i=>$f) {
            $key_cols[$i] .= " = ? ";
        }
        
        $sql .= implode(" AND ", $key_cols);

        $statement = $this->conn->prepare($sql);
        foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
        }
        
        foreach ($key_vals as $i=>$v) {
           if($i==0){
               $statement->bindValue((count($fields)+$i+1), $v);
            }else{
               $statement->bindValue((count($fields)+$i), $v);
            }
        }
        
       return  $statement->execute();

    }


    public function get_payroll_by_name($payroll_name){
		$sql = "SELECT * FROM payroll WHERE payroll_name LIKE '%".$payroll_name."%'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}
	
	
	public function read_all_payrolls(){
		$sql = "SELECT z.*,a.* FROM payroll z
        JOIN employee a ON z.employee_id=a.employee_id";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    public function read_payroll($payroll_id,$current_month){
		$sql = "SELECT a.*,b.* FROM payroll a 
		JOIN employee b ON a.employee_id=b.employee_id 
		WHERE MONTH(a.payment_date)=:current_month  
		AND a.payroll_id=:payroll_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":payroll_id",$payroll_id);
		$statement->bindParam(":current_month",$current_month);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

	public function read_user_payroll($employee_id,$current_month){
		$sql = "SELECT a.*,b.* FROM payroll a 
		JOIN employee b ON a.employee_id=b.employee_id 
		WHERE MONTH(a.payment_date)=:current_month  
		AND a.employee_id=:employee_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":employee_id",$employee_id);
		$statement->bindParam(":current_month",$current_month);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

	public function remove_payroll($payroll_id){
		$sql = "DELETE FROM payroll WHERE payroll_id=:payroll_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":payroll_id",$payroll_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

}