import modify subcontractor sheet Laying model import added

This commit is contained in:
2026-01-15 12:27:29 +05:30
parent e0541ff80c
commit aad9b2b967
2 changed files with 113 additions and 1 deletions

View File

@@ -1,19 +1,22 @@
import os
import pandas as pd
from werkzeug.utils import secure_filename
from app.utils.file_utils import ensure_upload_folder
from app.config import Config
from app import db
# Subcontractor models import
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
# Client models import
from app.models.tr_ex_client_model import TrenchExcavationClient
from app.models.mh_ex_client_model import ManholeExcavationClient
from app.models.mh_dc_client_model import ManholeDomesticChamberClient
from app.utils.file_utils import ensure_upload_folder
class FileService:
@@ -60,10 +63,12 @@ class FileService:
df_tr_ex = pd.read_excel(filepath, sheet_name="Tr.Ex.", header=12, dtype={"MH No": str})
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12, dtype={"MH No": str})
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11, dtype={"MH No": str})
df_laying = pd.read_excel(filepath, sheet_name="Laying", header=11, dtype={"MH No": str})
self.process_trench_excavation(df_tr_ex, subcontractor_id, RA_Bill_No)
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)
self.process_laying(df_laying, subcontractor_id, RA_Bill_No)
return True, "SUBCONTRACTOR File uploaded successfully."
@@ -245,6 +250,66 @@ class FileService:
db.session.commit()
# ---------------- Laying (Subcontractor) ----------------
def process_laying(self, df, subcontractor_id, RA_Bill_No):
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")
if "Location" in df.columns:
df["Location"] = df["Location"].ffill()
errors = []
for idx, row in df.iterrows():
location = self.normalize(row.get("Location"))
mh_no = self.normalize(row.get("MH_NO"))
if not location or not mh_no:
continue
# exists = ManholeDomesticChamber.query.filter_by(
# subcontractor_id=subcontractor_id,
# RA_Bill_No=RA_Bill_No,
# Location=location,
# MH_NO=mh_no,
# ).first()
# if exists:
# errors.append(
# f"Model-MH & DC (Row {idx+1}): Duplicate → Location={location}, MH_NO={mh_no}"
# )
# continue
record_data = {}
for col in df.columns:
if hasattr(Laying, col):
val = row[col]
if pd.isna(val) or str(val).strip() in ["", "-", "", "nan"]:
val = None
record_data[col] = val
record = Laying(
subcontractor_id=subcontractor_id,
RA_Bill_No=RA_Bill_No,
**record_data,
)
db.session.add(record)
if errors:
raise Exception(" | ".join(errors))
db.session.commit()
# ---------------- CLIENT FILE UPLOAD ----------------
def handle_client_file_upload(self, file, RA_Bill_No):