Option Strict On Option Explicit On Imports System.Data Imports System.Linq Imports Extensiones Imports Microsoft.Reporting.WebForms Partial Class AC_CatPresupuestoEgresos Inherits ClsPaginas #Region " PROPIEDADES " Private Property DtPresupuestoEgresos As DataTable Get Return CType(ViewState("DtPresupuestoEgresos"), DataTable) End Get Set(value As DataTable) ViewState("DtPresupuestoEgresos") = value End Set End Property Public Property pTotal As String Get Return CType(ViewState("pTotal"), String) End Get Set(value As String) ViewState("pTotal") = value End Set End Property #End Region Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not Page.IsPostBack Then SetValues() CargaDatos() End If End Sub #Region " VALORES INICIALES " Private Sub SetValues() CargaEjercicios() End Sub Private Sub CargaEjercicios() If UsuarioActual.Empresa.UsaAC Then Dim ClsConta As New ClsContPAQi(UsuarioActual.Empresa) If ClsConta.GetEjercicios() Then DdlEjercicio.DataSource = ClsConta.DtResultados Else DdlEjercicio.DataSource = Nothing End If ClsConta.Dispose() DdlEjercicio.DataBind() Else Dim ClsConta As New ClsContabilidad If ClsConta.GetEjercicios(UsuarioActual.CveEmpresa) Then DdlEjercicio.DataSource = ClsConta.DtResultados Else DdlEjercicio.DataSource = Nothing End If ClsConta.Dispose() DdlEjercicio.DataBind() End If DdlEjercicio.SelectedValueExist(UsuarioActual.Empresa.EjercicioActual.ToString) End Sub #End Region #Region " CARGA DATOS " Protected Sub BtnConsultar_Click(sender As Object, e As EventArgs) BtnCongelarPrespuesto.Visible = False BtnPresupuesto.Visible = False CargaDatos() End Sub Private Sub CargaDatos() Dim AC As New ClsAC Dim Ejercicio As Integer = DdlEjercicio.SelectedValue.ToString.ToEntero Dim EsProyecto As Boolean = CBool(DdlProyecto.SelectedValue.ToString) LblEmpresa.Text = UsuarioActual.Empresa.Empresa LblPresupAnio.Text = String.Format("Presupuesto {0}", DdlEjercicio.SelectedValue) If AC.GetPresupuestoEgresos(-1, EsProyecto, Ejercicio, UsuarioActual.CveEmpresa) Then DtPresupuestoEgresos = AC.DtResultados Gv.DataSource = AC.DtResultados Gv.DataBind() 'debe de haber datos para habilitar los botones If DtPresupuestoEgresos.Rows.Count > 0 Then If EsProyecto Then If DtPresupuestoEgresos.Rows(0)("Bloqueado").ToString = "0" Then 'Ejercicio = UsuarioActual.Empresa.EjercicioActual AndAlso BtnCongelarPrespuesto.Visible = True End If Else 'si es anteproyecto If DtPresupuestoEgresos.Rows(0)("Bloqueado").ToString = "0" Then 'AndAlso Ejercicio >= UsuarioActual.Empresa.EjercicioActual BtnPresupuesto.Visible = True End If ' If Ejercicio = UsuarioActual.Empresa.EjercicioActual Then End If Dim Total As Double = Gv.Rows.Cast(Of GridViewRow)().Sum(Function(gvr) gvr.DataKey("Total").ToDouble) LbTotales.Text = Total.ToString.FormatoNumero Else LbTotales.Text = "0.00" pTotal = "0.00" UpTotales.Visible = False End If End If UpTotales.Update() UPDatos.Update() AC.Dispose() End Sub Private Sub CargaGrid(Orden As String) If Orden <> String.Empty Then Dim DV As New DataView(CType(ViewState("DT_AC_PresupuestosEgresos"), DataTable), "", Orden, DataViewRowState.CurrentRows) Gv.DataSource = DV.ToTable() DV.Dispose() Else Gv.DataSource = CType(ViewState("DT_AC_PresupuestosEgresos"), DataTable) End If Gv.DataBind() UPDatos.Update() End Sub #End Region #Region " FUNCIONES " Private Function FNCIDCuenta(dt As DataTable, codigo As String) As Integer Dim Retorno As Integer = 0 Dim dr As DataRow dr = dt.FiltroPrimero(String.Format("Codigo ='{0}'", codigo)) If dr IsNot Nothing Then Retorno = dr("Id").ToString.ToEntero End If Return Retorno End Function Private Function FNCAddCuenta(dt As DataTable, id As Integer, codigo As String) As Integer ' se crea una coleccion para grabar las claves y las cuentas que se van insertando, por si se comparten, no marque error de que ya esta registrada y esta en la transaccion todavia Dim Retorno As Integer = 0 Dim dr As DataRow = dt.NewRow dr("Codigo") = codigo dr("Id") = id dt.Rows.Add(dr) dt.AcceptChanges() Return Retorno End Function Private Function FnCuentaCargo(CC As String) As String Return CC.Replace("-0", "").Replace("-1", "").Replace("-2", "").Replace("-3", "").Replace("-4", "").Replace("-5", "").Replace("-6", "").Replace("-7", "").Replace("-8", "").Replace("-9", "") End Function #End Region Protected Sub BtnCongelarPrespuesto_Click(sender As Object, e As EventArgs) If UsuarioActual.Empresa.UsaAC Then Dim Conta As New ClsContPAQi(UsuarioActual.Empresa) Conta.IniciaTransacSQL() Dim AC As New ClsAC AC.IniciaTransacSQL() 'registro las cuentas prespuestales por cada movimiento, si existe la ignoro Dim dtCuentas As DataTable = Nothing Dim IDPoliza As Integer Dim IdCuentaAprobado, IdCuentaPorEjercer, IdCuentaModificaciones, IdCuentaComprometido, IdCuentaDevengado, IdCuentaEjercido, IdCuentaPagado, IdCuentaMatriz, IdCuentaMatrizAbono As Integer Dim Codigo As String Dim TieneError As Boolean = False Dim MensajeError = "" Dim IdCuentaAprobadoPadre, IdCuentaPorEjercerPadre, IdCuentaModificacionesPadre, IdCuentaComprometidoPadre, IdCuentaDevengadoPadre, IdCuentaEjercidoPadre, IdCuentaPagadoPadre, IdCuentaMatrizPadre As Integer 'hay que registrar una poliza de cargo en 821, aprobado contra Ley de egresos, luego registrar o de por ejercer If Conta.GetCuentas(-1, "-1", -1) Then dtCuentas = Conta.DtResultados End If Dim drPadre As DataRow drPadre = dtCuentas.FiltroPrimero("Nombre = 'PRESUPUESTO DE EGRESOS APROBADO'") IdCuentaAprobadoPadre = If(drPadre IsNot Nothing, drPadre("Id").ToString.ToEntero, 0) drPadre = dtCuentas.FiltroPrimero("Nombre = 'PRESUPUESTO DE EGRESOS POR EJERCER'") IdCuentaPorEjercerPadre = If(drPadre IsNot Nothing, drPadre("Id").ToString.ToEntero, 0) drPadre = dtCuentas.FiltroPrimero("Nombre = 'MODIFICACIONES AL PRESUPUESTO DE EGRESOS APROBADO'") IdCuentaModificacionesPadre = If(drPadre IsNot Nothing, drPadre("Id").ToString.ToEntero, 0) drPadre = dtCuentas.FiltroPrimero("Nombre = 'PRESUPUESTO DE EGRESOS COMPROMETIDO'") IdCuentaComprometidoPadre = If(drPadre IsNot Nothing, drPadre("Id").ToString.ToEntero, 0) drPadre = dtCuentas.FiltroPrimero("Nombre = 'PRESUPUESTO DE EGRESOS DEVENGADO'") IdCuentaDevengadoPadre = If(drPadre IsNot Nothing, drPadre("Id").ToString.ToEntero, 0) drPadre = dtCuentas.FiltroPrimero("Nombre = 'PRESUPUESTO DE EGRESOS EJERCIDO'") IdCuentaEjercidoPadre = If(drPadre IsNot Nothing, drPadre("Id").ToString.ToEntero, 0) drPadre = dtCuentas.FiltroPrimero("Nombre = 'PRESUPUESTO DE EGRESOS PAGADO'") IdCuentaPagadoPadre = If(drPadre IsNot Nothing, drPadre("Id").ToString.ToEntero, 0) Dim Total As Double = Gv.Rows.Cast(Of GridViewRow)().Sum(Function(gvr) gvr.DataKey("Total").ToDouble) Dim Fecha As Date = String.Format("01/01/{0}", DdlEjercicio.SelectedValue).ToDate IDPoliza = Conta.AddPoliza(4, String.Format("POLIZA INICIAL - PRESUPUESTO DE EGRESOS APROBADO {0}", DdlEjercicio.SelectedValue), Total, Total, UsuarioActual.CveUsuario, Fecha) If IDPoliza > 0 Then For Each gvr As GridViewRow In Gv.Rows Codigo = String.Format("{0}{1}{2}{3}{4}", gvr.DataKey("ClaveProgramaOperativo").PadLeft(2, "0"c), gvr.DataKey("CodigoUnidadAdministrativa").PadLeft(2, "0"c), gvr.DataKey("CodigoFuenteFinanciamiento").PadLeft(2, "0"c), gvr.DataKey("CveTipoGasto").PadLeft(2, "0"c), gvr.DataKey("CodigoCOG")).PadLeft(5, "0"c) 'Registro las cuentas afectables IdCuentaAprobado = FNCIDCuenta(dtCuentas, String.Format("00821{0}", Codigo)) If IdCuentaAprobado = 0 Then IdCuentaAprobado = Conta.AddCuenta(String.Format("00821{0}", Codigo), gvr.DataKey("Clasificacion").ToUpper, "L", 2, 0, 1, 0, "", 1, IdCuentaAprobadoPadre) : FNCAddCuenta(dtCuentas, IdCuentaAprobado, String.Format("00821{0}", Codigo)) IdCuentaPorEjercer = FNCIDCuenta(dtCuentas, String.Format("00822{0}", Codigo)) If IdCuentaPorEjercer = 0 Then IdCuentaPorEjercer = Conta.AddCuenta(String.Format("00822{0}", Codigo), gvr.DataKey("Clasificacion").ToUpper, "L", 2, 0, 1, 0, "", 1, IdCuentaPorEjercerPadre) : FNCAddCuenta(dtCuentas, IdCuentaPorEjercer, String.Format("00822{0}", Codigo)) IdCuentaModificaciones = FNCIDCuenta(dtCuentas, String.Format("00823{0}", Codigo)) If IdCuentaModificaciones = 0 Then IdCuentaModificaciones = Conta.AddCuenta(String.Format("00823{0}", Codigo), gvr.DataKey("Clasificacion").ToUpper, "L", 2, 0, 1, 0, "", 1, IdCuentaModificacionesPadre) : FNCAddCuenta(dtCuentas, IdCuentaModificaciones, String.Format("00823{0}", Codigo)) IdCuentaComprometido = FNCIDCuenta(dtCuentas, String.Format("00824{0}", Codigo)) If IdCuentaComprometido = 0 Then IdCuentaComprometido = Conta.AddCuenta(String.Format("00824{0}", Codigo), gvr.DataKey("Clasificacion").ToUpper, "L", 2, 0, 1, 0, "", 1, IdCuentaComprometidoPadre) : FNCAddCuenta(dtCuentas, IdCuentaComprometido, String.Format("00824{0}", Codigo)) IdCuentaDevengado = FNCIDCuenta(dtCuentas, String.Format("00825{0}", Codigo)) If IdCuentaDevengado = 0 Then IdCuentaDevengado = Conta.AddCuenta(String.Format("00825{0}", Codigo), gvr.DataKey("Clasificacion").ToUpper, "L", 2, 0, 1, 0, "", 1, IdCuentaDevengadoPadre) : FNCAddCuenta(dtCuentas, IdCuentaDevengado, String.Format("00825{0}", Codigo)) IdCuentaEjercido = FNCIDCuenta(dtCuentas, String.Format("00826{0}", Codigo)) If IdCuentaEjercido = 0 Then IdCuentaEjercido = Conta.AddCuenta(String.Format("00826{0}", Codigo), gvr.DataKey("Clasificacion").ToUpper, "L", 2, 0, 1, 0, "", 1, IdCuentaEjercidoPadre) : FNCAddCuenta(dtCuentas, IdCuentaEjercido, String.Format("00826{0}", Codigo)) IdCuentaPagado = FNCIDCuenta(dtCuentas, String.Format("00827{0}", Codigo)) If IdCuentaPagado = 0 Then IdCuentaPagado = Conta.AddCuenta(String.Format("00827{0}", Codigo), gvr.DataKey("Clasificacion").ToUpper, "L", 2, 0, 1, 0, "", 1, IdCuentaPagadoPadre) : FNCAddCuenta(dtCuentas, IdCuentaPagado, String.Format("00827{0}", Codigo)) Dim filtro As New StringBuilder filtro.AppendFormat("Partida = {0} and TipoGasto = {1}", gvr.DataKey("CodigoCOG").ToString.Substring(0, 4), gvr.DataKey("CveTipoGasto").ToString) Dim RegistroMatriz As DataRow = GlobalDTAC_MatrizEgresos.FiltroPrimero(filtro.ToString) If RegistroMatriz Is Nothing Then Dim CCOG As String = gvr.DataKey("CodigoCOG").ToString.Substring(0, 3) If CCOG = "161" Then CCOG = "159" ' ajuste manual, porque no hay conversion de estas cuentas RegistroMatriz = GlobalDTAC_MatrizEgresos.FiltroPrimero(String.Format("Partida = {0}0 and TipoGasto = {1}", CCOG, gvr.DataKey("CveTipoGasto").ToString)) End If If RegistroMatriz IsNot Nothing AndAlso FnCuentaCargo(RegistroMatriz("CuentaCargo").ToString).ToEntero > 0 Then IdCuentaMatrizPadre = FnCuentaCargo(RegistroMatriz("CuentaCargo").ToString).ToEntero Dim CuentaCargo As Integer = dtCuentas.FiltroPrimeroValor("Id", String.Format("Codigo = '{0}'", String.Format("0{0}0000000000000", IdCuentaMatrizPadre))).ToEntero If CuentaCargo > 0 Then IdCuentaMatriz = FNCIDCuenta(dtCuentas, String.Format("0{0}{1}", IdCuentaMatrizPadre, Codigo)) If IdCuentaMatriz = 0 Then IdCuentaMatriz = Conta.AddCuenta(String.Format("0{0}{1}", IdCuentaMatrizPadre, Codigo), gvr.DataKey("Clasificacion").ToUpper, "G", 2, 0, 1, 0, "", 1, CuentaCargo) : FNCAddCuenta(dtCuentas, IdCuentaMatriz, String.Format("0{0}{1}", IdCuentaMatrizPadre, Codigo)) IdCuentaMatrizAbono = 0 'si el COG, es de la 1000, se le ingresa la cuenta abono If gvr.DataKey("CodigoCOG").ToString.Substring(0, 1) = "1" Then IdCuentaMatrizPadre = FnCuentaCargo(RegistroMatriz("CuentaAbono").ToString.Substring(0, 4)).ToEntero Dim CuentaAbono As Integer = dtCuentas.FiltroPrimero(String.Format("Codigo = '{0}'", String.Format("0{0}0000000000000", IdCuentaMatrizPadre)))("Id").ToString.ToEntero IdCuentaMatrizAbono = FNCIDCuenta(dtCuentas, String.Format("0{0}{1}", IdCuentaMatrizPadre, Codigo)) If IdCuentaMatrizAbono = 0 Then IdCuentaMatrizAbono = Conta.AddCuenta(String.Format("0{0}{1}", IdCuentaMatrizPadre, Codigo), gvr.DataKey("Clasificacion").ToUpper, "G", 2, 0, 1, 0, "", 1, CuentaAbono) : FNCAddCuenta(dtCuentas, IdCuentaMatriz, String.Format("0{0}{1}", IdCuentaMatrizPadre, Codigo)) End If Total = gvr.DataKey("Total").ToDouble If Total > 0 Then If Conta.AddMovimiento(IDPoliza, IdCuentaAprobado, True, Total, "", "", 0, Fecha, "") Then 'actualizo el idcuenta del presupuesto If Not Conta.AddMovimiento(IDPoliza, IdCuentaPorEjercer, False, Total, "", "", 0, Fecha, "") Then MensajeError = "Ocurrió un error al generar los movimientos contables de Ejercido" TieneError = True Exit For End If Else MensajeError = "Ocurrió un error al generar los movimientos contables de Aprobado" TieneError = True Exit For End If End If If Not AC.UpdateCuentasPresupuestoEgresos(IdCuentaAprobado, IdCuentaPorEjercer, IdCuentaModificaciones, IdCuentaComprometido, IdCuentaDevengado, IdCuentaEjercido, IdCuentaPagado, IdCuentaMatriz, IdCuentaMatrizAbono, gvr.DataKey("CvePresupuestoEgreso").ToEntero) Then MensajeError = "Ocurrió un error al actualizar el presupuesto de egreso" TieneError = True Exit For End If Else MensajeError = $"Ocurrió un error al buscar la cuenta cargo del presupuesto de egreso.

Cuenta: {String.Format("0{0}0000000000000", IdCuentaMatrizPadre)}" TieneError = True Exit For End If Else MensajeError = "Ocurrió un error al buscar la cuenta cargo matriz del presupuesto de egreso" TieneError = True Exit For End If Next Else MensajeError = "Ocurrió un error al generar la poliza inicial contable" TieneError = True End If If Not TieneError Then If Not AC.UpdateEjercicios(DdlEjercicio.SelectedValue.ToEntero, -1, IDPoliza, UsuarioActual.CveEmpresa) Then TieneError = True End If End If If Not TieneError Then 'actualizo las cuentas de cadapresupuesto AC.Commit() Conta.Commit() Alert("El Presupuesto de Egresos se ha aprobado correctamente", TipoMensaje.MsgInformacion) BtnCongelarPrespuesto.Visible = False Else Conta.RollBack() AC.RollBack() Alert($"Ocurrió un error al aprobar el Presupuesto de Egresos. Por favor vuelva a intentar.

{MensajeError}", TipoMensaje.MsgError) End If Conta.Dispose() AC.Dispose() Else Dim Conta As New ClsContabilidad Dim TieneError As Boolean = False If Not Conta.GetGeneraPolizaPresupuestoEgresos(UsuarioActual.CveEmpresa, DdlEjercicio.SelectedValue.ToEntero) Then TieneError = True End If If TieneError Then Alert("Ocurrió un error al aprobar el Presupuesto de Egresos. Por favor vuelva a intentar.", TipoMensaje.MsgError) Else Alert("El Presupuesto de Egresos se ha aprobado correctamente", TipoMensaje.MsgInformacion) BtnCongelarPrespuesto.Visible = False End If End If End Sub Protected Sub BtnPresupuesto_Click(sender As Object, e As EventArgs) Dim AC As New ClsAC AC.IniciaTransacSQL() Dim TieneError As Boolean = False For Each gvr As GridViewRow In Gv.Rows Dim Total As Double = gvr.DataKey("Total").ToDouble Dim Enero As Double = gvr.DataKey("Enero").ToDouble Dim Febrero As Double = gvr.DataKey("Febrero").ToDouble Dim Marzo As Double = gvr.DataKey("Marzo").ToDouble Dim Abril As Double = gvr.DataKey("Abril").ToDouble Dim Mayo As Double = gvr.DataKey("Mayo").ToDouble Dim Junio As Double = gvr.DataKey("Junio").ToDouble Dim Julio As Double = gvr.DataKey("Julio").ToDouble Dim Agosto As Double = gvr.DataKey("Agosto").ToDouble Dim Septiembre As Double = gvr.DataKey("Septiembre").ToDouble Dim Octubre As Double = gvr.DataKey("Octubre").ToDouble Dim Noviembre As Double = gvr.DataKey("Noviembre").ToDouble Dim Diciembre As Double = gvr.DataKey("Diciembre").ToDouble Dim Ejercicio As Integer = 2015 Dim CveProgramaOperativo As Integer = gvr.DataKey("CveProgramaOperativo").ToEntero Dim CveFuenteFinanciamiento As Integer = gvr.DataKey("CveFuenteFinanciamiento").ToEntero Dim CveTipoGasto As Integer = gvr.DataKey("CveTipoGasto").ToEntero Dim CveUnidadAdministrativa As Integer = gvr.DataKey("CveUnidadAdministrativa").ToEntero Dim CveClasificacionOG As Integer = gvr.DataKey("CveClasificacion").ToEntero Dim CvePresupuestoEgresos As Integer = gvr.DataKey("CvePresupuestoEgreso").ToEntero If Not AC.ProyectoPresupuestoEgresos(Ejercicio, CveProgramaOperativo, CveFuenteFinanciamiento, CveTipoGasto, CveUnidadAdministrativa, CveClasificacionOG, Total, Enero, Febrero, Marzo, Abril, Mayo, Junio, Julio, Agosto, Septiembre, Octubre, Noviembre, Diciembre, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa, CvePresupuestoEgresos) Then TieneError = True Exit For End If Next If Not TieneError Then AC.Commit() Alert("Movimientos Guardados", TipoMensaje.MsgInformacion) BtnPresupuesto.Visible = False Else AC.RollBack() End If AC.Dispose() End Sub #Region " IMPRIMIR EXCEL " Protected Sub LBExcel_Click(sender As Object, e As EventArgs) ImprimirReporte("Excel") End Sub Protected Sub LBPdf_Click(sender As Object, e As EventArgs) ImprimirReporte() End Sub Private Sub ImprimirReporte(Optional Formato As String = "PDF") 'parametros a enviar al reporte Dim params As New List(Of ReportParameter) params.Add(New ReportParameter("pEmp", UsuarioActual.Empresa.Empresa)) params.Add(New ReportParameter("pImg", New Uri(Server.MapPath(String.Format("~/Empresas/{0}/Logo.png", UsuarioActual.CveEmpresa))).AbsoluteUri)) params.Add(New ReportParameter("pEjercicio", DdlEjercicio.SelectedValue.ToString)) params.Add(New ReportParameter("pTotal", pTotal)) ImprimirPDF("AC/Reportes/RepCatPresupuestoEgresos.rdlc", DtPresupuestoEgresos, params, "PresupuestoEgresos", Formato) End Sub #End Region End Class