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"]
content.append(Paragraph("Distance Driven over Time", title_style))
tasks_data = report_data["tasks"]
task_values = [entry["DistanceAtTime"] for entry in tasks_data]
task_dates = [parser.parse(entry['EndDateTime']).strftime("%m/%d/%Y, %H:%M:%S") for entry in tasks_data]

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

# Add space between the tables
content.append(drawing)
content.append(Paragraph("<br/><br/>", title_style))
content.append(Paragraph("<br/><br/>", title_style))
content.append(Paragraph("<br/><br/>", title_style))


# Add tasks information
title_style = getSampleStyleSheet()["Heading2"]
content.append(Paragraph("All DriveTasks", title_style))
tasks_header = ["Task ID", "Description", "Start Location", "End Location", "Distance Covered", "Status", "Start DateTime", "End DateTime"]
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]}",
"{} 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)
if tasks_data.__len__() == 0:
content.append(Paragraph("No tasks done.", title_style))
else:
task_values = [entry["DistanceAtTime"] for entry in tasks_data]
task_dates = [parser.parse(entry['EndDateTime']).strftime("%m/%d/%Y, %H:%M:%S") for entry in tasks_data]

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

# Add space between the tables
content.append(drawing)
content.append(Paragraph("<br/><br/>", title_style))
content.append(Paragraph("<br/><br/>", title_style))
content.append(Paragraph("<br/><br/>", title_style))


# Add tasks information
title_style = getSampleStyleSheet()["Heading2"]
content.append(Paragraph("All DriveTasks", title_style))
tasks_header = ["Task ID", "Description", "Start Location", "End Location", "Distance Covered", "Status", "Start DateTime", "End DateTime"]
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]}",
"{} 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)
tasks_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),
])

tasks_table = Table([tasks_header] + tasks_info, style=tasks_table_style)
content.append(tasks_table)
# 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([
('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),
])

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))
# Add chart of fuel spendings
fuel_spent_data = report_data["FuelSpent"]
fuel_values = [entry["total"] for entry in fuel_spent_data]
fuel_dates = [entry["date"] for entry in fuel_spent_data]

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

# Add space between the tables
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)
if fuel_spent_data.__len__() == 0:
content.append(Paragraph("No fuel spent.", title_style))
else:
fuel_values = [entry["total"] for entry in fuel_spent_data]
fuel_dates = [entry["date"] for entry in fuel_spent_data]

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

# Add space between the tables
content.append(drawing)
content.append(Paragraph("<br/><br/>", title_style))
# 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_header = ["Date", "Fuel Refilled", "Total Fuel Spent"]
fuel_info = []

fuel_table = Table([fuel_header] + fuel_info, style=fuel_table_style)
content.append(fuel_table)
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
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())

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

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

# Add space between the tables
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)
if maintenance_data.__len__() == 0:
content.append(Paragraph("No maintenance done.", title_style))
else:
maintenance_values = [entry["total"] for entry in maintenance_data]
maintenance_dates = [entry["date"] for entry in maintenance_data]

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

# Add space between the tables
content.append(drawing)
content.append(Paragraph("<br/><br/>", title_style))
# 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_header = ["Date", "Maintenance Cost", "Total Maintenance Cost"]
maintenance_info = []

maintenance_table = Table([maintenance_header] + maintenance_info, style=maintenance_table_style)
content.append(maintenance_table)
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
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)


Loading…
Cancel
Save