This commit is contained in:
2026-08-08 13:57:04 +05:30
12 changed files with 1289 additions and 143 deletions

View File

@@ -19,7 +19,7 @@ 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
from app.services.abstract_service import AbstractReportService, ClientAbstractReportService
# --- BLUEPRINT DEFINITION ---
@@ -115,9 +115,6 @@ def add_action_columns(df, model_key):
# ---------------- SELECT-ALL HEADER ----------------
def add_select_all_header(table_html, model_key):
"""Swap pandas' plain 'Select' column header for a select-all checkbox
scoped to this table (via data-model), so checking it only toggles
rows in this table - not the other 3 category tables on the page."""
return table_html.replace(
"<th>Select</th>",
f'<th><input type="checkbox" class="select-all-checkbox" '
@@ -136,7 +133,10 @@ def add_data_field_attrs(table_html, raw_fields):
Uses match spans (not string search-and-replace) to rebuild each row,
because a naive .replace() on duplicate cell text (e.g. two cells
that both just say "0.00") would edit the wrong cell."""
that both just say "0.00") would edit the wrong cell.
NOTE: only used for Subcontractor tables (which have Bulk Edit).
Client tables do not use this."""
total_cols = 1 + len(raw_fields) + 2 # Select + data columns + Update + Delete
def process_row(m):
@@ -174,15 +174,21 @@ def render_table_or_empty(df, model_key, table_class, raw_fields=None):
JS registered after it - including the select-all checkbox handler
for the OTHER tables on the page. Returning a plain message instead
of an empty <table class="datatable"> avoids ever handing DataTables
something it can't initialize."""
something it can't initialize.
raw_fields is only needed for Subcontractor tables (Bulk Edit);
leave it as None for Client tables and this just skips the
data-field tagging step."""
if df.empty:
return '<div class="alert alert-info mb-0">No records found.</div>'
html = df.to_html(classes=table_class, index=False, escape=False)
html = add_select_all_header(html, model_key)
html = add_data_field_attrs(html, raw_fields or [])
if raw_fields:
html = add_data_field_attrs(html, raw_fields)
return html
<<<<<<< HEAD
@@ -295,6 +301,8 @@ def format_detail_sheet(workbook, worksheet, df, sheet_name, contractor_name="",
worksheet.set_row(header_row, 30)
=======
>>>>>>> 80d4e82f9b14a0454b3beb45992dab986ab8853f
# ---------------- FETCH ----------------
class SubcontractorBill:
def __init__(self):
@@ -412,7 +420,11 @@ def delete_records():
"tr": TrenchExcavation,
"mh": ManholeExcavation,
"dc": ManholeDomesticChamber,
"laying": Laying
"laying": Laying,
"tr_client": TrenchExcavationClient,
"mh_client": ManholeExcavationClient,
"dc_client": ManholeDomesticChamberClient,
"laying_client": LayingClient
}
ModelClass = model_map.get(model)
@@ -440,7 +452,8 @@ def delete_records():
@file_report_bp.route("/bulk_update", methods=["POST"])
@login_required
def bulk_update():
"""Bulk-edit save endpoint. Expects JSON shaped like:
"""Bulk-edit save endpoint. Subcontractor tables only - Client
report does not have Bulk Edit. Expects JSON shaped like:
{ "tr": { "5": {"MH_NO": "12A", "Location": "Pune"}, ... }, "mh": {...}, ... }
Field names are validated against each model's real table columns
@@ -499,7 +512,11 @@ def edit_record(model, record_id):
"tr": TrenchExcavation,
"mh": ManholeExcavation,
"dc": ManholeDomesticChamber,
"laying": Laying
"laying": Laying,
"tr_client": TrenchExcavationClient,
"mh_client": ManholeExcavationClient,
"dc_client": ManholeDomesticChamberClient,
"laying_client": LayingClient
}
ModelClass = model_map.get(model)
@@ -524,7 +541,10 @@ def edit_record(model, record_id):
try:
db.session.commit()
flash("Record updated successfully.", "success")
# ✅ fixed: correct blueprint name
if model.endswith("_client"):
return redirect(url_for("file_report.client_report"))
return redirect(url_for("file_report.report_file"))
except Exception as e:
@@ -754,7 +774,6 @@ def report_file():
}
# 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 "
@@ -797,23 +816,66 @@ class ClientBill:
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()
def Fetch(self, RA_Bill_No=None, location=None, mh_no=None):
filters = {}
if RA_Bill_No:
filters["RA_Bill_No"] = RA_Bill_No
trench = TrenchExcavationClient.query.filter_by(**filters).all()
mh = ManholeExcavationClient.query.filter_by(**filters).all()
dc = ManholeDomesticChamberClient.query.filter_by(**filters).all()
lay = LayingClient.query.filter_by(**filters).all()
# LOCATION FILTER
if location:
search = location.strip().lower()
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()
]
# MH NO FILTER
if mh_no:
search_mh = mh_no.strip().lower()
trench = [
t for t in trench
if search_mh in (t.MH_NO or "").strip().lower()
]
mh = [
t for t in mh
if search_mh in (t.MH_NO or "").strip().lower()
]
dc = [
t for t in dc
if search_mh in (t.MH_NO or "").strip().lower()
]
lay = [
t for t in lay
if search_mh in (t.MH_NO or "").strip().lower()
]
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"]
drop_cols = ["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)
format_column_names(df)
# --- CLIENT REPORT (PREVIEW + DOWNLOAD) ---
@@ -821,23 +883,41 @@ class ClientBill:
@login_required
def client_report():
tables = {"tr": None, "mh": None, "dc": None, "laying": None}
tables = None
ra_val = ""
location_val = ""
mh_no_val = ""
category_val = ""
abstract_html = ""
has_data = {"tr": False, "mh": False, "dc": False, "laying": False}
if request.method == "POST":
# ⚠ MUST match HTML name
RA_Bill_No = request.form.get("RA_Bill_No")
action = request.form.get("action")
RA_Bill_No = request.form.get("RA_Bill_No", "").strip()
location = request.form.get("location", "").strip()
mh_no = request.form.get("mh_no", "").strip()
category = request.form.get("category", "")
action = request.form.get("action", "preview")
ra_val = RA_Bill_No
location_val = location
mh_no_val = mh_no
category_val = category
if not RA_Bill_No:
flash("Please enter RA Bill No.", "danger")
return render_template("client_report.html", tables=tables, ra_val=ra_val)
return render_template(
"client_report.html",
tables=tables, ra_val=ra_val,
location_val=location_val, mh_no_val=mh_no_val,
category_val=category_val,
abstract_html=abstract_html,
has_data=has_data
)
# -------- FETCH CLIENT DATA --------
bill_gen = ClientBill()
bill_gen.Fetch(RA_Bill_No)
bill_gen.Fetch(RA_Bill_No, location, mh_no)
# If no data
if (
@@ -847,7 +927,27 @@ def client_report():
bill_gen.df_laying.empty
):
flash(f"No Client records found for RA Bill {RA_Bill_No}", "warning")
return render_template("client_report.html", tables=tables, ra_val=ra_val)
return render_template(
"client_report.html",
tables=tables, ra_val=ra_val,
location_val=location_val, mh_no_val=mh_no_val,
category_val=category_val,
abstract_html=abstract_html,
has_data=has_data
)
abstract_service = ClientAbstractReportService(ra_bill_no=RA_Bill_No)
abstract_html = abstract_service.generate_html()
# ---------------- CATEGORY FILTER ----------------
if category == "tr":
bill_gen.df_mh = bill_gen.df_dc = bill_gen.df_laying = pd.DataFrame()
elif category == "mh":
bill_gen.df_tr = bill_gen.df_dc = bill_gen.df_laying = pd.DataFrame()
elif category == "dc":
bill_gen.df_tr = bill_gen.df_mh = bill_gen.df_laying = pd.DataFrame()
elif category == "laying":
bill_gen.df_tr = bill_gen.df_mh = bill_gen.df_dc = pd.DataFrame()
# -------- DOWNLOAD --------
if action == "download":
@@ -855,10 +955,13 @@ def client_report():
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")
workbook = writer.book
abstract_service.generate(workbook)
bill_gen.df_tr.to_excel(writer, index=False, sheet_name="Tr.Ex")
bill_gen.df_mh.to_excel(writer, index=False, sheet_name="Mh.Ex")
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")
bill_gen.df_laying.to_excel(writer, index=False, sheet_name="Pipe Laying")
output.seek(0)
@@ -868,15 +971,47 @@ def client_report():
as_attachment=True
)
# -------- PREVIEW --------
table_class = "table table-bordered table-striped table-hover table-sm"
# ===================================================
# ADD ACTIONS (same helpers as Subcontractor report)
# ===================================================
bill_gen.df_tr = add_action_columns(bill_gen.df_tr, "tr_client")
bill_gen.df_mh = add_action_columns(bill_gen.df_mh, "mh_client")
bill_gen.df_dc = add_action_columns(bill_gen.df_dc, "dc_client")
bill_gen.df_laying = add_action_columns(bill_gen.df_laying, "laying_client")
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)
has_data = {
"tr": not bill_gen.df_tr.empty,
"mh": not bill_gen.df_mh.empty,
"dc": not bill_gen.df_dc.empty,
"laying": not bill_gen.df_laying.empty,
}
return render_template("client_report.html", tables=tables, ra_val=ra_val)
table_class = (
"table "
"table-bordered "
"table-hover "
"table-striped "
"table-sm "
"align-middle "
"datatable "
"text-nowrap "
"mb-0"
)
tables = {
"tr": render_table_or_empty(bill_gen.df_tr, "tr_client", table_class),
"mh": render_table_or_empty(bill_gen.df_mh, "mh_client", table_class),
"dc": render_table_or_empty(bill_gen.df_dc, "dc_client", table_class),
"laying": render_table_or_empty(bill_gen.df_laying, "laying_client", table_class)
}
return render_template(
"client_report.html",
tables=tables, ra_val=ra_val,
location_val=location_val, mh_no_val=mh_no_val,
category_val=category_val,
abstract_html=abstract_html,
has_data=has_data
)
def format_column_names(df):