<?php

abstract class Agro_Db_Table_Safras extends Agro_Db_Table_Abstract {
	protected $_rowClass = 'Agro_Db_Table_Row_Safra';
	protected $_schema = 'produto';
	protected $_name = 'safras';
	protected $_primary = 'id';
	public $safraCurrent = 0;
	
	public function init() {
		$this->safraCurrent = $this->getDefaultAdapter()->fetchOne("select id from produto.safras where fl_vigente is true order by dt_inicio desc limit 1");
	}
	
	public function getCombo($limit=0, $first=false, $exibeOpcaoTodas=false, $exibeOpcao3Ultimas=false) {
		$return = array();
		
		if(Zend_Auth::getInstance()->getIdentity()->ds_chave == 'SEGURADORA_ESSOR'){
			$stFiltro = " and id >= 16";
		}
		if(Zend_Auth::getInstance()->getIdentity()->ds_chave == 'SEGURADORA'){
			$stFiltro = " and id = 15";				
		}		
		
		if ($limit) {
			$Rs = $this->getDefaultAdapter()->fetchPairs("SELECT id, ds_nome_safra FROM produto.safras WHERE (fl_ativo = true) $stFiltro ORDER BY fl_vigente DESC, id DESC LIMIT $limit");
		} else {
			$Rs = $this->getDefaultAdapter()->fetchPairs("SELECT id, ds_nome_safra FROM produto.safras WHERE (fl_ativo = true) $stFiltro ORDER BY fl_vigente DESC, id DESC");
		}
		
		if (count($Rs)) {
			if ($first) {
				$return = array('' => '--- Selecione uma safra ---', '');	
			}

			if ($exibeOpcaoTodas){
			    $return['TODAS'] = "--- Todas ---";
			}

			if ($exibeOpcao3Ultimas) {
				$return['TRES_ULTIMAS'] = "Três Últimas";
			}
			
			foreach ($Rs as $value => $text) {
				$return[$value] = $text;
			}
		} 
		
		return $return;
	}
	
}