Compare commits
6 Commits
feature/bu
...
feature/Ag
| Author | SHA1 | Date | |
|---|---|---|---|
| f7fc802ec2 | |||
| 50e35992db | |||
| 9a8827bb6f | |||
| 6e0b7eaa91 | |||
| 9700d81dea | |||
| 8c842a6212 |
@@ -51,28 +51,6 @@ def auditor_descargas(pedimento, servicio, related_name, variable, mensaje):
|
|||||||
print(f"✗ No se encontró proceso de auditoría para pedimento {pedimento_id}.")
|
print(f"✗ No se encontró proceso de auditoría para pedimento {pedimento_id}.")
|
||||||
|
|
||||||
## Auditar pedimentos
|
## Auditar pedimentos
|
||||||
@shared_task
|
|
||||||
def auditar_procesamiento_remesas(organizacion_id):
|
|
||||||
pedimentos = obtener_pedimentos(organizacion_id)
|
|
||||||
|
|
||||||
for pedimento in pedimentos:
|
|
||||||
if pedimento.remesas:
|
|
||||||
# Tipo 3: Remesa
|
|
||||||
if not pedimento.documents.filter(document_type=3).exists():
|
|
||||||
ProcesamientoPedimento.objects.get_or_create(
|
|
||||||
pedimento=pedimento,
|
|
||||||
servicio_id=5, # ID del servicio de remesas
|
|
||||||
organizacion=organizacion_id
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
xml_data = extraer_coves(pedimento)
|
|
||||||
if xml_data:
|
|
||||||
for remesa in xml_data:
|
|
||||||
Cove.objects.get_or_create(
|
|
||||||
pedimento=pedimento,
|
|
||||||
numero_cove=remesa.get('remesaSA'),
|
|
||||||
organizacion=organizacion_id
|
|
||||||
)
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
def auditar_procesamiento_remesa_por_pedimento(pedimento_id):
|
def auditar_procesamiento_remesa_por_pedimento(pedimento_id):
|
||||||
@@ -259,4 +237,68 @@ def auditar_acuse(organizacion_id):
|
|||||||
variable='acuse_descargado',
|
variable='acuse_descargado',
|
||||||
mensaje='acuse'
|
mensaje='acuse'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def auditar_cove_por_pedimento(pedimento_id):
|
||||||
|
try:
|
||||||
|
from api.customs.models import Pedimento
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
auditor_descargas(
|
||||||
|
pedimento,
|
||||||
|
servicio=8,
|
||||||
|
related_name='coves',
|
||||||
|
variable='acuse_descargado',
|
||||||
|
mensaje='COVE'
|
||||||
|
)
|
||||||
|
return {'success': True, 'pedimento_id': str(pedimento_id)}
|
||||||
|
except Exception as e:
|
||||||
|
return {'success': False, 'error': str(e), 'pedimento_id': str(pedimento_id)}
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def auditar_acuse_cove_por_pedimento(pedimento_id):
|
||||||
|
try:
|
||||||
|
from api.customs.models import Pedimento
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
auditor_descargas(
|
||||||
|
pedimento,
|
||||||
|
servicio=9,
|
||||||
|
related_name='coves',
|
||||||
|
variable='acuse_cove_descargado',
|
||||||
|
mensaje='acuse de COVE'
|
||||||
|
)
|
||||||
|
return {'success': True, 'pedimento_id': str(pedimento_id)}
|
||||||
|
except Exception as e:
|
||||||
|
return {'success': False, 'error': str(e), 'pedimento_id': str(pedimento_id)}
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def auditar_edocument_por_pedimento(pedimento_id):
|
||||||
|
try:
|
||||||
|
from api.customs.models import Pedimento
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
auditor_descargas(
|
||||||
|
pedimento,
|
||||||
|
servicio=7,
|
||||||
|
related_name='documentos',
|
||||||
|
variable='edocument_descargado',
|
||||||
|
mensaje='EDocument'
|
||||||
|
)
|
||||||
|
return {'success': True, 'pedimento_id': str(pedimento_id)}
|
||||||
|
except Exception as e:
|
||||||
|
return {'success': False, 'error': str(e), 'pedimento_id': str(pedimento_id)}
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def auditar_acuse_por_pedimento(pedimento_id):
|
||||||
|
try:
|
||||||
|
from api.customs.models import Pedimento
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
auditor_descargas(
|
||||||
|
pedimento,
|
||||||
|
servicio=6,
|
||||||
|
related_name='documentos',
|
||||||
|
variable='acuse_descargado',
|
||||||
|
mensaje='acuse'
|
||||||
|
)
|
||||||
|
return {'success': True, 'pedimento_id': str(pedimento_id)}
|
||||||
|
except Exception as e:
|
||||||
|
return {'success': False, 'error': str(e), 'pedimento_id': str(pedimento_id)}
|
||||||
|
|
||||||
|
|||||||
@@ -265,7 +265,6 @@ def procesar_remesas(organizacion_id):
|
|||||||
|
|
||||||
print(f"Servicio enviado para pedimento {pedimento.pedimento}")
|
print(f"Servicio enviado para pedimento {pedimento.pedimento}")
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
def procesar_coves(organizacion_id):
|
def procesar_coves(organizacion_id):
|
||||||
pedimentos = Pedimento.objects.filter(
|
pedimentos = Pedimento.objects.filter(
|
||||||
@@ -429,3 +428,5 @@ def documentos_con_errores(organizacion_id):
|
|||||||
# Aquí puedes agregar lógica adicional para manejar documentos con errores
|
# Aquí puedes agregar lógica adicional para manejar documentos con errores
|
||||||
# como enviar notificaciones, registrar en un log, etc.
|
# como enviar notificaciones, registrar en un log, etc.
|
||||||
# documentos = Document.objects.all() --- IGNORE ---
|
# documentos = Document.objects.all() --- IGNORE ---
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,8 +34,15 @@ from .views_auditor import (
|
|||||||
crear_partidas_organizacion,
|
crear_partidas_organizacion,
|
||||||
crear_partidas_pedimento,
|
crear_partidas_pedimento,
|
||||||
auditar_pedimentos_endpoint,
|
auditar_pedimentos_endpoint,
|
||||||
auditar_procesamiento_remesas_endpoint,
|
auditar_coves_endpoint,
|
||||||
auditar_procesamiento_remesa_pedimento_endpoint
|
auditar_acuse_cove_endpoint,
|
||||||
|
auditar_edocuments_endpoint,
|
||||||
|
auditar_acuse_endpoint,
|
||||||
|
auditar_cove_pedimento_endpoint,
|
||||||
|
auditar_acuse_cove_pedimento_endpoint,
|
||||||
|
auditar_edocument_pedimento_endpoint,
|
||||||
|
auditar_acuse_pedimento_endpoint,
|
||||||
|
auditor_procesar_pedimentos_organizacion
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@@ -43,6 +50,13 @@ urlpatterns = [
|
|||||||
path('auditor/crear-partidas/organizacion/', crear_partidas_organizacion, name='crear-partidas-organizacion'),
|
path('auditor/crear-partidas/organizacion/', crear_partidas_organizacion, name='crear-partidas-organizacion'),
|
||||||
path('auditor/crear-partidas/pedimento/', crear_partidas_pedimento, name='crear-partidas-pedimento'),
|
path('auditor/crear-partidas/pedimento/', crear_partidas_pedimento, name='crear-partidas-pedimento'),
|
||||||
path('auditor/auditar-pedimentos/', auditar_pedimentos_endpoint, name='auditar-pedimentos'),
|
path('auditor/auditar-pedimentos/', auditar_pedimentos_endpoint, name='auditar-pedimentos'),
|
||||||
path('auditor/auditar-procesamiento-remesas/', auditar_procesamiento_remesas_endpoint, name='auditar-procesamiento-remesas'),
|
path('auditor/auditar-coves/', auditar_coves_endpoint, name='auditar-coves'),
|
||||||
path('auditor/auditar-procesamiento-remesa/pedimento/', auditar_procesamiento_remesa_pedimento_endpoint, name='auditar-procesamiento-remesa-pedimento'),
|
path('auditor/auditar-acuse-cove/', auditar_acuse_cove_endpoint, name='auditar-acuse-cove'),
|
||||||
|
path('auditor/auditar-edocuments/', auditar_edocuments_endpoint, name='auditar-edocuments'),
|
||||||
|
path('auditor/auditar-acuse/', auditar_acuse_endpoint, name='auditar-acuse'),
|
||||||
|
path('auditor/auditar-cove/pedimento/', auditar_cove_pedimento_endpoint, name='auditar-cove-pedimento'),
|
||||||
|
path('auditor/auditar-acuse-cove/pedimento/', auditar_acuse_cove_pedimento_endpoint, name='auditar-acuse-cove-pedimento'),
|
||||||
|
path('auditor/auditar-edocument/pedimento/', auditar_edocument_pedimento_endpoint, name='auditar-edocument-pedimento'),
|
||||||
|
path('auditor/auditar-acuse/pedimento/', auditar_acuse_pedimento_endpoint, name='auditar-acuse-pedimento'),
|
||||||
|
path('auditor/procesar-pedimentos/organizaciones/', auditor_procesar_pedimentos_organizacion, name='procesar-pedimentos-organizaciones'),
|
||||||
]
|
]
|
||||||
@@ -38,23 +38,6 @@ from api.customs.serializers import (
|
|||||||
from api.logger.mixins import LoggingMixin
|
from api.logger.mixins import LoggingMixin
|
||||||
from mixins.filtrado_organizacion import OrganizacionFiltradaMixin, ProcesosPorOrganizacionMixin
|
from mixins.filtrado_organizacion import OrganizacionFiltradaMixin, ProcesosPorOrganizacionMixin
|
||||||
import requests
|
import requests
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import zipfile
|
|
||||||
import tempfile
|
|
||||||
import shutil
|
|
||||||
from datetime import datetime
|
|
||||||
from django.core.files.base import ContentFile
|
|
||||||
from django.db import transaction
|
|
||||||
from rest_framework.parsers import MultiPartParser, FormParser
|
|
||||||
from api.record.models import Document, DocumentType
|
|
||||||
|
|
||||||
# Importar rarfile de manera opcional
|
|
||||||
try:
|
|
||||||
import rarfile
|
|
||||||
RAR_SUPPORT = True
|
|
||||||
except ImportError:
|
|
||||||
RAR_SUPPORT = False
|
|
||||||
|
|
||||||
|
|
||||||
from .tasks.microservice_v2 import *
|
from .tasks.microservice_v2 import *
|
||||||
@@ -194,24 +177,24 @@ class ViewSetPedimento(LoggingMixin, viewsets.ModelViewSet, OrganizacionFiltrada
|
|||||||
|
|
||||||
# Verificar si la respuesta fue exitosa
|
# Verificar si la respuesta fue exitosa
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
print(f"✅ Servicio FastAPI ejecutado exitosamente: {response.status_code}")
|
print(f"Servicio FastAPI ejecutado exitosamente: {response.status_code}")
|
||||||
print(f"📄 Respuesta: {response.json()}")
|
print(f"Respuesta: {response.json()}")
|
||||||
elif response.status_code == 201:
|
elif response.status_code == 201:
|
||||||
print(f"✅ Recurso creado exitosamente en FastAPI: {response.status_code}")
|
print(f"Recurso creado exitosamente en FastAPI: {response.status_code}")
|
||||||
print(f"📄 Respuesta: {response.json()}")
|
print(f"Respuesta: {response.json()}")
|
||||||
else:
|
else:
|
||||||
print(f"⚠️ Servicio FastAPI respondió con error: {response.status_code}")
|
print(f"Servicio FastAPI respondió con error: {response.status_code}")
|
||||||
print(f"📄 Respuesta: {response.text}")
|
print(f"Respuesta: {response.text}")
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError as e:
|
except requests.exceptions.ConnectionError as e:
|
||||||
print(f"❌ No se pudo conectar al servicio FastAPI: {e}")
|
print(f"No se pudo conectar al servicio FastAPI: {e}")
|
||||||
print(f"🔧 Verifica que el servicio FastAPI esté corriendo en {SERVICE_API_URL}")
|
print(f"Verifica que el servicio FastAPI esté corriendo en {SERVICE_API_URL}")
|
||||||
except requests.exceptions.Timeout as e:
|
except requests.exceptions.Timeout as e:
|
||||||
print(f"⏰ Timeout al conectar con el servicio FastAPI: {e}")
|
print(f"Timeout al conectar con el servicio FastAPI: {e}")
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"🚨 Error de request al servicio FastAPI: {e}")
|
print(f"Error de request al servicio FastAPI: {e}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"💥 Error inesperado al llamar al servicio FastAPI: {e}")
|
print(f"Error inesperado al llamar al servicio FastAPI: {e}")
|
||||||
|
|
||||||
def perform_update(self, serializer):
|
def perform_update(self, serializer):
|
||||||
"""
|
"""
|
||||||
@@ -341,362 +324,6 @@ class ViewSetPedimento(LoggingMixin, viewsets.ModelViewSet, OrganizacionFiltrada
|
|||||||
|
|
||||||
return Response(response_data, status=response_status)
|
return Response(response_data, status=response_status)
|
||||||
|
|
||||||
@action(detail=False, methods=['post'], url_path='bulk-create', parser_classes=[MultiPartParser, FormParser])
|
|
||||||
def bulk_create(self, request):
|
|
||||||
"""
|
|
||||||
Endpoint para crear múltiples pedimentos de manera masiva desde archivos.
|
|
||||||
|
|
||||||
FormData esperado:
|
|
||||||
- contribuyente: string (nombre del contribuyente)
|
|
||||||
- archivos: files (pueden ser múltiples archivos: zip, rar o individuales)
|
|
||||||
|
|
||||||
Nomenclatura esperada de archivos: anio-aduana-patente-pedimento
|
|
||||||
- anio: 2 dígitos (ej: 24)
|
|
||||||
- aduana: 2 o 3 dígitos (ej: 01, 123)
|
|
||||||
- patente: 4 dígitos (ej: 3420)
|
|
||||||
- pedimento: 7 dígitos (ej: 1234567)
|
|
||||||
|
|
||||||
Ejemplo: 24-01-3420-1234567
|
|
||||||
|
|
||||||
Nota: Cada archivo ZIP/RAR se procesa independientemente en su propio subdirectorio.
|
|
||||||
|
|
||||||
Respuesta exitosa:
|
|
||||||
{
|
|
||||||
"message": "Pedimentos creados exitosamente",
|
|
||||||
"created_count": 5,
|
|
||||||
"created_pedimentos": [...],
|
|
||||||
"documents_created": 15,
|
|
||||||
"processed_files": 3,
|
|
||||||
"summary": "Procesados 3 archivo(s): 5 pedimento(s) creado(s), 15 documento(s) asociado(s)",
|
|
||||||
"failed_files": [],
|
|
||||||
"errors": []
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
print(request.data)
|
|
||||||
# Validar datos requeridos
|
|
||||||
contribuyente = request.data.get('contribuyente')
|
|
||||||
archivos = request.FILES.getlist('archivos')
|
|
||||||
|
|
||||||
if not contribuyente:
|
|
||||||
return Response(
|
|
||||||
{"error": "Se requiere el campo 'contribuyente'"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
|
|
||||||
if not archivos:
|
|
||||||
return Response(
|
|
||||||
{"error": "Se requiere al menos un archivo"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
|
|
||||||
# Validar organización del usuario
|
|
||||||
if not request.user.is_authenticated or not hasattr(request.user, 'organizacion'):
|
|
||||||
return Response(
|
|
||||||
{"error": "Usuario no autenticado o sin organización"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
|
|
||||||
organizacion = request.user.organizacion
|
|
||||||
|
|
||||||
# Regex para validar nomenclatura: anio-aduana-patente-pedimento
|
|
||||||
nomenclatura_pattern = re.compile(r'^(\d{2})-(\d{2,3})-(\d{4})-(\d{7})$')
|
|
||||||
|
|
||||||
created_pedimentos = []
|
|
||||||
failed_files = []
|
|
||||||
errors = []
|
|
||||||
documents_created = 0
|
|
||||||
temp_dir = None
|
|
||||||
|
|
||||||
# Obtener DocumentType ANTES de la transacción atómica
|
|
||||||
print("Intentando obtener o crear DocumentType...")
|
|
||||||
try:
|
|
||||||
# Primero intentar obtener si ya existe
|
|
||||||
try:
|
|
||||||
document_type = DocumentType.objects.get(nombre="Pedimento")
|
|
||||||
print(f"DocumentType obtenido existente: {document_type.nombre} (ID: {document_type.id})")
|
|
||||||
except DocumentType.DoesNotExist:
|
|
||||||
# Si no existe, crear uno nuevo
|
|
||||||
document_type = DocumentType.objects.create(
|
|
||||||
nombre="Pedimento",
|
|
||||||
descripcion="Documento de pedimento"
|
|
||||||
)
|
|
||||||
print(f"DocumentType creado nuevo: {document_type.nombre} (ID: {document_type.id})")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error al obtener/crear DocumentType: {str(e)}")
|
|
||||||
# Como fallback, intentar obtener cualquier DocumentType existente
|
|
||||||
try:
|
|
||||||
document_type = DocumentType.objects.first()
|
|
||||||
if document_type:
|
|
||||||
print(f"Usando DocumentType existente como fallback: {document_type.nombre} (ID: {document_type.id})")
|
|
||||||
else:
|
|
||||||
print("No hay DocumentType disponible")
|
|
||||||
return Response(
|
|
||||||
{"error": "No se pudo configurar el tipo de documento y no hay tipos existentes"},
|
|
||||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
||||||
)
|
|
||||||
except Exception as fallback_error:
|
|
||||||
print(f"Error en fallback: {str(fallback_error)}")
|
|
||||||
return Response(
|
|
||||||
{"error": f"Error crítico al configurar tipo de documento: {str(e)}"},
|
|
||||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
print("Iniciando transacción atómica...")
|
|
||||||
with transaction.atomic():
|
|
||||||
# Crear directorio temporal
|
|
||||||
temp_dir = tempfile.mkdtemp()
|
|
||||||
print(f"Directorio temporal creado: {temp_dir}")
|
|
||||||
|
|
||||||
# Procesar cada archivo enviado
|
|
||||||
for idx, archivo in enumerate(archivos):
|
|
||||||
archivo_name = archivo.name.lower()
|
|
||||||
print(f"Procesando archivo {idx + 1}/{len(archivos)}: {archivo_name}")
|
|
||||||
|
|
||||||
# Crear subdirectorio para cada archivo usando el nombre del archivo sin extensión
|
|
||||||
archivo_name_sin_extension = os.path.splitext(archivo.name)[0]
|
|
||||||
sub_dir = os.path.join(temp_dir, archivo_name_sin_extension)
|
|
||||||
os.makedirs(sub_dir, exist_ok=True)
|
|
||||||
print(f"Subdirectorio creado: {sub_dir}")
|
|
||||||
|
|
||||||
if archivo_name.endswith('.zip'):
|
|
||||||
# Manejar archivo ZIP
|
|
||||||
print("Es un archivo ZIP")
|
|
||||||
try:
|
|
||||||
with zipfile.ZipFile(archivo, 'r') as zip_ref:
|
|
||||||
zip_ref.extractall(sub_dir)
|
|
||||||
print("Archivo ZIP extraído exitosamente")
|
|
||||||
except zipfile.BadZipFile as e:
|
|
||||||
return Response(
|
|
||||||
{"error": f"Archivo ZIP corrupto o inválido: {archivo.name} - {str(e)}"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
return Response(
|
|
||||||
{"error": f"Error al extraer ZIP {archivo.name}: {str(e)}"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
elif archivo_name.endswith('.rar'):
|
|
||||||
# Manejar archivo RAR
|
|
||||||
if not RAR_SUPPORT:
|
|
||||||
return Response(
|
|
||||||
{"error": "Soporte para archivos RAR no disponible. Instalar rarfile: pip install rarfile"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
with rarfile.RarFile(archivo, 'r') as rar_ref:
|
|
||||||
rar_ref.extractall(sub_dir)
|
|
||||||
print(f"Archivo RAR {archivo.name} extraído en sub_dir")
|
|
||||||
except rarfile.Error as e:
|
|
||||||
return Response(
|
|
||||||
{"error": f"Error al extraer archivo RAR {archivo.name}: {str(e)}"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Asumir que es un archivo individual
|
|
||||||
# Crear el archivo en el subdirectorio
|
|
||||||
archivo_path = os.path.join(sub_dir, archivo.name)
|
|
||||||
with open(archivo_path, 'wb') as f:
|
|
||||||
for chunk in archivo.chunks():
|
|
||||||
f.write(chunk)
|
|
||||||
print(f"Archivo individual {archivo.name} guardado en sub_dir:", archivo_path)
|
|
||||||
|
|
||||||
# Recorrer todos los archivos extraídos o el directorio
|
|
||||||
print("Iniciando recorrido de archivos...")
|
|
||||||
for root, dirs, files in os.walk(temp_dir):
|
|
||||||
print(f"Revisando directorio: {root}")
|
|
||||||
print(f"Archivos encontrados: {files}")
|
|
||||||
|
|
||||||
for file_name in files:
|
|
||||||
print(f"Procesando archivo: {file_name}")
|
|
||||||
file_path = os.path.join(root, file_name)
|
|
||||||
|
|
||||||
# Obtener la ruta relativa para determinar la estructura de carpetas
|
|
||||||
relative_path = os.path.relpath(file_path, temp_dir)
|
|
||||||
print(f"Ruta relativa: {relative_path}")
|
|
||||||
|
|
||||||
# Determinar si el archivo está en una carpeta que sigue la nomenclatura
|
|
||||||
folder_name = None
|
|
||||||
if os.path.dirname(relative_path):
|
|
||||||
# El archivo está dentro de una carpeta
|
|
||||||
folder_parts = relative_path.split(os.sep)
|
|
||||||
folder_name = folder_parts[0] # Primera carpeta (nombre del archivo ZIP/RAR sin extensión)
|
|
||||||
else:
|
|
||||||
# El archivo está en la raíz, usar el nombre del archivo sin extensión
|
|
||||||
folder_name = os.path.splitext(file_name)[0]
|
|
||||||
|
|
||||||
print(f"Folder name para validación: {folder_name}")
|
|
||||||
|
|
||||||
# Validar nomenclatura
|
|
||||||
match = nomenclatura_pattern.match(folder_name)
|
|
||||||
if not match:
|
|
||||||
print(f"Nomenclatura inválida: {folder_name}")
|
|
||||||
# Determinar el archivo original basado en el subdirectorio
|
|
||||||
archivo_original = folder_name + ('.zip' if any(f.endswith('.zip') for f in [a.name for a in archivos]) else '.rar')
|
|
||||||
failed_files.append({
|
|
||||||
"file": relative_path,
|
|
||||||
"archivo_original": archivo_original,
|
|
||||||
"error": f"Nomenclatura inválida: {folder_name}. Esperado: anio-aduana-patente-pedimento"
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
|
|
||||||
print(f"Nomenclatura válida: {folder_name}")
|
|
||||||
anio, aduana, patente, pedimento_num = match.groups()
|
|
||||||
print(f"Extraído - Año: {anio}, Aduana: {aduana}, Patente: {patente}, Pedimento: {pedimento_num}")
|
|
||||||
|
|
||||||
# Crear fecha_pago basada en el año
|
|
||||||
try:
|
|
||||||
# Convertir año de 2 dígitos a 4 dígitos
|
|
||||||
anio_completo = 2000 + int(anio) if int(anio) < 50 else 1900 + int(anio)
|
|
||||||
fecha_pago = datetime(anio_completo, 1, 1).date()
|
|
||||||
print(f"Fecha de pago calculada: {fecha_pago}")
|
|
||||||
except ValueError:
|
|
||||||
archivo_original = folder_name + ('.zip' if any(f.endswith('.zip') for f in [a.name for a in archivos]) else '.rar')
|
|
||||||
failed_files.append({
|
|
||||||
"file": relative_path,
|
|
||||||
"archivo_original": archivo_original,
|
|
||||||
"error": f"Año inválido: {anio}"
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Generar pedimento_app
|
|
||||||
pedimento_app = f"{anio}-{aduana.zfill(2)}-{patente}-{pedimento_num}"
|
|
||||||
print(f"Pedimento_app generado: {pedimento_app}")
|
|
||||||
print(f"Buscando pedimento existente con pedimento_app: {pedimento_app} y organización ID: {organizacion.id}")
|
|
||||||
# Verificar si el pedimento ya existe
|
|
||||||
existing_pedimento = Pedimento.objects.filter(
|
|
||||||
pedimento_app=pedimento_app,
|
|
||||||
organizacion=organizacion
|
|
||||||
).first()
|
|
||||||
|
|
||||||
print(f"Pedimento existente: {existing_pedimento is not None}")
|
|
||||||
|
|
||||||
if not existing_pedimento:
|
|
||||||
print("📝 Pedimento no existe, creando nuevo...")
|
|
||||||
# Crear nuevo pedimento
|
|
||||||
try:
|
|
||||||
print("🔄 Iniciando creación de pedimento...")
|
|
||||||
|
|
||||||
# Obtener o crear el importador
|
|
||||||
print(f"🏢 Buscando/creando importador con RFC: {contribuyente}")
|
|
||||||
importador, created = Importador.objects.get_or_create(
|
|
||||||
rfc=contribuyente,
|
|
||||||
defaults={
|
|
||||||
'nombre': f"Importador {contribuyente}",
|
|
||||||
'organizacion': organizacion
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if created:
|
|
||||||
print(f"✅ Importador creado: {importador.rfc} - {importador.nombre}")
|
|
||||||
else:
|
|
||||||
print(f"♻️ Importador existente: {importador.rfc} - {importador.nombre}")
|
|
||||||
|
|
||||||
pedimento = Pedimento.objects.create(
|
|
||||||
organizacion=organizacion,
|
|
||||||
contribuyente=importador,
|
|
||||||
pedimento=int(pedimento_num),
|
|
||||||
aduana=int(aduana),
|
|
||||||
patente=int(patente),
|
|
||||||
fecha_pago=fecha_pago,
|
|
||||||
pedimento_app=pedimento_app,
|
|
||||||
agente_aduanal=f"Agente {patente}", # Valor por defecto
|
|
||||||
clave_pedimento="A1" # Valor por defecto
|
|
||||||
)
|
|
||||||
|
|
||||||
print(f"✅ Pedimento creado exitosamente: ID {pedimento.id}, pedimento_app: {pedimento_app}")
|
|
||||||
|
|
||||||
created_pedimentos.append({
|
|
||||||
"id": str(pedimento.id),
|
|
||||||
"pedimento_app": pedimento_app,
|
|
||||||
"contribuyente": importador.rfc,
|
|
||||||
"contribuyente_nombre": importador.nombre
|
|
||||||
})
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"❌ Error al crear pedimento: {str(e)}")
|
|
||||||
archivo_original = folder_name + ('.zip' if any(f.endswith('.zip') for f in [a.name for a in archivos]) else '.rar')
|
|
||||||
failed_files.append({
|
|
||||||
"file": relative_path,
|
|
||||||
"archivo_original": archivo_original,
|
|
||||||
"error": f"Error al crear pedimento: {str(e)}"
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
print(f"♻️ Usando pedimento existente: ID {existing_pedimento.id}")
|
|
||||||
# Usar pedimento existente
|
|
||||||
pedimento = existing_pedimento
|
|
||||||
|
|
||||||
print(f"🔄 Iniciando creación de documento para pedimento ID: {pedimento.id}")
|
|
||||||
# Crear documento asociado al pedimento
|
|
||||||
try:
|
|
||||||
print("📖 Leyendo archivo desde directorio temporal...")
|
|
||||||
# Leer el archivo desde el directorio temporal
|
|
||||||
with open(file_path, 'rb') as f:
|
|
||||||
file_content = f.read()
|
|
||||||
|
|
||||||
print(f"📄 Archivo leído: {len(file_content)} bytes")
|
|
||||||
# Crear ContentFile que Django puede manejar correctamente
|
|
||||||
django_file = ContentFile(file_content, name=file_name)
|
|
||||||
|
|
||||||
print(f"Creando documento para archivo: {file_name}")
|
|
||||||
# Crear documento - Django automáticamente guardará el archivo en media/documents/
|
|
||||||
document = Document.objects.create(
|
|
||||||
organizacion=organizacion,
|
|
||||||
pedimento_id=pedimento.id,
|
|
||||||
document_type=document_type,
|
|
||||||
archivo=django_file,
|
|
||||||
size=len(file_content),
|
|
||||||
extension=os.path.splitext(file_name)[1].lower().lstrip('.')
|
|
||||||
)
|
|
||||||
print(f"Documento creado exitosamente: {document.id}")
|
|
||||||
|
|
||||||
documents_created += 1
|
|
||||||
print(f"📊 Total documentos creados hasta ahora: {documents_created}")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"❌ Error al crear documento: {str(e)}")
|
|
||||||
archivo_original = folder_name + ('.zip' if any(f.endswith('.zip') for f in [a.name for a in archivos]) else '.rar')
|
|
||||||
failed_files.append({
|
|
||||||
"file": relative_path,
|
|
||||||
"archivo_original": archivo_original,
|
|
||||||
"error": f"Error al crear documento: {str(e)}"
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
|
|
||||||
print(f"🏁 Procesamiento completado. Archivos procesados en este directorio.")
|
|
||||||
except Exception as e:
|
|
||||||
return Response(
|
|
||||||
{"error": f"Error durante el procesamiento: {str(e)}"},
|
|
||||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
# Limpiar directorio temporal
|
|
||||||
if temp_dir and os.path.exists(temp_dir):
|
|
||||||
shutil.rmtree(temp_dir)
|
|
||||||
|
|
||||||
# Preparar respuesta
|
|
||||||
response_data = {
|
|
||||||
"created_count": len(created_pedimentos),
|
|
||||||
"created_pedimentos": created_pedimentos,
|
|
||||||
"documents_created": documents_created,
|
|
||||||
"failed_files": failed_files,
|
|
||||||
"processed_files": len(archivos),
|
|
||||||
"summary": f"Procesados {len(archivos)} archivo(s): {len(created_pedimentos)} pedimento(s) creado(s), {documents_created} documento(s) asociado(s)"
|
|
||||||
}
|
|
||||||
|
|
||||||
if failed_files:
|
|
||||||
response_data.update({
|
|
||||||
"message": "Procesamiento completado con algunos errores",
|
|
||||||
"errors": [item["error"] for item in failed_files]
|
|
||||||
})
|
|
||||||
response_status = status.HTTP_207_MULTI_STATUS
|
|
||||||
else:
|
|
||||||
response_data["message"] = "Pedimentos creados exitosamente"
|
|
||||||
response_status = status.HTTP_201_CREATED
|
|
||||||
|
|
||||||
return Response(response_data, status=response_status)
|
|
||||||
|
|
||||||
my_tags = ['Pedimentos']
|
my_tags = ['Pedimentos']
|
||||||
|
|
||||||
class PartidaViewSet(viewsets.ModelViewSet):
|
class PartidaViewSet(viewsets.ModelViewSet):
|
||||||
|
|||||||
@@ -8,11 +8,20 @@ from core.permissions import IsSuperUser, IsSameOrganizationDeveloper
|
|||||||
from .tasks.auditoria import (
|
from .tasks.auditoria import (
|
||||||
crear_partidas,
|
crear_partidas,
|
||||||
crear_partidas_por_pedimento,
|
crear_partidas_por_pedimento,
|
||||||
auditar_procesamiento_remesas,
|
auditar_procesamiento_remesa_por_pedimento,
|
||||||
auditar_procesamiento_remesa_por_pedimento
|
auditar_coves,
|
||||||
|
auditar_acuse_cove,
|
||||||
|
auditar_edocuments,
|
||||||
|
auditar_acuse,
|
||||||
|
auditar_cove_por_pedimento,
|
||||||
|
auditar_acuse_cove_por_pedimento,
|
||||||
|
auditar_edocument_por_pedimento,
|
||||||
|
auditar_acuse_por_pedimento
|
||||||
)
|
)
|
||||||
from .tasks.internal_services import auditar_pedimentos
|
from .tasks.internal_services import auditar_pedimentos
|
||||||
|
from .tasks.microservice_v2 import procesar_pedimentos_completos
|
||||||
from api.customs.models import Pedimento
|
from api.customs.models import Pedimento
|
||||||
|
from api.organization.models import Organizacion
|
||||||
|
|
||||||
|
|
||||||
@swagger_auto_schema(
|
@swagger_auto_schema(
|
||||||
@@ -230,7 +239,7 @@ def auditar_procesamiento_remesa_pedimento_endpoint(request):
|
|||||||
|
|
||||||
@swagger_auto_schema(
|
@swagger_auto_schema(
|
||||||
method='post',
|
method='post',
|
||||||
operation_description="Audita el procesamiento de remesas de una organización",
|
operation_description="Audita los COVEs de una organización",
|
||||||
request_body=openapi.Schema(
|
request_body=openapi.Schema(
|
||||||
type=openapi.TYPE_OBJECT,
|
type=openapi.TYPE_OBJECT,
|
||||||
properties={
|
properties={
|
||||||
@@ -246,10 +255,10 @@ def auditar_procesamiento_remesa_pedimento_endpoint(request):
|
|||||||
)
|
)
|
||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
||||||
def auditar_procesamiento_remesas_endpoint(request):
|
def auditar_coves_endpoint(request):
|
||||||
"""
|
"""
|
||||||
Inicia una tarea de auditoría para el procesamiento de remesas de una organización.
|
Inicia una tarea de auditoría para los COVEs de una organización.
|
||||||
Verifica el estado y la integridad del procesamiento de remesas.
|
Verifica la existencia y validez de los COVEs generados.
|
||||||
"""
|
"""
|
||||||
organizacion_id = request.data.get('organizacion_id')
|
organizacion_id = request.data.get('organizacion_id')
|
||||||
|
|
||||||
@@ -268,8 +277,8 @@ def auditar_procesamiento_remesas_endpoint(request):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Ejecutar la tarea de auditoría
|
# Ejecutar la tarea de auditoría
|
||||||
task = auditar_procesamiento_remesas.delay(organizacion_id)
|
task = auditar_coves.delay(organizacion_id)
|
||||||
message = f"Auditoría de procesamiento de remesas iniciada para la organización {organizacion_id}"
|
message = f"Auditoría de COVEs iniciada para la organización {organizacion_id}"
|
||||||
|
|
||||||
return Response({
|
return Response({
|
||||||
'message': message,
|
'message': message,
|
||||||
@@ -277,3 +286,350 @@ def auditar_procesamiento_remesas_endpoint(request):
|
|||||||
}, status=status.HTTP_200_OK)
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Audita los acuses de COVE de una organización",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'organizacion_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID de la organización')
|
||||||
|
},
|
||||||
|
required=['organizacion_id']
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de auditoría iniciada correctamente'),
|
||||||
|
400: openapi.Response('Error en los parámetros'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
||||||
|
def auditar_acuse_cove_endpoint(request):
|
||||||
|
"""
|
||||||
|
Inicia una tarea de auditoría para los acuses de COVE de una organización.
|
||||||
|
Verifica la recepción y validez de los acuses de COVE.
|
||||||
|
"""
|
||||||
|
organizacion_id = request.data.get('organizacion_id')
|
||||||
|
|
||||||
|
if not organizacion_id:
|
||||||
|
return Response(
|
||||||
|
{'error': 'Debe proporcionar organizacion_id'},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
|
||||||
|
# Validar permisos
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser and str(user.organizacion.id) != organizacion_id:
|
||||||
|
return Response(
|
||||||
|
{'error': 'No tiene permisos para esta organización'},
|
||||||
|
status=status.HTTP_403_FORBIDDEN
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ejecutar la tarea de auditoría
|
||||||
|
task = auditar_acuse_cove.delay(organizacion_id)
|
||||||
|
message = f"Auditoría de acuses de COVE iniciada para la organización {organizacion_id}"
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
'message': message,
|
||||||
|
'task_id': task.id
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Audita los EDocuments de una organización",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'organizacion_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID de la organización')
|
||||||
|
},
|
||||||
|
required=['organizacion_id']
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de auditoría iniciada correctamente'),
|
||||||
|
400: openapi.Response('Error en los parámetros'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
||||||
|
def auditar_edocuments_endpoint(request):
|
||||||
|
"""
|
||||||
|
Inicia una tarea de auditoría para los EDocuments de una organización.
|
||||||
|
Verifica la existencia y validez de los EDocuments generados.
|
||||||
|
"""
|
||||||
|
organizacion_id = request.data.get('organizacion_id')
|
||||||
|
|
||||||
|
if not organizacion_id:
|
||||||
|
return Response(
|
||||||
|
{'error': 'Debe proporcionar organizacion_id'},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
|
||||||
|
# Validar permisos
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser and str(user.organizacion.id) != organizacion_id:
|
||||||
|
return Response(
|
||||||
|
{'error': 'No tiene permisos para esta organización'},
|
||||||
|
status=status.HTTP_403_FORBIDDEN
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ejecutar la tarea de auditoría
|
||||||
|
task = auditar_edocuments.delay(organizacion_id)
|
||||||
|
message = f"Auditoría de EDocuments iniciada para la organización {organizacion_id}"
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
'message': message,
|
||||||
|
'task_id': task.id
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Audita los acuses de una organización",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'organizacion_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID de la organización')
|
||||||
|
},
|
||||||
|
required=['organizacion_id']
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de auditoría iniciada correctamente'),
|
||||||
|
400: openapi.Response('Error en los parámetros'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
||||||
|
def auditar_acuse_endpoint(request):
|
||||||
|
"""
|
||||||
|
Inicia una tarea de auditoría para los acuses de una organización.
|
||||||
|
Verifica la recepción y validez de los acuses.
|
||||||
|
"""
|
||||||
|
organizacion_id = request.data.get('organizacion_id')
|
||||||
|
|
||||||
|
if not organizacion_id:
|
||||||
|
return Response(
|
||||||
|
{'error': 'Debe proporcionar organizacion_id'},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
|
||||||
|
# Validar permisos
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser and str(user.organizacion.id) != organizacion_id:
|
||||||
|
return Response(
|
||||||
|
{'error': 'No tiene permisos para esta organización'},
|
||||||
|
status=status.HTTP_403_FORBIDDEN
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ejecutar la tarea de auditoría
|
||||||
|
task = auditar_acuse.delay(organizacion_id)
|
||||||
|
message = f"Auditoría de acuses iniciada para la organización {organizacion_id}"
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
'message': message,
|
||||||
|
'task_id': task.id
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Audita el COVE de un pedimento específico",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'pedimento_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID del pedimento')
|
||||||
|
},
|
||||||
|
required=['pedimento_id']
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de auditoría iniciada correctamente'),
|
||||||
|
400: openapi.Response('Error en los parámetros'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes'),
|
||||||
|
404: openapi.Response('Pedimento no encontrado')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated])
|
||||||
|
def auditar_cove_pedimento_endpoint(request):
|
||||||
|
pedimento_id = request.data.get('pedimento_id')
|
||||||
|
if not pedimento_id:
|
||||||
|
return Response({'error': 'Debe proporcionar pedimento_id'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
try:
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser and str(pedimento.organizacion.id) != str(user.organizacion.id):
|
||||||
|
return Response({'error': 'No tiene permisos para este pedimento'}, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
except Pedimento.DoesNotExist:
|
||||||
|
return Response({'error': 'Pedimento no encontrado'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
task = auditar_cove_por_pedimento.delay(pedimento_id)
|
||||||
|
return Response({'message': f'Auditoría de COVE iniciada para el pedimento {pedimento_id}', 'task_id': task.id}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Audita el acuse de COVE de un pedimento específico",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'pedimento_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID del pedimento')
|
||||||
|
},
|
||||||
|
required=['pedimento_id']
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de auditoría iniciada correctamente'),
|
||||||
|
400: openapi.Response('Error en los parámetros'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes'),
|
||||||
|
404: openapi.Response('Pedimento no encontrado')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated])
|
||||||
|
def auditar_acuse_cove_pedimento_endpoint(request):
|
||||||
|
pedimento_id = request.data.get('pedimento_id')
|
||||||
|
if not pedimento_id:
|
||||||
|
return Response({'error': 'Debe proporcionar pedimento_id'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
try:
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser and str(pedimento.organizacion.id) != str(user.organizacion.id):
|
||||||
|
return Response({'error': 'No tiene permisos para este pedimento'}, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
except Pedimento.DoesNotExist:
|
||||||
|
return Response({'error': 'Pedimento no encontrado'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
task = auditar_acuse_cove_por_pedimento.delay(pedimento_id)
|
||||||
|
return Response({'message': f'Auditoría de acuse de COVE iniciada para el pedimento {pedimento_id}', 'task_id': task.id}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Audita el EDocument de un pedimento específico",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'pedimento_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID del pedimento')
|
||||||
|
},
|
||||||
|
required=['pedimento_id']
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de auditoría iniciada correctamente'),
|
||||||
|
400: openapi.Response('Error en los parámetros'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes'),
|
||||||
|
404: openapi.Response('Pedimento no encontrado')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated])
|
||||||
|
def auditar_edocument_pedimento_endpoint(request):
|
||||||
|
pedimento_id = request.data.get('pedimento_id')
|
||||||
|
if not pedimento_id:
|
||||||
|
return Response({'error': 'Debe proporcionar pedimento_id'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
try:
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser and str(pedimento.organizacion.id) != str(user.organizacion.id):
|
||||||
|
return Response({'error': 'No tiene permisos para este pedimento'}, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
except Pedimento.DoesNotExist:
|
||||||
|
return Response({'error': 'Pedimento no encontrado'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
task = auditar_edocument_por_pedimento.delay(pedimento_id)
|
||||||
|
return Response({'message': f'Auditoría de EDocument iniciada para el pedimento {pedimento_id}', 'task_id': task.id}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Audita el acuse de un pedimento específico",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'pedimento_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID del pedimento')
|
||||||
|
},
|
||||||
|
required=['pedimento_id']
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de auditoría iniciada correctamente'),
|
||||||
|
400: openapi.Response('Error en los parámetros'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes'),
|
||||||
|
404: openapi.Response('Pedimento no encontrado')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated])
|
||||||
|
def auditar_acuse_pedimento_endpoint(request):
|
||||||
|
pedimento_id = request.data.get('pedimento_id')
|
||||||
|
if not pedimento_id:
|
||||||
|
return Response({'error': 'Debe proporcionar pedimento_id'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
try:
|
||||||
|
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser and str(pedimento.organizacion.id) != str(user.organizacion.id):
|
||||||
|
return Response({'error': 'No tiene permisos para este pedimento'}, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
except Pedimento.DoesNotExist:
|
||||||
|
return Response({'error': 'Pedimento no encontrado'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
task = auditar_acuse_por_pedimento.delay(pedimento_id)
|
||||||
|
return Response({'message': f'Auditoría de acuse iniciada para el pedimento {pedimento_id}', 'task_id': task.id}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
### Procesamiento de pedimentos ###
|
||||||
|
@swagger_auto_schema(
|
||||||
|
method='post',
|
||||||
|
operation_description="Procesamiento de todos los pedimentos de todas las organizaciones",
|
||||||
|
request_body=openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={}
|
||||||
|
),
|
||||||
|
responses={
|
||||||
|
200: openapi.Response('Tarea de procesamiento iniciada correctamente'),
|
||||||
|
403: openapi.Response('No tiene permisos suficientes'),
|
||||||
|
404: openapi.Response('No se encontraron organizaciones')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@api_view(['POST'])
|
||||||
|
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
||||||
|
def auditor_procesar_pedimentos_organizacion(request):
|
||||||
|
"""
|
||||||
|
Inicia una tarea de procesamiento para todos los pedimentos de todas las organizaciones.
|
||||||
|
Solo usuarios administradores pueden ejecutar esta función.
|
||||||
|
"""
|
||||||
|
# Validar permisos (solo superusuarios pueden procesar todas las organizaciones)
|
||||||
|
user = request.user
|
||||||
|
if not user.is_superuser:
|
||||||
|
return Response(
|
||||||
|
{'error': 'Solo los superusuarios pueden procesar todas las organizaciones'},
|
||||||
|
status=status.HTTP_403_FORBIDDEN
|
||||||
|
)
|
||||||
|
|
||||||
|
organizaciones = Organizacion.objects.all()
|
||||||
|
if not organizaciones.exists():
|
||||||
|
return Response(
|
||||||
|
{'error': 'No se encontraron organizaciones'},
|
||||||
|
status=status.HTTP_404_NOT_FOUND
|
||||||
|
)
|
||||||
|
|
||||||
|
# Lista para recopilar todos los task_ids y detalles
|
||||||
|
tasks_iniciadas = []
|
||||||
|
|
||||||
|
for organizacion in organizaciones:
|
||||||
|
organizacion_id = str(organizacion.id)
|
||||||
|
print(f"Procesando organización: {organizacion_id} - {organizacion.nombre}")
|
||||||
|
# Ejecutar la tarea de procesamiento
|
||||||
|
task = procesar_pedimentos_completos.delay(organizacion_id)
|
||||||
|
|
||||||
|
# Agregar información de la tarea a la lista
|
||||||
|
tasks_iniciadas.append({
|
||||||
|
'organizacion_id': organizacion_id,
|
||||||
|
'organizacion_nombre': organizacion.nombre,
|
||||||
|
'task_id': task.id
|
||||||
|
})
|
||||||
|
|
||||||
|
# Crear mensaje general y lista de task_ids
|
||||||
|
total_organizaciones = len(tasks_iniciadas)
|
||||||
|
task_ids = [task['task_id'] for task in tasks_iniciadas]
|
||||||
|
|
||||||
|
message = f"Procesamiento de pedimentos iniciado para {total_organizaciones} organización(es)"
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
'message': message,
|
||||||
|
'total_organizaciones': total_organizaciones,
|
||||||
|
'task_ids': task_ids,
|
||||||
|
'tasks_detalle': tasks_iniciadas
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
### Fin Procesamiento de pedimentos ###
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
14
api/tasks/filters.py
Normal file
14
api/tasks/filters.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
from django_filters import rest_framework as filters
|
||||||
|
from .models import Task
|
||||||
|
|
||||||
|
class TaskFilter(filters.FilterSet):
|
||||||
|
servicio = filters.NumberFilter(field_name='servicio__id')
|
||||||
|
pedimento_app = filters.CharFilter(field_name='pedimento__pedimento_app')
|
||||||
|
pedimento = filters.UUIDFilter(field_name='pedimento__id')
|
||||||
|
timestamp_gte = filters.DateTimeFilter(field_name='timestamp', lookup_expr='gte')
|
||||||
|
timestamp_lte = filters.DateTimeFilter(field_name='timestamp', lookup_expr='lte')
|
||||||
|
status = filters.CharFilter(field_name='status')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Task
|
||||||
|
fields = ['servicio', 'pedimento_app', 'pedimento', 'timestamp_gte', 'timestamp_lte', 'status']
|
||||||
@@ -9,3 +9,4 @@ class Task(models.Model):
|
|||||||
message = models.TextField()
|
message = models.TextField()
|
||||||
status = models.CharField(max_length=50)
|
status = models.CharField(max_length=50)
|
||||||
servicio = models.ForeignKey('customs.Servicio', on_delete=models.CASCADE, null=True, blank=True)
|
servicio = models.ForeignKey('customs.Servicio', on_delete=models.CASCADE, null=True, blank=True)
|
||||||
|
|
||||||
@@ -2,6 +2,8 @@ from rest_framework import serializers
|
|||||||
from .models import Task
|
from .models import Task
|
||||||
|
|
||||||
class TaskSerializer(serializers.ModelSerializer):
|
class TaskSerializer(serializers.ModelSerializer):
|
||||||
|
pedimento_app = serializers.CharField(source='pedimento.pedimento_app', read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Task
|
model = Task
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets, filters
|
||||||
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
|
from rest_framework.pagination import PageNumberPagination
|
||||||
|
|
||||||
from api.logger.mixins import LoggingMixin
|
from api.logger.mixins import LoggingMixin
|
||||||
from .models import Task
|
from .models import Task
|
||||||
from .serializers import TaskSerializer
|
from .serializers import TaskSerializer
|
||||||
|
from .filters import TaskFilter
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@@ -14,9 +17,19 @@ from core.permissions import (
|
|||||||
IsSuperUser
|
IsSuperUser
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class TaskPagination(PageNumberPagination):
|
||||||
|
page_size = 10
|
||||||
|
page_size_query_param = 'page_size'
|
||||||
|
max_page_size = 100
|
||||||
|
|
||||||
class TaskViewSet(LoggingMixin,viewsets.ModelViewSet):
|
class TaskViewSet(LoggingMixin,viewsets.ModelViewSet):
|
||||||
permission_classes = [IsAuthenticated & (IsSameOrganization | IsSameOrganizationAndAdmin | IsSameOrganizationDeveloper | IsSuperUser)]
|
permission_classes = [IsAuthenticated & (IsSameOrganization | IsSameOrganizationAndAdmin | IsSameOrganizationDeveloper | IsSuperUser)]
|
||||||
queryset = Task.objects.all()
|
queryset = Task.objects.select_related('pedimento', 'servicio').all()
|
||||||
serializer_class = TaskSerializer
|
serializer_class = TaskSerializer
|
||||||
|
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
|
||||||
|
filterset_class = TaskFilter
|
||||||
|
pagination_class = TaskPagination
|
||||||
|
ordering_fields = ['timestamp']
|
||||||
|
ordering = ['-timestamp'] # ordenamiento por defecto, más reciente primero
|
||||||
|
|
||||||
my_tags = ['tasks']
|
my_tags = ['tasks']
|
||||||
@@ -3,46 +3,7 @@ from celery.schedules import crontab
|
|||||||
|
|
||||||
|
|
||||||
CELERY_BEAT_SCHEDULE = {
|
CELERY_BEAT_SCHEDULE = {
|
||||||
# Ejecutar pedimento completo de 5:00 a 22:00 (cada hora)
|
|
||||||
'creacion-servicio-pedimento-completo': {
|
|
||||||
'task': 'api.customs.tasks.internal_services.crear_todos_los_servicios',
|
|
||||||
'schedule': crontab(minute=0, hour='5-22'),
|
|
||||||
},
|
|
||||||
# Ejecutar pedimento completo de 5:00 a 22:00 (cada hora)
|
|
||||||
'ejecutar-pedimentos-completos-dia': {
|
|
||||||
'task': 'api.customs.tasks.microservice.ejecutar_pedimento_completo',
|
|
||||||
'schedule': crontab(minute=0, hour='5-22'),
|
|
||||||
},
|
|
||||||
# Ejecutar partidas de 5:00 a 22:00 (cada hora)
|
|
||||||
'ejecutar-partidas-dia': {
|
|
||||||
'task': 'api.customs.tasks.microservice.ejecutar_partidas_pedimento',
|
|
||||||
'schedule': crontab(minute=0, hour='5-23'),
|
|
||||||
},
|
|
||||||
# Ejecutar coves de 5:00 a 22:00 (cada hora)
|
|
||||||
'ejecutar-coves-dia': {
|
|
||||||
'task': 'api.customs.tasks.microservice.ejecutar_coves',
|
|
||||||
'schedule': crontab(minute=0, hour='5-23'),
|
|
||||||
},
|
|
||||||
# Ejecutar remesas de 5:00 a 22:00 (cada hora)
|
|
||||||
'ejecutar-remesas-dia': {
|
|
||||||
'task': 'api.customs.tasks.microservice.ejecutar_remesas',
|
|
||||||
'schedule': crontab(minute=0, hour='5-23'),
|
|
||||||
},
|
|
||||||
# Ejecutar acuse coves de 5:00 a 22:00 (cada hora)
|
|
||||||
'ejecutar-acuse-coves-dia': {
|
|
||||||
'task': 'api.customs.tasks.microservice.ejecutar_acuseCoves',
|
|
||||||
'schedule': crontab(minute=0, hour='5-23'),
|
|
||||||
},
|
|
||||||
# Ejecutar acuse de 5:00 a 22:00 (cada hora)
|
|
||||||
'ejecutar-acuse-dia': {
|
|
||||||
'task': 'api.customs.tasks.microservice.ejecutar_acuse',
|
|
||||||
'schedule': crontab(minute=0, hour='5-23'),
|
|
||||||
},
|
|
||||||
# Ejecutar edocs solo de 23:00 a 4:59 (cada hora en ese rango)
|
|
||||||
'ejecutar-edocs-noche': {
|
|
||||||
'task': 'api.customs.tasks.microservice.ejecutar_edocs',
|
|
||||||
'schedule': crontab(minute=42, hour='23,0,1,2,3,4'),
|
|
||||||
},
|
|
||||||
|
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user