56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from ninja import Router
|
|
from pydantic import BaseModel
|
|
from typing import List, Optional
|
|
from decimal import Decimal
|
|
from .services.sii import SIIService
|
|
from .models import Project
|
|
|
|
router = Router()
|
|
|
|
class RUTValidationResponse(BaseModel):
|
|
is_valid: bool
|
|
formatted_rut: str
|
|
error_message: Optional[str] = None
|
|
|
|
@router.get("/sii/validate-rut", response=RUTValidationResponse)
|
|
def validate_rut(request, rut: str):
|
|
"""
|
|
Valida un RUT chileno usando la lógica del SIIService.
|
|
"""
|
|
is_valid, result = SIIService.validate_rut(rut)
|
|
if is_valid:
|
|
return RUTValidationResponse(is_valid=True, formatted_rut=result)
|
|
else:
|
|
return RUTValidationResponse(is_valid=False, formatted_rut="", error_message=result)
|
|
|
|
class ProjectOut(BaseModel):
|
|
id: int
|
|
code: str
|
|
name: str
|
|
|
|
@router.get("/projects/", response=List[ProjectOut])
|
|
def get_projects(request):
|
|
"""
|
|
Obtiene la lista de proyectos reales (excluye Portafolios y Centros de Costo).
|
|
"""
|
|
# Proyectos reales: parent__parent__isnull=False
|
|
qs = Project.objects.filter(parent__parent__isnull=False, status='Activo').order_by('name')
|
|
return [ProjectOut(id=p.id, code=p.code, name=p.name) for p in qs]
|
|
|
|
class TaxCalculationResponse(BaseModel):
|
|
net_amount: float
|
|
tax_amount: float
|
|
total_amount: float
|
|
|
|
@router.get("/sii/calculate-tax", response=TaxCalculationResponse)
|
|
def calculate_tax(request, amount: float, tax_type: str):
|
|
"""
|
|
Calcula el impuesto y el total basado en el tipo de documento.
|
|
"""
|
|
res = SIIService.calculate_taxes(amount, tax_type)
|
|
return TaxCalculationResponse(
|
|
net_amount=float(res['net_amount']),
|
|
tax_amount=float(res['tax_amount']),
|
|
total_amount=float(res['total_amount'])
|
|
)
|