You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

78 lines
2.8 KiB

  1. from datetime import datetime
  2. from sqlalchemy.orm import Session
  3. from db.models.drivetask import DriveTask
  4. from db.models.user import User
  5. from db.models.fuelingtask import FuelingTask
  6. from db.models.maintenancejob import MaintenanceJob
  7. from db.repository.maintenancejob import calculate_total_cost
  8. def get_repot_jobsdone_by_driver(driver_id: int, db: Session):
  9. driver = db.query(User).filter(User.Id == driver_id).first()
  10. if not driver:
  11. return "notdriver"
  12. if driver.Role != "Driver":
  13. return "notdriver"
  14. tasks = db.query(DriveTask).filter((DriveTask.DriverId == driver_id) & (DriveTask.Status == "Completed")).all()
  15. #order tasks by completion date
  16. tasks.sort(key=lambda x: x.EndDateTime)
  17. #for each task, add TOTAL distance driven at the time of completion
  18. dist = 0
  19. taskslist = []
  20. for task in tasks:
  21. dist += task.DistanceCovered
  22. taskslist.append(task.__dict__)
  23. taskslist[-1]["DistanceAtTime"] = dist
  24. res = {}
  25. res["tasks"] = taskslist
  26. res["Driver"] = driver.__dict__
  27. res["Driver"]["AssignedVehicle"] = driver.vehicle
  28. dist = 0
  29. for task in tasks:
  30. dist += task.DistanceCovered
  31. res["TotalDistance"] = dist
  32. timespent = 0.0
  33. for task in tasks:
  34. edate = task.EndDateTime
  35. sdate = task.StartDateTime
  36. if type(edate) == str:
  37. edate = datetime.strptime(edate, "%Y-%m-%d %H:%M:%S.%f")
  38. timetaken = edate - sdate
  39. timespent += timetaken.total_seconds()
  40. #time spent in hours and minutes
  41. hours = timespent // 3600
  42. minutes = (timespent % 3600) // 60
  43. res["TotalTime"] = str(hours) + " hours " + str(minutes) + " minutes"
  44. #add fuel expenditures
  45. fuel = db.query(FuelingTask).filter(FuelingTask.DriverId == driver_id).all()
  46. fuel.sort(key=lambda x: x.Date)
  47. totalfuelspent = 0
  48. fuelspent = []
  49. for f in fuel:
  50. fuelpoint = {}
  51. totalfuelspent += f.FuelRefilled
  52. fuelpoint["fuelrefilled"] = f.FuelRefilled
  53. fuelpoint["total"] = totalfuelspent
  54. fuelpoint["date"] = f.Date
  55. fuelspent.append(fuelpoint)
  56. res["FuelSpent"] = fuelspent
  57. res["TotalFuelSpent"] = totalfuelspent
  58. #add maintenance costs
  59. mainten = db.query(MaintenanceJob).filter(MaintenanceJob.VehicleDriverId == driver_id).all()
  60. mainten.sort(key=lambda x: x.Date)
  61. totalmaintenspent = 0
  62. maintenspent = []
  63. for m in mainten:
  64. maintenpoint = {}
  65. cost = calculate_total_cost(m.CarParts)
  66. totalmaintenspent += cost
  67. maintenpoint["cost"] = cost
  68. maintenpoint["total"] = totalmaintenspent
  69. maintenpoint["date"] = m.Date
  70. maintenspent.append(maintenpoint)
  71. res["MaintenanceSpent"] = maintenspent
  72. res["TotalMaintenanceSpent"] = totalmaintenspent
  73. return res