<?php
/**
 * Class that operate on table 'project'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2016-06-21 12:04
 */
class ProjectMySqlExtDAO extends ProjectMySqlDAO{

	public function queryBydel($del){
		$sql = 'SELECT project.*, projectkindname FROM project LEFT JOIN projectkind ON projectkind.projectkindid = project.`projectkindid` WHERE del = '.$del.' order by id desc';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	public function queryBydel2($search_params = ''){
		$sql = 'SELECT project.*, projectkindname, sum(incomeValue) as sumincomevalue, sum(expensesValue) as sumexpensesvalue, sum(projectexchmaterial.totalbuyprice) as projectexchmaterialtotal
		, sum(projectexchmaterialreturn.totalbuyprice) as projectexchmaterialreturntotal
		, sum(supplierdebtchangeamount) as supplierdebtchangeamounttotal
		 FROM project 
		LEFT JOIN projectkind ON projectkind.projectkindid = project.`projectkindid` 
		LEFT JOIN income on income.costcenterid = project.id 
		LEFT JOIN expenses on expenses.costcenterid = project.id
		LEFT JOIN projectexchmaterial on projectexchmaterial.projectid = project.id
		LEFT JOIN projectexchmaterialreturn on projectexchmaterialreturn.projectid = project.id
		LEFT JOIN supplierdebtchange on supplierdebtchange.costcenterid = project.id and supplierdebtchange.del = 0
		 WHERE project.del = 0 '. $search_params .'  order by project.id desc';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}


	public function queryAllData( $search_params = '', $search_orders = '', $search_joins = '', $search_groups = ''){
		$sql = 'SELECT project.*, projectkindname, employeename
		 FROM project 
		LEFT JOIN projectkind ON projectkind.projectkindid = project.`projectkindid` 
		LEFT JOIN user ON project.userid = user.userid 
		'.$search_joins.'
		 WHERE project.del = 0 '. $search_params . '  ' . $search_groups . '  '. $search_orders.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}




	public function queryBydel3($search_params = ''){
		$sql = 'SELECT project.*, projectkindname
		 FROM project 
		LEFT JOIN projectkind ON projectkind.projectkindid = project.`projectkindid` 
		LEFT JOIN income on income.costcenterid = project.id 
		LEFT JOIN expenses on expenses.costcenterid = project.id
		LEFT JOIN projectexchmaterial on projectexchmaterial.projectid = project.id
		LEFT JOIN projectexchmaterialreturn on projectexchmaterialreturn.projectid = project.id
		LEFT JOIN supplierdebtchange on supplierdebtchange.costcenterid = project.id and supplierdebtchange.del = 0
		 WHERE project.del = 0 '. $search_params .'  order by project.id desc';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	


        
        
        public function updateentry($entryid ,$proid){
		$sql = 'UPDATE project SET entryid = '.$entryid.'  WHERE id = '.$proid.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
        
        
      
    
    public function getprojectsExt($name){
		$sql = 'SELECT id,name FROM project WHERE name LIKE "%' . $name . '%"
				AND del=0 and finish =0
				AND name != ""';
                
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}



	public function queryWithConditionWithQueryString($queryString){
		$sql = 'SELECT id,name FROM project WHERE del=0 and finish =0
				AND name != "" ' . $queryString . ' ';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
        
     public function getprojectsforreportExt($name){
		$sql = 'SELECT id,name FROM project WHERE name LIKE "%' . $name . '%"
				AND del=0 
				AND name != ""';
                
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

    public function queryByFinishAndDel($finished, $deleted) {
        $sql = 'SELECT * FROM project WHERE finish = ? and del = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->setNumber($finished);
        $sqlQuery->setNumber($deleted);
        return $this->getList($sqlQuery);
    }

        
        public function updatefinished($finish,$proid){
		$sql = 'UPDATE project SET finish = '.$finish.'  WHERE id = '.$proid.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
        
        public function updatedel($del,$proid){
		$sql = 'UPDATE project SET del = '.$del.'  WHERE id = '.$proid.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
        
        
        public function queryByNameadd($value){
		$sql = 'SELECT * FROM project WHERE name = ? AND del=0 and finish =0';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}
}
?>