<?php

class Produto_ValidacoesrestricoescriteriosController extends Agro_Controller_Action
{
    public function buscarorcamentosvinculadosAction()
    {
        $this->isAjax();

        try {
            if (empty($this->_requestParams['idRestricaoCriterio'])) {
                throw new Exception('ID restrição/critério não informado.');
            }

            $this->_return['orcamentos'] = (new Produto_Model_ValidacoesRestricoesCriteriosPropostasExcecoes())
                ->fetchAll(
                    "id_validacoes_restricoes_criterios = {$this->_requestParams['idRestricaoCriterio']}",
                    'id_proposta DESC'
                )
                ->toArray();
        } catch (Exception $erro) {
            $this->_return['erro'] = true;
            $this->_return['mensagem'] = $erro->getMessage();
        }

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

    public function deletarorcamentoAction()
    {
        $this->isAjax();
        $adapter = Agro_Db_Table_Abstract::getDefaultAdapter();

        try {
            $adapter->beginTransaction();

            if (empty($this->_requestParams['id'])) {
                throw new Exception('ID não informado.');
            }

            (new Produto_Model_ValidacoesRestricoesCriteriosPropostasExcecoes())->delete(
                $this->_requestParams['id']
            );

            $adapter->commit();
        } catch (Exception $erro) {
            $this->_return['erro'] = true;
            $this->_return['mensagem'] = $erro->getMessage();

            $adapter->rollBack();
        }

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

    public function deleteAction()
    {
        $this->getModel()->deletarVinculos($this->_requestParams['id']);
        parent::deleteAction();
    }

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

    public function relacionarpropostasexcecoesAction()
    {
        $this->getHelper('layout')->disableLayout();

        $this->view->idRestricaoCriterio = $this->_requestParams['id'];

        if ($this->getRequest()->isPost()) {
            $this->isAjax();
            $adapter = Agro_Db_Table_Abstract::getDefaultAdapter();

            try {
                $adapter->beginTransaction();

                if (empty($this->_requestParams['idRestricaoCriterio'])) {
                    throw new Exception('ID da restrição não informado.');
                }

                if (empty($this->_requestParams['idProposta'])) {
                    throw new Exception('Orçamento não informado.');
                }

                (new Produto_Model_ValidacoesRestricoesCriteriosPropostasExcecoes())->save(
                    [
                        'id_validacoes_restricoes_criterios' => $this->_requestParams['idRestricaoCriterio'],
                        'id_proposta' => $this->_requestParams['idProposta'],
                    ]
                );

                $adapter->commit();
            } catch (Exception $erro) {
                $this->_return['erro'] = true;
                $this->_return['mensagem'] = $erro->getMessage();

                $adapter->rollBack();
            }

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