AO, cit, itat report download code commit.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
from AppCode.Config import DBConfig
|
||||
import mysql.connector
|
||||
import pandas as pd
|
||||
import io
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -91,3 +94,43 @@ class AOHandler:
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
def ao_report_download(self, selected_year):
|
||||
try:
|
||||
# Call stored proc to fetch year-wise records
|
||||
self.cursor.callproc("GetAOByYear", [selected_year])
|
||||
|
||||
rows = []
|
||||
for result in self.cursor.stored_results():
|
||||
rows = result.fetchall()
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
|
||||
# TRANSPOSE
|
||||
df_transposed = df.transpose()
|
||||
df_transposed.insert(0, 'Field', df_transposed.index)
|
||||
|
||||
# Rename columns: Record 1, 2, 3...
|
||||
record_cols = {
|
||||
i: f"Record {i}"
|
||||
for i in df_transposed.columns if isinstance(i, int)
|
||||
}
|
||||
df_transposed.rename(columns=record_cols, inplace=True)
|
||||
df_transposed.reset_index(drop=True, inplace=True)
|
||||
|
||||
# Excel Output
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
df_transposed.to_excel(writer, index=False, sheet_name="AO_Vertical")
|
||||
worksheet = writer.sheets["AO_Vertical"]
|
||||
worksheet.set_column(0, 0, 30)
|
||||
|
||||
output.seek(0)
|
||||
return output
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print("MySQL Error:", e)
|
||||
return None
|
||||
@@ -1,5 +1,9 @@
|
||||
from AppCode.Config import DBConfig
|
||||
import mysql.connector
|
||||
import pandas as pd
|
||||
import io
|
||||
|
||||
|
||||
|
||||
class CITHandler:
|
||||
|
||||
@@ -75,3 +79,42 @@ class CITHandler:
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
|
||||
def cit_report_download(self, selected_year):
|
||||
try:
|
||||
# Call stored procedure
|
||||
self.cursor.callproc("GetCITByYear", [selected_year])
|
||||
|
||||
rows = []
|
||||
for result in self.cursor.stored_results():
|
||||
rows = result.fetchall()
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
|
||||
# Excel output
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
|
||||
for i, (_, row) in enumerate(df.iterrows(), start=1):
|
||||
# Convert row to vertical format
|
||||
vertical_df = pd.DataFrame(row).reset_index()
|
||||
vertical_df.columns = ['Field', 'Value']
|
||||
|
||||
start_row = (i - 1) * (len(vertical_df) + 3) # gap between blocks
|
||||
vertical_df.to_excel(
|
||||
writer,
|
||||
sheet_name='CIT_Report',
|
||||
index=False,
|
||||
startrow=start_row
|
||||
)
|
||||
|
||||
output.seek(0)
|
||||
return output
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print("MySQL Error:", e)
|
||||
return None
|
||||
@@ -1,6 +1,8 @@
|
||||
# AppCode/ITATHandler.py
|
||||
from AppCode.Config import DBConfig
|
||||
|
||||
from AppCode.Config import DBConfig
|
||||
import mysql.connector
|
||||
import pandas as pd
|
||||
import io
|
||||
|
||||
class ITATHandler:
|
||||
|
||||
@@ -59,7 +61,34 @@ class ITATHandler:
|
||||
self.conn.commit()
|
||||
|
||||
|
||||
# CLOSE CONNECTION
|
||||
|
||||
def itat_report_download(self, selected_year):
|
||||
try:
|
||||
# Call stored procedure
|
||||
self.cursor.callproc("GetITATByYear", [selected_year])
|
||||
|
||||
rows = []
|
||||
for result in self.cursor.stored_results():
|
||||
rows = result.fetchall()
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
|
||||
# Excel output
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
df.T.to_excel(writer, header=False, sheet_name="ITAT_Report")
|
||||
|
||||
output.seek(0)
|
||||
return output
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print("MySQL Error:", e)
|
||||
return None
|
||||
|
||||
# CLOSE CONNECTION
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
@@ -116,7 +116,6 @@ class ITRHandler:
|
||||
|
||||
|
||||
|
||||
|
||||
def itr_report_download(self, selected_year):
|
||||
|
||||
try:
|
||||
@@ -137,6 +136,9 @@ class ITRHandler:
|
||||
df_transposed = df.transpose()
|
||||
df_transposed.insert(0, 'Field', df_transposed.index)
|
||||
|
||||
print("df-->",df_transposed)
|
||||
|
||||
|
||||
record_cols = {
|
||||
i: f"Record {i}"
|
||||
for i in df_transposed.columns if isinstance(i, int)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
279
main.py
279
main.py
@@ -720,7 +720,7 @@ def reports():
|
||||
return render_template("reports.html")
|
||||
|
||||
|
||||
# new new
|
||||
# Itr report download by year
|
||||
@app.route('/itr_report', methods=['GET'])
|
||||
def itr_report():
|
||||
yearGetter = YearGet()
|
||||
@@ -746,6 +746,87 @@ def itr_report():
|
||||
yearGetter.close()
|
||||
return render_template("itr_reports.html", years=years)
|
||||
|
||||
# Ao report download by year
|
||||
@app.route('/ao_report', methods=['GET'])
|
||||
def ao_report():
|
||||
yearGetter = YearGet()
|
||||
selected_year = request.args.get('year')
|
||||
|
||||
if selected_year:
|
||||
ao = AOHandler()
|
||||
output = ao.ao_report_download(selected_year)
|
||||
ao.close()
|
||||
|
||||
if output is None:
|
||||
return "No records found for the selected year."
|
||||
|
||||
return send_file(
|
||||
output,
|
||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
as_attachment=True,
|
||||
download_name=f"AO_Report_{selected_year}.xlsx"
|
||||
)
|
||||
|
||||
else:
|
||||
years = yearGetter.get_year_by_model("GetAOYears")
|
||||
yearGetter.close()
|
||||
|
||||
return render_template("ao_reports.html", years=years)
|
||||
|
||||
# Cit report download by year
|
||||
@app.route('/cit_report', methods=['GET'])
|
||||
def cit_report():
|
||||
selected_year = request.args.get('year')
|
||||
yearGetter = YearGet()
|
||||
|
||||
if selected_year:
|
||||
cit = CITHandler()
|
||||
output = cit.cit_report_download(selected_year)
|
||||
cit.close()
|
||||
|
||||
if output is None:
|
||||
return "No records found for the selected year."
|
||||
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
as_attachment=True,
|
||||
download_name=f"CIT_Report_{selected_year}_Vertical.xlsx"
|
||||
)
|
||||
|
||||
else:
|
||||
years = yearGetter.get_year_by_model("GetCITYears")
|
||||
yearGetter.close()
|
||||
|
||||
return render_template("cit_reports.html", years=years)
|
||||
|
||||
# Itat report download by year
|
||||
@app.route('/itat_report', methods=['GET'])
|
||||
def itat_report():
|
||||
selected_year = request.args.get('year')
|
||||
yearGetter = YearGet()
|
||||
|
||||
if selected_year:
|
||||
itat = ITATHandler()
|
||||
output = itat.itat_report_download(selected_year)
|
||||
itat.close()
|
||||
|
||||
if output is None:
|
||||
return "No records found for the selected year."
|
||||
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
as_attachment=True,
|
||||
download_name=f"ITAT_Report_{selected_year}_Vertical.xlsx"
|
||||
)
|
||||
|
||||
else:
|
||||
# Use stored procedure for years
|
||||
years = yearGetter.get_year_by_model("GetITATYears")
|
||||
yearGetter.close()
|
||||
|
||||
return render_template("itat_reports.html", years=years)
|
||||
|
||||
|
||||
# @app.route('/itr/reports', methods=['GET', 'POST'])
|
||||
@@ -771,124 +852,124 @@ def itr_report():
|
||||
|
||||
|
||||
|
||||
@app.route('/ao_report', methods=['GET'])
|
||||
def ao_report():
|
||||
selected_year = request.args.get('year')
|
||||
connection = pymysql.connect(**db_config)
|
||||
# @app.route('/ao_report', methods=['GET'])
|
||||
# def ao_report():
|
||||
# selected_year = request.args.get('year')
|
||||
# connection = pymysql.connect(**db_config)
|
||||
|
||||
try:
|
||||
if selected_year:
|
||||
query = "SELECT * FROM ao WHERE year = %s"
|
||||
df = pd.read_sql(query, connection, params=[selected_year])
|
||||
# try:
|
||||
# if selected_year:
|
||||
# query = "SELECT * FROM ao WHERE year = %s"
|
||||
# df = pd.read_sql(query, connection, params=[selected_year])
|
||||
|
||||
if df.empty:
|
||||
return "No records found for the selected year."
|
||||
# if df.empty:
|
||||
# return "No records found for the selected year."
|
||||
|
||||
# Transpose the DataFrame: rows → fields, columns → records
|
||||
df_transposed = df.transpose()
|
||||
df_transposed.insert(0, 'Field', df_transposed.index)
|
||||
# # Transpose the DataFrame: rows → fields, columns → records
|
||||
# df_transposed = df.transpose()
|
||||
# df_transposed.insert(0, 'Field', df_transposed.index)
|
||||
|
||||
# Rename columns to "Record 1", "Record 2", ...
|
||||
for i in range(1, df_transposed.shape[1]):
|
||||
df_transposed.rename(columns={df_transposed.columns[i]: f"Record {i}"}, inplace=True)
|
||||
# # Rename columns to "Record 1", "Record 2", ...
|
||||
# for i in range(1, df_transposed.shape[1]):
|
||||
# df_transposed.rename(columns={df_transposed.columns[i]: f"Record {i}"}, inplace=True)
|
||||
|
||||
df_transposed.reset_index(drop=True, inplace=True)
|
||||
# df_transposed.reset_index(drop=True, inplace=True)
|
||||
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||
df_transposed.to_excel(writer, index=False, sheet_name='AO_Vertical')
|
||||
# output = io.BytesIO()
|
||||
# with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||
# df_transposed.to_excel(writer, index=False, sheet_name='AO_Vertical')
|
||||
|
||||
# Optional: Adjust formatting
|
||||
workbook = writer.book
|
||||
worksheet = writer.sheets['AO_Vertical']
|
||||
worksheet.set_column(0, 0, 30) # Widen 'Field' column
|
||||
# # Optional: Adjust formatting
|
||||
# workbook = writer.book
|
||||
# worksheet = writer.sheets['AO_Vertical']
|
||||
# worksheet.set_column(0, 0, 30) # Widen 'Field' column
|
||||
|
||||
output.seek(0)
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
as_attachment=True,
|
||||
download_name=f"AO_Report_{selected_year}.xlsx"
|
||||
)
|
||||
else:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("SELECT DISTINCT year FROM ao ORDER BY year DESC")
|
||||
years = [row[0] for row in cursor.fetchall()]
|
||||
return render_template("ao_reports.html", years=years)
|
||||
finally:
|
||||
connection.close()
|
||||
# output.seek(0)
|
||||
# return send_file(
|
||||
# output,
|
||||
# mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
# as_attachment=True,
|
||||
# download_name=f"AO_Report_{selected_year}.xlsx"
|
||||
# )
|
||||
# else:
|
||||
# with connection.cursor() as cursor:
|
||||
# cursor.execute("SELECT DISTINCT year FROM ao ORDER BY year DESC")
|
||||
# years = [row[0] for row in cursor.fetchall()]
|
||||
# return render_template("ao_reports.html", years=years)
|
||||
# finally:
|
||||
# connection.close()
|
||||
|
||||
|
||||
@app.route('/cit_report', methods=['GET'])
|
||||
def cit_report():
|
||||
selected_year = request.args.get('year')
|
||||
connection = pymysql.connect(**db_config)
|
||||
# @app.route('/cit_report', methods=['GET'])
|
||||
# def cit_report():
|
||||
# selected_year = request.args.get('year')
|
||||
# connection = pymysql.connect(**db_config)
|
||||
|
||||
try:
|
||||
if selected_year:
|
||||
# Fetch data from the `cit` table for the selected year
|
||||
query = "SELECT * FROM cit WHERE year = %s"
|
||||
df = pd.read_sql(query, connection, params=[selected_year])
|
||||
# try:
|
||||
# if selected_year:
|
||||
# # Fetch data from the `cit` table for the selected year
|
||||
# query = "SELECT * FROM cit WHERE year = %s"
|
||||
# df = pd.read_sql(query, connection, params=[selected_year])
|
||||
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||
workbook = writer.book
|
||||
# output = io.BytesIO()
|
||||
# with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||
# workbook = writer.book
|
||||
|
||||
# Write each row vertically on a separate sheet or below one another
|
||||
for i, (_, row) in enumerate(df.iterrows(), start=1):
|
||||
# Convert the row to vertical format
|
||||
vertical_df = pd.DataFrame(row).reset_index()
|
||||
vertical_df.columns = ['Field', 'Value']
|
||||
# # Write each row vertically on a separate sheet or below one another
|
||||
# for i, (_, row) in enumerate(df.iterrows(), start=1):
|
||||
# # Convert the row to vertical format
|
||||
# vertical_df = pd.DataFrame(row).reset_index()
|
||||
# vertical_df.columns = ['Field', 'Value']
|
||||
|
||||
# Write each vertical entry below the previous (e.g., block by block)
|
||||
start_row = (i - 1) * (len(vertical_df) + 3) # 3-row gap between entries
|
||||
vertical_df.to_excel(writer, sheet_name='CIT_Report', index=False, startrow=start_row)
|
||||
# # Write each vertical entry below the previous (e.g., block by block)
|
||||
# start_row = (i - 1) * (len(vertical_df) + 3) # 3-row gap between entries
|
||||
# vertical_df.to_excel(writer, sheet_name='CIT_Report', index=False, startrow=start_row)
|
||||
|
||||
output.seek(0)
|
||||
# output.seek(0)
|
||||
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
as_attachment=True,
|
||||
download_name=f"CIT_Report_{selected_year}_Vertical.xlsx"
|
||||
)
|
||||
else:
|
||||
# Render dropdown for year selection
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("SELECT DISTINCT year FROM cit ORDER BY year DESC")
|
||||
years = [row[0] for row in cursor.fetchall()]
|
||||
return render_template("cit_reports.html", years=years)
|
||||
finally:
|
||||
connection.close()
|
||||
# return send_file(
|
||||
# output,
|
||||
# mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
# as_attachment=True,
|
||||
# download_name=f"CIT_Report_{selected_year}_Vertical.xlsx"
|
||||
# )
|
||||
# else:
|
||||
# # Render dropdown for year selection
|
||||
# with connection.cursor() as cursor:
|
||||
# cursor.execute("SELECT DISTINCT year FROM cit ORDER BY year DESC")
|
||||
# years = [row[0] for row in cursor.fetchall()]
|
||||
# return render_template("cit_reports.html", years=years)
|
||||
# finally:
|
||||
# connection.close()
|
||||
|
||||
@app.route('/itat_report', methods=['GET'])
|
||||
def itat_report():
|
||||
selected_year = request.args.get('year')
|
||||
connection = pymysql.connect(**db_config)
|
||||
# @app.route('/itat_report', methods=['GET'])
|
||||
# def itat_report():
|
||||
# selected_year = request.args.get('year')
|
||||
# connection = pymysql.connect(**db_config)
|
||||
|
||||
try:
|
||||
if selected_year:
|
||||
query = "SELECT * FROM itat WHERE year = %s"
|
||||
df = pd.read_sql(query, connection, params=[selected_year])
|
||||
# try:
|
||||
# if selected_year:
|
||||
# query = "SELECT * FROM itat WHERE year = %s"
|
||||
# df = pd.read_sql(query, connection, params=[selected_year])
|
||||
|
||||
output = io.BytesIO()
|
||||
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||
df.T.to_excel(writer, header=False, sheet_name='ITAT_Report')
|
||||
output.seek(0)
|
||||
# output = io.BytesIO()
|
||||
# with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
||||
# df.T.to_excel(writer, header=False, sheet_name='ITAT_Report')
|
||||
# output.seek(0)
|
||||
|
||||
return send_file(
|
||||
output,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
as_attachment=True,
|
||||
download_name=f"ITAT_Report_{selected_year}_Vertical.xlsx"
|
||||
)
|
||||
else:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("SELECT DISTINCT year FROM itat ORDER BY year DESC")
|
||||
years = [row[0] for row in cursor.fetchall()]
|
||||
return render_template("itat_reports.html", years=years)
|
||||
finally:
|
||||
connection.close()
|
||||
# return send_file(
|
||||
# output,
|
||||
# mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
# as_attachment=True,
|
||||
# download_name=f"ITAT_Report_{selected_year}_Vertical.xlsx"
|
||||
# )
|
||||
# else:
|
||||
# with connection.cursor() as cursor:
|
||||
# cursor.execute("SELECT DISTINCT year FROM itat ORDER BY year DESC")
|
||||
# years = [row[0] for row in cursor.fetchall()]
|
||||
# return render_template("itat_reports.html", years=years)
|
||||
# finally:
|
||||
# connection.close()
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user