Recovered lost local code

This commit is contained in:
2026-03-23 12:31:04 +05:30
parent d1dca531c3
commit 1821f04fe0
501 changed files with 1668 additions and 1842 deletions

View File

@@ -6,21 +6,20 @@ import openpyxl
from flask import Blueprint, request, render_template, redirect, url_for, jsonify, current_app
from flask_login import current_user
from model.Log import LogHelper
import config
from model.FolderAndFile import FolderAndFile
import config # your database connection module
excel_bp = Blueprint('excel', __name__)
# Default folder in case config not set
# DEFAULT_UPLOAD_FOLDER = 'uploads'
DEFAULT_UPLOAD_FOLDER = 'uploads'
# def get_upload_folder():
# """Returns the upload folder from Flask config or default, ensures it exists."""
# folder = current_app.config.get('UPLOAD_FOLDER', DEFAULT_UPLOAD_FOLDER)
# if not os.path.exists(folder):
# os.makedirs(folder)
# return folder
def get_upload_folder():
"""Returns the upload folder from Flask config or default, ensures it exists."""
folder = current_app.config.get('UPLOAD_FOLDER', DEFAULT_UPLOAD_FOLDER)
if not os.path.exists(folder):
os.makedirs(folder)
return folder
# ---------------- Upload Excel File ----------------
@@ -30,11 +29,8 @@ def upload():
if request.method == 'POST':
file = request.files.get('file')
if file and file.filename.endswith('.xlsx'):
# upload_folder = get_upload_folder()
# filepath = os.path.join(upload_folder, file.filename)
filepath =FolderAndFile.get_upload_path(file.filename)
upload_folder = get_upload_folder()
filepath = os.path.join(upload_folder, file.filename)
file.save(filepath)
LogHelper.log_action(
@@ -51,8 +47,7 @@ def show_table(filename):
global data
data = []
# filepath = os.path.join(get_upload_folder(), filename)
filepath = FolderAndFile.get_upload_path(filename)
filepath = os.path.join(get_upload_folder(), filename)
wb = openpyxl.load_workbook(filepath, data_only=True)
sheet = wb.active
@@ -74,6 +69,7 @@ def show_table(filename):
try:
cursor = connection.cursor(dictionary=True)
print(f"Calling GetStateByName with: {file_info['State']}")
cursor.callproc('GetStateByName', [file_info['State']])
for result in cursor.stored_results():
state_data = result.fetchone()
@@ -81,6 +77,7 @@ def show_table(filename):
errors.append(f"State '{file_info['State']}' is not valid. Please add it.")
if state_data:
print(f"Calling GetDistrictByNameAndStates with: {file_info['District']}, {state_data['State_ID']}")
cursor.callproc('GetDistrictByNameAndStates', [file_info['District'], state_data['State_ID']])
for result in cursor.stored_results():
district_data = result.fetchone()
@@ -88,23 +85,27 @@ def show_table(filename):
errors.append(f"District '{file_info['District']}' is not valid under state '{file_info['State']}'.")
if district_data:
print(f"Calling GetBlockByNameAndDistricts with: {file_info['Block']}, {district_data['District_ID']}")
cursor.callproc('GetBlockByNameAndDistricts', [file_info['Block'], district_data['District_ID']])
for result in cursor.stored_results():
block_data = result.fetchone()
if not block_data:
errors.append(f"Block '{file_info['Block']}' is not valid under district '{file_info['District']}'.")
print(f"Calling GetSubcontractorByName with: {file_info['Subcontractor']}")
cursor.callproc('GetSubcontractorByName', [file_info['Subcontractor']])
for result in cursor.stored_results():
subcontractor_data = result.fetchone()
if not subcontractor_data:
print(f"Inserting subcontractor: {file_info['Subcontractor']}")
cursor.callproc('InsertSubcontractor', [file_info['Subcontractor']])
connection.commit()
cursor.callproc('GetSubcontractorByName', [file_info['Subcontractor']])
for result in cursor.stored_results():
subcontractor_data = result.fetchone()
print("Calling GetAllHoldTypes")
cursor.callproc("GetAllHoldTypes")
hold_types_data = []
for ht in cursor.stored_results():

View File

@@ -1,98 +1,200 @@
# # controllers/invoice_controller.py
# from flask import Blueprint, request, jsonify, render_template
# from flask_login import login_required, current_user
# from model.Invoice import *
# from model.Log import LogHelper
# invoice_bp = Blueprint('invoice', __name__)
# # -------------------------------- Add Invoice ---------------------------------
# @invoice_bp.route('/add_invoice', methods=['GET', 'POST'])
# @login_required
# def add_invoice():
# if request.method == 'POST':
# try:
# village_name = request.form.get('village')
# village_result = get_village_id(village_name)
# if not village_result:
# return jsonify({"status": "error", "message": f"Village '{village_name}' not found"}), 400
# village_id = village_result['Village_Id']
# data = request.form
# invoice_id = insert_invoice(data, village_id)
# assign_subcontractor(data, village_id)
# insert_hold_types(data, invoice_id)
# LogHelper.log_action("Add invoice", f"User {current_user.id} Added invoice '{data.get('pmc_no')}'")
# return jsonify({"status": "success", "message": "Invoice added successfully"}), 201
# except Exception as e:
# return jsonify({"status": "error", "message": str(e)}), 500
# invoices = get_all_invoice_details()
# villages = get_all_villages()
# return render_template('add_invoice.html', invoices=invoices, villages=villages)
# # ------------------- Search Subcontractor -------------------
# @invoice_bp.route('/search_subcontractor', methods=['POST'])
# @login_required
# def search_subcontractor():
# sub_query = request.form.get("query")
# results = search_contractors(sub_query)
# if not results:
# return "<li>No subcontractor found</li>"
# output = "".join(
# f"<li data-id='{row['Contractor_Id']}'>{row['Contractor_Name']}</li>"
# for row in results
# )
# return output
# # ------------------- Get Hold Types -------------------
# @invoice_bp.route('/get_hold_types', methods=['GET'])
# @login_required
# def get_hold_types():
# hold_types = get_all_hold_types()
# LogHelper.log_action("Get hold type", f"User {current_user.id} Get hold type '{hold_types}'")
# return jsonify(hold_types)
# # ------------------- Edit Invoice -------------------
# @invoice_bp.route('/edit_invoice/<int:invoice_id>', methods=['GET', 'POST'])
# @login_required
# def edit_invoice(invoice_id):
# if request.method == 'POST':
# data = request.form
# update_invoice(data, invoice_id)
# update_inpayment(data)
# LogHelper.log_action("Edit invoice", f"User {current_user.id} Edit invoice '{invoice_id}'")
# return jsonify({"status": "success", "message": "Invoice updated successfully"}), 200
# invoice = get_invoice_by_id(invoice_id)
# return render_template('edit_invoice.html', invoice=invoice)
# # ------------------- Delete Invoice -------------------
# @invoice_bp.route('/delete_invoice/<int:invoice_id>', methods=['GET'])
# @login_required
# def delete_invoice_route(invoice_id):
# try:
# delete_invoice_data(invoice_id, current_user.id)
# LogHelper.log_action("Delete Invoice", f"User {current_user.id} deleted Invoice '{invoice_id}'")
# return jsonify({
# "message": f"Invoice {invoice_id} deleted successfully.",
# "status": "success"
# })
# except Exception as e:
# return jsonify({
# "message": str(e),
# "status": "error"
# }), 500
# controllers/invoice_controller.py
from flask import Blueprint, request, jsonify, render_template
from flask_login import login_required, current_user
from model.Invoice import *
from model.Log import LogHelper
from model.Log import LogHelper
invoice_bp = Blueprint('invoice', __name__)
# -------------------------------- Add Invoice ---------------------------------
@invoice_bp.route('/add_invoice', methods=['GET', 'POST'])
@login_required
def add_invoice():
if request.method == 'POST':
# ------------------------------- Helpers -------------------------------
def handle_exception(func):
"""Decorator to handle exceptions and return JSON error responses."""
def wrapper(*args, **kwargs):
try:
village_name = request.form.get('village')
village_result = get_village_id(village_name)
if not village_result:
return jsonify({"status": "error", "message": f"Village '{village_name}' not found"}), 400
village_id = village_result['Village_Id']
data = request.form
invoice_id = insert_invoice(data, village_id)
assign_subcontractor(data, village_id)
insert_hold_types(data, invoice_id)
LogHelper.log_action("Add invoice", f"User {current_user.id} Added invoice '{data.get('pmc_no')}'")
return jsonify({"status": "success", "message": "Invoice added successfully"}), 201
return func(*args, **kwargs)
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
wrapper.__name__ = func.__name__
return wrapper
def log_action(action: str, detail: str):
LogHelper.log_action(action, f"User {current_user.id} {detail}")
# ------------------------------- Add Invoice -------------------------------
@invoice_bp.route('/add_invoice', methods=['GET', 'POST'])
@login_required
@handle_exception
def add_invoice():
if request.method == 'POST':
data = request.form
village_name = data.get('village')
village_result = get_village_id(village_name)
if not village_result:
return jsonify({"status": "error", "message": f"Village '{village_name}' not found"}), 400
village_id = village_result['Village_Id']
invoice_id = insert_invoice(data, village_id)
assign_subcontractor(data, village_id)
insert_hold_types(data, invoice_id)
log_action("Add invoice", f"added invoice '{data.get('pmc_no')}'")
return jsonify({"status": "success", "message": "Invoice added successfully"}), 201
invoices = get_all_invoice_details()
villages = get_all_villages()
return render_template('add_invoice.html', invoices=invoices, villages=villages)
# ------------------- Search Subcontractor -------------------
# ------------------------------- Search Subcontractor -------------------------------
@invoice_bp.route('/search_subcontractor', methods=['POST'])
@login_required
@handle_exception
def search_subcontractor():
sub_query = request.form.get("query")
results = search_contractors(sub_query)
query = request.form.get("query", "").strip()
results = search_contractors(query)
if not results:
return "<li>No subcontractor found</li>"
output = "".join(
f"<li data-id='{row['Contractor_Id']}'>{row['Contractor_Name']}</li>"
for row in results
)
return output
return "".join(f"<li data-id='{r['Contractor_Id']}'>{r['Contractor_Name']}</li>" for r in results)
# ------------------- Get Hold Types -------------------
# ------------------------------- Get Hold Types -------------------------------
@invoice_bp.route('/get_hold_types', methods=['GET'])
@login_required
@handle_exception
def get_hold_types():
hold_types = get_all_hold_types()
LogHelper.log_action("Get hold type", f"User {current_user.id} Get hold type '{hold_types}'")
log_action("Get hold type", f"retrieved hold types '{hold_types}'")
return jsonify(hold_types)
# ------------------- Edit Invoice -------------------
# ------------------------------- Edit Invoice -------------------------------
@invoice_bp.route('/edit_invoice/<int:invoice_id>', methods=['GET', 'POST'])
@login_required
@handle_exception
def edit_invoice(invoice_id):
if request.method == 'POST':
data = request.form
update_invoice(data, invoice_id)
update_inpayment(data)
LogHelper.log_action("Edit invoice", f"User {current_user.id} Edit invoice '{invoice_id}'")
log_action("Edit invoice", f"edited invoice '{invoice_id}'")
return jsonify({"status": "success", "message": "Invoice updated successfully"}), 200
invoice = get_invoice_by_id(invoice_id)
return render_template('edit_invoice.html', invoice=invoice)
# ------------------- Delete Invoice -------------------
# ------------------------------- Delete Invoice -------------------------------
@invoice_bp.route('/delete_invoice/<int:invoice_id>', methods=['GET'])
@login_required
@handle_exception
def delete_invoice_route(invoice_id):
try:
delete_invoice_data(invoice_id, current_user.id)
LogHelper.log_action("Delete Invoice", f"User {current_user.id} deleted Invoice '{invoice_id}'")
return jsonify({
"message": f"Invoice {invoice_id} deleted successfully.",
"status": "success"
})
except Exception as e:
return jsonify({
"message": str(e),
"status": "error"
}), 500
delete_invoice_data(invoice_id, current_user.id)
log_action("Delete invoice", f"deleted invoice '{invoice_id}'")
return jsonify({"status": "success", "message": f"Invoice {invoice_id} deleted successfully."})

View File

@@ -1,13 +1,13 @@
from flask import Blueprint, render_template, send_from_directory
from flask_login import login_required, current_user
from model.PmcReport import PmcReport
pmc_report_bp = Blueprint("pmc_report", __name__)
@pmc_report_bp.route("/pmc_report/<pmc_no>")
@login_required
def pmc_report(pmc_no):
data = PmcReport.get_pmc_report(pmc_no)
if not data:
return "No PMC found with this number", 404
@@ -24,7 +24,6 @@ def pmc_report(pmc_no):
)
@pmc_report_bp.route("/download_pmc_report/<pmc_no>")
@login_required
def download_pmc_report(pmc_no):
result = PmcReport.download_pmc_report(pmc_no)

View File

@@ -3,11 +3,11 @@ from flask_login import login_required, current_user
from model.Report import ReportHelper
from model.Log import LogHelper
import config
from datetime import datetime
import os
import openpyxl
from openpyxl.styles import Font
from model.ContractorInfo import ContractorInfo
from model.FolderAndFile import FolderAndFile
report_bp = Blueprint("report", __name__)
@@ -26,17 +26,35 @@ def report_page():
def search_contractor():
subcontractor_name = request.form.get("subcontractor_name")
pmc_no = request.form.get("pmc_no")
state = request.form.get("state")
district = request.form.get("district")
block = request.form.get("block")
village = request.form.get("village")
year_from = request.form.get("year_from")
year_to = request.form.get("year_to")
LogHelper.log_action(
"Search Contractor",
f"User {current_user.id} searched contractor '{subcontractor_name}'"
f"User {current_user.id} Search contractor '{subcontractor_name}'"
)
data = ReportHelper.search_contractor(request)
data = ReportHelper.search_contractor(
subcontractor_name,
pmc_no,
state,
district,
block,
village,
year_from,
year_to
)
return jsonify(data)
# ---------------- Contractor Report ----------------
@report_bp.route('/contractor_report/<int:contractor_id>')
@login_required
def contractor_report(contractor_id):
@@ -49,145 +67,136 @@ def contractor_report(contractor_id):
**data
)
class FilePathData:
downloadReportFolder = "static/download"
@report_bp.route('/download_report/<int:contractor_id>')
@login_required
def download_report(contractor_id):
try:
connection = config.get_db_connection()
cursor = connection.cursor(dictionary=True)
return ReportHelper().download_report(contractor_id=contractor_id)
# -------- Contractor Info --------
contractor = ContractorInfo(contractor_id)
contInfo = contractor.contInfo
if not contInfo:
return "No contractor found", 404
# @report_bp.route('/download_report/<int:contractor_id>')
# @login_required
# def download_report(contractor_id):
# try:
# connection = config.get_db_connection()
# cursor = connection.cursor(dictionary=True)
# -------- Invoice Data --------
cursor.callproc('FetchInvoicesByContractor', [contractor_id])
# # -------- Contractor Info --------
# contractor = ContractorInfo(contractor_id)
# contInfo = contractor.contInfo
invoices = []
for result in cursor.stored_results():
invoices.extend(result.fetchall())
# if not contInfo:
# return "No contractor found", 404
if not invoices:
return "No invoice data found"
# # -------- Invoice Data --------
# cursor.callproc('FetchInvoicesByContractor', [contractor_id])
# -------- Create Workbook --------
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = "Contractor Report"
# invoices = []
# for result in cursor.stored_results():
# invoices.extend(result.fetchall())
# ================= CONTRACTOR DETAILS =================
sheet.append(["SUB CONTRACTOR DETAILS"])
sheet.cell(row=sheet.max_row, column=1).font = Font(bold=True)
sheet.append([])
# if not invoices:
# return "No invoice data found"
sheet.append(["Name", contInfo.get("Contractor_Name") or ""])
sheet.append(["Mobile No", contInfo.get("Mobile_No") or ""])
sheet.append(["Email", contInfo.get("Email") or ""])
sheet.append(["Village", contInfo.get("Village_Name") or ""])
sheet.append(["Block", contInfo.get("Block_Name") or ""])
sheet.append(["District", contInfo.get("District_Name") or ""])
sheet.append(["State", contInfo.get("State_Name") or ""])
sheet.append(["Address", contInfo.get("Address") or ""])
sheet.append(["GST No", contInfo.get("GST_No") or ""])
sheet.append(["PAN No", contInfo.get("PAN_No") or ""])
sheet.append([])
sheet.append([])
# # -------- Create Workbook --------
# workbook = openpyxl.Workbook()
# sheet = workbook.active
# sheet.title = "Contractor Report"
# ================= TABLE HEADERS =================
headers = [
"PMC No", "Village", "Invoice No", "Invoice Date", "Work Type","Invoice_Details",
"Basic Amount", "Debit Amount", "After Debit Amount",
"Amount", "GST Amount", "TDS Amount", "SD Amount",
"On Commission", "Hydro Testing", "Hold Amount",
"GST SD Amount", "Final Amount",
"Payment Amount", "TDS Payment",
"Total Amount", "UTR"
]
sheet.append(headers)
for col in range(1, len(headers) + 1):
sheet.cell(row=sheet.max_row, column=col).font = Font(bold=True)
# # ================= CONTRACTOR DETAILS =================
# sheet.append(["SUB CONTRACTOR DETAILS"])
# sheet.cell(row=sheet.max_row, column=1).font = Font(bold=True)
# sheet.append([])
# ================= DATA =================
total_final = 0
total_payment = 0
total_amount = 0
# sheet.append(["Name", contInfo.get("Contractor_Name") or ""])
# sheet.append(["Mobile No", contInfo.get("Mobile_No") or ""])
# sheet.append(["Email", contInfo.get("Email") or ""])
# sheet.append(["Village", contInfo.get("Village_Name") or ""])
# sheet.append(["Block", contInfo.get("Block_Name") or ""])
# sheet.append(["District", contInfo.get("District_Name") or ""])
# sheet.append(["State", contInfo.get("State_Name") or ""])
# sheet.append(["Address", contInfo.get("Address") or ""])
# sheet.append(["GST No", contInfo.get("GST_No") or ""])
# sheet.append(["PAN No", contInfo.get("PAN_No") or ""])
# sheet.append([])
# sheet.append([])
for inv in invoices:
row = [
inv.get("PMC_No"),
inv.get("Village_Name"),
inv.get("invoice_no"),
inv.get("Invoice_Date"),
inv.get("Work_Type"),
inv.get("Invoice_Details"),
inv.get("Basic_Amount"),
inv.get("Debit_Amount"),
inv.get("After_Debit_Amount"),
inv.get("Amount"),
inv.get("GST_Amount"),
inv.get("TDS_Amount"),
inv.get("SD_Amount"),
inv.get("On_Commission"),
inv.get("Hydro_Testing"),
inv.get("Hold_Amount"),
inv.get("GST_SD_Amount"),
inv.get("Final_Amount"),
inv.get("Payment_Amount"),
inv.get("TDS_Payment_Amount"),
inv.get("Total_Amount"),
inv.get("UTR")
]
# # ================= TABLE HEADERS =================
# headers = [
# "PMC No", "Village", "Invoice No", "Invoice Date", "Work Type","Invoice_Details",
# "Basic Amount", "Debit Amount", "After Debit Amount",
# "Amount", "GST Amount", "TDS Amount", "SD Amount",
# "On Commission", "Hydro Testing", "Hold Amount",
# "GST SD Amount", "Final Amount",
# "Payment Amount", "TDS Payment",
# "Total Amount", "UTR"
# ]
# sheet.append(headers)
# for col in range(1, len(headers) + 1):
# sheet.cell(row=sheet.max_row, column=col).font = Font(bold=True)
total_final += float(inv.get("Final_Amount") or 0)
total_payment += float(inv.get("Payment_Amount") or 0)
total_amount += float(inv.get("Total_Amount") or 0)
# # ================= DATA =================
# total_final = 0
# total_payment = 0
# total_amount = 0
sheet.append(row)
# for inv in invoices:
# row = [
# inv.get("PMC_No"),
# inv.get("Village_Name"),
# inv.get("invoice_no"),
# inv.get("Invoice_Date"),
# inv.get("Work_Type"),
# inv.get("Invoice_Details"),
# inv.get("Basic_Amount"),
# inv.get("Debit_Amount"),
# inv.get("After_Debit_Amount"),
# inv.get("Amount"),
# inv.get("GST_Amount"),
# inv.get("TDS_Amount"),
# inv.get("SD_Amount"),
# inv.get("On_Commission"),
# inv.get("Hydro_Testing"),
# inv.get("Hold_Amount"),
# inv.get("GST_SD_Amount"),
# inv.get("Final_Amount"),
# inv.get("Payment_Amount"),
# inv.get("TDS_Payment_Amount"),
# inv.get("Total_Amount"),
# inv.get("UTR")
# ]
# ================= TOTAL ROW =================
sheet.append([])
sheet.append([
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"TOTAL",
total_final,
total_payment,
"",
total_amount,
""
])
# total_final += float(inv.get("Final_Amount") or 0)
# total_payment += float(inv.get("Payment_Amount") or 0)
# total_amount += float(inv.get("Total_Amount") or 0)
# ================= AUTO WIDTH =================
for column in sheet.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
if cell.value:
max_length = max(max_length, len(str(cell.value)))
sheet.column_dimensions[column_letter].width = max_length + 2
# sheet.append(row)
# ================= SAVE FILE =================
output_folder = "downloads"
os.makedirs(output_folder, exist_ok=True)
filename = f"Contractor_Report_{contInfo.get('Contractor_Name')}.xlsx"
output_file = os.path.join(output_folder, filename)
workbook.save(output_file)
# # ================= TOTAL ROW =================
# sheet.append([])
# sheet.append([
# "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
# "TOTAL",
# total_final,
# total_payment,
# "",
# total_amount,
# ""
# ])
return send_file(output_file, as_attachment=True)
# # ================= AUTO WIDTH =================
# for column in sheet.columns:
# max_length = 0
# column_letter = column[0].column_letter
# for cell in column:
# if cell.value:
# max_length = max(max_length, len(str(cell.value)))
# sheet.column_dimensions[column_letter].width = max_length + 2
# # ================= SAVE FILE =================
# # output_folder = "downloads"
# # os.makedirs(output_folder, exist_ok=True)
# # filename = f"Contractor_Report_{contInfo.get('Contractor_Name')}.xlsx"
# # output_file = os.path.join(output_folder, filename)
# filename = f"Contractor_Report_{contInfo.get('Contractor_Name')}.xlsx"
# output_file = FolderAndFile.get_download_path(filename)
# workbook.save(output_file)
# return send_file(output_file, as_attachment=True)
# except Exception as e:
# return str(e)
except Exception as e:
return str(e)

View File

@@ -1,12 +1,12 @@
# controllers/subcontractor_controller.py
from flask import Blueprint, render_template, request, redirect, url_for, jsonify
from flask_login import login_required
from model.Subcontractor import Subcontractor
from model.Log import LogHelper
subcontractor_bp = Blueprint('subcontractor', __name__)
# ----------------------------------------------------------
# Helpers (unchanged)
# ----------------------------------------------------------
# Simple replacements for missing HtmlHelper and ResponseHandler
class HtmlHelper:
@staticmethod
def json_response(data, status=200):
@@ -30,88 +30,71 @@ class ResponseHandler:
return {"status": "error", "message": f"Failed to delete {entity}"}
# ----------------------------------------------------------
# LIST + ADD
# ----------------------------------------------------------
@subcontractor_bp.route('/subcontractor', methods=['GET', 'POST'])
@login_required
def subcontract():
subcontractor = []
sub = Subcontractor()
# ---------------- GET ----------------
if request.method == 'GET':
subcontractor = sub.GetAllSubcontractors(request)
subcontractor, error = Subcontractor.get_all_subcontractors()
if error:
return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor"), 500)
if not sub.isSuccess:
return HtmlHelper.json_response(
ResponseHandler.fetch_failure("Subcontractor"), 500
)
return render_template('add_subcontractor.html', subcontractor=subcontractor)
# ---------------- POST (ADD) ----------------
if request.method == 'POST':
contractor_data = {
'Contractor_Name': request.form['Contractor_Name'],
'Address': request.form['Address'],
'Mobile_No': request.form['Mobile_No'],
'PAN_No': request.form['PAN_No'],
'Email': request.form['Email'],
'Gender': request.form['Gender'],
'GST_Registration_Type': request.form['GST_Registration_Type'],
'GST_No': request.form['GST_No'],
'Contractor_password': request.form['Contractor_password'],
}
error = Subcontractor.save_subcontractor(contractor_data)
if error:
return HtmlHelper.json_response(ResponseHandler.add_failure("Subcontractor"), 500)
subcontractor, _ = Subcontractor.get_all_subcontractors()
sub.AddSubcontractor(request)
if not sub.isSuccess:
return HtmlHelper.json_response(
ResponseHandler.add_failure("Subcontractor"), 500
)
# Reload list after insert
subcontractor = sub.GetAllSubcontractors(request)
return render_template('add_subcontractor.html', subcontractor=subcontractor)
return render_template('add_subcontractor.html', subcontractor=subcontractor)
# ----------------------------------------------------------
# EDIT
# ----------------------------------------------------------
@subcontractor_bp.route('/edit_subcontractor/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_subcontractor(id):
sub = Subcontractor()
# Fetch data
subcontractor = sub.GetSubcontractorByID(id)
subcontractor, error = Subcontractor.get_subcontractor_by_id(id)
if error:
return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor"), 500)
if not subcontractor:
return HtmlHelper.json_response(
ResponseHandler.fetch_failure("Subcontractor"), 404
)
return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor"), 404)
# ---------------- POST (UPDATE) ----------------
if request.method == 'POST':
sub.EditSubcontractor(request, id)
if not sub.isSuccess:
return HtmlHelper.json_response(
ResponseHandler.update_failure("Subcontractor"), 500
)
updated_data = {
'Contractor_Name': request.form['Contractor_Name'],
'Address': request.form['Address'],
'Mobile_No': request.form['Mobile_No'],
'PAN_No': request.form['PAN_No'],
'Email': request.form['Email'],
'Gender': request.form['Gender'],
'GST_Registration_Type': request.form['GST_Registration_Type'],
'GST_No': request.form['GST_No'],
'Contractor_password': request.form['Contractor_password'],
}
error = Subcontractor.update_subcontractor(id, updated_data)
if error:
return HtmlHelper.json_response(ResponseHandler.update_failure("Subcontractor"), 500)
return redirect(url_for('subcontractor.subcontract'))
return render_template('edit_subcontractor.html', subcontractor=subcontractor)
# ----------------------------------------------------------
# DELETE
# ----------------------------------------------------------
@subcontractor_bp.route('/deleteSubContractor/<int:id>', methods=['GET', 'POST'])
@login_required
def deleteSubContractor(id):
sub = Subcontractor()
sub.DeleteSubcontractor(request, id)
if not sub.isSuccess:
return HtmlHelper.json_response(
ResponseHandler.delete_failure("Subcontractor"), 500
)
error, affected_rows = Subcontractor.delete_subcontractor(id)
if error:
return HtmlHelper.json_response(ResponseHandler.delete_failure("Subcontractor"), 500)
if affected_rows == 0:
return HtmlHelper.json_response(ResponseHandler.fetch_failure("Subcontractor not deleted"), 404)
return redirect(url_for('subcontractor.subcontract'))

View File

@@ -1,8 +1,177 @@
# from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify
# from flask_login import login_required
# import config
# from model.Village import Village
# from model.State import State
# # Create Blueprint
# village_bp = Blueprint('village', __name__)
# # ------------------------- Add Village -------------------------
# @village_bp.route('/add_village', methods=['GET', 'POST'])
# @login_required
# def add_village():
# village = Village()
# if request.method == 'POST':
# village.AddVillage(request=request)
# return village.resultMessage
# state = State()
# states = state.GetAllStates(request=request)
# villages = village.GetAllVillages(request=request)
# return render_template(
# 'add_village.html',
# states=states,
# villages=villages
# )
# # ------------------------- Fetch Districts -------------------------
# @village_bp.route('/get_districts/<int:state_id>')
# @login_required
# def get_districts(state_id):
# connection = config.get_db_connection()
# cursor = connection.cursor()
# cursor.callproc("GetDistrictByStateID", [state_id])
# districts = []
# for rs in cursor.stored_results():
# districts = rs.fetchall()
# cursor.close()
# connection.close()
# district_list = []
# for d in districts:
# district_list.append({
# "id": d[0],
# "name": d[1]
# })
# return jsonify(district_list)
# # ------------------------- Fetch Blocks -------------------------
# @village_bp.route('/get_blocks/<int:district_id>')
# @login_required
# def get_blocks(district_id):
# connection = config.get_db_connection()
# cursor = connection.cursor()
# cursor.callproc("GetBlocksByDistrictID", [district_id])
# blocks = []
# for rs in cursor.stored_results():
# blocks = rs.fetchall()
# cursor.close()
# connection.close()
# block_list = []
# for b in blocks:
# block_list.append({
# "id": b[0],
# "name": b[1]
# })
# return jsonify(block_list)
# # ------------------------- Check Village -------------------------
# @village_bp.route('/check_village', methods=['POST'])
# @login_required
# def check_village():
# village = Village()
# return village.CheckVillage(request=request)
# # ------------------------- Delete Village -------------------------
# @village_bp.route('/delete_village/<int:village_id>')
# @login_required
# def delete_village(village_id):
# village = Village()
# village.DeleteVillage(request=request, village_id=village_id)
# if not village.isSuccess:
# flash(village.resultMessage, "error")
# else:
# flash(village.resultMessage, "success")
# return redirect(url_for('village.add_village'))
# # ------------------------- Edit Village -------------------------
# @village_bp.route('/edit_village/<int:village_id>', methods=['GET', 'POST'])
# @login_required
# def edit_village(village_id):
# village = Village()
# if request.method == 'POST':
# village.EditVillage(request=request, village_id=village_id)
# if village.isSuccess:
# flash(village.resultMessage, "success")
# return redirect(url_for('village.add_village'))
# else:
# flash(village.resultMessage, "error")
# village_data = village.GetVillageByID(id=village_id)
# blocks = village.GetAllBlocks()
# return render_template(
# 'edit_village.html',
# village_data=village_data,
# blocks=blocks
# )
# else:
# village_data = village.GetVillageByID(request=request, id=village_id)
# if not village.isSuccess:
# flash(village.resultMessage, "error")
# return redirect(url_for('village.add_village'))
# blocks = village.GetAllBlocks(request=request)
# if village_data is None:
# village_data = []
# if blocks is None:
# blocks = []
# return render_template(
# 'edit_village.html',
# village_data=village_data,
# blocks=blocks
# )
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify
from flask_login import login_required
import config
from model.Village import Village
from model.State import State
@@ -23,7 +192,6 @@ def add_village():
state = State()
states = state.GetAllStates(request=request)
villages = village.GetAllVillages(request=request)
return render_template(
@@ -42,7 +210,6 @@ def get_districts(state_id):
cursor = connection.cursor()
cursor.callproc("GetDistrictByStateID", [state_id])
districts = []
for rs in cursor.stored_results():
@@ -51,15 +218,7 @@ def get_districts(state_id):
cursor.close()
connection.close()
district_list = []
for d in districts:
district_list.append({
"id": d[0],
"name": d[1]
})
return jsonify(district_list)
return jsonify([{"id": d[0], "name": d[1]} for d in districts])
# ------------------------- Fetch Blocks -------------------------
@@ -71,7 +230,6 @@ def get_blocks(district_id):
cursor = connection.cursor()
cursor.callproc("GetBlocksByDistrictID", [district_id])
blocks = []
for rs in cursor.stored_results():
@@ -80,22 +238,13 @@ def get_blocks(district_id):
cursor.close()
connection.close()
block_list = []
for b in blocks:
block_list.append({
"id": b[0],
"name": b[1]
})
return jsonify(block_list)
return jsonify([{"id": b[0], "name": b[1]} for b in blocks])
# ------------------------- Check Village -------------------------
@village_bp.route('/check_village', methods=['POST'])
@login_required
def check_village():
village = Village()
return village.CheckVillage(request=request)
@@ -106,14 +255,9 @@ def check_village():
def delete_village(village_id):
village = Village()
village.DeleteVillage(request=request, village_id=village_id)
if not village.isSuccess:
flash(village.resultMessage, "error")
else:
flash(village.resultMessage, "success")
flash(village.resultMessage, "success" if village.isSuccess else "error")
return redirect(url_for('village.add_village'))
@@ -135,8 +279,8 @@ def edit_village(village_id):
else:
flash(village.resultMessage, "error")
village_data = village.GetVillageByID(request=request, id=village_id)
blocks = village.GetAllBlocks(request=request)
village_data = village.GetVillageByID(id=village_id) or []
blocks = village.GetAllBlocks() or []
return render_template(
'edit_village.html',
@@ -145,23 +289,17 @@ def edit_village(village_id):
)
else:
village_data = village.GetVillageByID(request=request, id=village_id)
# ✅ FIXED HERE (removed request)
village_data = village.GetVillageByID(id=village_id)
if not village.isSuccess:
flash(village.resultMessage, "error")
return redirect(url_for('village.add_village'))
blocks = village.GetAllBlocks(request=request)
if village_data is None:
village_data = []
if blocks is None:
blocks = []
blocks = village.GetAllBlocks() or []
return render_template(
'edit_village.html',
village_data=village_data,
village_data=village_data or [],
blocks=blocks
)