added subcontractor report generation by RA bill and subcontractor name
This commit is contained in:
@@ -12,102 +12,40 @@ import pandas as pd
|
||||
import io
|
||||
from enum import Enum
|
||||
|
||||
# --- 1. DEFINE BLUEPRINT FIRST (Prevents NameError) ---
|
||||
file_report_bp = Blueprint("file_report", __name__, url_prefix="/file")
|
||||
|
||||
class BillType(Enum):
|
||||
Client = 1
|
||||
Subcontractor = 2
|
||||
|
||||
# --- 2. DEFINE CLASSES ---
|
||||
class SubcontractorBill:
|
||||
df_tr = []
|
||||
df_mh = []
|
||||
df_dc =[]
|
||||
def __init__(self):
|
||||
# Initialize as empty DataFrames so .to_excel() always exists
|
||||
self.df_tr = pd.DataFrame()
|
||||
self.df_mh = pd.DataFrame()
|
||||
self.df_dc = pd.DataFrame()
|
||||
|
||||
def Fetch(self, RA_Bill_No):
|
||||
# CLIENT DATA (RA BILL WISE)
|
||||
|
||||
trench = TrenchExcavation.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
||||
mh = ManholeExcavation.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
||||
dc = ManholeDomesticChamber.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
||||
def Fetch(self, RA_Bill_No, subcontractor_id):
|
||||
# Query data filtered by both Bill No and Subcontractor ID
|
||||
trench = TrenchExcavation.query.filter_by(RA_Bill_No=RA_Bill_No, subcontractor_id=subcontractor_id).all()
|
||||
mh = ManholeExcavation.query.filter_by(RA_Bill_No=RA_Bill_No, subcontractor_id=subcontractor_id).all()
|
||||
dc = ManholeDomesticChamber.query.filter_by(RA_Bill_No=RA_Bill_No, subcontractor_id=subcontractor_id).all()
|
||||
|
||||
# Convert SQL objects to DataFrames
|
||||
self.df_tr = pd.DataFrame([c.__dict__ for c in trench])
|
||||
self.df_mh = pd.DataFrame([c.__dict__ for c in mh])
|
||||
self.df_dc = pd.DataFrame([c.__dict__ for c in dc])
|
||||
|
||||
# CLEAN COLUMNS
|
||||
drop_cols = ["id","created_at","Remarks","_sa_instance_state"]
|
||||
|
||||
# Clean Columns (remove SQLAlchemy internal state)
|
||||
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)
|
||||
|
||||
mh_exc_columns = [
|
||||
"Location", "MH_NO", "Upto_IL_Depth", "Cutting_Depth", "ID_of_MH_m",
|
||||
"Ex_Dia_of_Manhole", "Area_of_Manhole",
|
||||
|
||||
"Soft_Murum_0_to_1_5", "Soft_Murum_1_5_to_3_0", "Soft_Murum_3_0_to_4_5",
|
||||
"Hard_Murum_0_to_1_5", "Hard_Murum_1_5_to_3_0",
|
||||
"Soft_Rock_0_to_1_5", "Soft_Rock_1_5_to_3_0",
|
||||
"Hard_Rock_0_to_1_5", "Hard_Rock_1_5_to_3_0",
|
||||
"Hard_Rock_3_0_to_4_5", "Hard_Rock_4_5_to_6_0", "Hard_Rock_6_0_to_7_5",
|
||||
|
||||
"Soft_Murum_0_to_1_5_total", "Soft_Murum_1_5_to_3_0_total",
|
||||
"Soft_Murum_3_0_to_4_5_total",
|
||||
"Hard_Murum_0_to_1_5_total", "Hard_Murum_1_5_and_above_total",
|
||||
"Soft_Rock_0_to_1_5_total", "Soft_Rock_1_5_and_above_total",
|
||||
"Hard_Rock_0_to_1_5_total", "Hard_Rock_1_5_and_above_total",
|
||||
"Hard_Rock_3_0_to_4_5_total", "Hard_Rock_4_5_to_6_0_total",
|
||||
"Hard_Rock_6_0_to_7_5_total",
|
||||
|
||||
"Remarks", "Total"
|
||||
]
|
||||
|
||||
trench_columns = [
|
||||
"Location", "MH_NO", "CC_length", "Invert_Level", "MH_Top_Level",
|
||||
"Ground_Level", "ID_of_MH_m", "Actual_Trench_Length", "Pipe_Dia_mm",
|
||||
|
||||
"Width_0_to_2_5", "Width_2_5_to_3_0", "Width_3_0_to_4_5", "Width_4_5_to_6_0",
|
||||
|
||||
"Upto_IL_Depth", "Cutting_Depth", "Avg_Depth",
|
||||
|
||||
"Soft_Murum_0_to_1_5", "Soft_Murum_1_5_to_3_0", "Soft_Murum_3_0_to_4_5",
|
||||
"Hard_Murum_0_to_1_5", "Hard_Murum_1_5_to_3_0",
|
||||
"Soft_Rock_0_to_1_5", "Soft_Rock_1_5_to_3_0",
|
||||
"Hard_Rock_0_to_1_5", "Hard_Rock_1_5_to_3_0",
|
||||
"Hard_Rock_3_0_to_4_5", "Hard_Rock_4_5_to_6_0", "Hard_Rock_6_0_to_7_5",
|
||||
|
||||
"Soft_Murum_0_to_1_5_total", "Soft_Murum_1_5_to_3_0_total",
|
||||
"Soft_Murum_3_0_to_4_5_total",
|
||||
"Hard_Murum_0_to_1_5_total", "Hard_Murum_1_5_and_above_total",
|
||||
"Soft_Rock_0_to_1_5_total", "Soft_Rock_1_5_and_above_total",
|
||||
"Hard_Rock_0_to_1_5_total", "Hard_Rock_1_5_and_above_total",
|
||||
"Hard_Rock_3_0_to_4_5_total", "Hard_Rock_4_5_to_6_0_total",
|
||||
"Hard_Rock_6_0_to_7_5_total",
|
||||
|
||||
"Remarks", "Total"
|
||||
]
|
||||
|
||||
domestic_columns = [
|
||||
"Location", "Node_No", "Depth_of_MH",
|
||||
"d_0_to_0_75", "d_1_06_to_1_65", "d_1_66_to_2_15",
|
||||
"d_2_16_to_2_65", "d_2_66_to_3_15", "d_3_16_to_3_65",
|
||||
"d_3_66_to_4_15", "d_4_16_to_4_65", "d_4_66_to_5_15",
|
||||
"d_5_16_to_5_65", "d_5_66_to_6_15", "d_6_16_to_6_65",
|
||||
"d_6_66_to_7_15", "d_7_16_to_7_65", "d_7_66_to_8_15",
|
||||
"d_8_16_to_8_65", "d_8_66_to_9_15", "d_9_16_to_9_65",
|
||||
"Domestic_Chambers"
|
||||
]
|
||||
|
||||
|
||||
|
||||
# Reorder columns serial wise
|
||||
df_mh_exc = self.df_mh.reindex(columns=mh_exc_columns, fill_value="")
|
||||
df_trench = self.df_tr.reindex(columns=trench_columns, fill_value="")
|
||||
df_domestic = self.df_dc.reindex(columns=domestic_columns, fill_value="")
|
||||
|
||||
|
||||
|
||||
# get report by contractor id and dowanload
|
||||
# --- 3. DEFINE ROUTES ---
|
||||
@file_report_bp.route("/report", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def report_file():
|
||||
@@ -115,27 +53,33 @@ def report_file():
|
||||
|
||||
if request.method == "POST":
|
||||
subcontractor_id = request.form.get("subcontractor_id")
|
||||
ra_bill_no = request.form.get("ra_bill_no") # Collected from the updated HTML
|
||||
|
||||
if not subcontractor_id:
|
||||
flash("Please select a subcontractor.", "danger")
|
||||
if not subcontractor_id or not ra_bill_no:
|
||||
flash("Please select a subcontractor and enter an RA Bill Number.", "danger")
|
||||
return render_template("report.html", subcontractors=subcontractors)
|
||||
|
||||
# Fetch subcontractor for file name
|
||||
subcontractor = Subcontractor.query.get(subcontractor_id)
|
||||
subcontractorBill = SubcontractorBill()
|
||||
|
||||
|
||||
# WRITE EXCEL FILE
|
||||
# Instantiate and Fetch Data
|
||||
bill_gen = SubcontractorBill()
|
||||
bill_gen.Fetch(RA_Bill_No=ra_bill_no, subcontractor_id=subcontractor_id)
|
||||
|
||||
# Check if any data was found
|
||||
if bill_gen.df_tr.empty and bill_gen.df_mh.empty and bill_gen.df_dc.empty:
|
||||
flash(f"No data found for {subcontractor.subcontractor_name} in RA Bill {ra_bill_no}", "warning")
|
||||
return render_template("report.html", subcontractors=subcontractors)
|
||||
|
||||
# WRITE EXCEL FILE
|
||||
output = io.BytesIO()
|
||||
file_name = f"{subcontractor.subcontractor_name}_Report.xlsx"
|
||||
file_name = f"{subcontractor.subcontractor_name}_RA_{ra_bill_no}_Report.xlsx"
|
||||
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
subcontractorBill.df_tr.to_excel(writer, index=False, sheet_name="Tr.Ex.")
|
||||
subcontractorBill.df_mh.to_excel(writer, index=False, sheet_name="MH.Ex.")
|
||||
subcontractorBill.df_dc.to_excel(writer, index=False, sheet_name="MH & DC")
|
||||
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")
|
||||
|
||||
output.seek(0)
|
||||
|
||||
return send_file(
|
||||
output,
|
||||
download_name=file_name,
|
||||
@@ -145,31 +89,8 @@ def report_file():
|
||||
|
||||
return render_template("report.html", subcontractors=subcontractors)
|
||||
|
||||
# (ClientBill class and client_vs_all_subcontractor route would follow here...)
|
||||
|
||||
class ClientBill:
|
||||
df_tr = []
|
||||
df_mh = []
|
||||
df_dc =[]
|
||||
|
||||
def Fetch(self, RA_Bill_No):
|
||||
# CLIENT DATA (RA BILL WISE)
|
||||
|
||||
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.__dict__ for c in trench])
|
||||
self.df_mh = pd.DataFrame([c.__dict__ for c in mh])
|
||||
self.df_dc = pd.DataFrame([c.__dict__ for c in dc])
|
||||
|
||||
# CLEAN COLUMNS
|
||||
drop_cols = ["id","created_at","Remarks","_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)
|
||||
|
||||
|
||||
|
||||
@file_report_bp.route("/client_vs_subcont", methods=["GET", "POST"])
|
||||
@login_required
|
||||
@@ -300,4 +221,4 @@ def client_vs_all_subcontractor():
|
||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
)
|
||||
|
||||
return render_template("generate_comparison_client_vs_subcont.html")
|
||||
return render_template("generate_comparison_client_vs_subcont.html")
|
||||
@@ -2,38 +2,39 @@
|
||||
|
||||
{% block content %}
|
||||
<div class="container mt-4">
|
||||
<h2 class="mb-4">Generate Subcontractor Report</h2>
|
||||
|
||||
<h2 class="mb-4">File Report</h2>
|
||||
|
||||
<!-- FLASH MESSAGES -->
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% if messages %}
|
||||
{% for category, message in messages %}
|
||||
<div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
|
||||
{{ message }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% if messages %}
|
||||
{% for category, message in messages %}
|
||||
<div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
|
||||
{{ message }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
)
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
<div class="card p-4 shadow-sm">
|
||||
|
||||
<form method="POST">
|
||||
<div class="mb-3">
|
||||
<label class="form-label fw-semibold">Select Subcontractor</label>
|
||||
<select name="subcontractor_id" class="form-select" required>
|
||||
<option value="">-- Select Subcontractor --</option>
|
||||
{% for sc in subcontractors %}
|
||||
<option value="{{ sc.id }}">{{ sc.subcontractor_name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- SELECT SUBCONTRACTOR -->
|
||||
<label class="form-label fw-semibold">Select Subcontractor</label>
|
||||
<select name="subcontractor_id" id="subcontractor_id" class="form-select mb-3" required>
|
||||
<option value="">-- Select Subcontractor --</option>
|
||||
<div class="mb-3">
|
||||
<label class="form-label fw-semibold">RA Bill Number</label>
|
||||
<input type="text" name="ra_bill_no" class="form-control" placeholder="e.g. 01" required>
|
||||
</div>
|
||||
|
||||
{% for sc in subcontractors %}
|
||||
<option value="{{ sc.id }}">{{ sc.subcontractor_name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
|
||||
<button class="btn btn-primary w-100">Generate Report</button>
|
||||
<button type="submit" class="btn btn-primary w-100">Generate Excel Report</button>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user