<?php

class Comercializacao_OrcamentoController extends Agro_Controller_Comercializacao
{
    public function init()
    {
        $this->view->title = 'Produto » Comercializações » Orçamentos';
        $this->tipo = 'O';
    }

    public function getForm()
    {
        $Form = new Comercializacao_Form_Comercializacao();
        $Form->populate($this->getRequest()->getParams());

        return $Form;
    }

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

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

        $myxls = new PhpSimpleXlsGen();
        $myxls->totalcol = 6;
        //produto | DATA INICIO VEN | DATA FIM VEND | DADTA INICIO COB | DATA FIM COB |  ESTADO
        $myxls->InsertText('Produto');
        $myxls->InsertText('Inicio Vendas');
        $myxls->InsertText('Fim Vendas');
        $myxls->InsertText('Inicio Cobertura');
        $myxls->InsertText('Fim Cobertura');
        $myxls->InsertText('Estado');
        foreach ($ret as $linha) {

            $myxls->InsertText(utf8_decode($linha['ds_nome_produto']) . ' ');
            $myxls->InsertText(Agro_Util::formatDate($linha['dt_comercializacao_inicio'], Zend_Date::DATES) . ' ');
            $myxls->InsertText(Agro_Util::formatDate($linha['dt_comercializacao_fim'], Zend_Date::DATES) . ' ');
            $myxls->InsertText(Agro_Util::formatDate($linha['dt_cobertura_inicio'], Zend_Date::DATES) . ' ');
            $myxls->InsertText(Agro_Util::formatDate($linha['dt_cobertura_fim'], Zend_Date::DATES) . ' ');
            $myxls->InsertText(utf8_decode($linha['ds_sigla']) . ' ');
        }

        $myxls->SendFile();
    }

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

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

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

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

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

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

        return $where;
    }
}
