This commit is contained in:
@@ -50,6 +50,51 @@ def line_total(quantity: Decimal, unit_price: Decimal | None, discount: Decimal)
|
||||
return max(Decimal("0"), Decimal(quantity) * money(unit_price) - money(discount)).quantize(Decimal("0.01"))
|
||||
|
||||
|
||||
def _product_profile_label(product: ServiceProductItem) -> str | None:
|
||||
details = [product.viscosity, product.specification]
|
||||
label = " ".join(str(item).strip() for item in details if item).strip()
|
||||
return label or product.specification or product.title or None
|
||||
|
||||
|
||||
def _product_volume(product: ServiceProductItem) -> Decimal | None:
|
||||
if product.used_volume is not None:
|
||||
return product.used_volume
|
||||
if product.volume is not None:
|
||||
return product.volume
|
||||
if product.unit == "l":
|
||||
return product.quantity
|
||||
return None
|
||||
|
||||
|
||||
def sync_vehicle_profile_from_products(vehicle: Car, product_items: list[ServiceProductItem]) -> dict[str, str]:
|
||||
updates: dict[str, str] = {}
|
||||
for product in product_items:
|
||||
category = product.category
|
||||
label = _product_profile_label(product)
|
||||
volume = _product_volume(product)
|
||||
if category == "engine_oil":
|
||||
if label and not vehicle.engine_oil_type:
|
||||
vehicle.engine_oil_type = label
|
||||
updates["engine_oil_type"] = label
|
||||
if volume is not None and vehicle.engine_oil_volume_l is None:
|
||||
vehicle.engine_oil_volume_l = volume
|
||||
updates["engine_oil_volume_l"] = str(volume)
|
||||
elif category == "transmission_fluid":
|
||||
if label and not vehicle.transmission_fluid_type:
|
||||
vehicle.transmission_fluid_type = label
|
||||
updates["transmission_fluid_type"] = label
|
||||
if volume is not None and vehicle.transmission_fluid_volume_l is None:
|
||||
vehicle.transmission_fluid_volume_l = volume
|
||||
updates["transmission_fluid_volume_l"] = str(volume)
|
||||
elif category == "coolant" and label and not vehicle.coolant_type:
|
||||
vehicle.coolant_type = label
|
||||
updates["coolant_type"] = label
|
||||
elif category == "brake_fluid" and label and not vehicle.brake_fluid_type:
|
||||
vehicle.brake_fluid_type = label
|
||||
updates["brake_fluid_type"] = label
|
||||
return updates
|
||||
|
||||
|
||||
async def add_status_history(
|
||||
session: AsyncSession,
|
||||
visit: ServiceVisit,
|
||||
@@ -294,6 +339,7 @@ async def close_work_order(
|
||||
source_appointment_id=appointment.id if appointment else None,
|
||||
)
|
||||
)
|
||||
vehicle_profile_updates = sync_vehicle_profile_from_products(vehicle, product_items)
|
||||
visit.version = (visit.version or 1) + 1
|
||||
visit.completed_snapshot = {
|
||||
"work_order_number": visit.work_order_number,
|
||||
@@ -306,6 +352,7 @@ async def close_work_order(
|
||||
"final_total": str(visit.final_total),
|
||||
"currency": visit.currency,
|
||||
"completed_at": visit.completed_at.isoformat() if visit.completed_at else None,
|
||||
"vehicle_profile_updates": vehicle_profile_updates,
|
||||
}
|
||||
await create_service_notification(
|
||||
session,
|
||||
|
||||
Reference in New Issue
Block a user