Sistema completo para monitoreo y gestion de flotas de vehiculos con: - Backend FastAPI con PostgreSQL/TimescaleDB - Frontend React con TypeScript y TailwindCSS - App movil React Native con Expo - Soporte para dispositivos GPS, Meshtastic y celulares - Video streaming en vivo con MediaMTX - Geocercas, alertas, viajes y reportes - Autenticacion JWT y WebSockets en tiempo real Documentacion completa y guias de usuario incluidas.
101 lines
3.1 KiB
Python
101 lines
3.1 KiB
Python
"""
|
|
Modelo de Carga de Combustible para registrar recargas de combustible.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import (
|
|
DateTime,
|
|
Float,
|
|
ForeignKey,
|
|
Index,
|
|
String,
|
|
Text,
|
|
)
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.core.database import Base
|
|
from app.models.base import TimestampMixin
|
|
|
|
|
|
class CargaCombustible(Base, TimestampMixin):
|
|
"""Modelo para registrar cargas de combustible de los vehículos."""
|
|
|
|
__tablename__ = "cargas_combustible"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
|
|
# Relaciones
|
|
vehiculo_id: Mapped[int] = mapped_column(
|
|
ForeignKey("vehiculos.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
conductor_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("conductores.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
index=True,
|
|
)
|
|
|
|
# Fecha y hora de la carga
|
|
fecha: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
|
|
|
# Cantidad y precio
|
|
litros: Mapped[float] = mapped_column(Float, nullable=False)
|
|
precio_litro: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
total: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
|
|
# Tipo de combustible
|
|
tipo_combustible: Mapped[str | None] = mapped_column(String(20), nullable=True) # gasolina, diesel, premium
|
|
|
|
# Odómetro al momento de la carga
|
|
odometro: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
|
|
# Estación de servicio
|
|
estacion: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
|
estacion_direccion: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
|
|
# Ubicación de la carga
|
|
lat: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
lng: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
|
|
# Tanque lleno (para cálculo de rendimiento)
|
|
tanque_lleno: Mapped[bool] = mapped_column(default=True, nullable=False)
|
|
|
|
# Método de pago
|
|
metodo_pago: Mapped[str | None] = mapped_column(String(50), nullable=True) # efectivo, tarjeta, vales
|
|
numero_factura: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
|
|
|
# Notas
|
|
notas: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
# Relaciones ORM
|
|
vehiculo: Mapped["Vehiculo"] = relationship(
|
|
"Vehiculo",
|
|
back_populates="cargas_combustible",
|
|
lazy="selectin",
|
|
)
|
|
conductor: Mapped["Conductor | None"] = relationship(
|
|
"Conductor",
|
|
back_populates="cargas_combustible",
|
|
lazy="selectin",
|
|
)
|
|
|
|
# Índices
|
|
__table_args__ = (
|
|
Index("idx_cargas_vehiculo_fecha", "vehiculo_id", "fecha"),
|
|
)
|
|
|
|
@property
|
|
def rendimiento_calculado(self) -> float | None:
|
|
"""
|
|
Calcula el rendimiento en km/litro si hay datos suficientes.
|
|
|
|
Este cálculo requiere la carga anterior para comparar odómetros.
|
|
Se implementa en el servicio de combustible.
|
|
"""
|
|
return None # Se calcula en el servicio
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<CargaCombustible(id={self.id}, vehiculo_id={self.vehiculo_id}, litros={self.litros})>"
|