public ActionResult Lancamento(ExpenseIncomeModel model) { if (ModelState.IsValid) { try { if (model.dValor <= 0) { throw new InvalidOperationException("Valor deve ser maior que 0 (zero)!"); } ExpenseIncome expInc = null; if (!string.IsNullOrEmpty(model.sID) && expenseIncomeBusiness.Get.Any(e => e.sID == model.sID)) { // Alterar expInc = expenseIncomeBusiness.Get.FirstOrDefault(e => e.sID == model.sID); if (!string.IsNullOrEmpty(expInc.sAgrupadorOcorrencia)) { return(Json(new { Sucesso = false, Mensagem = "Lançamento com Agrupador não podem ser alterados pela tela de alteração individual!", Titulo = "Restrição", Url = "/Home/PaginaInicial" })); } expInc.dData = model.dData; expInc.dDataBase = model.bDataDiferente ? model.dDataBase : expInc.dData; expInc.dValor = model.dValor; expInc.sCategoryID = model.sCategoryID; expInc.CategoryType = categoryBusiness.Get.FirstOrDefault(c => c.sID == model.sCategoryID).CategoryType; expInc.sAccountID = model.sAccountID; expInc.sDescricao = model.sDescricao; expInc.sNotificationID = model.sNotificationID; expenseIncomeBusiness.Update(expInc); return(Json(new { Sucesso = true, Mensagem = "Lançamento Alterado com Sucesso!", Titulo = "Sucesso", Url = "/Home/PaginaInicial" })); } else { // Inserir Account conta = accountBusiness.Get.FirstOrDefault(c => c.sID == model.sAccountID); if (conta.AccountType != AccountType.CartaoDeCredito) { model.iTotalParcelas = 1; } double dValorDivisao = model.dValor / model.iTotalParcelas; if (dValorDivisao < 0.01) { throw new InvalidOperationException("Valor da divisão menor que R$ 0,01!"); } dValorDivisao = Math.Round(dValorDivisao, 2); double dValorSobra = model.dValor; string sAgrupadorParcelas = sAgrupadorParcelas = Guid.NewGuid().ToString(); for (int i = 1; i <= model.iTotalParcelas; i++) { expInc = new ExpenseIncome(); expInc.dData = model.dData; expInc.dDataBase = model.bDataDiferente ? model.dDataBase : expInc.dData; expInc.sCategoryID = model.sCategoryID; expInc.CategoryType = categoryBusiness.Get.FirstOrDefault(c => c.sID == model.sCategoryID).CategoryType; expInc.sAccountID = model.sAccountID; expInc.sDescricao = model.sDescricao; expInc.sNotificationID = model.sNotificationID; expInc.sUserID = AuthProvider.UserAntenticated.sID; if (i == model.iTotalParcelas) { expInc.dValor = dValorSobra; } else { expInc.dValor = dValorDivisao; dValorSobra -= expInc.dValor; } expInc.iNumeroOcorrencia = i; expInc.iTotalOcorrencia = model.iTotalParcelas; if (model.iTotalParcelas > 1) { expInc.sAgrupadorOcorrencia = sAgrupadorParcelas; } expenseIncomeBusiness.Insert(expInc); } return(Json(new { Sucesso = true, Mensagem = "Lançamento Gerado com Sucesso!", Titulo = "Sucesso", Url = "/Home/PaginaInicial" })); } } catch (DbEntityValidationException dbEx) { string sErro = string.Empty; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { sErro += string.Format(" Property: {0} Error: {1} |", validationError.PropertyName, validationError.ErrorMessage); } } return(Json(new { Sucesso = false, Mensagem = sErro, Titulo = "Erro" })); } catch (Exception ex) { string sErro = ex.Message; if (ex.InnerException != null) { sErro += " - " + ex.InnerException.Message; } return(Json(new { Sucesso = false, Mensagem = sErro, Titulo = "Erro" })); } } else { model.listaCategorias = getComboCategorias(new List <CategoryType>() { CategoryType.Despesa, CategoryType.Receita }); model.sConcatCartaoCredito = getConcatCartaoCredito(); model.listaContas = getComboContas(false, true); model.listaNotifications = getComboNotificacoes(); return(View(model)); } }
public ActionResult Lancamento(string sNotificacaoID, string sExpenseIncomeID) { ExpenseIncomeModel model = new ExpenseIncomeModel(); if (!string.IsNullOrEmpty(sExpenseIncomeID) && expenseIncomeBusiness.Get.Any(e => e.sID == sExpenseIncomeID)) { ExpenseIncome expInc = expenseIncomeBusiness.Get.FirstOrDefault(e => e.sID == sExpenseIncomeID); // restrições ----------------------------------------------------------------------------------------------------------- //if (expInc.CategoryType == CategoryType.PagamentoCartaoCredito) return Json(new { Sucesso = false, Mensagem = "Lançamentos de Pagamento de Cartão de Crédito não podem ser alterados!", Titulo = "Restrição" }, JsonRequestBehavior.AllowGet); if (expInc.CategoryType == CategoryType.Transferencia) { return(Json(new { Sucesso = false, Mensagem = "Lançamentos de Transferência não podem ser alterados!", Titulo = "Restrição" }, JsonRequestBehavior.AllowGet)); } if (expInc.bPagoCartaoCredito) { return(Json(new { Sucesso = false, Mensagem = "Lançamentos já marcados como pago no Cartão de Crédito não podem ser alterados!", Titulo = "Restrição" }, JsonRequestBehavior.AllowGet)); } //----------------------------------------------------------------------------------------------------------------------- model.sID = expInc.sID; model.sDescricao = expInc.sDescricao; model.sCategoryID = expInc.sCategoryID; model.eCategoryType = expInc.CategoryType; model.sNotificationID = expInc.sNotificationID; model.sAccountID = expInc.sAccountID; model.dData = expInc.dData; model.dDataBase = expInc.dDataBase; model.bDataDiferente = model.dData != model.dDataBase ? true : false; model.dValor = expInc.dValor; } else { if (!string.IsNullOrEmpty(sNotificacaoID)) { Notification not = notificationBusiness.Get.FirstOrDefault(n => n.sID == sNotificacaoID); if (not != null) { model.sNotificationID = not.sID; model.sDescricao = not.sDescricao; model.dValor = not.dValor; model.sCategoryID = not.sCategoryID; } } model.dData = DateTime.Now; model.dDataBase = DateTime.Now; model.bDataDiferente = false; } if (model.eCategoryType == CategoryType.PagamentoCartaoCredito) { model.listaCategorias = getComboCategorias(new List <CategoryType>() { CategoryType.PagamentoCartaoCredito }); } else { model.listaCategorias = getComboCategorias(new List <CategoryType>() { CategoryType.Despesa, CategoryType.Receita }); } model.sConcatCartaoCredito = getConcatCartaoCredito(); model.listaContas = getComboContas(false, true); model.listaNotifications = getComboNotificacoes(); return(View(model)); }