Files
SIG/Ashx/E189.ashx
JPerez 282b31b4eb --Impresion servicios de construccion
--API E189
--FIX Metas vendedores
2025-07-08 01:43:10 -07:00

216 lines
7.2 KiB
Plaintext

<%@ WebHandler Language="VB" Class="E189" %>
Option Strict On
Option Explicit On
Imports System.Data
Imports System.IO
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports Extensiones
Public Class E189 : Implements IHttpHandler, System.Web.SessionState.IRequiresSessionState
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Private ReadOnly Property CveEmpresa As Integer = 189
Private ReadOnly Property CveUsuario As Integer = 100
Private ReadOnly Property NombreCadena As String = "Conexion"
Public ReadOnly Property EsProduccion As Boolean
Get
If IsNothing(System.Configuration.ConfigurationManager.AppSettings("EsProduccion")) Then
Return False
Else
Return (System.Configuration.ConfigurationManager.AppSettings("EsProduccion").ToString() = "1")
End If
End Get
End Property
#Region "V A L I D A - D A T O S"
Private Function ValidaUsuarioSistema(CveUsuario As Integer, CveEmpresa As Integer) As Usuario
Dim UsuarioActual As Usuario = Nothing
Dim NombreCadena As String = ClsPaginas.GlobalNombreCadenaConexion(CveEmpresa)
Dim ObjUsuarios As New ClsSisUsuarioAutenticacion(NombreCadena:=NombreCadena)
UsuarioActual = ObjUsuarios.GetUsuarioDetalle(100, CveEmpresa)
If UsuarioActual IsNot Nothing Then
HttpContext.Current.Session("UsuarioSIG") = UsuarioActual
'CreaObjetoEmpresa(UsuarioActual)
End If
ObjUsuarios.Dispose()
Return UsuarioActual
End Function
'Private Sub CreaObjetoEmpresa(ByRef UsuarioActual As Usuario)
' Dim ObjEmpresa As New ClsSisEmpresas()
' If ObjEmpresa.GetEmpresas(UsuarioActual.CveEmpresa, "-1", "-1", "-1", "-1") Then
' If ObjEmpresa.GetEmpresasFuncionalidades(UsuarioActual.CveEmpresa) Then
' Dim dttmp As Data.DataTable = ObjEmpresa.DtResultados
' Dim EmpresasFuncionalidades As New HybridDictionary
' For Each Row As Data.DataRow In dttmp.Rows
' EmpresasFuncionalidades.Add(Convert.ToInt32(Row("CveFuncionalidad")), Row("Valor").ToString)
' Next
' ObjEmpresa.Empresa.SetFuncionalidades(EmpresasFuncionalidades)
' End If
' UsuarioActual.Empresa = ObjEmpresa.Empresa
' End If
' ObjEmpresa.Dispose()
'End Sub
#End Region
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim retorno As String = ""
Dim MsgLog As New StringBuilder
MsgLog.AppendLine("Inicia Petición.")
Try
Dim Metodo As String = If(context.Request("Metodo") = Nothing, "", context.Request("Metodo").ToString())
MsgLog.AppendLine(Metodo)
If Metodo = "Stock" Then
context.Response.ContentType = "application/json"
retorno = ConsultaDatos(context)
End If
context.Response.Write(retorno)
Catch ex As Exception
context.Response.ContentType = "text/html"
MsgLog.AppendLine("Error de procesamiento:" & ex.Message)
Dim respuesta As New AuthMessage
respuesta.ErrorMessage = New ResponseMsgError() With {.message = "La solicitud no se pudo procesar."}
respuesta.IsSuccess = False
respuesta.StatusCode = 200
retorno = JsonConvert.SerializeObject(respuesta.ErrorMessage)
context.Response.Write(retorno)
End Try
MsgLog.ToString.GuardaLogAD("LogE119")
End Sub
Private Function ConsultaDatos(ByVal context As HttpContext) As String
Dim Respuesta As New ResponseMsg
Dim RespuestaError As New ResponseMsgError
Dim TieneError As Boolean = False
Dim productos As JArray = Nothing
Using ObjInventario As New ClsProductosServicios
If ObjInventario.GetPS_APIMarcasStock(189) AndAlso ObjInventario.DtResultados IsNot Nothing Then
productos = JArray.Parse(ObjInventario.DtResultados.DataTableToJSONIPP())
Else
RespuestaError.message = "no se pudo obtener los datos"
TieneError = True
End If
End Using
If TieneError Then
context.Response.StatusCode = 200
Return JsonConvert.SerializeObject(RespuestaError)
Else
context.Response.StatusCode = 200
Try
Return New JObject(New JProperty("productos", productos)).ToString()
Catch ex As Exception
RespuestaError.message = "Ocurrió un error al generar la petición"
Return JsonConvert.SerializeObject(RespuestaError)
End Try
End If
End Function
Private Class ResponseMsg
Sub New()
status = "success"
End Sub
Public Property status As String
Public Property data As Object
Public Property message As String
End Class
Private Class ResponseMsgError
Sub New()
status = "error"
End Sub
Public Property status As String
Public Property message As String
End Class
Private Class AuthMessage
Public Property IsSuccess As Boolean
Public Property ErrorMessage As ResponseMsgError
Public Property StatusCode As Integer
End Class
'Private Function ActualizarExistenciasJson(Todos As Boolean) As String
' Dim payload = New JObject()
' Dim CountPeticiones As Integer = 0, CountPeticionesExitosas As Integer = 0
' Dim DtPS As DataTable = Nothing
' Using ObjPS As New ClsProductosServicios
' If ObjPS.GetExistenciasEmpresa("-1", CveEmpresa) Then
' DtPS = ObjPS.DtResultados.Copy()
' End If
' End Using
' If DtPS IsNot Nothing Then
' payload.Add(New JProperty("stock", GenerarPayloadExistencias(DtPS)))
' End If
' Return payload.ToString()
'End Function
Private Function Parametros(Cadena As String) As Hashtable
Dim P1() As String = Cadena.Split("&"c)
Dim Tmp As String
Dim P2() As String
Dim URLParametros As New Hashtable
For Each Tmp In P1
P2 = Tmp.Replace("%26%", "&").Split("="c)
URLParametros.Add(P2(0).ToString.ToUpper, P2(1))
Next
Return URLParametros
End Function
Private Shared Function IsValidJson(ByVal strInput As String) As Boolean
If String.IsNullOrWhiteSpace(strInput) Then
Return False
End If
strInput = strInput.Trim()
If (strInput.StartsWith("{") AndAlso strInput.EndsWith("}")) OrElse (strInput.StartsWith("[") AndAlso strInput.EndsWith("]")) Then
Try
Dim obj As JToken = JToken.Parse(strInput)
Return True
Catch jex As JsonReaderException
Console.WriteLine(jex.Message)
Return False
Catch ex As Exception
Console.WriteLine(ex.ToString())
Return False
End Try
Else
Return False
End If
End Function
End Class