Sistema CRUD sin servicio sitar
This commit is contained in:
@@ -8,7 +8,7 @@ from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.database import get_core_db
|
||||
from core.security import get_current_user
|
||||
from core.security import get_current_user, get_tenant_from_token
|
||||
|
||||
from .dto import (
|
||||
TariffFractionCreateDTO,
|
||||
@@ -27,6 +27,7 @@ router = APIRouter(prefix="/tariff-fractions", tags=["a76 / general catalogs / t
|
||||
description="Get paginated list of Tariff Fractions with optional search filter (global catalog)",
|
||||
)
|
||||
async def list_tariff_fractions(
|
||||
company_id: int = Query(..., description="Company ID"),
|
||||
page: int = Query(1, ge=1, description="Page number"),
|
||||
page_size: int = Query(50, ge=1, le=10000, description="Page size"),
|
||||
search: Optional[str] = Query(None, description="Search in code, fraction, description, nico, or umt"),
|
||||
@@ -47,8 +48,9 @@ async def list_tariff_fractions(
|
||||
# Service.get_all calls Sitar (async) or DB (sync).
|
||||
# This should be fine.
|
||||
|
||||
tenant_id = current_user.get("tenant_id")
|
||||
company_id = current_user.get("company_id") # Assuming user is context-aware or we use a default?
|
||||
tenant_id = get_tenant_from_token(current_user)
|
||||
if tenant_id is None:
|
||||
tenant_id = current_user.get("tenant_id")
|
||||
# If using headers for selected company, it might be in current_user context if middleware sets it.
|
||||
|
||||
items, total = await TariffFractionService.get_all(
|
||||
@@ -121,7 +123,7 @@ async def create_tariff_fraction(
|
||||
pass
|
||||
|
||||
us_dto = USTariffFractionCreateDTO(
|
||||
code=fraction_data.code,
|
||||
code=fraction_data.fraction, # Store the punctuated fraction in the DB
|
||||
description=fraction_data.description,
|
||||
unit_of_measure=fraction_data.umt,
|
||||
ad_valorem=ad_valorem,
|
||||
@@ -131,7 +133,7 @@ async def create_tariff_fraction(
|
||||
fixed_cost=None
|
||||
)
|
||||
|
||||
created = USTariffFractionService.create(db, tenant_id, company_id, us_dto)
|
||||
created = USTariffFractionService.create(db, us_dto, tenant_id, company_id)
|
||||
return TariffFractionService.to_domain_usa_local(created)
|
||||
|
||||
else:
|
||||
@@ -171,12 +173,13 @@ async def update_tariff_fraction(
|
||||
pass
|
||||
|
||||
us_dto = USTariffFractionUpdateDTO(
|
||||
code=fraction_data.fraction,
|
||||
description=fraction_data.description,
|
||||
unit_of_measure=fraction_data.umt,
|
||||
ad_valorem=ad_valorem
|
||||
)
|
||||
|
||||
updated = USTariffFractionService.update(db, tenant_id, company_id, tariff_fraction_id, us_dto)
|
||||
updated = USTariffFractionService.update(db, tariff_fraction_id, tenant_id, us_dto, company_id)
|
||||
if not updated:
|
||||
raise HTTPException(status_code=404, detail="US Tariff fraction not found")
|
||||
return TariffFractionService.to_domain_usa_local(updated)
|
||||
@@ -202,7 +205,7 @@ async def delete_tariff_fraction(
|
||||
|
||||
if catalog == "american":
|
||||
from api.v1.modules.a76.general_catalogs.fractions.us_tariff_fractions.service import USTariffFractionService
|
||||
success = USTariffFractionService.delete(db, tenant_id, company_id, tariff_fraction_id)
|
||||
success = USTariffFractionService.delete(db, tariff_fraction_id, tenant_id, company_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=404, detail="US Tariff fraction not found")
|
||||
return {"ok": True}
|
||||
|
||||
@@ -96,10 +96,14 @@ class TariffFractionService:
|
||||
# US format: 1234.56.78.90. For now return as is or use helper if available.
|
||||
# item is USTariffFraction (imported inside method to avoid circular import if needed, or assumed available)
|
||||
|
||||
# Remove formatting (e.g. dots) for the 'code' property
|
||||
code_str = str(item.code)
|
||||
clean_code = code_str.replace(".", "").replace("-", "")
|
||||
|
||||
return TariffFraction(
|
||||
id=item.id,
|
||||
code=item.code,
|
||||
fraction=item.code, # TODO: Format if needed
|
||||
code=clean_code,
|
||||
fraction=code_str,
|
||||
description=item.description or "(Sin descripción)",
|
||||
nico=None,
|
||||
umt=item.unit_of_measure,
|
||||
@@ -133,11 +137,11 @@ class TariffFractionService:
|
||||
from api.v1.modules.a76.general_catalogs.fractions.us_tariff_fractions.service import USTariffFractionService
|
||||
|
||||
# Use local service directly
|
||||
usa_items, total = USTariffFractionService._get_all_local(
|
||||
usa_items, total = USTariffFractionService.get_all(
|
||||
db, tenant_id, company_id, skip, limit, filters
|
||||
)
|
||||
|
||||
items = [TariffFractionMapper.to_domain_usa_local(item) for item in usa_items]
|
||||
items = [TariffFractionService.to_domain_usa_local(item) for item in usa_items]
|
||||
return items, total
|
||||
|
||||
# USA CATALOG HANDLING (API - 'Fracciones US')
|
||||
|
||||
@@ -3,9 +3,9 @@ DTOs para fracciones arancelarias americanas
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from typing import Optional, Any
|
||||
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from pydantic import BaseModel, Field, ConfigDict, model_validator
|
||||
|
||||
|
||||
class USTariffFractionCreateDTO(BaseModel):
|
||||
@@ -23,6 +23,7 @@ class USTariffFractionCreateDTO(BaseModel):
|
||||
class USTariffFractionUpdateDTO(BaseModel):
|
||||
"""DTO para actualizar fracción arancelaria americana"""
|
||||
|
||||
code: Optional[str] = Field(None, max_length=16)
|
||||
prefix: Optional[str] = Field(None, max_length=10)
|
||||
type_code: Optional[str] = Field(None, max_length=10)
|
||||
ad_valorem: Optional[float] = None
|
||||
@@ -38,6 +39,7 @@ class USTariffFractionResponseDTO(BaseModel):
|
||||
|
||||
id: int
|
||||
code: str
|
||||
fraction: Optional[str] = None
|
||||
prefix: Optional[str] = None
|
||||
type_code: Optional[str] = None
|
||||
ad_valorem: Optional[float] = None
|
||||
@@ -46,3 +48,36 @@ class USTariffFractionResponseDTO(BaseModel):
|
||||
description: Optional[str] = None
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def format_code_and_fraction(cls, data: Any) -> Any:
|
||||
# Check if data is an ORM model or dict
|
||||
if hasattr(data, "code"):
|
||||
raw_code = data.code
|
||||
elif isinstance(data, dict):
|
||||
raw_code = data.get("code")
|
||||
else:
|
||||
return data
|
||||
|
||||
if raw_code:
|
||||
code_str = str(raw_code)
|
||||
# fraction keeps the original formatted string
|
||||
fraction = code_str
|
||||
# code strips dots and hyphens
|
||||
code = code_str.replace(".", "").replace("-", "")
|
||||
|
||||
if isinstance(data, dict):
|
||||
data["code"] = code
|
||||
data["fraction"] = fraction
|
||||
else:
|
||||
# If it's an ORM object, we can't easily modify the object's attribute
|
||||
# cleanly without side effects for other things, so we convert it to dict
|
||||
new_data = {
|
||||
c.name: getattr(data, c.name) for c in data.__table__.columns
|
||||
}
|
||||
new_data["code"] = code
|
||||
new_data["fraction"] = fraction
|
||||
return new_data
|
||||
|
||||
return data
|
||||
|
||||
@@ -26,6 +26,7 @@ crud_router = TenantCRUDRoutes(
|
||||
prefix="/us-tariff-fractions",
|
||||
tags=["a76 / general catalogs / us tariff fractions"],
|
||||
resource_name="US Tariff Fraction",
|
||||
id_name="id",
|
||||
enable_list=False, # We implement our custom list endpoint
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user