diff --git a/App_Code/ClsClientes.vb b/App_Code/ClsClientes.vb index a1169276d..69ac29dc8 100644 --- a/App_Code/ClsClientes.vb +++ b/App_Code/ClsClientes.vb @@ -1026,6 +1026,24 @@ Public Class ClsClientes OnError("Ocurrió un error al obtener el las Sucursales del Cliente.", ex, SqlAdapter.SelectCommand.Parameters.ParametrosInfo()) End Try + Return Retorno + End Function + + Public Function GetListaSucursales(RFC As String, CveEmpresa As Integer, Optional CveCliente As Integer = -1, Optional CveClienteSucursal As Integer = -1) As Boolean + Dim Retorno As Boolean = False + + DataAdapter("PA_Vta_ClientesSucursalesGet") + SqlAdapter.SelectCommand.Parameters.Add("@RFC", SqlDbType.VarChar).Value = RFC + SqlAdapter.SelectCommand.Parameters.Add("@CveEmpresa", SqlDbType.Int).Value = CveEmpresa + SqlAdapter.SelectCommand.Parameters.Add("@CveCliente", SqlDbType.Int).Value = CveCliente + SqlAdapter.SelectCommand.Parameters.Add("@CveClienteSucursal", SqlDbType.Int).Value = CveClienteSucursal + Try + SqlAdapter.Fill(Data) + Retorno = True + Catch ex As Exception + OnError("Ocurrió un error al obtener el las Sucursales del Cliente.", ex, SqlAdapter.SelectCommand.Parameters.ParametrosInfo()) + End Try + Return Retorno End Function diff --git a/Ashx/E141.ashx b/Ashx/E141.ashx index ca6e8c319..e139c0ecc 100644 --- a/Ashx/E141.ashx +++ b/Ashx/E141.ashx @@ -4,6 +4,7 @@ Option Strict On Option Explicit On Imports System.Data +Imports System.IO Imports System.Net.Http Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq @@ -66,6 +67,42 @@ Public Class E141 : Implements IHttpHandler End Get End Property + +#Region "GLOBAL DT" + Public Property GlobalDTSubProyectos(CveProyecto As Integer, Optional CveSubProyecto As Integer = -1, Optional CveEmpresa As Integer = -1, Optional Estatus As Integer = 1) As DataTable + Get + Dim AppNombre As String = String.Format("GlobalDTSubProyectos-{0}", CveEmpresa) + 'sino existe, recarga los datos + If DirectCast(HttpContext.Current.Application(AppNombre), DataTable) Is Nothing Or (DirectCast(HttpContext.Current.Application(AppNombre), DataTable) IsNot Nothing AndAlso DirectCast(HttpContext.Current.Application(AppNombre), DataTable).Rows.Count = 0) Then + Dim P As New ClsSubProyectos + If P.GetSubProyectos(CveEmpresa:=CveEmpresa) Then HttpContext.Current.Application(AppNombre) = P.DtResultados + P.Dispose() + End If + + If CveProyecto <> -1 OrElse CveSubProyecto <> -1 OrElse CveEmpresa <> -1 OrElse Estatus <> -1 Then + Dim StrSQL As New StringBuilder + If CveProyecto <> -1 Then StrSQL.AppendFormat(" AND CveProyecto = {0}", CveProyecto) + If CveSubProyecto <> -1 Then StrSQL.AppendFormat(" AND CveSubProyecto = {0}", CveSubProyecto) + If Estatus <> -1 Then StrSQL.AppendFormat(" AND CveEstatus = {0}", Estatus) + If CveEmpresa <> -1 Then StrSQL.AppendFormat(" AND CveEmpresa = {0}", CveEmpresa) + + Dim Dv As New DataView(DirectCast(HttpContext.Current.Application(AppNombre), DataTable), StrSQL.ToString.Trim.TrimStart("A"c, "N"c, "D"c), "", DataViewRowState.CurrentRows) + GlobalDTSubProyectos = Dv.ToTable + Dv.Dispose() + + Else + GlobalDTSubProyectos = DirectCast(HttpContext.Current.Application(AppNombre), DataTable) + End If + + End Get + Set(ByVal value As DataTable) + Dim AppNombre As String = String.Format("GlobalDTSubProyectos-{0}", CveEmpresa) + HttpContext.Current.Application(AppNombre) = value + End Set + End Property + +#End Region + Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim retorno As String = "" Try @@ -88,7 +125,56 @@ Public Class E141 : Implements IHttpHandler Dim Codigo = If(context.Request("Sku") = Nothing, "", context.Request("Sku").ToString()) retorno = GetExistencia(context, Codigo) + ElseIf Metodo = "ConsultarRFC" Then + context.Response.ContentType = "application/json" + Dim Auth = ValidarSolicitud(context) + If Auth.IsSucces Then + retorno = ConsultaRFC(context) + Else + retorno = JsonConvert.SerializeObject(Auth.ErrorMessage) + End If + + ElseIf Metodo = "ValidarCarrito" Then + context.Response.ContentType = "application/json" + Dim Auth = ValidarSolicitud(context) + + If Auth.IsSucces Then + retorno = ValidarCarrito(context) + Else + retorno = JsonConvert.SerializeObject(Auth.ErrorMessage) + End If + + ElseIf Metodo = "RegistrarPedido" Then + context.Response.ContentType = "application/json" + Dim Auth = ValidarSolicitud(context) + + If Auth.IsSucces Then + retorno = RegistrarPedido(context) + Else + retorno = JsonConvert.SerializeObject(Auth.ErrorMessage) + End If + + ElseIf Metodo = "RegistrarCotizacion" Then + context.Response.ContentType = "application/json" + Dim Auth = ValidarSolicitud(context) + + If Auth.IsSucces Then + retorno = RegistrarCotizacion(context) + Else + retorno = JsonConvert.SerializeObject(Auth.ErrorMessage) + End If + + ElseIf Metodo = "GetSucursales" Then + context.Response.ContentType = "application/json" + Dim Auth = ValidarSolicitud(context) + + If Auth.IsSucces Then + retorno = GetSucursales(context) + Else + retorno = JsonConvert.SerializeObject(Auth.ErrorMessage) + End If + context.Response.StatusCode = Auth.StatusCode Else context.Response.ContentType = "text/html" @@ -132,11 +218,219 @@ Public Class E141 : Implements IHttpHandler 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.UsuarioWSE141 AndAlso password = Me.PasswordWSE141) + + Else + respuesta.ErrorMessage = New FidelityAppsResponseError() With {.message = "el usuario y/o contraseña son requeridos"} + respuesta.IsSucces = False + respuesta.StatusCode = 401 + + Return respuesta + End If + + If unauthorized Then + respuesta.ErrorMessage = New FidelityAppsResponseError() With {.message = "el usuario y/o contraseña no son validos"} + respuesta.IsSucces = False + respuesta.StatusCode = 401 + + Return respuesta + Else + respuesta.ErrorMessage = Nothing + respuesta.IsSucces = True + respuesta.StatusCode = 200 + + Return respuesta + End If + + End Function + + Private Function ConsultaRFC(ByVal context As HttpContext) As String + Dim Respuesta As New FidelityAppsResponse + Dim RespuestaError As New FidelityAppsResponseError + Dim TieneError As Boolean = False + Dim JsonRequest As JObject = Nothing + + If context.Request.InputStream IsNot Nothing Then + Using reader As New StreamReader(context.Request.InputStream) + Dim json = reader.ReadToEnd + + If Not String.IsNullOrEmpty(json) Then + JsonRequest = JObject.Parse(json) + If JsonRequest Is Nothing Then + TieneError = True + End If + End If + + End Using + End If + + If Not TieneError AndAlso Not String.IsNullOrEmpty(JsonRequest("rfc").ToString().Trim()) Then + + Dim data As New JArray + Dim Nombre As String = "-1" + Dim Codigo As String = "-1" + Dim NombreCorto As String = "-1" + Dim RFC As String = JsonRequest("rfc").ToString() + Dim Telefono As String = "-1" + Dim Mail As String = "-1" + + Dim DtSucursales As DataTable = Nothing + + Using ObjClientes As New ClsClientes + If ObjClientes.GetListaSucursales(RFC, CveEmpresa) Then + DtSucursales = ObjClientes.DtResultados.Copy() + End If + End Using + + If DtSucursales IsNot Nothing Then + Try + For Each Row As DataRow In DtSucursales.Rows + Dim objSucursal As New JObject( + New JProperty("Id", Row("CveCliente")), + New JProperty("rfc", Row("RFC")), + New JProperty("name", Row("SucursalCliente")), + New JProperty("phone", Row("Telefono")), + New JProperty("email", Row("Correo")), + New JProperty("address", Row("Direccion")), + New JProperty("int_number", Row("NoInt")), + New JProperty("ext_number", Row("NoExt")), + New JProperty("street", Row("Direccion")), + New JProperty("between_streets", ""), + New JProperty("city", Row("Localidad")), + New JProperty("state", Row("Estado")), + New JProperty("zip_code", Row("CP")), + New JProperty("reference", ""), + New JProperty("Branch_office_code", ""), + New JProperty("Branch_office_name", ""), + New JProperty("Branch_office_id", "") + ) + data.Add(objSucursal) + Next + Catch ex As Exception + TieneError = True + RespuestaError.message = "Ocurrió un error al obtener la sucursales" + End Try + DtSucursales.Dispose() + Else + TieneError = True + End If + + If data.Count > 0 Then + Respuesta.data = data + End If + End If + + If TieneError Then + Return JsonConvert.SerializeObject(RespuestaError) + Else + Return JsonConvert.SerializeObject(Respuesta) + End If + End Function + + Private Function ValidarCarrito(ByVal context As HttpContext) As String + Dim Respuesta As New FidelityAppsResponse + Dim RespuestaError As New FidelityAppsResponseError + Dim TieneError As Boolean = False + + + If TieneError Then + Return JsonConvert.SerializeObject(RespuestaError) + Else + Return JsonConvert.SerializeObject(Respuesta) + End If + End Function + + Private Function RegistrarPedido(ByVal context As HttpContext) As String + Dim Respuesta As New FidelityAppsResponse + Dim RespuestaError As New FidelityAppsResponseError + Dim TieneError As Boolean = False + + + If TieneError Then + Return JsonConvert.SerializeObject(RespuestaError) + Else + Return JsonConvert.SerializeObject(Respuesta) + End If + End Function + + Private Function RegistrarCotizacion(ByVal context As HttpContext) As String + Dim Respuesta As New FidelityAppsResponse + Dim RespuestaError As New FidelityAppsResponseError + Dim TieneError As Boolean = False + + + If TieneError Then + Return JsonConvert.SerializeObject(RespuestaError) + Else + Return JsonConvert.SerializeObject(Respuesta) + End If + End Function + + Private Function GetSucursales(ByVal context As HttpContext) As String + Dim Respuesta As New FidelityAppsResponse + Dim RespuestaError As New FidelityAppsResponseError + Dim TieneError As Boolean = False + + Dim data As New JArray + + Dim DtSucursales As DataTable = GlobalDTSubProyectos(-1, CveEmpresa:=CveEmpresa).FiltraParametros("EsSucursal = True") + If DtSucursales IsNot Nothing Then + Try + For Each Row As DataRow In DtSucursales.Rows + Dim objSucursal As New JObject( + New JProperty("code", Row("CveSubProyecto")), + New JProperty("name", Row("SubProyecto")), + New JProperty("street", Row("Calle")), + New JProperty("no_ext", Row("NoExt")), + New JProperty("no_int", Row("NoInt")), + New JProperty("zip", Row("CP")), + New JProperty("neighborhood", Row("Colonia")), + New JProperty("city", Row("Municipio")), + New JProperty("phone", Row("Telefono")), + New JProperty("email", Row("Correo")), + New JProperty("geo_lat", ""), + New JProperty("geo_lng", ""), + New JProperty("image", "") + ) + data.Add(objSucursal) + Next + Catch ex As Exception + TieneError = True + RespuestaError.message = "Ocurrió un error al obtener la sucursales" + End Try + DtSucursales.Dispose() + Else + TieneError = True + End If + + If data.Count > 0 Then + Respuesta.data = data + End If + + If TieneError Then + Return JsonConvert.SerializeObject(RespuestaError) + Else + Return JsonConvert.SerializeObject(Respuesta) + End If + End Function + Private Function GetExistencia(ByVal context As HttpContext, Codigo As String) As String Dim respuesta = New With {.isSuccess = False, .message = "", .products = New JArray} If String.IsNullOrEmpty(Codigo) OrElse Codigo = "-1" Then respuesta.message = "el parametro 'Sku' es invalido" - respuesta.isSuccess = False + Respuesta.isSuccess = False Return JsonConvert.SerializeObject(respuesta) End If @@ -735,4 +1029,23 @@ Public Class E141 : Implements IHttpHandler End If End Function + Private Class FidelityAppsResponse + Public Property status As String + Public Property data As Object + End Class + + Private Class FidelityAppsResponseError + 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 FidelityAppsResponseError + Public Property StatusCode As Integer + End Class + End Class \ No newline at end of file