This commit is contained in:
JORGE LUIS CAUDILLO ESPARZA
2019-09-04 16:28:31 -07:00
parent 0bb14f5138
commit 19d0e41453
12 changed files with 3228 additions and 135 deletions

674
Nomina/Nomina2.aspx.cs Normal file
View File

@@ -0,0 +1,674 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Extensiones;
using System.Data;
using System.Text;
public partial class Nomina_Nomina2 : ClsPaginas
{
#region Propiedades
private DataTable dtPeriodos
{
get
{
if (ViewState["dtPeriodos"] == null) { return null; }
else { return (DataTable)ViewState["dtPeriodos"]; }
}
set { ViewState["dtPeriodos"] = value; }
}
private DataTable dtConceptos
{
get
{
if (ViewState["dtConceptos"] == null) { return null; }
else { return (DataTable)ViewState["dtConceptos"]; }
}
set { ViewState["dtConceptos"] = value; }
}
private DataTable dtempleados
{
get
{
if (ViewState["dtempleados"] == null) { return null; }
else { return (DataTable)ViewState["dtempleados"]; }
}
set { ViewState["dtempleados"] = value; }
}
protected bool PermisoCancelarEmitido
{
get
{
if (ViewState["PermisoCancelarEmitido"] == null) { return false; }
else { return (bool)ViewState["PermisoCancelarEmitido"]; }
}
set { ViewState["PermisoCancelarEmitido"] = value; }
}
protected bool PermisoAutorizar
{
get
{
if (ViewState["PermisoAutorizar"] == null) { return false; }
else { return (bool)ViewState["PermisoAutorizar"]; }
}
set { ViewState["PermisoAutorizar"] = value; }
}
protected bool PermisoEmitir
{
get
{
if (ViewState["PermisoEmitir"] == null) { return false; }
else { return (bool)ViewState["PermisoEmitir"]; }
}
set { ViewState["PermisoEmitir"] = value; }
}
protected int CvePeriodoSeleccionado
{
get
{
if (ViewState["CvePeriodoSeleccionado"] == null) { return 0; }
else { return ViewState["CvePeriodoSeleccionado"].ToString().ToEntero(); }
}
set { ViewState["CvePeriodoSeleccionado"] = value; }
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cargaEjercicios();
CargaTiposPeriodo();
cargaPeriodos();
//Permisos
if (UsuarioActual.TienePermisoProceso(82))
{
PermisoAutorizar = true;
}
else
{
PermisoAutorizar = false;
}
if (UsuarioActual.TienePermisoProceso(83))
{
PermisoEmitir = true;
PermisoCancelarEmitido = true;
}
else
{
PermisoEmitir = false;
PermisoCancelarEmitido = false;
}
}
}
#region Filtros
private void cargaEjercicios()
{
int EjercicioActual = UsuarioActual.Empresa.EjercicioActual;
DataTable dtejercicio = null;
using (ClsNominas objnom = new ClsNominas())
{
if (objnom.GetEjercicios(UsuarioActual.CveEmpresa))
{
dtejercicio = objnom.DtResultados;
}
}
DdlEjercicios.DataSource = dtejercicio;
DdlEjercicios.DataBind();
if ((dtejercicio != null) && dtejercicio.Rows.Count > 0)
{
if (dtejercicio.COUNT("Ejercicio", string.Format("Ejercicio={0}", EjercicioActual)) == 0)
{
DdlEjercicios.Items.Insert(0, new ListItem(EjercicioActual.ToString(), EjercicioActual.ToString()));
}
if (dtejercicio.COUNT("Ejercicio", string.Format("Ejercicio={0}", (EjercicioActual - 1))) == 0)
{
DdlEjercicios.Items.Insert(1, new ListItem((EjercicioActual - 1).ToString(), (EjercicioActual - 1).ToString()));
}
}
else
{
DdlEjercicios.Items.Add(new ListItem(EjercicioActual.ToString(), EjercicioActual.ToString()));
DdlEjercicios.Items.Add(new ListItem((EjercicioActual - 1).ToString(), (EjercicioActual - 1).ToString()));
}
if (DdlEjercicios.Items.Count > 0)
{
DdlEjercicios.SelectedIndex = 0;
}
}
private void CargaTiposPeriodo()
{
DdlTipoPeriodo.DataSource = GlobalDTTipoPeriodo.FiltraParametros("Estatus = 'Activo'");
DdlTipoPeriodo.DataBind();
DdlTipoPeriodo.Items.Insert(0, new ListItem("Seleccione", "-1"));
if (DdlTipoPeriodo.Items.Count > 0)
{
DdlTipoPeriodo.SelectedIndex = 0;
}
}
private void cargaPeriodos()
{
int tipoPeriodo = DdlTipoPeriodo.SelectedValue.ToEntero();
int ejercicio = DdlEjercicios.SelectedValue.ToEntero();
dtPeriodos = null;
if (DdlTipoPeriodo.SelectedValue.ToEntero() > 0)
{
using (ClsNominas objperiodos = new ClsNominas())
{
if (objperiodos.GetPeriodos(tipoPeriodo, UsuarioActual.CveEmpresa, ejercicio, -1, -1))
{
dtPeriodos = objperiodos.DtResultados.FiltraParametros("Estatus IN (1,2,3,4,5)");
}
}
}
DdlPeriodo.DataSource = dtPeriodos;
DdlPeriodo.DataBind();
DdlPeriodo.Items.Insert(0, new ListItem("Seleccione", "-1"));
muestraPeriodo();
}
protected void DdlEjercicios_SelectedIndexChanged(object sender, EventArgs e)
{
cargaPeriodos();
}
protected void DdlTipoPeriodo_SelectedIndexChanged(object sender, EventArgs e)
{
cargaPeriodos();
}
protected void DdlPeriodo_SelectedIndexChanged(object sender, EventArgs e)
{
muestraPeriodo();
}
private void muestraPeriodo()
{
int CvePeriodo = DdlPeriodo.SelectedValue.ToEntero();
LnkConsultar.Visible = (CvePeriodo > 0);
UPFiltros.Update();
UpDatos.Update();
}
#endregion
#region Datos
protected void LnkConsultar_Click(object sender, EventArgs e)
{
bool continuar = false;
int CveProyecto = DdlProyecto.SelectedValue.ToEntero();
int CveSubProyecto = DdlSubProyecto.SelectedValue.ToEntero();
int CveTurno = DdlTurno.SelectedValue.ToEntero();
int CvePeriodo = DdlPeriodo.SelectedValue.ToEntero();
int CveEmpresa = UsuarioActual.CveEmpresa;
int Agrupador = DdlAgrupador.SelectedValue.ToEntero();
CvePeriodoSeleccionado = 0;
dtempleados = null;
dtConceptos = null;
using (ClsNominas objnom = new ClsNominas())
{
if (objnom.GetNominaEmpleado(CveProyecto, CveSubProyecto, CveTurno, CvePeriodo, CveEmpresa, Agrupador) && objnom.DTSetResultados.Tables.Count > 1)
{
dtempleados = objnom.DTSetResultados.Tables[0];
dtConceptos = objnom.DTSetResultados.Tables[1];
CvePeriodoSeleccionado = CvePeriodo;
continuar = true;
}
}
#region CargaGrid
if (continuar)
{
string ctipo = "P";
while (GvNominas.Columns.Count > 3)
{
GvNominas.Columns.RemoveAt(2);
}
int CurrentColumn = GvNominas.Columns.Count - 1;
foreach (DataRow row in dtConceptos.FiltraParametros("Tipo IN ('P','D','O') AND EsPadre = 1 ").Rows)
{
int CveConcepto = row["CveConcepto"].ToString().ToEntero();
string Concepto = row["Concepto"].ToString();
string Tipo = row["Tipo"].ToString();
if (Tipo != ctipo)
{
if (ctipo == "P")
{
BoundField field = new BoundField();
field.HeaderText = "Percepciones";
field.DataField = "SumaPercepciones";
field.DataFormatString = "{0:N2}";
field.ItemStyle.Width = Unit.Pixel(120);
field.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
field.ItemStyle.BackColor = System.Drawing.Color.LightGray;
GvNominas.Columns.Insert(CurrentColumn++, field);
if (Tipo != "D")
{
field = new BoundField();
field.HeaderText = "Deducciones";
field.DataField = "SumaDeducciones";
field.DataFormatString = "{0:N2}";
field.ItemStyle.Width = Unit.Pixel(120);
field.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
field.ItemStyle.BackColor = System.Drawing.Color.LightGray;
GvNominas.Columns.Insert(CurrentColumn++, field);
field = new BoundField();
field.HeaderText = "Total";
field.DataField = "Total";
field.DataFormatString = "{0:N2}";
field.ItemStyle.Width = Unit.Pixel(120);
field.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
field.ItemStyle.BackColor = System.Drawing.Color.LightGray;
GvNominas.Columns.Insert(CurrentColumn++, field);
}
}
else if (ctipo == "D")
{
BoundField field = new BoundField();
field.HeaderText = "Deducciones";
field.DataField = "SumaDeducciones";
field.DataFormatString = "{0:N2}";
field.ItemStyle.Width = Unit.Pixel(120);
field.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
field.ItemStyle.BackColor = System.Drawing.Color.LightGray;
GvNominas.Columns.Insert(CurrentColumn++, field);
field = new BoundField();
field.HeaderText = "Total";
field.DataField = "Total";
field.DataFormatString = "{0:N2}";
field.ItemStyle.Width = Unit.Pixel(120);
field.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
field.ItemStyle.BackColor = System.Drawing.Color.LightGray;
GvNominas.Columns.Insert(CurrentColumn++, field);
}
ctipo = Tipo;
}
if (dtempleados.SUM(string.Format("[{0}]", CveConcepto)) > 0)
{
BoundField field = new BoundField();
field.HeaderText = Concepto;
field.DataField = CveConcepto.ToString();
field.DataFormatString = "{0:N2}";
field.ItemStyle.Width = Unit.Pixel(80);
GvNominas.Columns.Insert(CurrentColumn++, field);
}
}
BoundField field2 = new BoundField();
field2.HeaderText = "Obligaciones";
field2.DataField = "SumaObligaciones";
field2.DataFormatString = "{0:N2}";
field2.ItemStyle.Width = Unit.Pixel(120);
field2.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
field2.ItemStyle.BackColor = System.Drawing.Color.LightGray;
GvNominas.Columns.Insert(CurrentColumn++, field2);
GvNominas.DataSource = dtempleados;
GvNominas.DataBind();
}
else
{
GvNominas.DataSource = null;
GvNominas.DataBind();
}
#endregion
UpDatos.Update();
}
protected void GvNominas_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Literal LtlNombre = e.Row.FCLiteral("LtlNombre");
LinkButton LnkTimbrar = (LinkButton)e.Row.FindControl("LnkTimbrar");
int cvePersonal = e.Row.DataKey("CvePersonal").ToEntero();
int CvePeriodo = e.Row.DataKey("CvePeriodo").ToEntero();
int CveNomina = e.Row.DataKey("CveNomina").ToEntero();
string SeTimbro = e.Row.DBCampo("SeTimbro").ToString();
string Nombre = e.Row.DBCampo("Nombre").ToString();
string Turno = e.Row.DBCampo("Turno").ToString();
string SubProyecto = e.Row.DBCampo("SubProyecto").ToString();
LnkTimbrar.Visible = false;
if (cvePersonal > 0)
{
LtlNombre.Text = String.Format("<a class=\"Texto\" href=\"/RH/Personal.aspx?CvePersonal={0}\">{1}</a>", cvePersonal, Nombre);
if (CveNomina > 0)
{
if (SeTimbro == "False")
{
LnkTimbrar.Visible = true;
e.Row.CssClass = "info";
}
else
{
e.Row.CssClass = "success";
}
}
}
else
{
LtlNombre.Text = "Total General";
if (Turno.Length == 0 && SubProyecto.Length > 0)
{
LtlNombre.Text = String.Format("<b>Sub Proyecto: {0}<b>", SubProyecto);
}
else if (Turno.Length > 0)
{
LtlNombre.Text = String.Format("<b>Turno: {0}<b>", SubProyecto);
}
else
{
e.Row.CssClass = "SubTotales";
}
e.Row.FindControl("LnkSobreRecibo").Visible = false;
}
}
}
#endregion
#region SobreRecibo
protected void LnkSobreRecibo_Click(object sender, EventArgs e)
{
}
#endregion
protected void LnkTimbrar_Click(object sender, EventArgs e)
{
}
protected void ImgDelete_Click(object sender, EventArgs e)
{
}
protected void LnkCancelar_Click(object sender, EventArgs e)
{
}
protected void LnkCargarNomina_Click(object sender, EventArgs e)
{
List<Archivo> Filesgral = TxtArchivoNomina.getfiles().Archivos;
bool continuar = Filesgral.Count > 0;
string stringError = "";
Archivo fileItem = continuar ? Filesgral[0] : null;
if (continuar)
{
string FileExtension = fileItem.Extencion.ToUpper();
if (FileExtension == "XLS" | FileExtension == "XLSX" | FileExtension == "CSV")
{
}
else
{
continuar = false;
stringError = "No se inserto un archivo de tipo Excel";
}
}
DataTable DTEXcel = new DataTable();
if (continuar)
{
try
{
using (System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection("Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + fileItem.FileUrl))
{
conn.Open();
string exceltableName = "Nomina";
DataTable tables = conn.GetSchema("Tables");
if (tables.FiltraParametros(string.Format("TABLE_NAME LIKE'%{0}%'", exceltableName)).Rows.Count == 0)
{
continuar = false;
stringError = "El documento no contiene la hoja nomina";
}
else
{
exceltableName = tables.FiltroPrimero(string.Format("TABLE_NAME LIKE'%{0}%'", exceltableName))["TABLE_NAME"].ToString();
}
if (continuar)
{
using (System.Data.Odbc.OdbcCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = $"SELECT * FROM [{exceltableName}] WHERE LEN(Codigo)>0 AND Codigo <> 'Codigo'";
using (System.Data.Odbc.OdbcDataAdapter da = new System.Data.Odbc.OdbcDataAdapter(cmd))
{
da.Fill(DTEXcel);
}
}
}
}
}
catch (Exception ex)
{
continuar = false;
stringError = "Ocurrio un Error al procesar el excel ";
string.Format(stringError, ex.ToString()).GuardaLog();
}
}
if (continuar && DTEXcel.Rows.Count == 0)
{
continuar = false;
stringError = "No se encontraron cambios";
}
StringBuilder str = new StringBuilder();
bool rollback = false;
if (continuar)
{
int CveMoneda = 1;
List<int> lConceptos = new List<int>();
foreach (DataColumn item in DTEXcel.Columns)
{
if (item.ColumnName.ToEntero() > 0)
{
lConceptos.Add(item.ColumnName.ToEntero());
}
}
using (ClsRH objpersonal = new ClsRH())
{
using (ClsNominas objNomina = new ClsNominas())
{
foreach (DataRow row in DTEXcel.Rows)
{
string Codigo = row["Codigo"].ToString();
int CvePersonal = 0;
int CveNomina = 0;
double SubTotal = 0;
double Total = 0;
int CveMetodoPago = 0;
if (Codigo != "-1" && objpersonal.GetPersonal(-1, Codigo, "-1", "-1", "-1", -1, -1, -1, -1, -1, -1, -1, 2, -1, -1, "-1", -1, -1, -1, -1, -1, -1, "-1", "-1", "-1", "-1", UsuarioActual.CveEmpresa) &&
objpersonal.DtResultados.Rows.Count == 1)
{
CvePersonal = objpersonal.CvePersonal;
CveMetodoPago = objpersonal.CveMetodoPago;
}
else
{
str.AppendLine(string.Format("No se encontro el codigo {0}<br>", Codigo));
}
if (CvePersonal > 0)
{
if (objNomina.GetNom_Nominas(-1, CvePersonal, CvePeriodoSeleccionado, -1, UsuarioActual.CveEmpresa))
{
if (objNomina.DtResultados.Rows.Count > 0)
{
CveNomina = objNomina.CveNomina;
}
else
{
if (objNomina.AddNom_Nominas(-1, 1, CvePersonal, CvePeriodoSeleccionado, SubTotal, 0, 0, Total, CveMoneda, 1, CveMetodoPago, DateTime.Now, "Deducciones nómina",
"", "", "", "", 0, 0, 0, new DateTime(), new DateTime(), new DateTime(), UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa))
{
CveNomina = objNomina.CveNomina;
}
else
{
rollback = true;
}
}
}
else
{
rollback = true;
}
}
if (CveNomina > 0)
{
foreach (int CveConcepto in lConceptos)
{
decimal Importe = 0;
decimal ImporteGravado = 0;
decimal ImporteExento = 0;
Importe = row[CveConcepto.ToString()].ToString().ToDecimal();
try
{
ImporteExento = row[string.Format("E{0}",CveConcepto)].ToString().ToDecimal();
}
catch (Exception) { }
ImporteGravado = Importe - ImporteExento;
objNomina.AddUPNom_NominasDetalles(objNomina.CveNomina, CvePeriodoSeleccionado, CvePersonal, CveConcepto, Importe, ImporteGravado, ImporteExento,UsuarioActual.CveUsuario);
}
}
}
}
}
}
if (!continuar)
{
Alert(stringError, ModExtensiones.TipoMensaje.MsgAdvertencia);
}
else
{
Alert("La información se a actualizado satisfactoriamente");
}
}
protected void LnkFormatoEmpleados_Click(object sender, EventArgs e)
{
}
protected void LnkFormato_Click(object sender, EventArgs e)
{
try
{
ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook();
var libro = wb.AddWorksheet("Nomina");
int column = 1;
libro.Row(1).Hide();
libro.Cell(1, column).Value = "Codigo";
libro.Cell(2, column).Value = "Codigo";
column++;
libro.Cell(1, column).Value = "Empleado";
libro.Cell(2, column).Value = "Empleado";
libro.Column(column).Width = 35;
foreach (DataRow row in dtConceptos.FiltraParametros("Tipo IN ('P','D','O') AND EsPadre = 1 ").Rows)
{
column++;
string CveConcepto = row["CveConcepto"].ToString();
string Concepto = row["Concepto"].ToString();
libro.Cell(1, column).Value = CveConcepto;
libro.Cell(2, column).Value = Concepto;
libro.Column(column).Width = 15;
column++;
CveConcepto = string.Format("E{0}", row["CveConcepto"]);
Concepto = string.Format("Exento {0}", row["Concepto"]);
libro.Cell(1, column).Value = CveConcepto;
libro.Cell(2, column).Value = Concepto;
libro.Column(column).Width = 15;
}
libro.Row(1).CellsUsed().Style.Font.FontColor = ClosedXML.Excel.XLColor.White;
libro.Row(2).CellsUsed().Style.Font.FontColor = ClosedXML.Excel.XLColor.White;
libro.Row(1).CellsUsed().Style.Fill.SetBackgroundColor(ClosedXML.Excel.XLColor.FromArgb(79, 129, 189));
libro.Row(2).CellsUsed().Style.Fill.SetBackgroundColor(ClosedXML.Excel.XLColor.FromArgb(79, 129, 189));
//libro.Row(1).Cell(13).Style.Fill.SetBackgroundColor(ClosedXML.Excel.XLColor.FromArgb(247, 150, 70));
//libro.Row(1).Cell(14).Style.Fill.SetBackgroundColor(ClosedXML.Excel.XLColor.FromArgb(0, 176, 240));
//libro.Row(1).Cell(15).Style.Fill.SetBackgroundColor(ClosedXML.Excel.XLColor.FromArgb(177, 160, 199));
int Fila = 2;
foreach (DataRow erow in dtempleados.FiltraParametros("CvePersonal>0").Rows)
{
Fila++;
column = 1;
libro.Cell(Fila, column).Value = erow["Codigo"];
column++;
libro.Cell(Fila, column).Value = erow["Nombre"];
foreach (DataRow row in dtConceptos.FiltraParametros("Tipo IN ('P','D','O') AND EsPadre = 1 ").Rows)
{
column++;
string Concepto = row["CveConcepto"].ToString();
libro.Cell(Fila, column).Value = erow[Concepto];
column++;
Concepto = string.Format("{0}-E", row["CveConcepto"]);
libro.Cell(Fila, column).Value = erow[Concepto];
}
}
//libro.Row(2).SetAutoFilter();
System.IO.Stream fs = new System.IO.MemoryStream();
wb.SaveAs(fs);
fs.Position = 0;
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
using (System.IO.Stream input = fs)
{
input.CopyTo(memoryStream);
}
memoryStream.Position = 0;
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xlsx", "FormatoCargaNominas"));
Response.BinaryWrite(memoryStream.ToArray());
Response.End();
}
catch (Exception ex)
{
Alert("Ocurrio un error al generar el Formato de nominas" + ex.ToString());
}
}
}