Browse Source

qfic

main
Madiwka 1 year ago
parent
commit
e78fa8ade4
1 changed files with 164 additions and 155 deletions
  1. +164
    -155
      app/db/repository/report.py

+ 164
- 155
app/db/repository/report.py View File

@@ -187,64 +187,67 @@ def get_pdf(driver_id: int, db: Session):
title_style = getSampleStyleSheet()["Heading2"] title_style = getSampleStyleSheet()["Heading2"]
content.append(Paragraph("Distance Driven over Time", title_style)) content.append(Paragraph("Distance Driven over Time", title_style))
tasks_data = report_data["tasks"] tasks_data = report_data["tasks"]
task_values = [entry["DistanceAtTime"] for entry in tasks_data] if tasks_data.__len__() == 0:
task_dates = [parser.parse(entry['EndDateTime']).strftime("%m/%d/%Y, %H:%M:%S") for entry in tasks_data] content.append(Paragraph("No tasks done.", title_style))

else:
drawing = Drawing(width=400, height=200) task_values = [entry["DistanceAtTime"] for entry in tasks_data]
chart = HorizontalLineChart() task_dates = [parser.parse(entry['EndDateTime']).strftime("%m/%d/%Y, %H:%M:%S") for entry in tasks_data]
chart.x = 0 drawing = Drawing(width=400, height=200)
chart.y = 50 chart = HorizontalLineChart()
chart.width = 500 chart.x = 0
chart.height = 125 chart.y = 50
chart.data = [task_values] chart.width = 500
chart.categoryAxis.labels.boxAnchor = 'e' chart.height = 125
chart.valueAxis.valueMin = 0 chart.data = [task_values]
chart.valueAxis.valueMax = max(task_values) + 10 chart.categoryAxis.labels.boxAnchor = 'e'
chart.lines[0].strokeColor = colors.blue chart.valueAxis.valueMin = 0
chart.lines[0].strokeWidth = 2 chart.valueAxis.valueMax = max(task_values) + 10
chart.categoryAxis.categoryNames = [str(date) for date in task_dates] chart.lines[0].strokeColor = colors.blue
chart.categoryAxis.labels.angle = 90 chart.lines[0].strokeWidth = 2
drawing.add(chart) chart.categoryAxis.categoryNames = [str(date) for date in task_dates]

chart.categoryAxis.labels.angle = 90
# Add space between the tables drawing.add(chart)
content.append(drawing) # Add space between the tables
content.append(Paragraph("<br/><br/>", title_style)) content.append(drawing)
content.append(Paragraph("<br/><br/>", title_style)) content.append(Paragraph("<br/><br/>", title_style))
content.append(Paragraph("<br/><br/>", title_style)) content.append(Paragraph("<br/><br/>", title_style))

content.append(Paragraph("<br/><br/>", title_style))

# Add tasks information
# Add tasks information title_style = getSampleStyleSheet()["Heading2"]
title_style = getSampleStyleSheet()["Heading2"] content.append(Paragraph("All DriveTasks", title_style))
content.append(Paragraph("All DriveTasks", title_style)) tasks_header = ["Task ID", "Description", "Start Location", "End Location", "Distance Covered", "Status", "Start DateTime", "End DateTime"]
tasks_header = ["Task ID", "Description", "Start Location", "End Location", "Distance Covered", "Status", "Start DateTime", "End DateTime"] tasks_info = []
tasks_info = [] for task in tasks_data:

task_info = [f"{task['Id']}", task["Description"], f"{task['StartLocation'][0]}, {task['StartLocation'][1]}", f"{task['EndLocation'][0]}, {task['EndLocation'][1]}",
for task in tasks_data: "{} km".format(task["DistanceCovered"]), task["Status"], task['StartDateTime'].strftime("%m/%d/%Y, %H:%M:%S"), parser.parse(task['EndDateTime']).strftime("%m/%d/%Y, %H:%M:%S")]
task_info = [f"{task['Id']}", task["Description"], f"{task['StartLocation'][0]}, {task['StartLocation'][1]}", f"{task['EndLocation'][0]}, {task['EndLocation'][1]}", tasks_info.append(task_info)
"{} km".format(task["DistanceCovered"]), task["Status"], task['StartDateTime'].strftime("%m/%d/%Y, %H:%M:%S"), parser.parse(task['EndDateTime']).strftime("%m/%d/%Y, %H:%M:%S")]
tasks_info.append(task_info)
# Define the styles
styles = getSampleStyleSheet()
normal_style = styles["Normal"]

# Convert strings to Paragraphs to allow line breaks
for row in tasks_info:
for i, cell in enumerate(row):
row[i] = Paragraph(cell, normal_style)
tasks_table_style = TableStyle([ # Define the styles
('BACKGROUND', (0, 0), (-1, 0), colors.grey), styles = getSampleStyleSheet()
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke), normal_style = styles["Normal"]
('ALIGN', (0, 0), (-1, -1), 'CENTER'), # Convert strings to Paragraphs to allow line breaks
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'), for row in tasks_info:
('BOTTOMPADDING', (0, 0), (-1, 0), 12), for i, cell in enumerate(row):
('BACKGROUND', (0, 1), (-1, -1), colors.beige), row[i] = Paragraph(cell, normal_style)
('GRID', (0, 0), (-1, -1), 1, colors.black), tasks_table_style = TableStyle([
]) ('BACKGROUND', (0, 0), (-1, 0), colors.grey),

('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
tasks_table = Table([tasks_header] + tasks_info, style=tasks_table_style) ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
content.append(tasks_table) ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('GRID', (0, 0), (-1, -1), 1, colors.black),
])

tasks_table = Table([tasks_header] + tasks_info, style=tasks_table_style)
content.append(tasks_table)
@@ -257,57 +260,60 @@ def get_pdf(driver_id: int, db: Session):
content.append(Paragraph("Fuel Spent Over Time", title_style)) content.append(Paragraph("Fuel Spent Over Time", title_style))
# Add chart of fuel spendings # Add chart of fuel spendings
fuel_spent_data = report_data["FuelSpent"] fuel_spent_data = report_data["FuelSpent"]
fuel_values = [entry["total"] for entry in fuel_spent_data] if fuel_spent_data.__len__() == 0:
fuel_dates = [entry["date"] for entry in fuel_spent_data] content.append(Paragraph("No fuel spent.", title_style))

else:
drawing = Drawing(width=400, height=200) fuel_values = [entry["total"] for entry in fuel_spent_data]
chart = HorizontalLineChart() fuel_dates = [entry["date"] for entry in fuel_spent_data]
chart.x = 0 drawing = Drawing(width=400, height=200)
chart.y = 50 chart = HorizontalLineChart()
chart.width = 500 chart.x = 0
chart.height = 125 chart.y = 50
chart.data = [fuel_values] chart.width = 500
chart.categoryAxis.labels.boxAnchor = 'e' chart.height = 125
chart.valueAxis.valueMin = 0 chart.data = [fuel_values]
chart.valueAxis.valueMax = max(fuel_values) + 10 chart.categoryAxis.labels.boxAnchor = 'e'
chart.lines[0].strokeColor = colors.blue chart.valueAxis.valueMin = 0
chart.lines[0].strokeWidth = 2 chart.valueAxis.valueMax = max(fuel_values) + 10
chart.categoryAxis.categoryNames = [str(date) for date in fuel_dates] chart.lines[0].strokeColor = colors.blue
chart.categoryAxis.labels.angle = 90 chart.lines[0].strokeWidth = 2
drawing.add(chart) chart.categoryAxis.categoryNames = [str(date) for date in fuel_dates]

chart.categoryAxis.labels.angle = 90
# Add space between the tables drawing.add(chart)
content.append(drawing) # Add space between the tables
content.append(Paragraph("<br/><br/>", title_style)) content.append(drawing)
content.append(Paragraph("<br/><br/>", title_style))
fuel_header = ["Date", "Fuel Refilled", "Total Fuel Spent"]
fuel_info = []

for fuel in fuel_spent_data:
fuel_info1 = [fuel["date"].strftime("%m/%d/%Y, %H:%M:%S"), "{} L".format(fuel["fuelrefilled"]), "{} L".format(fuel["total"])]
fuel_info.append(fuel_info1)
# Define the styles fuel_header = ["Date", "Fuel Refilled", "Total Fuel Spent"]
styles = getSampleStyleSheet() fuel_info = []
normal_style = styles["Normal"]

# Convert strings to Paragraphs to allow line breaks
for row in fuel_info:
for i, cell in enumerate(row):
row[i] = Paragraph(cell, normal_style)
fuel_table_style = TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('GRID', (0, 0), (-1, -1), 1, colors.black),
])


fuel_table = Table([fuel_header] + fuel_info, style=fuel_table_style) for fuel in fuel_spent_data:
content.append(fuel_table) fuel_info1 = [fuel["date"].strftime("%m/%d/%Y, %H:%M:%S"), "{} L".format(fuel["fuelrefilled"]), "{} L".format(fuel["total"])]
fuel_info.append(fuel_info1)
# Define the styles
styles = getSampleStyleSheet()
normal_style = styles["Normal"]

# Convert strings to Paragraphs to allow line breaks
for row in fuel_info:
for i, cell in enumerate(row):
row[i] = Paragraph(cell, normal_style)
fuel_table_style = TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('GRID', (0, 0), (-1, -1), 1, colors.black),
])

fuel_table = Table([fuel_header] + fuel_info, style=fuel_table_style)
content.append(fuel_table)
content.append(PageBreak()) content.append(PageBreak())


@@ -316,57 +322,60 @@ def get_pdf(driver_id: int, db: Session):
content.append(Paragraph("Maintenance Over Time", title_style)) content.append(Paragraph("Maintenance Over Time", title_style))
# Add chart of fuel spendings # Add chart of fuel spendings
maintenance_data = report_data["MaintenanceSpent"] maintenance_data = report_data["MaintenanceSpent"]
maintenance_values = [entry["total"] for entry in maintenance_data] if maintenance_data.__len__() == 0:
maintenance_dates = [entry["date"] for entry in maintenance_data] content.append(Paragraph("No maintenance done.", title_style))

else:
drawing = Drawing(width=400, height=200) maintenance_values = [entry["total"] for entry in maintenance_data]
chart = HorizontalLineChart() maintenance_dates = [entry["date"] for entry in maintenance_data]
chart.x = 0 drawing = Drawing(width=400, height=200)
chart.y = 50 chart = HorizontalLineChart()
chart.width = 500 chart.x = 0
chart.height = 125 chart.y = 50
chart.data = [maintenance_values] chart.width = 500
chart.categoryAxis.labels.boxAnchor = 'e' chart.height = 125
chart.valueAxis.valueMin = 0 chart.data = [maintenance_values]
chart.valueAxis.valueMax = max(maintenance_values) + 10 chart.categoryAxis.labels.boxAnchor = 'e'
chart.lines[0].strokeColor = colors.blue chart.valueAxis.valueMin = 0
chart.lines[0].strokeWidth = 2 chart.valueAxis.valueMax = max(maintenance_values) + 10
chart.categoryAxis.categoryNames = [str(date) for date in maintenance_dates] chart.lines[0].strokeColor = colors.blue
chart.categoryAxis.labels.angle = 90 chart.lines[0].strokeWidth = 2
drawing.add(chart) chart.categoryAxis.categoryNames = [str(date) for date in maintenance_dates]

chart.categoryAxis.labels.angle = 90
# Add space between the tables drawing.add(chart)
content.append(drawing) # Add space between the tables
content.append(Paragraph("<br/><br/>", title_style)) content.append(drawing)
content.append(Paragraph("<br/><br/>", title_style))
maintenance_header = ["Date", "Maintenance Cost", "Total Maintenance Cost"]
maintenance_info = []

for fuel in maintenance_data:
fuel_info1 = [fuel["date"].strftime("%m/%d/%Y, %H:%M:%S"), "{} L".format(fuel["cost"]), "{} L".format(fuel["total"])]
maintenance_info.append(fuel_info1)
# Define the styles maintenance_header = ["Date", "Maintenance Cost", "Total Maintenance Cost"]
styles = getSampleStyleSheet() maintenance_info = []
normal_style = styles["Normal"]

# Convert strings to Paragraphs to allow line breaks
for row in maintenance_info:
for i, cell in enumerate(row):
row[i] = Paragraph(cell, normal_style)
maintenance_table_style = TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('GRID', (0, 0), (-1, -1), 1, colors.black),
])


maintenance_table = Table([maintenance_header] + maintenance_info, style=maintenance_table_style) for fuel in maintenance_data:
content.append(maintenance_table) fuel_info1 = [fuel["date"].strftime("%m/%d/%Y, %H:%M:%S"), "{} L".format(fuel["cost"]), "{} L".format(fuel["total"])]
maintenance_info.append(fuel_info1)
# Define the styles
styles = getSampleStyleSheet()
normal_style = styles["Normal"]

# Convert strings to Paragraphs to allow line breaks
for row in maintenance_info:
for i, cell in enumerate(row):
row[i] = Paragraph(cell, normal_style)
maintenance_table_style = TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('GRID', (0, 0), (-1, -1), 1, colors.black),
])

maintenance_table = Table([maintenance_header] + maintenance_info, style=maintenance_table_style)
content.append(maintenance_table)


||||||
x
 
000:0
Loading…
Cancel
Save