Resolviendo conflicto

This commit is contained in:
2026-05-08 17:44:16 -05:00
39 changed files with 1499 additions and 386 deletions

View File

@@ -9,7 +9,12 @@ from sqlalchemy.orm import Session
from api.v1.modules.a76.general_catalogs.company.models import Company
from api.v1.modules.a76.invoices.models import InvoiceHeader
from core.database import rls_company_var, rls_tenant_var
from core.database import (
RLS_COMPANY_KEY,
RLS_TENANT_KEY,
rls_company_var,
rls_tenant_var,
)
from .services.service import AuditService
from .utils.serialization import serialize_for_json
@@ -56,13 +61,17 @@ def _resolve_audit_company_tenant(session: Session, target) -> tuple:
if company_id is not None:
resolution_source = "company_self_id"
if company_id is None:
company_id = rls_company_var.get()
company_id = session.info.get(RLS_COMPANY_KEY)
if company_id is None:
company_id = rls_company_var.get()
if company_id is not None:
resolution_source = "rls_context"
tenant_id = getattr(target, "tenant_id", None)
if tenant_id is None:
tenant_id = rls_tenant_var.get()
tenant_id = session.info.get(RLS_TENANT_KEY)
if tenant_id is None:
tenant_id = rls_tenant_var.get()
if tenant_id is not None and resolution_source == "target":
resolution_source = "rls_context"

View File

@@ -164,16 +164,13 @@ class TariffFractionService:
search_description = term
try:
usa_items = await usa_service.search(
usa_items, total = await usa_service.search_with_total(
fraccion=search_term,
descripcion=search_description,
skip=skip,
limit=limit,
)
items = [TariffFractionMapper.to_domain_usa(item) for item in usa_items]
total = len(items) + skip
if len(items) == limit:
total += 1
return items, total
except Exception as e:
import traceback
@@ -245,30 +242,25 @@ class TariffFractionService:
# Note: Sitar search might not return total count.
# We fetch page items. Pagination might be tricky if Sitar doesn't return total.
# Assuming Sitar returns a list.
sitar_items = await sitar_service.search(
sitar_items, total = await sitar_service.search_with_total(
fraccion=sitar_fraccion,
nico=sitar_nico,
description=sitar_description,
nivel=level_filter, # Dynamic level
skip=skip,
limit=limit
limit=limit,
)
# STRICT API USAGE:
# We do NOT fallback to local DB on empty list, as user requested strict API consumption.
# We also do NOT attempt enrichment as codes mismatch (API uses '010191A' vs Local '01012101').
# Map items
items = [TariffFractionMapper.to_domain(item) for item in sitar_items]
# Legacy browse behavior: keep table in ascending fracción order.
items = sorted(items, key=lambda row: ((row.code or ""), (row.nico or "")))
# Estimate total (Sitar service doesn't return total currently)
# If we got full limit, assume there are more.
total = len(items) + skip
if len(items) == limit:
total += 1 # Indicate more pages
# total comes from SITAR PaginatedFraccionesResponse (matches API-wide count for the query).
return items, total
except Exception as e:

View File

@@ -103,7 +103,7 @@ async def list_us_tariff_fractions(
search_description = search
try:
sitar_items = await svc.search(
sitar_items, total = await svc.search_with_total(
fraccion=search_term,
descripcion=search_description,
skip=skip,
@@ -118,10 +118,6 @@ async def list_us_tariff_fractions(
"pages": 0,
}
total = len(sitar_items) + skip
if len(sitar_items) == page_size:
total += 1
items = [
USTariffFractionResponseDTO.model_validate(_sitar_row_to_us_response_payload(row))
for row in sitar_items

View File

@@ -3,6 +3,7 @@ from typing import Dict, Any, Optional
from core.config import settings
from core.database import get_core_db
from core.exceptions import BaseAPIException
from core.security import collect_user_role_names, get_current_user, validate_access_to_resource
from fastapi import APIRouter, Depends, HTTPException, Query, Path
from sqlalchemy import func, or_, and_
@@ -69,6 +70,8 @@ def get_creation_data(
return InvoiceCatalogService.get_creation_data(db, tenant_id, company_id)
except HTTPException:
raise
except BaseAPIException:
raise
except Exception as e:
logger.exception("get_creation_data failed: %s", e)
raise HTTPException(status_code=500, detail=f"Error al cargar datos de creación: {str(e)}")
@@ -97,6 +100,8 @@ def get_edition_data(
return data
except HTTPException:
raise
except BaseAPIException:
raise
except Exception as e:
logger.exception("get_edition_data failed: %s", e)
raise HTTPException(status_code=500, detail=f"Error al cargar datos de edición: {str(e)}")
@@ -191,6 +196,8 @@ def create_invoice(
return services.InvoiceService.create(db, data, tenant_id, company_id)
except HTTPException:
raise
except BaseAPIException:
raise
except Exception as e:
logger.exception("create_invoice failed: %s", e)
raise HTTPException(status_code=500, detail=f"Error al guardar factura: {str(e)}")
@@ -337,6 +344,8 @@ def list_invoices(
"page": page,
"page_size": page_size
}
except BaseAPIException:
raise
except Exception as e:
logger.exception("list_invoices failed: %s", e)
raise HTTPException(status_code=500, detail=f"Internal server error in invoices list: {str(e)}")