Option Explicit On Option Strict On Imports System.Data Imports Extensiones Partial Class Contratos_Presupuestos Inherits ClsPaginas #Region "P R O P I E D A D E S" Public Property CvePresupuesto() As Integer Get Return ViewState("CvePresupuesto").ToString.ToEntero End Get Set(ByVal value As Integer) ViewState("CvePresupuesto") = value End Set End Property Public Property EstatusActual() As Integer Get Return ViewState("EstatusActual").ToString.ToEntero End Get Set(ByVal value As Integer) ViewState("EstatusActual") = value End Set End Property Public ReadOnly Property EstatusFinal() As Integer Get Return 2 End Get End Property Public Property DtClasificaciones() As DataTable Get Return DirectCast(ViewState("DtClasificaciones"), DataTable) End Get Set(ByVal value As DataTable) ViewState("DtClasificaciones") = value End Set End Property #End Region Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not IsPostBack Then CvePresupuesto = IfNull(Request("CvePresupuesto"), -1).ToString.ToEntero If CvePresupuesto > 0 Then CargaDatos() Else InicializaValores() CargaClasificaciones() End If UpAcciones.Update() UpDatosG.Update() UpGvClasificaciones.Update() UpIndicadores.Update() End If End Sub #Region "I N I C I A L I Z A - V A L O R E S" Private Sub InicializaValores() TxtFechaI.Text = $"01/01/{Now.Year}" TxtFechaF.Text = Now.ToShortDateString CargaProyecto() CargaMoneda() End Sub Private Sub CargaMoneda() 'DdlMoneda.DataSource = GlobalDTMonedas() 'DdlMoneda.DataBind() End Sub Private Sub CargaProyecto() DdlProyecto.Items.Clear() DdlProyecto.DataSource = GlobalDTProyectos(UsuarioActual.Empresa.CveEmpresa).FiltraParametros("", "", {"CveProyecto", "Proyecto"}) DdlProyecto.DataBind() DdlProyecto.Items.Insert(0, New ListItem("Seleccione un Proyecto", "-1")) End Sub Protected Sub DdlProyecto_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DdlProyecto.SelectedIndexChanged CargaSubProyectoContrato(DdlProyecto.SelectedValue.ToEntero) End Sub Protected Sub CargaSubProyectoContrato(CveProyecto As Integer) DdlSubProyecto.Items.Clear() If CveProyecto > 0 Then DdlSubProyecto.DataSource = GlobalDTSubProyectos(CveProyecto, CveEmpresa:=UsuarioActual.CveEmpresa).FiltraParametros("", "", {"CveSubproyecto", "SubProyecto2"}) DdlSubProyecto.DataBind() DdlSubProyecto.Items.Insert(0, New ListItem("Seleccione un SubProyecto", "-1")) Else DdlSubProyecto.Items.Insert(0, New ListItem("Seleccione un SubProyecto", "-1")) End If End Sub Private Sub CargaClasificaciones() Dim ObjPres As New ClsContratos If ObjPres.GetPresupuestosDetalle(-1, -1, UsuarioActual.CveEmpresa) Then DtClasificaciones = ObjPres.DtResultados End If ObjPres.Dispose() GvPresupuestos.DataSource = DtClasificaciones GvPresupuestos.DataBind() End Sub Protected Sub GvPresupuestos_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GvPresupuestos.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim TipoGasto = e.Row.DBCampo("TipoGasto") Select Case e.Row.DBCampo("TipoTotal").ToEntero Case 1 e.Row.CssClass = "SubTotales" e.Row.FCLabel("LblImporteClasiTotal").Visible = True e.Row.FCLabel("LblImporteClasiTotal").CssClass = $"LabelGVTotal{TipoGasto}" e.Row.FCLabel("LblImporteClasiTotal").Text = String.Format("$ {0:C}", e.Row.DBCampo("Importe").FormatoNumero) e.Row.FCTextBox("TxtImporteClasi").Visible = False e.Row.FCTextBox("TxtObservacion").Visible = False Case 2 e.Row.CssClass = "Totales" e.Row.FCLabel("LblImporteClasiTotal").Visible = True e.Row.FCLabel("LblImporteClasiTotal").CssClass = "LabelGVTotal" e.Row.FCLabel("LblImporteClasiTotal").Text = String.Format("$ {0:C}", e.Row.DBCampo("Importe").FormatoNumero) e.Row.FCTextBox("TxtImporteClasi").Visible = False e.Row.FCTextBox("TxtObservacion").Visible = False Case 3 Dim Importe = e.Row.FCTextBox("TxtImporteClasi").Text.ToDouble e.Row.FCTextBox("TxtImporteClasi").Text = Importe.FormatoNumero() e.Row.FCTextBox("TxtImporteClasi").CssClass = $"form-control input-sm sumaGV txt{TipoGasto}" e.Row.FCTextBox("TxtImporteClasi").Attributes.Add("onKeyUp ", $"calculaTotal();calculaTotalTipoGasto('{TipoGasto}');") End Select End If If e.Row.RowType = DataControlRowType.Footer Then Dim Importe = DtClasificaciones.SUM("Importe", "CveClasificacion IS NOT NULL") e.Row.FCLabel("LblTotal").Text = String.Format("{0:C}", Importe) End If End Sub #End Region #Region "C A R G A - D A T O S" Private Sub CargaDatos() CargaMoneda() CargaProyecto() EstatusActual = -1 If CvePresupuesto > 0 Then Dim ObjContrato As New ClsContratos() If ObjContrato.GetPresupuestos(CvePresupuesto, "-1", -1, -1, -1, UsuarioActual.CveEmpresa) Then If ObjContrato.DtResultados.Rows.Count = 1 Then Dim DrContrato = ObjContrato.DtResultados.Copy(0) If DrContrato IsNot Nothing Then Dim ImporteOriginal = DrContrato("Importe").ToString.ToDecimal 'Dim ImporteVigente = DrContrato("ImporteVigente").ToString.ToDecimal 'Dim CveMoneda = DrContrato("CveMoneda").ToString.ToEntero 'DdlMoneda.SelectedValueExist(CveMoneda) 'HdnTotal.Value = ImporteVigente.FormatoNumero 'LblTotal.Text = String.Format("{0:C}", ImporteVigente) TxtPresupuesto.Text = DrContrato("Nombre").ToString TxtDescripcion.Text = DrContrato("Observacion").ToString EstatusActual = DrContrato("Estatus").ToString.ToEntero Dim CveProyecto = DrContrato("CveProyecto").ToString.ToEntero Dim CveSubProyecto = DrContrato("CveSubProyecto").ToString.ToEntero Dim EstatusTexto = DrContrato("EstatusTexto").ToString LtlEstatus.Text = EstatusTexto DdlProyecto.SelectedValueExist(CveProyecto) CargaSubProyectoContrato(CveProyecto) DdlSubProyecto.SelectedValueExist(CveSubProyecto) TxtFechaI.Text = DrContrato("FechaInicio").ToString.ToDate.ToString("dd/MM/yyyy") TxtFechaF.Text = DrContrato("FechaFin").ToString.ToDate.ToString("dd/MM/yyyy") DivIndicadores.Visible = True End If If ObjContrato.GetPresupuestosDetalle(CvePresupuesto, -1, UsuarioActual.CveEmpresa) Then DtClasificaciones = ObjContrato.DtResultados End If End If End If If ObjContrato.GetPresupuestosRep(CvePresupuesto, -1, 1, TxtFechaI.Text.ToDate, TxtFechaF.Text.ToDate, UsuarioActual.CveEmpresa, DdlProyecto.SelectedValue.ToEntero, DdlSubProyecto.SelectedValue.ToEntero, UsuarioActual.CveUsuario, -1) Then If ObjContrato.DtResultados.Rows.Count > 0 Then Dim DrRep = ObjContrato.DtResultados.Copy(0) Dim Importe = DrRep("Autorizado").ToString.ToDecimal.FormatoNumero Dim Ejercido = DrRep("Ejercido").ToString.ToDecimal.FormatoNumero Dim PorEjercer = DrRep("PorEjercer").ToString.ToDecimal.FormatoNumero Dim EjercidoPorcentaje = DrRep("EjercidoPorcentaje").ToString.ToEntero Dim PorEjercerPorcentaje = DrRep("PorEjercerPorcentaje").ToString.ToEntero LtlAutorizado.Text = Importe LtlEjercido.Text = Ejercido LtlPorEjercer.Text = PorEjercer PBEjercido.Style.Remove("width") PBEjercido.Style.Add("width", $"{EjercidoPorcentaje.ToString}%") PBPorEjercer.Style.Remove("width") PBPorEjercer.Style.Add("width", $"{PorEjercerPorcentaje.ToString}%") End If End If ObjContrato.Dispose() If DtClasificaciones IsNot Nothing Then GvPresupuestos.DataSource = DtClasificaciones GvPresupuestos.DataBind() End If DeshabilitaControles(EstatusActual) End If End Sub Private Sub DeshabilitaControles(EstatusActual As Integer) If EstatusActual = EstatusFinal Then 'DdlMoneda.Enabled = False GvPresupuestos.Enabled = False TxtPresupuesto.Enabled = False TxtDescripcion.Enabled = False DdlProyecto.Enabled = False DdlSubProyecto.Enabled = False TxtFechaI.Enabled = False TxtFechaF.Enabled = False CEFechaI.Enabled = False CEFecha.Enabled = False ImgFechaI.Visible = False ImgFechaF.Visible = False BtnGuardar.Enabled = False LnkAutorizar.Visible = False HlModificar.Visible = True HlModificar.NavigateUrl = $"/Contratos/ModificarPresupuesto.aspx?CvePresupuesto={CvePresupuesto}" End If End Sub #End Region #Region "G U A R D A R " Private Function ValidaDatos() As Boolean If String.IsNullOrEmpty(TxtPresupuesto.Text.Trim) Then Alert("Debe escirbir un nombre al presupuesto", TipoMensaje.MsgAdvertencia) Return False End If If String.IsNullOrEmpty(TxtDescripcion.Text.Trim) Then Alert("Debe escribir una observación.", TipoMensaje.MsgAdvertencia) Return False End If If String.IsNullOrEmpty(TxtFechaI.Text.Trim) Then Alert("Debe seleccionar un Fecha Inicio.", TipoMensaje.MsgAdvertencia) Return False End If If String.IsNullOrEmpty(TxtFechaF.Text.Trim) Then Alert("Debe seleccionar una Fecha Fin.", TipoMensaje.MsgAdvertencia) Return False End If If DdlSubProyecto.SelectedValue.ToEntero <= 0 Then Alert("Debe seleccionar un Sub Proyecto.", TipoMensaje.MsgAdvertencia) Return False End If Dim FechaI As Date = TxtFechaI.Text.ToDate Dim FechaF As Date = TxtFechaF.Text.ToDate If FechaF < FechaI Then Alert("La Fecha Fin no puede ser menor que la Fecha Inicio.", TipoMensaje.MsgAdvertencia) Return False End If 'If HdnTotal.Value.ToDouble() <= 0 Then ' Alert("El Total no puede ser igual o menor que 0.", TipoMensaje.MsgAdvertencia) ' Return False 'End If Return True End Function Protected Sub BtnGuardar_Click(sender As Object, e As EventArgs) Handles BtnGuardar.Click If ValidaDatos() Then If CvePresupuesto > 0 Then If ActualizarPresupuesto() Then Dim Liga = $"document.location.href=""/Contratos/CatPresupuestos.aspx""" Alert("El Presupuesto se actualizo correctamente.", TipoMensaje.MsgInformacion, Liga) Else Alert("Ocurrió un error al actualizar el presupuesto", TipoMensaje.MsgError) End If Else AgregarPresupuesto() End If End If UpAcciones.Update() UpDatosG.Update() UpGvClasificaciones.Update() UpIndicadores.Update() End Sub Private Function ActualizarPresupuesto() As Boolean Dim Retorno As Boolean = False Dim Nombre = TxtPresupuesto.Text.Trim Dim Observacion = TxtDescripcion.Text.Trim Dim CveProyecto = DdlProyecto.SelectedValue.ToEntero Dim FechaI = TxtFechaI.Text.ToDate Dim FechaF = TxtFechaF.Text.ToDate Dim FechaIng = Now Dim CveSubProyecto = DdlSubProyecto.SelectedValue.ToEntero Dim Archivo = "" Dim Estatus = 1 Dim TieneError = False Dim ObjCont As New ClsContratos ObjCont.IniciaTransacSQL() TieneError = Not ObjCont.UpdatePresupuesto(CvePresupuesto, Nombre, Observacion, CveSubProyecto, FechaI, FechaF, Estatus, Archivo, Now, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa) If Not TieneError Then For Each GvRow As GridViewRow In GvPresupuestos.Rows If GvRow.RowType = DataControlRowType.DataRow Then Dim CveClasificacion = GvRow.DataKey("CveClasificacion").ToEntero Dim CveTipoGasto = GvRow.DataKey("CveTipoGasto").ToEntero Dim CvePresupuestoDetalle = GvRow.DataKey("CvePresupuestoDetalle").ToEntero Dim Importe = GvRow.FCTextBox("TxtImporteClasi").Text.ToDecimal Dim Obs = GvRow.FCTextBox("TxtObservacion").Text.Trim 'ACTUALIZA EL REGISTRO EXISTENTE If CvePresupuestoDetalle > 0 Then TieneError = Not ObjCont.UpdatePresupuestosDetalle(CvePresupuestoDetalle, CveClasificacion, Importe, Obs) If TieneError Then Exit For End If 'AGREGA LA CLASIFICACION SI NO EXISTE EN EN EL DT DETALLE Else If Importe > 0 And CveClasificacion > 0 Then TieneError = Not ObjCont.AddPresupuestosDetalle(CvePresupuesto, CveClasificacion, CveTipoGasto, Importe, Obs) If TieneError Then Exit For End If End If End If End If Next If TieneError Then ObjCont.RollBack() Retorno = False Else ObjCont.Commit() Retorno = True End If End If ObjCont.Dispose() Return Retorno End Function Private Sub AgregarPresupuesto() Dim Nombre = TxtPresupuesto.Text.Trim Dim Observacion = TxtDescripcion.Text.Trim Dim CveProyecto = DdlProyecto.SelectedValue.ToEntero Dim FechaI = TxtFechaI.Text.ToDate Dim FechaF = TxtFechaF.Text.ToDate Dim FechaIng = Now Dim CveSubProyecto = DdlSubProyecto.SelectedValue.ToEntero Dim Archivo = "" Dim TieneError = False Dim Estatus = 1 Dim ObjCont As New ClsContratos ObjCont.IniciaTransacSQL() If ObjCont.GetContratosEstatus(UsuarioActual.CveEmpresa) Then Dim CvePresupuestoNuevo = ObjCont.AddPresupuesto(Nombre, Observacion, CveSubProyecto, FechaI, FechaF, Estatus, Archivo, Now, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa) If CvePresupuestoNuevo > 0 Then Dim ImporteClasi = 0.0D Dim CveClasificacion = -1 Dim ObsDetalle = "" For Each GvRow As GridViewRow In GvPresupuestos.Rows If GvRow.RowType = DataControlRowType.DataRow Then ImporteClasi = GvRow.FCTextBox("TxtImporteClasi").Text.ToDecimal CveClasificacion = GvRow.DataKey("CveClasificacion").ToEntero Dim CveTipoGasto = GvRow.DataKey("CveTipoGasto").ToEntero ObsDetalle = GvRow.FCTextBox("TxtObservacion").Text.Trim If CveClasificacion > 0 Then If ImporteClasi > 0 Then TieneError = Not ObjCont.AddPresupuestosDetalle(CvePresupuestoNuevo, CveClasificacion, CveTipoGasto, ImporteClasi, ObsDetalle) If TieneError Then Exit For End If End If End If End If Next Else TieneError = True End If If Not TieneError Then Me.CvePresupuesto = CvePresupuestoNuevo End If End If If TieneError Then ObjCont.RollBack() ObjCont.Dispose() Alert("Ocurrió un error al agregar el presupuesto.", TipoMensaje.MsgError) Else ObjCont.Commit() ObjCont.Dispose() Dim Liga = $"document.location.href=""/Contratos/CatPresupuestos.aspx""" Alert("Se agregó el presupuesto correctamente.", TipoMensaje.MsgInformacion, Liga) End If End Sub #End Region Protected Sub LnkAutorizar_Click(sender As Object, e As EventArgs) Handles LnkAutorizar.Click If ValidaDatos() Then If ActualizarPresupuesto() Then If EstatusActual <> EstatusFinal Then Dim ObjPres As New ClsContratos ObjPres.IniciaTransacSQL() Dim TieneError As Boolean = False TieneError = Not ObjPres.UpdateEstatusPresupuesto(CvePresupuesto, EstatusFinal) 'If ObjPres.UpdateEstatusPresupuesto(CvePresupuesto, EstatusFinal) Then ' If ObjPres.GetPresupuestosDetalle(CvePresupuesto, -1, UsuarioActual.CveEmpresa) Then ' DtClasificaciones = ObjPres.DtResultados ' End If ' If DtClasificaciones IsNot Nothing Then ' Dim DtDetalle = DtClasificaciones.FiltraParametros("CvePresupuestoDetalle > 0", DistinctColumns:={"CvePresupuestoDetalle", "ImporteVigente"}) ' If DtDetalle.Rows.Count > 0 Then ' For Each Dr As DataRow In DtDetalle.Rows ' Dim CvePresupuestoDetalle As Integer = Dr("CvePresupuestoDetalle").ToString.ToEntero ' Dim Importe As Decimal = Dr("ImporteVigente").ToString.ToDecimal ' Dim Fecha = Now ' Dim Tipo = 1 ' Dim CveCompra As Integer = -1 ' Dim Justificacion = "-1" ' TieneError = Not ObjPres.AddPresupuestoMovimiento(CvePresupuesto, CveCompra, Tipo, Importe, Fecha, Justificacion) ' If TieneError Then ' Exit For ' End If ' Next ' End If ' End If 'Else ' TieneError = True 'End If If TieneError Then ObjPres.RollBack() Alert("Ocurrió un error al autorizar el presupuesto.", TipoMensaje.MsgError) Else ObjPres.Commit() Alert("Se autorizó el presupuesto correctamente.") CargaDatos() DeshabilitaControles(EstatusFinal) End If ObjPres.Dispose() Else Alert("El presupuesto ya esta autorizado", TipoMensaje.MsgError) End If Else Alert("Ocurrió un error al autorizar el presupuesto. No se pudieron guardar los datos", TipoMensaje.MsgError) End If Else Alert("Faltan campos por corregir", TipoMensaje.MsgAdvertencia) End If UpAcciones.Update() UpDatosG.Update() UpGvClasificaciones.Update() UpIndicadores.Update() End Sub Protected Sub LnkCargaPresupuesto_Click(sender As Object, e As EventArgs) Handles LnkCargaPresupuesto.Click RegistraScript("$('#ModalCargaPresupuesto').modal();") End Sub Protected Sub LnkFormatoPresup_Click(sender As Object, e As EventArgs) Handles LnkFormatoPresup.Click GvPresupuestos.ExportToExcelX("Plantilla Presupuesto " + DateTime.Now.ToShortDateString) End Sub Protected Sub LnkCargarPresup_Click(sender As Object, e As EventArgs) Handles LnkCargarPresup.Click If TxtCargarPresup.getfiles().Archivos.Count > 0 Then Dim file As Archivo = TxtCargarPresup.getfiles().Archivos(0) Dim Directorio As String = Server.MapPath("~") & file.path Dim Tipo As String = If(file.Extension.Contiene("xlsx", "xlsx", "xlsm", "xlsb"), "EXCEL", "CSV") CargarArchivo(Directorio, file.filename, Tipo) Else Alert("No existe el archivo. Favor de subir un archivo CSV o Excel a la plataforma.", TipoMensaje.MsgError) End If End Sub Private Sub CargarArchivo(Ruta As String, FileName As String, Tipo As String) Dim DTExcel As New DataTable Dim ErrorArchivo = False Dim ErrorExcel = False DTExcel = ReadExcelFile(Ruta + FileName, True) If Not ErrorArchivo Then If Not DTExcel.Columns(0).ColumnName = "Clasificación" Then Alert("El nombre de la primera columna debe ser 'Clasificación'. Favor de corregirlo", TipoMensaje.MsgError) Exit Sub End If If Not DTExcel.Columns(1).ColumnName = "Importe" Then Alert("El nombre de la segunda columna debe ser 'Importe'.Favor de corregirlo", TipoMensaje.MsgError) Exit Sub End If If Not DTExcel.Columns(2).ColumnName = "Observaciones" Then Alert("El nombre de la tercera columna debe ser 'Observaciones'.Favor de corregirlo", TipoMensaje.MsgError) Exit Sub End If If Not DTExcel.Columns(3).ColumnName = "Tipo Gasto" Then Alert("El nombre de la cuarta columna debe ser 'Tipo Gasto'. Favor de corregirlo", TipoMensaje.MsgError) Exit Sub End If End If Try Dim txtImporte As TextBox = Nothing Dim txtObservacion As TextBox = Nothing Dim i As Integer = 0 For Each row As GridViewRow In GvPresupuestos.Rows If (i < DTExcel.Rows.Count) Then txtImporte = row.FCTextBox("TxtImporteClasi") txtObservacion = row.FCTextBox("TxtObservacion") txtImporte.Text = DTExcel.Rows(i).Field(Of String)(1).ToString txtObservacion.Text = DTExcel.Rows(i).Field(Of String)(2).ToString i += 1 End If Next Catch ex As Exception GuardaLog($"Ocurrio un error al cargar archivo Excel en Contratos/Presupuestos.aspx - {ex.ToString()}") Alert("Se produjo un error al ingresar los datos. Favor de contactar a soporte.", TipoMensaje.MsgError) Exit Sub End Try RegistraScript("$('#ModalCargaPresupuesto').modal('hide');calculaTotal()") UpGvClasificaciones.Update() End Sub End Class