import pandas as pd
import io
from flask import Blueprint, render_template, request, send_file, flash, jsonify,redirect, url_for
from app.utils.helpers import login_required
from app.utils.regex_utils import RegularExpression
from app import db
import re
from app.models.subcontractor_model import Subcontractor
from app.models.manhole_excavation_model import ManholeExcavation
from app.models.trench_excavation_model import TrenchExcavation
from app.models.manhole_domestic_chamber_model import ManholeDomesticChamber
from app.models.laying_model import Laying
from app.models.mh_ex_client_model import ManholeExcavationClient
from app.models.tr_ex_client_model import TrenchExcavationClient
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
from app.models.laying_client_model import LayingClient
from app.services.abstract_service import AbstractReportService
# --- BLUEPRINT DEFINITION ---
file_report_bp = Blueprint("file_report", __name__, url_prefix="/file")
# ---------------- ACTION COLUMN ----------------
def add_action_columns(df, model_key):
if df.empty:
return df
df.insert(0, "Select", df["Id"].apply(
lambda x: f''
))
df["Update"] = df["Id"].apply(
lambda x: f' Edit'
)
df["Delete"] = df["Id"].apply(
lambda x: f''
)
return df
# ---------------- FETCH ----------------
class SubcontractorBill:
def __init__(self):
self.df_tr = pd.DataFrame()
self.df_mh = pd.DataFrame()
self.df_dc = pd.DataFrame()
self.df_laying = pd.DataFrame()
# self.df_abstract = pd.DataFrame() # NEW
def Fetch(self, RA_Bill_No=None, subcontractor_id=None, location=None):
filters = {}
if subcontractor_id:
filters["subcontractor_id"] = subcontractor_id
if RA_Bill_No:
filters["RA_Bill_No"] = RA_Bill_No
# Fetch data in database
trench = TrenchExcavation.query.filter_by(**filters).all()
mh = ManholeExcavation.query.filter_by(**filters).all()
dc = ManholeDomesticChamber.query.filter_by(**filters).all()
lay = Laying.query.filter_by(**filters).all()
# LOCATION FILTER
if location:
search = location.strip().lower()
print("location::",search)
trench = [
t for t in trench
if search in (t.Location or "").strip().lower()
]
mh = [
t for t in mh
if search in (t.Location or "").strip().lower()
]
dc = [
t for t in dc
if search in (t.Location or "").strip().lower()
]
lay = [
t for t in lay
if search in (t.Location or "").strip().lower()
]
# Set dataframe
self.df_tr = pd.DataFrame([c.serialize() for c in trench])
self.df_mh = pd.DataFrame([c.serialize() for c in mh])
self.df_dc = pd.DataFrame([c.serialize() for c in dc])
self.df_laying = pd.DataFrame([c.serialize() for c in lay])
drop_cols = ["11", "_sa_instance_state", "subcontractor_id" , "created_at"]
for df in [self.df_tr, self.df_mh, self.df_dc, self.df_laying]:
if not df.empty:
df.drop(columns=drop_cols, errors="ignore", inplace=True)
format_column_names(df)
name = ""
if subcontractor_id:
sc = Subcontractor.query.get(subcontractor_id)
if sc:
name = sc.subcontractor_name
# ---------------- DELETE ----------------
@file_report_bp.route("/delete_records", methods=["POST"])
@login_required
def delete_records():
data = request.json or {}
model = data.get("model")
ids = data.get("ids", [])
model_map = {
"tr": TrenchExcavation,
"mh": ManholeExcavation,
"dc": ManholeDomesticChamber,
"laying": Laying
}
ModelClass = model_map.get(model)
# validate model BEFORE using it
if not ModelClass:
return jsonify({"status": "error", "message": f"Invalid model '{model}'"}), 400
if not ids:
return jsonify({"status": "error", "message": "No IDs provided"}), 400
try:
for record_id in ids:
obj = ModelClass.query.get(record_id)
if obj:
db.session.delete(obj)
db.session.commit()
return jsonify({"status": "success"})
except Exception as e:
db.session.rollback()
return jsonify({"status": "error", "message": str(e)}), 500
@file_report_bp.route("/edit//", methods=["GET", "POST"])
@login_required
def edit_record(model, record_id):
model_map = {
"tr": TrenchExcavation,
"mh": ManholeExcavation,
"dc": ManholeDomesticChamber,
"laying": Laying
}
ModelClass = model_map.get(model)
if not ModelClass:
flash("Invalid Model.", "danger")
return redirect(url_for("file_report.report_file"))
record = ModelClass.query.get_or_404(record_id)
if request.method == "POST":
# Update all fields except id
for column in record.__table__.columns:
if column.name == "id":
continue
if column.name in request.form:
setattr(record, column.name, request.form.get(column.name))
try:
db.session.commit()
flash("Record updated successfully.", "success")
# ✅ fixed: correct blueprint name
return redirect(url_for("file_report.report_file"))
except Exception as e:
db.session.rollback()
flash(str(e), "danger")
return render_template(
"edit_record.html",
record=record,
model=model
)
@file_report_bp.route("/Subcontractor_report", methods=["GET", "POST"])
@login_required
def report_file():
# get all subcontractor data
subcontractors = Subcontractor.query.all()
tables = None
abstract_html = ""
selected_sc_id = None
ra_bill_no = ""
location = ""
category = ""
# Search or load data
if request.method == "POST":
# get from data
subcontractor_id = request.form.get("subcontractor_id")
ra_bill_no = request.form.get("ra_bill_no", "").strip()
location = request.form.get("location", "").strip()
category = request.form.get("category", "")
action = request.form.get("action", "preview")
if not subcontractor_id:
flash("Select Subcontractor", "danger")
return render_template(
"subcontractor_report.html",
subcontractors=subcontractors
)
selected_sc_id = subcontractor_id
bill = SubcontractorBill()
if action == "excel_all":
bill.Fetch(subcontractor_id=subcontractor_id)
else:
bill.Fetch(ra_bill_no,subcontractor_id,location)
# -----------------------------------------
# Generate Abstract Report for Web
# -----------------------------------------
abstract_service = AbstractReportService(
subcontractor_id=subcontractor_id,
ra_bill_no=ra_bill_no
)
abstract_html = abstract_service.generate_html()
# ---------------- CATEGORY FILTER ----------------
if category == "tr":
bill.df_mh = bill.df_dc = bill.df_laying = pd.DataFrame()
elif category == "mh":
bill.df_tr = bill.df_dc = bill.df_laying = pd.DataFrame()
elif category == "dc":
bill.df_tr = bill.df_mh = bill.df_laying = pd.DataFrame()
elif category == "laying":
bill.df_tr = bill.df_mh = bill.df_dc = pd.DataFrame()
# ===================================================
# DOWNLOAD EXCEL
# ===================================================
if action in ["excel", "excel_all"]:
output = io.BytesIO()
with pd.ExcelWriter(output,engine="xlsxwriter") as writer:
workbook = writer.book
abstract = AbstractReportService(subcontractor_id=subcontractor_id,ra_bill_no=ra_bill_no)
abstract.generate(workbook)
bill.df_tr.to_excel(writer,sheet_name="Tr.Ex",index=False)
bill.df_mh.to_excel(writer,sheet_name="Mh.Ex",index=False)
bill.df_dc.to_excel(writer,sheet_name="MH & DC",index=False)
bill.df_laying.to_excel(writer,sheet_name="Pipe Laying",index=False)
writer.close()
output.seek(0)
return send_file(
output,
download_name= "subcontractor_Report.xlsx",
as_attachment=True
)
# ===================================================
# PDF
# ===================================================
if action == "pdf":
flash(
"PDF Export Coming Soon.",
"info"
)
# ===================================================
# ADD ACTIONS
# ===================================================
bill.df_tr = add_action_columns(bill.df_tr, "tr")
bill.df_mh = add_action_columns(bill.df_mh, "mh")
bill.df_dc = add_action_columns(bill.df_dc, "dc")
bill.df_laying = add_action_columns(bill.df_laying, "laying")
# this are html classes
# table_class = ( "table " "table-bordered" "table-hover " "table-striped " "table-sm " "align-middle " "datatable " "mb-0")
table_class = (
"table "
"table-bordered "
"table-hover "
"table-striped "
"table-sm "
"align-middle "
"datatable "
"text-nowrap "
"mb-0"
)
# This are showing on web tables
tables = {
"tr": bill.df_tr.to_html(classes=table_class, index=False, escape=False),
"mh": bill.df_mh.to_html(classes=table_class, index=False, escape=False),
"dc": bill.df_dc.to_html(classes=table_class, index=False, escape=False ),
"laying": bill.df_laying.to_html(classes=table_class, index=False, escape=False)
}
return render_template(
"subcontractor_report.html",
subcontractors=subcontractors,
selected_sc_id=selected_sc_id,
selected_ra_bill=ra_bill_no,
selected_location=location,
selected_category=category,
tables=tables,
abstract_html=abstract_html
)
# --- Client class ---
class ClientBill:
def __init__(self):
self.df_tr = pd.DataFrame()
self.df_mh = pd.DataFrame()
self.df_dc = pd.DataFrame()
self.df_laying = pd.DataFrame()
def Fetch(self, RA_Bill_No):
trench = TrenchExcavationClient.query.filter_by(RA_Bill_No=RA_Bill_No).all()
mh = ManholeExcavationClient.query.filter_by(RA_Bill_No=RA_Bill_No).all()
dc = ManholeDomesticChamberClient.query.filter_by(RA_Bill_No=RA_Bill_No).all()
lay = LayingClient.query.filter_by(RA_Bill_No=RA_Bill_No).all()
self.df_tr = pd.DataFrame([c.serialize() for c in trench])
self.df_mh = pd.DataFrame([c.serialize() for c in mh])
self.df_dc = pd.DataFrame([c.serialize() for c in dc])
self.df_laying = pd.DataFrame([c.serialize() for c in lay])
drop_cols = ["id", "created_at", "_sa_instance_state"]
for df in [self.df_tr, self.df_mh, self.df_dc, self.df_laying]:
if not df.empty:
df.drop(columns=drop_cols, errors="ignore", inplace=True)
# --- CLIENT REPORT (PREVIEW + DOWNLOAD) ---
@file_report_bp.route("/client_report", methods=["GET", "POST"])
@login_required
def client_vs_all_subcontractor():
tables = {"tr": None, "mh": None, "dc": None}
ra_val = ""
if request.method == "POST":
# ⚠ MUST match HTML name
RA_Bill_No = request.form.get("RA_Bill_No")
ra_val = RA_Bill_No
if not RA_Bill_No:
flash("Please enter RA Bill No.", "danger")
return render_template("generate_comparison_client_vs_subcont.html", tables=tables, ra_val=ra_val)
clientBill = ClientBill()
clientBill.Fetch(RA_Bill_No=RA_Bill_No)
contractorBill = SubcontractorBill()
contractorBill.Fetch(RA_Bill_No=RA_Bill_No)
# --- SAFETY CHECK: Verify data exists before merging ---
if clientBill.df_tr.empty and clientBill.df_mh.empty:
flash(f"No Client records found for RA Bill {RA_Bill_No}", "warning")
return render_template("generate_comparison_client_vs_subcont.html", tables=tables, ra_val=ra_val)
qty_cols = [...] # (Keep your existing list)
mh_dc_qty_cols = [...] # (Keep your existing list)
mh_lay_qty_cols =[...]
def aggregate_df(df, group_cols, sum_cols):
if df.empty:
# Create an empty DF with the correct columns to avoid Merge/Key Errors
return pd.DataFrame(columns=group_cols + sum_cols)
existing_cols = [c for c in sum_cols if c in df.columns]
# Ensure group_cols exist in the DF
for col in group_cols:
if col not in df.columns:
df[col] = "N/A" # Fill missing join keys
return df.groupby(group_cols, as_index=False)[existing_cols].sum()
# Aggregate data
df_sub_tr_grp = aggregate_df(contractorBill.df_tr, ["Location", "MH_NO"], qty_cols)
df_sub_mh_grp = aggregate_df(contractorBill.df_mh, ["Location", "MH_NO"], qty_cols)
df_sub_dc_grp = aggregate_df(contractorBill.df_dc, ["Location", "MH_NO"], mh_dc_qty_cols)
df_sub_lay_grp = aggregate_df(contractorBill.df_dc, ["Location", "MH_NO"], mh_lay_qty_cols)
# --- FINAL MERGE LOGIC ---
# We check if "Location" exists in the client data. If not, we add it to prevent the KeyError.
for df_client in [clientBill.df_tr, clientBill.df_mh, clientBill.df_dc, clientBill.df_laying ]:
if not df_client.empty and "Location" not in df_client.columns:
df_client["Location"] = "Unknown"
try:
df_tr_cmp = clientBill.df_tr.merge(df_sub_tr_grp, on=["Location", "MH_NO"], how="left", suffixes=("_Client", "_Sub"))
df_mh_cmp = clientBill.df_mh.merge(df_sub_mh_grp, on=["Location", "MH_NO"], how="left", suffixes=("_Client", "_Sub"))
df_dc_cmp = clientBill.df_dc.merge(df_sub_dc_grp, on=["Location", "MH_NO"], how="left", suffixes=("_Client", "_Sub"))
df_lay_cmp = clientBill.df_laying.merge(df_sub_lay_grp, on=["Location", "MH_NO"], how="left", suffixes=("_Client", "_Sub"))
except KeyError as e:
flash(f"Merge Error: Missing column {str(e)}. Check if 'Location' is defined in your database models.", "danger")
return render_template("client_report.html", tables=tables, ra_val=ra_val)
<<<<<<< HEAD
# Convert to HTML for preview
tables["tr"] = df_tr_cmp.to_html(classes='table table-striped table-hover table-sm', index=False)
tables["mh"] = df_mh_cmp.to_html(classes='table table-striped table-hover table-sm', index=False)
tables["dc"] = df_dc_cmp.to_html(classes='table table-striped table-hover table-sm', index=False)
tables["laying"] = df_lay_cmp.to_html(classes='table table-striped table-hover table-sm', index=False)
return render_template("client_report.html", tables=tables, ra_val=ra_val)
=======
# -------- DOWNLOAD --------
if action == "download":
output = io.BytesIO()
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
bill_gen.df_tr.to_excel(writer, index=False, sheet_name="Trench")
bill_gen.df_mh.to_excel(writer, index=False, sheet_name="MH")
bill_gen.df_dc.to_excel(writer, index=False, sheet_name="MH & DC")
bill_gen.df_laying.to_excel(writer, index=False, sheet_name="Laying")
output.seek(0)
return send_file(
output,
download_name=f"Client_RA_{RA_Bill_No}_Report.xlsx",
as_attachment=True
)
# -------- PREVIEW --------
table_class = "table table-bordered table-striped table-hover table-sm"
tables["tr"] = bill_gen.df_tr.to_html(classes=table_class, index=False)
tables["mh"] = bill_gen.df_mh.to_html(classes=table_class, index=False)
tables["dc"] = bill_gen.df_dc.to_html(classes=table_class, index=False)
tables["laying"] = bill_gen.df_laying.to_html(classes=table_class, index=False)
return render_template("client_report.html", tables=tables, ra_val=ra_val)
def format_column_names(df):
if df.empty:
return df
new_columns = []
for col in df.columns:
# ----------------------------------------
# Pipe columns
# pipe_150_mm -> Pipe 150 MM
# ----------------------------------------
if RegularExpression.PIPE_MM_PATTERN.match(col):
m = re.match(r"pipe_(\d+)_mm", col)
new_columns.append(f"Pipe {m.group(1)} MM")
continue
# ----------------------------------------
# Domestic Chamber
# d_0_to_0_75 -> 0.00 To 0.75
# d_1_5_to_3_0 -> 1.50 To 3.00
# ----------------------------------------
if RegularExpression.D_RANGE_PATTERN.match(col):
value = col[2:] # remove d_
value = re.sub(
r'(\d+)_(\d+)',
lambda m: f"{m.group(1)}.{m.group(2)}",
value
)
value = value.replace("_to_", " To ")
new_columns.append(value)
continue
# ----------------------------------------
# Total columns
# Soft_Murum_0_to_1_5_total
# ->
# Soft Murum 0 To 1.5 Total
# ----------------------------------------
if RegularExpression.STR_TOTAL_PATTERN.match(col):
value = col[:-6] # remove _total
value = re.sub(
r'(\d+)_(\d+)',
lambda m: f"{m.group(1)}.{m.group(2)}",
value
)
value = value.replace("_to_", " To ")
value = value.replace("_", " ")
new_columns.append(value.title() + " Total")
continue
# ----------------------------------------
# General columns
# ----------------------------------------
value = col.replace("_", " ").title()
replacements = {
"Mh No": "MH No",
"Ra Bill No": "RA Bill No",
"Cc Length": "CC Length",
"Id Of Mh M": "ID of MH (m)",
"Pipe Dia Mm": "Pipe Dia (MM)",
"Mh Top Level": "MH Top Level",
"Upto Il Depth": "Upto IL Depth",
"Actual Trench Length": "Actual Trench Length",
"Ground Level": "Ground Level",
"Invert Level": "Invert Level",
"Ex Dia Of Manhole": "External Dia of Manhole",
"Area Of Manhole": "Area of Manhole",
"Depth Of Mh": "Depth of MH",
}
value = replacements.get(value, value)
new_columns.append(value)
df.columns = new_columns
return df
>>>>>>> pankaj-dev