client models and report commit

This commit is contained in:
2025-12-13 18:50:27 +05:30
parent b11a974869
commit 89328c9066
21 changed files with 645 additions and 17 deletions

View File

@@ -6,6 +6,8 @@ from app import db
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.tr_ex_client_model import TrenchExcavationClient
from app.models.mh_ex_client_model import ManholeExcavationClient
from app.utils.file_utils import ensure_upload_folder
@@ -42,17 +44,25 @@ class FileService:
print(df.head())
print("=============================\n")
# Trench Excavation save
# Trench Excavation save (subcontractor)
if file_type == "trench_excavation":
return self.process_trench_excavation(df, subcontractor_id)
# Manhole Excavation save
# Manhole Excavation save (subcontractor)
if file_type == "manhole_excavation":
return self.process_manhole_excavation(df, subcontractor_id)
# Manhole and Domestic Chamber Construction save
# Manhole and Domestic Chamber Construction save (subcontractor)
if file_type == "manhole_domestic_chamber":
return self.process_manhole_domestic_chamber(df, subcontractor_id)
# Trench Excavation save (client)
if file_type =="tr_ex_client":
return self.client_trench_excavation(df, subcontractor_id)
# Manhole Excavation save (client)
# if file_type =="mh_ex_client":
# return self.client_manhole_excavation(df, subcontractor_id)
return True, "File uploaded successfully."
@@ -61,7 +71,7 @@ class FileService:
return False, f"Processing failed: {e}"
# ---------------------- Sub contractor --------------------------
# Trench Excavation save method (TrenchExcavation model)
def process_trench_excavation(self, df, subcontractor_id):
df.columns = [str(c).strip() for c in df.columns]
@@ -105,7 +115,7 @@ class FileService:
db.session.rollback()
return False, f"Trench Excavation Save Failed: {e}"
# Manhole Excavation save method (ManholeExcavation model)
def process_manhole_excavation(self, df, subcontractor_id):
# Clean column names (strip whitespace)
@@ -201,6 +211,94 @@ class FileService:
# ---------------------- client ----------------------------------
# Trench Excavation save method (TrenchExcavationClient model)
def client_trench_excavation(self, df, subcontractor_id):
df.columns = [str(c).strip() for c in df.columns]
# If the sheet has merged cells -> forward fill Location
if "Location" in df.columns:
df["Location"] = df["Location"].ffill()
df = df.dropna(how="all") # REMOVE empty rows
# Identify missing location rows before insert
missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")]
if not missing_loc.empty:
return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}"
saved_count = 0
try:
for index, row in df.iterrows():
record_data = {}
# Insert only fields that exist in model
for col in df.columns:
if hasattr(TrenchExcavationClient, col):
value = row[col]
# Normalize empty values
if pd.isna(value) or str(value).strip() in ["", "-", "", "nan", "NaN"]:
value = None
record_data[col] = value
record = TrenchExcavationClient(
subcontractor_id=subcontractor_id,
**record_data
)
db.session.add(record)
saved_count += 1
db.session.commit()
return True, f"Clinnt Tr Ex data saved successfully. Total rows: {saved_count}"
except Exception as e:
db.session.rollback()
return False, f"Clinnt Tr Ex Save Failed: {e}"
# Manhole Excavation save method (ManholeExcavationClient model)
# def client_manhole_excavation(self, df, subcontractor_id):
# # Clean column names (strip whitespace)
# df.columns = [str(c).strip() for c in df.columns]
# # If the sheet has merged cells -> forward fill Location
# if "Location" in df.columns:
# df["Location"] = df["Location"].ffill()
# # REMOVE empty rows
# df = df.dropna(how="all")
# # Identify missing location rows before insert
# missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")]
# if not missing_loc.empty:
# return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}"
# saved_count = 0
# try:
# for index, row in df.iterrows():
# record_data = {}
# # Insert only fields that exist in model
# for col in df.columns:
# if hasattr(ManholeExcavationClient, col):
# value = row[col]
# # Normalize empty values
# if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
# value = None
# record_data[col] = value
# record = ManholeExcavationClient(
# subcontractor_id=subcontractor_id,
# **record_data
# )
# db.session.add(record)
# saved_count += 1
# db.session.commit()
# return True, f" Client Mh Ex. data saved successfully. Total rows: {saved_count}"
# except Exception as e:
# db.session.rollback()
# return False, f"Client Mh Ex. Save Failed: {e}"