<?php

class Sistema_PrepostoperiodocadastroController extends Agro_Controller_Action
{
    public function getModel()
    {
        return new Sistema_Model_PrepostosPeriodoCadastroAnual();
    }

    public function salvarcadastrovalidadoAction()
    {
        $this->isAjax();
        $parametros = $this->getRequest()->getParams();
        $retorno = ['erro' => false, 'mensagem' => 'Dados processados com sucesso.'];
        $adapter = Agro_Db_Table_Abstract::getDefaultAdapter();

        try {
            $adapter->beginTransaction();
            $this->getModel()->validarCadastro($parametros['ids']);
            $adapter->commit();
        } catch (Exception $erro) {
            $adapter->rollback();
            $retorno = ['erro' => true, 'mensagem' => 'Erro ao processar os dados.'];
        }

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

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

        $modelUsuarios = new Sistema_Model_Usuarios();
        $usuarioLogado = Zend_Auth::getInstance()->getIdentity();

        if ($modelUsuarios->isFuncionarioAgro($usuarioLogado->id)) {
            $this->getResponse()->setBody(Zend_Json::encode(false));
        } else {
            $rows = $this->getModel()->getPrepostosByCorretor($this->getRequest()->getParam('id_corretor'), date('Y-m-d'), false)->toArray();

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