Пример #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            tcuenta Cuenta      = db.tcuentas.Find(id);
            int     Movimientos = Cuenta.tmovimientoes.Count;

            Cuenta.TipoCuentas = sesion.TipoCuentas.Find(c => c.Id == ((Cuenta.CuentaCredito == true) ? 1 : 0));
            try
            {
                if (Movimientos > 0)
                {
                    Cuenta.Activo          = false;
                    db.Entry(Cuenta).State = EntityState.Modified;
                    AddMsgWeb("Registro desactivo, cuenta con movimientos asociados", ToastType.Warning);
                }
                else
                {
                    db.tcuentas.Remove(Cuenta);
                    AddMsgWeb("Registro eliminado exitosamente", ToastType.Success);
                }
                db.SaveChanges();
                ActualizaCuenta(Cuenta.Id);
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ModelErr", ex.Message);
                AddMsgWeb("Error al eliminar el registro", ToastType.Error);
                tcuenta tcuenta = db.tcuentas.Find(id);
                return(View(tcuenta));
            }
        }
Пример #2
0
        // GET: Cuentas/Create
        public ActionResult Create()
        {
            tcuenta cuenta = new tcuenta();

            cuenta.Id_Usuario     = sesion.Usuario.Id;
            cuenta.Activo         = true;
            ViewBag.Id_Moneda     = new SelectList(db.tmonedas.Where(m => m.Id_Usuario == sesion.Usuario.Id && m.Activo), "Id", "Descripcion");
            ViewBag.Id_TipoCuenta = new SelectList(sesion.TipoCuentas, "Id", "Descripcion");
            return(View(cuenta));
        }
Пример #3
0
        // GET: Cuentas/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tcuenta tcuenta = db.tcuentas.Find(id);

            if (tcuenta == null)
            {
                return(HttpNotFound());
            }
            return(View(tcuenta));
        }
Пример #4
0
        // GET: Cuentas/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tcuenta tcuenta = db.tcuentas.Find(id);

            if (tcuenta == null)
            {
                return(HttpNotFound());
            }
            tcuenta.TipoCuentas = sesion.TipoCuentas.Find(c => c.Id == ((tcuenta.CuentaCredito == true) ? 1 : 0));
            return(View(tcuenta));
        }
Пример #5
0
        // GET: Cuentas/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tcuenta cuenta = db.tcuentas.Find(id);

            if (cuenta == null)
            {
                return(HttpNotFound());
            }
            cuenta.Id_TipoCuenta  = Convert.ToInt16(cuenta.CuentaCredito);
            ViewBag.Id_Moneda     = new SelectList(db.tmonedas.Where(m => m.Id_Usuario == sesion.Usuario.Id && m.Activo), "Id", "Descripcion", cuenta.Id_Moneda);
            ViewBag.Id_TipoCuenta = new SelectList(sesion.TipoCuentas, "Id", "Descripcion", cuenta.Id_TipoCuenta);
            return(View(cuenta));
        }
Пример #6
0
 public ActionResult Edit([Bind(Include = "Id,Id_Usuario,Id_Moneda,Nombre,Descripcion,Imagen,SaldoActual,Activo,Id_TipoCuenta")] tcuenta pCuenta)
 {
     if (ModelState.IsValid)
     {
         try {
             pCuenta.CuentaCredito = Convert.ToBoolean(pCuenta.Id_TipoCuenta);
             db.tcuentas.Add(pCuenta);
             db.Entry(pCuenta).State = EntityState.Modified;
             db.SaveChanges();
             ActualizaCuenta(pCuenta.Id);
             AddMsgWeb("Registro editado exitosamente", ToastType.Success);
             return(RedirectToAction("Index"));
         }
         catch (Exception ex) {
             ModelState.AddModelError("ModelErr", ex.Message);
             AddMsgWeb("Error al editar el registro", ToastType.Error);
         }
     }
     ViewBag.Id_Moneda     = new SelectList(db.tmonedas.Where(m => m.Id_Usuario == sesion.Usuario.Id && m.Activo), "Id", "Descripcion", pCuenta.Id_Moneda);
     ViewBag.Id_TipoCuenta = new SelectList(sesion.TipoCuentas, "Id", "Descripcion", pCuenta.Id_TipoCuenta);
     return(View(pCuenta));
 }
Пример #7
0
        public ActionResult EditarCompleto([Bind(Include = "Id,Id_Usuario,Id_Categoria,Id_Cuenta,Fecha,Descripcion,Tipo,TipoMovimiento,Monto,MontoAnterior")] tmovimiento Movimiento)
        {
            try
            {
                bool has = db.tmovimientoes.Any(p => p.Id_Usuario == Movimiento.Id_Usuario && p.Id != Movimiento.Id);
                if (has)
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        try
                        {
                            //Edito el Movimiento
                            db.Entry(Movimiento).State = EntityState.Modified;
                            db.SaveChanges();
                            if (Movimiento.Monto != Movimiento.MontoAnterior)
                            {
                                decimal Diferencia = Movimiento.Monto - Movimiento.MontoAnterior;
                                //Actulizo Saldo
                                tcuenta Cuenta = db.tcuentas.Find(Movimiento.Id_Cuenta);
                                Cuenta.SaldoAnterior = Cuenta.SaldoActual;
                                if (Movimiento.Tipo == 1)
                                {
                                    Cuenta.SaldoActual += Diferencia;
                                }
                                else if (Movimiento.Tipo == 2)
                                {
                                    Cuenta.SaldoActual -= Diferencia;
                                }
                                else
                                {
                                    AddMsgWeb("Las transferencias no pueden alterarse.", ToastType.Warning);
                                    throw new Exception("Las transferencias no pueden alterarse.");
                                }
                                db.Entry(Cuenta).State = EntityState.Modified;
                                db.SaveChanges();
                            }
                            //Cierro tracssaccion
                            transaction.Commit();
                            AddMsgWeb("Registro editado exitosamente", ToastType.Success);
                        }
                        catch (DbEntityValidationException exp)
                        {
                            transaction.Rollback();

                            foreach (var eve in exp.EntityValidationErrors)
                            {
                                string msg = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                           eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                AddMsgWeb("EntityValidationErrors: " + msg, ToastType.Error);

                                foreach (var ve in eve.ValidationErrors)
                                {
                                    string msg1 = string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                                    AddMsgWeb("EntityValidationErrors: " + msg1, ToastType.Error);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            ModelState.AddModelError("ModelErr", ex.Message);
                            AddMsgWeb("Error al agregar el registro", ToastType.Error);
                        }
                    }
                }
                else
                {
                    AddMsgWeb("Error proceso invalido !!", ToastType.Error);
                }
            }
            catch (Exception)
            {
                AddMsgWeb("Error al acceder a los datos", ToastType.Error);
            }
            return(RedirectToAction("Index", "Inicio"));
        }
Пример #8
0
        public ActionResult EliminarMovimiento(int?Id)
        {
            try
            {
                var Movimiento = db.tmovimientoes.Where(m => m.Id == Id && m.Id_Usuario == sesion.Usuario.Id).FirstOrDefault();

                if (Movimiento != null)
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        try
                        {   //Elimino Movimiento
                            db.tmovimientoes.Remove(Movimiento);
                            db.SaveChanges();

                            //Actulizo Saldo
                            tcuenta Cuenta = db.tcuentas.Find(Movimiento.Id_Cuenta);
                            Cuenta.SaldoAnterior = Cuenta.SaldoActual;
                            if (Movimiento.Tipo == 1)//Creditos
                            {
                                // Cuentas Credito de se invierte el valor del monto
                                if (Cuenta.CuentaCredito == true)
                                {
                                    Cuenta.SaldoActual += Movimiento.Monto;
                                }
                                else
                                {
                                    Cuenta.SaldoActual -= Movimiento.Monto;
                                }
                            }
                            else if (Movimiento.Tipo == 2)//Debitos
                            {
                                Cuenta.SaldoActual += Movimiento.Monto;
                            }

                            else
                            {
                                AddMsgWeb("Las transferencias no pueden alterarse.", ToastType.Warning);
                                throw new Exception("Las transferencias no pueden alterarse.");
                            }

                            db.Entry(Cuenta).State = EntityState.Modified;
                            db.SaveChanges();

                            //Cierro tracssaccion
                            transaction.Commit();

                            AddMsgWeb("Registro eliminado exitosamente", ToastType.Warning);
                        }
                        catch (DbEntityValidationException exp)
                        {
                            transaction.Rollback();

                            foreach (var eve in exp.EntityValidationErrors)
                            {
                                string msg = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                           eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                AddMsgWeb("EntityValidationErrors: " + msg, ToastType.Error);

                                foreach (var ve in eve.ValidationErrors)
                                {
                                    string msg1 = string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                                    AddMsgWeb("EntityValidationErrors: " + msg1, ToastType.Error);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            ModelState.AddModelError("ModelErr", ex.Message);
                            AddMsgWeb("Error al agregar el registro", ToastType.Error);
                        }
                    }
                }
                else
                {
                    AddMsgWeb("Error proceso invalido !!", ToastType.Error);
                }
            }
            catch
            {
                AddMsgWeb("Error al acceder a los datos", ToastType.Error);
            }
            return(RedirectToAction("Index", "Inicio"));
        }
Пример #9
0
        public ActionResult Transferencia([Bind(Include = "CuentaOrigen,CuentaDestino,Descripcion,Monto")] Transferencia pTransferencia)
        {
            if (pTransferencia.Monto < 0)
            {
                AddMsgWeb("Monto invalido, valor negativo", ToastType.Error);
                ModelState.AddModelError("ModelErr", "Monto invalido, valor negativo");
            }
            tcuenta CuentaOrigen = db.tcuentas.Where(c => c.Id == pTransferencia.CuentaOrigen).SingleOrDefault();

            if (CuentaOrigen != null)
            {
                ViewBag.Saldo   = CuentaOrigen.SaldoActual_Format;
                ViewBag.Simbolo = CuentaOrigen.tmoneda.Simbolo;
                if (pTransferencia.Monto > CuentaOrigen.SaldoActual && (CuentaOrigen.CuentaCredito == false))
                {
                    ModelState.AddModelError("ModelErr", "Monto de transferencia mayor al saldo de la cuenta");
                    AddMsgWeb("Monto de transferencia mayor al saldo de la cuenta", ToastType.Error);
                }
            }
            else
            {
                ViewBag.Saldo = "0.0";
            }
            if (ModelState.IsValid)
            {
                using (DbContextTransaction transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var         Categoria = db.tcategorias.Where(c => c.IdUsuario == sesion.Usuario.Id && c.Tipo == 3).FirstOrDefault();
                        tmovimiento Gasto     = new tmovimiento();
                        //Ingreso Gasto
                        Gasto.Id_Usuario   = sesion.Usuario.Id;
                        Gasto.Fecha        = DateTime.Now;
                        Gasto.Id_Categoria = Categoria.Id;
                        Gasto.Id_Cuenta    = pTransferencia.CuentaOrigen;
                        Gasto.Monto        = pTransferencia.Monto;
                        Gasto.Descripcion  = "Transferencia " + pTransferencia.Descripcion;
                        Gasto.Tipo         = IntValue(TipoMovimiento.Debito);
                        db.tmovimientoes.Add(Gasto);


                        tmovimiento Ingreso = new tmovimiento();
                        //Ingreso Gasto
                        Ingreso.Id_Usuario   = sesion.Usuario.Id;
                        Ingreso.Fecha        = DateTime.Now;
                        Ingreso.Id_Categoria = Categoria.Id;
                        Ingreso.Id_Cuenta    = pTransferencia.CuentaDestino;
                        Ingreso.Monto        = pTransferencia.Monto;
                        Ingreso.Descripcion  = "Transferencia " + pTransferencia.Descripcion;
                        Ingreso.Tipo         = IntValue(TipoMovimiento.Credito);
                        db.tmovimientoes.Add(Ingreso);

                        db.SaveChanges();

                        //Actulizo Saldo
                        tcuenta COrigen = db.tcuentas.Find(pTransferencia.CuentaOrigen);
                        COrigen.SaldoAnterior = COrigen.SaldoActual;
                        //Cuentas de credito los montos de gastos se convierten en negativos
                        //if (COrigen.CuentaCredito == true)
                        //{
                        //    COrigen.SaldoActual -= (pTransferencia.Monto *= -1);
                        //}
                        //else {
                        COrigen.SaldoActual -= pTransferencia.Monto;
                        //}
                        db.Entry(COrigen).State = EntityState.Modified;


                        //Actulizo Saldo
                        tcuenta CDestino = db.tcuentas.Find(pTransferencia.CuentaDestino);
                        CDestino.SaldoAnterior   = CDestino.SaldoActual;
                        CDestino.SaldoActual    += pTransferencia.Monto;
                        db.Entry(CDestino).State = EntityState.Modified;

                        db.SaveChanges();

                        //Cierro tracssaccion
                        transaction.Commit();
                        AddMsgWeb("Gasto agregado exitosamente", ToastType.Success);
                        return(RedirectToAction("Index", "Inicio"));
                    }
                    catch (DbEntityValidationException exp)
                    {
                        transaction.Rollback();

                        foreach (var eve in exp.EntityValidationErrors)
                        {
                            string msg = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                       eve.Entry.Entity.GetType().Name, eve.Entry.State);
                            AddMsgWeb("EntityValidationErrors: " + msg, ToastType.Error);

                            foreach (var ve in eve.ValidationErrors)
                            {
                                string msg1 = string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                                AddMsgWeb("EntityValidationErrors: " + msg1, ToastType.Error);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        ModelState.AddModelError("ModelErr", ex.Message);
                        AddMsgWeb("Error al agregar el registro", ToastType.Error);
                    }
                }
            }
            ViewBag.CuentaOrigen  = new SelectList(sesion.Cuentas, "Id", "Nombre", pTransferencia.CuentaOrigen);
            ViewBag.CuentaDestino = new SelectList(sesion.Cuentas.Where(c => c.Id_Usuario == sesion.Usuario.Id &&
                                                                        c.Activo && c.Id != CuentaOrigen.Id && c.Id_Moneda == CuentaOrigen.Id_Moneda), "Id", "Nombre", pTransferencia.CuentaDestino);
            return(View(pTransferencia));
        }
Пример #10
0
        public ActionResult Gasto([Bind(Include = "Id_Usuario,Id_Categoria,Id_Cuenta,Fecha,Descripcion,TipoMovimiento,Monto")] tmovimiento pMovimiento)
        {
            if (pMovimiento.Monto < 0)
            {
                AddMsgWeb("Monto invalido, valor negativo", ToastType.Error);
                ModelState.AddModelError("ModelErr", "Monto invalido, valor negativo");
            }
            if (ModelState.IsValid)
            {
                using (DbContextTransaction transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        //Ingreso Movimiento
                        pMovimiento.Tipo = IntValue(pMovimiento.TipoMovimiento);
                        db.tmovimientoes.Add(pMovimiento);
                        //db.SaveChanges();

                        //Actulizo Saldo
                        tcuenta cuenta = db.tcuentas.Find(pMovimiento.Id_Cuenta);
                        cuenta.SaldoAnterior = cuenta.SaldoActual;
                        //Cuentas de credito los montos de gastos se convierten en negativos
                        //if (cuenta.CuentaCredito == true)
                        //{
                        //    decimal MontoCredito = pMovimiento.Monto *-1;
                        //    cuenta.SaldoActual += MontoCredito;
                        //}
                        //else
                        //{
                        cuenta.SaldoActual -= pMovimiento.Monto;
                        //}
                        db.Entry(cuenta).State = EntityState.Modified;
                        db.SaveChanges();

                        //Cierro tracssaccion
                        transaction.Commit();
                        AddMsgWeb("Gasto agregado exitosamente", ToastType.Success);
                        return(RedirectToAction("Index", "Inicio"));
                    }
                    catch (DbEntityValidationException exp)
                    {
                        transaction.Rollback();

                        foreach (var eve in exp.EntityValidationErrors)
                        {
                            string msg = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                       eve.Entry.Entity.GetType().Name, eve.Entry.State);
                            AddMsgWeb("EntityValidationErrors: " + msg, ToastType.Error);

                            foreach (var ve in eve.ValidationErrors)
                            {
                                string msg1 = string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                                AddMsgWeb("EntityValidationErrors: " + msg1, ToastType.Error);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        ModelState.AddModelError("ModelErr", ex.Message);
                        AddMsgWeb("Error al agregar el registro", ToastType.Error);
                    }
                }
            }
            ViewBag.Id_Cuenta = new SelectList(sesion.Cuentas, "Id", "Nombre", pMovimiento.Id_Cuenta);
            int tipo = IntValue(TipoMovimiento.Debito);

            ViewBag.Id_Categoria = new SelectList(db.tcategorias.Where(c => c.IdUsuario == sesion.Usuario.Id &&
                                                                       c.Tipo == tipo && c.Activo), "Id", "Nombre", pMovimiento.Id_Categoria);
            return(View(pMovimiento));
        }