Exemplo n.º 1
0
        public ActionResult ObtenerActivosLista()
        {
            List <ProyectoDTO> listado = Mapper.Map <IEnumerable <Proyectos>, List <ProyectoDTO> >(ProyectosNegocios.ObtenerActivosDelAnio(DateTime.Now.Year));

            return(Json(new { results = listado.Select(p => new { id = p.Clave, text = p.DescripcionCompleta }) }));
        }
        public JsonResult ImportarArchivo()
        {
            //List<CalendarioClavePresupuestal> claves = new List<CalendarioClavePresupuestal>();

            List <ClavesPresupuestales> claves = new List <ClavesPresupuestales>();

            var archivo = Request.Files[0];

            using (ExcelPackage package = new ExcelPackage(archivo.InputStream))
            {
                //validar que solo tenga una hoja
                if (package.Workbook.Worksheets.Count != 1)
                {
                    return(Json(new { exitoso = false, mensaje = "El archivo tiene más de una hoja" }));
                }

                var hoja = package.Workbook.Worksheets[1];

                int filas = hoja.Dimension.End.Row;

                for (int i = 2; i <= filas; i++)
                {
                    string cadenaClave = hoja.Cells[i, 1].Value.ToString();

                    var pro  = ProgramasNegocios.ObtenerActivosDelAnioPorClave(2019, cadenaClave.Substring(22, 3));
                    var proj = ProyectosNegocios.ObtenerActivosDelAnioPorClave(2019, cadenaClave.Substring(25, 4));
                    var act  = ActividadesNegocios.ObtenerActivosDelAnioPorClave(2019, cadenaClave.Substring(29, 4));
                    var par  = PartidasNegocios.ObtenerActivaPorClave(cadenaClave.Substring(39, 4));
                    var cc   = CentrosCostosNegocios.ObtenerPorClave(ObtenerClaveCentroDeCostoPorClaveActividad(cadenaClave.Substring(29, 4)));

                    claves.Add(new ClavesPresupuestales
                    {
                        Clave                 = cadenaClave,
                        PresupuestoEnero      = hoja.Cells[i, 2].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 2].Value.ToString()),
                        PresupuestoFebrero    = hoja.Cells[i, 3].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 3].Value.ToString()),
                        PresupuestoMarzo      = hoja.Cells[i, 4].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 4].Value.ToString()),
                        PresupuestoAbril      = hoja.Cells[i, 5].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 5].Value.ToString()),
                        PresupuestoMayo       = hoja.Cells[i, 6].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 6].Value.ToString()),
                        PresupuestoJunio      = hoja.Cells[i, 7].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 7].Value.ToString()),
                        PresupuestoJulio      = hoja.Cells[i, 8].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 8].Value.ToString()),
                        PresupuestoAgosto     = hoja.Cells[i, 9].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 9].Value.ToString()),
                        PresupuestoSeptiembre = hoja.Cells[i, 10].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 10].Value.ToString()),
                        PresupuestoOctubre    = hoja.Cells[i, 11].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 11].Value.ToString()),
                        PresupuestoNoviembre  = hoja.Cells[i, 12].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 12].Value.ToString()),
                        PresupuestoDiciembre  = hoja.Cells[i, 13].Value == null ? 0 : Convert.ToDecimal(hoja.Cells[i, 13].Value.ToString()),
                        Anio          = 2019,
                        Activo        = true,
                        IdPrograma    = pro.Id,
                        Programas     = pro,
                        IdProyecto    = proj.Id,
                        Proyectos     = proj,
                        IdActividad   = act.Id,
                        Actividades   = act,
                        IdPartida     = par.Id,
                        Partidas      = par,
                        IdCentroCosto = cc.Id,
                        CentrosCostos = cc
                    });
                }

                var clavesDTO = AutoMapper.Mapper.Map <IEnumerable <ClavesPresupuestales>, IEnumerable <ClavePresupuestalDTO> >(claves);
                Session["ClavesPresupuestales"] = clavesDTO;

                return(Json(new
                {
                    exitoso = true,
                    mensaje = "Se importó correctamente",
                    claves = clavesDTO.Select(c => new
                    {
                        clave = c.Clave + "| |" + c.PresupuestoEnero + "|" + c.PresupuestoFebrero + "|" + c.PresupuestoMarzo +
                                "|" + c.PresupuestoAbril + "|" + c.PresupuestoMayo + "|" + c.PresupuestoJunio + "|" + c.PresupuestoJulio + "|" + c.PresupuestoAgosto + "|" + c.PresupuestoSeptiembre +
                                "|" + c.PresupuestoOctubre + "|" + c.PresupuestoNoviembre + "|" + c.PresupuestoDiciembre
                    }),
                    clavesPresupuestales = clavesDTO
                }));
            }
        }