Files
plantillas-proyectos/backend/api/v1/modules/a76/items/common/fractions.py
acazares 0440543f24 Refactor item validation and service logic
- Consolidated item creation and update validation into a common function to reduce code duplication.
- Updated the `validate_create` and `validate_update` functions to utilize the new common validation logic.
- Introduced a new `common_validators.py` file for shared validation functions.
- Added a new `fractions.py` file to handle fraction-related logic and searches.
- Enhanced the `LineCustom` model to use an enumeration for `fraction_type`.
- Improved the `ItemService` class with methods for locking invoices and renumbering line items.
- Updated the `Sector` model to use a boolean type for the `authorized` field.
- Fixed import issues in the router by replacing the old `a24_router` with `sitar_router`.
2026-02-04 23:29:05 -06:00

80 lines
2.9 KiB
Python

import asyncio
from typing import Optional, Tuple
from api.v1.modules.a76.general_catalogs.company.models import Company
from api.v1.modules.sitar.tlcs import TLCSService
def _search_historical_fraction() -> Tuple[Optional[str], float]:
"""Search for historical fraction data
TODO: Implement historical fraction search logic
This corresponds to BUSCA_FRACCION_HISTORICA in original Clarion code
Returns:
Tuple of (rate_im, adv_impo)
"""
# Placeholder for historical search
return None, 0.0
def search_fraction_preference(
country: str, fraction_type: str, company: Company, fraccion: str
) -> Tuple[Optional[str], float]:
"""Search fraction preference and return rate_im and adv_impo
Args:
country: Country code
fraction_type: Type of fraction (e.g., 'TLCS')
company: Company object with configuration
fraccion: Tariff fraction code to search
Returns:
Tuple of (rate_im, adv_impo) where:
- rate_im: Tax rate as string (e.g., "EXE", "5.0%")
- adv_impo: Numeric ad valorem rate
"""
rate_im = None
adv_impo = 0.0
country_group = "USA" if country == "MEX" else country
if fraction_type.upper() == "TLCS":
# Si tiene seventh_amendment configurado, consulta API externa
if company.seventh_amendment:
try:
# Get TLCS service instance
tlcs_service = TLCSService.get_instance()
# Fetch TLCS data using the service
tlcs_data = asyncio.run(
tlcs_service.search(
fraccion=fraccion, pais=country_group, limit=100
)
)
if not tlcs_data:
# No se encontraron registros, buscar en histórico
print(
f"No TLCS data found for fraction {fraccion[:8]}, searching historical"
)
return _search_historical_fraction()
else:
# Se encontraron registros, tomar el primero
first_record = tlcs_data[0]
rate_im = first_record.TASATXT
adv_impo = float(first_record.TASA1NUM or 0.0)
print(f"Found TLCS data: rate_im={rate_im}, adv_impo={adv_impo}")
except Exception as e:
print(f"Error fetching TLCS data from SITAR API: {e}")
return _search_historical_fraction()
else:
# Sin seventh_amendment, buscar en tabla local (base de datos)
# TODO: Implement local database search for TLCS
# This corresponds to the Access:GFracTLCSSifra.TryFetch logic in Clarion
print("Local TLCS search not implemented yet")
# Placeholder: search historical as fallback
return _search_historical_fraction()
return rate_im, adv_impo