Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. from datetime import datetime
  2. from fastapi.responses import StreamingResponse
  3. from sqlalchemy.orm import Session
  4. from db.models.drivetask import DriveTask
  5. from db.models.user import User
  6. from db.models.fuelingtask import FuelingTask
  7. from db.models.maintenancejob import MaintenanceJob
  8. from db.repository.maintenancejob import calculate_total_cost
  9. from reportlab.lib import colors
  10. from reportlab.lib.pagesizes import letter
  11. from reportlab.platypus import (
  12. SimpleDocTemplate,
  13. Table,
  14. TableStyle,
  15. Paragraph,
  16. Image,
  17. PageBreak,
  18. )
  19. from reportlab.lib.styles import getSampleStyleSheet
  20. from reportlab.lib.units import inch
  21. from reportlab.graphics.shapes import Drawing
  22. from reportlab.graphics.charts.linecharts import HorizontalLineChart
  23. from reportlab.graphics.widgets.markers import makeMarker
  24. from reportlab.platypus import Spacer
  25. from reportlab.graphics.charts.axes import CategoryAxis
  26. from dateutil import parser
  27. def get_repot_jobsdone_by_driver(driver_id: int, db: Session):
  28. driver = db.query(User).filter(User.Id == driver_id).first()
  29. if not driver:
  30. return "notdriver"
  31. if driver.Role != "Driver":
  32. return "notdriver"
  33. tasks = (
  34. db.query(DriveTask)
  35. .filter((DriveTask.DriverId == driver_id) & (DriveTask.Status == "Completed"))
  36. .all()
  37. )
  38. # order tasks by completion date
  39. tasks.sort(key=lambda x: x.EndDateTime)
  40. # for each task, add TOTAL distance driven at the time of completion
  41. dist = 0
  42. taskslist = []
  43. for task in tasks:
  44. dist += task.DistanceCovered
  45. taskslist.append(task.__dict__)
  46. taskslist[-1]["DistanceAtTime"] = dist
  47. res = {}
  48. res["tasks"] = taskslist
  49. res["Driver"] = driver.__dict__
  50. res["Driver"]["AssignedVehicle"] = driver.vehicle
  51. dist = 0
  52. for task in tasks:
  53. dist += task.DistanceCovered
  54. res["TotalDistance"] = dist
  55. timespent = 0.0
  56. for task in tasks:
  57. edate = task.EndDateTime
  58. sdate = task.StartDateTime
  59. if edate is None or sdate is None:
  60. continue
  61. if type(edate) == str:
  62. edate = datetime.strptime(edate, "%Y-%m-%d %H:%M:%S.%f")
  63. timetaken = edate - sdate
  64. timespent += timetaken.total_seconds()
  65. # time spent in hours and minutes
  66. hours = timespent // 3600
  67. minutes = (timespent % 3600) // 60
  68. res["TotalTime"] = str(hours) + " hours " + str(minutes) + " minutes"
  69. # add fuel expenditures
  70. fuel = db.query(FuelingTask).filter(FuelingTask.DriverId == driver_id).all()
  71. fuel.sort(key=lambda x: x.Date)
  72. totalfuelspent = 0
  73. fuelspent = []
  74. for f in fuel:
  75. fuelpoint = {}
  76. totalfuelspent += f.FuelRefilled
  77. fuelpoint["fuelrefilled"] = f.FuelRefilled
  78. fuelpoint["total"] = totalfuelspent
  79. fuelpoint["date"] = f.Date
  80. fuelspent.append(fuelpoint)
  81. res["FuelSpent"] = fuelspent
  82. res["TotalFuelSpent"] = totalfuelspent
  83. # add maintenance costs
  84. mainten = (
  85. db.query(MaintenanceJob)
  86. .filter(MaintenanceJob.VehicleDriverId == driver_id)
  87. .all()
  88. )
  89. mainten.sort(key=lambda x: x.Date)
  90. totalmaintenspent = 0
  91. maintenspent = []
  92. for m in mainten:
  93. maintenpoint = {}
  94. cost = calculate_total_cost(m.CarParts)
  95. totalmaintenspent += cost
  96. maintenpoint["cost"] = cost
  97. maintenpoint["total"] = totalmaintenspent
  98. maintenpoint["date"] = m.Date
  99. maintenspent.append(maintenpoint)
  100. res["MaintenanceSpent"] = maintenspent
  101. res["TotalMaintenanceSpent"] = totalmaintenspent
  102. return res
  103. def get_pdf(driver_id: int, db: Session):
  104. report_data = get_repot_jobsdone_by_driver(driver_id, db)
  105. if report_data == "notdriver":
  106. return "notdriver"
  107. # Assuming your report data is stored in a variable named 'report_data'
  108. # Create a PDF document
  109. pdf_filename = "report1.pdf"
  110. document = SimpleDocTemplate(pdf_filename, pagesize=letter)
  111. # Create a list to hold the contents of the PDF
  112. content = []
  113. # Add a title to the PDF
  114. title_style = getSampleStyleSheet()["Title"]
  115. title = Paragraph("Driver Report", title_style)
  116. content.append(title)
  117. content.append(Paragraph("<br/>", title_style))
  118. # Add detailed information about the driver
  119. driver_info = [
  120. ["Driver Name", report_data["Driver"]["Name"]],
  121. ["Last Name", report_data["Driver"]["LastName"]],
  122. ["Government ID", report_data["Driver"]["GovernmentId"]],
  123. ["Email", report_data["Driver"]["Email"]],
  124. ["Contact Number", report_data["Driver"]["ContactNumber"]],
  125. ["Address", report_data["Driver"]["Address"]],
  126. ["Role", report_data["Driver"]["Role"]],
  127. ]
  128. driver_table_style = TableStyle(
  129. [
  130. ("ALIGN", (0, 0), (-1, -1), "LEFT"),
  131. ("BOX", (0, 0), (-1, -1), 1, colors.black),
  132. ]
  133. )
  134. driver_table = Table(driver_info, style=driver_table_style)
  135. content.append(Paragraph("<br/><br/>", title_style))
  136. vehicle = report_data["Driver"]["AssignedVehicle"].__dict__
  137. if (
  138. vehicle["CurrentLocation"] is None
  139. or vehicle["CurrentLocation"] == ""
  140. or vehicle["CurrentLocation"] == False
  141. or len(vehicle["CurrentLocation"]) == 0
  142. ):
  143. vehicle["CurrentLocation"] = ["Unknown", "Unknown"]
  144. loc = vehicle["CurrentLocation"][0]
  145. if len(vehicle["CurrentLocation"]) > 1:
  146. loc += ", " + vehicle["CurrentLocation"][1]
  147. # Add vehicle details
  148. vehicle_info = [
  149. ["Vehicle Type", vehicle["Type"]],
  150. ["Vehicle Model", vehicle["Model"]],
  151. ["Year", vehicle["Year"]],
  152. ["License Plate", vehicle["LicensePlate"]],
  153. ["Current Location", loc],
  154. ["Mileage", vehicle["Mileage"]],
  155. ["Capacity", vehicle["Capacity"]],
  156. ]
  157. vehicle_table_style = TableStyle(
  158. [
  159. ("ALIGN", (0, 0), (-1, -1), "LEFT"),
  160. ("BOX", (0, 0), (-1, -1), 1, colors.black),
  161. ]
  162. )
  163. vehicle_table = Table(vehicle_info, style=vehicle_table_style)
  164. content.append(Spacer(1, 12)) # Add space between the tables
  165. content.append(Table([[driver_table, vehicle_table]]))
  166. content.append(Paragraph("<br/><br/>", title_style))
  167. total_info = [
  168. ["Total Tasks Done", report_data["tasks"].__len__()],
  169. ["Total Fuel Spent", report_data["TotalFuelSpent"]],
  170. ["Total Time Spent", report_data["TotalTime"]],
  171. ["Total Distance Driven", report_data["TotalDistance"]],
  172. ["Total Money Spent", report_data["TotalMaintenanceSpent"]],
  173. ]
  174. total_table_style = TableStyle(
  175. [
  176. ("ALIGN", (0, 0), (-1, -1), "LEFT"),
  177. ("BOX", (0, 0), (-1, -1), 1, colors.black),
  178. ]
  179. )
  180. total_table = Table(total_info, style=total_table_style)
  181. content.append(total_table)
  182. content.append(Paragraph("<br/><br/>", title_style))
  183. content.append(PageBreak())
  184. title_style = getSampleStyleSheet()["Heading1"]
  185. content.append(Paragraph("Driving Tasks", title_style))
  186. title_style = getSampleStyleSheet()["Heading2"]
  187. content.append(Paragraph("Distance Driven over Time", title_style))
  188. tasks_data = report_data["tasks"]
  189. if tasks_data.__len__() == 0:
  190. content.append(Paragraph("No tasks done.", title_style))
  191. else:
  192. task_values = [entry["DistanceAtTime"] for entry in tasks_data]
  193. task_dates = [
  194. # if type(entry["EndDateTime"]) == str parse, else just use
  195. parser.parse(entry["EndDateTime"]).strftime("%m/%d/%Y, %H:%M:%S")
  196. if type(entry["EndDateTime"]) == str
  197. else entry["EndDateTime"].strftime("%m/%d/%Y, %H:%M:%S")
  198. for entry in tasks_data
  199. ]
  200. drawing = Drawing(width=400, height=200)
  201. chart = HorizontalLineChart()
  202. chart.x = 0
  203. chart.y = 50
  204. chart.width = 500
  205. chart.height = 125
  206. chart.data = [task_values]
  207. chart.categoryAxis.labels.boxAnchor = "e"
  208. chart.valueAxis.valueMin = 0
  209. chart.valueAxis.valueMax = max(task_values) + 10
  210. chart.lines[0].strokeColor = colors.blue
  211. chart.lines[0].strokeWidth = 2
  212. chart.categoryAxis.categoryNames = [str(date) for date in task_dates]
  213. chart.categoryAxis.labels.angle = 90
  214. drawing.add(chart)
  215. # Add space between the tables
  216. content.append(drawing)
  217. content.append(Paragraph("<br/><br/>", title_style))
  218. content.append(Paragraph("<br/><br/>", title_style))
  219. content.append(Paragraph("<br/><br/>", title_style))
  220. # Add tasks information
  221. title_style = getSampleStyleSheet()["Heading2"]
  222. content.append(Paragraph("All DriveTasks", title_style))
  223. tasks_header = [
  224. "Task ID",
  225. "Description",
  226. "Start Location",
  227. "End Location",
  228. "Distance Covered",
  229. "Status",
  230. "Start DateTime",
  231. "End DateTime",
  232. ]
  233. tasks_info = []
  234. for task in tasks_data:
  235. if task.get("EndDateTime") is None or task["EndDateTime"] == "":
  236. continue
  237. if task.get("StartDateTime") is None or task["StartDateTime"] == "":
  238. continue
  239. task_info = [
  240. f"{task['Id']}",
  241. task["Description"],
  242. f"{task['StartLocation'][0]}, {task['StartLocation'][1]}",
  243. f"{task['EndLocation'][0]}, {task['EndLocation'][1]}",
  244. "{} km".format(task["DistanceCovered"]),
  245. task["Status"],
  246. task["StartDateTime"].strftime("%m/%d/%Y, %H:%M:%S"),
  247. parser.parse(task["EndDateTime"]).strftime("%m/%d/%Y, %H:%M:%S")
  248. if type(task["EndDateTime"]) == str
  249. else task["EndDateTime"].strftime("%m/%d/%Y, %H:%M:%S"),
  250. ]
  251. tasks_info.append(task_info)
  252. # Define the styles
  253. styles = getSampleStyleSheet()
  254. normal_style = styles["Normal"]
  255. # Convert strings to Paragraphs to allow line breaks
  256. for row in tasks_info:
  257. for i, cell in enumerate(row):
  258. row[i] = Paragraph(cell, normal_style)
  259. tasks_table_style = TableStyle(
  260. [
  261. ("BACKGROUND", (0, 0), (-1, 0), colors.grey),
  262. ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
  263. ("ALIGN", (0, 0), (-1, -1), "CENTER"),
  264. ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
  265. ("BOTTOMPADDING", (0, 0), (-1, 0), 12),
  266. ("BACKGROUND", (0, 1), (-1, -1), colors.beige),
  267. ("GRID", (0, 0), (-1, -1), 1, colors.black),
  268. ]
  269. )
  270. tasks_table = Table([tasks_header] + tasks_info, style=tasks_table_style)
  271. content.append(tasks_table)
  272. content.append(PageBreak())
  273. title_style = getSampleStyleSheet()["Heading1"]
  274. content.append(Paragraph("Fuel Spent Over Time", title_style))
  275. # Add chart of fuel spendings
  276. fuel_spent_data = report_data["FuelSpent"]
  277. if fuel_spent_data.__len__() == 0:
  278. content.append(Paragraph("No fuel spent.", title_style))
  279. else:
  280. fuel_values = [entry["total"] for entry in fuel_spent_data]
  281. fuel_dates = [entry["date"] for entry in fuel_spent_data]
  282. drawing = Drawing(width=400, height=200)
  283. chart = HorizontalLineChart()
  284. chart.x = 0
  285. chart.y = 50
  286. chart.width = 500
  287. chart.height = 125
  288. chart.data = [fuel_values]
  289. chart.categoryAxis.labels.boxAnchor = "e"
  290. chart.valueAxis.valueMin = 0
  291. chart.valueAxis.valueMax = max(fuel_values) + 10
  292. chart.lines[0].strokeColor = colors.blue
  293. chart.lines[0].strokeWidth = 2
  294. chart.categoryAxis.categoryNames = [str(date) for date in fuel_dates]
  295. chart.categoryAxis.labels.angle = 90
  296. drawing.add(chart)
  297. # Add space between the tables
  298. content.append(drawing)
  299. content.append(Paragraph("<br/><br/>", title_style))
  300. fuel_header = ["Date", "Fuel Refilled", "Total Fuel Spent"]
  301. fuel_info = []
  302. for fuel in fuel_spent_data:
  303. fuel_info1 = [
  304. fuel["date"].strftime("%m/%d/%Y, %H:%M:%S"),
  305. "{} L".format(fuel["fuelrefilled"]),
  306. "{} L".format(fuel["total"]),
  307. ]
  308. fuel_info.append(fuel_info1)
  309. # Define the styles
  310. styles = getSampleStyleSheet()
  311. normal_style = styles["Normal"]
  312. # Convert strings to Paragraphs to allow line breaks
  313. for row in fuel_info:
  314. for i, cell in enumerate(row):
  315. row[i] = Paragraph(cell, normal_style)
  316. fuel_table_style = TableStyle(
  317. [
  318. ("BACKGROUND", (0, 0), (-1, 0), colors.grey),
  319. ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
  320. ("ALIGN", (0, 0), (-1, -1), "CENTER"),
  321. ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
  322. ("BOTTOMPADDING", (0, 0), (-1, 0), 12),
  323. ("BACKGROUND", (0, 1), (-1, -1), colors.beige),
  324. ("GRID", (0, 0), (-1, -1), 1, colors.black),
  325. ]
  326. )
  327. fuel_table = Table([fuel_header] + fuel_info, style=fuel_table_style)
  328. content.append(fuel_table)
  329. content.append(PageBreak())
  330. title_style = getSampleStyleSheet()["Heading1"]
  331. content.append(Paragraph("Maintenance Over Time", title_style))
  332. # Add chart of fuel spendings
  333. maintenance_data = report_data["MaintenanceSpent"]
  334. if maintenance_data.__len__() == 0:
  335. content.append(Paragraph("No maintenance done.", title_style))
  336. else:
  337. maintenance_values = [entry["total"] for entry in maintenance_data]
  338. maintenance_dates = [entry["date"] for entry in maintenance_data]
  339. drawing = Drawing(width=400, height=200)
  340. chart = HorizontalLineChart()
  341. chart.x = 0
  342. chart.y = 50
  343. chart.width = 500
  344. chart.height = 125
  345. chart.data = [maintenance_values]
  346. chart.categoryAxis.labels.boxAnchor = "e"
  347. chart.valueAxis.valueMin = 0
  348. chart.valueAxis.valueMax = max(maintenance_values) + 10
  349. chart.lines[0].strokeColor = colors.blue
  350. chart.lines[0].strokeWidth = 2
  351. chart.categoryAxis.categoryNames = [str(date) for date in maintenance_dates]
  352. chart.categoryAxis.labels.angle = 90
  353. drawing.add(chart)
  354. # Add space between the tables
  355. content.append(drawing)
  356. content.append(Paragraph("<br/><br/>", title_style))
  357. maintenance_header = ["Date", "Maintenance Cost", "Total Maintenance Cost"]
  358. maintenance_info = []
  359. for fuel in maintenance_data:
  360. fuel_info1 = [
  361. fuel["date"].strftime("%m/%d/%Y, %H:%M:%S"),
  362. "{} L".format(fuel["cost"]),
  363. "{} L".format(fuel["total"]),
  364. ]
  365. maintenance_info.append(fuel_info1)
  366. # Define the styles
  367. styles = getSampleStyleSheet()
  368. normal_style = styles["Normal"]
  369. # Convert strings to Paragraphs to allow line breaks
  370. for row in maintenance_info:
  371. for i, cell in enumerate(row):
  372. row[i] = Paragraph(cell, normal_style)
  373. maintenance_table_style = TableStyle(
  374. [
  375. ("BACKGROUND", (0, 0), (-1, 0), colors.grey),
  376. ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
  377. ("ALIGN", (0, 0), (-1, -1), "CENTER"),
  378. ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
  379. ("BOTTOMPADDING", (0, 0), (-1, 0), 12),
  380. ("BACKGROUND", (0, 1), (-1, -1), colors.beige),
  381. ("GRID", (0, 0), (-1, -1), 1, colors.black),
  382. ]
  383. )
  384. maintenance_table = Table(
  385. [maintenance_header] + maintenance_info, style=maintenance_table_style
  386. )
  387. content.append(maintenance_table)
  388. document.build(content)
  389. print(f"PDF generated successfully: {pdf_filename}")
  390. return pdf_filename