Se agregaron los microservicios_v2
This commit is contained in:
269
api/customs/tasks/microservice_v2.py
Normal file
269
api/customs/tasks/microservice_v2.py
Normal file
@@ -0,0 +1,269 @@
|
||||
|
||||
from celery import shared_task, group
|
||||
from api.customs.models import *
|
||||
from api.customs.serializers import PedimentoSerializer
|
||||
from api.vucem.models import *
|
||||
import requests
|
||||
from config.settings import SERVICE_API_URL_V2
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
def credenciales_to_dict(credenciales):
|
||||
if not credenciales:
|
||||
return {}
|
||||
return {
|
||||
"id": str(credenciales.id),
|
||||
"user": credenciales.usuario,
|
||||
"password": credenciales.password,
|
||||
"efirma": credenciales.efirma,
|
||||
"key": credenciales.key.url if credenciales.key else None,
|
||||
"cer": credenciales.cer.url if credenciales.cer else None,
|
||||
"is_active": credenciales.is_active,
|
||||
"organizacion": str(credenciales.organizacion.id) if credenciales.organizacion else None,
|
||||
}
|
||||
|
||||
def pedimento_to_dict(pedimento):
|
||||
return {
|
||||
"id": str(pedimento.id),
|
||||
"pedimento": str(pedimento.pedimento),
|
||||
"pedimento_app": str(pedimento.pedimento_app),
|
||||
"aduana": str(pedimento.aduana),
|
||||
"patente": str(pedimento.patente),
|
||||
"organizacion": str(pedimento.organizacion.id), # nunca None
|
||||
"regimen": str(pedimento.regimen or ""),
|
||||
"clave_pedimento": str(pedimento.clave_pedimento or ""),
|
||||
"numero_operacion": str(pedimento.numero_operacion or "")
|
||||
}
|
||||
|
||||
def cove_to_dict(cove):
|
||||
return {
|
||||
"id": cove.id,
|
||||
"cove": str(cove.numero_cove),
|
||||
}
|
||||
|
||||
def edoc_to_dict(edoc):
|
||||
return {
|
||||
"id": edoc.id,
|
||||
"numero_edocument": str(edoc.numero_edocument),
|
||||
}
|
||||
|
||||
def partida_to_dict(partida):
|
||||
return {
|
||||
"id": partida.id,
|
||||
"numero": partida.numero_partida,
|
||||
}
|
||||
|
||||
@shared_task
|
||||
def procesar_pedimentos_completos(organizacion_id):
|
||||
pedimentos = Pedimento.objects.filter(organizacion_id=organizacion_id)
|
||||
respuestas = []
|
||||
for pedimento in pedimentos[:10]:
|
||||
if not pedimento.documents.filter(document_type=2).exists(): # Tipo 2: Pedimento Completo
|
||||
# Convertir el pedimento a JSON usando el serializer
|
||||
pedimento_dict = pedimento_to_dict(pedimento)
|
||||
credenciales = Vucem.objects.filter(id=CredencialesImportador.objects.filter(rfc=pedimento.contribuyente).first().vucem.id).first()
|
||||
|
||||
credenciales_dict = credenciales_to_dict(credenciales)
|
||||
|
||||
payload = {
|
||||
"pedimento": pedimento_dict,
|
||||
"credencial": credenciales_dict
|
||||
}
|
||||
|
||||
|
||||
response = requests.post(
|
||||
f"{SERVICE_API_URL_V2}/services/pedimento_completo",
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
# Aquí puedes continuar con el resto de tu lógica
|
||||
|
||||
respuestas.append(response.json())
|
||||
return respuestas
|
||||
|
||||
@shared_task
|
||||
def procesar_remesas(organizacion_id):
|
||||
pedimentos = Pedimento.objects.filter(organizacion_id=organizacion_id)
|
||||
respuestas = []
|
||||
for pedimento in pedimentos:
|
||||
if not pedimento.documents.filter(document_type=3).exists(): # Tipo 3: Remesa
|
||||
# Convertir el pedimento a JSON usando el serializer
|
||||
pedimento_dict = pedimento_to_dict(pedimento)
|
||||
credenciales = Vucem.objects.filter(id=CredencialesImportador.objects.filter(rfc=pedimento.contribuyente).first().vucem.id).first()
|
||||
|
||||
credenciales_dict = credenciales_to_dict(credenciales)
|
||||
|
||||
payload = {
|
||||
"pedimento": pedimento_dict,
|
||||
"credencial": credenciales_dict
|
||||
}
|
||||
|
||||
|
||||
response = requests.post(
|
||||
f"{SERVICE_API_URL_V2}/services/remesas",
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
# Aquí puedes continuar con el resto de tu lógica
|
||||
|
||||
respuestas.append(response.json())
|
||||
return respuestas
|
||||
|
||||
@shared_task
|
||||
def procesar_coves(organizacion_id):
|
||||
pedimentos = Pedimento.objects.filter(
|
||||
organizacion_id=organizacion_id,
|
||||
coves__isnull=False
|
||||
).distinct()
|
||||
respuestas = []
|
||||
for pedimento in pedimentos[:10]:
|
||||
if pedimento.coves.filter(cove_descargado=False).exists(): # Tipo 3: Remesa
|
||||
|
||||
# Convertir el pedimento a JSON usando el serializer
|
||||
pedimento_dict = pedimento_to_dict(pedimento)
|
||||
credenciales = Vucem.objects.filter(id=CredencialesImportador.objects.filter(rfc=pedimento.contribuyente).first().vucem.id).first()
|
||||
|
||||
credenciales_dict = credenciales_to_dict(credenciales)
|
||||
|
||||
payload = {
|
||||
"coves": [cove_to_dict(cove) for cove in pedimento.coves.filter(cove_descargado=False)],
|
||||
"pedimento": pedimento_dict,
|
||||
"credencial": credenciales_dict
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
f"{SERVICE_API_URL_V2}/services/all/coves",
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
# Aquí puedes continuar con el resto de tu lógica
|
||||
|
||||
respuestas.append(response.json())
|
||||
return respuestas
|
||||
|
||||
@shared_task
|
||||
def procesar_acuse_coves(organizacion_id):
|
||||
pedimentos = Pedimento.objects.filter(
|
||||
organizacion_id=organizacion_id,
|
||||
coves__isnull=False
|
||||
).distinct()
|
||||
respuestas = []
|
||||
for pedimento in pedimentos[10:20]:
|
||||
if pedimento.coves.filter(acuse_cove_descargado=False).exists(): # Tipo 3: Remesa
|
||||
|
||||
# Convertir el pedimento a JSON usando el serializer
|
||||
pedimento_dict = pedimento_to_dict(pedimento)
|
||||
credenciales = Vucem.objects.filter(id=CredencialesImportador.objects.filter(rfc=pedimento.contribuyente).first().vucem.id).first()
|
||||
|
||||
credenciales_dict = credenciales_to_dict(credenciales)
|
||||
|
||||
payload = {
|
||||
"coves": [cove_to_dict(cove) for cove in pedimento.coves.filter(acuse_cove_descargado=False)],
|
||||
"pedimento": pedimento_dict,
|
||||
"credencial": credenciales_dict
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
f"{SERVICE_API_URL_V2}/services/all/acuse/cove/",
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
# Aquí puedes continuar con el resto de tu lógica
|
||||
|
||||
respuestas.append(response.json())
|
||||
return respuestas
|
||||
|
||||
@shared_task
|
||||
def procesar_acuses(organizacion_id):
|
||||
pedimentos = Pedimento.objects.filter(
|
||||
organizacion_id=organizacion_id,
|
||||
documentos__isnull=False
|
||||
).distinct()
|
||||
respuestas = []
|
||||
for pedimento in pedimentos[:10]:
|
||||
if pedimento.documentos.filter(acuse_descargado=False).exists(): # Tipo 3: Remesa
|
||||
|
||||
# Convertir el pedimento a JSON usando el serializer
|
||||
pedimento_dict = pedimento_to_dict(pedimento)
|
||||
credenciales = Vucem.objects.filter(id=CredencialesImportador.objects.filter(rfc=pedimento.contribuyente).first().vucem.id).first()
|
||||
|
||||
credenciales_dict = credenciales_to_dict(credenciales)
|
||||
|
||||
payload = {
|
||||
"edocs": [edoc_to_dict(edoc) for edoc in pedimento.documentos.filter(acuse_descargado=False)],
|
||||
"pedimento": pedimento_dict,
|
||||
"credencial": credenciales_dict
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
f"{SERVICE_API_URL_V2}/services/all/acuse/pedimento/",
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
# Aquí puedes continuar con el resto de tu lógica
|
||||
|
||||
respuestas.append(response.json())
|
||||
return respuestas
|
||||
|
||||
@shared_task
|
||||
def procesar_edocs(organizacion_id):
|
||||
pedimentos = Pedimento.objects.filter(
|
||||
organizacion_id=organizacion_id,
|
||||
documentos__isnull=False
|
||||
).distinct()
|
||||
respuestas = []
|
||||
for pedimento in pedimentos[:10]:
|
||||
if pedimento.documentos.filter(edocument_descargado=False).exists(): # Tipo 3: Remesa
|
||||
|
||||
# Convertir el pedimento a JSON usando el serializer
|
||||
pedimento_dict = pedimento_to_dict(pedimento)
|
||||
credenciales = Vucem.objects.filter(id=CredencialesImportador.objects.filter(rfc=pedimento.contribuyente).first().vucem.id).first()
|
||||
|
||||
credenciales_dict = credenciales_to_dict(credenciales)
|
||||
|
||||
payload = {
|
||||
"edocs": [edoc_to_dict(edoc) for edoc in pedimento.documentos.filter(edocument_descargado=False)],
|
||||
"pedimento": pedimento_dict,
|
||||
"credencial": credenciales_dict
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
f"{SERVICE_API_URL_V2}/services/all/acuse/pedimento/",
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
# Aquí puedes continuar con el resto de tu lógica
|
||||
|
||||
respuestas.append(response.json())
|
||||
return respuestas
|
||||
|
||||
@shared_task
|
||||
def procesar_partidas(organizacion_id):
|
||||
pedimentos = Pedimento.objects.filter(
|
||||
organizacion_id=organizacion_id,
|
||||
partidas__isnull=False
|
||||
).distinct()
|
||||
respuestas = []
|
||||
for pedimento in pedimentos[:10]:
|
||||
if pedimento.partidas.filter(descargado=False).exists(): # Tipo 4: Partidas
|
||||
# Convertir el pedimento a JSON usando el serializer
|
||||
pedimento_dict = pedimento_to_dict(pedimento)
|
||||
credenciales = Vucem.objects.filter(id=CredencialesImportador.objects.filter(rfc=pedimento.contribuyente).first().vucem.id).first()
|
||||
|
||||
credenciales_dict = credenciales_to_dict(credenciales)
|
||||
|
||||
payload = {
|
||||
"partidas": [partida_to_dict(partida) for partida in pedimento.partidas.filter(descargado=False)],
|
||||
"pedimento": pedimento_dict,
|
||||
"credencial": credenciales_dict
|
||||
}
|
||||
|
||||
response = requests.post(
|
||||
f"{SERVICE_API_URL_V2}/services/all/partidas/",
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
# Aquí puedes continuar con el resto de tu lógica
|
||||
|
||||
respuestas.append(response.json())
|
||||
return respuestas
|
||||
Reference in New Issue
Block a user