feat: compute_km_for_entry resolves generic moteur key to specific vehicle
This commit is contained in:
@@ -1,11 +1,29 @@
|
||||
def compute_km_for_entry(journey_profile_id: str | None, journeys: dict) -> dict[str, int]:
|
||||
def compute_km_for_entry(
|
||||
journey_profile_id: str | None,
|
||||
journeys: dict,
|
||||
motor_vehicle_id: str | None = None,
|
||||
) -> dict[str, int]:
|
||||
"""
|
||||
Retourne un dict {vehicle_id: km} pour un profil de trajet donné.
|
||||
La clé générique 'moteur' est remplacée par motor_vehicle_id si fourni.
|
||||
Retourne {} si pas de profil (TT, CONGE, etc.).
|
||||
"""
|
||||
if not journey_profile_id:
|
||||
return {}
|
||||
profile = journeys.get(journey_profile_id, {})
|
||||
return dict(profile.get("distances", {}))
|
||||
distances = dict(profile.get("distances", {}))
|
||||
|
||||
if "moteur" in distances:
|
||||
motor_km = distances.pop("moteur")
|
||||
if motor_vehicle_id:
|
||||
distances[motor_vehicle_id] = motor_km
|
||||
# Si pas de motor_vehicle_id, la distance moteur est ignorée
|
||||
|
||||
return distances
|
||||
|
||||
|
||||
def compute_co2_grams(km_by_vehicle: dict[str, int], vehicles: dict) -> float:
|
||||
"""Calcule le CO2 total en grammes pour un dict {vehicle_id: km}."""
|
||||
total = 0.0
|
||||
for vehicle_id, km in km_by_vehicle.items():
|
||||
vehicle = vehicles.get(vehicle_id, {})
|
||||
@@ -14,12 +32,16 @@ def compute_co2_grams(km_by_vehicle: dict[str, int], vehicles: dict) -> float:
|
||||
return total
|
||||
|
||||
|
||||
def compute_frais_reels(total_km_voiture: float, tranches: list[dict]) -> float:
|
||||
if not tranches or total_km_voiture <= 0:
|
||||
def compute_frais_reels(total_km_moteur: float, tranches: list[dict]) -> float:
|
||||
"""
|
||||
Calcule les frais réels fiscaux selon le barème kilométrique.
|
||||
km_max = 0 signifie "pas de limite" (dernière tranche).
|
||||
"""
|
||||
if not tranches or total_km_moteur <= 0:
|
||||
return 0.0
|
||||
for tranche in tranches:
|
||||
km_max = tranche["km_max"]
|
||||
if km_max == 0 or total_km_voiture <= km_max:
|
||||
return total_km_voiture * tranche["taux"] + tranche.get("forfait", 0)
|
||||
if km_max == 0 or total_km_moteur <= km_max:
|
||||
return total_km_moteur * tranche["taux"] + tranche.get("forfait", 0)
|
||||
last = tranches[-1]
|
||||
return total_km_voiture * last["taux"] + last.get("forfait", 0)
|
||||
return total_km_moteur * last["taux"] + last.get("forfait", 0)
|
||||
|
||||
Reference in New Issue
Block a user