upload client side MH and MH DC model commit

This commit is contained in:
2025-12-14 18:04:03 +05:30
parent 1d476c4851
commit 14eac5b958
10 changed files with 266 additions and 117 deletions

View File

@@ -1,15 +1,18 @@
from flask import Blueprint, render_template, request, send_file, flash
from app.models.subcontractor_model import Subcontractor
from app.models.subcontractor_model import Subcontractor
from app.models.trench_excavation_model import TrenchExcavation
from app.models.tr_ex_client_model import TrenchExcavationClient
from app.models.manhole_excavation_model import ManholeExcavation
from app.models.mh_ex_client_model import ManholeExcavationClient
from app import db
import pandas as pd
import io
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
@generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
@@ -21,21 +24,26 @@ def comparison_report():
if not subcontractor_id:
flash("Please select a subcontractor.", "danger")
return render_template("generate_comparison_report.html", subcontractors=subcontractors)
return render_template(
"generate_comparison_report.html",
subcontractors=subcontractors
)
subcontractor = Subcontractor.query.get_or_404(subcontractor_id)
# Fetch data
contractor_rows = TrenchExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
client_rows = TrenchExcavationClient.query.filter_by(subcontractor_id=subcontractor_id).all()
contractor_tr_ex = TrenchExcavation.query.filter_by(
subcontractor_id=subcontractor_id
).all()
client_tr_ex = TrenchExcavationClient.query.filter_by(
subcontractor_id=subcontractor_id
).all()
diff_rows = []
combined_rows_tr_ex = []
for row1, row2 in zip(client_rows, contractor_rows):
total1 = (
for row1, row2 in zip(client_tr_ex, contractor_tr_ex):
# -------- Tr.Ex CLIENT TOTAL ----------
client_total = (
(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) +
@@ -55,7 +63,8 @@ def comparison_report():
(row1.Hard_Rock_6_0_to_7_5_total or 0)
)
total2 = (
# -------- SUBCONTRACTOR TOTAL ----------
contractor_total = (
(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) +
@@ -69,54 +78,132 @@ def comparison_report():
(row2.Hard_Rock_6_0_to_7_5_total or 0)
)
diff = total1 - total2
diff = client_total - contractor_total
# ---- store for excel ----
diff_rows.append({
# -------- COMBINED ROW ----------
row_data = {
"Location": row1.Location,
"Node No": row1.MH_NO,
"Client Sum": round(total1, 2),
"Subcontractor Sum": round(total2, 2),
"Diff": round(diff, 2)
})
"MH No": row1.MH_NO,
}
# optional console print
# Client columns
for col, val in row1.__dict__.items():
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
continue
row_data[f"Client_{col}"] = val
row_data["Client_Total"] = round(client_total, 2)
# Subcontractor columns
for col, val in row2.__dict__.items():
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
continue
row_data[f"Sub_{col}"] = val
row_data["Sub_Total"] = round(contractor_total, 2)
row_data["Diff"] = round(diff, 2)
combined_rows_tr_ex.append(row_data)
# Console log
print(
f"Location : {row1.Location} | "
f"MH_NO : {row1.MH_NO} | "
f"Client : {total1} | "
f"Sub : {total2} | "
f"Diff : {diff}"
f"{row1.Location} | {row1.MH_NO} | "
f"Client={client_total} | Sub={contractor_total} | Diff={diff}"
)
# -------- Tr.Ex model ----------
df_tr_ex = pd.DataFrame(combined_rows_tr_ex)
# manhole ex
# contractor_mh_ex = ManholeExcavation.query.filter_by(
# subcontractor_id=subcontractor_id
# ).all()
# client_mh_ex = ManholeExcavationClient.query.filter_by(
# subcontractor_id=subcontractor_id
# ).all()
# combined_rows_mh_ex = []
# for row1, row2 in zip(client_mh_ex, contractor_mh_ex):
# # -------- Tr.Ex CLIENT TOTAL ----------
# client_total = (
# (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)
# )
# # -------- SUBCONTRACTOR TOTAL ----------
# contractor_total = (
# (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 = client_total - contractor_total
# # -------- COMBINED ROW ----------
# row_data = {
# "Location": row1.Location,
# "MH No": row1.MH_NO,
# }
# # Client columns
# for col, val in row1.__dict__.items():
# if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
# continue
# row_data[f"Client_{col}"] = val
# row_data["Client_Total"] = round(client_total, 2)
# # Subcontractor columns
# for col, val in row2.__dict__.items():
# if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
# continue
# row_data[f"Sub_{col}"] = val
# row_data["Sub_Total"] = round(contractor_total, 2)
# row_data["Diff"] = round(diff, 2)
# combined_rows_tr_ex.append(row_data)
# # Console log
# print(
# f"{row1.Location} | {row1.MH_NO} | "
# f"Client={client_total} | Sub={contractor_total} | 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"]
df_contractor.drop(columns=drop_cols, errors="ignore", inplace=True)
df_client.drop(columns=drop_cols, errors="ignore", inplace=True)
# Convert to numeric
df_contractor = df_contractor.apply(pd.to_numeric, errors="coerce").fillna(0)
df_client = df_client.apply(pd.to_numeric, errors="coerce").fillna(0)
# EXPORT EXCEL
output = io.BytesIO()
file_name = f"{subcontractor.subcontractor_name}_Comparison_Report.xlsx"
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_diff.to_excel(writer, index=False, sheet_name="Diff")
df_tr_ex.to_excel(writer, index=False, sheet_name="Tr.Ex")
output.seek(0)
@@ -127,7 +214,7 @@ def comparison_report():
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
return render_template("generate_comparison_report.html", subcontractors=subcontractors)
return render_template(
"generate_comparison_report.html",
subcontractors=subcontractors
)