<?

class Safra12_Model_AvaliacaoDanosDesfolhamento extends Agro_Db_Table_Abstract {
	protected $_schema = 'safra12';
	protected $_name = 'avaliacao_danos_desfolhamento';
	protected $_primary = array('id_cultura', 'ds_estadio', 'vl_percent_desfolhamento');
	public $_order = 'id_cultura';
		
	public function find($idCultura, $dsEstadio, $vlPercentDesfolhamento) {
		if ($dsEstadio && $vlPercentDesfolhamento) {
			$select = $this->select();
			$select->setIntegrityCheck(false);
			$select->from(array('av'=>"$this->_schema.$this->_name"), '*');
			$select->where('av.id_cultura = ?', $idCultura);
			$select->where('av.ds_estadio = ?', $dsEstadio);
			$select->where('av.vl_percent_desfolhamento_min <= ?', $vlPercentDesfolhamento);
			$select->where('av.vl_percent_desfolhamento_max >= ?', $vlPercentDesfolhamento);
			
			if ($this->fetchRow($select)) {
				return $this->fetchRow($select)->toArray();
			} else {
				return array();
			}
		} else {
			
			return array();
		}
	}

	public function getComboEstadio($idCultura) {

		$select = "
			select 
				av.ds_estadio
			from 
				$this->_schema.$this->_name av
			where
				id_cultura = $idCultura
			group by av.ds_estadio
			order by av.ds_estadio
		";

		return $this->_db->fetchAll($select);
	}

	public function getComboPercentDesfolhamento($idCultura) {

		$select = "
			select 
				av.vl_percent_desfolhamento
			from 
				$this->_schema.$this->_name av
			where
				id_cultura = $idCultura
			group by av.vl_percent_desfolhamento
			order by av.vl_percent_desfolhamento
		";

		return $this->_db->fetchAll($select);
	}		
}