2025-12-13 18:50:27 +05:30
|
|
|
from flask import Blueprint, render_template, request, send_file, flash
|
2025-12-18 10:41:54 +05:30
|
|
|
import pandas as pd
|
|
|
|
|
import io
|
2025-12-13 18:50:27 +05:30
|
|
|
|
2025-12-14 18:04:03 +05:30
|
|
|
from app.models.subcontractor_model import Subcontractor
|
2025-12-13 18:50:27 +05:30
|
|
|
from app.models.trench_excavation_model import TrenchExcavation
|
2025-12-14 18:04:03 +05:30
|
|
|
from app.models.manhole_excavation_model import ManholeExcavation
|
2025-12-15 00:15:34 +05:30
|
|
|
from app.models.manhole_domestic_chamber_model import ManholeDomesticChamber
|
2026-01-17 17:39:31 +05:30
|
|
|
from app.models.laying_model import Laying
|
|
|
|
|
|
|
|
|
|
from app.models.tr_ex_client_model import TrenchExcavationClient
|
|
|
|
|
from app.models.mh_ex_client_model import ManholeExcavationClient
|
2025-12-15 00:15:34 +05:30
|
|
|
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
|
2026-01-17 17:39:31 +05:30
|
|
|
from app.models.laying_client_model import LayingClient
|
|
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
from app.utils.helpers import login_required
|
2025-12-14 18:04:03 +05:30
|
|
|
|
|
|
|
|
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
|
2025-12-13 18:50:27 +05:30
|
|
|
|
2025-12-14 11:49:45 +05:30
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
# NORMALIZER
|
|
|
|
|
def normalize_key(value):
|
|
|
|
|
if value is None:
|
|
|
|
|
return None
|
|
|
|
|
return str(value).strip().upper()
|
|
|
|
|
|
|
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
# HEADER FORMATTER
|
|
|
|
|
def format_header(header):
|
|
|
|
|
if "-" in header:
|
|
|
|
|
prefix, rest = header.split("-", 1)
|
|
|
|
|
prefix = prefix.title()
|
|
|
|
|
else:
|
|
|
|
|
prefix, rest = None, header
|
2025-12-14 11:49:45 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
parts = rest.split("_")
|
|
|
|
|
result = []
|
|
|
|
|
i = 0
|
2025-12-14 11:49:45 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
while i < len(parts):
|
|
|
|
|
if i + 1 < len(parts) and parts[i].isdigit() and parts[i + 1].isdigit():
|
|
|
|
|
result.append(f"{parts[i]}.{parts[i + 1]}")
|
|
|
|
|
i += 2
|
|
|
|
|
else:
|
|
|
|
|
result.append(parts[i].title())
|
|
|
|
|
i += 1
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
final_text = " ".join(result)
|
|
|
|
|
return f"{prefix}-{final_text}" if prefix else final_text
|
2025-12-14 11:49:45 +05:30
|
|
|
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
# LOOKUP CREATOR
|
2026-01-09 15:22:32 +05:30
|
|
|
def make_lookup(rows, key_field):
|
2026-01-10 01:04:21 +05:30
|
|
|
lookup = {}
|
|
|
|
|
for r in rows:
|
|
|
|
|
location = normalize_key(r.get("Location"))
|
|
|
|
|
key_val = normalize_key(r.get(key_field))
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
if location and key_val:
|
|
|
|
|
lookup[(location, key_val)] = r
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
return lookup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# COMPARISON BUILDER
|
2026-01-09 15:22:32 +05:30
|
|
|
def build_comparison(client_rows, contractor_rows, key_field):
|
|
|
|
|
contractor_lookup = make_lookup(contractor_rows, key_field)
|
|
|
|
|
output = []
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
for c in client_rows:
|
2026-01-10 01:04:21 +05:30
|
|
|
client_location = normalize_key(c.get("Location"))
|
|
|
|
|
client_key = normalize_key(c.get(key_field))
|
|
|
|
|
|
|
|
|
|
if not client_location or not client_key:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
s = contractor_lookup.get((client_location, client_key))
|
2026-01-09 15:22:32 +05:30
|
|
|
if not s:
|
|
|
|
|
continue
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
client_total = sum(
|
|
|
|
|
float(v or 0)
|
|
|
|
|
for k, v in c.items()
|
|
|
|
|
if k.endswith("_total")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sub_total = sum(
|
|
|
|
|
float(v or 0)
|
|
|
|
|
for k, v in s.items()
|
|
|
|
|
if k.endswith("_total")
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
diff = client_total - sub_total
|
2025-12-15 00:15:34 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
row = {
|
2026-01-10 01:04:21 +05:30
|
|
|
"Location": client_location,
|
|
|
|
|
key_field.replace("_", " "): client_key
|
2026-01-09 15:22:32 +05:30
|
|
|
}
|
2025-12-14 11:49:45 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
# CLIENT DATA
|
|
|
|
|
for k, v in c.items():
|
|
|
|
|
if k in ["id", "created_at"]:
|
2025-12-24 16:37:07 +05:30
|
|
|
continue
|
2026-01-09 15:22:32 +05:30
|
|
|
row[f"Client-{k}"] = v
|
2025-12-24 16:37:07 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
row["Client-Total"] = round(client_total, 2)
|
|
|
|
|
row[" "] = ""
|
2025-12-15 00:15:34 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
# SUBCONTRACTOR DATA
|
|
|
|
|
for k, v in s.items():
|
|
|
|
|
if k in ["id", "created_at", "subcontractor_id"]:
|
2025-12-24 16:37:07 +05:30
|
|
|
continue
|
2026-01-09 15:22:32 +05:30
|
|
|
row[f"Subcontractor-{k}"] = v
|
|
|
|
|
|
|
|
|
|
row["Subcontractor-Total"] = round(sub_total, 2)
|
|
|
|
|
row["Diff"] = round(diff, 2)
|
|
|
|
|
|
|
|
|
|
output.append(row)
|
|
|
|
|
|
|
|
|
|
df = pd.DataFrame(output)
|
|
|
|
|
df.columns = [format_header(col) for col in df.columns]
|
|
|
|
|
return df
|
|
|
|
|
|
|
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
# EXCEL SHEET WRITER
|
2026-01-09 15:22:32 +05:30
|
|
|
def write_sheet(writer, df, sheet_name, subcontractor_name):
|
|
|
|
|
workbook = writer.book
|
|
|
|
|
df.to_excel(writer, sheet_name=sheet_name, index=False, startrow=3)
|
|
|
|
|
ws = writer.sheets[sheet_name]
|
|
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
title_fmt = workbook.add_format({"bold": True, "font_size": 14})
|
|
|
|
|
client_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#D9EDF7"})
|
|
|
|
|
sub_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#F7E1D9"})
|
|
|
|
|
total_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#FFF2CC"})
|
|
|
|
|
diff_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#E2EFDA"})
|
|
|
|
|
default_header_fmt = workbook.add_format({"bold": True,"border": 1,"bg_color": "#E7E6E6","align": "center","valign": "vcenter"})
|
|
|
|
|
|
|
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
ws.merge_range(
|
|
|
|
|
0, 0, 0, len(df.columns) - 1,
|
2026-01-10 01:04:21 +05:30
|
|
|
"CLIENT vs SUBCONTRACTOR",
|
|
|
|
|
title_fmt
|
|
|
|
|
)
|
|
|
|
|
ws.merge_range(
|
|
|
|
|
1, 0, 1, len(df.columns) - 1,
|
|
|
|
|
f"Subcontractor Name - {subcontractor_name}",
|
2026-01-09 15:22:32 +05:30
|
|
|
title_fmt
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for col_num, col_name in enumerate(df.columns):
|
|
|
|
|
if col_name.startswith("Client-"):
|
|
|
|
|
ws.write(3, col_num, col_name, client_fmt)
|
|
|
|
|
elif col_name.startswith("Subcontractor-"):
|
|
|
|
|
ws.write(3, col_num, col_name, sub_fmt)
|
|
|
|
|
elif col_name.endswith("Total"):
|
|
|
|
|
ws.write(3, col_num, col_name, total_fmt)
|
|
|
|
|
elif col_name == "Diff":
|
|
|
|
|
ws.write(3, col_num, col_name, diff_fmt)
|
|
|
|
|
else:
|
2026-01-10 01:04:21 +05:30
|
|
|
ws.write(3, col_num, col_name, default_header_fmt)
|
2026-01-09 15:22:32 +05:30
|
|
|
|
|
|
|
|
ws.set_column(col_num, col_num, 20)
|
|
|
|
|
|
|
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
# REPORT ROUTE
|
2026-01-09 15:22:32 +05:30
|
|
|
@generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
|
2026-01-10 01:04:21 +05:30
|
|
|
@login_required
|
2026-01-09 15:22:32 +05:30
|
|
|
def comparison_report():
|
|
|
|
|
subcontractors = Subcontractor.query.all()
|
2025-12-24 16:37:07 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
if request.method == "POST":
|
|
|
|
|
subcontractor_id = request.form.get("subcontractor_id")
|
|
|
|
|
if not subcontractor_id:
|
|
|
|
|
flash("Please select subcontractor", "danger")
|
2026-01-10 01:04:21 +05:30
|
|
|
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
2025-12-15 00:15:34 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
subcontractor = Subcontractor.query.get_or_404(subcontractor_id)
|
2025-12-15 00:15:34 +05:30
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
# -------- DATA --------
|
2026-01-09 15:22:32 +05:30
|
|
|
tr_client = [r.serialize() for r in TrenchExcavationClient.query.all()]
|
|
|
|
|
tr_sub = [r.serialize() for r in TrenchExcavation.query.filter_by(
|
|
|
|
|
subcontractor_id=subcontractor_id
|
|
|
|
|
).all()]
|
|
|
|
|
df_tr = build_comparison(tr_client, tr_sub, "MH_NO")
|
2025-12-15 00:15:34 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
mh_client = [r.serialize() for r in ManholeExcavationClient.query.all()]
|
|
|
|
|
mh_sub = [r.serialize() for r in ManholeExcavation.query.filter_by(
|
|
|
|
|
subcontractor_id=subcontractor_id
|
|
|
|
|
).all()]
|
|
|
|
|
df_mh = build_comparison(mh_client, mh_sub, "MH_NO")
|
2025-12-15 00:15:34 +05:30
|
|
|
|
2026-01-09 15:22:32 +05:30
|
|
|
dc_client = [r.serialize() for r in ManholeDomesticChamberClient.query.all()]
|
|
|
|
|
dc_sub = [r.serialize() for r in ManholeDomesticChamber.query.filter_by(
|
|
|
|
|
subcontractor_id=subcontractor_id
|
|
|
|
|
).all()]
|
|
|
|
|
df_dc = build_comparison(dc_client, dc_sub, "MH_NO")
|
2025-12-15 00:15:34 +05:30
|
|
|
|
2026-01-17 17:39:31 +05:30
|
|
|
lay_client = [r.serialize() for r in LayingClient.query.all()]
|
|
|
|
|
lay_sub = [r.serialize() for r in Laying.query.filter_by(
|
|
|
|
|
subcontractor_id=subcontractor_id
|
|
|
|
|
).all()]
|
|
|
|
|
df_lay = build_comparison(lay_client, lay_sub, "MH_NO")
|
|
|
|
|
|
|
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
# -------- EXCEL --------
|
2025-12-13 18:50:27 +05:30
|
|
|
output = io.BytesIO()
|
2026-01-09 15:22:32 +05:30
|
|
|
filename = f"{subcontractor.subcontractor_name}_Comparison_Report.xlsx"
|
2025-12-13 18:50:27 +05:30
|
|
|
|
|
|
|
|
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
2026-01-09 15:22:32 +05:30
|
|
|
write_sheet(writer, df_tr, "Tr.Ex", subcontractor.subcontractor_name)
|
|
|
|
|
write_sheet(writer, df_mh, "Mh.Ex", subcontractor.subcontractor_name)
|
|
|
|
|
write_sheet(writer, df_dc, "MH & DC", subcontractor.subcontractor_name)
|
2026-01-17 17:39:31 +05:30
|
|
|
write_sheet(writer, df_lay, "Laying", subcontractor.subcontractor_name)
|
2025-12-13 18:50:27 +05:30
|
|
|
|
|
|
|
|
output.seek(0)
|
|
|
|
|
return send_file(
|
|
|
|
|
output,
|
|
|
|
|
as_attachment=True,
|
2026-01-09 15:22:32 +05:30
|
|
|
download_name=filename,
|
2025-12-13 18:50:27 +05:30
|
|
|
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-10 01:04:21 +05:30
|
|
|
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
|
|
|
|
|
|
|
|
|
|