// GET: RecordsWeek
        public ActionResult Index()
        {
            DO_Persona personaConectada = ((DO_Persona)Session["UsuarioConectado"]);

            DO_Compania dO_Compania = DataManager.GetCompania(personaConectada.idCompania);

            List <DO_Semana> dO_Semanas = DataManager.GetSemanas(dO_Compania.FechaRegistro);

            List <SelectListItem> listItems = new List <SelectListItem>();

            SelectListItem selectListItem1 = new SelectListItem();

            selectListItem1.Text  = "Selecciona una opción";
            selectListItem1.Value = "0";
            listItems.Add(selectListItem1);

            foreach (var item in dO_Semanas)
            {
                SelectListItem selectListItem = new SelectListItem();

                selectListItem.Text  = "Semana " + "#" + item.NoSemana + "  De  " + item.SFechaInicial + " a " + item.SFechaFinal;
                selectListItem.Value = item.IdSemana.ToString();

                listItems.Add(selectListItem);
            }

            ViewBag.Semanas = listItems;

            return(View());
        }
        public ActionResult Editar(int id = 0, DO_Persona persona = null)
        {
            DO_Persona personaBuscada   = DataManager.GetPersona(id);
            DO_Persona personaConectada = ((DO_Persona)Session["UsuarioConectado"]);

            DO_Compania dO_Compania = DataManager.GetCompania(personaConectada.idCompania);

            ViewBag.Personas = DataManager.GetPosiblesJefes(personaConectada.idCompania);

            foreach (SelectListItem item in ViewBag.Personas)
            {
                if (Convert.ToInt32(item.Value) == personaBuscada.IdJefe)
                {
                    item.Selected = true;
                }
            }

            List <SelectListItem> roles = new List <SelectListItem>();

            roles.Add(new SelectListItem {
                Text = "Promotor", Value = "4", Selected = false
            });
            roles.Add(new SelectListItem {
                Text = "Supervisor", Value = "6", Selected = false
            });
            roles.Add(new SelectListItem {
                Text = "Supervidor Elite", Value = "7", Selected = false
            });
            roles.Add(new SelectListItem {
                Text = "Asistente", Value = "8", Selected = false
            });

            foreach (var rol in roles)
            {
                if (Convert.ToInt32(rol.Value) == personaBuscada.ID_ROL)
                {
                    rol.Selected = true;
                }
            }

            ViewBag.Roles = roles;



            List <DO_Semana> dO_Semanas = DataManager.GetSemanas(dO_Compania.FechaRegistro);

            List <SelectListItem> listItems = new List <SelectListItem>();

            SelectListItem selectListItem1 = new SelectListItem();

            selectListItem1.Text  = "Selecciona una opción";
            selectListItem1.Value = "0";
            listItems.Add(selectListItem1);

            foreach (var item in dO_Semanas)
            {
                SelectListItem selectListItem = new SelectListItem();

                selectListItem.Text  = "Semana " + "#" + item.NoSemana + "  De  " + item.SFechaInicial + " a " + item.SFechaFinal;
                selectListItem.Value = item.IdSemana.ToString();

                listItems.Add(selectListItem);
            }

            ViewBag.Semanas = listItems;

            return(View(personaBuscada));
        }
        public ActionResult BajarArchivo(int idSemana)
        {
            DO_Persona        personaConectada = ((DO_Persona)Session["UsuarioConectado"]);
            DO_Compania       compania         = DataManager.GetCompania(personaConectada.idCompania);
            List <DO_Almacen> almacens         = DataManager.GetAllAlmacen(compania.IdCompania);

            string     path       = Server.MapPath("~/assets/files/formatoreportesemanal.xlsx");
            SLDocument sLDocument = new SLDocument(path, "Reporte");

            List <DO_Deposito> depositos = new List <DO_Deposito>();

            depositos = DataManager.GetDepositosPorWeek(personaConectada.idUsuario, idSemana);

            List <DO_Movimiento> entradas = new List <DO_Movimiento>();

            entradas = DataManager.GetMovimientoEntradasPorWeek(personaConectada.idCompania, idSemana);

            List <DO_Movimiento> salidas = new List <DO_Movimiento>();

            salidas = DataManager.GetMovimientoSalidasPorWeek(personaConectada.idCompania, idSemana);

            List <DO_Ventas> ventas = new List <DO_Ventas>();

            ventas = DataManager.GetListVentaPorSemana(personaConectada.idUsuario, idSemana);

            DO_Semana dO_Semana  = DataManager.GetSemana(idSemana);
            string    rangoFecha = dO_Semana.SFechaInicial + " a " + dO_Semana.SFechaFinal;

            List <DO_ReporteSemanal> dO_Reportes = new List <DO_ReporteSemanal>();

            List <DO_Movimiento> movimientosEntradas = new List <DO_Movimiento>();

            movimientosEntradas = DataManager.GetAllEntradas(almacens[0].idAlmacen, idSemana);

            //Entradas
            foreach (DO_Movimiento entrada in entradas)
            {
                if (dO_Reportes.Where(x => x.IdArticulo == entrada.IdArticulo).ToList().Count > 0)
                {
                    DO_ReporteSemanal reporteSemanal = dO_Reportes.Where(x => x.IdArticulo == entrada.IdArticulo).FirstOrDefault();
                    int index = dO_Reportes.IndexOf(reporteSemanal);

                    dO_Reportes[index].Entradas += entrada.Cantidad;
                    dO_Reportes[index].Origen   += entrada.BodegaDestino + "(" + entrada.Cantidad + ") ";
                }
                else
                {
                    DO_ReporteSemanal reporteSemanal = new DO_ReporteSemanal();

                    reporteSemanal.NombreArticulo = entrada.Nombre;
                    reporteSemanal.Entradas       = entrada.Cantidad;
                    reporteSemanal.Origen         = entrada.BodegaDestino + "(" + entrada.Cantidad + ") ";
                    reporteSemanal.IdArticulo     = entrada.IdArticulo;

                    dO_Reportes.Add(reporteSemanal);
                }
            }

            //Salidas
            foreach (DO_Movimiento salida in salidas)
            {
                if (dO_Reportes.Where(x => x.IdArticulo == salida.IdArticulo).ToList().Count > 0)
                {
                    DO_ReporteSemanal reporteSemanal = dO_Reportes.Where(x => x.IdArticulo == salida.IdArticulo).FirstOrDefault();
                    int index = dO_Reportes.IndexOf(reporteSemanal);

                    dO_Reportes[index].Salidas += salida.Cantidad;
                    dO_Reportes[index].Destino += salida.BodegaDestino + "(" + salida.Cantidad + ") ";
                }
                else
                {
                    DO_ReporteSemanal reporteSemanal = new DO_ReporteSemanal();

                    reporteSemanal.NombreArticulo = salida.Nombre;
                    reporteSemanal.Salidas        = salida.Cantidad;
                    reporteSemanal.Destino        = salida.BodegaDestino + "(" + salida.Cantidad + ") ";
                    reporteSemanal.IdArticulo     = salida.IdArticulo;

                    dO_Reportes.Add(reporteSemanal);
                }
            }

            //Ventas
            foreach (var venta in ventas)
            {
                if (dO_Reportes.Where(x => x.IdArticulo == venta.IdArticulo).ToList().Count > 0)
                {
                    DO_ReporteSemanal reporteSemanal = dO_Reportes.Where(x => x.IdArticulo == venta.IdArticulo).FirstOrDefault();
                    int index = dO_Reportes.IndexOf(reporteSemanal);

                    dO_Reportes[index].ArticulosVendidos += venta.Cantidad;
                }
                else
                {
                    DO_ReporteSemanal reporteSemanal = new DO_ReporteSemanal();

                    reporteSemanal.NombreArticulo    = venta.Nombre;
                    reporteSemanal.IdArticulo        = venta.IdArticulo;
                    reporteSemanal.ArticulosVendidos = venta.Cantidad;

                    dO_Reportes.Add(reporteSemanal);
                }
            }

            //PRECIO
            foreach (DO_ReporteSemanal item in dO_Reportes)
            {
                double costo = DataManager.GetArticulo(item.IdArticulo).PRECIO_MASTER;

                item.CostoUnitario = costo;
            }

            //Inventario inicial

            List <FO_Item> existencias = DataManager.GetCorteExistencia(idSemana, almacens[0].idAlmacen);

            foreach (var item in dO_Reportes)
            {
                int i = existencias.Where(x => x.NombreInt == item.IdArticulo).ToList().Count;
                if (i == 0)
                {
                    item.InventarioInicial = 0;
                }
                else
                {
                    FO_Item temp       = existencias.Where(x => x.NombreInt == item.IdArticulo).FirstOrDefault();
                    int     existencia = temp.ValueInt;
                    item.InventarioInicial = existencia;
                }
            }

            #region Llenado de información
            sLDocument.SetCellValue("F4", personaConectada.NombreCompleto);
            sLDocument.SetCellValue("I5", compania.Direccion);
            sLDocument.SetCellValue("F7", compania.Telefono);
            sLDocument.SetCellValue("F10", dO_Semana.NoSemana);
            sLDocument.SetCellValue("H10", rangoFecha);
            sLDocument.SetCellValue("K7", personaConectada.Usuario);

            //Llenado de depositos.
            int c = 32;
            foreach (var deposito in depositos)
            {
                sLDocument.SetCellValue("A" + c, deposito.FechaIngreso);
                sLDocument.SetCellValue("B" + c, deposito.Banco);
                sLDocument.SetCellValue("C" + c, deposito.Importe);
                sLDocument.SetCellValue("D" + c, deposito.Descripcion);
                sLDocument.SetCellValue("F" + c, deposito.Importe);
                c++;
            }

            //Llenado de costo de guia
            foreach (var item in movimientosEntradas)
            {
                sLDocument.SetCellValue("D" + c, item.NoFactura);
                sLDocument.SetCellValue("F" + c, item.CostoGuia);
                c++;
            }

            c = 17;
            //Llenado de articulos
            foreach (var reporteSemanal in dO_Reportes)
            {
                sLDocument.SetCellValue("B" + c, reporteSemanal.NombreArticulo);
                sLDocument.SetCellValue("C" + c, reporteSemanal.InventarioInicial);
                sLDocument.SetCellValue("D" + c, reporteSemanal.Entradas);
                sLDocument.SetCellValue("E" + c, reporteSemanal.Origen);
                sLDocument.SetCellValue("F" + c, reporteSemanal.Salidas);
                sLDocument.SetCellValue("G" + c, reporteSemanal.Destino);
                sLDocument.SetCellValue("I" + c, reporteSemanal.ArticulosVendidos);
                sLDocument.SetCellValue("K" + c, reporteSemanal.CostoUnitario);

                c++;
            }

            #endregion

            sLDocument.AutoFitRow(18);

            if (!Directory.Exists(Server.MapPath("~/files/reportesemanal/" + personaConectada.Nombre)))
            {
                Directory.CreateDirectory(Server.MapPath("~/files/reportesemanal/" + personaConectada.Nombre));
            }

            string newPath = Server.MapPath("~/files/reportesemanal/" + personaConectada.Nombre + "/reporte_" + dO_Semana.NoSemana + ".xlsx");
            sLDocument.SaveAs(newPath);

            byte[] fileBytes = System.IO.File.ReadAllBytes(newPath);

            string fileName = "Reporte_" + dO_Semana.NoSemana + "_" + personaConectada.Nombre + ".xlsx";

            DataManager.InsertBitacora(personaConectada.Nombre + " " + personaConectada.Usuario, "Se bajo el archivo de excel se la semana id: " + idSemana);

            return(File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName));
        }