<?php

class Comercializacao_TaxaorcamentoController extends Agro_Controller_TaxaAgrupamento
{
    public $_tipo = 'O';

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

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

    public function getForm() {
        return new Comercializacao_Form_TaxaOrcamento();
    }

    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_proposta_search')) {
            $where[] = array(
              'field'    => 'id_proposta',
              'operator' => '=',
              'value'    => $this->getRequest()->getParam('id_proposta_search')
            );
        }

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

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

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

        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('ds_nome_agrupamento_taxas')) {
            $where[] = array(
              'field'    => 'ds_nome_agrupamento_taxas',
              'operator' => 'ilike',
              'value'    => '%'. $this->getRequest()->getParam('ds_nome_agrupamento_taxas') . '%'
            );
        }

        return $where;
    }

    public function listAction()
    {
        $this->getHelper('layout')->disableLayout();
        $where = $this->getFilter();
        $Model = new Comercializacao_Model_TaxasAgrupamentoOrcamentos();

        if ($this->getRequest()->getParam("o")) {
            $Model->_order = trim($this->getRequest()->getParam("o") . ' ' . $this->getRequest()->getParam("s"));
        }

        $Module = $this->getRequest()->getModuleName();
        $Controller = $this->getRequest()->getControllerName();

        $Session = new Zend_Session_Namespace('filtro');
        $Session->{$Module}->{$Controller} = $this->getRequest()->getParams();

        $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;
        }
    }
}
