305 lines
11 KiB
Plaintext
305 lines
11 KiB
Plaintext
<%@ WebHandler Language="VB" Class="E119" %>
|
|
|
|
Option Strict On
|
|
Option Explicit On
|
|
|
|
Imports System.Data
|
|
Imports System.IO
|
|
Imports System.Net.Http
|
|
Imports Newtonsoft.Json
|
|
Imports Newtonsoft.Json.Linq
|
|
Imports Extensiones
|
|
|
|
Public Class E119 : Implements IHttpHandler
|
|
|
|
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
|
|
Get
|
|
Return False
|
|
End Get
|
|
End Property
|
|
|
|
Private ReadOnly Property CveEmpresa As Integer = 119
|
|
|
|
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
|
|
Public ReadOnly Property UsuarioWSE119 As String
|
|
Get
|
|
If IsNothing(System.Configuration.ConfigurationManager.AppSettings("UsuarioWSE119")) Then
|
|
Return ""
|
|
Else
|
|
Return System.Configuration.ConfigurationManager.AppSettings("UsuarioWSE119").ToString()
|
|
End If
|
|
End Get
|
|
End Property
|
|
Public ReadOnly Property PasswordWSE119 As String
|
|
Get
|
|
If IsNothing(System.Configuration.ConfigurationManager.AppSettings("PasswordWSE119")) Then
|
|
Return ""
|
|
Else
|
|
Return System.Configuration.ConfigurationManager.AppSettings("PasswordWSE119").ToString()
|
|
End If
|
|
End Get
|
|
End Property
|
|
Public ReadOnly Property TokenAutorizacion As String
|
|
Get
|
|
Return "$2y$10$LGqtRWUloKQmAt.jcUldc.PmS5LJ6l.WU2w1G4PBOTLMVKZRNQFPS"
|
|
End Get
|
|
End Property
|
|
|
|
Public ReadOnly Property URLBase As String
|
|
Get
|
|
Return "https://services.fidelityapps.mx/"
|
|
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 ObjUsuarios As New ClsSisUsuarios
|
|
If ObjUsuarios.GetUsuarios(CveUsuario) Then
|
|
If ObjUsuarios.DtResultados.Rows.Count > 0 Then
|
|
Dim Usuario = ObjUsuarios.DtResultados.Rows(0)("Usuario").ToString()
|
|
Dim Contrasenia = ObjUsuarios.DtResultados.Rows(0)("Contraseña").ToString()
|
|
UsuarioActual = ObjUsuarios.ValidaUsuario(Usuario, Contrasenia, IMEI, CveEmpresa)
|
|
UsuarioActual.CveEmpresa = CveEmpresa
|
|
CreaObjetoEmpresa(UsuarioActual)
|
|
End If
|
|
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 = ""
|
|
Try
|
|
Dim Metodo = If(context.Request("Metodo") = Nothing, "", context.Request("Metodo").ToString())
|
|
'OBTENER EXISTENCIAS
|
|
If Metodo = "BASF_POG40" Then
|
|
context.Response.ContentType = "application/json"
|
|
Dim Auth = ValidarSolicitud(context)
|
|
|
|
If Auth.IsSucces Then
|
|
retorno = ConsultaDatos(context)
|
|
Else
|
|
context.Response.StatusCode = Auth.StatusCode
|
|
retorno = JsonConvert.SerializeObject(Auth.ErrorMessage)
|
|
End If
|
|
|
|
End If
|
|
|
|
context.Response.Write(retorno)
|
|
|
|
Catch ex As Exception
|
|
context.Response.ContentType = "text/html"
|
|
retorno = ex.ToString()
|
|
ex.GuardaLog(ex.ToString())
|
|
|
|
context.Response.Write(retorno)
|
|
End Try
|
|
End Sub
|
|
|
|
Private Function ValidarSolicitud(ByVal context As HttpContext) As AuthMessage
|
|
Dim unauthorized As Boolean = True
|
|
Dim respuesta As New AuthMessage
|
|
|
|
Dim authHeader = context.Request.Headers("Authorization")
|
|
If authHeader IsNot Nothing Then
|
|
Dim basicAuth As String = authHeader.Replace("Basic ", "")
|
|
|
|
Dim usernamePasswordArray = Encoding.UTF8.GetString(Convert.FromBase64String(basicAuth)).Split(":"c)
|
|
|
|
Dim userName = usernamePasswordArray(0)
|
|
Dim password = usernamePasswordArray(1)
|
|
unauthorized = Not (userName = Me.UsuarioWSE119 AndAlso password = Me.PasswordWSE119)
|
|
|
|
Else
|
|
respuesta.ErrorMessage = New ResponseMsgError() With {.message = "el usuario y/o contraseña son requeridos"}
|
|
respuesta.IsSucces = False
|
|
respuesta.StatusCode = 200
|
|
|
|
Return respuesta
|
|
End If
|
|
|
|
If unauthorized Then
|
|
respuesta.ErrorMessage = New ResponseMsgError() With {.message = "el usuario y/o contraseña no son validos"}
|
|
respuesta.IsSucces = False
|
|
respuesta.StatusCode = 200
|
|
|
|
Return respuesta
|
|
Else
|
|
respuesta.ErrorMessage = Nothing
|
|
respuesta.IsSucces = True
|
|
respuesta.StatusCode = 200
|
|
|
|
Return respuesta
|
|
End If
|
|
|
|
End Function
|
|
|
|
|
|
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 strFecha = If(context.Request("Fecha") = Nothing, "", context.Request("Fecha").ToString())
|
|
|
|
If String.IsNullOrEmpty(strFecha) Then
|
|
RespuestaError.message = "Debe especificar una fecha"
|
|
context.Response.StatusCode = 200
|
|
Return JsonConvert.SerializeObject(RespuestaError)
|
|
End If
|
|
|
|
If Not IsDate(strFecha) Then
|
|
RespuestaError.message = "La fecha no tiene el formato correcto"
|
|
context.Response.StatusCode = 200
|
|
Return JsonConvert.SerializeObject(RespuestaError)
|
|
End If
|
|
|
|
Dim Fecha As DateTime = strFecha.ToDateTime()
|
|
Dim Facturas As JArray = Nothing
|
|
Dim Stock As JArray = Nothing
|
|
|
|
If Fecha > MinFecha Then
|
|
Using ObjInventario As New ClsInventarios
|
|
If ObjInventario.GetJAM_BASF_POG(Fecha) AndAlso ObjInventario.DataSetResultados IsNot Nothing Then
|
|
Facturas = JArray.Parse(ObjInventario.DataSetResultados.Tables(0).DataTableToJSONIPP())
|
|
Stock = JArray.Parse(ObjInventario.DataSetResultados.Tables(1).DataTableToJSONIPP())
|
|
Else
|
|
RespuestaError.message = "no se pudo obtener los datos"
|
|
TieneError = True
|
|
End If
|
|
End Using
|
|
Dim Msg As String = "Información día " & Fecha.ToShortDateString & If(RespuestaError.message <> "", " Error: " & RespuestaError.message, "")
|
|
Msg.GuardaLogAD("LogE119")
|
|
Else
|
|
RespuestaError.message = "la fecha no es leida correctamente"
|
|
TieneError = True
|
|
End If
|
|
|
|
If TieneError Then
|
|
context.Response.StatusCode = 200
|
|
Return JsonConvert.SerializeObject(RespuestaError)
|
|
Else
|
|
context.Response.StatusCode = 200
|
|
Try
|
|
Respuesta.data = New JObject(New JProperty("Facturas", Facturas), New JProperty("Stock", Stock))
|
|
Respuesta.message = "Los datos se obtuvieron correctamente"
|
|
Return JsonConvert.SerializeObject(Respuesta)
|
|
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 IsSucces 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 = 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 |