update code of dash and report of cont_client and devlp abstract of qty

This commit is contained in:
2026-07-30 14:38:37 +05:30
parent 12b2032430
commit bf1df3a817
42 changed files with 3066 additions and 952 deletions

View File

@@ -6,14 +6,21 @@ import matplotlib.pyplot as plt
import io
import base64
from app.utils.plot_utils import plot_to_base64
from app.utils.helpers import login_required
from app.services.dashboard_service import DashboardService
from sqlalchemy import func
from app import db
# Subcontractor models import
from app.models.subcontractor_model import Subcontractor
from app.models.trench_excavation_model import TrenchExcavation
from app.models.manhole_excavation_model import ManholeExcavation
from app.models.manhole_domestic_chamber_model import ManholeDomesticChamber
from app.models.laying_model import Laying
from app.models.subcontractor_model import Subcontractor
# client models import
from app.models.tr_ex_client_model import TrenchExcavationClient
dashboard_bp = Blueprint("dashboard", __name__, url_prefix="/dashboard")
@@ -26,6 +33,7 @@ def dashboard():
@dashboard_bp.route("/api/live-stats")
@login_required
def live_stats():
try:
# 1. Overall Volume
@@ -64,6 +72,7 @@ def live_stats():
# subcontractor dashboard
@dashboard_bp.route("/subcontractor_dashboard")
@login_required
def subcontractor_dashboard():
if not session.get("user_id"):
@@ -76,8 +85,9 @@ def subcontractor_dashboard():
subcontractors=subcontractors
)
# API: Get Unique RA Bills
# API: Get Unique RA Bills
@dashboard_bp.route("/api/get-ra-bills")
@login_required
def get_ra_bills():
subcontractor_id = request.args.get("subcontractor")
@@ -96,149 +106,161 @@ def get_ra_bills():
).distinct().order_by(TrenchExcavation.RA_Bill_No).all()
# (Add others same pattern later)
case _:
results = []
case "manhole_excavation":
results = db.session.query(
ManholeExcavation.RA_Bill_No
).filter(
ManholeExcavation.subcontractor_id == subcontractor_id
).distinct().order_by(ManholeExcavation.RA_Bill_No).all()
case "Manhole_Domestic_Chamber":
results = db.session.query(
ManholeDomesticChamber.RA_Bill_No
).filter(
ManholeDomesticChamber.subcontractor_id == subcontractor_id
).distinct().order_by(ManholeDomesticChamber.RA_Bill_No).all()
case "Laying":
results = db.session.query(
Laying.RA_Bill_No
).filter(
Laying.subcontractor_id == subcontractor_id
).distinct().order_by(Laying.RA_Bill_No).all()
ra_bills = [r[0] for r in results if r[0]]
print(results)
return {"ra_bills": ra_bills}
# API FOR CHART DATA
# @dashboard_bp.route("/api/subcontractor-chart")
# def subcontractor_chart():
# subcontractor_id = request.args.get("subcontractor")
# category = request.args.get("category")
# ra_bill = request.args.get("ra_bill")
# labels = []
# values = []
# match category:
# # ✅ Trench
# case "trench_excavation":
# query = db.session.query(
# TrenchExcavation.excavation_category,
# func.sum(TrenchExcavation.Total)
# )
# if subcontractor_id:
# query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id)
# if ra_bill:
# query = query.filter(TrenchExcavation.RA_Bill_No == ra_bill)
# results = query.group_by(TrenchExcavation.excavation_category).all()
# # ✅ Manhole
# case "manhole_excavation":
# query = db.session.query(
# ManholeExcavation.excavation_category,
# func.sum(ManholeExcavation.Total)
# )
# if subcontractor_id:
# query = query.filter(ManholeExcavation.subcontractor_id == subcontractor_id)
# if ra_bill:
# query = query.filter(ManholeExcavation.RA_Bill_No == ra_bill)
# results = query.group_by(ManholeExcavation.excavation_category).all()
# case _:
# results = []
# for r in results:
# labels.append(r[0])
# values.append(float(r[1] or 0))
# return jsonify({
# "labels": labels,
# "values": values
# })
def total(records, field):
return float(sum(getattr(r, field) or 0 for r in records))
@dashboard_bp.route("/api/trench-analysis")
def trench_analysis():
subcontractor_id = request.args.get("subcontractor")
ra_bill = request.args.get("ra_bill")
subcontractor_id = request.args.get("subcontractor", "").strip()
ra_bill = request.args.get("ra_bill", "").strip()
query = TrenchExcavation.query
# Convert "1,2,3" -> ["1", "2", "3"]
ra_bill_list = []
if ra_bill:
ra_bill_list = [x.strip() for x in ra_bill.split(",") if x.strip()]
# Subcontractor Query
sub_query = TrenchExcavation.query
if subcontractor_id:
query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id)
if ra_bill:
query = query.filter(TrenchExcavation.RA_Bill_No == ra_bill)
data = query.all()
result = {
"Soft Murum": {"depth": 0, "qty": 0},
"Hard Murum": {"depth": 0, "qty": 0},
"Soft Rock": {"depth": 0, "qty": 0},
"Hard Rock": {"depth": 0, "qty": 0},
}
for r in data:
# Soft Murum
result["Soft Murum"]["depth"] += (
(r.Soft_Murum_0_to_1_5 or 0) +
(r.Soft_Murum_1_5_to_3_0 or 0) +
(r.Soft_Murum_3_0_to_4_5 or 0)
)
result["Soft Murum"]["qty"] += (
(r.Soft_Murum_0_to_1_5_total or 0) +
(r.Soft_Murum_1_5_to_3_0_total or 0) +
(r.Soft_Murum_3_0_to_4_5_total or 0)
sub_query = sub_query.filter(
TrenchExcavation.subcontractor_id == int(subcontractor_id)
)
# Hard Murum
result["Hard Murum"]["depth"] += (
(r.Hard_Murum_0_to_1_5 or 0) +
(r.Hard_Murum_1_5_to_3_0 or 0)
)
result["Hard Murum"]["qty"] += (
(r.Hard_Murum_0_to_1_5_total or 0) +
(r.Hard_Murum_1_5_and_above_total or 0)
if ra_bill_list:
sub_query = sub_query.filter(
TrenchExcavation.RA_Bill_No.in_(ra_bill_list)
)
# Soft Rock
result["Soft Rock"]["depth"] += (
(r.Soft_Rock_0_to_1_5 or 0) +
(r.Soft_Rock_1_5_to_3_0 or 0)
)
result["Soft Rock"]["qty"] += (
(r.Soft_Rock_0_to_1_5_total or 0) +
(r.Soft_Rock_1_5_and_above_total or 0)
tr_sub = [r.serialize() for r in sub_query.all()]
# Client Query
client_query = TrenchExcavationClient.query
if ra_bill_list:
client_query = client_query.filter(
TrenchExcavationClient.RA_Bill_No.in_(ra_bill_list)
)
# Hard Rock
result["Hard Rock"]["depth"] += (
(r.Hard_Rock_0_to_1_5 or 0) +
(r.Hard_Rock_1_5_to_3_0 or 0) +
(r.Hard_Rock_3_0_to_4_5 or 0) +
(r.Hard_Rock_4_5_to_6_0 or 0) +
(r.Hard_Rock_6_0_to_7_5 or 0)
)
result["Hard Rock"]["qty"] += (
(r.Hard_Rock_0_to_1_5_total or 0) +
(r.Hard_Rock_1_5_to_3_0_total or 0) +
(r.Hard_Rock_3_0_to_4_5_total or 0) +
(r.Hard_Rock_4_5_to_6_0_total or 0) +
(r.Hard_Rock_6_0_to_7_5_total or 0)
)
tr_client = [r.serialize() for r in client_query.all()]
labels = list(result.keys())
depth = [result[k]["depth"] for k in labels]
qty = [result[k]["qty"] for k in labels]
chart_data = [
{
"label": "Marshi 0 to 1.5",
"client": total(client_query.all(), "Marshi_Muddy_Slushy_0_to_1_5_total"),
"sub": 0
},
{
"label": "Marshi 1.5 to 3.0",
"client": total(client_query.all(), "Marshi_Muddy_Slushy_1_5_to_3_0_total"),
"sub": 0
},
{
"label": "Marshi 3.0-4.5",
"client": total(client_query.all(), "Marshi_Muddy_Slushy_3_0_to_4_5_total"),
"sub": 0
},
{
"label": "Soft Murum 0-1.5",
"client": total(client_query.all(), "Soft_Murum_0_to_1_5_total"),
"sub": total(sub_query.all(), "Soft_Murum_0_to_1_5_total")
},
{
"label": "Soft Murum 1.5-3.0",
"client": total(client_query.all(), "Soft_Murum_1_5_to_3_0_total"),
"sub": total(sub_query.all(), "Soft_Murum_1_5_to_3_0_total")
},
{
"label": "Soft Murum 3.0-4.5",
"client": total(client_query.all(), "Soft_Murum_3_0_to_4_5_total"),
"sub": total(sub_query.all(), "Soft_Murum_3_0_to_4_5_total")
},
{
"label": "Hard Murum 0-1.5",
"client": total(client_query.all(), "Hard_Murum_0_to_1_5_total"),
"sub": total(sub_query.all(), "Hard_Murum_0_to_1_5_total")
},
{
"label": "Hard Murum 1.5+",
"client": total(client_query.all(), "Hard_Murum_1_5_to_3_0_total"),
"sub": total(sub_query.all(), "Hard_Murum_1_5_and_above_total")
},
{
"label": "Soft Rock 0-1.5",
"client": total(client_query.all(), "Soft_Rock_0_to_1_5_total"),
"sub": total(sub_query.all(), "Soft_Rock_0_to_1_5_total")
},
{
"label": "Soft Rock 1.5+",
"client": total(client_query.all(), "Soft_Rock_1_5_to_3_0_total"),
"sub": total(sub_query.all(), "Soft_Rock_1_5_and_above_total")
},
{
"label": "Hard Rock 0-1.5",
"client": total(client_query.all(), "Hard_Rock_0_to_1_5_total"),
"sub": total(sub_query.all(), "Hard_Rock_0_to_1_5_total")
},
{
"label": "Hard Rock 1.5-3.0",
"client": total(client_query.all(), "Hard_Rock_1_5_to_3_0_total"),
"sub": total(sub_query.all(), "Hard_Rock_1_5_to_3_0_total")
},
{
"label": "Hard Rock 3.0-4.5",
"client": total(client_query.all(), "Hard_Rock_3_0_to_4_5_total"),
"sub": total(sub_query.all(), "Hard_Rock_3_0_to_4_5_total")
},
{
"label": "Hard Rock 4.5-6.0",
"client": total(client_query.all(), "Hard_Rock_4_5_to_6_0_total"),
"sub": total(sub_query.all(), "Hard_Rock_4_5_to_6_0_total")
},
{
"label": "Hard Rock 6.0-7.5",
"client": total(client_query.all(), "Hard_Rock_6_0_to_7_5_total"),
"sub": total(sub_query.all(), "Hard_Rock_6_0_to_7_5_total")
}
]
return jsonify({
"labels": labels,
"depth": depth,
"qty": qty
})
"labels": [x["label"] for x in chart_data],
"client_qty": [x["client"] for x in chart_data],
"sub_qty": [x["sub"] for x in chart_data]
})