<?php
class Sistema_AssessoriaController extends Agro_Controller_Action {

	public function init() {
		$this->view->title = 'Sistema » Cadastro » Assessorias/Co-corretagem';
	}

	public function getModel() {
		return new Sistema_Model_Assessorias();
	}

	public function getForm() {
		$this->view->formEndereco = new Sistema_Form_AssessoriaEndereco();
		$this->view->formCorretor = new Sistema_Form_AssessoriaCorretor();
		return new Sistema_Form_Assessoria();
	}

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

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

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

		return $where;
	}

	public function viewAction() {
		$Model = $this->getModel();
		$this->view->title .= ' » Visualização';
		$this->view->data = $this->getRow();
		$this->view->enderecos = $Model->getEnderecos($this->view->data['id']);

		parent::viewAction();
	}

    public function formAction()
    {
        $parametros = $this->getRequest()->getParams();

        if (isset($parametros['salvar']) && empty($parametros['id']) && $this->ambienteProducao) {
            $agroSenha = new Agro_Senha();
            $novaSenha = $agroSenha->gerarNovaSenha();

            $this->getRequest()->setParam('ds_senha', $novaSenha);
            $this->getRequest()->setParam('ds_senha2', $novaSenha);
            $this->getRequest()->setParam('ds_url_senha', $agroSenha->gerarHash());
        }

        if (!empty(trim($this->getRequest()->getParam('ds_senha')))) {
            $this->getRequest()->setParam('validarSenha', true);
        }

        parent::formAction(); // TODO: Change the autogenerated stub
    }

    public function beforeSave()
    {
        $parametros = $this->getRequest()->getParams();

        $this->validarUsuario($parametros);
    }

    public function getassessoriasAction()
    {
        $this->isAjax();
        $rows = $this->getModel()->getAssessorias()->toArray();

        $this->getResponse()->setBody(Zend_Json::encode($rows));
    }

    public function excelAction()
    {
        $this->isAjax();

        $model = $this->getModel();
        $rows = $model->getCorretoresSubloginsVinculados($this->getFilter());

        $colunas = [
            'cpf_cnpj' => 'CPF/CNPJ',
            'ds_nome_assessoria' => 'Nome',
            'nome_vinculado' => 'Corretor/Sublogin Vinculado',
            'valor_percentual' => 'Valor Percentual',
        ];

        $rs = $model->getPaginator($rows, $this->getRequest()->getParam('page'), $this->recordsPerPage ?? RECORDS_PER_PAGE, $this->getRequest()->getParam('show_paginacao'));

        $csv = new Agro_Plugin_Rs2Csv();
        $csv->setTitles($colunas);
        $csv->setRecordSet($rs);
        $csv->setType("application/octet-stream");
        $csv->setFileName("assessoria.csv");
        $csv->setDisposition("attachment");
        $csv->setSeparator(";");
        $csv->getCsv();
    }
}