public ActionResult PagamentoCartaoCredito() { CCPaymentModel ccPayment = new CCPaymentModel(); ccPayment.listaCategorias = getComboCategorias(new List <CategoryType>() { CategoryType.PagamentoCartaoCredito }); ccPayment.listaContas = getComboCartaoCredito(); ccPayment.listaContasPagamento = getComboContas(false, true); ccPayment.dData = DateTime.Now; return(View(ccPayment)); }
public ActionResult PagamentoCartaoCredito(CCPaymentModel ccPayment) { using (var scope = new TransactionScope()) { try { if (string.IsNullOrEmpty(ccPayment.sListaGastosID)) { throw new InvalidOperationException("Os gastos devem ser selecionados."); } if (string.IsNullOrEmpty(ccPayment.sDescricao)) { throw new InvalidOperationException("A descrição deve ser preenchida."); } if (ccPayment.dData == DateTime.MinValue) { throw new InvalidOperationException("A data deve ser preenchida."); } List <string> listaGastos = ccPayment.sListaGastosID.Split(';').ToList(); List <ExpenseIncome> listaExpenses = new List <ExpenseIncome>(); List <string> listaGastosProcessados = new List <string>(); double dTotalPagar = 0; foreach (string gasto in listaGastos) { if (!string.IsNullOrEmpty(gasto) && !listaGastosProcessados.Contains(gasto)) { ExpenseIncome exp = expenseIncomeBusiness.Get.FirstOrDefault(e => e.sID == gasto); if (exp == null) { throw new InvalidOperationException("Problemas na identificação do gasto (" + gasto + ")."); } if (exp.bPagoCartaoCredito) { throw new InvalidOperationException("Gasto (" + exp.sDescricao + ") já marcado como pago."); } double valorLancamento = exp.dValor; if (exp.CategoryType == CategoryType.Receita) { valorLancamento = valorLancamento * (-1); } dTotalPagar += valorLancamento; listaExpenses.Add(exp); listaGastosProcessados.Add(gasto); } } ExpenseIncome expPagCC = new ExpenseIncome(); expPagCC.bPagoCartaoCredito = false; expPagCC.bTransferOrigem = false; expPagCC.CategoryType = CategoryType.PagamentoCartaoCredito; expPagCC.dData = ccPayment.dData; expPagCC.dDataBase = ccPayment.dData; expPagCC.dValor = dTotalPagar; expPagCC.sAccountID = ccPayment.sAccountPaymentID; expPagCC.sCategoryID = ccPayment.sCategoryID; expPagCC.sDescricao = ccPayment.sDescricao; expPagCC.sUserID = AuthProvider.UserAntenticated.sID; expenseIncomeBusiness.Insert(expPagCC); foreach (ExpenseIncome exp in listaExpenses) { expenseIncomeBusiness.RegistraExpensePagoCartaoCredito(exp); ExpenseIncomeReference expRef = new ExpenseIncomeReference(); expRef.sExpenseIncomeOriginID = exp.sID; expRef.sExpenseIncomeDestinyID = expPagCC.sID; expenseIncomeReferenceBusiness.Insert(expRef); } scope.Complete(); return(Json(new { Sucesso = true, Mensagem = "Pagamento de Cartão de Crédito realizado com sucesso.", Titulo = "Sucesso" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { scope.Dispose(); return(Json(new { Sucesso = false, Mensagem = ex.Message, Titulo = "Erro" }, JsonRequestBehavior.AllowGet)); } } }