Files
SIG/Compras/RepNotasCredito.aspx.vb
2023-04-20 19:05:46 -07:00

281 lines
14 KiB
VB.net

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.setEsWebService(True)
DdlProveedores.Attributes.Add("data-metodo", "getproveedores")
DdlProveedores.SelectedText = "Todos"
DdlProveedores.SelectedValue = "-1"
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("<b>Proveedor:</b> {0}, <b>Estatus:</b> {1}", DdlProveedores.SelectedItem.Text, DdlOpcion.SelectedItem.Text))
' If DdlTipoComprobante.SelectedValue <> "-1" Then StrFiltro.AppendFormat("&emsp;<b>Tipo Documento</b>: {0}", DdlTipoComprobante.SelectedItem.Text)
If DdlProveedores.SelectedValue <> "-1" Then StrFiltro.AppendFormat("&emsp;<b>Proveedor:</b> {0}", DdlProveedores.SelectedText)
If DdlOpcion.SelectedValue <> "-1" Then StrFiltro.AppendFormat("&emsp;<b>Estatus:</b> {0}", DdlOpcion.SelectedItem.Text)
If txtFechaI.Text <> "" Then StrFiltro.AppendFormat("&emsp;<b>Fecha Inicio </b>: {0}", txtFechaI.Text.ToDate.ToShortDateString)
If txtFechaF.Text <> "" Then StrFiltro.AppendFormat("&emsp;<b>Fecha Fin </b>: {0}", txtFechaF.Text.ToDate.ToShortDateString)
Dim Imagen As New System.IO.FileInfo($"{UsuarioActual.Empresa.Ruta}/Logo.png")
'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))
params.Add(New ReportParameter("LogoPath", If(Imagen.Exists, $"file://{Imagen.FullName}", "")))
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
CvePrePoliza = ObjConta.AddPrePolizas("Compras/RepNotasCredito", 103, 0, 0, 1, TxtFechaSaldar.Text.ToDate, Concepto, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa)
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
Dim CveTipoConcepto = gr.DataKey("CveTipoConcepto").ToEntero
If CveMoneda > 1 Then
IdCtaContable = gr.DataKey("IdCtaContExtranjera").ToEntero
ImporteMN = (gr.DataKey("Saldo").ToDecimal * TCCompra).RoundSQL
Else
IdCtaContable = gr.DataKey("IdCtaContProveedores").ToEntero
End If
Dim ConPreP As ConceptosTipoSaldo = ClsPaginas.ObtenerCveConceptoPrePoliza(15, CveMoneda, False, CveTipoConcepto)
'Dim CveCon As Integer = ObtConceptoContableProvisionCompra(CveTipoConcepto, DdlTipoComprobante.SelectedValue, DdlMoneda.SelectedValue.ToEntero, False)
If Not ObjConta.AddPrePolizasDetalle(CvePrePoliza, False, -Importe, gr.DataKey("SerieFolio"), 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, CveSubProyecto:=gr.DataKey("CveSubProyecto").ToEntero) 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, CveSubProyecto:=gr.DataKey("CveSubProyecto").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
If Not ObjConta.AddPrePolizasDetalle(CvePrePoliza, False, Importe, gr.DataKey("SerieFolio"), 211, Concepto, TxtFechaSaldar.Text.ToDate, 0, -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
Else
CveConceptoFluctua = 64
TipoMovtoFluctua = True
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, CveSubProyecto:=gr.DataKey("CveSubProyecto").ToEntero) Then
MsgError = "Ocurrio un Error al saldar el movimiento"
End If
End If
If MsgError <> "" Then Exit For
End If
Next
If MsgError = "" Then
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: <br/>")
End If
MensajeAlert.AppendFormat("<ul><li><a Class=""Texto"" href=""/Contabilidad/RegPoliza.aspx?IdPoliza={0}"">Poliza {1}</a></li></ul>", ObjConta.ContaIdPoliza, ObjConta.ContaFolio)
End If
End If
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
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("<i class='fa fa-sort-asc' style='color:#999' ></i>")
Else
ViewState(String.Format("O{0}", Campo)) = String.Format("{0} DESC", Campo)
Literal.Text = String.Format("<i class='fa fa-sort-desc' style='color:#999' ></i>")
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