<?php

class Comercializacao_TaxaestadoController extends Agro_Controller_TaxaAgrupamento {
	public $_tipo = 'E';

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

	public function init() {
		$this->view->title = 'Produto » Taxas » Estados';

		$Session = new Zend_Session_Namespace('agrupamento');
		$Session->unsetAll();
	}

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

		$where[] = array(
			'field' => 'tp_taxa_agrupamento',
			'operator' => '=',
			'value' => $this->_tipo
		);

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

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

		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[] = " id in (select id_taxa_agrupamento from comercializacao.taxas_agrupamento_estados where id_taxa_agrupamento = vt.id and id_estado=".$this->getRequest()->getParam('id_estado').")";
		}

		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		//print_r($where);die;
		return $where;
	}

	public function geraxlsAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$dados = $this->getRequest()->getParams();
		$Model = new Comercializacao_Model_TaxasAgrupamentos();

		$ret = $Model->getTaxaEstadoXls($dados);
		require_once(APPLICATION_PATH . "/../library/Agro/xls/PhpSimpleXlsGen.php");

		$myxls = new PhpSimpleXlsGen();
		$myxls->totalcol = 6;
		//(NOME | PRODUTO | COBERTURA | VALOR FRANQUIA| VALOR TAXA |  ESTADO)
		$myxls->InsertText('Nome');
		$myxls->InsertText('Produto');
		$myxls->InsertText('Cobertura');
		$myxls->InsertText('Valor Franquia');
		$myxls->InsertText('Valor Taxa');
		$myxls->InsertText('Estado');
		foreach($ret as $linha) {

			$myxls->InsertText( utf8_decode($linha['ds_nome_agrupamento_taxas']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_nome_produto']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_nome_cobertura']) . ' ');
			$myxls->InsertText( utf8_decode($linha['vl_franquia']) . ' ');
			$myxls->InsertText( utf8_decode($linha['vl_taxa']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_sigla']) . ' ' );
		}

		$myxls->SendFile();
	}
}
