Download client vs sub cont report with show diff. commit

This commit is contained in:
2025-12-14 11:49:45 +05:30
parent 89328c9066
commit 1d476c4851
2 changed files with 67 additions and 152 deletions

View File

@@ -11,55 +11,7 @@ import io
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
COMPARISON_COLUMNS = [
("Soft Murum 01.5",
"Soft_Murum_0_to_1_5_total",
"Soft_Murum_0_to_1_5_total"),
("Soft Murum 1.53.0",
"Soft_Murum_1_5_to_3_0_total",
"Soft_Murum_1_5_to_3_0_total"),
("Soft Murum 3.04.5",
"Soft_Murum_3_0_to_4_5_total",
"Soft_Murum_3_0_to_4_5_total"),
("Hard Murum 01.5",
"Hard_Murum_0_to_1_5_total",
"Hard_Murum_0_to_1_5_total"),
("Hard Murum Above 1.5",
"Hard_Murum_1_5_and_above_total",
"Hard_Murum_1_5_to_3_0_total"),
("Soft Rock 01.5",
"Soft_Rock_0_to_1_5_total",
"Soft_Rock_0_to_1_5_total"),
("Soft Rock Above 1.5",
"Soft_Rock_1_5_and_above_total",
"Soft_Rock_1_5_to_3_0_total"),
("Hard Rock 01.5",
"Hard_Rock_0_to_1_5_total",
"Hard_Rock_0_to_1_5_total"),
("Hard Rock Above 1.5",
"Hard_Rock_1_5_and_above_total",
"Hard_Rock_1_5_to_3_0_total"),
("Hard Rock 3.04.5",
"Hard_Rock_3_0_to_4_5_total",
"Hard_Rock_3_0_to_4_5_total"),
("Hard Rock 4.56.0",
"Hard_Rock_4_5_to_6_0_total",
"Hard_Rock_4_5_to_6_0_total"),
("Hard Rock 6.07.5",
"Hard_Rock_6_0_to_7_5_total",
"Hard_Rock_6_0_to_7_5_total"),
]
@generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
def comparison_report():
subcontractors = Subcontractor.query.all()
@@ -77,9 +29,73 @@ def comparison_report():
contractor_rows = TrenchExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
client_rows = TrenchExcavationClient.query.filter_by(subcontractor_id=subcontractor_id).all()
diff_rows = []
for row1, row2 in zip(client_rows, contractor_rows):
total1 = (
(row1.Marshi_Muddy_Slushy_0_to_1_5_total or 0) +
(row1.Marshi_Muddy_Slushy_1_5_to_3_0_total or 0) +
(row1.Marshi_Muddy_Slushy_3_0_to_4_5_total or 0) +
(row1.Soft_Murum_0_to_1_5_total or 0) +
(row1.Soft_Murum_1_5_to_3_0_total or 0) +
(row1.Soft_Murum_3_0_to_4_5_total or 0) +
(row1.Hard_Murum_0_to_1_5_total or 0) +
(row1.Hard_Murum_1_5_to_3_0_total or 0) +
(row1.Hard_Murum_3_0_to_4_5_total or 0) +
(row1.Soft_Rock_0_to_1_5_total or 0) +
(row1.Soft_Rock_1_5_to_3_0_total or 0) +
(row1.Soft_Rock_3_0_to_4_5_total or 0) +
(row1.Hard_Rock_0_to_1_5_total or 0) +
(row1.Hard_Rock_1_5_to_3_0_total or 0) +
(row1.Hard_Rock_3_0_to_4_5_total or 0) +
(row1.Hard_Rock_4_5_to_6_0_total or 0) +
(row1.Hard_Rock_6_0_to_7_5_total or 0)
)
total2 = (
(row2.Soft_Murum_0_to_1_5_total or 0) +
(row2.Soft_Murum_1_5_to_3_0_total or 0) +
(row2.Soft_Murum_3_0_to_4_5_total or 0) +
(row2.Hard_Murum_0_to_1_5_total or 0) +
(row2.Hard_Murum_1_5_and_above_total or 0) +
(row2.Soft_Rock_0_to_1_5_total or 0) +
(row2.Soft_Rock_1_5_and_above_total or 0) +
(row2.Hard_Rock_0_to_1_5_total or 0) +
(row2.Hard_Rock_1_5_and_above_total or 0) +
(row2.Hard_Rock_4_5_to_6_0_total or 0) +
(row2.Hard_Rock_6_0_to_7_5_total or 0)
)
diff = total1 - total2
# ---- store for excel ----
diff_rows.append({
"Location": row1.Location,
"Node No": row1.MH_NO,
"Client Sum": round(total1, 2),
"Subcontractor Sum": round(total2, 2),
"Diff": round(diff, 2)
})
# optional console print
print(
f"Location : {row1.Location} | "
f"MH_NO : {row1.MH_NO} | "
f"Client : {total1} | "
f"Sub : {total2} | "
f"Diff : {diff}"
)
# Convert to DataFrame
df_contractor = pd.DataFrame([r.__dict__ for r in contractor_rows])
df_client = pd.DataFrame([r.__dict__ for r in client_rows])
df_diff = pd.DataFrame(diff_rows, columns=["Location", "Node No", "Client Sum", "Subcontractor Sum", "Diff"])
# Drop unwanted columns
drop_cols = ["id", "subcontractor_id", "created_at", "_sa_instance_state", "Remarks"]
@@ -90,24 +106,6 @@ def comparison_report():
df_contractor = df_contractor.apply(pd.to_numeric, errors="coerce").fillna(0)
df_client = df_client.apply(pd.to_numeric, errors="coerce").fillna(0)
# CREATE SUMMARY
summary = []
for label, cont_col, client_col in COMPARISON_COLUMNS:
cont_sum = round(df_contractor.get(cont_col, pd.Series()).sum(), 2)
client_sum = round(df_client.get(client_col, pd.Series()).sum(), 2)
diff = round(client_sum - cont_sum, 2)
summary.append({
"Excavation Type": label,
"Subcontractor Total": cont_sum,
"Client Total": client_sum,
"Difference (Client - Subcontractor)": diff
})
df_summary = pd.DataFrame(summary)
# EXPORT EXCEL
output = io.BytesIO()
@@ -116,7 +114,9 @@ def comparison_report():
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
df_contractor.to_excel(writer, index=False, sheet_name="Contractor_Data")
df_client.to_excel(writer, index=False, sheet_name="Client_Data")
df_summary.to_excel(writer, index=False, sheet_name="Comparison_Summary")
df_diff.to_excel(writer, index=False, sheet_name="Diff")
output.seek(0)
@@ -131,88 +131,3 @@ def comparison_report():
# @generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
# def report_subcont_client():
# subcontractors = Subcontractor.query.all()
# if request.method == "POST":
# subcontractor_id = request.form.get("subcontractor_id")
# if not subcontractor_id:
# flash("Please select a subcontractor.", "danger")
# return render_template("report.html", subcontractors=subcontractors)
# # Fetch subcontractor for file name
# subcontractor = Subcontractor.query.get(subcontractor_id)
# trench_excavation1 = TrenchExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
# trench_excavation2 = TrenchExcavationClient.query.filter_by(subcontractor_id=subcontractor_id).all()
# # Convert to DataFrame
# df_tr_ex1 = pd.DataFrame([m.__dict__ for m in trench_excavation1])
# df_tr_ex2 = pd.DataFrame([t.__dict__ for t in trench_excavation2])
# # Drop unnecessary SQLAlchemy fields
# drop_cols = ["id", "subcontractor_id", "created_at", "_sa_instance_state","Remarks"]
# df_tr_ex1.drop(columns=drop_cols, errors="ignore", inplace=True)
# df_tr_ex2.drop(columns=drop_cols, errors="ignore", inplace=True)
# trench_columns = [
# "Location", "MH_NO", "CC_length", "Invert_Level", "MH_Top_Level",
# "Ground_Level", "ID_of_MH_m", "Actual_Trench_Length", "Pipe_Dia_mm",
# "Width_0_to_2_5", "Width_2_5_to_3_0", "Width_3_0_to_4_5", "Width_4_5_to_6_0",
# "Upto_IL_Depth", "Cutting_Depth", "Avg_Depth",
# "Soft_Murum_0_to_1_5", "Soft_Murum_1_5_to_3_0", "Soft_Murum_3_0_to_4_5",
# "Hard_Murum_0_to_1_5", "Hard_Murum_1_5_to_3_0",
# "Soft_Rock_0_to_1_5", "Soft_Rock_1_5_to_3_0",
# "Hard_Rock_0_to_1_5", "Hard_Rock_1_5_to_3_0",
# "Hard_Rock_3_0_to_4_5", "Hard_Rock_4_5_to_6_0", "Hard_Rock_6_0_to_7_5",
# "Soft_Murum_0_to_1_5_total", "Soft_Murum_1_5_to_3_0_total",
# "Soft_Murum_3_0_to_4_5_total",
# "Hard_Murum_0_to_1_5_total", "Hard_Murum_1_5_and_above_total",
# "Soft_Rock_0_to_1_5_total", "Soft_Rock_1_5_and_above_total",
# "Hard_Rock_0_to_1_5_total", "Hard_Rock_1_5_and_above_total",
# "Hard_Rock_3_0_to_4_5_total", "Hard_Rock_4_5_to_6_0_total",
# "Hard_Rock_6_0_to_7_5_total",
# "Remarks", "Total"
# ]
# # Reorder columns serial wise
# df_tr_ex1 = df_tr_ex1.reindex(columns=trench_columns, fill_value="")
# df_tr_ex2 = df_tr_ex2.reindex(columns=trench_columns, fill_value="")
# # WRITE EXCEL FILE
# output = io.BytesIO()
# file_name = f"{subcontractor.subcontractor_name}_Report.xlsx"
# with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
# df_tr_ex1.to_excel(writer, index=False, sheet_name="Tr.Ex-contractor.")
# df_tr_ex2.to_excel(writer, index=False, sheet_name="Tr.Ex-client.")
# output.seek(0)
# return send_file(
# output,
# download_name=file_name,
# as_attachment=True,
# mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
# )
# return render_template("generate_comparison_report.html", subcontractors=subcontractors)