Option Explicit On Option Strict On Imports System.Data Imports Extensiones Imports Microsoft.Reporting.WebForms Partial Class Compras_RepNotasCredito Inherits ClsPaginas #Region "PROPIEDADES" Private Property DtDatos As DataTable Get DtDatos = CType(ViewState("DtDatos"), DataTable) End Get Set(value As DataTable) ViewState("DtDatos") = value End Set End Property #End Region Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then DdlProveedores.DataSource = GlobalDTCompProveedores(UsuarioActual.CveEmpresa) DdlProveedores.DataBind() TxtFechaSaldar.Text = Now.ToShortDateString CargaDatos() End If End Sub Private Sub CargaDatos() Dim ObjCompras As New ClsCompras Dim FechaI, FechaF As String If txtFechaI.Text <> "" Then FechaI = txtFechaI.Text Else FechaI = "-1" If txtFechaF.Text <> "" Then FechaF = txtFechaF.Text Else FechaF = "-1" If ObjCompras.GetNotasCreditoRep(DdlProveedores.SelectedValue.ToEntero, DdlOpcion.SelectedValue.ToEntero, FechaI, FechaF, UsuarioActual.CveEmpresa) Then DtDatos = ObjCompras.DtResultados CargaGrid("") Else Alert("Ocurrió un error al obtener el catálogo de Notas de Crédito", Extensiones.TipoMensaje.MsgError) End If ObjCompras.Dispose() End Sub Protected Sub GvDatos_RowDataBound(sender As Object, e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then e.Row.FCHyperLink("HLRazonSocial").NavigateUrl = String.Format("/Compras/Proveedores.aspx?CveProveedor={0}", e.Row.DBCampo("CveProveedor")) e.Row.FCHyperLink("HLSerieFolio").NavigateUrl = String.Format("/Compras/Compras.aspx?CveCompra={0}", e.Row.DBCampo("CveCompra")) End If End Sub Protected Sub BtnConsultar_Click(sender As Object, e As EventArgs) CargaDatos() End Sub Protected Sub ImgExcel_Click(sender As Object, e As EventArgs) Handles ImgExcel.Click GvDatos.ExportToExcel("RepNotasCredito." & Now.ToShortDateString) End Sub Private Sub GenerarPDF(Optional Formato As String = "PDF") If DtDatos IsNot Nothing Then Dim StrFiltro As New StringBuilder 'StrFiltro.AppendFormat(String.Format("Proveedor: {0}, Estatus: {1}", DdlProveedores.SelectedItem.Text, DdlOpcion.SelectedItem.Text)) ' If DdlTipoComprobante.SelectedValue <> "-1" Then StrFiltro.AppendFormat(" Tipo Documento: {0}", DdlTipoComprobante.SelectedItem.Text) If DdlProveedores.SelectedValue <> "-1" Then StrFiltro.AppendFormat(" Proveedor: {0}", DdlProveedores.SelectedItem.Text) If DdlOpcion.SelectedValue <> "-1" Then StrFiltro.AppendFormat(" Estatus: {0}", DdlOpcion.SelectedItem.Text) If txtFechaI.Text <> "" Then StrFiltro.AppendFormat(" Fecha Inicio : {0}", txtFechaI.Text.ToDate.ToShortDateString) If txtFechaF.Text <> "" Then StrFiltro.AppendFormat(" Fecha Fin : {0}", txtFechaF.Text.ToDate.ToShortDateString) Dim Imagen As New System.IO.FileInfo(String.Format("{0}/Logo.png", UsuarioActual.Empresa.Ruta)) 'Dim dt As DataTable = DtVentas.FiltraParametros("", "Orden") '.FiltraParametros("Total > 0", DistinctColumns:={"GPO", "Cantidad", "SubTotal", "Descuento", "Amortizacion", "IVA", "Total"}) Dim FechaI, FechaF As String FechaI = If(txtFechaI.Text = "" Or txtFechaI.Text = "__/__/____", "-1", txtFechaI.Text) FechaF = If(txtFechaF.Text = "" Or txtFechaF.Text = "__/__/____", "-1", txtFechaF.Text) Dim params As New List(Of ReportParameter) params.Add(New ReportParameter("NombreEmpresa", UsuarioActual.Empresa.Empresa)) params.Add(New ReportParameter("FechaInicio", FechaI)) params.Add(New ReportParameter("FechaFin", FechaF)) params.Add(New ReportParameter("UsuarioImprimio", UsuarioActual.Nombre)) params.Add(New ReportParameter("FechaImpresion", Now.ToDateTime.ToShortDateString)) params.Add(New ReportParameter("Filtros", StrFiltro.ToString)) If Imagen.Exists Then params.Add(New ReportParameter("LogoPath", $"file://{Imagen.FullName}")) Else params.Add(New ReportParameter("LogoPath", "")) End If ImprimirPDF("Empresas/Grales/Formatos/RepNotasCredito.rdlc", DtDatos, params, String.Format("ReporteVentas-{0}", Now.ToDateTime.ToShortDateString), Formato:=Formato) Else Alert("No hay datos consultados", TipoMensaje.MsgAdvertencia) End If End Sub Protected Sub ImgImprimir_Click(sender As Object, e As EventArgs) Handles ImgImprimir.Click GenerarPDF() End Sub Private Sub LnkHabilitarSaldar_Click(sender As Object, e As EventArgs) Handles LnkHabilitarSaldar.Click For Each gr As GridViewRow In GvDatos.Rows If gr.DataKey("CveCompra") <> "" Then gr.FCCheckBox("Chk").Visible = True HLSaldar.Visible = True LnkHabilitarSaldar.Visible = False UpDatos.Update() End If Next End Sub Protected Sub BtnSaldar_Click(sender As Object, e As EventArgs) Dim ObjConta As New ClsContabilidad ObjConta.IniciaTransacSQL() Dim CvePrePoliza As Integer = 0 Dim MsgError As String = "" Dim IdCtaContable As Integer = 0 Dim IdCtaContableComplementaria As Integer = 0 Dim Importe As Decimal = 0 Dim ImporteMN As Decimal = 0 Dim ImporteComplementaria As Decimal = 0 Dim CveMoneda As Integer = 0 Dim TC As Decimal = GlobalTipoCambio(TxtFechaSaldar.Text.ToDate).ToDecimal Dim TCCompra As Decimal = 0 Dim Concepto As String = TxtConcepto.Text Dim UsaComplementaria As Boolean = UsuarioActual.CveEmpresa.Contiene(119) Dim MensajeAlert = New StringBuilder For Each gr As GridViewRow In GvDatos.Rows If gr.FCCheckBox("Chk").Checked Then IdCtaContable = 0 IdCtaContableComplementaria = 0 Importe = gr.DataKey("Saldo").ToDecimal.RoundSQL ImporteMN = gr.DataKey("Saldo").ToDecimal.RoundSQL TCCompra = gr.DataKey("TipoCambio").ToDecimal CveMoneda = gr.DataKey("CveMoneda").ToEntero CvePrePoliza = ObjConta.AddPrePolizas("Compras/RepNotasCredito", 103, gr.DataKey("Saldo").ToDecimal, gr.DataKey("Saldo").ToDecimal, gr.DataKey("CveMoneda").ToEntero, TxtFechaSaldar.Text.ToDate, "Saldado manual", UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa) If CveMoneda > 1 Then IdCtaContable = gr.DataKey("IdCtaContExtranjera").ToEntero ImporteMN = (gr.DataKey("Saldo").ToDecimal * TCCompra).RoundSQL Else IdCtaContable = gr.DataKey("IdCtaContProveedores").ToEntero End If If Not ObjConta.AddPrePolizasDetalle(CvePrePoliza, False, -Importe, "", 18, Concepto, TxtFechaSaldar.Text.ToDate, IdCtaContable, -1, gr.DataKey("CveIE").ToEntero, CveProveedor:=gr.DataKey("CveProveedor").ToEntero, CvePersonal:=gr.DataKey("CvePersonal").ToEntero, ImporteMX:=-If(CveMoneda > 1 AndAlso UsaComplementaria, Importe, ImporteMN), ImporteME:=-If(CveMoneda > 1, Importe, 0), CveCompra:=gr.DataKey("CveCompra").ToEntero, CveCompraAmortizacion:=gr.DataKey("CveCompra").ToEntero, TipoCambio:=TCCompra) Then MsgError = "Ocurrio un Error al saldar el movimiento" End If If UsaComplementaria AndAlso CveMoneda > 1 Then ImporteComplementaria = (Importe * TCCompra - Importe).RoundSQL IdCtaContableComplementaria = gr.DataKey("IdCtaContComplementaria").ToEntero If Not ObjConta.AddPrePolizasDetalle(CvePrePoliza, False, -ImporteComplementaria, "", 235, Concepto, TxtFechaSaldar.Text.ToDate, IdCtaContableComplementaria, -1, CveCompra:=gr.DataKey("CveCompra").ToEntero, CveCompraAmortizacion:=gr.DataKey("CveCompra").ToEntero, CvePersonal:=gr.DataKey("CvePersonal").ToEntero, CveProveedor:=gr.DataKey("CveProveedor").ToEntero, ImporteMX:=-ImporteComplementaria, ImporteME:=-Importe, TipoCambioPago:=TC, TipoCambio:=TCCompra) Then MsgError = "Ocurrio un Error al saldar el movimiento" End If End If IdCtaContable = UsuarioActual.Empresa.Funcionalidad(266).ToString.ToEntero If Not ObjConta.AddPrePolizasDetalle(CvePrePoliza, False, Importe, "", 211, Concepto, TxtFechaSaldar.Text.ToDate, IdCtaContable, -1, gr.DataKey("CveIE").ToEntero, ImporteMX:=If(CveMoneda > 1, (Importe * TC), Importe).RoundSQL, CveCuenta:=-1, CveCompra:=gr.DataKey("CveCompra").ToEntero, CveSubProyecto:=gr.DataKey("CveSubProyecto").ToEntero, CveProveedor:=gr.DataKey("CveProveedor").ToEntero) Then MsgError = "Ocurrio un Error al saldar el movimiento" End If If TC <> TCCompra AndAlso CveMoneda > 1 Then Dim difFluctuacion As Decimal = TC - TCCompra Dim CveConceptoFluctua As Integer = 64 '64.ganacia, 65.perdida Dim TipoMovtoFluctua As Boolean = True Dim IdCtaContableFluctua As Integer If difFluctuacion < 0 Then CveConceptoFluctua = 65 TipoMovtoFluctua = False IdCtaContableFluctua = UsuarioActual.Empresa.IdCuentaPerdidaCambiaria Else CveConceptoFluctua = 64 TipoMovtoFluctua = True IdCtaContableFluctua = UsuarioActual.Empresa.IdCuentaGananciaCambiaria End If Dim ImporteFluctuacion As Decimal = Math.Abs((Importe * difFluctuacion).RoundSQL) If Not ObjConta.AddPrePolizasDetalle(CvePrePoliza, TipoMovtoFluctua, ImporteFluctuacion, "", CveConceptoFluctua, Concepto, Now, IdCtaContableFluctua, -1, CveCompra:=gr.DataKey("CveCompra").ToEntero, CveProveedor:=gr.DataKey("CveProveedor").ToEntero, ImporteMX:=ImporteFluctuacion, ImporteME:=ImporteFluctuacion, TipoCambio:=TCCompra, TipoCambioPago:=TC) Then MsgError = "Ocurrio un Error al saldar el movimiento" End If End If If ObjConta.ValidacionPrePolizaCuadre(CvePrePoliza, 5) Then If ObjConta.ValorPolizaDescuadre > 0 Then MsgError = ObjConta.MensajePolizaDescuadre GuardaLogAD($"Ventas/Ventas-ProcesoPrePolizas | Error:{ObjConta.MensajePolizaDescuadre} Diferencia:{ObjConta.ValorDiferencia} | CvePrePoliza:{CvePrePoliza}", "LogPrePolizas") End If End If If UsuarioActual.Empresa.Funcionalidad(200) = "1" AndAlso MsgError = "" Then If ObjConta.ContabilizarPrePoliza(CvePrePoliza, UsuarioActual.CveUsuario, UsuarioActual.Empresa) Then If MensajeAlert.ToString = "" Then MensajeAlert.AppendFormat("Los siguientes movimientos fueron saldados correctamente:
") End If MensajeAlert.AppendFormat("", ObjConta.ContaIdPoliza, ObjConta.ContaFolio) End If End If If MsgError <> "" Then Exit For End If Next If MsgError = "" Then ObjConta.Commit() If MensajeAlert.ToString <> "" Then Alert(MensajeAlert.ToString) Else Alert("Los movimientos fueron saldados correctamente.") End If Alert(MensajeAlert.ToString) CargaDatos() HLSaldar.Visible = False RegistraScript("$('#DivSaldar').modal('hide')") LnkHabilitarSaldar.Visible = True Else ObjConta.RollBack() Alert(MsgError, TipoMensaje.MsgError) End If ObjConta.Dispose() End Sub Protected Sub LnkSaldar_Click(sender As Object, e As EventArgs) End Sub #Region "O R D E N A M I E N T O" Protected Sub LbOrden_Click(ByVal sender As Object, ByVal e As EventArgs) Dim Orden As String = "" Dim Campo As String = "" Dim Literal As Literal = Nothing Const OrdenInicial As String = "" Dim Arg(2) As String Arg = CType(sender, LinkButton).CommandArgument().ToString.Split(";"c) Campo = Arg(0) Literal = CType(CType(sender, LinkButton).NamingContainer.FindControl("Img" & Campo.Replace(", ", "_")), Literal) If ViewState(String.Format("O{0}", Campo)) IsNot Nothing AndAlso ViewState(String.Format("O{0}", Campo)).ToString = String.Format("{0} DESC", Campo) Then ViewState(String.Format("O{0}", Campo)) = String.Format("{0} ASC", Campo) Literal.Text = String.Format("") Else ViewState(String.Format("O{0}", Campo)) = String.Format("{0} DESC", Campo) Literal.Text = String.Format("") End If Orden = OrdenInicial & ViewState(String.Format("O{0}", Campo)).ToString ViewState("Orden") = Orden CargaGrid(Orden) End Sub Protected Sub CargaGrid(Orden As String) Dim DV As DataView = Nothing DV = New DataView(DtDatos, "", Orden, DataViewRowState.CurrentRows) If Orden <> String.Empty Then GvDatos.DataSource = DV.ToTable() Else GvDatos.DataSource = DtDatos End If GvDatos.DataBind() UpDatos.Update() DV.Dispose() End Sub #End Region End Class