2025-12-11 10:16:43 +05:30
|
|
|
import os
|
|
|
|
|
import pandas as pd
|
|
|
|
|
from werkzeug.utils import secure_filename
|
2025-12-23 12:20:38 +05:30
|
|
|
|
2025-12-11 10:16:43 +05:30
|
|
|
from app.config import Config
|
|
|
|
|
from app import db
|
2025-12-23 12:20:38 +05:30
|
|
|
|
2025-12-11 10:16:43 +05:30
|
|
|
from app.models.trench_excavation_model import TrenchExcavation
|
2025-12-11 17:58:56 +05:30
|
|
|
from app.models.manhole_excavation_model import ManholeExcavation
|
|
|
|
|
from app.models.manhole_domestic_chamber_model import ManholeDomesticChamber
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-13 18:50:27 +05:30
|
|
|
from app.models.tr_ex_client_model import TrenchExcavationClient
|
|
|
|
|
from app.models.mh_ex_client_model import ManholeExcavationClient
|
2025-12-14 18:04:03 +05:30
|
|
|
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
|
|
|
|
|
|
2025-12-11 10:16:43 +05:30
|
|
|
from app.utils.file_utils import ensure_upload_folder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileService:
|
|
|
|
|
|
|
|
|
|
def allowed_file(self, filename):
|
2025-12-23 12:20:38 +05:30
|
|
|
return (
|
|
|
|
|
"." in filename
|
|
|
|
|
and filename.rsplit(".", 1)[1].lower() in Config.ALLOWED_EXTENSIONS
|
|
|
|
|
)
|
2025-12-11 10:16:43 +05:30
|
|
|
|
2025-12-23 15:02:04 +05:30
|
|
|
# -------------------------- SUBCONTRACTORFILE UPLOAD -------------------------
|
2025-12-23 12:20:38 +05:30
|
|
|
# SUBCONTRACTOR FILE UPLOAD
|
2025-12-19 17:53:45 +05:30
|
|
|
def handle_file_upload(self, file, subcontractor_id, RA_Bill_No):
|
2025-12-11 10:16:43 +05:30
|
|
|
|
|
|
|
|
if not subcontractor_id:
|
|
|
|
|
return False, "Please select subcontractor."
|
2025-12-23 12:20:38 +05:30
|
|
|
|
2025-12-20 12:39:16 +05:30
|
|
|
if not RA_Bill_No:
|
2025-12-23 12:20:38 +05:30
|
|
|
return False, "Please Enter RA Bill No."
|
|
|
|
|
|
2025-12-11 10:16:43 +05:30
|
|
|
if not file or file.filename == "":
|
|
|
|
|
return False, "No file selected."
|
2025-12-23 12:20:38 +05:30
|
|
|
|
2025-12-11 10:16:43 +05:30
|
|
|
if not self.allowed_file(file.filename):
|
|
|
|
|
return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
|
|
|
|
|
|
|
|
|
ensure_upload_folder()
|
|
|
|
|
|
|
|
|
|
folder = os.path.join(Config.UPLOAD_FOLDER, f"sub_{subcontractor_id}")
|
|
|
|
|
os.makedirs(folder, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
|
filepath = os.path.join(folder, filename)
|
|
|
|
|
file.save(filepath)
|
|
|
|
|
|
|
|
|
|
try:
|
2025-12-23 12:20:38 +05:30
|
|
|
df_tr_ex = pd.read_excel(filepath, sheet_name="Tr.Ex.", header=12)
|
2025-12-18 18:29:25 +05:30
|
|
|
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12)
|
2025-12-19 17:53:45 +05:30
|
|
|
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11)
|
2025-12-18 18:29:25 +05:30
|
|
|
|
|
|
|
|
self.process_trench_excavation(df_tr_ex, subcontractor_id, RA_Bill_No)
|
2025-12-19 17:53:45 +05:30
|
|
|
self.process_manhole_excavation(df_mh_ex, subcontractor_id, RA_Bill_No)
|
|
|
|
|
self.process_manhole_domestic_chamber(df_mh_dc, subcontractor_id, RA_Bill_No)
|
2025-12-18 18:29:25 +05:30
|
|
|
|
2025-12-23 15:02:04 +05:30
|
|
|
return True, "SUBCONTRACTOR File uploaded successfully."
|
2025-12-11 10:16:43 +05:30
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-12-23 12:20:38 +05:30
|
|
|
db.session.rollback()
|
2025-12-23 15:02:04 +05:30
|
|
|
return False, f"Processing failed(SUBCONTRACTOR): {e}"
|
2025-12-18 18:29:25 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
# Trench Excavation (Subcontractor)
|
|
|
|
|
def process_trench_excavation(self, df, subcontractor_id, RA_Bill_No):
|
2025-12-18 18:29:25 +05:30
|
|
|
|
|
|
|
|
df.columns = (
|
|
|
|
|
df.columns.astype(str)
|
|
|
|
|
.str.strip()
|
|
|
|
|
.str.replace(r"[^\w]", "_", regex=True)
|
|
|
|
|
.str.replace("__+", "_", regex=True)
|
|
|
|
|
.str.strip("_")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
df = df.dropna(how="all")
|
|
|
|
|
|
2025-12-11 10:16:43 +05:30
|
|
|
if "Location" in df.columns:
|
|
|
|
|
df["Location"] = df["Location"].ffill()
|
|
|
|
|
|
|
|
|
|
try:
|
2025-12-23 12:20:38 +05:30
|
|
|
for _, row in df.iterrows():
|
2025-12-18 18:29:25 +05:30
|
|
|
location = row.get("Location")
|
|
|
|
|
mh_no = row.get("MH_NO")
|
|
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
if (
|
|
|
|
|
pd.isna(location)
|
|
|
|
|
or str(location).strip() == ""
|
|
|
|
|
or pd.isna(mh_no)
|
|
|
|
|
or str(mh_no).strip() == ""
|
|
|
|
|
):
|
2025-12-18 18:29:25 +05:30
|
|
|
continue
|
|
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
record_data = {}
|
2025-12-11 10:16:43 +05:30
|
|
|
for col in df.columns:
|
|
|
|
|
if hasattr(TrenchExcavation, col):
|
2025-12-23 12:20:38 +05:30
|
|
|
val = row[col]
|
|
|
|
|
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
|
|
|
|
val = None
|
|
|
|
|
record_data[col] = val
|
2025-12-11 10:16:43 +05:30
|
|
|
|
|
|
|
|
record = TrenchExcavation(
|
2025-12-23 12:20:38 +05:30
|
|
|
subcontractor_id=subcontractor_id,
|
|
|
|
|
RA_Bill_No=RA_Bill_No,
|
|
|
|
|
**record_data,
|
2025-12-11 10:16:43 +05:30
|
|
|
)
|
|
|
|
|
db.session.add(record)
|
|
|
|
|
|
|
|
|
|
db.session.commit()
|
2025-12-23 12:20:38 +05:30
|
|
|
return True, "Trench Excavation saved successfully."
|
2025-12-11 10:16:43 +05:30
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
db.session.rollback()
|
2025-12-23 12:20:38 +05:30
|
|
|
raise e
|
2025-12-11 17:58:56 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
# Manhole Excavation (Subcontractor)
|
2025-12-18 18:29:25 +05:30
|
|
|
def process_manhole_excavation(self, df, subcontractor_id, RA_Bill_No):
|
2025-12-11 17:58:56 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
df.columns = (
|
|
|
|
|
df.columns.astype(str)
|
|
|
|
|
.str.strip()
|
|
|
|
|
.str.replace(r"[^\w]", "_", regex=True)
|
|
|
|
|
.str.replace("__+", "_", regex=True)
|
|
|
|
|
.str.strip("_")
|
|
|
|
|
)
|
2025-12-18 18:29:25 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
df = df.dropna(how="all")
|
2025-12-18 18:29:25 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
if "Location" in df.columns:
|
|
|
|
|
df["Location"] = df["Location"].ffill()
|
2025-12-18 18:29:25 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
try:
|
|
|
|
|
for _, row in df.iterrows():
|
|
|
|
|
location = row.get("Location")
|
|
|
|
|
mh_no = row.get("MH_NO")
|
2025-12-18 18:29:25 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
if (
|
|
|
|
|
pd.isna(location)
|
|
|
|
|
or str(location).strip() == ""
|
|
|
|
|
or pd.isna(mh_no)
|
|
|
|
|
or str(mh_no).strip() == ""
|
|
|
|
|
):
|
|
|
|
|
continue
|
2025-12-18 18:29:25 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
record_data = {}
|
|
|
|
|
for col in df.columns:
|
|
|
|
|
if hasattr(ManholeExcavation, col):
|
|
|
|
|
val = row[col]
|
|
|
|
|
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
|
|
|
|
val = None
|
|
|
|
|
record_data[col] = val
|
|
|
|
|
|
|
|
|
|
record = ManholeExcavation(
|
|
|
|
|
subcontractor_id=subcontractor_id,
|
|
|
|
|
RA_Bill_No=RA_Bill_No,
|
|
|
|
|
**record_data,
|
2025-12-11 17:58:56 +05:30
|
|
|
)
|
2025-12-23 12:20:38 +05:30
|
|
|
db.session.add(record)
|
2025-12-11 17:58:56 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
db.session.commit()
|
|
|
|
|
return True, "Manhole Excavation saved successfully."
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
except Exception as e:
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
raise e
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 15:02:04 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
# Manhole & Domestic Chamber (Subcontractor)
|
|
|
|
|
def process_manhole_domestic_chamber(self, df, subcontractor_id, RA_Bill_No):
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
df.columns = (
|
|
|
|
|
df.columns.astype(str)
|
|
|
|
|
.str.strip()
|
|
|
|
|
.str.replace(r"[^\w]", "_", regex=True)
|
|
|
|
|
.str.replace("__+", "_", regex=True)
|
|
|
|
|
.str.strip("_")
|
|
|
|
|
)
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
df = df.dropna(how="all")
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
if "Location" in df.columns:
|
|
|
|
|
df["Location"] = df["Location"].ffill()
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
try:
|
|
|
|
|
for _, row in df.iterrows():
|
|
|
|
|
location = row.get("Location")
|
|
|
|
|
node_no = row.get("Node_No")
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
pd.isna(location)
|
|
|
|
|
or str(location).strip() == ""
|
|
|
|
|
or pd.isna(node_no)
|
|
|
|
|
or str(node_no).strip() == ""
|
|
|
|
|
):
|
|
|
|
|
continue
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
record_data = {}
|
|
|
|
|
for col in df.columns:
|
|
|
|
|
if hasattr(ManholeDomesticChamber, col):
|
|
|
|
|
val = row[col]
|
|
|
|
|
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan"]:
|
|
|
|
|
val = None
|
|
|
|
|
record_data[col] = val
|
|
|
|
|
|
|
|
|
|
record = ManholeDomesticChamber(
|
|
|
|
|
subcontractor_id=subcontractor_id,
|
|
|
|
|
RA_Bill_No=RA_Bill_No,
|
|
|
|
|
**record_data,
|
2025-12-19 17:53:45 +05:30
|
|
|
)
|
2025-12-23 12:20:38 +05:30
|
|
|
db.session.add(record)
|
2025-12-19 17:53:45 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
db.session.commit()
|
|
|
|
|
return True, "Manhole Domestic Chamber saved successfully."
|
2025-12-20 16:35:35 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
except Exception as e:
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
raise e
|
2025-12-11 17:58:56 +05:30
|
|
|
|
2025-12-23 15:02:04 +05:30
|
|
|
|
|
|
|
|
# ------------------- CLIENT FILE UPLOAD --------------------------
|
2025-12-20 16:35:35 +05:30
|
|
|
def handle_client_file_upload(self, file, RA_Bill_No):
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-20 16:35:35 +05:30
|
|
|
if not RA_Bill_No:
|
|
|
|
|
return False, "Please Enter RA Bill No."
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-20 16:35:35 +05:30
|
|
|
if not file or file.filename == "":
|
|
|
|
|
return False, "No file selected."
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-20 16:35:35 +05:30
|
|
|
if not self.allowed_file(file.filename):
|
|
|
|
|
return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-20 16:35:35 +05:30
|
|
|
ensure_upload_folder()
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-20 16:35:35 +05:30
|
|
|
folder = os.path.join(Config.UPLOAD_FOLDER, f"Client_Bill_{RA_Bill_No}")
|
|
|
|
|
os.makedirs(folder, exist_ok=True)
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-20 16:35:35 +05:30
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
|
filepath = os.path.join(folder, filename)
|
|
|
|
|
file.save(filepath)
|
2025-12-14 18:04:03 +05:30
|
|
|
|
2025-12-20 16:35:35 +05:30
|
|
|
try:
|
2025-12-23 12:20:38 +05:30
|
|
|
df_tr_ex = pd.read_excel(filepath, sheet_name="Tr.Ex.", header=4)
|
|
|
|
|
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=4)
|
|
|
|
|
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=3)
|
|
|
|
|
|
|
|
|
|
print("------------------")
|
|
|
|
|
print("df_tr_ex :",df_tr_ex)
|
|
|
|
|
print("------------------")
|
|
|
|
|
print("df_mh_ex :",df_mh_ex)
|
|
|
|
|
print("------------------")
|
|
|
|
|
print("df_mh_dc :",df_mh_dc)
|
|
|
|
|
print("------------------")
|
|
|
|
|
|
2025-12-23 15:02:04 +05:30
|
|
|
self.save_client_data(df_tr_ex, TrenchExcavationClient, RA_Bill_No)
|
|
|
|
|
self.save_client_data(df_mh_ex, ManholeExcavationClient, RA_Bill_No)
|
|
|
|
|
self.save_client_data(df_mh_dc, ManholeDomesticChamberClient, RA_Bill_No)
|
2025-12-14 18:04:03 +05:30
|
|
|
|
|
|
|
|
db.session.commit()
|
2025-12-20 16:35:35 +05:30
|
|
|
return True, "Client file uploaded and all data saved successfully."
|
|
|
|
|
|
2025-12-14 18:04:03 +05:30
|
|
|
except Exception as e:
|
|
|
|
|
db.session.rollback()
|
2025-12-23 12:20:38 +05:30
|
|
|
return False, f"Client data save failed: {e}"
|
2025-12-13 18:50:27 +05:30
|
|
|
|
2025-12-23 15:02:04 +05:30
|
|
|
# Client all model Save Method
|
|
|
|
|
def save_client_data(self, df, model, RA_Bill_No):
|
2025-12-13 18:50:27 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
df.columns = [str(c).strip() for c in df.columns]
|
2025-12-20 16:35:35 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
if "Location" in df.columns:
|
|
|
|
|
df["Location"] = df["Location"].ffill()
|
2025-12-13 18:50:27 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
df = df.dropna(how="all")
|
2025-12-13 18:50:27 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
if "Location" in df.columns:
|
|
|
|
|
missing_loc = df[
|
|
|
|
|
df["Location"].isna()
|
|
|
|
|
| (df["Location"].astype(str).str.strip() == "")
|
|
|
|
|
]
|
|
|
|
|
if not missing_loc.empty:
|
|
|
|
|
raise Exception(
|
|
|
|
|
f"{model.__name__}: Empty Location at rows {missing_loc.index.tolist()}"
|
2025-12-14 18:04:03 +05:30
|
|
|
)
|
2025-12-13 18:50:27 +05:30
|
|
|
|
2025-12-23 12:20:38 +05:30
|
|
|
for _, row in df.iterrows():
|
|
|
|
|
record_data = {}
|
|
|
|
|
|
|
|
|
|
for col in df.columns:
|
|
|
|
|
if hasattr(model, col):
|
|
|
|
|
val = row[col]
|
|
|
|
|
if pd.isna(val) or str(val).strip() in ["", "-", "—", "nan", "NaN"]:
|
|
|
|
|
val = None
|
|
|
|
|
record_data[col] = val
|
|
|
|
|
|
|
|
|
|
record = model(RA_Bill_No=RA_Bill_No, **record_data)
|
|
|
|
|
db.session.add(record)
|