public async Task <ISaida> ExcluirLancamento(int idLancamento, int idUsuario) { this.NotificarSeMenorOuIgualA(idLancamento, 0, LancamentoMensagem.Id_Lancamento_Invalido); this.NotificarSeMenorOuIgualA(idUsuario, 0, Mensagem.Id_Usuario_Invalido); if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } var lancamento = await _lancamentoRepositorio.ObterPorId(idLancamento); // Verifica se o lançamento existe this.NotificarSeNulo(lancamento, LancamentoMensagem.Id_Lancamento_Nao_Existe); if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } // Verifica se o lançamento pertece ao usuário informado. this.NotificarSeDiferentes(lancamento.IdUsuario, idUsuario, LancamentoMensagem.Lancamento_Excluir_Nao_Pertence_Usuario); if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } foreach (var anexo in lancamento.Anexos) { // Exclui os anexos do banco de dados e os arquivos do Google Drive await _anexoRepositorio.Deletar(anexo); } // Verifica se o lançamento está associado ao pagamento de uma fatura de cartão de crédito. if (lancamento.Categoria.Id == (int)TipoCategoriaEspecial.PagamentoFaturaCartao) { var fatura = await _faturaRepositorio.ObterPorLancamento(lancamento.Id); if (fatura != null) { var parcelas = await _parcelaRepositorio.ObterPorFatura(fatura.Id); foreach (var parcela in parcelas) { parcela.DesfazerLancamento(); } } } // Caso o lançamento esteja associado a uma parcela, a parcela deve ser reaberta. if (lancamento.IdParcela.HasValue) { var parcela = await _parcelaRepositorio.ObterPorId(lancamento.IdParcela.Value); parcela?.DesfazerLancamento(); } if (string.IsNullOrEmpty(lancamento.IdTransferencia)) { _lancamentoRepositorio.Deletar(lancamento); } // Caso o lançamento pertence a uma transferência, todos os lançamentos relacionados a transferência também serão excluídos. else { var lancamentosTransferencia = await _lancamentoRepositorio.ObterPorIdTransferencia(lancamento.IdTransferencia); foreach (var item in lancamentosTransferencia) { _lancamentoRepositorio.Deletar(item); } } await _uow.Commit(); return(new Saida(true, new[] { LancamentoMensagem.Lancamento_Excluido_Com_Sucesso }, new LancamentoSaida(lancamento))); }