<?php

class Produto_ValidacoessubvencaocomercialController extends Agro_Controller_Action {

	public function init() {
		$this->view->id = $this->getRequest()->getParam('id');
		$this->id = $this->getRequest()->getParam('id');

		$ModelValidacao = new Produto_Model_Validacoes();
		$Validacao = $ModelValidacao->find($this->id_validacao)->current();
		$this->view->title = "Produto » Cadastro » Validações » Configurar » Limite de Subvenção por Comercial";
		$this->view->back = "/produto/validacoessubvencaocomercial";
	}

	public function getModel() {
		return new Produto_Model_ValidacoesSubvencaoComercial();
	}

	public function getForm() {
		$Form = new Produto_Form_ValidacoesSubvencaoComercial();
		$Form->populate($this->getRequest()->getParams());
		return $Form;
	}

	public function indexAction() {
		$this->view->id_validacao = $this->getRequest()->getParam('id_validacao');
		parent::indexAction();
	}

	public function getFilter() {
		$where = array();

		if ($this->getRequest()->getParam('id_cultura')) {
			$where[] = array(
				'field' => 'vls.id_cultura',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_cultura')
			);
		}
		if ($this->getRequest()->getParam('id_corretor')) {
			if ($this->getRequest()->getParam('id_corretor')) {
				$where[] = array(
					'field' => 'vls.id_corretor',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_corretor')
				);
			}
		}

		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array (
				'field' => 'vls.fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}

		if ($this->getRequest()->getParam('ds_ano')) {
			$where[] = array (
				'field' => 'vls.ds_ano',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('ds_ano')
			);
		}

		return $where;
	}

	public function formAction() {
		$this->view->form = $this->getForm();
		$this->view->title .= ' » Formulário';
		$this->view->back = "/produto/validacoessubvencaocomercial";

		$Model = $this->getModel();

		$this->view->form->populate($this->getRequest()->getParams());

		if ($this->getRequest()->isPost()) {
			if ($this->view->form->isValid($this->getRequest()->getPost())) {
				if (method_exists($Model, 'save')) {
				    if(!$Model->verifica_limite_cadastrado($this->getRequest()->getParam('id_usuario'), $this->getRequest()->getParam('ds_ano'), $this->getRequest()->getParam('id'))){
    					if ($Model->save($this->getRequest()->getParams())) {
    						$this->view->showMessage(MSG_SUCCESS, 'success');
    						$this->_forward('index', 'validacoessubvencaocomercial', 'produto', array('id' => $this->getRequest()->getParam("id")));
    					} else {
    						$this->view->showMessage(MSG_ERROR, 'error');
    					}
				    } else {
				        $this->view->showMessage('Limite já cadastrado para o ano informado.', 'error');
				    }
				} else {
					$this->save($this->getRequest()->getParams());
				}
			} else {
				$Errors = $this->view->form->getMessages();
				$this->view->showMessage($this->getHtmlFormError($Errors), 'error');
			}
		} else if ($this->getRequest()->getParam('id')) {
			$row = $this->getRow();
			$this->view->form->populate($row);
		}
	}
}
