Esempio n. 1
0
        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            Convertir funCon       = new Convertir();
            Funciones funFunciones = new Funciones();

            try
            {
                //Se traen los datos
                var productoRecetaLineaTarea = (ProductoRecetaLineaTarea)productoRecetaLineaTareaBindingSource.Current;
                if (productoRecetaLineaTarea == null)
                {
                    throw new Exception("No se selecciono tarea");
                }

                //Listado de empleados
                string c_idtra_in = string.Empty;
                var    idsEmpleadosSeleccionados = empleados.Select(o => o.n_id).Distinct();
                if (idsEmpleadosSeleccionados.Count() > 0)
                {
                    int indice = 1;
                    foreach (var idEmpleado in idsEmpleadosSeleccionados)
                    {
                        if (indice == 1)
                        {
                            c_idtra_in = idEmpleado.ToString();
                        }
                        else
                        {
                            c_idtra_in = string.Format("{0}, {1}", c_idtra_in, idEmpleado);
                        }
                        indice++;
                    }
                }

                DgvResultado.DataSource = null;
                var listaEmpleadoAvance = Empleado.FetchEmpleadosAvance(producto.n_id
                                                                        , STU_SISTEMA.EMPRESAID
                                                                        , c_idtra_in
                                                                        , productoRecetaLineaTarea.n_idtar
                                                                        , DtpFechaInicio.Value
                                                                        , DtpFechaFin.Value);

                if (listaEmpleadoAvance.Count == 0)
                {
                    throw new Exception("No se encontraron resultados");
                }

                //Se crea la tabla
                tabla = new DataTable();
                DataColumn column;
                DataRow    row;

                column = new DataColumn
                {
                    DataType   = Type.GetType("System.String"),
                    ColumnName = "Empleado"
                };
                tabla.Columns.Add(column);

                int      numeroDias  = Convert.ToInt32((DtpFechaFin.Value - DtpFechaInicio.Value).TotalDays) + 1;
                DateTime fechaInicio = DtpFechaInicio.Value;
                for (int i = 1; i <= numeroDias; i++)
                {
                    column = new DataColumn
                    {
                        DataType   = Type.GetType("System.Double"),
                        ColumnName = fechaInicio.ToString("dd/MM")
                    };
                    tabla.Columns.Add(column);
                    fechaInicio = fechaInicio.AddDays(1);
                }

                //Se obtiene el listado de Ids de personal
                var listaIdsPersonal = listaEmpleadoAvance.Select(o => o.n_idper).Distinct();

                foreach (var idPer in listaIdsPersonal)
                {
                    var empleadoAvancePer = listaEmpleadoAvance.Where(o => o.n_idper == idPer).ToList();

                    row             = tabla.NewRow();
                    row["Empleado"] = empleadoAvancePer.FirstOrDefault().c_apenom;

                    fechaInicio = DtpFechaInicio.Value;
                    for (int i = 1; i <= numeroDias; i++)
                    {
                        // Se busca registro de la fecha
                        var empleadoAvance = empleadoAvancePer
                                             .Where(o => o.d_fchtra.Day == fechaInicio.Day &&
                                                    o.d_fchtra.Month == fechaInicio.Month && o.d_fchtra.Year == fechaInicio.Year)
                                             .FirstOrDefault();

                        if (empleadoAvance != null)
                        {
                            double n_horIni      = funCon.HoraEnDecimal(empleadoAvance.c_horini);
                            double n_horFin      = funCon.HoraEnDecimal(empleadoAvance.c_horter);
                            double n_numhor      = funCon.HoraEnDecimal(funFunciones.HorasRestar(empleadoAvance.c_horini, empleadoAvance.c_horter));
                            double n_numhor_real = n_numhor;

                            string columnaFecha = fechaInicio.ToString("dd/MM");
                            //hora de almuerzo
                            if (n_horFin >= 13.5 && n_horIni <= 12.5)
                            {
                                n_numhor_real = n_numhor_real - 1;
                            }
                            //Kg/h/p
                            if (n_numhor_real > 0)
                            {
                                double n_kg_h_p = empleadoAvance.n_can / n_numhor_real;
                                row[columnaFecha] = Genericas.Round(n_kg_h_p, 2);
                            }
                            else
                            {
                                row[columnaFecha] = 0;
                            }
                        }

                        fechaInicio = fechaInicio.AddDays(1);
                    }

                    tabla.Rows.Add(row);
                }

                DgvResultado.DataSource = tabla;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Ocurrió un error: {0}", ex.Message), "Buscar", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }