<?php

class Comercializacao_TaxaproponenteController extends Agro_Controller_TaxaAgrupamento {
	public $_columnList = false;
	public $_tipo = 'P';
	
	public function init() {
		$this->view->title = 'Produto » Taxas » Proponente';
	
		$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' => 'c.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')) {
			$where['id_estado'] =  $this->getRequest()->getParam('id_estado');
		}
		
		if($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'c.fl_ativo',
				'operator' => ' = ',
				'value' =>  $this->getRequest()->getParam('fl_ativo')
			);
		}
		
		if($this->getRequest()->getParam('cpf_cnpj_search')) {
			$where[] = array(
				'field' => 'cpf_cnpj',
				'operator' => ' = ',
				'value' =>  $this->getRequest()->getParam('cpf_cnpj_search')
			);
		}

        if($this->getRequest()->getParam('id_municipio')) {
            $where[] = array(
              'field' => 'id_municipio',
              'operator' => ' = ',
              'value' =>  $this->getRequest()->getParam('id_municipio')
            );
        }
		
		return $where;
	}
	
	
	public function listAction() {
		$this->getHelper('layout')->disableLayout();
		$where = $this->getFilter();
		$Model = new Comercializacao_Model_TaxasAgrupamentoProponente();

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

        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->getTaxaProponenteXls($dados);
		require_once(APPLICATION_PATH . "/../library/Agro/xls/PhpSimpleXlsGen.php");
		
		$myxls = new PhpSimpleXlsGen();
		$myxls->totalcol = 7;
		
		$myxls->InsertText('CPF/CNPJ');
		$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['cpf_cnpj']) . ' ');
			$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();
	}
	
	
	
}