Compare commits
2 Commits
5284fa9e11
...
a7de81be73
| Author | SHA1 | Date | |
|---|---|---|---|
| a7de81be73 | |||
| 47df8a2668 |
@@ -11,7 +11,7 @@ class ManholeDomesticChamber(db.Model):
|
|||||||
subcontractor = db.relationship("Subcontractor", backref="manhole_domestic_chamber_records")
|
subcontractor = db.relationship("Subcontractor", backref="manhole_domestic_chamber_records")
|
||||||
|
|
||||||
# Basic Fields
|
# Basic Fields
|
||||||
Location = db.Column(db.String(255))
|
Location = db.Column(db.String(500))
|
||||||
Node_No = db.Column(db.String(100))
|
Node_No = db.Column(db.String(100))
|
||||||
Depth_of_MH = db.Column(db.Float)
|
Depth_of_MH = db.Column(db.Float)
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class ManholeExcavation(db.Model):
|
|||||||
subcontractor = db.relationship("Subcontractor", backref="manhole_records")
|
subcontractor = db.relationship("Subcontractor", backref="manhole_records")
|
||||||
|
|
||||||
# Basic Fields
|
# Basic Fields
|
||||||
Location = db.Column(db.String(255))
|
Location = db.Column(db.String(500))
|
||||||
MH_NO = db.Column(db.String(100))
|
MH_NO = db.Column(db.String(100))
|
||||||
|
|
||||||
Upto_IL_Depth = db.Column(db.Float)
|
Upto_IL_Depth = db.Column(db.Float)
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ class ManholeDomesticChamberClient(db.Model):
|
|||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
# Foreign Key to Subcontractor table
|
# Foreign Key to Subcontractor table
|
||||||
subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False)
|
# subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False)
|
||||||
# Relationship for easy access (subcontractor.subcontractor_name)
|
# Relationship for easy access (subcontractor.subcontractor_name)
|
||||||
subcontractor = db.relationship("Subcontractor", backref="mh_dc_records")
|
# subcontractor = db.relationship("Subcontractor", backref="mh_dc_records")
|
||||||
|
|
||||||
# Basic Fields
|
# Basic Fields
|
||||||
Location = db.Column(db.String(255))
|
RA_Bill_No=db.Column(db.String(500))
|
||||||
|
Location = db.Column(db.String(500))
|
||||||
Node_No = db.Column(db.String(100))
|
Node_No = db.Column(db.String(100))
|
||||||
MH_TOP_LEVEL = db.Column(db.Float)
|
MH_TOP_LEVEL = db.Column(db.Float)
|
||||||
MH_IL_LEVEL = db.Column(db.Float)
|
MH_IL_LEVEL = db.Column(db.Float)
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ class ManholeExcavationClient(db.Model):
|
|||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
# Foreign Key to Subcontractor table
|
# Foreign Key to Subcontractor table
|
||||||
subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False)
|
# subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False)
|
||||||
# Relationship for easy access (subcontractor.subcontractor_name)
|
# Relationship for easy access (subcontractor.subcontractor_name)
|
||||||
subcontractor = db.relationship("Subcontractor", backref="mh_ex_records")
|
# subcontractor = db.relationship("Subcontractor", backref="mh_ex_records")
|
||||||
|
|
||||||
# Basic Fields
|
# Basic Fields
|
||||||
Location = db.Column(db.String(255))
|
RA_Bill_No=db.Column(db.String(500))
|
||||||
|
Location = db.Column(db.String(500))
|
||||||
MH_NO = db.Column(db.String(100))
|
MH_NO = db.Column(db.String(100))
|
||||||
GL_m = db.Column(db.Float)
|
GL_m = db.Column(db.Float)
|
||||||
MH_Invert_Level = db.Column(db.Float)
|
MH_Invert_Level = db.Column(db.Float)
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ class TrenchExcavationClient(db.Model):
|
|||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
# Foreign Key to Subcontractor table
|
# Foreign Key to Subcontractor table
|
||||||
subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False)
|
# subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False)
|
||||||
# Relationship for easy access (subcontractor.subcontractor_name)
|
# Relationship for easy access (subcontractor.subcontractor_name)
|
||||||
subcontractor = db.relationship("Subcontractor", backref="tr_ex_records")
|
# subcontractor = db.relationship("Subcontractor", backref="tr_ex_records")
|
||||||
|
|
||||||
# Basic Fields
|
# Basic Fields
|
||||||
Location = db.Column(db.String(255))
|
RA_Bill_No=db.Column(db.String(500))
|
||||||
|
Location = db.Column(db.String(500))
|
||||||
MH_NO = db.Column(db.String(100))
|
MH_NO = db.Column(db.String(100))
|
||||||
CC_length = db.Column(db.Float)
|
CC_length = db.Column(db.Float)
|
||||||
Actual_Trench_Length = db.Column(db.Float)
|
Actual_Trench_Length = db.Column(db.Float)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class TrenchExcavation(db.Model):
|
|||||||
subcontractor = db.relationship("Subcontractor", backref="trench_records")
|
subcontractor = db.relationship("Subcontractor", backref="trench_records")
|
||||||
|
|
||||||
# Basic Fields
|
# Basic Fields
|
||||||
Location = db.Column(db.String(255))
|
Location = db.Column(db.String(500))
|
||||||
MH_NO = db.Column(db.String(100))
|
MH_NO = db.Column(db.String(100))
|
||||||
CC_length = db.Column(db.Float)
|
CC_length = db.Column(db.Float)
|
||||||
Invert_Level = db.Column(db.Float)
|
Invert_Level = db.Column(db.Float)
|
||||||
|
|||||||
@@ -31,11 +31,12 @@ def client_import_file():
|
|||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
file = request.files.get("file")
|
file = request.files.get("file")
|
||||||
subcontractor_id = request.form.get("subcontractor_id")
|
# subcontractor_id = request.form.get("subcontractor_id")
|
||||||
file_type = request.form.get("file_type")
|
# file_type = request.form.get("file_type")
|
||||||
|
RA_Bill_No = request.form.get("RA_Bill_No")
|
||||||
|
|
||||||
service = FileService()
|
service = FileService()
|
||||||
success, msg = service.handle_file_upload(file, subcontractor_id, file_type)
|
success, msg = service.handle_client_file_upload(file, RA_Bill_No)
|
||||||
|
|
||||||
flash(msg, "success" if success else "danger")
|
flash(msg, "success" if success else "danger")
|
||||||
|
|
||||||
|
|||||||
@@ -23,12 +23,10 @@ class FileService:
|
|||||||
# def handle_file_upload(self, file, subcontractor_id, file_type):
|
# def handle_file_upload(self, file, subcontractor_id, file_type):
|
||||||
def handle_file_upload(self, file, subcontractor_id, RA_Bill_No):
|
def handle_file_upload(self, file, subcontractor_id, RA_Bill_No):
|
||||||
|
|
||||||
# RA_Bill_No = 1 # this RA bill define temp for upload (changes also)
|
|
||||||
|
|
||||||
if not subcontractor_id:
|
if not subcontractor_id:
|
||||||
return False, "Please select subcontractor."
|
return False, "Please select subcontractor."
|
||||||
# if not file_type:
|
if not RA_Bill_No:
|
||||||
# return False, "Please select file type."
|
return False, "Please Enter RA Bill No)."
|
||||||
if not file or file.filename == "":
|
if not file or file.filename == "":
|
||||||
return False, "No file selected."
|
return False, "No file selected."
|
||||||
if not self.allowed_file(file.filename):
|
if not self.allowed_file(file.filename):
|
||||||
@@ -45,69 +43,26 @@ class FileService:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# df = pd.read_csv(filepath) if filename.endswith(".csv") else pd.read_excel(filepath)
|
|
||||||
|
|
||||||
df_tr_ex = pd.read_excel(filepath, sheet_name ="Tr.Ex.", header=12)
|
df_tr_ex = pd.read_excel(filepath, sheet_name ="Tr.Ex.", header=12)
|
||||||
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12)
|
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12)
|
||||||
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11)
|
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11)
|
||||||
|
|
||||||
print("\n=== Uploaded File tr ex ===")
|
|
||||||
print(df_tr_ex.head())
|
|
||||||
print("=============================\n")
|
|
||||||
|
|
||||||
print("\n=== Uploaded File mh ex ===")
|
|
||||||
print(df_mh_ex.head())
|
|
||||||
print("========================================\n")
|
|
||||||
|
|
||||||
print("\n=== Uploaded File MH DC ===")
|
|
||||||
print(df_mh_dc.head())
|
|
||||||
print("========================================\n")
|
|
||||||
|
|
||||||
|
|
||||||
self.process_trench_excavation(df_tr_ex, subcontractor_id, RA_Bill_No)
|
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_excavation(df_mh_ex, subcontractor_id, RA_Bill_No)
|
||||||
self.process_manhole_domestic_chamber(df_mh_dc, subcontractor_id, RA_Bill_No)
|
self.process_manhole_domestic_chamber(df_mh_dc, subcontractor_id, RA_Bill_No)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# df = pd.read_csv(filepath) if filename.endswith(".csv") else pd.read_excel(filepath)
|
|
||||||
|
|
||||||
# # Trench Excavation save (subcontractor)
|
|
||||||
# if file_type == "trench_excavation":
|
|
||||||
# return self.process_trench_excavation(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Manhole Excavation save (subcontractor)
|
|
||||||
# if file_type == "manhole_excavation":
|
|
||||||
# return self.process_manhole_excavation(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Manhole and Domestic Chamber Construction save (subcontractor)
|
|
||||||
# if file_type == "manhole_domestic_chamber":
|
|
||||||
# return self.process_manhole_domestic_chamber(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Tr Ex save (client)
|
|
||||||
# if file_type =="tr_ex_client":
|
|
||||||
# return self.client_trench_excavation(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Mh Ex save (client)
|
|
||||||
# if file_type =="mh_ex_client":
|
|
||||||
# return self.client_manhole_excavation(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Mh and Dc save (client)
|
|
||||||
# if file_type == "mh_dc_client":
|
|
||||||
# return self.client_manhole_domestic_chamber(df, subcontractor_id)
|
|
||||||
|
|
||||||
return True, "File uploaded successfully."
|
return True, "File uploaded successfully."
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False, f"Processing failed: {e}"
|
return False, f"Processing failed: {e}"
|
||||||
|
|
||||||
|
|
||||||
# new new
|
# Trench Excavation save method (TrenchExcavation model)
|
||||||
def process_trench_excavation(self, df, subcontractor_id,RA_Bill_No):
|
def process_trench_excavation(self, df, subcontractor_id,RA_Bill_No):
|
||||||
|
|
||||||
print("RA_Bill_No of Tr Ex:",RA_Bill_No)
|
print("trench_excavation RA_Bill_No :",RA_Bill_No)
|
||||||
|
|
||||||
print("=== RAW HEADERS ===")
|
print("=== trench_excavation ===")
|
||||||
print(df.columns.tolist())
|
print(df.columns.tolist())
|
||||||
print("===================")
|
print("===================")
|
||||||
|
|
||||||
@@ -161,7 +116,7 @@ class FileService:
|
|||||||
**record_data
|
**record_data
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO)
|
print("trench_excavation Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO)
|
||||||
|
|
||||||
db.session.add(record)
|
db.session.add(record)
|
||||||
saved_count += 1
|
saved_count += 1
|
||||||
@@ -178,12 +133,12 @@ class FileService:
|
|||||||
return False, f"Trench Excavation save failed: {e}"
|
return False, f"Trench Excavation save failed: {e}"
|
||||||
|
|
||||||
|
|
||||||
# new new
|
# Manhole Excavation save method (ManholeExcavation model)
|
||||||
def process_manhole_excavation(self, df, subcontractor_id, RA_Bill_No):
|
def process_manhole_excavation(self, df, subcontractor_id, RA_Bill_No):
|
||||||
|
|
||||||
print("RA_Bill_No of MH EX:",RA_Bill_No)
|
print("manhole_excavation RA_Bill_No:",RA_Bill_No)
|
||||||
|
|
||||||
print("=== RAW HEADERS ===")
|
print("=== manhole_excavation ===")
|
||||||
print(df.columns.tolist())
|
print(df.columns.tolist())
|
||||||
print("===================")
|
print("===================")
|
||||||
|
|
||||||
@@ -237,7 +192,7 @@ class FileService:
|
|||||||
**record_data
|
**record_data
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO)
|
print("manhole_excavation Saving Row → Location:", record.Location, " MH_NO:", record.MH_NO)
|
||||||
|
|
||||||
db.session.add(record)
|
db.session.add(record)
|
||||||
saved_count += 1
|
saved_count += 1
|
||||||
@@ -253,12 +208,12 @@ class FileService:
|
|||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
return False, f"Manhole Excavation save failed: {e}"
|
return False, f"Manhole Excavation save failed: {e}"
|
||||||
|
|
||||||
# new new
|
# Manhole and Domestic Chamber Construction save method (ManholeDomesticChamber model)
|
||||||
def process_manhole_domestic_chamber(self, df, subcontractor_id, RA_Bill_No):
|
def process_manhole_domestic_chamber(self, df, subcontractor_id, RA_Bill_No):
|
||||||
|
|
||||||
print("RA_Bill_No of Manhole Domestic Chamber:",RA_Bill_No)
|
print("manhole_domestic_chamber RA_Bill_No :",RA_Bill_No)
|
||||||
|
|
||||||
print("=== RAW HEADERS ===")
|
print("=== manhole_domestic_chamber ===")
|
||||||
print(df.columns.tolist())
|
print(df.columns.tolist())
|
||||||
print("===================")
|
print("===================")
|
||||||
|
|
||||||
@@ -312,7 +267,7 @@ class FileService:
|
|||||||
**record_data
|
**record_data
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Saving Row → Location:", record.Location, " Node_No:", record.Node_No)
|
print("manhole_domestic_chamber Saving Row → Location:", record.Location, " Node_No:", record.Node_No)
|
||||||
|
|
||||||
db.session.add(record)
|
db.session.add(record)
|
||||||
saved_count += 1
|
saved_count += 1
|
||||||
@@ -330,74 +285,54 @@ class FileService:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
# olds
|
# ---------------------- client ----------------------------------
|
||||||
# def handle_file_upload(self, file, subcontractor_id, file_type):
|
|
||||||
# # def handle_file_upload(self, file, subcontractor_id):
|
|
||||||
|
|
||||||
# if not subcontractor_id:
|
# def handle_client_file_upload(self, file, RA_Bill_No):
|
||||||
# return False, "Please select subcontractor."
|
|
||||||
# # if not file_type:
|
|
||||||
# # return False, "Please select file type."
|
|
||||||
# if not file or file.filename == "":
|
|
||||||
# return False, "No file selected."
|
|
||||||
# if not self.allowed_file(file.filename):
|
|
||||||
# return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
|
||||||
|
|
||||||
# ensure_upload_folder()
|
# if not RA_Bill_No:
|
||||||
|
# return False, "Please Enter RA Bill No ."
|
||||||
|
|
||||||
# folder = os.path.join(Config.UPLOAD_FOLDER, f"sub_{subcontractor_id}")
|
# if not file or file.filename == "":
|
||||||
# os.makedirs(folder, exist_ok=True)
|
# return False, "No file selected."
|
||||||
|
# if not self.allowed_file(file.filename):
|
||||||
|
# return False, "Invalid file type! Allowed: CSV, XLSX, XLS"
|
||||||
|
|
||||||
# filename = secure_filename(file.filename)
|
# ensure_upload_folder()
|
||||||
# filepath = os.path.join(folder, filename)
|
|
||||||
# file.save(filepath)
|
|
||||||
|
|
||||||
# try:
|
# folder = os.path.join(Config.UPLOAD_FOLDER, f"Client_Bill_{RA_Bill_No}")
|
||||||
|
# os.makedirs(folder, exist_ok=True)
|
||||||
|
|
||||||
# # df = pd.read_csv(filepath) if filename.endswith(".csv") else pd.read_excel(filepath)
|
# filename = secure_filename(file.filename)
|
||||||
# df = pd.read_excel(filepath, sheet_name ="Tr.Ex.", header=0)
|
# filepath = os.path.join(folder, filename)
|
||||||
# df1 = pd.read_excel(filepath, sheet_name="MH Ex.", header=0)
|
# file.save(filepath)
|
||||||
|
|
||||||
# print("\n=== Uploaded File Preview ===")
|
# try:
|
||||||
# print(" file name h:",df)
|
# df_tr_ex = pd.read_excel(filepath, sheet_name ="Tr.Ex.", header=12)
|
||||||
# print("=============================\n")
|
# df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12)
|
||||||
# print(" file name h1:",df1)
|
# df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11)
|
||||||
# # print(df.head())
|
|
||||||
# print("=============================\n")
|
|
||||||
|
|
||||||
# # Trench Excavation save (subcontractor)
|
# print("\n=== Uploaded File tr ex ===")
|
||||||
# if file_type == "trench_excavation":
|
# print(df_tr_ex.head())
|
||||||
# return self.process_trench_excavation(df, subcontractor_id)
|
# print("=============================\n")
|
||||||
|
# print("\n=== Uploaded File mh ex ===")
|
||||||
# # Manhole Excavation save (subcontractor)
|
# print(df_mh_ex.head())
|
||||||
# if file_type == "manhole_excavation":
|
# print("========================================\n")
|
||||||
# return self.process_manhole_excavation(df, subcontractor_id)
|
# print("\n=== Uploaded File MH DC ===")
|
||||||
|
# print(df_mh_dc.head())
|
||||||
# # Manhole and Domestic Chamber Construction save (subcontractor)
|
# print("========================================\n")
|
||||||
# if file_type == "manhole_domestic_chamber":
|
|
||||||
# return self.process_manhole_domestic_chamber(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Tr Ex save (client)
|
|
||||||
# if file_type =="tr_ex_client":
|
|
||||||
# return self.client_trench_excavation(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Mh Ex save (client)
|
|
||||||
# if file_type =="mh_ex_client":
|
|
||||||
# return self.client_manhole_excavation(df, subcontractor_id)
|
|
||||||
|
|
||||||
# # Mh and Dc save (client)
|
|
||||||
# if file_type == "mh_dc_client":
|
|
||||||
# return self.client_manhole_domestic_chamber(df, subcontractor_id)
|
|
||||||
|
|
||||||
# return True, "File uploaded successfully."
|
|
||||||
|
|
||||||
# except Exception as e:
|
|
||||||
# return False, f"Processing failed: {e}"
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------- Sub contractor --------------------------
|
# self.client_trench_excavation(df_tr_ex, RA_Bill_No)
|
||||||
# Trench Excavation save method (TrenchExcavation model)
|
# self.client_manhole_excavation(df_mh_ex, RA_Bill_No)
|
||||||
# def process_trench_excavation(self, df, subcontractor_id):
|
# self.client_manhole_domestic_chamber(df_mh_dc, RA_Bill_No)
|
||||||
|
|
||||||
|
# return True, "File uploaded successfully."
|
||||||
|
|
||||||
|
# except Exception as e:
|
||||||
|
# return False, f"Processing failed: {e}"
|
||||||
|
|
||||||
|
# # Tr Ex save method (TrenchExcavationClient model)
|
||||||
|
# def client_trench_excavation(self, df, RA_Bill_No):
|
||||||
# df.columns = [str(c).strip() for c in df.columns]
|
# df.columns = [str(c).strip() for c in df.columns]
|
||||||
# # If the sheet has merged cells -> forward fill Location
|
# # If the sheet has merged cells -> forward fill Location
|
||||||
# if "Location" in df.columns:
|
# if "Location" in df.columns:
|
||||||
@@ -416,7 +351,7 @@ class FileService:
|
|||||||
# record_data = {}
|
# record_data = {}
|
||||||
# # Insert only fields that exist in model
|
# # Insert only fields that exist in model
|
||||||
# for col in df.columns:
|
# for col in df.columns:
|
||||||
# if hasattr(TrenchExcavation, col):
|
# if hasattr(TrenchExcavationClient, col):
|
||||||
# value = row[col]
|
# value = row[col]
|
||||||
# # Normalize empty values
|
# # Normalize empty values
|
||||||
# if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
|
# if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
|
||||||
@@ -424,8 +359,8 @@ class FileService:
|
|||||||
|
|
||||||
# record_data[col] = value
|
# record_data[col] = value
|
||||||
|
|
||||||
# record = TrenchExcavation(
|
# record = TrenchExcavationClient(
|
||||||
# subcontractor_id=subcontractor_id,
|
# RA_Bill_No=RA_Bill_No,
|
||||||
# **record_data
|
# **record_data
|
||||||
# )
|
# )
|
||||||
|
|
||||||
@@ -433,15 +368,15 @@ class FileService:
|
|||||||
# saved_count += 1
|
# saved_count += 1
|
||||||
|
|
||||||
# db.session.commit()
|
# db.session.commit()
|
||||||
# return True, f"Trench Excavation data saved successfully. Total rows: {saved_count}"
|
# return True, f"Clinnt Tr Ex data saved successfully. Total rows: {saved_count}"
|
||||||
|
|
||||||
# except Exception as e:
|
# except Exception as e:
|
||||||
# db.session.rollback()
|
# db.session.rollback()
|
||||||
# return False, f"Trench Excavation Save Failed: {e}"
|
# return False, f"Clinnt Tr Ex Save Failed: {e}"
|
||||||
|
|
||||||
|
|
||||||
|
# # Mh Ex save method (ManholeExcavationClient model)
|
||||||
# Manhole Excavation save method (ManholeExcavation model)
|
# def client_manhole_excavation(self, df, RA_Bill_No):
|
||||||
# def process_manhole_excavation(self, df, subcontractor_id):
|
|
||||||
# # Clean column names (strip whitespace)
|
# # Clean column names (strip whitespace)
|
||||||
# df.columns = [str(c).strip() for c in df.columns]
|
# df.columns = [str(c).strip() for c in df.columns]
|
||||||
# # If the sheet has merged cells -> forward fill Location
|
# # If the sheet has merged cells -> forward fill Location
|
||||||
@@ -462,7 +397,7 @@ class FileService:
|
|||||||
# record_data = {}
|
# record_data = {}
|
||||||
# # Insert only fields that exist in model
|
# # Insert only fields that exist in model
|
||||||
# for col in df.columns:
|
# for col in df.columns:
|
||||||
# if hasattr(ManholeExcavation, col):
|
# if hasattr(ManholeExcavationClient, col):
|
||||||
# value = row[col]
|
# value = row[col]
|
||||||
|
|
||||||
# # Normalize empty values
|
# # Normalize empty values
|
||||||
@@ -471,8 +406,8 @@ class FileService:
|
|||||||
|
|
||||||
# record_data[col] = value
|
# record_data[col] = value
|
||||||
|
|
||||||
# record = ManholeExcavation(
|
# record = ManholeExcavationClient(
|
||||||
# subcontractor_id=subcontractor_id,
|
# RA_Bill_No=RA_Bill_No,
|
||||||
# **record_data
|
# **record_data
|
||||||
# )
|
# )
|
||||||
|
|
||||||
@@ -480,196 +415,140 @@ class FileService:
|
|||||||
# saved_count += 1
|
# saved_count += 1
|
||||||
|
|
||||||
# db.session.commit()
|
# db.session.commit()
|
||||||
# return True, f"Manhole Excavation data saved successfully. Total rows: {saved_count}"
|
# return True, f" Client Mh Ex. data saved successfully. Total rows: {saved_count}"
|
||||||
|
|
||||||
# except Exception as e:
|
# except Exception as e:
|
||||||
# db.session.rollback()
|
# db.session.rollback()
|
||||||
# return False, f"Manhole Excavation Save Failed: {e}"
|
# return False, f"Client Mh Ex. Save Failed: {e}"
|
||||||
|
|
||||||
|
|
||||||
# Manhole and Domestic Chamber Construction save method (ManholeDomesticChamber model)
|
# # Mh and Dc save method (ManholeDomesticChamberClient model)
|
||||||
# def process_manhole_domestic_chamber(self, df, subcontractor_id):
|
# def client_manhole_domestic_chamber(self, df, RA_Bill_No):
|
||||||
# # 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(ManholeDomesticChamber, 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 = ManholeDomesticChamber(
|
|
||||||
# subcontractor_id=subcontractor_id,
|
|
||||||
# **record_data
|
|
||||||
# )
|
|
||||||
|
|
||||||
# db.session.add(record)
|
|
||||||
# saved_count += 1
|
|
||||||
|
|
||||||
# db.session.commit()
|
|
||||||
# return True, f"Manhole Domestic Chamber Construction data saved successfully. Total rows: {saved_count}"
|
|
||||||
|
|
||||||
# except Exception as e:
|
|
||||||
# db.session.rollback()
|
|
||||||
# return False, f"Manhole Domestic Chamber Construction Save Failed: {e}"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------- client ----------------------------------
|
|
||||||
# Tr Ex 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}"
|
|
||||||
|
|
||||||
|
|
||||||
# Mh Ex save method (ManholeExcavationClient model)
|
|
||||||
def client_manhole_excavation(self, df, subcontractor_id):
|
|
||||||
# Clean column names (strip whitespace)
|
# Clean column names (strip whitespace)
|
||||||
df.columns = [str(c).strip() for c in df.columns]
|
# df.columns = [str(c).strip() for c in df.columns]
|
||||||
# If the sheet has merged cells -> forward fill Location
|
# # If the sheet has merged cells -> forward fill Location
|
||||||
if "Location" in df.columns:
|
# if "Location" in df.columns:
|
||||||
df["Location"] = df["Location"].ffill()
|
# df["Location"] = df["Location"].ffill()
|
||||||
|
|
||||||
# REMOVE empty rows
|
# # REMOVE empty rows
|
||||||
df = df.dropna(how="all")
|
# df = df.dropna(how="all")
|
||||||
# Identify missing location rows before insert
|
# # Identify missing location rows before insert
|
||||||
missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")]
|
# missing_loc = df[df["Location"].isna() | (df["Location"].astype(str).str.strip() == "")]
|
||||||
if not missing_loc.empty:
|
# if not missing_loc.empty:
|
||||||
return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}"
|
# return False, f"Error: Some rows have empty Location. Rows: {missing_loc.index.tolist()}"
|
||||||
|
|
||||||
saved_count = 0
|
# 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(ManholeDomesticChamberClient, 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 = ManholeDomesticChamberClient(
|
||||||
|
# RA_Bill_No=RA_Bill_No,
|
||||||
|
# **record_data
|
||||||
|
# )
|
||||||
|
|
||||||
|
# db.session.add(record)
|
||||||
|
# saved_count += 1
|
||||||
|
|
||||||
|
# db.session.commit()
|
||||||
|
# return True, f"Mh and Dc data saved successfully. Total rows: {saved_count}"
|
||||||
|
|
||||||
|
# except Exception as e:
|
||||||
|
# db.session.rollback()
|
||||||
|
# return False, f"Mh and Dc data Save Failed: {e}"
|
||||||
|
|
||||||
|
def handle_client_file_upload(self, file, RA_Bill_No):
|
||||||
|
|
||||||
|
if not RA_Bill_No:
|
||||||
|
return False, "Please Enter RA Bill No."
|
||||||
|
|
||||||
|
if not file or file.filename == "":
|
||||||
|
return False, "No file selected."
|
||||||
|
|
||||||
|
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"Client_Bill_{RA_Bill_No}")
|
||||||
|
os.makedirs(folder, exist_ok=True)
|
||||||
|
|
||||||
|
filename = secure_filename(file.filename)
|
||||||
|
filepath = os.path.join(folder, filename)
|
||||||
|
file.save(filepath)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for index, row in df.iterrows():
|
df_tr_ex = pd.read_excel(filepath, sheet_name="Tr.Ex.", header=12)
|
||||||
|
df_mh_ex = pd.read_excel(filepath, sheet_name="MH Ex.", header=12)
|
||||||
|
df_mh_dc = pd.read_excel(filepath, sheet_name="MH & DC", header=11)
|
||||||
|
|
||||||
|
# Single transaction
|
||||||
|
self._save_client_data(df_tr_ex, TrenchExcavationClient, "Trench Excavation", RA_Bill_No)
|
||||||
|
self._save_client_data(df_mh_ex, ManholeExcavationClient, "Manhole Excavation", RA_Bill_No)
|
||||||
|
self._save_client_data(df_mh_dc, ManholeDomesticChamberClient, "MH & DC", RA_Bill_No)
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
|
return True, "Client file uploaded and all data saved successfully."
|
||||||
|
|
||||||
|
except ClientDataSaveError as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return False, str(e)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return False, f"Unexpected system error: {e}"
|
||||||
|
|
||||||
|
|
||||||
|
def _save_client_data(self, df, model, RA_Bill_No):
|
||||||
|
|
||||||
|
# Clean column names
|
||||||
|
df.columns = [str(c).strip() for c in df.columns]
|
||||||
|
|
||||||
|
# Forward fill Location (merged cells issue)
|
||||||
|
if "Location" in df.columns:
|
||||||
|
df["Location"] = df["Location"].ffill()
|
||||||
|
|
||||||
|
# Remove fully empty rows
|
||||||
|
df = df.dropna(how="all")
|
||||||
|
|
||||||
|
# Validate Location
|
||||||
|
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()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Insert rows
|
||||||
|
for _, row in df.iterrows():
|
||||||
record_data = {}
|
record_data = {}
|
||||||
# Insert only fields that exist in model
|
|
||||||
for col in df.columns:
|
for col in df.columns:
|
||||||
if hasattr(ManholeExcavationClient, col):
|
if hasattr(model, col):
|
||||||
value = row[col]
|
value = row[col]
|
||||||
|
|
||||||
# Normalize empty values
|
|
||||||
if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
|
if pd.isna(value) or str(value).strip() in ["", "-", "—", "nan", "NaN"]:
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
record_data[col] = value
|
record_data[col] = value
|
||||||
|
|
||||||
record = ManholeExcavationClient(
|
record = model(
|
||||||
subcontractor_id=subcontractor_id,
|
RA_Bill_No=RA_Bill_No,
|
||||||
**record_data
|
**record_data
|
||||||
)
|
)
|
||||||
|
|
||||||
db.session.add(record)
|
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}"
|
|
||||||
|
|
||||||
|
|
||||||
# Mh and Dc save method (ManholeDomesticChamberClient model)
|
|
||||||
def client_manhole_domestic_chamber(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(ManholeDomesticChamberClient, 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 = ManholeDomesticChamberClient(
|
|
||||||
subcontractor_id=subcontractor_id,
|
|
||||||
**record_data
|
|
||||||
)
|
|
||||||
|
|
||||||
db.session.add(record)
|
|
||||||
saved_count += 1
|
|
||||||
|
|
||||||
db.session.commit()
|
|
||||||
return True, f"Mh and Dc data saved successfully. Total rows: {saved_count}"
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
db.session.rollback()
|
|
||||||
return False, f"Mh and Dc data Save Failed: {e}"
|
|
||||||
|
|
||||||
@@ -8,17 +8,17 @@
|
|||||||
<form method="POST" enctype="multipart/form-data">
|
<form method="POST" enctype="multipart/form-data">
|
||||||
|
|
||||||
<!-- 1. SELECT SUBCONTRACTOR -->
|
<!-- 1. SELECT SUBCONTRACTOR -->
|
||||||
<label class="form-label">Select Subcontractor vs Client</label>
|
<!-- <label class="form-label">Select Subcontractor vs Client</label>
|
||||||
<select name="subcontractor_id" id="subcontractor_id" class="form-select mb-3" required>
|
<select name="subcontractor_id" id="subcontractor_id" class="form-select mb-3" required>
|
||||||
<option value="">-- Select Subcontractor --</option>
|
<option value="">-- Select Subcontractor --</option>
|
||||||
|
|
||||||
{% for sc in subcontractors %}
|
{% for sc in subcontractors %}
|
||||||
<option value="{{ sc.id }}">{{ sc.subcontractor_name }}</option>
|
<option value="{{ sc.id }}">{{ sc.subcontractor_name }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select> -->
|
||||||
|
|
||||||
<!-- 2. FILE TYPE (MODEL NAME) -->
|
<!-- 2. FILE TYPE (MODEL NAME) -->
|
||||||
<label class="form-label">Select File Type</label>
|
<!-- <label class="form-label">Select File Type</label>
|
||||||
<select name="file_type" id="file_type" class="form-select mb-3" required>
|
<select name="file_type" id="file_type" class="form-select mb-3" required>
|
||||||
<option value="">-- Select File Type --</option>
|
<option value="">-- Select File Type --</option>
|
||||||
<option value="">Sheet</option>
|
<option value="">Sheet</option>
|
||||||
@@ -26,8 +26,10 @@
|
|||||||
<option value="mh_ex_client">Mh. Ex </option>
|
<option value="mh_ex_client">Mh. Ex </option>
|
||||||
<option value="mh_dc_client">MH & DC </option>
|
<option value="mh_dc_client">MH & DC </option>
|
||||||
<option value="">Laying Sheet</option>
|
<option value="">Laying Sheet</option>
|
||||||
</select>
|
</select> -->
|
||||||
|
|
||||||
|
<label class="form-label">RA Bill No</label>
|
||||||
|
<input type="text" name="RA_Bill_No" class="form-control mb-3" required>
|
||||||
<!-- 3. FILE UPLOAD -->
|
<!-- 3. FILE UPLOAD -->
|
||||||
<label class="form-label">Choose File</label>
|
<label class="form-label">Choose File</label>
|
||||||
<input type="file" name="file" class="form-control mb-3" required>
|
<input type="file" name="file" class="form-control mb-3" required>
|
||||||
|
|||||||
Reference in New Issue
Block a user