Correciones Tareas Programdas E141

This commit is contained in:
2020-12-04 21:20:15 -07:00
parent 5325fa0462
commit b4b91e3dea

View File

@@ -1,14 +1,13 @@
<%@ WebHandler Language="VB" Class="E141" %> <%@ WebHandler Language="VB" Class="E141" %>
Imports System Option Strict On
Imports System.Web Option Explicit On
Imports System.Data Imports System.Data
Imports System.Net.Http Imports System.Net.Http
Imports System.Net
Imports Newtonsoft.Json Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq Imports Newtonsoft.Json.Linq
Imports Extensiones Imports Extensiones
Imports System.IO
Public Class E141 : Implements IHttpHandler Public Class E141 : Implements IHttpHandler
@@ -50,7 +49,7 @@ Public Class E141 : Implements IHttpHandler
Public ReadOnly Property TokenAutorizacion As String Public ReadOnly Property TokenAutorizacion As String
Get Get
If EsProduccion Then If EsProduccion Then
Return "$2y$10$AGCCDbEoGEpd0ys89fuBXedtWlunwUeLRlo4MdeLyxDvJF4GwpL7a" Return "$2y$10$8Mk3YeIkJESilIwD.U5dG.SsA4JloycJKbaGls8TNEw/AKBE1qane"
Else Else
Return "$2y$10$8Mk3YeIkJESilIwD.U5dG.SsA4JloycJKbaGls8TNEw/AKBE1qane" Return "$2y$10$8Mk3YeIkJESilIwD.U5dG.SsA4JloycJKbaGls8TNEw/AKBE1qane"
End If End If
@@ -60,7 +59,7 @@ Public Class E141 : Implements IHttpHandler
Public ReadOnly Property URLBase As String Public ReadOnly Property URLBase As String
Get Get
If EsProduccion Then If EsProduccion Then
Return "https://services.fidelityapps.mx/" Return "https://econollantas-pruebas.fidelityapps.mx"
Else Else
Return "https://econollantas-pruebas.fidelityapps.mx/" Return "https://econollantas-pruebas.fidelityapps.mx/"
End If End If
@@ -68,11 +67,11 @@ Public Class E141 : Implements IHttpHandler
End Property End Property
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "application/json" Dim retorno As String = ""
Dim retorno As String = "no se encontro la variable"
Try Try
Dim Params = If(context.Request("P") = Nothing, "", context.Request("P").ToString()) Dim Params = If(context.Request("P") = Nothing, "", context.Request("P").ToString())
'DESCARGA DEL PDF
If Not String.IsNullOrEmpty(Params) Then If Not String.IsNullOrEmpty(Params) Then
Dim UrlParametros As Hashtable = Parametros(Decripta(Params)) Dim UrlParametros As Hashtable = Parametros(Decripta(Params))
@@ -80,41 +79,54 @@ Public Class E141 : Implements IHttpHandler
If Not String.IsNullOrEmpty(File) Then If Not String.IsNullOrEmpty(File) Then
ImprimirbytesPDF(context, DescargarFactura(File), File) ImprimirbytesPDF(context, DescargarFactura(File), File)
End If End If
Else
End If Dim Metodo = If(context.Request("Metodo") = Nothing, "", context.Request("Metodo").ToString())
'OBTENER EXISTENCIAS
If Metodo = "GetExistencia" Then
context.Response.ContentType = "application/json"
Dim Metodo = If(context.Request("Metodo") = Nothing, "", context.Request("Metodo").ToString()) Dim Codigo = If(context.Request("Sku") = Nothing, "", context.Request("Sku").ToString())
retorno = GetExistencia(context, Codigo)
Dim FiltrarTodos = If(context.Request("FiltrarTodos") = Nothing, "", context.Request("FiltrarTodos").ToString()) Else
context.Response.ContentType = "text/html"
If Metodo = "CrearActualizarProductos" Then Dim FiltrarTodos = If(context.Request("FiltrarTodos") = Nothing, "", context.Request("FiltrarTodos").ToString())
retorno = CrearActualizarProductos(If(FiltrarTodos = "1", True, False))
End If
If Metodo = "ActualizarPrecios" Then Dim respuesta As Boolean = False
retorno = ActualizarPrecios(If(FiltrarTodos = "1", True, False))
End If
If Metodo = "ActualizarExistencias" Then 'TAREAS PROGRAMADAS
retorno = ActualizarExistencias(If(FiltrarTodos = "1", True, False)) Select Case Metodo
End If Case "CrearActualizarProductos"
respuesta = CrearActualizarProductos(If(FiltrarTodos = "1", True, False))
Case "ActualizarPrecios"
respuesta = ActualizarPrecios(If(FiltrarTodos = "1", True, False))
Case "ActualizarExistencias"
respuesta = ActualizarExistencias()
Case "ActualizarVendedores"
respuesta = ActualizarVendedores(If(FiltrarTodos = "1", True, False))
End Select
If Metodo = "ActualizarVendedores" Then If respuesta Then
retorno = ActualizarVendedores(If(FiltrarTodos = "1", True, False)) retorno = "True"
End If Else
retorno = "False"
End If
End If
context.Response.Write(retorno)
If Metodo = "GetExistencia" Then
Dim Codigo = If(context.Request("Sku") = Nothing, "", context.Request("Sku").ToString())
retorno = GetExistencia(context, Codigo)
End If End If
Catch ex As Exception Catch ex As Exception
context.Response.ContentType = "text/html"
retorno = ex.ToString() retorno = ex.ToString()
ex.GuardaLog(ex.ToString()) ex.GuardaLog(ex.ToString())
context.Response.Write(retorno)
End Try End Try
context.Response.Write(retorno)
End Sub End Sub
Private Function GetExistencia(ByVal context As HttpContext, Codigo As String) As String Private Function GetExistencia(ByVal context As HttpContext, Codigo As String) As String
@@ -157,17 +169,23 @@ Public Class E141 : Implements IHttpHandler
Using ObjPS As New ClsProductosServicios() Using ObjPS As New ClsProductosServicios()
If ObjPS.GetExistenciasEmpresa(Codigo, CveEmpresa) Then If ObjPS.GetExistenciasEmpresa(Codigo, CveEmpresa) Then
Dim ArrayPS As New JArray Dim ArrayPS As New JArray
For Each RowPS As DataRow In ObjPS.DtResultados.Rows If ObjPS.DtResultados.Rows.Count > 0 Then
Dim producto As New JObject( For Each RowPS As DataRow In ObjPS.DtResultados.Rows
New JProperty("sku", RowPS("Codigo")), Dim producto As New JObject(
New JProperty("stock", RowPS("Existencia")) New JProperty("sku", RowPS("Codigo")),
) New JProperty("stock", RowPS("Existencia"))
ArrayPS.Add(producto) )
Next ArrayPS.Add(producto)
Next
respuesta.isSuccess = True
respuesta.message = "La existencia se obtuvo correctamente"
Else
respuesta.isSuccess = True
respuesta.message = "No se encontro ningun producto"
End If
respuesta.products = ArrayPS respuesta.products = ArrayPS
respuesta.isSuccess = True
respuesta.message = "La existencia se obtuvo correctamente"
Else Else
respuesta.isSuccess = False respuesta.isSuccess = False
respuesta.message = "Ocurrió un error al obtener la existencia" respuesta.message = "Ocurrió un error al obtener la existencia"
@@ -182,6 +200,7 @@ Public Class E141 : Implements IHttpHandler
Dim Retorno As String = "{}" Dim Retorno As String = "{}"
Try Try
Using httpClient = New HttpClient() Using httpClient = New HttpClient()
httpClient.Timeout = New TimeSpan(0, 5, 0)
Using request = New HttpRequestMessage(HttpMethod.Post, New Uri(url)) Using request = New HttpRequestMessage(HttpMethod.Post, New Uri(url))
request.Headers.Add("token", TokenAutorizacion) request.Headers.Add("token", TokenAutorizacion)
@@ -233,12 +252,10 @@ Public Class E141 : Implements IHttpHandler
' Return Retorno ' Return Retorno
'End Function 'End Function
#Region "C R E A C I Ó N / A C T U A L I Z A C I Ó N - P R O D U C T O S" #Region "C R E A C I Ó N / A C T U A L I Z A C I Ó N - P R O D U C T O S"
Private Function CrearActualizarProductos(Todos As Boolean) As String
Return EnviarInformacion($"{Me.URLBase}api/services/products/import", GetProductosModificados(Todos))
End Function
Private Function GetProductosModificados(Todos As Boolean) As String Private Function CrearActualizarProductos(Todos As Boolean) As Boolean
Dim Retorno As String = "{}" Dim Retorno As Boolean = False
Dim CountPeticiones As Integer = 0, CountPeticionesExitosas As Integer = 0
Dim DtPS As DataTable = Nothing Dim DtPS As DataTable = Nothing
Using ObjPS As New ClsProductosServicios Using ObjPS As New ClsProductosServicios
@@ -250,59 +267,124 @@ Public Class E141 : Implements IHttpHandler
If DtPS IsNot Nothing Then If DtPS IsNot Nothing Then
Dim ArrayPS As New JArray If DtPS.Rows.Count > 0 AndAlso DtPS.Rows.Count = 1 Then
For Each RowPS As DataRow In DtPS.Rows Dim Dt As DataTable = DtPS.FiltraPorIndices(0, 0)
Dim payload = New JObject()
Dim PSData As String() = RowPS("PS").ToString().Split(" ".ToCharArray()) payload.Add(New JProperty("products", GenerarPayloadProductos(Dt)))
Dim Measurement As String = "", Size As String = "", alt_code As String = "" Dim resp = EnviarInformacion($"{Me.URLBase}api/services/products/import", payload.ToString())
If PSData.Length > 0 Then CountPeticiones += 1
Measurement = PSData(0)
If IsValidJson(resp) Then
Dim respuesta = JObject.Parse(resp)
If respuesta("status").ToString() = "success" Then
CountPeticionesExitosas += 1
End If
End If End If
If PSData.Length > 1 Then Else
Size = PSData(1) Dim TotalIndex As Integer = DtPS.Rows.Count - 1
End If Dim TotaRegistrosPorPeticion As Integer = 500
Dim PrevIndex As Integer = 0
Dim CurrentIndex As Integer = 0
alt_code = String.Concat(Measurement, Size) While PrevIndex < TotalIndex
CurrentIndex += TotaRegistrosPorPeticion
Dim producto As New JObject( If CurrentIndex > TotalIndex Then
New JProperty("clasificacion", RowPS("Clasificacion")), CurrentIndex = TotalIndex
New JProperty("category", "Prod"), End If
New JProperty("pza", RowPS("Unidad")),
New JProperty("sku", RowPS("Codigo")),
New JProperty("name", RowPS("PS")),
New JProperty("measurement", Measurement),
New JProperty("size", Size),
New JProperty("brand", RowPS("Marca")),
New JProperty("price", RowPS("Precio")),
New JProperty("active", RowPS("Estado")),
New JProperty("user", RowPS("UsuarioModifico")),
New JProperty("alt_code", alt_code),
New JProperty("short_description", ""),
New JProperty("description", ""),
New JProperty("currency", 1),
New JProperty("promo_price", RowPS("PrecioPromocion"))
)
ArrayPS.Add(producto) Dim Dt As DataTable = DtPS.FiltraPorIndices(PrevIndex, CurrentIndex)
Next Dim payload = New JObject()
Retorno = New JObject(New JProperty("products", ArrayPS)).ToString()
'If Not EsProduccion Then
' payload.Add(New JProperty("deleteNotIncluded", True))
'End If
payload.Add(New JProperty("products", GenerarPayloadProductos(Dt)))
PrevIndex = CurrentIndex
Dim resp = EnviarInformacion($"{Me.URLBase}api/services/products/import", payload.ToString())
CountPeticiones += 1
If IsValidJson(resp) Then
Dim respuesta = JObject.Parse(resp)
If respuesta("status").ToString() = "success" Then
CountPeticionesExitosas += 1
End If
End If
Threading.Thread.Sleep(3000)
End While
End If
End If
If CountPeticionesExitosas = CountPeticiones Then
Retorno = True
End If End If
Return Retorno Return Retorno
End Function End Function
Private Function GenerarPayloadProductos(DtPS As DataTable) As JArray
Dim ArrayPS As New JArray
For Each RowPS As DataRow In DtPS.Rows
Dim PSData As String() = RowPS("PS").ToString().Split(" ".ToCharArray())
Dim Measurement As String = "", Size As String = "", alt_code As String = ""
If PSData.Length > 0 Then
Measurement = PSData(0)
End If
If PSData.Length > 1 Then
Size = PSData(1)
End If
alt_code = String.Concat(Measurement, Size)
Dim producto As New JObject(
New JProperty("clasificacion", RowPS("Clasificacion")),
New JProperty("category", "Prod"),
New JProperty("pza", RowPS("Unidad")),
New JProperty("sku", RowPS("Codigo")),
New JProperty("name", RowPS("PS")),
New JProperty("measurement", Measurement),
New JProperty("size", Size),
New JProperty("brand", RowPS("Marca")),
New JProperty("price", RowPS("Precio")),
New JProperty("active", RowPS("Estado")),
New JProperty("user", RowPS("UsuarioModifico")),
New JProperty("alt_code", alt_code),
New JProperty("short_description", ""),
New JProperty("description", ""),
New JProperty("currency", 1),
New JProperty("promo_price", RowPS("PrecioPromocion"))
)
ArrayPS.Add(producto)
Next
Return ArrayPS
End Function
#End Region #End Region
#Region "A C T U A L I Z A R - P R E C I O S" #Region "A C T U A L I Z A R - P R E C I O S"
Private Function ActualizarPrecios(Todos As Boolean) As String 'Private Function ActualizarPrecios(Todos As Boolean) As String
Return EnviarInformacion($"{Me.URLBase}api/services/products/price-import", GetPreciosModificados(Todos)) ' Return EnviarInformacion($"{Me.URLBase}api/services/products/price-import", GetPreciosModificados(Todos))
End Function 'End Function
Private Function GetPreciosModificados(Todos As Boolean) As String Private Function ActualizarPrecios(Todos As Boolean) As Boolean
Dim Retorno As String = "{}" Dim Retorno As Boolean = False
Dim CountPeticiones As Integer = 0, CountPeticionesExitosas As Integer = 0
Dim DtPS As DataTable = Nothing Dim DtPS As DataTable = Nothing
Using ObjPS As New ClsProductosServicios Using ObjPS As New ClsProductosServicios
@@ -314,32 +396,90 @@ Public Class E141 : Implements IHttpHandler
If DtPS IsNot Nothing Then If DtPS IsNot Nothing Then
Dim ArrayPS As New JArray If DtPS.Rows.Count > 0 AndAlso DtPS.Rows.Count = 1 Then
For Each RowPS As DataRow In DtPS.Rows Dim Dt As DataTable = DtPS.FiltraPorIndices(0, 0)
Dim payload = New JObject()
Dim producto As New JObject( payload.Add(New JProperty("products", GenerarPayloadPrecios(Dt)))
New JProperty("sku", RowPS("Codigo")),
New JProperty("price", RowPS("Precio")),
New JProperty("promo_price", RowPS("PrecioPromocion")) Dim resp = EnviarInformacion($"{Me.URLBase}api/services/products/price-import", payload.ToString())
)
CountPeticiones += 1
If IsValidJson(resp) Then
Dim respuesta = JObject.Parse(resp)
If respuesta("status").ToString() = "success" Then
CountPeticionesExitosas += 1
End If
End If
Else
Dim TotalIndex As Integer = DtPS.Rows.Count - 1
Dim TotaRegistrosPorPeticion As Integer = 500
Dim PrevIndex As Integer = 0
Dim CurrentIndex As Integer = 0
While PrevIndex < TotalIndex
CurrentIndex += TotaRegistrosPorPeticion
If CurrentIndex > TotalIndex Then
CurrentIndex = TotalIndex
End If
Dim Dt As DataTable = DtPS.FiltraPorIndices(PrevIndex, CurrentIndex)
Dim payload = New JObject()
payload.Add(New JProperty("products", GenerarPayloadPrecios(Dt)))
PrevIndex = CurrentIndex
Dim resp = EnviarInformacion($"{Me.URLBase}api/services/products/price-import", payload.ToString())
CountPeticiones += 1
If IsValidJson(resp) Then
Dim respuesta = JObject.Parse(resp)
If respuesta("status").ToString() = "success" Then
CountPeticionesExitosas += 1
End If
End If
Threading.Thread.Sleep(3000)
End While
End If
ArrayPS.Add(producto)
Next
Retorno = New JObject(New JProperty("products", ArrayPS)).ToString()
End If End If
Return Retorno Return Retorno
End Function End Function
Private Function GenerarPayloadPrecios(DtPS As DataTable) As JArray
Dim ArrayPS As New JArray
For Each RowPS As DataRow In DtPS.Rows
Dim producto As New JObject(
New JProperty("sku", RowPS("Codigo")),
New JProperty("price", RowPS("Precio")),
New JProperty("promo_price", RowPS("PrecioPromocion"))
)
ArrayPS.Add(producto)
Next
Return ArrayPS
End Function
#End Region #End Region
#Region "A C T U A L I Z A R - E X I S T E N C I A S" #Region "A C T U A L I Z A R - E X I S T E N C I A S"
Private Function ActualizarExistencias() As String 'Private Function ActualizarExistencias() As String
Return EnviarInformacion($"{Me.URLBase}api/services/products/stock", GetPSExistencias()) ' Return EnviarInformacion($"{Me.URLBase}api/services/products/stock", GetPSExistencias())
End Function 'End Function
Private Function GetPSExistencias() As String Private Function ActualizarExistencias() As Boolean
Dim Retorno As String = "{}" Dim Retorno As Boolean = False
Dim CountPeticiones As Integer = 0, CountPeticionesExitosas As Integer = 0
Dim DtPS As DataTable = Nothing Dim DtPS As DataTable = Nothing
Using ObjPS As New ClsProductosServicios Using ObjPS As New ClsProductosServicios
@@ -350,34 +490,90 @@ Public Class E141 : Implements IHttpHandler
End Using End Using
If DtPS IsNot Nothing Then If DtPS IsNot Nothing Then
If DtPS.Rows.Count > 0 AndAlso DtPS.Rows.Count = 1 Then
Dim ArrayPS As New JArray Dim Dt As DataTable = DtPS.FiltraPorIndices(0, 0)
For Each RowPS As DataRow In DtPS.Rows Dim payload = New JObject()
Dim producto As New JObject( payload.Add(New JProperty("stock", GenerarPayloadExistencias(Dt)))
New JProperty("sku", RowPS("Codigo")),
New JProperty("branch_office_code", "01"),
New JProperty("stock", RowPS("Existencia"))
)
ArrayPS.Add(producto) Dim resp = EnviarInformacion($"{Me.URLBase}api/services/products/stock", payload.ToString())
Next
Retorno = New JObject(New JProperty("stock", ArrayPS)).ToString() CountPeticiones += 1
If IsValidJson(resp) Then
Dim respuesta = JObject.Parse(resp)
If respuesta("status").ToString() = "success" Then
CountPeticionesExitosas += 1
End If
End If
Else
Dim TotalIndex As Integer = DtPS.Rows.Count - 1
Dim TotaRegistrosPorPeticion As Integer = 500
Dim PrevIndex As Integer = 0
Dim CurrentIndex As Integer = 0
While PrevIndex < TotalIndex
CurrentIndex += TotaRegistrosPorPeticion
If CurrentIndex > TotalIndex Then
CurrentIndex = TotalIndex
End If
Dim Dt As DataTable = DtPS.FiltraPorIndices(PrevIndex, CurrentIndex)
Dim payload = New JObject()
payload.Add(New JProperty("stock", GenerarPayloadExistencias(Dt)))
PrevIndex = CurrentIndex
Dim resp = EnviarInformacion($"{Me.URLBase}api/services/products/stock", payload.ToString())
CountPeticiones += 1
If IsValidJson(resp) Then
Dim respuesta = JObject.Parse(resp)
If respuesta("status").ToString() = "success" Then
CountPeticionesExitosas += 1
End If
End If
Threading.Thread.Sleep(3000)
End While
End If
End If
If CountPeticionesExitosas = CountPeticiones Then
Retorno = True
End If End If
Return Retorno Return Retorno
End Function End Function
Private Function GenerarPayloadExistencias(DtPS As DataTable) As JArray
Dim ArrayPS As New JArray
For Each RowPS As DataRow In DtPS.Rows
Dim producto As New JObject(
New JProperty("sku", RowPS("Codigo")),
New JProperty("branch_office_code", "01"),
New JProperty("stock", RowPS("Existencia").ToString())
)
ArrayPS.Add(producto)
Next
Return ArrayPS
End Function
#End Region #End Region
#Region "A C T U A L I Z A R - E X I S T E N C I A S" #Region "A C T U A L I Z A R - E X I S T E N C I A S"
Private Function ActualizarVendedores(Todos As Boolean) As String Private Function ActualizarVendedores(Todos As Boolean) As Boolean
Return EnviarInformacion($"{Me.URLBase}api/v1/sellers/import", GetVendedores(Todos)) Dim Retorno As Boolean = False
End Function Dim CountPeticiones As Integer = 0, CountPeticionesExitosas As Integer = 0
Private Function GetVendedores(Todos As Boolean) As String
Dim Retorno As String = "{}"
Dim DtVendedores As DataTable = Nothing Dim DtVendedores As DataTable = Nothing
Using ObjPS As New ClsProductosServicios Using ObjPS As New ClsProductosServicios
@@ -410,6 +606,11 @@ Public Class E141 : Implements IHttpHandler
Apellido = String.Concat(VendedorData(0), " ", VendedorData(1)) Apellido = String.Concat(VendedorData(0), " ", VendedorData(1))
End Select End Select
If VendedorData.Length > 4 Then
Nombre = String.Concat(VendedorData(VendedorData.Length - 1), " ", VendedorData(VendedorData.Length))
Apellido = String.Concat(VendedorData(0), " ", VendedorData(1))
End If
Dim vendedor As New JObject( Dim vendedor As New JObject(
New JProperty("email", RowVendedor("Correo")), New JProperty("email", RowVendedor("Correo")),
New JProperty("password", RowVendedor("Correo")), New JProperty("password", RowVendedor("Correo")),
@@ -426,7 +627,19 @@ Public Class E141 : Implements IHttpHandler
ArrayVendedores.Add(vendedor) ArrayVendedores.Add(vendedor)
Next Next
Retorno = New JObject(New JProperty("sellers", ArrayVendedores)).ToString()
Dim payload = New JObject(New JProperty("sellers", ArrayVendedores)).ToString()
Dim resp = EnviarInformacion($"{Me.URLBase}api/v1/sellers/import", payload)
CountPeticiones += 1
If IsValidJson(resp) Then
Dim respuesta = JObject.Parse(resp)
If respuesta("status").ToString() = "success" Then
CountPeticionesExitosas += 1
End If
End If
End If End If
Return Retorno Return Retorno
@@ -495,4 +708,28 @@ Public Class E141 : Implements IHttpHandler
Return URLParametros Return URLParametros
End Function 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 End Class