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