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.
 
 

125 lines
4.2 KiB

  1. import base64
  2. from sqlalchemy.orm import Session
  3. from schemas.maintenancejob import CreateMaintenanceJob
  4. from db.models.maintenancejob import MaintenanceJob
  5. from schemas.carpart import CreateCarPart
  6. from db.models.carpart import CarPart
  7. from db.repository.user import get_car_driver
  8. def create_new_maintenancejob(maintenancejob: CreateMaintenanceJob, db: Session):
  9. print("Creating new maintenance job" + str(maintenancejob))
  10. vehicledriver = get_car_driver(maintenancejob.VehicleID, db)
  11. if vehicledriver != False:
  12. vehicledriver = vehicledriver.Id
  13. else:
  14. return "nodriver"
  15. maintenancejob_object = MaintenanceJob(
  16. Description=maintenancejob.Description,
  17. VehicleID=maintenancejob.VehicleID,
  18. Date=maintenancejob.Date,
  19. VehicleDriverId=get_car_driver(maintenancejob.VehicleID, db).Id,
  20. Status="Requested",
  21. )
  22. print("OBJECT CREATED")
  23. db.add(maintenancejob_object)
  24. db.commit()
  25. db.refresh(maintenancejob_object)
  26. return maintenancejob_object
  27. def create_car_part(car_part: CreateCarPart, db: Session):
  28. print("Creating new car part" + str(car_part))
  29. car_part_object = CarPart(
  30. ParentId=car_part.ParentId,
  31. Name=car_part.Name,
  32. Number=car_part.Number,
  33. Condition=car_part.Condition,
  34. ImageURL=car_part.image.file.read(),
  35. Cost=car_part.Cost,
  36. )
  37. print("OBJECT CREATED")
  38. db.add(car_part_object)
  39. db.commit()
  40. print("OBJECT SAVED")
  41. db.refresh(car_part_object)
  42. print("OBJECT REFRESHED")
  43. res_obj = car_part_object.__dict__
  44. res_obj["image"] = base64.b64encode(car_part_object.ImageURL).decode("ascii")
  45. return res_obj
  46. def calculate_total_cost(car_parts: CarPart):
  47. print("Calculating total cost")
  48. total_cost = 0
  49. for part in car_parts:
  50. total_cost += part.Cost
  51. return total_cost
  52. def get_all_maintenance_jobs(db: Session):
  53. print("Getting all maintenance jobs")
  54. maintenancejobs = db.query(MaintenanceJob).all()
  55. result = []
  56. for job in maintenancejobs:
  57. job_dict = job.__dict__
  58. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  59. for part in job_dict["CarPartsList"]:
  60. if (part["ImageURL"] is None) or (part["ImageURL"] == ""):
  61. part["image"] = ""
  62. else:
  63. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  64. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  65. if job.Status == "Complete":
  66. job_dict["FinishedBy"] = job.FinishedBy.__dict__
  67. job_dict["Vehicle"] = job.Vehicle.__dict__
  68. result.append(job_dict)
  69. return maintenancejobs
  70. def get_maintenance_job(maintenancejob_id: int, db: Session):
  71. print("Getting maintenance job by id: " + str(maintenancejob_id))
  72. maintenancejob = (
  73. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  74. )
  75. if maintenancejob is None:
  76. return None
  77. res = maintenancejob.__dict__
  78. res["CarPartsList"] = [part.__dict__ for part in maintenancejob.CarParts]
  79. for part in maintenancejob.CarPartsList:
  80. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  81. # print(type(result.CarPartsList))
  82. res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts)
  83. # print(result.TotalCost)
  84. if maintenancejob.Status == "Complete":
  85. res["FinishedBy"] = maintenancejob.FinishedBy.__dict__
  86. res["Vehicle"] = maintenancejob.Vehicle.__dict__
  87. return maintenancejob
  88. def change_maintenance_status(
  89. maintenancejob_id: int, status: str, worker_id: int, db: Session
  90. ):
  91. print(
  92. "Changing maintenance job status to "
  93. + status
  94. + " for job "
  95. + str(maintenancejob_id)
  96. + " by worker "
  97. + str(worker_id)
  98. )
  99. maintenancejob = (
  100. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  101. )
  102. if maintenancejob is None:
  103. return None
  104. if status == "Complete":
  105. maintenancejob.MaintenanceWorker = worker_id
  106. maintenancejob.Status = status
  107. db.commit()
  108. db.refresh(maintenancejob)
  109. return maintenancejob
  110. else:
  111. return "invalidstatus"