changes of genrate report
This commit is contained in:
@@ -11,41 +11,36 @@ from app import db
|
|||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import io
|
import io
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
file_report_bp = Blueprint("file_report", __name__, url_prefix="/file")
|
file_report_bp = Blueprint("file_report", __name__, url_prefix="/file")
|
||||||
|
|
||||||
# get report by contractor id and dowanload
|
class BillType(Enum):
|
||||||
@file_report_bp.route("/report", methods=["GET", "POST"])
|
Client = 1
|
||||||
def report_file():
|
Subcontractor = 2
|
||||||
subcontractors = Subcontractor.query.all()
|
|
||||||
|
|
||||||
if request.method == "POST":
|
class SubcontractorBill:
|
||||||
subcontractor_id = request.form.get("subcontractor_id")
|
df_tr = []
|
||||||
|
df_mh = []
|
||||||
|
df_dc =[]
|
||||||
|
|
||||||
if not subcontractor_id:
|
def Fetch(self, RA_Bill_No):
|
||||||
flash("Please select a subcontractor.", "danger")
|
# CLIENT DATA (RA BILL WISE)
|
||||||
return render_template("report.html", subcontractors=subcontractors)
|
|
||||||
|
|
||||||
# Fetch subcontractor for file name
|
trench = TrenchExcavation.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
||||||
subcontractor = Subcontractor.query.get(subcontractor_id)
|
mh = ManholeExcavation.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
||||||
|
dc = ManholeDomesticChamber.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
||||||
manhole_excavation = ManholeExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
|
|
||||||
trench_excavation = TrenchExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
|
|
||||||
domestic_chamber = ManholeDomesticChamber.query.filter_by(subcontractor_id=subcontractor_id).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])
|
||||||
|
|
||||||
# Convert to DataFrame
|
# CLEAN COLUMNS
|
||||||
df_mh_exc = pd.DataFrame([m.__dict__ for m in manhole_excavation])
|
drop_cols = ["id","created_at","Remarks","_sa_instance_state"]
|
||||||
df_trench = pd.DataFrame([t.__dict__ for t in trench_excavation])
|
|
||||||
df_domestic = pd.DataFrame([d.__dict__ for d in domestic_chamber])
|
|
||||||
|
|
||||||
# Drop unnecessary SQLAlchemy fields
|
for df in [self.df_tr, self.df_mh, self.df_dc]:
|
||||||
drop_cols = ["id", "subcontractor_id", "created_at", "_sa_instance_state","Remarks"]
|
if not df.empty:
|
||||||
|
df.drop(columns=drop_cols, errors="ignore", inplace=True)
|
||||||
df_mh_exc.drop(columns=drop_cols, errors="ignore", inplace=True)
|
|
||||||
df_trench.drop(columns=drop_cols, errors="ignore", inplace=True)
|
|
||||||
df_domestic.drop(columns=drop_cols, errors="ignore", inplace=True)
|
|
||||||
|
|
||||||
|
|
||||||
mh_exc_columns = [
|
mh_exc_columns = [
|
||||||
"Location", "MH_NO", "Upto_IL_Depth", "Cutting_Depth", "ID_of_MH_m",
|
"Location", "MH_NO", "Upto_IL_Depth", "Cutting_Depth", "ID_of_MH_m",
|
||||||
@@ -106,19 +101,40 @@ def report_file():
|
|||||||
"Domestic_Chambers"
|
"Domestic_Chambers"
|
||||||
]
|
]
|
||||||
|
|
||||||
# Reorder columns serial wise
|
|
||||||
df_mh_exc = df_mh_exc.reindex(columns=mh_exc_columns, fill_value="")
|
|
||||||
df_trench = df_trench.reindex(columns=trench_columns, fill_value="")
|
|
||||||
df_domestic = df_domestic.reindex(columns=domestic_columns, fill_value="")
|
|
||||||
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
@file_report_bp.route("/report", methods=["GET", "POST"])
|
||||||
|
def report_file():
|
||||||
|
subcontractors = Subcontractor.query.all()
|
||||||
|
|
||||||
|
if request.method == "POST":
|
||||||
|
subcontractor_id = request.form.get("subcontractor_id")
|
||||||
|
|
||||||
|
if not subcontractor_id:
|
||||||
|
flash("Please select a subcontractor.", "danger")
|
||||||
|
return render_template("report.html", subcontractors=subcontractors)
|
||||||
|
|
||||||
|
# Fetch subcontractor for file name
|
||||||
|
subcontractor = Subcontractor.query.get(subcontractor_id)
|
||||||
|
subcontractorBill = SubcontractorBill()
|
||||||
|
|
||||||
|
|
||||||
# WRITE EXCEL FILE
|
# WRITE EXCEL FILE
|
||||||
output = io.BytesIO()
|
output = io.BytesIO()
|
||||||
file_name = f"{subcontractor.subcontractor_name}_Report.xlsx"
|
file_name = f"{subcontractor.subcontractor_name}_Report.xlsx"
|
||||||
|
|
||||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||||
df_trench.to_excel(writer, index=False, sheet_name="Tr.Ex.")
|
subcontractorBill.df_tr.to_excel(writer, index=False, sheet_name="Tr.Ex.")
|
||||||
df_mh_exc.to_excel(writer, index=False, sheet_name="MH.Ex.")
|
subcontractorBill.df_mh.to_excel(writer, index=False, sheet_name="MH.Ex.")
|
||||||
df_domestic.to_excel(writer, index=False, sheet_name="MH & DC")
|
subcontractorBill.df_dc.to_excel(writer, index=False, sheet_name="MH & DC")
|
||||||
|
|
||||||
output.seek(0)
|
output.seek(0)
|
||||||
|
|
||||||
@@ -132,9 +148,30 @@ def report_file():
|
|||||||
return render_template("report.html", subcontractors=subcontractors)
|
return render_template("report.html", subcontractors=subcontractors)
|
||||||
|
|
||||||
|
|
||||||
|
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"])
|
@file_report_bp.route("/client_vs_subcont", methods=["GET", "POST"])
|
||||||
def client_vs_all_subcontractor():
|
def client_vs_all_subcontractor():
|
||||||
@@ -147,31 +184,13 @@ def client_vs_all_subcontractor():
|
|||||||
flash("Please select RA Bill No.", "danger")
|
flash("Please select RA Bill No.", "danger")
|
||||||
return render_template("generate_comparison_client_vs_subcont.html")
|
return render_template("generate_comparison_client_vs_subcont.html")
|
||||||
|
|
||||||
|
clientBill = ClientBill()
|
||||||
|
clientBill.Fetch(RA_Bill_No=RA_Bill_No)
|
||||||
|
|
||||||
|
contractorBill = SubcontractorBill()
|
||||||
|
contractorBill.Fetch(RA_Bill_No=RA_Bill_No)
|
||||||
|
|
||||||
|
|
||||||
# CLIENT DATA (RA BILL WISE)
|
|
||||||
client_trench = TrenchExcavationClient.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
|
||||||
client_mh = ManholeExcavationClient.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
|
||||||
client_dc = ManholeDomesticChamberClient.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
|
||||||
|
|
||||||
df_client_tr = pd.DataFrame([c.__dict__ for c in client_trench])
|
|
||||||
df_client_mh = pd.DataFrame([c.__dict__ for c in client_mh])
|
|
||||||
df_client_dc = pd.DataFrame([c.__dict__ for c in client_dc])
|
|
||||||
|
|
||||||
# ALL SUBCONTRACTOR DATA
|
|
||||||
sub_trench = TrenchExcavation.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
|
||||||
sub_mh = ManholeExcavation.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
|
||||||
sub_dc = ManholeDomesticChamber.query.filter_by(RA_Bill_No=RA_Bill_No).all()
|
|
||||||
|
|
||||||
df_sub_tr = pd.DataFrame([s.__dict__ for s in sub_trench])
|
|
||||||
df_sub_mh = pd.DataFrame([s.__dict__ for s in sub_mh])
|
|
||||||
df_sub_dc = pd.DataFrame([s.__dict__ for s in sub_dc])
|
|
||||||
|
|
||||||
# CLEAN COLUMNS
|
|
||||||
drop_cols = ["id","created_at","Remarks","_sa_instance_state"]
|
|
||||||
|
|
||||||
for df in [df_client_tr, df_client_mh, df_client_dc,df_sub_tr, df_sub_mh, df_sub_dc]:
|
|
||||||
if not df.empty:
|
|
||||||
df.drop(columns=drop_cols, errors="ignore", inplace=True)
|
|
||||||
|
|
||||||
|
|
||||||
# GROUP SUBCONTRACTOR DATA
|
# GROUP SUBCONTRACTOR DATA
|
||||||
@@ -213,31 +232,31 @@ def client_vs_all_subcontractor():
|
|||||||
"Domestic_Chambers"
|
"Domestic_Chambers"
|
||||||
]
|
]
|
||||||
|
|
||||||
df_sub_tr_grp = (df_sub_tr.groupby(["Location", "MH_NO"], as_index=False)[qty_cols].sum())
|
df_sub_tr_grp = (contractorBill.df_tr.groupby(["Location", "MH_NO"], as_index=False)[qty_cols].sum())
|
||||||
|
|
||||||
df_sub_mh_grp = (df_sub_mh.groupby(["Location", "MH_NO"], as_index=False)[qty_cols].sum())
|
df_sub_mh_grp = (contractorBill.df_mh.groupby(["Location", "MH_NO"], as_index=False)[qty_cols].sum())
|
||||||
|
|
||||||
df_sub_dc_grp = (df_sub_dc.groupby(["Location", "Node_No"], as_index=False)[mh_dc_qty_cols].sum())
|
df_sub_dc_grp = (contractorBill.df_dc.groupby(["Location", "Node_No"], as_index=False)[mh_dc_qty_cols].sum())
|
||||||
|
|
||||||
# MERGE CLIENT VS SUBCONTRACTOR
|
# MERGE CLIENT VS SUBCONTRACTOR
|
||||||
df_tr_cmp = df_client_tr.merge(
|
df_tr_cmp = clientBill.df_tr.merge(
|
||||||
df_sub_tr_grp,
|
df_sub_tr_grp,
|
||||||
on=["Location", "MH_NO"],
|
on=["Location", "MH_NO"],
|
||||||
how="left",
|
how="left",
|
||||||
suffixes=("_Client", "_Sub")
|
suffixes=("_Client", "_Sub")
|
||||||
)
|
)
|
||||||
|
|
||||||
df_mh_cmp = df_client_mh.merge(
|
df_mh_cmp = clientBill.df_mh.merge(
|
||||||
df_sub_mh_grp,
|
df_sub_mh_grp,
|
||||||
on=["Location", "MH_NO"],
|
on=["Location", "MH_NO"],
|
||||||
how="left",
|
how="left",
|
||||||
suffixes=("_Client", "_Sub")
|
suffixes=("_Client", "_Sub")
|
||||||
)
|
)
|
||||||
|
|
||||||
if "MH_NO" in df_client_dc.columns:
|
if "MH_NO" in clientBill.df_dc.columns:
|
||||||
df_client_dc.rename(columns={"MH_NO": "Node_No"}, inplace=True)
|
clientBill.df_dc.rename(columns={"MH_NO": "Node_No"}, inplace=True)
|
||||||
|
|
||||||
df_dc_cmp = df_client_dc.merge(
|
df_dc_cmp = clientBill.df_dc.merge(
|
||||||
df_sub_dc_grp,
|
df_sub_dc_grp,
|
||||||
on=["Location", "Node_No"],
|
on=["Location", "Node_No"],
|
||||||
how="left",
|
how="left",
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
|
|
||||||
from flask import Blueprint, render_template, request, send_file, flash
|
from flask import Blueprint, render_template, request, send_file, flash
|
||||||
from app import db
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import io
|
import io
|
||||||
|
|
||||||
from app.models.subcontractor_model import Subcontractor
|
from app.models.subcontractor_model import Subcontractor
|
||||||
|
|
||||||
from app.models.trench_excavation_model import TrenchExcavation
|
from app.models.trench_excavation_model import TrenchExcavation
|
||||||
from app.models.tr_ex_client_model import TrenchExcavationClient
|
from app.models.tr_ex_client_model import TrenchExcavationClient
|
||||||
from app.models.manhole_excavation_model import ManholeExcavation
|
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.manhole_domestic_chamber_model import ManholeDomesticChamber
|
||||||
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
|
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
|
||||||
|
|
||||||
|
|
||||||
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
|
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"])
|
@generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
|
||||||
def comparison_report():
|
def comparison_report():
|
||||||
subcontractors = Subcontractor.query.all()
|
subcontractors = Subcontractor.query.all()
|
||||||
@@ -25,69 +34,38 @@ def comparison_report():
|
|||||||
|
|
||||||
if not subcontractor_id:
|
if not subcontractor_id:
|
||||||
flash("Please select a subcontractor.", "danger")
|
flash("Please select a subcontractor.", "danger")
|
||||||
return render_template(
|
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
||||||
"generate_comparison_report.html",
|
|
||||||
subcontractors=subcontractors
|
|
||||||
)
|
|
||||||
|
|
||||||
subcontractor = Subcontractor.query.get_or_404(subcontractor_id)
|
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(
|
# ================= TRENCH EXCAVATION =================
|
||||||
subcontractor_id=subcontractor_id
|
client_tr_ex = TrenchExcavationClient.query.all()
|
||||||
).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 = []
|
combined_rows_tr_ex = []
|
||||||
|
|
||||||
for row1, row2 in zip(client_tr_ex, contractor_tr_ex):
|
for row1 in client_tr_ex:
|
||||||
# -------- Tr.Ex CLIENT TOTAL ----------
|
row2 = contractor_lookup.get((row1.Location, row1.MH_NO))
|
||||||
client_total = (
|
if not row2:
|
||||||
(row1.Marshi_Muddy_Slushy_0_to_1_5_total or 0) +
|
continue
|
||||||
(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)
|
|
||||||
)
|
|
||||||
|
|
||||||
# -------- SUBCONTRACTOR TOTAL ----------
|
# -------- CLIENT TOTAL ----------
|
||||||
contractor_total = (
|
client_total = sum(v or 0 for k, v in row1.__dict__.items()
|
||||||
(row2.Soft_Murum_0_to_1_5_total or 0) +
|
if k.endswith("_total"))
|
||||||
(row2.Soft_Murum_1_5_to_3_0_total or 0) +
|
|
||||||
(row2.Soft_Murum_3_0_to_4_5_total or 0) +
|
# -------- SUB TOTAL ----------
|
||||||
(row2.Hard_Murum_0_to_1_5_total or 0) +
|
contractor_total = sum(v or 0 for k, v in row2.__dict__.items()
|
||||||
(row2.Hard_Murum_1_5_and_above_total or 0) +
|
if k.endswith("_total"))
|
||||||
(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)
|
|
||||||
)
|
|
||||||
|
|
||||||
diff = client_total - contractor_total
|
diff = client_total - contractor_total
|
||||||
|
|
||||||
# -------- COMBINED ROW ----------
|
|
||||||
row_data = {
|
row_data = {
|
||||||
"Location": row1.Location,
|
"Location": row1.Location,
|
||||||
"MH No": row1.MH_NO,
|
"MH No": row1.MH_NO,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Client columns
|
# -------- CLIENT COLUMNS ----------
|
||||||
for col, val in row1.__dict__.items():
|
for col, val in row1.__dict__.items():
|
||||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||||
continue
|
continue
|
||||||
@@ -96,7 +74,7 @@ def comparison_report():
|
|||||||
row_data["Client_Total"] = round(client_total, 2)
|
row_data["Client_Total"] = round(client_total, 2)
|
||||||
row_data[" - "] = " "
|
row_data[" - "] = " "
|
||||||
|
|
||||||
# Subcontractor columns
|
# -------- SUB COLUMNS ----------
|
||||||
for col, val in row2.__dict__.items():
|
for col, val in row2.__dict__.items():
|
||||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||||
continue
|
continue
|
||||||
@@ -106,77 +84,34 @@ def comparison_report():
|
|||||||
row_data["--"] = " "
|
row_data["--"] = " "
|
||||||
row_data["Diff"] = round(diff, 2)
|
row_data["Diff"] = round(diff, 2)
|
||||||
|
|
||||||
|
|
||||||
combined_rows_tr_ex.append(row_data)
|
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)
|
df_tr_ex = pd.DataFrame(combined_rows_tr_ex)
|
||||||
|
|
||||||
# --------------------------- Mh.Ex model -----------------------------
|
# ================= MANHOLE EXCAVATION =================
|
||||||
contractor_mh_ex = ManholeExcavation.query.filter_by(
|
client_mh_ex = ManholeExcavationClient.query.all()
|
||||||
subcontractor_id=subcontractor_id
|
contractor_mh_ex = ManholeExcavation.query.filter_by(subcontractor_id=subcontractor_id).all()
|
||||||
).all()
|
|
||||||
|
|
||||||
client_mh_ex = ManholeExcavationClient.query.filter_by(
|
|
||||||
subcontractor_id=subcontractor_id
|
|
||||||
).all()
|
|
||||||
|
|
||||||
|
contractor_lookup = make_lookup(contractor_mh_ex, "MH_NO")
|
||||||
combined_rows_mh_ex = []
|
combined_rows_mh_ex = []
|
||||||
|
|
||||||
for row1, row2 in zip(client_mh_ex, contractor_mh_ex):
|
for row1 in client_mh_ex:
|
||||||
# -------- Tr.Ex CLIENT TOTAL ----------
|
row2 = contractor_lookup.get((row1.Location, row1.MH_NO))
|
||||||
client_total = (
|
if not row2:
|
||||||
(row1.Marshi_Muddy_Slushy_0_to_1_5_total or 0) +
|
continue
|
||||||
(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)
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
||||||
# -------- SUBCONTRACTOR TOTAL ----------
|
client_total = sum(v or 0 for k, v in row1.__dict__.items()
|
||||||
contractor_total = (
|
if k.endswith("_total"))
|
||||||
(row2.Soft_Murum_0_to_1_5_total or 0) +
|
contractor_total = sum(v or 0 for k, v in row2.__dict__.items()
|
||||||
(row2.Soft_Murum_1_5_to_3_0_total or 0) +
|
if k.endswith("_total"))
|
||||||
(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)
|
|
||||||
)
|
|
||||||
|
|
||||||
diff = client_total - contractor_total
|
diff = client_total - contractor_total
|
||||||
|
|
||||||
# -------- COMBINED ROW ----------
|
|
||||||
row_data = {
|
row_data = {
|
||||||
"Location": row1.Location,
|
"Location": row1.Location,
|
||||||
"MH No": row1.MH_NO,
|
"MH No": row1.MH_NO,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Client columns
|
|
||||||
for col, val in row1.__dict__.items():
|
for col, val in row1.__dict__.items():
|
||||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||||
continue
|
continue
|
||||||
@@ -185,7 +120,6 @@ def comparison_report():
|
|||||||
row_data["Client_Total"] = round(client_total, 2)
|
row_data["Client_Total"] = round(client_total, 2)
|
||||||
row_data[" - "] = " "
|
row_data[" - "] = " "
|
||||||
|
|
||||||
# Subcontractor columns
|
|
||||||
for col, val in row2.__dict__.items():
|
for col, val in row2.__dict__.items():
|
||||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||||
continue
|
continue
|
||||||
@@ -197,74 +131,33 @@ def comparison_report():
|
|||||||
|
|
||||||
combined_rows_mh_ex.append(row_data)
|
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)
|
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_lookup = make_lookup(contractor_mh_dc, "Node_no")
|
||||||
contractor_mh_dc = ManholeDomesticChamber.query.filter_by(
|
|
||||||
subcontractor_id=subcontractor_id
|
|
||||||
).all()
|
|
||||||
|
|
||||||
client_mh_dc = ManholeDomesticChamberClient.query.filter_by(
|
|
||||||
subcontractor_id=subcontractor_id
|
|
||||||
).all()
|
|
||||||
|
|
||||||
combined_rows_mh_dc = []
|
combined_rows_mh_dc = []
|
||||||
|
|
||||||
for row1, row2 in zip(client_mh_dc, contractor_mh_dc):
|
for row1 in client_mh_dc:
|
||||||
client_total = (
|
row2 = contractor_lookup.get((row1.Location, row1.MH_NO))
|
||||||
(row1.d_0_to_1_5 or 0) +
|
if not row2:
|
||||||
(row1.d_1_5_to_2_0 or 0) +
|
continue
|
||||||
(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)
|
|
||||||
)
|
|
||||||
|
|
||||||
contractor_total = (
|
client_total = sum(v or 0 for k, v in row1.__dict__.items()
|
||||||
(row2.d_0_to_0_75 or 0) +
|
if isinstance(v, (int, float)))
|
||||||
(row2.d_0_76_to_1_05 or 0) +
|
contractor_total = sum(v or 0 for k, v in row2.__dict__.items()
|
||||||
(row2.d_1_06_to_1_65 or 0) +
|
if isinstance(v, (int, float)))
|
||||||
(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)
|
|
||||||
)
|
|
||||||
|
|
||||||
diff = client_total - contractor_total
|
diff = client_total - contractor_total
|
||||||
|
|
||||||
# -------- COMBINED ROW ----------
|
|
||||||
row_data = {
|
row_data = {
|
||||||
"Location": row1.Location,
|
"Location": row1.Location,
|
||||||
"Node No": row1.Node_No,
|
"Node No": row1.MH_NO,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Client columns
|
|
||||||
for col, val in row1.__dict__.items():
|
for col, val in row1.__dict__.items():
|
||||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||||
continue
|
continue
|
||||||
@@ -272,8 +165,7 @@ def comparison_report():
|
|||||||
|
|
||||||
row_data["Client_Total"] = round(client_total, 2)
|
row_data["Client_Total"] = round(client_total, 2)
|
||||||
row_data[" - "] = " "
|
row_data[" - "] = " "
|
||||||
|
|
||||||
# Subcontractor columns
|
|
||||||
for col, val in row2.__dict__.items():
|
for col, val in row2.__dict__.items():
|
||||||
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
if col.startswith("_") or col in ["id", "created_at", "subcontractor_id"]:
|
||||||
continue
|
continue
|
||||||
@@ -285,24 +177,17 @@ def comparison_report():
|
|||||||
|
|
||||||
combined_rows_mh_dc.append(row_data)
|
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)
|
df_mh_dc = pd.DataFrame(combined_rows_mh_dc)
|
||||||
|
|
||||||
|
|
||||||
|
# ================== EXCEL EXPORT ======================
|
||||||
output = io.BytesIO()
|
output = io.BytesIO()
|
||||||
file_name = f"{subcontractor.subcontractor_name}_Comparison_Report.xlsx"
|
file_name = f"{subcontractor.subcontractor_name}_Comparison_Report.xlsx"
|
||||||
|
|
||||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
||||||
df_tr_ex.to_excel(writer, index=False, sheet_name="Tr.Ex")
|
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_ex.to_excel(writer, index=False, sheet_name="Mh.Ex")
|
||||||
df_mh_dc.to_excel(writer,index=False,sheet_name="MH & DC")
|
df_mh_dc.to_excel(writer, index=False, sheet_name="MH & DC")
|
||||||
|
|
||||||
|
|
||||||
output.seek(0)
|
output.seek(0)
|
||||||
|
|
||||||
@@ -313,9 +198,4 @@ def comparison_report():
|
|||||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
)
|
)
|
||||||
|
|
||||||
return render_template(
|
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
||||||
"generate_comparison_report.html",
|
|
||||||
subcontractors=subcontractors
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user