first qyality report
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
import mariadb
|
||||
from datetime import datetime, timedelta
|
||||
from flask import Blueprint, render_template, redirect, url_for, request, flash, session, current_app, jsonify
|
||||
from .models import User
|
||||
from . import db
|
||||
@@ -242,15 +243,43 @@ def save_external_db():
|
||||
flash('External database settings saved/updated successfully.')
|
||||
return redirect(url_for('main.settings'))
|
||||
|
||||
@bp.route('/get_report_data')
|
||||
@bp.route('/get_report_data', methods=['GET'])
|
||||
def get_report_data():
|
||||
report = request.args.get('report')
|
||||
# Mock data for demonstration
|
||||
data = {
|
||||
"headers": ["Column 1", "Column 2", "Column 3"],
|
||||
"rows": [
|
||||
[f"Row 1, Col 1 (Report {report})", "Row 1, Col 2", "Row 1, Col 3"],
|
||||
[f"Row 2, Col 1 (Report {report})", "Row 2, Col 2", "Row 2, Col 3"],
|
||||
],
|
||||
}
|
||||
data = {"headers": [], "rows": []}
|
||||
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
if report == "1":
|
||||
# Calculate the date 5 days ago
|
||||
five_days_ago = datetime.now() - timedelta(days=5)
|
||||
cursor.execute("""
|
||||
SELECT Id, operator_code, CP_base_code, OC1_code, OC2_code, quality_code, date, time, approved_quantity, rejected_quantity
|
||||
FROM scan1_orders
|
||||
WHERE date >= ?
|
||||
ORDER BY date DESC, time DESC
|
||||
""", (five_days_ago.strftime('%Y-%m-%d'),))
|
||||
rows = cursor.fetchall()
|
||||
|
||||
# Debugging: Print fetched rows
|
||||
print("Fetched rows for report 1:", rows)
|
||||
|
||||
# Define headers based on database column names
|
||||
data["headers"] = ["Id", "Operator Code", "CP Base Code", "OC1 Code", "OC2 Code", "Quality Code", "Date", "Time", "Approved Quantity", "Rejected Quantity"]
|
||||
|
||||
# Convert rows to JSON serializable format
|
||||
data["rows"] = [
|
||||
[str(cell) if isinstance(cell, (datetime, timedelta)) else cell for cell in row]
|
||||
for row in rows
|
||||
]
|
||||
|
||||
conn.close()
|
||||
except mariadb.Error as e:
|
||||
print(f"Error fetching report data: {e}")
|
||||
data["error"] = "Error fetching report data."
|
||||
|
||||
# Debugging: Print the final data being returned
|
||||
print("Data being returned:", data)
|
||||
return jsonify(data)
|
||||
Reference in New Issue
Block a user