Esempio n. 1
0
 public function __construct($iCodigo = "")
 {
     if (!empty($iCodigo)) {
         $oDao = new Entidade();
         $lRetorno = $oDao->buscarPorCodigo($iCodigo, "", $this);
         if ($lRetorno === false) {
             throw new Exception("Cliente não encontrado.");
         }
     }
 }
Esempio n. 2
0
 public function excluirPost()
 {
     if (!$this->getRequisicao()->isPost()) {
         $this->redireciona("/cliente/lista");
     }
     $iCodigo = $this->getRequisicao()->getPost("codigo");
     $lExcluiAnimais = $this->getRequisicao()->getPost("excluirAnimais");
     $oDao = new Entidade();
     $oDaoAnimal = new EntidadeAnimal();
     $sWhere = " usuario = " . $this->getSessao()->getUsuarioLogado()->getCodigo();
     try {
         $oDao->iniciaTransacao();
         $oCliente = $oDao->buscarPorCodigo($iCodigo, $sWhere);
         if ($oCliente === false || empty($oCliente->getCodigo())) {
             throw new Exception("Cliente não encontrada para exclusão.");
         }
         $aAnimais = $oCliente->getAnimais();
         if ($aAnimais === false) {
             throw new Exception("Houve um erro ao buscar os animais do Cliente. Contate o suporte.");
         }
         foreach ($aAnimais as $oAnimal) {
             if ($lExcluiAnimais) {
                 if (!$oDaoAnimal->excluir($oAnimal)) {
                     throw new Exception("Houve um problema ao excluir o Animal do Cliente. Contate o suporte.");
                 }
                 continue;
             }
             $oAnimal->setCliente(null);
             if (!$oDaoAnimal->salvar($oAnimal)) {
                 throw new Exception("Houve um erro ao desvincular o Cliente e o Animal. Contate o suporte.");
             }
         }
         if (!$oDao->excluir($oCliente)) {
             throw new Exception("Não foi possível excluir o Cliente. Contate o suporte.");
         }
         $oDao->encerraTransacao(false);
         $this->getSessao()->setMensagemSucesso("Cliente excluido com sucesso.");
         $this->redireciona("/cliente/lista");
     } catch (Exception $e) {
         $oDao->encerraTransacao(true);
         $this->getSessao()->setMensagemErro($e->getMessage());
         $this->redireciona("/cliente/excluir/{$iCodigo}");
     }
 }