Option Explicit On
Option Strict On
Imports System.Data
Imports Extensiones
Partial Class Compras_Importaciones
Inherits ClsPaginas
#Region "PROPIEDADES"
Private Property CveImportacion As Integer
Get
Return ViewState("CveImportacion").ToString.ToEntero
End Get
Set(value As Integer)
ViewState("CveImportacion") = value
End Set
End Property
Private Property RowAF As DataRow
Get
Return CType(ViewState("RowAF"), DataRow)
End Get
Set(value As DataRow)
ViewState("RowAF") = value
End Set
End Property
Private Property DtCompras As DataTable
Get
Return CType(ViewState("DtCompras"), DataTable)
End Get
Set(value As DataTable)
ViewState("DtCompras") = value
End Set
End Property
Private Property DtComprasVinculadas As DataTable
Get
Return CType(ViewState("DtComprasVinculadas"), DataTable)
End Get
Set(value As DataTable)
ViewState("DtComprasVinculadas") = 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
InicializaValores()
CargaURLParametros()
End If
End Sub
#Region " INICIALIZA VALORES "
Private Sub InicializaValores()
DtCompras = New DataTable
DtComprasVinculadas = New DataTable
DdlAduanal.DataSource = GlobalDTCompProveedores(UsuarioActual.CveEmpresa)
DdlAduanal.DataBind()
TxtFecha.Text = Now.ToShortDateString
End Sub
#End Region
#Region " CARGA DATOS "
Private Sub CargaURLParametros()
If Request("CveImportacion") IsNot Nothing AndAlso IsNumeric(Request("CveImportacion")) AndAlso Request("CveImportacion").ToEntero > 0 Then
CveImportacion = Request("CveImportacion").ToEntero
BtnGuardar.Text = "Guardar"
CargaImportacion()
If UsuarioActual.TienePermisoProceso(103) Then
BtnEliminar.Visible = True
End If
Else
CveImportacion = -1
End If
End Sub
Private Sub CargaImportacion()
Dim ObjImportaciones As New ClsCompras
If ObjImportaciones.GetImportaciones(CveImportacion, -1, -1, "-1", "-1", UsuarioActual.CveEmpresa) Then
If ObjImportaciones.DtResultados.Rows.Count > 0 Then
TxtPedimento.Text = ObjImportaciones.DtResultados.Rows(0)("Pedimento").ToString
TxtDescripcion.Text = ObjImportaciones.DtResultados.Rows(0)("Descripcion").ToString
TxtFecha.Text = ObjImportaciones.DtResultados.Rows(0)("Fecha").ToString
DdlAduanal.SelectedValue = ObjImportaciones.DtResultados.Rows(0)("CveAduanal").ToString
'HdnCveCompra.Value = ObjImportaciones.DtResultados.Rows(0)("CveCompra").ToString
'LbCompraProveedor.Text = ObjImportaciones.DtResultados.Rows(0)("RazonSocial").ToString
'HLCompraSerieFolio.Text = ObjImportaciones.DtResultados.Rows(0)("SerieFolio").ToString
'HLCompraSerieFolio.NavigateUrl = $"/Compras/Compras.aspx?CveCompra={HdnCveCompra.Value}"
'LbCompraImporte.Text = ObjImportaciones.DtResultados.Rows(0)("Total").ToString
HdnCveAF.Value = ObjImportaciones.DtResultados.Rows(0)("CveActivoFijo").ToString
VincularAF()
If ObjImportaciones.GetImportacionesDetalle(CveImportacion, -1) Then
DtCompras = ObjImportaciones.DtResultados
Else
Alert("Ocurrió un error al cargar el detalle de la importación", TipoMensaje.MsgError)
End If
If ObjImportaciones.GetImportacionCompras(CveImportacion) Then
DtComprasVinculadas = ObjImportaciones.DtResultados
End If
Else
Alert("No se encontró la importación", TipoMensaje.MsgError)
End If
Else
Alert("Ocurrió un error al cargar la importación", TipoMensaje.MsgError)
End If
ObjImportaciones.Dispose()
CargaGrid()
CargaGridCompraVinculada()
End Sub
#End Region
#Region " GUARDAR "
Protected Sub BtnGuardar_Click(sender As Object, e As EventArgs)
If Not GvComprasVinculadas.Rows.Count > 0 Then
Alert("Tiene que tener al menos una compra vinculada", TipoMensaje.MsgAdvertencia)
Exit Sub
End If
If Not GvDatos.Rows.Count > 0 Then
Alert("Tiene que tener al menos una compra de gasto", TipoMensaje.MsgAdvertencia)
Exit Sub
End If
Dim ObjImp As New ClsCompras
ObjImp.IniciaTransacSQL()
Dim ErrorImportacion = False
If CveImportacion < 0 Then
CveImportacion = ObjImp.AddImportacion(TxtPedimento.Text.Trim, TxtDescripcion.Text.Trim, DdlAduanal.SelectedValue.ToEntero, HdnCveAF.Value.ToEntero, TxtFecha.Text.ToDate, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa)
If CveImportacion > 0 Then
For Each dr As DataRow In DtCompras.Rows
If Not ObjImp.AddImportacionDetalle(CveImportacion, dr("CveCompra").ToString.ToEntero) Then
ErrorImportacion = True
End If
Next
Else
ErrorImportacion = True
End If
If CveImportacion > 0 Then
For Each dr As DataRow In DtComprasVinculadas.Rows
If Not ObjImp.AddImportacionCompra(CveImportacion, dr("CveCompra").ToString.ToEntero) Then
ErrorImportacion = True
End If
Next
Else
ErrorImportacion = True
End If
Else
If Not ObjImp.UpdateImportacion(TxtPedimento.Text.Trim, CveImportacion, TxtDescripcion.Text, DdlAduanal.SelectedValue.ToString.ToEntero, HdnCveAF.Value.ToEntero, TxtFecha.Text.ToDate, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa) Then
ErrorImportacion = True
End If
End If
If Not ErrorImportacion Then
BtnGuardar.Enabled = False
ObjImp.Commit()
Alert("Importación agregada con éxito.", TipoMensaje.MsgInformacion, "document.location.href=""/Compras/RepImportaciones.aspx"";")
Else
ObjImp.RollBack()
Alert("Ocurrió un error al registrar la importación. Por favor vuelva a intentar.", TipoMensaje.MsgError)
End If
ObjImp.Dispose()
End Sub
#End Region
#Region " ELIMINAR "
Protected Sub BtnEliminar_Click(sender As Object, e As EventArgs)
Dim ObjImp As New ClsCompras
ObjImp.IniciaTransacSQL()
Dim ErrorEliminar = False
For Each dr As DataRow In DtCompras.Rows
If Not ObjImp.RemoveImportacionDetalle(dr("CveImportacionDetalle").ToString.ToEntero) Then
ErrorEliminar = True
End If
Next
For Each dr As DataRow In DtComprasVinculadas.Rows
If Not ObjImp.RemoveImportacionCompra(dr("CveImportacionCompra").ToString.ToEntero) Then
ErrorEliminar = True
End If
Next
If Not ErrorEliminar Then
If Not ObjImp.RemoveImportacion(CveImportacion) Then
ErrorEliminar = True
End If
End If
If Not ErrorEliminar Then
Alert("Importación elimnada con éxito.", TipoMensaje.MsgInformacion, "document.location.href=""/Compras/RepImportaciones.aspx"";")
ObjImp.Commit()
Else
Alert("Ocurrió un error eliminar la importación. Por favor vuelva a intentar.", TipoMensaje.MsgError)
ObjImp.RollBack()
End If
ObjImp.Dispose()
End Sub
#End Region
#Region " VINCULA COMPRA "
Protected Sub BtnVincularCompra_Click(sender As Object, e As EventArgs)
'VinculaCompra()
AgregaCompraVinculada()
End Sub
Private Sub VinculaCompra()
If HdnCveCompra.Value <> "-1" AndAlso IsNumeric(HdnCveCompra.Value) AndAlso HdnCveCompra.Value.ToEntero > 0 Then
Dim ObjCompras As New ClsCompras
Dim RowCompra As DataRow
If ObjCompras.GetImportacionCompra(HdnCveCompra.Value.ToEntero) AndAlso ObjCompras.DtResultados.Rows.Count = 0 Then
If ObjCompras.GetCompras(HdnCveCompra.Value.ToEntero, "-1", "-1", -1, -1, UsuarioActual.CveEmpresa) Then
RowCompra = ObjCompras.DtResultados.Rows(0)
'LbCompraProveedor.Text = ObjCompras.DtResultados.Rows(0)("RazonSocial").ToString
'HLCompraSerieFolio.Text = $"{ObjCompras.DtResultados.Rows(0)("Serie").ToString} - {ObjCompras.DtResultados.Rows(0)("Folio").ToString}"
'LbCompraImporte.Text = $"$ {ObjCompras.DtResultados.Rows(0)("Total").ToString.FormatoNumero} {ObjCompras.DtResultados.Rows(0)("Abreviatura").ToString}"
Else
HdnCveCompra.Value = "-1"
End If
Else
Alert($"La compra ya forma parte de la importación: {ObjCompras.DtResultados.Rows(0)("Importacion")}. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
ObjCompras.Dispose()
End If
UpRegistro.Update()
End Sub
Private Sub CargaGridCompraVinculada()
GvComprasVinculadas.DataSource = DtComprasVinculadas
GvComprasVinculadas.DataBind()
UpComprasVinculadas.Update()
End Sub
Protected Sub LnkEliminaCompraVinculada_Click(sender As Object, e As EventArgs)
Dim gvRow As GridViewRow = DirectCast((DirectCast(sender, LinkButton)).NamingContainer, GridViewRow)
Dim TieneError = False
If CveImportacion > 0 AndAlso GvComprasVinculadas.Rows(gvRow.RowIndex).DataKey("CveImportacionCompra").ToEntero > 0 Then
Dim ObjImportaciones As New ClsCompras
If Not ObjImportaciones.RemoveImportacionCompra(GvComprasVinculadas.Rows(gvRow.RowIndex).DataKey("CveImportacionCompra").ToEntero) Then
TieneError = True
Alert("Ocurrió un error al eliminar la compra. Por favor intente de nuevo", TipoMensaje.MsgError)
Else
Alert("La compra de la importación se eliminó correctamente", TipoMensaje.MsgInformacion)
End If
ObjImportaciones.Dispose()
End If
If Not TieneError Then
'Dim RowDelete As DataRow = DtCompras.FiltroPrimero(String.Format("CveCompra = {0}", img.CommandArgument.ToString))
DtComprasVinculadas.Rows.RemoveAt(gvRow.RowIndex)
DtComprasVinculadas.AcceptChanges()
End If
CargaGridCompraVinculada()
End Sub
Private Sub AgregaCompraVinculada()
If Not (HdnCveCompra.Value <> "-1" AndAlso IsNumeric(HdnCveCompra.Value) AndAlso HdnCveCompra.Value.ToEntero > 0) Then
Alert("No hay compra seleccionaada", TipoMensaje.MsgAdvertencia)
Exit Sub
End If
Dim ObjCompras As New ClsCompras
Dim ErrorAgregar = False
Dim RowCompra As DataRow = Nothing
If ObjCompras.GetCompras(HdnCveCompra.Value.ToEntero, "-1", "-1", -1, -1, UsuarioActual.CveEmpresa) Then
RowCompra = ObjCompras.DtResultados.Rows(0)
If Not (ObjCompras.GetImportacionCompras(CveCompra:=HdnCveCompra.Value.ToEntero) AndAlso ObjCompras.DtResultados.Rows.Count = 0) Then
ErrorAgregar = True
Alert($"La compra ya forma parte de la importación: {ObjCompras.DtResultados.Rows(0)("Importacion")}. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
If Not (ObjCompras.GetImportacionCompra(HdnCveCompra.Value.ToEntero) AndAlso ObjCompras.DtResultados.Rows.Count = 0) Then
ErrorAgregar = True
Alert($"La compra ya forma parte de la importación: {ObjCompras.DtResultados.Rows(0)("Importacion")}. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
Else
ErrorAgregar = True
Alert("Ocurrió un error al cargar la información de la compra. Por favor comuniquese con atención SIG.", TipoMensaje.MsgError)
End If
If Not ErrorAgregar Then
If DtComprasVinculadas.FiltraParametros($"CveCompra = {RowCompra("CveCompra").ToString.ToEntero}").Rows.Count > 0 Then
ErrorAgregar = True
Alert("La compra seleccionada ya es parte de la importación. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
End If
If Not ErrorAgregar Then
If DtCompras.FiltraParametros($"CveCompra = {RowCompra("CveCompra").ToString.ToEntero}").Rows.Count > 0 Then
ErrorAgregar = True
Alert("La compra seleccionada ya es parte de la importación. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
End If
If Not ErrorAgregar Then
If Not GvComprasVinculadas.Rows.Count > 0 Then
DtComprasVinculadas = RowCompra.Table.Clone
DtComprasVinculadas.Columns.Add("CveImportacionCompra", Type.GetType("System.Int32"))
End If
If CveImportacion > 0 Then
If Not ObjCompras.AddImportacionCompra(CveImportacion, RowCompra("CveCompra").ToString.ToEntero) Then
ErrorAgregar = True
End If
End If
End If
If Not ErrorAgregar Then
DtComprasVinculadas.ImportRow(RowCompra)
CargaGridCompraVinculada()
If CveImportacion > 0 Then
Alert("La compra se agregó correctamente.", TipoMensaje.MsgInformacion)
End If
Else
Alert("Ocurrió un error al agregar la compra. Por favor intente de nuevo.", TipoMensaje.MsgError)
End If
HdnCveCompra.Value = "-1"
ObjCompras.Dispose()
UpComprasVinculadas.Update()
End Sub
#End Region
#Region " AGREGA AF "
Protected Sub ImgAF_Click(sender As Object, e As ImageClickEventArgs)
RegistraScript(String.Format("AbrirVentana('/AF/PopUpBuscarAF.aspx?Tipo=C',800,600,'N');", "aa"))
End Sub
Protected Sub BtnVincularAF_Click(sender As Object, e As EventArgs)
VincularAF()
End Sub
Private Sub VincularAF()
If HdnCveAF.Value <> "-1" AndAlso IsNumeric(HdnCveAF.Value) AndAlso HdnCveAF.Value.ToEntero > 0 Then
Dim ObjAF As New ClsAF
If ObjAF.GetActivosFijos(HdnCveAF.Value.ToString.ToEntero, UsuarioActual.CveEmpresa, "-1", "-1", -1, -1, "-1", -1, -1, -1, -1, -1, "", -1) Then
RowAF = ObjAF.DtResultados.Rows(0)
TxtAF.Text = RowAF("CodDesc").ToString
LtCodigoAF.Text = RowAF("Codigo").ToString
LtDescripcionAF.Text = RowAF("Descripcion").ToString
LtFechaAF.Text = RowAF("FechaCompra").ToString.Substring(1, 10)
LtValorCompraAF.Text = RowAF("ValorCompra").ToString.FormatoNumero
End If
ObjAF.Dispose()
End If
End Sub
#End Region
#Region " AGREGA COMPRA GASTO "
Protected Sub BtnVincularCompraGasto_Click(sender As Object, e As EventArgs)
AgregaCompraGasto()
End Sub
Private Sub VinculaCompraGasto()
'If HdnCveCompraGasto.Value <> "-1" AndAlso IsNumeric(HdnCveCompraGasto.Value) AndAlso HdnCveCompraGasto.Value.ToEntero > 0 Then
' Dim ObjCompras As New ClsCompras
' Dim RowCompra As DataRow
' If ObjCompras.GetCompras(HdnCveCompraGasto.Value.ToEntero, "-1", "-1", -1, -1, UsuarioActual.CveEmpresa) Then
' RowCompra = ObjCompras.DtResultados.Rows(0)
' TxtDetalleCompra.Text = String.Format("Proveedor:{0} | Serie-Folio:{1}--{2} | Importe:{3}", ObjCompras.DtResultados.Rows(0)("RazonSocial").ToString, ObjCompras.DtResultados.Rows(0)("Serie").ToString, ObjCompras.DtResultados.Rows(0)("Folio").ToString, ObjCompras.DtResultados.Rows(0)("Total").ToString.FormatoNumero)
' Else
' HdnCveCompraGasto.Value = "-1"
' End If
' ObjCompras.Dispose()
'Else
'End If
End Sub
Protected Sub ImgAgregarCompraGasto_Click(sender As Object, e As ImageClickEventArgs)
AgregaCompraGasto()
End Sub
Private Sub AgregaCompraGasto()
If Not (HdnCveCompraGasto.Value <> "-1" AndAlso IsNumeric(HdnCveCompraGasto.Value) AndAlso HdnCveCompraGasto.Value.ToEntero > 0) Then
Alert("No hay compra seleccionaada", TipoMensaje.MsgAdvertencia)
Exit Sub
End If
Dim ObjCompras As New ClsCompras
Dim ErrorAgregar = False
Dim RowCompra As DataRow = Nothing
If ObjCompras.GetCompras(HdnCveCompraGasto.Value.ToEntero, "-1", "-1", -1, -1, UsuarioActual.CveEmpresa) Then
RowCompra = ObjCompras.DtResultados.Rows(0)
If Not (ObjCompras.GetImportacionCompra(HdnCveCompraGasto.Value.ToEntero) AndAlso ObjCompras.DtResultados.Rows.Count = 0) Then
ErrorAgregar = True
Alert($"La compra ya forma parte de la importación: {ObjCompras.DtResultados.Rows(0)("Importacion")}. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
Else
ErrorAgregar = True
Alert("Ocurrió un error al cargar la información de la compra. Por favor comuniquese con atención SIG.", TipoMensaje.MsgError)
End If
If Not ErrorAgregar Then
If DtCompras.FiltraParametros($"CveCompra = {RowCompra("CveCompra").ToString.ToEntero}").Rows.Count > 0 Then
ErrorAgregar = True
Alert("La compra seleccionada ya es parte de la importación. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
End If
If Not ErrorAgregar Then
If DtComprasVinculadas.FiltraParametros($"CveCompra = {RowCompra("CveCompra").ToString.ToEntero}").Rows.Count > 0 Then
ErrorAgregar = True
Alert("La compra seleccionada ya es parte de la importación. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgAdvertencia)
End If
End If
If Not ErrorAgregar Then
If Not GvDatos.Rows.Count > 0 Then
DtCompras = RowCompra.Table.Clone
DtCompras.Columns.Add("CveImportacionDetalle", Type.GetType("System.Int32"))
End If
If CveImportacion > 0 Then
If Not ObjCompras.AddImportacionDetalle(CveImportacion, RowCompra("CveCompra").ToString.ToEntero) Then
ErrorAgregar = True
End If
End If
End If
If Not ErrorAgregar Then
DtCompras.ImportRow(RowCompra)
CargaGrid()
If CveImportacion > 0 Then
Alert("La compra se agregó correctamente.", TipoMensaje.MsgInformacion)
End If
Else
Alert("Ocurrió un error al agregar la compra. Por favor intente de nuevo.", TipoMensaje.MsgError)
End If
HdnCveCompraGasto.Value = "-1"
ObjCompras.Dispose()
End Sub
Private Sub GvDireccionesEnvio_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GvDatos.RowDeleting
Dim TieneError = False
If CveImportacion > 0 AndAlso GvDatos.Rows(e.RowIndex).DataKey("CveImportacionDetalle").ToEntero > 0 Then
Dim ObjImportaciones As New ClsCompras
If Not ObjImportaciones.RemoveImportacionDetalle(GvDatos.Rows(e.RowIndex).DataKey("CveImportacionDetalle").ToEntero) Then
TieneError = True
Alert("Ocurrió un error al eliminar el detalle. Por favor intente de nuevo", TipoMensaje.MsgError)
Else
Alert("El detalle de la importación se eliminó correctamente", TipoMensaje.MsgInformacion)
End If
ObjImportaciones.Dispose()
End If
If Not TieneError Then
'Dim RowDelete As DataRow = DtCompras.FiltroPrimero(String.Format("CveCompra = {0}", img.CommandArgument.ToString))
DtCompras.Rows.RemoveAt(e.RowIndex)
DtCompras.AcceptChanges()
End If
CargaGrid()
End Sub
Private Sub CargaGrid()
GvDatos.DataSource = DtCompras
GvDatos.DataBind()
UpComprasGasto.Update()
End Sub
#End Region
End Class