same changes of sub-cont dashboard
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
# project base url
|
|
||||||
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
# secret key
|
# secret key
|
||||||
@@ -23,7 +21,4 @@ class Config:
|
|||||||
)
|
)
|
||||||
|
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
# uploads folder path
|
|
||||||
UPLOAD_FOLDER = os.path.join(BASE_DIR, "static", "uploads")
|
|
||||||
# file extension
|
|
||||||
ALLOWED_EXTENSIONS = {"xlsx", "xls", "csv"}
|
|
||||||
@@ -76,39 +76,171 @@ def subcontractor_dashboard():
|
|||||||
subcontractors=subcontractors
|
subcontractors=subcontractors
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# ✅ API: Get Unique RA Bills
|
||||||
# API FOR CHART DATA
|
@dashboard_bp.route("/api/get-ra-bills")
|
||||||
@dashboard_bp.route("/api/subcontractor-chart")
|
def get_ra_bills():
|
||||||
def subcontractor_chart():
|
|
||||||
|
|
||||||
subcontractor_id = request.args.get("subcontractor")
|
subcontractor_id = request.args.get("subcontractor")
|
||||||
category = request.args.get("category")
|
category = request.args.get("category")
|
||||||
|
|
||||||
|
if not subcontractor_id or not category:
|
||||||
|
return {"ra_bills": []}
|
||||||
|
|
||||||
|
match category:
|
||||||
|
|
||||||
|
case "trench_excavation":
|
||||||
|
results = db.session.query(
|
||||||
|
TrenchExcavation.RA_Bill_No
|
||||||
|
).filter(
|
||||||
|
TrenchExcavation.subcontractor_id == subcontractor_id
|
||||||
|
).distinct().order_by(TrenchExcavation.RA_Bill_No).all()
|
||||||
|
|
||||||
|
# (Add others same pattern later)
|
||||||
|
case _:
|
||||||
|
results = []
|
||||||
|
|
||||||
|
ra_bills = [r[0] for r in results if r[0]]
|
||||||
|
|
||||||
|
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
|
||||||
|
# })
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@dashboard_bp.route("/api/trench-analysis")
|
||||||
|
def trench_analysis():
|
||||||
|
|
||||||
|
subcontractor_id = request.args.get("subcontractor")
|
||||||
ra_bill = request.args.get("ra_bill")
|
ra_bill = request.args.get("ra_bill")
|
||||||
|
|
||||||
query = db.session.query(
|
query = TrenchExcavation.query
|
||||||
TrenchExcavation.excavation_category,
|
|
||||||
func.sum(TrenchExcavation.Total)
|
|
||||||
)
|
|
||||||
|
|
||||||
if subcontractor_id:
|
if subcontractor_id:
|
||||||
query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id)
|
query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id)
|
||||||
|
|
||||||
if category:
|
|
||||||
query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id)
|
|
||||||
|
|
||||||
if ra_bill:
|
if ra_bill:
|
||||||
query = query.filter(TrenchExcavation.RA_Bill_No == ra_bill)
|
query = query.filter(TrenchExcavation.RA_Bill_No == ra_bill)
|
||||||
|
|
||||||
results = query.group_by(TrenchExcavation.excavation_category).all()
|
data = query.all()
|
||||||
|
|
||||||
labels = []
|
result = {
|
||||||
values = []
|
"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 results:
|
for r in data:
|
||||||
labels.append(r[0])
|
|
||||||
values.append(float(r[1] or 0))
|
# 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)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
)
|
||||||
|
|
||||||
|
labels = list(result.keys())
|
||||||
|
depth = [result[k]["depth"] for k in labels]
|
||||||
|
qty = [result[k]["qty"] for k in labels]
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"labels": labels,
|
"labels": labels,
|
||||||
"values": values
|
"depth": depth,
|
||||||
|
"qty": qty
|
||||||
})
|
})
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
|
from app import db
|
||||||
import os
|
import os
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
from app.utils.file_utils import ensure_upload_folder
|
from app.utils.file_utils import ensure_upload_folder
|
||||||
|
from app.utils.file_utils import get_uploads_folder
|
||||||
from app.config import Config
|
from app.utils.file_utils import ALLOWED_EXTENSIONS
|
||||||
from app import db
|
|
||||||
|
|
||||||
# Subcontractor models import
|
# Subcontractor models import
|
||||||
from app.models.trench_excavation_model import TrenchExcavation
|
from app.models.trench_excavation_model import TrenchExcavation
|
||||||
@@ -24,7 +24,7 @@ class FileService:
|
|||||||
|
|
||||||
# ---------------- COMMON HELPERS ----------------
|
# ---------------- COMMON HELPERS ----------------
|
||||||
def allowed_file(self, filename):
|
def allowed_file(self, filename):
|
||||||
return ("." in filename and filename.rsplit(".", 1)[1].lower() in Config.ALLOWED_EXTENSIONS)
|
return ("." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS)
|
||||||
|
|
||||||
def normalize(self, val):
|
def normalize(self, val):
|
||||||
if val is None or pd.isna(val):
|
if val is None or pd.isna(val):
|
||||||
@@ -52,8 +52,9 @@ class FileService:
|
|||||||
return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
||||||
|
|
||||||
ensure_upload_folder()
|
ensure_upload_folder()
|
||||||
|
path = get_uploads_folder()
|
||||||
|
|
||||||
folder = os.path.join(Config.UPLOAD_FOLDER, f"sub_{subcontractor_id}")
|
folder = os.path.join(path, f"sub_{subcontractor_id}")
|
||||||
os.makedirs(folder, exist_ok=True)
|
os.makedirs(folder, exist_ok=True)
|
||||||
|
|
||||||
filename = secure_filename(file.filename)
|
filename = secure_filename(file.filename)
|
||||||
@@ -310,7 +311,6 @@ class FileService:
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------- CLIENT FILE UPLOAD ----------------
|
# ---------------- CLIENT FILE UPLOAD ----------------
|
||||||
def handle_client_file_upload(self, file, RA_Bill_No):
|
def handle_client_file_upload(self, file, RA_Bill_No):
|
||||||
|
|
||||||
@@ -324,8 +324,9 @@ class FileService:
|
|||||||
return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
||||||
|
|
||||||
ensure_upload_folder()
|
ensure_upload_folder()
|
||||||
|
path = get_uploads_folder()
|
||||||
|
|
||||||
folder = os.path.join(Config.UPLOAD_FOLDER, f"Client_Bill_{RA_Bill_No}")
|
folder = os.path.join(path, f"Client_Bill_{RA_Bill_No}")
|
||||||
os.makedirs(folder, exist_ok=True)
|
os.makedirs(folder, exist_ok=True)
|
||||||
|
|
||||||
filename = secure_filename(file.filename)
|
filename = secure_filename(file.filename)
|
||||||
|
|||||||
@@ -1,33 +1,31 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<div class="container-fluid mt-3">
|
<div class="container mt-3">
|
||||||
|
|
||||||
<h4 class="mb-4">Subcontractor Dashboard</h4>
|
<h4>RA Bill Dashboard</h4>
|
||||||
|
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
|
|
||||||
|
<!-- Contractor -->
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<select id="subcontractor" class="form-control">
|
<select id="subcontractor" class="form-control">
|
||||||
<option value="">Select Subcontractor</option>
|
<option value="">Select Contractor</option>
|
||||||
|
|
||||||
{% for s in subcontractors %}
|
{% for s in subcontractors %}
|
||||||
<option value="{{s.id}}">{{s.subcontractor_name}}</option>
|
<option value="{{s.id}}">{{s.subcontractor_name}}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Category -->
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<select id="category" class="form-control">
|
<select id="category" class="form-control">
|
||||||
<option value="">Select Category</option>
|
<option value="">Select Category</option>
|
||||||
<option value="Soft Murum">Soft Murum</option>
|
<option value="trench_excavation">Trench Excavation</option>
|
||||||
<option value="Hard Murum">Hard Murum</option>
|
|
||||||
<option value="Soft Rock">Soft Rock</option>
|
|
||||||
<option value="Hard Rock">Hard Rock</option>
|
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- RA Bill -->
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<select id="ra_bill" class="form-control">
|
<select id="ra_bill" class="form-control">
|
||||||
<option value="">RA Bill</option>
|
<option value="">RA Bill</option>
|
||||||
@@ -36,70 +34,85 @@
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
<div class="card shadow">
|
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
<canvas id="comparisonChart"></canvas>
|
||||||
<canvas id="comparisonChart" height="120"></canvas>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
let chart;
|
let chart;
|
||||||
|
|
||||||
function loadChart() {
|
// ✅ Load RA Bills
|
||||||
|
function loadRABills() {
|
||||||
|
|
||||||
let subcontractor = document.getElementById("subcontractor").value
|
let subcontractor = document.getElementById("subcontractor").value
|
||||||
let category = document.getElementById("category").value
|
let category = document.getElementById("category").value
|
||||||
let ra_bill = document.getElementById("ra_bill").value
|
|
||||||
|
|
||||||
|
if (!subcontractor || !category) return
|
||||||
|
|
||||||
fetch(`/dashboard/api/subcontractor-chart?subcontractor=${subcontractor}&category=${category}&ra_bill=${ra_bill}`)
|
fetch(`/dashboard/api/get-ra-bills?subcontractor=${subcontractor}&category=${category}`)
|
||||||
|
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
|
|
||||||
if (chart) {
|
let ra = document.getElementById("ra_bill")
|
||||||
chart.destroy()
|
ra.innerHTML = '<option value="">RA Bill</option>'
|
||||||
|
|
||||||
|
data.ra_bills.forEach(bill => {
|
||||||
|
ra.innerHTML += `<option value="${bill}">${bill}</option>`
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctx = document.getElementById("comparisonChart")
|
// ✅ Load Chart
|
||||||
|
function loadChart() {
|
||||||
|
|
||||||
chart = new Chart(ctx, {
|
let subcontractor = document.getElementById("subcontractor").value
|
||||||
|
let ra_bill = document.getElementById("ra_bill").value
|
||||||
|
|
||||||
|
fetch(`/dashboard/api/trench-analysis?subcontractor=${subcontractor}&ra_bill=${ra_bill}`)
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => {
|
||||||
|
|
||||||
|
if (chart) chart.destroy()
|
||||||
|
|
||||||
|
chart = new Chart(document.getElementById("comparisonChart"), {
|
||||||
type: "bar",
|
type: "bar",
|
||||||
data: {
|
data: {
|
||||||
labels: data.labels,
|
labels: data.labels,
|
||||||
datasets: [{
|
datasets: [
|
||||||
label: "Subcontractor Quantity",
|
{
|
||||||
data: data.values,
|
label: "Depth",
|
||||||
backgroundColor: "#0d6efd"
|
data: data.depth,
|
||||||
}]
|
backgroundColor: "green"
|
||||||
},
|
},
|
||||||
options: {
|
{
|
||||||
responsive: true,
|
label: "Excavation Qty (cum)",
|
||||||
plugins: {
|
data: data.qty,
|
||||||
legend: { display: true }
|
backgroundColor: "blue"
|
||||||
}
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById("subcontractor").addEventListener("change", loadChart)
|
// Events
|
||||||
document.getElementById("category").addEventListener("change", loadChart)
|
document.getElementById("subcontractor").addEventListener("change", () => {
|
||||||
|
loadRABills()
|
||||||
|
})
|
||||||
|
|
||||||
|
document.getElementById("category").addEventListener("change", () => {
|
||||||
|
loadRABills()
|
||||||
|
})
|
||||||
|
|
||||||
document.getElementById("ra_bill").addEventListener("change", loadChart)
|
document.getElementById("ra_bill").addEventListener("change", loadChart)
|
||||||
|
|
||||||
loadChart()
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from app.config import Config
|
|
||||||
|
|
||||||
|
# file extension
|
||||||
|
ALLOWED_EXTENSIONS = {"xlsx", "xls", "csv"}
|
||||||
|
|
||||||
|
|
||||||
def get_download_format_folder():
|
def get_download_format_folder():
|
||||||
@@ -11,6 +14,13 @@ def get_download_format_folder():
|
|||||||
"format"
|
"format"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_uploads_folder():
|
||||||
|
return os.path.join(
|
||||||
|
current_app.root_path,
|
||||||
|
"static",
|
||||||
|
"uploads"
|
||||||
|
)
|
||||||
|
|
||||||
def ensure_upload_folder():
|
def ensure_upload_folder():
|
||||||
if not os.path.exists(Config.UPLOAD_FOLDER):
|
if not os.path.exists(get_uploads_folder()):
|
||||||
os.makedirs(Config.UPLOAD_FOLDER)
|
os.makedirs(get_uploads_folder())
|
||||||
Reference in New Issue
Block a user