<?php

class Agro_Controller_TaxaAgrupamento extends Agro_Controller_Action {
	/*
	public $_columnList = array(
		'ds_nome_agrupamento_taxas' => 'Nome',
		'ds_nome_safra' => array('label' => 'Safra', 'align' => 'left'),
		'ds_nome_produto' => array('label' => 'Produto', 'align' => 'left')
	
	);*/

    public function init()
    {
        $session = new Zend_Session_Namespace('agrupamento');

        if (!$session->taxa['id']) {
            $taxas = new Comercializacao_Model_Taxas();

            if ($this->getRequest()->getParam('id')) {
                $taxa = $taxas->find($this->getRequest()->getParam('id'))->current();
                $session->taxa['id'] = $taxa->id;
                $session->taxa['id_produto'] = $taxa->id_produto;
                $session->taxa['ds_nome_agrupamento_taxas'] = $taxa->ds_nome_agrupamento_taxas;
                $session->taxa['tp_taxa_agrupamento'] = $taxa->tp_taxa_agrupamento;
            }
        }

        switch ($session->taxa['tp_taxa_agrupamento']) {
            case Comercializacao_Model_Comercializacoes::TIPO_ESTADO:
                $this->view->back = '/comercializacao/taxaestado';
                break;

            case Comercializacao_Model_Comercializacoes::TIPO_MUNICIPIO:
                $this->view->back = '/comercializacao/taxamunicipio';
                break;

            case Comercializacao_Model_Comercializacoes::TIPO_USUARIO:
                $this->view->back = '/comercializacao/taxausuario';
                break;

            case Comercializacao_Model_Comercializacoes::TIPO_PROPONENTE:
                $this->view->back = '/comercializacao/taxaproponente';
                break;

			case Comercializacao_Model_Comercializacoes::TIPO_CAR:
				$this->view->back = '/comercializacao/taxacar';
				break;

            case Comercializacao_Model_Comercializacoes::TIPO_ORCAMENTO:
                $this->view->back = '/comercializacao/taxaorcamento';
                break;
        }
    }
	
	public function getModel() {
		return new Comercializacao_Model_TaxasAgrupamentos();
	}
	
	public function getForm() {
		return new Comercializacao_Form_TaxasAgrupamento();
	}
	
	public function getFilter() {
		$where = array();
		$where[] = array(
			'field' => 'tp_taxa_agrupamento',
			'operator' => '=',
			'value' => $this->_tipo
		);
		
		if ($this->getRequest()->getParam('id_safra')) {
			$where[] = array(
				'field' => 'id_safra',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_safra')
			);
		}
		
		if ($this->getRequest()->getParam('id_produto')) {
			$where[] = array(
				'field' => 'id_produto',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_produto')
			);
		}
		
		if ($this->getRequest()->getParam('id_estado')) {
			$where[] = array(
				'field' => 'te.id_estado',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_estado')
			);
		}
		
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		return $where;
	}
	
	public function save(array $post) {
		try {
			$Model = new Comercializacao_Model_TaxasAgrupamentos();
			
			if(!$post['id_produto']) {
				$this->view->showMessage('Nenhum produto foi Selecionado!', 'error');
				return false;
			}
			
			$data = array();
			$data['id_produto'] = $post['id_produto'];
			$data['ds_nome_agrupamento_taxas'] = $post['ds_nome_agrupamento_taxas'];
			$data['tp_taxa_agrupamento'] = $this->_tipo;
			$data['vl_comissao'] = $post['vl_comissao'];
			$data['fl_ativo'] = ($post['fl_ativo'] ? 1 : 0);
			$data['id_secao'] = ($post['id_secao'] > 0 ? $post['id_secao'] : null);
			
			if ($post['id']) {
				$Model->update($data, "id = {$post['id']}");
			} else {
				$Model->insert($data);
			}
			
			
			$this->view->showMessage(MSG_SUCCESS, 'success');
			if ($this->_tipo == 'M'){
				$this->_forward('index', 'taxamunicipio', 'comercializacao');
			} else if ($this->_tipo == 'E') {
				$this->_forward('index', 'taxaestado', 'comercializacao');
			} else if ($this->_tipo == 'P') {
				$this->_forward('index', 'taxaproponente', 'comercializacao');
			} else if ($this->_tipo == 'U') {
				$this->_forward('index', 'taxausuario', 'comercializacao');
			} else if ($this->_tipo == 'O') {
                $this->_forward('index', 'taxaorcamento', 'comercializacao');
            }  else if ($this->_tipo == 'C') {
                $this->_forward('index', 'taxacar', 'comercializacao');
            }
		} catch (Exception $e) {
			$this->view->showMessage(MSG_ERROR, 'error');
		}
	}
	
}