Пример #1
0
        private void toolAplicar_Click(object sender, EventArgs e)
        {
            if (dgvCargaFaltas.Rows.Count == 0)
            {
                MessageBox.Show("No se puede aplicar verifique.", "Error");
                return;
            }

            int idEmpleado = 0;
            bool EsAlta = false, EsReingreso = false, EsBaja = false;

            cnx = new SqlConnection(cdn);
            cmd = new SqlCommand();
            cmd.Connection = cnx;

            fh = new Faltas.Core.FaltasHelper();
            fh.Command = cmd;

            emph = new Empleados.Core.EmpleadosHelper();
            emph.Command = cmd;

            Altas.Core.AltasHelper ah = new Altas.Core.AltasHelper();
            ah.Command = cmd;

            Reingreso.Core.ReingresoHelper rh = new Reingreso.Core.ReingresoHelper();
            rh.Command = cmd;

            Bajas.Core.BajasHelper bh = new Bajas.Core.BajasHelper();
            bh.Command = cmd;

            Periodos.Core.PeriodosHelper ph = new Periodos.Core.PeriodosHelper();
            ph.Command = cmd;

            List<Altas.Core.Altas> lstAlta;
            List<Reingreso.Core.Reingresos> lstReingreso;
            List<Bajas.Core.Bajas> lstBaja;

            foreach (DataGridViewRow fila in dgvCargaFaltas.Rows)
            {
                try
                {
                    cnx.Open();
                    idEmpleado = (int)emph.obtenerIdTrabajador(fila.Cells["noempleado"].Value.ToString(), idEmpresa);
                    cnx.Close();

                }
                catch (Exception error)
                {
                    MessageBox.Show("Error: Obtener ID del empleado. \r\n \r\n" + error.Message, "Error");
                    return;
                }

                int idperiodo = 0;
                try
                {
                    cnx.Open();
                    idperiodo = (int)emph.obtenerIdPeriodo(idEmpleado);
                    cnx.Close();
                }
                catch
                {
                    MessageBox.Show("Error: al obtener el Id del Periodo.", "Error");
                    cnx.Dispose();
                    return;
                }

                Altas.Core.Altas alta = new Altas.Core.Altas();
                alta.idempresa = GLOBALES.IDEMPRESA;
                alta.idtrabajador = idEmpleado;
                alta.periodoInicio = _inicioPeriodo.Date;
                alta.periodoFin = _finPeriodo.Date;

                Reingreso.Core.Reingresos reingreso = new Reingreso.Core.Reingresos();
                reingreso.idempresa = GLOBALES.IDEMPRESA;
                reingreso.idtrabajador = idEmpleado;
                reingreso.periodoinicio = _inicioPeriodo.Date;
                reingreso.periodofin = _finPeriodo.Date;

                Bajas.Core.Bajas baja = new Bajas.Core.Bajas();
                baja.idempresa = GLOBALES.IDEMPRESA;
                baja.idtrabajador = idEmpleado;
                baja.periodoinicio = _inicioPeriodo.Date;
                baja.periodofin = _finPeriodo.Date;

                lstAlta = new List<Altas.Core.Altas>();
                lstReingreso = new List<Reingreso.Core.Reingresos>();
                lstBaja = new List<Bajas.Core.Bajas>();

                Periodos.Core.Periodos p = new Periodos.Core.Periodos();
                p.idperiodo = idperiodo;

                int periodo = 0;
                try
                {
                    cnx.Open();
                    periodo = (int)ph.DiasDePago(p);

                    lstAlta = ah.obtenerAlta(alta);
                    lstReingreso = rh.obtenerReingreso(reingreso);
                    lstBaja = bh.obtenerBaja(baja);
                    cnx.Close();
                }
                catch
                {
                    MessageBox.Show("Error: al obtener los dias de pago.", "Error");
                    cnx.Dispose();
                    return;
                }

                if (lstAlta.Count != 0)
                    EsAlta = true;
                if (lstBaja.Count != 0)
                    EsBaja = true;
                if (lstReingreso.Count != 0)
                    EsReingreso = true;

                int falta = int.Parse(fila.Cells["faltas"].Value.ToString());
                DateTime fecha = DateTime.Parse(fila.Cells["fechainicio"].Value.ToString());

                if (falta > 15)
                    falta = 15;

                for (int i = 0; i < falta; i++)
                {
                    int existe = 0;
                    int existeFalta = 0;
                    int existeVacacion = 0;
                    try
                    {
                        cnx.Open();
                        existeFalta = (int)fh.existeFalta(idEmpleado, fecha.AddDays(i).Date);
                        cnx.Close();
                    }
                    catch
                    {
                        MessageBox.Show("Error: Al verificar existencia de falta.", "Error");
                        cnx.Dispose();
                        return;
                    }

                    if (existeFalta == 0)
                    {
                        Incidencias.Core.IncidenciasHelper ih = new Incidencias.Core.IncidenciasHelper();
                        ih.Command = cmd;

                        Vacaciones.Core.VacacionesHelper vh = new Vacaciones.Core.VacacionesHelper();
                        vh.Command = cmd;

                        bool FLAG_FALTAS = false;

                        if (EsAlta)
                        {
                            if (fecha.AddDays(i).Date < lstAlta[0].fechaingreso.Date)
                            {
                                MessageBox.Show("Error: Alta del empleado: " + fila.Cells["noempleado"].Value.ToString() + ", Fecha de Ingreso = " + lstAlta[0].fechaingreso.Date.ToShortDateString() + "\r\n Fecha de la falta es menor.", "Error");
                                FLAG_FALTAS = true;
                            }
                            else
                                FLAG_FALTAS = false;
                        }

                        if (EsReingreso)
                        {
                            if (fecha.AddDays(i).Date < lstReingreso[0].fechaingreso.Date)
                            {
                                MessageBox.Show("Error: Alta del empleado: " + fila.Cells["noempleado"].Value.ToString() + ", Fecha de Reingreso = " + lstReingreso[0].fechaingreso.Date.ToShortDateString() + "\r\n Fecha de la falta es menor.", "Error");
                                FLAG_FALTAS = true;
                            }
                            else
                                FLAG_FALTAS = false;
                        }

                        if (EsBaja)
                        {
                            if (fecha.AddDays(i).Date > lstBaja[0].fecha.Date)
                            {
                                MessageBox.Show("Error: Alta del empleado " + fila.Cells["noempleado"].Value.ToString() +
                                    ", Fecha de Reingreso = " + lstBaja[0].fecha.Date.ToShortDateString() +
                                    "\r\n Fecha de la falta es mayor.", "Error");
                                FLAG_FALTAS = true;
                            }
                            else
                                FLAG_FALTAS = false;
                        }

                        if (!FLAG_FALTAS)
                        {
                            try
                            {
                                cnx.Open();
                                existe = (int)ih.existeIncidenciaEnFalta(idEmpleado, fecha.AddDays(i).Date);
                                existeVacacion = (int)vh.existeVacacionEnFalta(idEmpleado, fecha.AddDays(i).Date);
                                cnx.Close();
                            }
                            catch
                            {
                                MessageBox.Show("Error: Al guardar la falta.", "Error");
                                cnx.Dispose();
                                return;
                            }

                            if (existe == 0 && existeVacacion == 0)
                                try
                                {
                                    Faltas.Core.Faltas f = new Faltas.Core.Faltas();
                                    f.idempresa = GLOBALES.IDEMPRESA;
                                    f.idtrabajador = idEmpleado;
                                    f.periodo = periodo;
                                    f.faltas = 1;
                                    f.fechainicio = DateTime.Parse(dgvCargaFaltas.Rows[0].Cells["fechainicio"].Value.ToString());
                                    f.fechafin = DateTime.Parse(dgvCargaFaltas.Rows[0].Cells["fechafin"].Value.ToString());
                                    f.fecha = fecha.AddDays(i).Date;

                                    cnx.Open();
                                    fh.insertaFalta(f);
                                    cnx.Close();
                                }
                                catch
                                {
                                    MessageBox.Show("Error: Al guardar la falta.", "Error");
                                    cnx.Dispose();
                                }
                            else
                                MessageBox.Show("La falta ingresada, se empalma con una incapacidad y/o dia de vacación del trabajador.", "Error");
                        }
                    }
                }
                EsAlta = false; EsReingreso = false; EsBaja = false;
            }
            MessageBox.Show("Faltas importadas", "Confirmación");
            cnx.Dispose();
            dgvCargaFaltas.Rows.Clear();
        }
        private void dgvFaltas_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (FLAGCARGA)
            {
                FLAGCARGA = false;
                return;
            }

            if (dgvFaltas.Columns[e.ColumnIndex].Name.ToString() == "")
                return;
            if (dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value.ToString() == "" ||
                dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value.ToString() == "0")
                return;

            try
            {
                int.Parse(dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value.ToString());
            }
            catch
            {
                MessageBox.Show("Dato incorrecto. SOLO NUMEROS.", "Información");
                dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value = "";
                return;
            }

            int existe = 0, existeVacacion = 0;
            int faltas = int.Parse(dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value.ToString());
            DateTime fechaColumna = DateTime.Parse(dgvFaltas.Columns[e.ColumnIndex].Name.ToString());

            if (faltas > 1 || faltas < 0)
            {
                MessageBox.Show("Solo se admite el valor de 1 falta por dia.", "Información");
                dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value = "";
                return;
            }

            cnx = new SqlConnection(cdn);
            cmd = new SqlCommand();
            cmd.Connection = cnx;
            Incidencias.Core.IncidenciasHelper ih = new Incidencias.Core.IncidenciasHelper();
            ih.Command = cmd;

            Vacaciones.Core.VacacionesHelper vh = new Vacaciones.Core.VacacionesHelper();
            vh.Command = cmd;

            try
            {
                cnx.Open();
                existe = (int)ih.existeIncidenciaEnFalta(int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["idtrabajadorfalta"].Value.ToString()), fechaColumna.Date);
                existeVacacion = (int)vh.existeVacacionEnFalta(int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["idtrabajadorfalta"].Value.ToString()), fechaColumna.Date);
                cnx.Close();
            }
            catch
            {
                MessageBox.Show("Error: \r\n \r\n No se pudo verificar la incapacidad, verifique.", "Error");
                dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value = "";
                cnx.Dispose();
            }

            if (existe == 0 && existeVacacion == 0)
            {
                fh = new Faltas.Core.FaltasHelper();
                fh.Command = cmd;
                Faltas.Core.Faltas falta = new Faltas.Core.Faltas();
                falta.idtrabajador = int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["idtrabajadorfalta"].Value.ToString());
                falta.idempresa = GLOBALES.IDEMPRESA;
                falta.periodo = _periodo;
                falta.faltas = int.Parse(dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value.ToString());
                falta.fechainicio = periodoInicio.Date;
                falta.fechafin = periodoFin.Date;
                falta.fecha = DateTime.Parse(dgvFaltas.Columns[e.ColumnIndex].Name.ToString());

                try
                {
                    cnx.Open();
                    fh.insertaFalta(falta);
                    cnx.Close();
                    cnx.Dispose();
                }
                catch
                {
                    MessageBox.Show("Error: \r\n \r\n Se ingreso un valor incorrecto, verifique.", "Error");
                    dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value = "";
                    cnx.Dispose();
                }
            }
            else
            {
                MessageBox.Show("La falta ingresada, se empalma con una incapacidad y/o dia de vacación del trabajador.", "Error");
                dgvFaltas.Rows[e.RowIndex].Cells[dgvFaltas.Columns[e.ColumnIndex].Name.ToString()].Value = "";
            }

            #region CODIGO COMENTADO
            //if (dgvFaltas.Columns[e.ColumnIndex].Name == "falta")
            //{
            //    if (int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["falta"].Value.ToString()) > _periodo)
            //    {
            //        MessageBox.Show("La falta ingresada es mayor al periodo. Verifique", "Error");
            //        dgvFaltas.Rows[e.RowIndex].Cells["falta"].Value = 0;
            //        return;
            //    }

            //    if (dgvFaltas.Rows[e.RowIndex].Cells["falta"].Value.ToString() != "0")
            //    {
            //        if (dgvFaltas.Rows[e.RowIndex].Cells["incapacidad"].Value.ToString() != "0")
            //        {
            //            borraIncapacidad(e.ColumnIndex + 1, e.RowIndex);
            //            dgvFaltas.Rows[e.RowIndex].Cells["incapacidad"].Value = 0;
            //        }

            //        cnx = new SqlConnection(cdn);
            //        cmd = new SqlCommand();
            //        cmd.Connection = cnx;
            //        fh = new Faltas.Core.FaltasHelper();
            //        fh.Command = cmd;
            //        Faltas.Core.Faltas falta = new Faltas.Core.Faltas();
            //        falta.idtrabajador = int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["idtrabajadorfalta"].Value.ToString());
            //        falta.idempresa = GLOBALES.IDEMPRESA;
            //        falta.periodo = _periodo;
            //        falta.faltas = int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["falta"].Value.ToString());
            //        falta.fechainicio = dtpPeriodoInicio.Value.Date;
            //        falta.fechafin = dtpPeriodoFin.Value.Date;

            //        try
            //        {
            //            cnx.Open();
            //            fh.insertaFalta(falta);
            //            cnx.Close();
            //            cnx.Dispose();
            //        }
            //        catch (Exception error)
            //        {
            //            MessageBox.Show("Error: \r\n \r\n Se ingreso un valor incorrecto, verifique.", "Error");
            //            dgvFaltas.Rows[e.RowIndex].Cells["falta"].Value = 0;
            //            cnx.Dispose();
            //        }
            //    }
            //    else
            //    {
            //        borraFalta(e.ColumnIndex, e.RowIndex);
            //    }
            //}

            //if (dgvFaltas.Columns[e.ColumnIndex].Name == "incapacidad")
            //{
            //    if (int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["incapacidad"].Value.ToString()) > _periodo)
            //    {
            //        MessageBox.Show("La incapacidad ingresada es mayor al periodo. Verifique", "Error");
            //        dgvFaltas.Rows[e.RowIndex].Cells["incapacidad"].Value = 0;
            //        return;
            //    }

            //    if (dgvFaltas.Rows[e.RowIndex].Cells["incapacidad"].Value.ToString() != "0")
            //    {
            //        if (dgvFaltas.Rows[e.RowIndex].Cells["falta"].Value.ToString() != "0")
            //        {
            //            borraFalta(e.ColumnIndex - 1, e.RowIndex);
            //            dgvFaltas.Rows[e.RowIndex].Cells["falta"].Value = 0;
            //        }

            //        cnx = new SqlConnection(cdn);
            //        cmd = new SqlCommand();
            //        cmd.Connection = cnx;
            //        ih = new Incapacidad.Core.IncapacidadHelper();
            //        ih.Command = cmd;
            //        Incapacidad.Core.Incapacidades incapacidad = new Incapacidad.Core.Incapacidades();
            //        incapacidad.idtrabajador = int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["idtrabajadorfalta"].Value.ToString());
            //        incapacidad.idempresa = GLOBALES.IDEMPRESA;
            //        incapacidad.diasincapacidad = 0;
            //        incapacidad.diastomados = int.Parse(dgvFaltas.Rows[e.RowIndex].Cells["incapacidad"].Value.ToString());
            //        incapacidad.diasrestantes = 0;
            //        incapacidad.diasapagar = 0;
            //        incapacidad.tipo = 0;
            //        incapacidad.aplicada = 1;
            //        incapacidad.consecutiva = 1;
            //        incapacidad.fechainicio = dtpPeriodoInicio.Value.Date;
            //        incapacidad.fechafin = dtpPeriodoFin.Value.Date;

            //        try
            //        {
            //            cnx.Open();
            //            ih.insertaIncapacidad(incapacidad);
            //            cnx.Close();
            //            cnx.Dispose();
            //        }
            //        catch (Exception error)
            //        {
            //            MessageBox.Show("Error: \r\n \r\n Se ingreso un valor incorrecto, verifique.", "Error");
            //            dgvFaltas.Rows[e.RowIndex].Cells["incapacidad"].Value = 0;
            //            cnx.Dispose();
            //        }
            //    }
            //    else
            //    {
            //        borraIncapacidad(e.ColumnIndex, e.RowIndex);
            //    }
            //}
            #endregion
        }