<?php

class Comercializacao_TaxamunicipioController extends Agro_Controller_TaxaAgrupamento {
	public $_tipo = 'M';
	
	public function init() {
		$this->view->title = 'Produto » Taxas » Municípios';
	
		$Session = new Zend_Session_Namespace('agrupamento');
		$Session->unsetAll();
	}
	
	public function getModel() {
		return new Comercializacao_Model_TaxasAgrupamentos();
	}
	
	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' => 't.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_estado'] =  $this->getRequest()->getParam('id_estado');
		}

		if ($this->getRequest()->getParam('id_municipio')) {
			$where['id_municipio'] =  $this->getRequest()->getParam('id_municipio');
			unset($where['id_estado']);
		}

        if ($this->getRequest()->getParam('id_municipio_hidden')) {
            $where['id_municipio'] =  $this->getRequest()->getParam('id_municipio_hidden');
            unset($where['id_estado']);
        }
		
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		
		return $where;
	}
	
	public function listAction() {
		$this->getHelper('layout')->disableLayout();
		$where = $this->getFilter();
		
		// Habilita ou Desabilita a paginacao pelo combo na tela
	  	if (array_key_exists('show_paginacao', $this->getRequest()->getParams())) {
			$this->_paginator = $this->getRequest()->getParam('show_paginacao');
		}
		
		$Module = $this->getRequest()->getModuleName();
		$Controller = $this->getRequest()->getControllerName();
		
		$Session = new Zend_Session_Namespace('filtro');
		$Session->{$Module}->{$Controller} = $this->getRequest()->getParams();
		
		$Model = new Comercializacao_Model_TaxasAgrupamentoMunicipios();
		
		if ($this->getRequest()->getParam("o")) {
			$Model->_order = trim($this->getRequest()->getParam("o") .' '. $this->getRequest()->getParam("s"));
		}
		
		$recordsPerPage = ($this->recordsPerPage ? $this->recordsPerPage :  RECORDS_PER_PAGE);
		$Rs = $Model->fetchAllPaginator($where, $this->getRequest()->getParam('page'), $recordsPerPage, $this->_paginator);
		
		if ($this->_columnList) {
			$this->getHelper('ViewRenderer')->setNoRender();
			
			$List = new Agro_List_Table($this->_columnList, $Rs, $this->_paginator, $this->getRequest());
			$this->getResponse()->setBody($List->toString());
		} else {
			$this->view->paginator = $Rs;
		}
	}
	
	public function geraxlsAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$dados = $this->getRequest()->getParams();
		$Model = new Comercializacao_Model_TaxasAgrupamentos();
		
		$ret = $Model->getTaxaMunicipioXls($dados);
		require_once(APPLICATION_PATH . "/../library/Agro/xls/PhpSimpleXlsGen.php");
		
		$myxls = new PhpSimpleXlsGen();
		$myxls->totalcol = 7;
		//NOME | PRODUTO | COBERTURA | VALOR FRANQUIA| VALOR TAXA |  ESTADO | MUNICIPIO
		$myxls->InsertText('Nome');
		$myxls->InsertText('Produto');
		$myxls->InsertText('Cobertura');
		$myxls->InsertText('Valor Franquia');
		$myxls->InsertText('Valor Taxa');
		$myxls->InsertText('Estado');
		$myxls->InsertText('Municipio');
		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->InsertText( utf8_decode($linha['ds_nome_municipio']) . ' ' );
		}
		
		$myxls->SendFile();
	}
	
	
}