<?php

class Produto_Form_ValidacoesSubvencaoGrupos extends Agro_Form {

	public function init() {
		$this->addElement('hidden', 'id_validacao', array('decorators' => $this->buttonDecorator));
		$this->id_validacao->setValue('9');
		$this->addElement('hidden', 'id', array('decorators' => $this->buttonDecorator));

		$User = Zend_Auth::getInstance()->getIdentity();

		//Se for Departamento Comercial da AgroBrasil
		if($User->id_departamento == 27 ){
    		$this->addElement('hidden', 'id_usuario', array('decorators' => $this->buttonDecorator));
    		$this->id_usuario->setValue($User->id);

    		$this->addElement('text', 'ds_ano', array(
    			'decorators' => $this->elementDecorator,
    			'label' => 'Ano',
    			'class' => 'text',
    			'required' => true,
    			'size' => 10,
    		    'readonly' => true,
    		    'value' => date("Y")
    		));
		} else {
    		$Empresa = new Sistema_Model_Empresas();
    		$this->addElement('select', 'id_usuario', array(
    			'decorators' => $this->elementDecorator,
    			'MultiOptions' => $Empresa->getComercial(true,false),
    			'required' => true,
    			'label' => 'Comercial'
    		));

    		$this->addElement('select', 'ds_ano', array(
    		    'MultiOptions' => $this->getAnosSafra(),
    			'decorators' => $this->elementDecorator,
    			'label' => 'Ano'
    		));
		}

	    $Cultura = new Produto_Model_Culturas();
        $rsCulturas = $Cultura->getCombo();
        $this->addElement('select', 'id_cultura_search', array(
            'MultiOptions' => $rsCulturas,
            'decorators' => $this->elementDecorator,
            'label' => 'Cultura',
            'required' => false
        ));

		$this->addElement('text', 'ds_nome_validacao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Nome validação',
			'class' => 'text',
			'required' => true,
			'maxlength' => 100,
			'size' => 30
		));

		$this->addElement('text', 'vl_limite_subvencao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Valor Limite (R$)',
			'class' => 'currency',
			'required' => true,
			'maxlength' => 20,
			'size' => 15
		));

		$this->addElement('text', 'vl_disponivel_validacao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Disponível (R$)',
			'class' => 'currency',
			'required' => false,
		    'readonly' => true,
			'maxlength' => 20,
			'size' => 15
		));

		$this->addElement('text', 'vl_subvencao_utilizada', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Utilizado (R$)',
			'class' => 'currency',
			'required' => false,
		    'readonly' => true,
			'maxlength' => 20,
			'size' => 15
		));

		$this->addElement('select', 'fl_ativo', array(
			'MultiOptions' => array(1=>'Sim',0=>'Não'),
			'decorators' => $this->elementDecorator,
			'label' => 'Ativo'
		));
	}

	public function populate($valores){
		$valores['vl_limite_subvencao'] = Agro_Util::formatDbCurrency($valores['vl_limite_subvencao']);
		$valores['vl_disponivel_validacao'] = Agro_Util::formatDbCurrency($valores['vl_disponivel_validacao']);
		$valores['vl_subvencao_utilizada'] = Agro_Util::formatDbCurrency($valores['vl_subvencao_utilizada']);
		return parent::populate($valores);
	}

	private function getAnosSafra()
    {
        $modelSafras = new Produto_Model_Safras();
        $safras = $modelSafras->getSafrasByIdSafra(18, ' EXTRACT(year FROM dt_inicio) as ano')->toArray();
        $anosSafra = [];

        foreach ($safras as $safra) {
            $anosSafra[$safra['ano']] = $safra['ano'];
        }

        return $anosSafra;
    }
}
