<?

class Nobre_AvisoencerramentocolheitaController extends Agro_Controller_Action{
	
	public function init() {
		$this->view->title = 'Sinistro » Avisos » Encerramento de Colheita';	
		$this->user = Zend_Auth::getInstance()->getIdentity();
	}
	
	public function getModel() {
		return new Sinistro_Model_AvisosEncerramento();
	}

	public function indexAction() {
		$this->view->title .= " » Listagem";
		$this->view->formPesquisaProposta = new Sinistro_Form_PesquisaProposta();
	}

	public function formAction() {
		$this->view->back = "/sinistro/avisoencerramentocolheita";
		$this->view->title .= " » Formulário";
		
		$this->view->processo = $this->getRequest()->getParam('processo');
		$this->view->proposta = $this->getRequest()->getParam('proposta');
		
		$User = Zend_Auth::getInstance()->getIdentity();
	
		$Model = $this->getModel();
		$this->view->itensSegurados = $Model->getItensSeguradosAvisosEncerramento($this->view->processo, $this->view->proposta);

		$this->view->listDadosItens = array();
		if ($this->getRequest()->isPost()) {
			$errorMsg = $this->verificaData($this->getRequest()->getParams());
			if ($errorMsg) {
				$this->populaCampos($this->getRequest()->getParams());
				$this->view->showMessage($errorMsg, 'error');
			} else {			
				$this->save($this->getRequest()->getParams());
			}
		}
	}

	public function verificaData($post) {
		
		$dt1 = explode('/', date('d/m/Y'));
		$dt2 = explode('/', $post['dt_encerramento_colheita']);
		
		$dtAtual    = $dt1[2] . $dt1[1] . $dt1[0];
		$dtColheita = $dt2[2] . $dt2[1] . $dt2[0];
		
		$msg = '';
		if ($dtColheita > $dtAtual) {
			$msg = 'A data de colheita não pode ser maior que a data atual.';
		} 
		
		return $msg;		
	}

	public function getFilter() {
		$where = array();
		$filter = array();
		
		if ($this->getRequest()->getParam('tipo_pesquisa') && $this->getRequest()->getParam('valor_pesquisa')) {
			$filter['field'] = $this->getRequest()->getParam('tipo_pesquisa');
		
			if(is_numeric($this->getRequest()->getParam('valor_pesquisa'))){
				$filter['operator'] = '=';
				$filter['value'] = $this->getRequest()->getParam('valor_pesquisa');
			}else{
				$filter['operator'] = 'ilike';
				$filter['value'] = '%'.$this->getRequest()->getParam('valor_pesquisa').'%';
			}
		
			$where[] = $filter;
		}
		if ($this->user->tp_usuario == 'C') {
			$filter['field'] = 'id_corretor';
			$filter['operator'] = '=';
			$filter['value'] = $this->user->id;
				
			$where[] = $filter;
		} else if ($this->user->tp_usuario == 'P') {
			$filter['field'] = 'id_usuario_criacao';
			$filter['operator'] = '=';
			$filter['value'] = $this->user->id;
				
			$where[] = $filter;
		}

		return $where;
	}
	
	public function save(array $post) {
		
		$Db = Agro_Db_Table_Abstract::getDefaultAdapter();
		$Db->beginTransaction();
		try {
			/****************************************************/
			// Tabela sinistro.avisos_encerramento
			/***************************************************/
			$AvisoEncerramento = new Sinistro_Model_AvisosEncerramento();
			
			$data = array();			
			$data['id_proposta'] = $post['proposta'];
			$data['dt_encerramento_colheita'] = Agro_Util::formatDate($post['dt_encerramento_colheita'], 'yyyy-MM-dd');
			
			$idAvisoEncerramento = $AvisoEncerramento->insert($data);
			
			if ($post['itens_segurados']) {
				/****************************************************/
				// Tabela sinistro.avisos_encerramento_itens_segurados
				/***************************************************/
				$AvisoItemSegurado = new Sinistro_Model_AvisosEncerramentoItensSegurados();

				foreach ($post['itens_segurados'] as $value) {
					$idItem = $value;

					$item = array();
					$item['id_aviso_encerramento']    = $idAvisoEncerramento;
					$item['id_item_segurado']   = $idItem;
					
					$AvisoItemSegurado->insert($item);
				}
			}
			$Db->commit();
			
			$this->view->showMessage(MSG_SUCCESS, 'success');
			$this->forward($this->_forwardSettings);
		} catch (Exception $e) {
			$Db->rollback();
			$this->populaCampos($post);
			$this->view->showMessage(MSG_ERROR, 'error');
		}
	}

	public function populaCampos($post) {
		
		$this->view->dt_colheita = $post['dt_colheita'];

		if ($post['itens_segurados']) {
			foreach ($post['itens_segurados'] as $item) {
				$idItem = $item;
			
				$this->view->listDadosItens[$idItem] = $idItem;
			}
		}
	}
	
	public function viewAction() {
	    $this->getHelper('layout')->setLayout('popup');
	    
	    $idAvisoEncerramento = $this->getRequest()->getParam('id');
	    
	    $AvisosEncerramento = new Sinistro_Model_AvisosEncerramento();
	    $this->view->data = $AvisosEncerramento->getDataViewAvisoEncerramento($idAvisoEncerramento);
	}
}