Exemplo n.º 1
0
        /// <summary>
        /// Crea un identificador único para un determinado documento,
        /// usando las claves del departamento y área a que pertenezca.
        /// </summary>
        /// <param name="idDepartamento"></param>
        /// <param name="idArea"></param>
        /// <param name="conta"></param>
        /// <returns></returns>
        public static string CrearUID(int idDepartamento, int idArea, out int conta)
        {
            conta = 0;
            List <Documento> lst = new PerDocumento().ObtenerTodosArea(idArea);
            var contadores       = new List <int>();

            foreach (var documento in lst)
            {
                if (documento.ContadorUID.HasValue)
                {
                    contadores.Add(documento.ContadorUID.Value);
                }
            }

            contadores = contadores.OrderByDescending(o => o).ToList();
            conta      = contadores.FirstOrDefault();
            conta++;

            Departamento depa = new PerDepartamento().Obtener(idDepartamento);
            Area         area = new PerArea().Obtener(idArea);

            var uid = depa.Clave + "-" + area.Clave + "-" + conta.ToString("D4");

            return(uid);
        }
        public ActionResult Index()
        {
            List <Departamento> lst = new PerDepartamento().ObtenerActivos();

            if (lst != null)
            {
                lst = lst.OrderBy(o => o.Nombre).ToList();
            }

            return(View(lst));
        }
        public ActionResult Eliminar(int id)
        {
            var pd    = new PerDepartamento();
            var error = "";
            var str   = new StringBuilder();

            str.Append("{");
            str.Append("\"Eliminado\":");

            try
            {
                if (id > 0)
                {
                    // Checar si no hay un área o un documento que haga referencia a éste departamento.
                    if (pd.EnUsoPorArea(id) || pd.EnUsoPorDocumento(id))
                    {
                        str.Append("0");
                        error = "Departamento en uso.";
                        Debug.WriteLine("Departamento en uso");
                    }
                    else
                    {
                        pd.Eliminar(id);
                        str.Append("1");
                        Debug.WriteLine("Eliminado");
                    }
                }
                else
                {
                    str.Append("0");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                error = ex.Message;
                str.Append("0");
            }

            str.Append(",");
            str.Append("\"Error\":");
            str.Append("\"" + error + "\"");
            str.Append("}");

            return(Json(str.ToString(), JsonRequestBehavior.AllowGet));
        }
        public ActionResult Nuevo(Departamento depa)
        {
            ActionResult ar  = RedirectToAction("Index");
            var          job = new JObject();

            job["Error"]  = true;
            job["Clase"]  = this.GetType().Name;
            job["Metodo"] = System.Reflection.MethodBase.GetCurrentMethod().Name;
            job["Nombre"] = "";
            job["Clave"]  = "";

            if (depa != null)
            {
                depa.Nombre = depa.Nombre.Trim();
                depa.Clave  = depa.Clave.Trim();

                var error = false;
                var pd    = new PerDepartamento();

                // Checar si ya hay un departamento con este nombre.
                List <Departamento> lst = pd.ObtenerActivos(0, depa.Nombre);

                if (lst.Count() > 0)
                {
                    Debug.WriteLine("lst.Count() > 0");
                    error = true;

                    foreach (var departamento in lst)
                    {
                        if (departamento.Nombre.ToUpper() == depa.Nombre.ToUpper())
                        {
                            job["Nombre"] += "El departamento \"" + depa.Nombre + "\" ya existe. ";
                        }
                    }
                }

                // Checar si ya hay un departamento con esta clave.
                List <Departamento> lstClave = pd.ObtenerActivosClave(0, depa.Clave);

                if (lstClave.Count() > 0)
                {
                    error = true;

                    foreach (var departamento in lstClave)
                    {
                        if (departamento.Clave.ToUpper() == depa.Clave.ToUpper())
                        {
                            job["Clave"] += "La clave \"" + depa.Clave + "\" ya existe. ";
                        }
                    }
                }

                if (error)
                {
                    ViewBag.validacionDeDatos = job;
                    ar = View(depa);
                }
                else
                {
                    try
                    {
                        pd.Insertar(depa);
                        job["Error"] = false;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.ToString());
                        job["Nombre"] += "Error al guardar el registro: " + ex.Message;
                        if (ex.InnerException != null)
                        {
                            job["Nombre"] += " " + ex.InnerException.Message;
                        }

                        ViewBag.validacionDeDatos = job;
                        ar = View(depa);
                    }
                }
            }

            return(ar);
        }
        public ViewResult Editar(int id)
        {
            Departamento depa = new PerDepartamento().Obtener(id);

            return(View(depa));
        }
Exemplo n.º 6
0
        public ActionResult Editar(Documento doc, HttpPostedFileBase archivo)
        {
            string rutaLog = HttpContext.Server.MapPath("/App_Data/Log.txt");

            //Utilerias.EscribirLog(rutaLog, "Usuario: " + Sesion.Current.Usuario.Nombre + " quiere editar archivo con id: " + doc.IdDocumento + ".");

            ViewBag.Departamento = new PerDepartamento().ObtenerActivos();
            ViewBag.Area         = new PerArea().ObtenerActivasDepartamento(0, doc.IdDepartamento.GetValueOrDefault());

            ActionResult ar = RedirectToAction("Index");

            var job = new JObject();

            job["Error"]   = true;
            job["Clase"]   = this.GetType().Name;
            job["Metodo"]  = System.Reflection.MethodBase.GetCurrentMethod().Name;
            job["Mensaje"] = "";

            if (archivo == null || archivo.ContentLength == 0)
            {
                job["Mensaje"]    += "Error al subir el archivo: El archivo esta vacío";
                ViewBag.validacion = job;
                ar = View(doc);

                return(ar);
            }

            if (doc != null)
            {
                // Insertar físicamente el archivo en la carpeta correspondiente y hacer commit.
                try
                {
                    // Se supone que las carpetas ya están creadas.
                    Departamento depa = new PerDepartamento().Obtener(doc.IdDepartamento.GetValueOrDefault());
                    Area         area = new PerArea().Obtener(doc.IdArea.GetValueOrDefault());

                    string depaArea = @"\" + depa.Nombre + @"\" + area.Nombre;
                    string ruta     = UtilSVN.ObtenerRutaCopiaTrabajo() + depaArea;

                    string nombreArchivo = Path.GetFileName(archivo.FileName);
                    ruta = Path.Combine(ruta, nombreArchivo);

                    // Obtener el usuario logueado.
                    var usr = Sesion.Current.Usuario;

                    // Borrar el archivo para poder poner el reemplazo.
                    System.IO.File.Delete(ruta);

                    // Guardar en el servidor.
                    archivo.SaveAs(ruta);

                    string msjActualizarArchivo = UtilSVN.ActualizarArchivo(depaArea + nombreArchivo, usr.Nombre);

                    if (msjActualizarArchivo == "Exito")
                    {
                        doc.Nombre = nombreArchivo;
                        doc.Ruta   = ruta;

                        // Editar registro en la BD.
                        new PerDocumento().Editar(doc);

                        job["Error"] = false;
                    }
                    else
                    {
                        job["Mensaje"] += msjActualizarArchivo;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    //Utilerias.EscribirLog(rutaLog, ex.ToString());

                    job["Mensaje"] += "Error al subir el archivo: " + ex.Message;

                    if (ex.InnerException != null)
                    {
                        job["Mensaje"] += " " + ex.InnerException.Message;
                    }

                    ViewBag.validacion = job;
                    ar = View(doc);
                }
            }

            return(ar);
        }
Exemplo n.º 7
0
        public ActionResult Nuevo(Documento doc, HttpPostedFileBase archivo)
        {
            string rutaLog = HttpContext.Server.MapPath("/App_Data/Log.txt");

            //Utilerias.EscribirLog(rutaLog, "Usuario: " + Sesion.Current.Usuario.Nombre + " quiere crear un nuevo archivo.");

            ViewBag.Departamento = new PerDepartamento().ObtenerActivos();
            ViewBag.Area         = null;

            ActionResult ar = RedirectToAction("Index");

            var job = new JObject();

            job["Error"]   = true;
            job["Clase"]   = this.GetType().Name;
            job["Metodo"]  = System.Reflection.MethodBase.GetCurrentMethod().Name;
            job["Mensaje"] = "";

            if (archivo == null || archivo.ContentLength == 0)
            {
                job["Mensaje"]    += "Error al subir el archivo: El archivo esta vacío";
                ViewBag.validacion = job;
                ar = View(doc);

                return(ar);
            }

            string nombreArchivo = Path.GetFileName(archivo.FileName);

            if (doc != null)
            {
                var error = false;

                // Checar si no existe un documento con el mismo nombre en esta area y departamento.
                var lst = new PerDocumento().ObtenerTodosArea(doc.IdArea.GetValueOrDefault());

                foreach (var documento in lst)
                {
                    if (documento.Nombre == nombreArchivo)
                    {
                        error = true;
                        break;
                    }
                }

                if (error)
                {
                    job["Mensaje"]    += "El nombre de archivo ya existe en esta área.";
                    ViewBag.validacion = job;
                    ar = View(doc);

                    return(ar);
                }

                try
                {
                    // Crear carpetas si no existen.
                    Departamento depa = new PerDepartamento().Obtener(doc.IdDepartamento.GetValueOrDefault());
                    Area         area = new PerArea().Obtener(doc.IdArea.GetValueOrDefault());

                    string depaArea = @"\" + depa.Nombre + @"\" + area.Nombre;
                    string ruta     = UtilSVN.ObtenerRutaCopiaTrabajo() + depaArea;
                    Utilerias.CrearRuta(ruta);

                    // Guardar en el servidor.
                    ruta = Path.Combine(ruta, nombreArchivo);
                    Debug.WriteLine("ruta al guardar archivo: " + ruta);
                    archivo.SaveAs(ruta);

                    // Obtener el usuario logueado
                    var usr = Sesion.Current.Usuario;

                    // Agregar archivo en el SVN.
                    string msjAgregarArchivo = UtilSVN.AgregarArchivo(depaArea + nombreArchivo, usr.Nombre);

                    if (msjAgregarArchivo == "Exito")
                    {
                        doc.Nombre = nombreArchivo;
                        doc.Ruta   = ruta;

                        // Crear UID.
                        int    conta;
                        string uid = Utilerias.CrearUID(doc.IdDepartamento.GetValueOrDefault(), doc.IdArea.GetValueOrDefault(), out conta);

                        doc.UID         = uid;
                        doc.ContadorUID = conta;

                        // Agregar registro a la BD.
                        new PerDocumento().Insertar(doc);

                        job["Error"] = false;
                    }
                    else
                    {
                        job["Mensaje"] += msjAgregarArchivo;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    //Utilerias.EscribirLog(rutaLog, ex.ToString());

                    job["Mensaje"] += "Error al subir el archivo: " + ex.Message;

                    if (ex.InnerException != null)
                    {
                        job["Mensaje"] += " " + ex.InnerException.Message;
                    }

                    ViewBag.validacion = job;
                    ar = View(doc);
                }
            }

            return(ar);
        }