import pandas as pd import io from flask import Blueprint, render_template, request, send_file, flash 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.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.utils.helpers import login_required # --- BLUEPRINT DEFINITION --- file_report_bp = Blueprint("file_report", __name__, url_prefix="/file") # --- DATA WRAPPERS --- class ClientBill: def __init__(self): self.df_tr = pd.DataFrame() self.df_mh = pd.DataFrame() self.df_dc = 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() 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]) if not self.df_dc.empty and "MH_NO" in self.df_dc.columns: self.df_dc.rename(columns={"MH_NO": "Node_No"}, inplace=True) drop_cols = ["id", "created_at", "_sa_instance_state"] for df in [self.df_tr, self.df_mh, self.df_dc]: if not df.empty: df.drop(columns=drop_cols, errors="ignore", inplace=True) 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() def Fetch(self, RA_Bill_No=None, subcontractor_id=None): filters = {} if subcontractor_id: filters["subcontractor_id"] = subcontractor_id if RA_Bill_No: filters["RA_Bill_No"] = RA_Bill_No trench = TrenchExcavation.query.filter_by(**filters).all() mh = ManholeExcavation.query.filter_by(**filters).all() dc = ManholeDomesticChamber.query.filter_by(**filters).all() lay = ManholeDomesticChamber.query.filter_by(**filters).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]) # if not self.df_dc.empty and "MH_NO" in self.df_dc.columns: # self.df_dc.rename(columns={"MH_NO": "Node_No"}, inplace=True) 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) @file_report_bp.route("/report", methods=["GET", "POST"]) @login_required def report_file(): subcontractors = Subcontractor.query.all() tables = None selected_sc_id = None ra_bill_no = None download_all = False if request.method == "POST": subcontractor_id = request.form.get("subcontractor_id") ra_bill_no = request.form.get("ra_bill_no") download_all = request.form.get("download_all") == "true" action = request.form.get("action") if not subcontractor_id: flash("Please select a subcontractor.", "danger") return render_template("report.html", subcontractors=subcontractors) subcontractor = Subcontractor.query.get(subcontractor_id) bill_gen = SubcontractorBill() if download_all: bill_gen.Fetch(subcontractor_id=subcontractor_id) file_name = f"{subcontractor.subcontractor_name}_ALL_BILLS.xlsx" else: if not ra_bill_no: flash("Please enter an RA Bill Number.", "danger") return render_template("report.html", subcontractors=subcontractors) bill_gen.Fetch(RA_Bill_No=ra_bill_no, subcontractor_id=subcontractor_id) file_name = f"{subcontractor.subcontractor_name}_RA_{ra_bill_no}_Report.xlsx" if bill_gen.df_tr.empty and bill_gen.df_mh.empty and bill_gen.df_dc.empty: flash("No data found for this selection.", "warning") return render_template("report.html", subcontractors=subcontractors) # If download is clicked, return file immediately 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="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") output.seek(0) return send_file(output, download_name=file_name, as_attachment=True) # We add bootstrap classes directly to the pandas output table_classes = "table table-hover table-bordered table-striped" tables = { "tr": bill_gen.df_tr.to_html(classes=table_classes, index=False), "mh": bill_gen.df_mh.to_html(classes=table_classes, index=False), "dc": bill_gen.df_dc.to_html(classes=table_classes, index=False), "laying": bill_gen.df_laying.to_html(classes=table_classes, index=False) } selected_sc_id = subcontractor_id return render_template( "report.html", subcontractors=subcontractors, tables=tables, selected_sc_id=selected_sc_id, ra_bill_no=ra_bill_no, download_all=download_all ) @file_report_bp.route("/client_vs_subcont", methods=["GET", "POST"]) @login_required def client_vs_all_subcontractor(): tables = {"tr": None, "mh": None, "dc": None} ra_val = "" if request.method == "POST": 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) 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", "Node_No"], mh_dc_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]: 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", "Node_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("generate_comparison_client_vs_subcont.html", tables=tables, ra_val=ra_val) # ... (Rest of your calculation and download logic) ... # 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) return render_template("generate_comparison_client_vs_subcont.html", tables=tables, ra_val=ra_val)