<?php

class Comercializacao_TaxausuarioController extends Agro_Controller_TaxaAgrupamento {
	public $_tipo = 'U';

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

		$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('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('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_estado_search')) {
			$where['id_estado'] =  $this->getRequest()->getParam('id_estado_search');
		}

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

		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_TaxasAgrupamentoUsuarios();

		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->getTaxaUsuarioXls($dados);
		require_once(APPLICATION_PATH . "/../library/Agro/xls/PhpSimpleXlsGen.php");

		$myxls = new PhpSimpleXlsGen();
		$myxls->totalcol = 8;

		$myxls->InsertText('Corretor');
		$myxls->InsertText('Preposto');
		$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_fantasia']) . ' ');
			$myxls->InsertText(utf8_decode($linha['ds_nome_preposto']) . ' ');
			$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();
	}
}
