214 lines
9.0 KiB
Python
214 lines
9.0 KiB
Python
|
|
from celery import shared_task, group
|
|
from api.customs.models import ProcesamientoPedimento
|
|
import requests
|
|
from config.settings import SERVICE_API_URL
|
|
from datetime import datetime
|
|
|
|
|
|
# ===================
|
|
# Pedimento Completo
|
|
# ===================
|
|
@shared_task
|
|
def procesar_pedimento_completo_individual(pedimento_id, organizacion_id):
|
|
response = requests.post(
|
|
f"{SERVICE_API_URL}/async/services/pedimento_completo",
|
|
json={"pedimento": str(pedimento_id), "organizacion": str(organizacion_id)}
|
|
)
|
|
if response.status_code == 200:
|
|
print(f"Pedimento {pedimento_id} procesado correctamente.")
|
|
else:
|
|
print(f"Error al procesar el pedimento {pedimento_id}: {response.status_code} - {response.text}")
|
|
print(f"Disparando evento para procesamiento {pedimento_id}")
|
|
|
|
@shared_task
|
|
def ejecutar_pedimento_completo():
|
|
pendientes = ProcesamientoPedimento.objects.filter(estado=1, servicio=3)
|
|
batch_size = 20
|
|
ids = list(pendientes.values_list('pedimento_id', 'organizacion_id'))
|
|
for i in range(0, len(ids), batch_size):
|
|
batch = ids[i:i+batch_size]
|
|
job = group(procesar_pedimento_completo_individual.s(ped_id, org_id) for ped_id, org_id in batch)
|
|
job.apply_async()
|
|
# Validar horario permitido (5:00 a 22:00)
|
|
ahora = datetime.now().time()
|
|
if (ahora < datetime.strptime('05:00', '%H:%M').time()) or (ahora >= datetime.strptime('22:00', '%H:%M').time()):
|
|
print('ejecutar_pedimento_completo: fuera de horario permitido (5:00-22:00). Abortando.')
|
|
return
|
|
|
|
# ===================
|
|
# Partidas
|
|
# ===================
|
|
@shared_task
|
|
def procesar_partida_individual(pedimento_id, organizacion_id):
|
|
response = requests.post(
|
|
f"{SERVICE_API_URL}/async/services/partidas",
|
|
json={"pedimento": str(pedimento_id), "organizacion": str(organizacion_id)}
|
|
)
|
|
if response.status_code == 200:
|
|
print(f"Partidas del pedimento {pedimento_id} procesadas correctamente.")
|
|
else:
|
|
print(f"Error al procesar partidas del pedimento {pedimento_id}: {response.status_code} - {response.text}")
|
|
print(f"Disparando evento para procesamiento {pedimento_id}")
|
|
|
|
@shared_task
|
|
def ejecutar_partidas_pedimento():
|
|
pendientes = ProcesamientoPedimento.objects.filter(estado=1, servicio=4)
|
|
batch_size = 20
|
|
ids = list(pendientes.values_list('pedimento_id', 'organizacion_id'))
|
|
for i in range(0, len(ids), batch_size):
|
|
batch = ids[i:i+batch_size]
|
|
job = group(procesar_partida_individual.s(ped_id, org_id) for ped_id, org_id in batch)
|
|
job.apply_async()
|
|
# Validar horario permitido (5:00 a 22:00)
|
|
ahora = datetime.now().time()
|
|
if (ahora < datetime.strptime('05:00', '%H:%M').time()) or (ahora >= datetime.strptime('22:00', '%H:%M').time()):
|
|
print('ejecutar_partidas_pedimento: fuera de horario permitido (5:00-22:00). Abortando.')
|
|
return
|
|
|
|
# ===================
|
|
# Remesas
|
|
# ===================
|
|
@shared_task
|
|
def procesar_remesa_individual(pedimento_id, organizacion_id):
|
|
response = requests.post(
|
|
f"{SERVICE_API_URL}/async/services/remesas",
|
|
json={"pedimento": str(pedimento_id), "organizacion": str(organizacion_id)}
|
|
)
|
|
if response.status_code == 200:
|
|
print(f"Remesas del pedimento {pedimento_id} procesadas correctamente.")
|
|
else:
|
|
print(f"Error al procesar remesas del pedimento {pedimento_id}: {response.status_code} - {response.text}")
|
|
print(f"Disparando evento para procesamiento {pedimento_id}")
|
|
|
|
@shared_task
|
|
def ejecutar_remesas():
|
|
pendientes = ProcesamientoPedimento.objects.filter(estado=1, servicio=5)
|
|
batch_size = 20
|
|
ids = list(pendientes.values_list('pedimento_id', 'organizacion_id'))
|
|
for i in range(0, len(ids), batch_size):
|
|
batch = ids[i:i+batch_size]
|
|
job = group(procesar_remesa_individual.s(ped_id, org_id) for ped_id, org_id in batch)
|
|
job.apply_async()
|
|
# Validar horario permitido (5:00 a 22:00)
|
|
ahora = datetime.now().time()
|
|
if (ahora < datetime.strptime('05:00', '%H:%M').time()) or (ahora >= datetime.strptime('22:00', '%H:%M').time()):
|
|
print('ejecutar_remesas: fuera de horario permitido (5:00-22:00). Abortando.')
|
|
return
|
|
|
|
# ===================
|
|
# Acuses
|
|
# ===================
|
|
@shared_task
|
|
def procesar_acuse_individual(pedimento_id, organizacion_id):
|
|
response = requests.post(
|
|
f"{SERVICE_API_URL}/async/services/acuse",
|
|
json={"pedimento": str(pedimento_id), "organizacion": str(organizacion_id)}
|
|
)
|
|
if response.status_code == 200:
|
|
print(f"Acuses del pedimento {pedimento_id} procesadas correctamente.")
|
|
else:
|
|
print(f"Error al procesar Acuses del pedimento {pedimento_id}: {response.status_code} - {response.text}")
|
|
print(f"Disparando evento para procesamiento {pedimento_id}")
|
|
|
|
@shared_task
|
|
def ejecutar_acuse():
|
|
pendientes = ProcesamientoPedimento.objects.filter(estado=1, servicio=6)
|
|
batch_size = 20
|
|
ids = list(pendientes.values_list('pedimento_id', 'organizacion_id'))
|
|
for i in range(0, len(ids), batch_size):
|
|
batch = ids[i:i+batch_size]
|
|
job = group(procesar_acuse_individual.s(ped_id, org_id) for ped_id, org_id in batch)
|
|
job.apply_async()
|
|
# Validar horario permitido (5:00 a 22:00)
|
|
ahora = datetime.now().time()
|
|
if (ahora < datetime.strptime('05:00', '%H:%M').time()) or (ahora >= datetime.strptime('22:00', '%H:%M').time()):
|
|
print('ejecutar_acuse: fuera de horario permitido (5:00-22:00). Abortando.')
|
|
return
|
|
|
|
# ===================
|
|
# Edocuments
|
|
# ===================
|
|
@shared_task
|
|
def procesar_edoc_individual(pedimento_id, organizacion_id):
|
|
response = requests.post(
|
|
f"{SERVICE_API_URL}/async/services/edocument",
|
|
json={"pedimento": str(pedimento_id), "organizacion": str(organizacion_id)}
|
|
)
|
|
if response.status_code == 200:
|
|
print(f"Edocuments del pedimento {pedimento_id} procesadas correctamente.")
|
|
else:
|
|
print(f"Error al procesar Edocuments del pedimento {pedimento_id}: {response.status_code} - {response.text}")
|
|
print(f"Disparando evento para procesamiento {pedimento_id}")
|
|
|
|
@shared_task
|
|
def ejecutar_edocs():
|
|
pendientes = ProcesamientoPedimento.objects.filter(estado=1, servicio=7)
|
|
batch_size = 20
|
|
ids = list(pendientes.values_list('pedimento_id', 'organizacion_id'))
|
|
for i in range(0, len(ids), batch_size):
|
|
batch = ids[i:i+batch_size]
|
|
job = group(procesar_edoc_individual.s(ped_id, org_id) for ped_id, org_id in batch)
|
|
job.apply_async()
|
|
|
|
# ===================
|
|
# Coves
|
|
# ===================
|
|
@shared_task
|
|
def procesar_cove_individual(pedimento_id, organizacion_id):
|
|
response = requests.post(
|
|
f"{SERVICE_API_URL}/async/services/coves",
|
|
json={"pedimento": str(pedimento_id), "organizacion": str(organizacion_id)}
|
|
)
|
|
if response.status_code == 200:
|
|
print(f"Coves del pedimento {pedimento_id} procesadas correctamente.")
|
|
else:
|
|
print(f"Error al procesar Coves del pedimento {pedimento_id}: {response.status_code} - {response.text}")
|
|
print(f"Disparando evento para procesamiento {pedimento_id}")
|
|
|
|
@shared_task
|
|
def ejecutar_coves():
|
|
pendientes = ProcesamientoPedimento.objects.filter(estado=1, servicio=8)
|
|
batch_size = 20
|
|
ids = list(pendientes.values_list('pedimento_id', 'organizacion_id'))
|
|
for i in range(0, len(ids), batch_size):
|
|
batch = ids[i:i+batch_size]
|
|
job = group(procesar_cove_individual.s(ped_id, org_id) for ped_id, org_id in batch)
|
|
job.apply_async()
|
|
# Validar horario permitido (5:00 a 22:00)
|
|
ahora = datetime.now().time()
|
|
if (ahora < datetime.strptime('05:00', '%H:%M').time()) or (ahora >= datetime.strptime('22:00', '%H:%M').time()):
|
|
print('ejecutar_coves: fuera de horario permitido (5:00-22:00). Abortando.')
|
|
return
|
|
|
|
# ===================
|
|
# Acuse Cove
|
|
# ===================
|
|
@shared_task
|
|
def procesar_acuse_cove_individual(pedimento_id, organizacion_id):
|
|
response = requests.post(
|
|
f"{SERVICE_API_URL}/async/services/acuse-cove",
|
|
json={"pedimento": str(pedimento_id), "organizacion": str(organizacion_id)}
|
|
)
|
|
if response.status_code == 200:
|
|
print(f"Coves del pedimento {pedimento_id} procesadas correctamente.")
|
|
else:
|
|
print(f"Error al procesar Coves del pedimento {pedimento_id}: {response.status_code} - {response.text}")
|
|
print(f"Disparando evento para procesamiento {pedimento_id}")
|
|
|
|
@shared_task
|
|
def ejecutar_acuseCoves():
|
|
pendientes = ProcesamientoPedimento.objects.filter(estado=1, servicio=9)
|
|
batch_size = 20
|
|
ids = list(pendientes.values_list('pedimento_id', 'organizacion_id'))
|
|
for i in range(0, len(ids), batch_size):
|
|
batch = ids[i:i+batch_size]
|
|
job = group(procesar_acuse_cove_individual.s(ped_id, org_id) for ped_id, org_id in batch)
|
|
job.apply_async()
|
|
# Validar horario permitido (5:00 a 22:00)
|
|
ahora = datetime.now().time()
|
|
if (ahora < datetime.strptime('05:00', '%H:%M').time()) or (ahora >= datetime.strptime('22:00', '%H:%M').time()):
|
|
print('ejecutar_acuseCoves: fuera de horario permitido (5:00-22:00). Abortando.')
|
|
return
|
|
|