From 18874cc84e4bb836401f9af2236666396cca5ecf Mon Sep 17 00:00:00 2001 From: pjpatil12 Date: Fri, 7 Aug 2026 10:29:15 +0530 Subject: [PATCH 1/4] changes of rate model of sub-cont and there functions --- app/routes/engineering_master_routes.py | 93 ++++- app/services/subcontractor_rate_service.py | 109 +++-- app/templates/base.html | 28 +- app/templates/engineering/add_rate.html | 236 ----------- app/templates/engineering/client_rate.html | 20 + .../engineering/contractor_rate.html | 384 ++++++++++++++++++ app/templates/engineering/list.html | 0 7 files changed, 563 insertions(+), 307 deletions(-) delete mode 100644 app/templates/engineering/add_rate.html create mode 100644 app/templates/engineering/client_rate.html create mode 100644 app/templates/engineering/contractor_rate.html delete mode 100644 app/templates/engineering/list.html diff --git a/app/routes/engineering_master_routes.py b/app/routes/engineering_master_routes.py index 71cb99f..4a2fea5 100644 --- a/app/routes/engineering_master_routes.py +++ b/app/routes/engineering_master_routes.py @@ -4,7 +4,7 @@ from flask import ( request, redirect, url_for, - flash + flash, jsonify ) from app.models.subcontractor_model import Subcontractor @@ -26,26 +26,8 @@ def engineering_master(): "engineering/index.html", title="Engineering Masters" ) - - -@engi_bp.route("/subcontractor-rate") -def add_subcontractor_rates(): - subcontractors = Subcontractor.query.filter_by(status="Active").all() - - if request.method == "POST": - try: - SubcontractorRateService.save_rate(request.form) - flash(SuccessMessage.SAVE, "success") - return redirect(url_for("engineering.add_subcontractor_rates")) - except Exception as e: - flash(ErrorMessage.INTERNAL_SERVER_ERROR, "danger") - - return render_template( - "engineering/add_rate.html", - title="Subcontractor Rate Master", - subcontractors=subcontractors - ) - + +# Client rate model @engi_bp.route("/client-rate") def client_rates(): @@ -54,3 +36,72 @@ def client_rates(): title="Client Rate Master" ) +@engi_bp.route("/subcontractor-rate", methods=["GET", "POST"]) +def add_subcontractor_rates(): + + subcontractors = Subcontractor.query.filter_by(status="Active").all() + + if request.method == "POST": + result = SubcontractorRateService.save_or_update(request.form) + if result["success"]: + flash(result["message"], "success") + return redirect(url_for("engineering.add_subcontractor_rates")) + else: + flash(result["message"], "danger") + + rates = SubcontractorRateService.get_all_rates() + + return render_template( + "engineering/contractor_rate.html", + title="Subcontractor Rate Master", + subcontractors=subcontractors, + rates=rates + ) + +@engi_bp.route("/subcontractor-rate/edit/", methods=["GET", "POST"]) +def edit_rate(rate_id): + + subcontractors = Subcontractor.query.filter_by(status="Active").all() + + if request.method == "POST": + + result = SubcontractorRateService.save_or_update(request.form) + + if result["success"]: + flash(result["message"], "success") + return redirect(url_for("engineering.add_subcontractor_rates")) + + flash(result["message"], "danger") + + rate = SubcontractorRateService.get_rate(rate_id) + + rates = SubcontractorRateService.get_all_rates() + + return render_template( + "engineering/contractor_rate.html", + subcontractors=subcontractors, + rate=rate, + rates=rates + ) + +@engi_bp.route("/subcontractor-rate/delete/") +def delete_rate(rate_id): + SubcontractorRateService.delete_rate(rate_id) + flash("Rate deleted successfully.", "success") + return redirect(url_for("engineering.add_subcontractor_rates")) + + +@engi_bp.route("/check-rate") +def check_rate(): + + exists = SubcontractorRateService.check_duplicate( + subcontractor_id=request.args.get("subcontractor_id"), + category=request.args.get("category"), + item_name=request.args.get("item_name"), + rate_id=request.args.get("rate_id") + ) + + return jsonify({ + "exists": exists + }) + diff --git a/app/services/subcontractor_rate_service.py b/app/services/subcontractor_rate_service.py index 951c378..a21107c 100644 --- a/app/services/subcontractor_rate_service.py +++ b/app/services/subcontractor_rate_service.py @@ -1,28 +1,66 @@ from app.services.db_service import db from app.models.subcontractor_rate_model import SubcontractorRate +from sqlalchemy import func class SubcontractorRateService: @staticmethod - def save_rate(form): + def save_or_update(form): - rate = SubcontractorRate( - subcontractor_id=form.get("subcontractor_id"), - category=form.get("category"), - item_code=form.get("item_code"), - item_name=form.get("item_name"), - unit=form.get("unit"), - rate=form.get("rate"), - effective_from=form.get("effective_from"), - effective_to=form.get("effective_to") or None, - status=form.get("status") + rate_id = form.get("id") + + subcontractor_id = form.get("subcontractor_id") + category = form.get("category") + item_name = form.get("item_name").strip() + + # ----------------------------- + # Duplicate Validation + # ----------------------------- + duplicate = ( + SubcontractorRate.query + .filter( + SubcontractorRate.subcontractor_id == subcontractor_id, + SubcontractorRate.category == category, + func.lower(SubcontractorRate.item_name) == item_name.lower() + ) + .first() ) - db.session.add(rate) + # Ignore current record while editing + if duplicate and (not rate_id or duplicate.id != int(rate_id)): + return { + "success": False, + "message": "Category and Rate already exists for this Subcontractor." + } + + # ----------------------------- + # Insert / Update + # ----------------------------- + if rate_id: + rate = SubcontractorRate.query.get_or_404(rate_id) + else: + rate = SubcontractorRate() + + rate.subcontractor_id = subcontractor_id + rate.category = category + rate.item_code = form.get("item_code") + rate.item_name = item_name + rate.unit = form.get("unit") + rate.rate = form.get("rate") + rate.effective_from = form.get("effective_from") + rate.effective_to = form.get("effective_to") or None + rate.status = form.get("status") + + if not rate_id: + db.session.add(rate) + db.session.commit() - return rate - + + return { + "success": True, + "message": "Saved Successfully." + } @staticmethod def get_all_rates(): @@ -32,34 +70,33 @@ class SubcontractorRateService: .order_by(SubcontractorRate.created_at.desc()) .all() ) + + @staticmethod def get_rate(rate_id): return SubcontractorRate.query.get_or_404(rate_id) - @staticmethod - def update_rate(rate_id, form): - - rate = SubcontractorRate.query.get_or_404(rate_id) - - rate.subcontractor_id = form.get("subcontractor_id") - rate.category = form.get("category") - rate.item_code = form.get("item_code") - rate.item_name = form.get("item_name") - rate.unit = form.get("unit") - rate.rate = form.get("rate") - rate.effective_from = form.get("effective_from") - rate.effective_to = form.get("effective_to") or None - rate.status = form.get("status") - - db.session.commit() - return rate - - @staticmethod def delete_rate(rate_id): - rate = SubcontractorRate.query.get_or_404(rate_id) - db.session.delete(rate) + db.session.commit() + - db.session.commit() \ No newline at end of file + @staticmethod + def check_duplicate(subcontractor_id, category, item_name, rate_id=None): + + query = SubcontractorRate.query.filter( + SubcontractorRate.subcontractor_id == subcontractor_id, + SubcontractorRate.category == category, + func.lower(SubcontractorRate.item_name) == item_name.strip().lower() + ) + + if rate_id: + query = query.filter(SubcontractorRate.id != int(rate_id)) + + return query.first() is not None + + + + \ No newline at end of file diff --git a/app/templates/base.html b/app/templates/base.html index cca715e..05c9340 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -128,6 +128,13 @@ + + + - - - - + @@ -185,10 +184,10 @@ - +
  • - - Dashboard + + Masters
  • @@ -199,12 +198,13 @@ - +
  • diff --git a/app/templates/engineering/add_rate.html b/app/templates/engineering/add_rate.html deleted file mode 100644 index 2b8f2a7..0000000 --- a/app/templates/engineering/add_rate.html +++ /dev/null @@ -1,236 +0,0 @@ -{% extends "base.html" %} -{% block content %} -
    - -
    - -
    -

    - - Subcontractor Rate Master -

    -
    - -
    - -
    - -
    - - -
    - - - -
    - - -
    - - - - - -
    - - -
    - - - - - -
    - -
    - -
    - - -
    - - - - - -
    - - -
    - - - - - -
    - - -
    - - - - - -
    - - -
    - - - - - -
    - -
    - -
    - - -
    - - - - - -
    - - -
    - - - - - -
    - -
    - -
    - -
    - - - - - -
    - -
    - -
    - -
    - -
    - - -{% endblock %} \ No newline at end of file diff --git a/app/templates/engineering/client_rate.html b/app/templates/engineering/client_rate.html new file mode 100644 index 0000000..a41a2b1 --- /dev/null +++ b/app/templates/engineering/client_rate.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% block content %} + +
    + +
    + +
    + +

    + + Client Rate Master +

    + +
    + +
    + + +{% endblock %} \ No newline at end of file diff --git a/app/templates/engineering/contractor_rate.html b/app/templates/engineering/contractor_rate.html new file mode 100644 index 0000000..5f892a9 --- /dev/null +++ b/app/templates/engineering/contractor_rate.html @@ -0,0 +1,384 @@ +{% extends "base.html" %} +{% block content %} + +
    + +
    + +
    + +

    + + Subcontractor Rate Master +

    + +
    + +
    + +
    + + + + +
    + + +
    + + +
    + + +
    + + +
    + + +
    + + + +
    +
    + +
    + + +
    + + +
    + + +
    + + + + +
    + + + +
    + + +
    + + +
    + + +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    + +
    + +
    + + Reset + + + +
    +
    +
    +
    + + +
    +
    +
    + Rate List +
    +
    + +
    + + + + + + + + + + + + + + + + + {% for row in rates %} + + + + + + + + + + + + + {% endfor %} + + +
    #SubcontractorCategoryItem CodeItem NameUnitRateStatusAction
    {{ loop.index }}{{ row.subcontractor.subcontractor_name }}{{ row.category }}{{ row.item_code }}{{ row.item_name }}{{ row.unit }}{{ row.rate }} + {% if row.status=="Active" %} + + Active + + {% else %} + + Inactive + + {% endif %} + + + + + + + + +
    +
    +
    +
    + + + +{% endblock %} \ No newline at end of file diff --git a/app/templates/engineering/list.html b/app/templates/engineering/list.html deleted file mode 100644 index e69de29..0000000 From c09cb8ea3a5703aef2e30b6becf10421fe11e686 Mon Sep 17 00:00:00 2001 From: anishd100 Date: Fri, 7 Aug 2026 11:51:51 +0530 Subject: [PATCH 2/4] suctr location filter, MH NO. search, subctr manadatory field removed, added select all for delete and also all edit at once feature added --- app/routes/file_report.py | 367 +++++++++++++++++----- app/templates/client_report.html | 18 -- app/templates/subcontractor_report.html | 387 ++++++++++++++++++++---- 3 files changed, 616 insertions(+), 156 deletions(-) diff --git a/app/routes/file_report.py b/app/routes/file_report.py index c5bd57c..c3aebd1 100644 --- a/app/routes/file_report.py +++ b/app/routes/file_report.py @@ -4,6 +4,7 @@ from flask import Blueprint, render_template, request, send_file, flash, jsonify from app.utils.helpers import login_required from app.utils.regex_utils import RegularExpression from app import db +from sqlalchemy import func import re from app.models.subcontractor_model import Subcontractor @@ -22,7 +23,74 @@ from app.services.abstract_service import AbstractReportService # --- BLUEPRINT DEFINITION --- file_report_bp = Blueprint("file_report", __name__, url_prefix="/file") - + + +# ---------------- LOCATION HELPERS ---------------- +WORK_MODELS = [TrenchExcavation, ManholeExcavation, ManholeDomesticChamber, Laying] + + +def get_distinct_locations(): + """Union of distinct, non-empty Location values across all 4 work tables.""" + locations = set() + for Model in WORK_MODELS: + rows = db.session.query(Model.Location).distinct().all() + for (loc,) in rows: + if loc and loc.strip(): + locations.add(loc.strip()) + return sorted(locations) + + +def get_subcontractors_for_location(location): + """Return Subcontractor objects that have at least one work record + (in any of the 4 category tables) at the given location. If no + location is given, returns every subcontractor. Shared by the AJAX + endpoint below and by the server-rendered dropdown so the list is + correct even before/without JS running (e.g. on page reload or a + validation-error re-render). + + Matching is case- and whitespace-insensitive, since Location is a + free-text field and stored values can drift ("Pune" vs "PUNE " etc.) + even though the dropdown options themselves come from a distinct + query and look identical.""" + location = (location or "").strip() + + if not location: + return Subcontractor.query.order_by(Subcontractor.subcontractor_name).all() + + target = location.upper() + sc_ids = set() + for Model in WORK_MODELS: + rows = ( + db.session.query(Model.subcontractor_id) + .filter(func.upper(func.trim(Model.Location)) == target) + .distinct() + .all() + ) + for (sid,) in rows: + if sid: + sc_ids.add(sid) + + if not sc_ids: + return [] + + return ( + Subcontractor.query.filter(Subcontractor.id.in_(sc_ids)) + .order_by(Subcontractor.subcontractor_name) + .all() + ) + + +@file_report_bp.route("/get_subcontractors_by_location") +@login_required +def get_subcontractors_by_location(): + """AJAX endpoint: return subcontractors that have at least one work + record (in any of the 4 category tables) at the given location.""" + location = request.args.get("location", "").strip() + subs = get_subcontractors_for_location(location) + + return jsonify([{"id": s.id, "name": s.subcontractor_name} for s in subs]) + + # ---------------- ACTION COLUMN ---------------- def add_action_columns(df, model_key): @@ -30,7 +98,7 @@ def add_action_columns(df, model_key): return df df.insert(0, "Select", df["Id"].apply( - lambda x: f'' + lambda x: f'' )) df["Update"] = df["Id"].apply( @@ -44,6 +112,76 @@ def add_action_columns(df, model_key): return df +# ---------------- 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( + "Select", + f'', + 1 + ) + + +# ---------------- TABLE OR EMPTY-STATE ---------------- +def add_data_field_attrs(table_html, raw_fields): + """Tag each editable data with a data-field attribute naming its + underlying database column, so bulk-edit mode on the frontend knows + exactly which field to submit for each input it creates. The 'id' + column is deliberately skipped - it's the primary key and must never + be editable. + + 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.""" + total_cols = 1 + len(raw_fields) + 2 # Select + data columns + Update + Delete + + def process_row(m): + row_html = m.group(0) + matches = list(re.finditer(r".*?", row_html, flags=re.S)) + if len(matches) != total_cols: + return row_html # shape mismatch - leave untouched rather than guess + + pieces = [] + last_end = 0 + for i, mt in enumerate(matches): + pieces.append(row_html[last_end:mt.start()]) + cell = mt.group(0) + if 1 <= i <= len(raw_fields): + field = raw_fields[i - 1] + if field.lower() != "id": + cell = f'' + cell[len(""):] + pieces.append(cell) + last_end = mt.end() + pieces.append(row_html[last_end:]) + return "".join(pieces) + + return re.sub(r".*?", process_row, table_html, flags=re.S) + + +def render_table_or_empty(df, model_key, table_class, raw_fields=None): + """Render a table, or a friendly placeholder if there's no data. + + IMPORTANT: pandas' to_html() on a fully empty DataFrame (0 rows AND + 0 columns, which is what we get when a category has no matching + records) still emits a - just with a + header row that has zero
    cells. jQuery DataTables then tries to + initialize on a table with no columns and throws, which (since the + init code runs as one synchronous block) silently kills every bit of + 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 avoids ever handing DataTables + something it can't initialize.""" + if df.empty: + return '
    No records found.
    ' + 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 []) + return html + + @@ -56,7 +194,7 @@ class SubcontractorBill: self.df_laying = pd.DataFrame() # self.df_abstract = pd.DataFrame() # NEW - def Fetch(self, RA_Bill_No=None, subcontractor_id=None, location=None): + def Fetch(self, RA_Bill_No=None, subcontractor_id=None, location=None, mh_no=None): filters = {} if subcontractor_id: @@ -73,7 +211,6 @@ class SubcontractorBill: # 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() @@ -93,7 +230,30 @@ class SubcontractorBill: t for t in lay if search in (t.Location or "").strip().lower() ] - + + # MH NO FILTER + if mh_no: + mh_search = mh_no.strip().lower() + trench = [ + t for t in trench + if mh_search in (t.MH_NO or "").strip().lower() + ] + + mh = [ + t for t in mh + if mh_search in (t.MH_NO or "").strip().lower() + ] + + dc = [ + t for t in dc + if mh_search in (t.MH_NO or "").strip().lower() + ] + + lay = [ + t for t in lay + if mh_search in (t.MH_NO 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]) @@ -102,9 +262,24 @@ class SubcontractorBill: 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]: + # Raw (pre-format_column_names) column names for each table, e.g. + # "MH_NO" rather than the display label "MH No". Bulk-edit mode + # needs these to know which real database column each input maps + # to, since the table only shows the prettified header text. + self.tr_fields = [] + self.mh_fields = [] + self.dc_fields = [] + self.laying_fields = [] + + for df, attr in [ + (self.df_tr, "tr_fields"), + (self.df_mh, "mh_fields"), + (self.df_dc, "dc_fields"), + (self.df_laying, "laying_fields"), + ]: if not df.empty: df.drop(columns=drop_cols, errors="ignore", inplace=True) + setattr(self, attr, list(df.columns)) format_column_names(df) name = "" @@ -152,6 +327,60 @@ def delete_records(): return jsonify({"status": "error", "message": str(e)}), 500 +@file_report_bp.route("/bulk_update", methods=["POST"]) +@login_required +def bulk_update(): + """Bulk-edit save endpoint. Expects JSON shaped like: + { "tr": { "5": {"MH_NO": "12A", "Location": "Pune"}, ... }, "mh": {...}, ... } + + Field names are validated against each model's real table columns + server-side - the frontend sending a field name is not enough on its + own to permit writing it; id/subcontractor_id/created_at are always + refused regardless of what's submitted. + """ + data = request.json or {} + + model_map = { + "tr": TrenchExcavation, + "mh": ManholeExcavation, + "dc": ManholeDomesticChamber, + "laying": Laying + } + PROTECTED_FIELDS = {"id", "subcontractor_id", "created_at"} + + updated = 0 + errors = [] + + try: + for model_key, records in data.items(): + ModelClass = model_map.get(model_key) + if not ModelClass: + errors.append(f"Unknown table '{model_key}'") + continue + + valid_columns = {c.name for c in ModelClass.__table__.columns} - PROTECTED_FIELDS + + for record_id, fields in (records or {}).items(): + obj = ModelClass.query.get(record_id) + if not obj: + errors.append(f"{model_key} #{record_id}: record not found") + continue + + for field, value in (fields or {}).items(): + if field not in valid_columns: + errors.append(f"{model_key} #{record_id}: '{field}' is not editable") + continue + setattr(obj, field, value) + updated += 1 + + db.session.commit() + except Exception as e: + db.session.rollback() + return jsonify({"status": "error", "message": str(e)}), 500 + + return jsonify({"status": "success", "updated": updated, "errors": errors}) + + @file_report_bp.route("/edit//", methods=["GET", "POST"]) @login_required def edit_record(model, record_id): @@ -204,28 +433,40 @@ def edit_record(model, record_id): def report_file(): # get all subcontractor data subcontractors = Subcontractor.query.all() + locations = get_distinct_locations() tables = None abstract_html = "" selected_sc_id = None + has_data = {"tr": False, "mh": False, "dc": False, "laying": False} ra_bill_no = "" location = "" + mh_no = "" category = "" # Search or load data if request.method == "POST": # get from data - subcontractor_id = request.form.get("subcontractor_id") + subcontractor_id = request.form.get("subcontractor_id") or None 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") - if not subcontractor_id: - flash("Select Subcontractor", "danger") + # Keep the subcontractor dropdown scoped to the chosen location + # even on a plain (non-JS) page render. + subcontractors = get_subcontractors_for_location(location) + + # Subcontractor is now optional - at least one other filter must + # be given so the search isn't a "return everything" query. + if not subcontractor_id and not location and not ra_bill_no and not mh_no: + flash("Enter at least a Location, RA Bill No, MH No, or Subcontractor to search", "danger") return render_template( "subcontractor_report.html", - subcontractors=subcontractors + subcontractors=subcontractors, + locations=locations, + has_data=has_data ) selected_sc_id = subcontractor_id @@ -234,7 +475,7 @@ def report_file(): if action == "excel_all": bill.Fetch(subcontractor_id=subcontractor_id) else: - bill.Fetch(ra_bill_no,subcontractor_id,location) + bill.Fetch(ra_bill_no, subcontractor_id, location, mh_no) # ----------------------------------------- # Generate Abstract Report for Web @@ -296,6 +537,15 @@ def report_file(): 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") + + # Used by the template to hide Delete Selected / Bulk Edit for a + # category that has no rows to act on. + has_data = { + "tr": not bill.df_tr.empty, + "mh": not bill.df_mh.empty, + "dc": not bill.df_dc.empty, + "laying": not bill.df_laying.empty, + } # this are html classes # table_class = ( "table " "table-bordered" "table-hover " "table-striped " "table-sm " "align-middle " "datatable " "mb-0") @@ -313,21 +563,24 @@ def report_file(): # 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) + "tr": render_table_or_empty(bill.df_tr, "tr", table_class, bill.tr_fields), + "mh": render_table_or_empty(bill.df_mh, "mh", table_class, bill.mh_fields), + "dc": render_table_or_empty(bill.df_dc, "dc", table_class, bill.dc_fields), + "laying": render_table_or_empty(bill.df_laying, "laying", table_class, bill.laying_fields) } return render_template( "subcontractor_report.html", subcontractors=subcontractors, + locations=locations, selected_sc_id=selected_sc_id, selected_ra_bill=ra_bill_no, selected_location=location, + selected_mh_no=mh_no, selected_category=category, tables=tables, - abstract_html=abstract_html + abstract_html=abstract_html, + has_data=has_data ) @@ -360,78 +613,35 @@ class ClientBill: # --- 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} +def client_report(): + + tables = {"tr": None, "mh": None, "dc": None, "laying": None} ra_val = "" if request.method == "POST": # ⚠ MUST match HTML name RA_Bill_No = request.form.get("RA_Bill_No") + action = request.form.get("action") 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) - -======= + + # -------- FETCH CLIENT DATA -------- + bill_gen = ClientBill() + bill_gen.Fetch(RA_Bill_No) + + # If no data + if ( + bill_gen.df_tr.empty and + bill_gen.df_mh.empty and + bill_gen.df_dc.empty and + 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) # -------- DOWNLOAD -------- if action == "download": @@ -549,5 +759,4 @@ def format_column_names(df): df.columns = new_columns - return df ->>>>>>> pankaj-dev + return df \ No newline at end of file diff --git a/app/templates/client_report.html b/app/templates/client_report.html index dfa2696..03f5bc3 100644 --- a/app/templates/client_report.html +++ b/app/templates/client_report.html @@ -31,7 +31,6 @@ type="button">Tr.Ex @@ -40,26 +39,9 @@ - - - + @@ -151,82 +155,75 @@ + + + + + - - + + @@ -41,6 +59,7 @@