changes of genrate report
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
|
||||
from flask import Blueprint, render_template, request, send_file, flash
|
||||
from app import db
|
||||
import pandas as pd
|
||||
import io
|
||||
|
||||
from app.models.subcontractor_model import Subcontractor
|
||||
|
||||
from app.models.trench_excavation_model import TrenchExcavation
|
||||
from app.models.tr_ex_client_model import TrenchExcavationClient
|
||||
from app.models.manhole_excavation_model import ManholeExcavation
|
||||
@@ -12,10 +11,20 @@ from app.models.mh_ex_client_model import ManholeExcavationClient
|
||||
from app.models.manhole_domestic_chamber_model import ManholeDomesticChamber
|
||||
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
|
||||
|
||||
|
||||
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
|
||||
|
||||
|
||||
|
||||
# HELPER FUNCTION
|
||||
|
||||
def make_lookup(rows, key_field):
|
||||
return {
|
||||
(r.Location, getattr(r, key_field)): r
|
||||
for r in rows
|
||||
if r.Location and getattr(r, key_field, None)
|
||||
}
|
||||
|
||||
|
||||
@generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
|
||||
def comparison_report():
|
||||
subcontractors = Subcontractor.query.all()
|
||||
@@ -25,69 +34,38 @@ def comparison_report():
|
||||
|
||||
if not subcontractor_id:
|
||||
flash("Please select a subcontractor.", "danger")
|
||||
return render_template(
|
||||
"generate_comparison_report.html",
|
||||
subcontractors=subcontractors
|
||||
)
|
||||
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
||||
|
||||
subcontractor = Subcontractor.query.get_or_404(subcontractor_id)
|
||||
# --------------------------- Tr.Ex model -----------------------------
|
||||
contractor_tr_ex = TrenchExcavation.query.filter_by(
|
||||
subcontractor_id=subcontractor_id
|
||||
).all()
|
||||
|
||||
client_tr_ex = TrenchExcavationClient.query.filter_by(
|
||||
subcontractor_id=subcontractor_id
|
||||
).all()
|
||||
# ================= TRENCH EXCAVATION =================
|
||||
client_tr_ex = TrenchExcavationClient.query.all()
|
||||
contractor_tr_ex = TrenchExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
|
||||
|
||||
contractor_lookup = make_lookup(contractor_tr_ex, "MH_NO")
|
||||
combined_rows_tr_ex = []
|
||||
|
||||
for row1, row2 in zip(client_tr_ex, contractor_tr_ex):
|
||||
# -------- Tr.Ex CLIENT TOTAL ----------
|
||||
client_total = (
|
||||
(row1.Marshi_Muddy_Slushy_0_to_1_5_total or 0) +
|
||||
(row1.Marshi_Muddy_Slushy_1_5_to_3_0_total or 0) +
|
||||
(row1.Marshi_Muddy_Slushy_3_0_to_4_5_total or 0) +
|
||||
(row1.Soft_Murum_0_to_1_5_total or 0) +
|
||||
(row1.Soft_Murum_1_5_to_3_0_total or 0) +
|
||||
(row1.Soft_Murum_3_0_to_4_5_total or 0) +
|
||||
(row1.Hard_Murum_0_to_1_5_total or 0) +
|
||||
(row1.Hard_Murum_1_5_to_3_0_total or 0) +
|
||||
(row1.Hard_Murum_3_0_to_4_5_total or 0) +
|
||||
(row1.Soft_Rock_0_to_1_5_total or 0) +
|
||||
(row1.Soft_Rock_1_5_to_3_0_total or 0) +
|
||||
(row1.Soft_Rock_3_0_to_4_5_total or 0) +
|
||||
(row1.Hard_Rock_0_to_1_5_total or 0) +
|
||||
(row1.Hard_Rock_1_5_to_3_0_total or 0) +
|
||||
(row1.Hard_Rock_3_0_to_4_5_total or 0) +
|
||||
(row1.Hard_Rock_4_5_to_6_0_total or 0) +
|
||||
(row1.Hard_Rock_6_0_to_7_5_total or 0)
|
||||
)
|
||||
for row1 in client_tr_ex:
|
||||
row2 = contractor_lookup.get((row1.Location, row1.MH_NO))
|
||||
if not row2:
|
||||
continue
|
||||
|
||||
# -------- SUBCONTRACTOR TOTAL ----------
|
||||
contractor_total = (
|
||||
(row2.Soft_Murum_0_to_1_5_total or 0) +
|
||||
(row2.Soft_Murum_1_5_to_3_0_total or 0) +
|
||||
(row2.Soft_Murum_3_0_to_4_5_total or 0) +
|
||||
(row2.Hard_Murum_0_to_1_5_total or 0) +
|
||||
(row2.Hard_Murum_1_5_and_above_total or 0) +
|
||||
(row2.Soft_Rock_0_to_1_5_total or 0) +
|
||||
(row2.Soft_Rock_1_5_and_above_total or 0) +
|
||||
(row2.Hard_Rock_0_to_1_5_total or 0) +
|
||||
(row2.Hard_Rock_1_5_and_above_total or 0) +
|
||||
(row2.Hard_Rock_4_5_to_6_0_total or 0) +
|
||||
(row2.Hard_Rock_6_0_to_7_5_total or 0)
|
||||
)
|
||||
# -------- CLIENT TOTAL ----------
|
||||
client_total = sum(v or 0 for k, v in row1.__dict__.items()
|
||||
if k.endswith("_total"))
|
||||
|
||||
# -------- SUB TOTAL ----------
|
||||
contractor_total = sum(v or 0 for k, v in row2.__dict__.items()
|
||||
if k.endswith("_total"))
|
||||
|
||||
diff = client_total - contractor_total
|
||||
|
||||
# -------- COMBINED ROW ----------
|
||||
row_data = {
|
||||
"Location": row1.Location,
|
||||
"MH No": row1.MH_NO,
|
||||
}
|
||||
|
||||
# Client columns
|
||||
# -------- CLIENT COLUMNS ----------
|
||||
for col, val in row1.__dict__.items():
|
||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||
continue
|
||||
@@ -96,7 +74,7 @@ def comparison_report():
|
||||
row_data["Client_Total"] = round(client_total, 2)
|
||||
row_data[" - "] = " "
|
||||
|
||||
# Subcontractor columns
|
||||
# -------- SUB COLUMNS ----------
|
||||
for col, val in row2.__dict__.items():
|
||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||
continue
|
||||
@@ -106,77 +84,34 @@ def comparison_report():
|
||||
row_data["--"] = " "
|
||||
row_data["Diff"] = round(diff, 2)
|
||||
|
||||
|
||||
combined_rows_tr_ex.append(row_data)
|
||||
|
||||
# Console log
|
||||
print(
|
||||
f"{row1.Location} | {row1.MH_NO} | "
|
||||
f"Client={client_total} | Sub={contractor_total} | Diff={diff}"
|
||||
)
|
||||
|
||||
|
||||
df_tr_ex = pd.DataFrame(combined_rows_tr_ex)
|
||||
|
||||
# --------------------------- Mh.Ex model -----------------------------
|
||||
contractor_mh_ex = ManholeExcavation.query.filter_by(
|
||||
subcontractor_id=subcontractor_id
|
||||
).all()
|
||||
|
||||
client_mh_ex = ManholeExcavationClient.query.filter_by(
|
||||
subcontractor_id=subcontractor_id
|
||||
).all()
|
||||
# ================= MANHOLE EXCAVATION =================
|
||||
client_mh_ex = ManholeExcavationClient.query.all()
|
||||
contractor_mh_ex = ManholeExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
|
||||
|
||||
contractor_lookup = make_lookup(contractor_mh_ex, "MH_NO")
|
||||
combined_rows_mh_ex = []
|
||||
|
||||
for row1, row2 in zip(client_mh_ex, contractor_mh_ex):
|
||||
# -------- Tr.Ex CLIENT TOTAL ----------
|
||||
client_total = (
|
||||
(row1.Marshi_Muddy_Slushy_0_to_1_5_total or 0) +
|
||||
(row1.Marshi_Muddy_Slushy_1_5_to_3_0_total or 0) +
|
||||
(row1.Marshi_Muddy_Slushy_3_0_to_4_5_total or 0) +
|
||||
(row1.Soft_Murum_0_to_1_5_total or 0) +
|
||||
(row1.Soft_Murum_1_5_to_3_0_total or 0) +
|
||||
(row1.Soft_Murum_3_0_to_4_5_total or 0) +
|
||||
(row1.Hard_Murum_0_to_1_5_total or 0) +
|
||||
(row1.Hard_Murum_1_5_to_3_0_total or 0) +
|
||||
(row1.Hard_Murum_3_0_to_4_5_total or 0) +
|
||||
(row1.Soft_Rock_0_to_1_5_total or 0) +
|
||||
(row1.Soft_Rock_1_5_to_3_0_total or 0) +
|
||||
(row1.Soft_Rock_3_0_to_4_5_total or 0) +
|
||||
(row1.Hard_Rock_0_to_1_5_total or 0) +
|
||||
(row1.Hard_Rock_1_5_to_3_0_total or 0) +
|
||||
(row1.Hard_Rock_3_0_to_4_5_total or 0) +
|
||||
(row1.Hard_Rock_4_5_to_6_0_total or 0) +
|
||||
(row1.Hard_Rock_6_0_to_7_5_total or 0)
|
||||
|
||||
)
|
||||
for row1 in client_mh_ex:
|
||||
row2 = contractor_lookup.get((row1.Location, row1.MH_NO))
|
||||
if not row2:
|
||||
continue
|
||||
|
||||
# -------- SUBCONTRACTOR TOTAL ----------
|
||||
contractor_total = (
|
||||
(row2.Soft_Murum_0_to_1_5_total or 0) +
|
||||
(row2.Soft_Murum_1_5_to_3_0_total or 0) +
|
||||
(row2.Soft_Murum_3_0_to_4_5_total or 0) +
|
||||
(row2.Hard_Murum_0_to_1_5_total or 0) +
|
||||
(row2.Hard_Murum_1_5_and_above_total or 0) +
|
||||
(row2.Soft_Rock_0_to_1_5_total or 0) +
|
||||
(row2.Soft_Rock_1_5_and_above_total or 0) +
|
||||
(row2.Hard_Rock_0_to_1_5_total or 0) +
|
||||
(row2.Hard_Rock_1_5_and_above_total or 0) +
|
||||
(row2.Hard_Rock_3_0_to_4_5_total or 0) +
|
||||
(row2.Hard_Rock_4_5_to_6_0_total or 0) +
|
||||
(row2.Hard_Rock_6_0_to_7_5_total or 0)
|
||||
)
|
||||
client_total = sum(v or 0 for k, v in row1.__dict__.items()
|
||||
if k.endswith("_total"))
|
||||
contractor_total = sum(v or 0 for k, v in row2.__dict__.items()
|
||||
if k.endswith("_total"))
|
||||
|
||||
diff = client_total - contractor_total
|
||||
|
||||
# -------- COMBINED ROW ----------
|
||||
row_data = {
|
||||
"Location": row1.Location,
|
||||
"MH No": row1.MH_NO,
|
||||
}
|
||||
|
||||
# Client columns
|
||||
for col, val in row1.__dict__.items():
|
||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||
continue
|
||||
@@ -185,7 +120,6 @@ def comparison_report():
|
||||
row_data["Client_Total"] = round(client_total, 2)
|
||||
row_data[" - "] = " "
|
||||
|
||||
# Subcontractor columns
|
||||
for col, val in row2.__dict__.items():
|
||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||
continue
|
||||
@@ -197,74 +131,33 @@ def comparison_report():
|
||||
|
||||
combined_rows_mh_ex.append(row_data)
|
||||
|
||||
# Console log
|
||||
print(
|
||||
f"{row1.Location} | {row1.MH_NO} | "
|
||||
f"Client={client_total} | Sub={contractor_total} | Diff={diff}"
|
||||
)
|
||||
|
||||
df_mh_ex = pd.DataFrame(combined_rows_mh_ex)
|
||||
|
||||
|
||||
# ============ MANHOLE DOMESTIC CHAMBER =================
|
||||
client_mh_dc = ManholeDomesticChamberClient.query.all()
|
||||
contractor_mh_dc = ManholeDomesticChamber.query.filter_by(subcontractor_id=subcontractor_id).all()
|
||||
|
||||
# ---------------------------- MH dC Model --------------------------
|
||||
contractor_mh_dc = ManholeDomesticChamber.query.filter_by(
|
||||
subcontractor_id=subcontractor_id
|
||||
).all()
|
||||
|
||||
client_mh_dc = ManholeDomesticChamberClient.query.filter_by(
|
||||
subcontractor_id=subcontractor_id
|
||||
).all()
|
||||
|
||||
contractor_lookup = make_lookup(contractor_mh_dc, "Node_no")
|
||||
combined_rows_mh_dc = []
|
||||
|
||||
for row1, row2 in zip(client_mh_dc, contractor_mh_dc):
|
||||
client_total = (
|
||||
(row1.d_0_to_1_5 or 0) +
|
||||
(row1.d_1_5_to_2_0 or 0) +
|
||||
(row1.d_2_0_to_2_5 or 0) +
|
||||
(row1.d_2_5_to_3_0 or 0) +
|
||||
(row1.d_3_0_to_3_5 or 0) +
|
||||
(row1.d_3_5_to_4_0 or 0) +
|
||||
(row1.d_4_0_to_4_5 or 0) +
|
||||
(row1.d_4_5_to_5_0 or 0) +
|
||||
(row1.d_5_0_to_5_5 or 0) +
|
||||
(row1.d_5_5_to_6_0 or 0) +
|
||||
(row1.d_6_0_to_6_5 or 0) +
|
||||
(row1.Domestic_Chambers or 0)
|
||||
)
|
||||
for row1 in client_mh_dc:
|
||||
row2 = contractor_lookup.get((row1.Location, row1.MH_NO))
|
||||
if not row2:
|
||||
continue
|
||||
|
||||
contractor_total = (
|
||||
(row2.d_0_to_0_75 or 0) +
|
||||
(row2.d_0_76_to_1_05 or 0) +
|
||||
(row2.d_1_06_to_1_65 or 0) +
|
||||
(row2.d_1_66_to_2_15 or 0) +
|
||||
(row2.d_2_16_to_2_65 or 0) +
|
||||
(row2.d_2_66_to_3_15 or 0) +
|
||||
(row2.d_3_16_to_3_65 or 0) +
|
||||
(row2.d_3_66_to_4_15 or 0) +
|
||||
(row2.d_4_16_to_4_65 or 0) +
|
||||
(row2.d_4_66_to_5_15 or 0) +
|
||||
(row2.d_5_16_to_5_65 or 0) +
|
||||
(row2.d_5_66_to_6_15 or 0) +
|
||||
(row2.d_6_16_to_6_65 or 0) +
|
||||
(row2.d_6_66_to_7_15 or 0) +
|
||||
(row2.d_7_16_to_7_65 or 0) +
|
||||
(row2.d_7_66_to_8_15 or 0) +
|
||||
(row2.d_8_16_to_8_65 or 0) +
|
||||
(row2.d_8_66_to_9_15 or 0) +
|
||||
(row2.d_9_16_to_9_65 or 0) +
|
||||
(row2.Domestic_Chambers or 0)
|
||||
)
|
||||
client_total = sum(v or 0 for k, v in row1.__dict__.items()
|
||||
if isinstance(v, (int, float)))
|
||||
contractor_total = sum(v or 0 for k, v in row2.__dict__.items()
|
||||
if isinstance(v, (int, float)))
|
||||
|
||||
diff = client_total - contractor_total
|
||||
|
||||
# -------- COMBINED ROW ----------
|
||||
row_data = {
|
||||
"Location": row1.Location,
|
||||
"Node No": row1.Node_No,
|
||||
"Node No": row1.MH_NO,
|
||||
}
|
||||
|
||||
# Client columns
|
||||
for col, val in row1.__dict__.items():
|
||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||
continue
|
||||
@@ -272,8 +165,7 @@ def comparison_report():
|
||||
|
||||
row_data["Client_Total"] = round(client_total, 2)
|
||||
row_data[" - "] = " "
|
||||
|
||||
# Subcontractor columns
|
||||
|
||||
for col, val in row2.__dict__.items():
|
||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||
continue
|
||||
@@ -285,24 +177,17 @@ def comparison_report():
|
||||
|
||||
combined_rows_mh_dc.append(row_data)
|
||||
|
||||
# Console log
|
||||
print(
|
||||
f"{row1.Location} | {row1.Node_No} | "
|
||||
f"Client={client_total} | Sub={contractor_total} | Diff={diff}"
|
||||
)
|
||||
|
||||
df_mh_dc = pd.DataFrame(combined_rows_mh_dc)
|
||||
|
||||
|
||||
|
||||
|
||||
# ================== EXCEL EXPORT ======================
|
||||
output = io.BytesIO()
|
||||
file_name = f"{subcontractor.subcontractor_name}_Comparison_Report.xlsx"
|
||||
|
||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||
df_tr_ex.to_excel(writer, index=False, sheet_name="Tr.Ex")
|
||||
df_mh_ex.to_excel(writer,index=False,sheet_name="Mh.Ex")
|
||||
df_mh_dc.to_excel(writer,index=False,sheet_name="MH & DC")
|
||||
|
||||
df_mh_ex.to_excel(writer, index=False, sheet_name="Mh.Ex")
|
||||
df_mh_dc.to_excel(writer, index=False, sheet_name="MH & DC")
|
||||
|
||||
output.seek(0)
|
||||
|
||||
@@ -313,9 +198,4 @@ def comparison_report():
|
||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
)
|
||||
|
||||
return render_template(
|
||||
"generate_comparison_report.html",
|
||||
subcontractors=subcontractors
|
||||
)
|
||||
|
||||
|
||||
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
||||
|
||||
Reference in New Issue
Block a user