Merge branch 'development' of https://git.aduanasoft.com/ADUANASOFT/anexo76 into development
This commit is contained in:
141
backend/main.py
141
backend/main.py
@@ -58,6 +58,11 @@ from api.v1.modules.a76.clients_and_providers.models import ClientProvider
|
||||
from api.v1.modules.a76.customs_brokers.models import CustomsBroker
|
||||
from api.v1.modules.a76.general_catalogs.company.models import Company
|
||||
|
||||
# Transportation Modules
|
||||
from api.v1.modules.a76.transportation.trailers.models import Trailer
|
||||
from api.v1.modules.a76.transportation.transporters.models import Transporter
|
||||
from api.v1.modules.a76.transportation.vehicles.models import Vehicle
|
||||
|
||||
# Core Modules & Transactional Models
|
||||
from api.v1.modules.a76.items.models import LineItem
|
||||
from api.v1.modules.a76.items.series.models import Serie
|
||||
@@ -146,6 +151,137 @@ def run_migrations():
|
||||
subprocess.run(["alembic", "upgrade", "head"], check=True)
|
||||
|
||||
|
||||
def create_transportation_tables():
|
||||
"""Crea las tablas de transporte directamente si no existen.
|
||||
Se usa en lugar de una migración Alembic para evitar gestionar versiones.
|
||||
"""
|
||||
from sqlalchemy import text
|
||||
from core.database import core_engine
|
||||
|
||||
ddl_statements = [
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS a76.transporter (
|
||||
transporter_key VARCHAR(23) PRIMARY KEY,
|
||||
name VARCHAR(256),
|
||||
short_name VARCHAR(10),
|
||||
responsible VARCHAR(100),
|
||||
rfc VARCHAR(30),
|
||||
streets VARCHAR(100),
|
||||
postal_code VARCHAR(15),
|
||||
city VARCHAR(30),
|
||||
state VARCHAR(30),
|
||||
country VARCHAR(3),
|
||||
loader_code VARCHAR(9),
|
||||
caat_code VARCHAR(49),
|
||||
transport_code VARCHAR(8),
|
||||
transport_interface_type VARCHAR(20),
|
||||
ftp_server VARCHAR(200),
|
||||
ftp_user VARCHAR(200),
|
||||
ftp_password VARCHAR(100),
|
||||
ftp_directory VARCHAR(1000),
|
||||
filler_code VARCHAR(20),
|
||||
tenant_id INTEGER NOT NULL REFERENCES core.tenants(id),
|
||||
company_id INTEGER NOT NULL REFERENCES a76.company(id),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT now(),
|
||||
deleted_at TIMESTAMP
|
||||
);
|
||||
""",
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS a76.trailer (
|
||||
trailer_number VARCHAR(20) PRIMARY KEY,
|
||||
ace_trailer_number VARCHAR(10),
|
||||
trailer_type_key VARCHAR(2),
|
||||
seal VARCHAR(15),
|
||||
entity_code VARCHAR(1),
|
||||
plate_number VARCHAR(17),
|
||||
state VARCHAR(30),
|
||||
country VARCHAR(3),
|
||||
container_key VARCHAR(3),
|
||||
tenant_id INTEGER NOT NULL REFERENCES core.tenants(id),
|
||||
company_id INTEGER NOT NULL REFERENCES a76.company(id),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT now(),
|
||||
deleted_at TIMESTAMP
|
||||
);
|
||||
""",
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS a76.vehicle (
|
||||
vehicle_key VARCHAR(14) PRIMARY KEY,
|
||||
ace_vehicle_key VARCHAR(10),
|
||||
transporter_key VARCHAR(23),
|
||||
transport_identifier VARCHAR(30),
|
||||
transport_type VARCHAR(2),
|
||||
entity_code VARCHAR(1),
|
||||
transponder_number VARCHAR(16),
|
||||
dot_number VARCHAR(8),
|
||||
plate_number VARCHAR(17),
|
||||
city VARCHAR(30),
|
||||
state VARCHAR(30),
|
||||
country VARCHAR(3),
|
||||
seal VARCHAR(49),
|
||||
insurance_company_name VARCHAR(30),
|
||||
insurance_number VARCHAR(20),
|
||||
insurance_amount NUMERIC(13, 2),
|
||||
insurance_date INTEGER,
|
||||
box_number VARCHAR(300),
|
||||
brand VARCHAR(20),
|
||||
year VARCHAR(4),
|
||||
series VARCHAR(30),
|
||||
description VARCHAR(100),
|
||||
engine_number VARCHAR(50),
|
||||
sct_permission VARCHAR(40),
|
||||
color VARCHAR(20),
|
||||
container_key VARCHAR(3),
|
||||
tenant_id INTEGER NOT NULL REFERENCES core.tenants(id),
|
||||
company_id INTEGER NOT NULL REFERENCES a76.company(id),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT now(),
|
||||
deleted_at TIMESTAMP
|
||||
);
|
||||
""",
|
||||
]
|
||||
|
||||
with core_engine.connect() as conn:
|
||||
for stmt in ddl_statements:
|
||||
conn.execute(text(stmt))
|
||||
# Ampliar columnas que pudieron haberse creado con tamaño incorrecto
|
||||
conn.execute(text("""
|
||||
DO $$
|
||||
BEGIN
|
||||
-- Fix transporter_key si fue creada como VARCHAR(5)
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema='a76' AND table_name='transporter'
|
||||
AND column_name='transporter_key'
|
||||
AND character_maximum_length < 23
|
||||
) THEN
|
||||
ALTER TABLE a76.transporter ALTER COLUMN transporter_key TYPE VARCHAR(23);
|
||||
END IF;
|
||||
-- Fix filler_code si fue creada como VARCHAR(4)
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema='a76' AND table_name='transporter'
|
||||
AND column_name='filler_code'
|
||||
AND character_maximum_length < 20
|
||||
) THEN
|
||||
ALTER TABLE a76.transporter ALTER COLUMN filler_code TYPE VARCHAR(20);
|
||||
END IF;
|
||||
|
||||
-- Eliminar constraint de trailer_type_key en trailer si existe
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_name='trailer_trailer_type_key_fkey'
|
||||
AND table_schema='a76' AND table_name='trailer'
|
||||
) THEN
|
||||
ALTER TABLE a76.trailer DROP CONSTRAINT trailer_trailer_type_key_fkey;
|
||||
END IF;
|
||||
END$$;
|
||||
"""))
|
||||
conn.commit()
|
||||
logger.info("Tablas de transporte verificadas/creadas correctamente.")
|
||||
|
||||
|
||||
# Inicializar la base de datos
|
||||
@app.on_event("startup")
|
||||
async def on_startup():
|
||||
@@ -153,6 +289,7 @@ async def on_startup():
|
||||
logger.info("Iniciando la aplicación Anexo76...")
|
||||
init_db()
|
||||
run_migrations()
|
||||
create_transportation_tables()
|
||||
logger.info("Base de datos inicializada correctamente.")
|
||||
|
||||
|
||||
@@ -266,6 +403,10 @@ def register_audit():
|
||||
CustomsBroker,
|
||||
Part,
|
||||
Company,
|
||||
# Transportation Modules
|
||||
Trailer,
|
||||
Transporter,
|
||||
Vehicle,
|
||||
# Reference Data
|
||||
Country,
|
||||
CurrencyType,
|
||||
|
||||
Reference in New Issue
Block a user