filter task show main task value and main and sub task edit
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,7 +3,6 @@ from flask_login import login_user, logout_user, current_user
|
||||
from ldap3 import Server, Connection, ALL
|
||||
from app import LDAPUser
|
||||
|
||||
|
||||
def login_controller():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for("main.dashboard"))
|
||||
@@ -56,7 +55,6 @@ def login_controller():
|
||||
# return render_template("login.html")
|
||||
|
||||
# ldap_user_dn = f"uid={username},ou=users,dc=lcepl,dc=org"
|
||||
|
||||
# try:
|
||||
# # Connect to LDAP server
|
||||
# # server = Server("openldap", port=389, get_info=ALL)
|
||||
|
||||
@@ -8,7 +8,6 @@ from app import db
|
||||
def dashboard_controller():
|
||||
|
||||
selected_block = request.args.getlist('block[]', None)
|
||||
|
||||
rate_col = cast(Task.rate, Float)
|
||||
qty_col = cast(Task.in_this_ra_bill_qty, Float)
|
||||
|
||||
@@ -29,7 +28,6 @@ def dashboard_controller():
|
||||
|
||||
if selected_block and "All" not in selected_block:
|
||||
query = query.filter(Task.block_name.in_(selected_block))
|
||||
|
||||
query = query.group_by(Task.block_name, Task.village_name)
|
||||
villages = query.all()
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@ from app import db
|
||||
from app.models import Task, WorkDetail
|
||||
from app.service.logger import log_activity
|
||||
|
||||
import pandas as pd
|
||||
from flask import request, send_file
|
||||
from io import BytesIO
|
||||
|
||||
|
||||
def filter_tasks_controller():
|
||||
|
||||
@@ -18,7 +22,7 @@ def filter_tasks_controller():
|
||||
# blocks
|
||||
if district:
|
||||
blocks = [b[0] for b in db.session.query(WorkDetail.block)
|
||||
.filter(WorkDetail.district == district).distinct()]
|
||||
.filter(WorkDetail.district == district).distinct()]
|
||||
else:
|
||||
blocks = []
|
||||
|
||||
@@ -40,7 +44,7 @@ def filter_tasks_controller():
|
||||
query = (
|
||||
db.session.query(Task)
|
||||
.join(WorkDetail,
|
||||
Task.village_name == WorkDetail.name_of_village)
|
||||
Task.village_name == WorkDetail.name_of_village)
|
||||
.filter(
|
||||
WorkDetail.district == district,
|
||||
WorkDetail.block == block,
|
||||
@@ -94,4 +98,59 @@ def filter_tasks_controller():
|
||||
selected_district=district,
|
||||
selected_block=block,
|
||||
selected_village=village
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def download_filtered_tasks_controller():
|
||||
district = request.args.get('district')
|
||||
block = request.args.get('block')
|
||||
village = request.args.get('village')
|
||||
|
||||
query = (
|
||||
db.session.query(Task)
|
||||
.join(WorkDetail,
|
||||
Task.village_name == WorkDetail.name_of_village)
|
||||
.filter(
|
||||
WorkDetail.district == district,
|
||||
WorkDetail.block == block,
|
||||
WorkDetail.name_of_village == village
|
||||
)
|
||||
)
|
||||
|
||||
tasks = query.all()
|
||||
|
||||
data = []
|
||||
for task in tasks:
|
||||
data.append({
|
||||
"Task Name": task.task_name,
|
||||
"Unit": task.unit,
|
||||
"Qty": task.qty,
|
||||
"Rate": task.rate,
|
||||
"BOQ Amount": task.boq_amount,
|
||||
"Prev Billed Qty": task.previous_billed_qty,
|
||||
"Prev Billing Amount": task.previous_billing_amount,
|
||||
"RA Bill Qty": task.in_this_ra_bill_qty,
|
||||
"RA Bill Amount": task.in_this_ra_billing_amount,
|
||||
"Cum Billed Qty": task.cumulative_billed_qty,
|
||||
"Cum Billed Amount": task.cumulative_billed_amount,
|
||||
"Variation Qty": task.variation_qty,
|
||||
"Variation Amount": task.variation_amount,
|
||||
})
|
||||
|
||||
df = pd.DataFrame(data)
|
||||
|
||||
output = BytesIO()
|
||||
df.to_excel(output, index=False, engine='openpyxl')
|
||||
output.seek(0)
|
||||
|
||||
filename = f"{district}_{block}_{village}_tasks.xlsx"
|
||||
|
||||
return send_file(
|
||||
output,
|
||||
download_name=filename,
|
||||
as_attachment=True,
|
||||
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
||||
)
|
||||
@@ -5,7 +5,7 @@ from app.models import Task
|
||||
from app.service.logger import log_activity
|
||||
|
||||
|
||||
def generate_report_page_controller():
|
||||
def generate_report_page_controller():
|
||||
|
||||
selected_district = request.args.get('district')
|
||||
selected_block = request.args.get('block')
|
||||
|
||||
@@ -8,21 +8,23 @@ from app.service.logger import log_activity
|
||||
def recalc_task(task):
|
||||
rate = float(task.rate or 0)
|
||||
qty = float(task.qty or 0)
|
||||
|
||||
|
||||
prev_qty = float(task.previous_billed_qty or 0)
|
||||
ra_qty = float(task.in_this_ra_bill_qty or 0)
|
||||
|
||||
# H = G * E
|
||||
task.previous_billing_amount = round(prev_qty * rate, 2)
|
||||
# J = I * E
|
||||
task.in_this_ra_billing_amount = round(ra_qty * rate, 2)
|
||||
|
||||
# K = I + G
|
||||
task.cumulative_billed_qty = round(prev_qty + ra_qty, 2)
|
||||
# L = K * E
|
||||
task.cumulative_billed_amount = round(task.cumulative_billed_qty * rate, 2)
|
||||
|
||||
# M = IF(K > D , K - D , 0)
|
||||
if task.cumulative_billed_qty > qty:
|
||||
task.variation_qty = round(task.cumulative_billed_qty - qty, 2)
|
||||
else:
|
||||
task.variation_qty = 0
|
||||
|
||||
# N = M * E
|
||||
task.variation_amount = round(task.variation_qty * rate, 2)
|
||||
|
||||
|
||||
@@ -40,116 +42,54 @@ def update_tasks_controller():
|
||||
"variation_qty",
|
||||
"variation_amount"
|
||||
]
|
||||
|
||||
for key, new_value in updates.items():
|
||||
|
||||
if '_' not in key:
|
||||
continue
|
||||
|
||||
field_name, task_id_str = key.rsplit('_', 1)
|
||||
|
||||
if not task_id_str.isdigit():
|
||||
continue
|
||||
|
||||
task = Task.query.get(int(task_id_str))
|
||||
|
||||
if task:
|
||||
|
||||
if field_name in formula_fields:
|
||||
continue
|
||||
|
||||
current_value = getattr(task, field_name, None)
|
||||
|
||||
if str(current_value) != str(new_value):
|
||||
setattr(task, field_name, new_value)
|
||||
|
||||
recalc_task(task)
|
||||
|
||||
update_count += 1
|
||||
|
||||
log_activity(
|
||||
current_user.username,
|
||||
"Task Update",
|
||||
f"Task ID {task.id} - {field_name} changed to {new_value}"
|
||||
)
|
||||
|
||||
if update_count > 0:
|
||||
db.session.commit()
|
||||
|
||||
log_activity(
|
||||
current_user.username,
|
||||
"Database Commit",
|
||||
f"{update_count} task field(s) updated"
|
||||
)
|
||||
|
||||
return jsonify({'message': f'count: {update_count} field(s) updated.'})
|
||||
|
||||
return jsonify({'message': 'No fields were updated.'})
|
||||
|
||||
except Exception as e:
|
||||
log_activity(current_user.username, "Error", str(e))
|
||||
return jsonify({'error': 'Update failed'}), 500
|
||||
|
||||
|
||||
# # Update tasks route
|
||||
# @main.route('/update_tasks', methods=['POST'])
|
||||
# @login_required
|
||||
# def update_tasks():
|
||||
# try:
|
||||
# updates = request.get_json()
|
||||
# update_count = 0
|
||||
|
||||
# for key, new_value in updates.items():
|
||||
# if '_' not in key:
|
||||
# continue
|
||||
|
||||
# field_name, task_id_str = key.rsplit('_', 1)
|
||||
# if not task_id_str.isdigit():
|
||||
# continue
|
||||
|
||||
# task_id = int(task_id_str)
|
||||
# task = db.session.query(Task).filter_by(id=task_id).first()
|
||||
|
||||
# if task:
|
||||
# current_value = getattr(task, field_name, None)
|
||||
# if current_value != new_value:
|
||||
# setattr(task, field_name, new_value)
|
||||
# update_count += 1
|
||||
# log_activity(current_user.username, "Task Update", f"Task ID {task.id} - {field_name} changed to {new_value}")
|
||||
|
||||
# if update_count > 0:
|
||||
# db.session.commit()
|
||||
# log_activity(current_user.username, "Database Commit", f"{update_count} task field(s) updated")
|
||||
# return jsonify({'message': f'count: {update_count} field(s) updated.'})
|
||||
# else:
|
||||
# return jsonify({'message': 'No fields were updated.'})
|
||||
|
||||
# except Exception as e:
|
||||
# log_activity(current_user.username, "Error", f"Update tasks error: {str(e)}")
|
||||
# return jsonify({'error': 'An error occurred while updating tasks.'}), 500
|
||||
|
||||
|
||||
def display_tasks_controller():
|
||||
|
||||
work_details = WorkDetail.query.order_by(
|
||||
WorkDetail.uploaded_at.desc()
|
||||
).first()
|
||||
|
||||
if not work_details:
|
||||
log_activity(current_user.username, "Tasks View", "No work details")
|
||||
return "No work details available.", 404
|
||||
|
||||
tasks = Task.query.filter_by(
|
||||
district=work_details.district,
|
||||
village_name=work_details.name_of_village,
|
||||
block_name=work_details.block
|
||||
).order_by(Task.uploaded_at.desc()).all()
|
||||
|
||||
grouped_tasks = []
|
||||
current_main_task = None
|
||||
|
||||
for task in tasks:
|
||||
|
||||
task_data = {
|
||||
"id": task.id,
|
||||
"task_name": task.task_name,
|
||||
@@ -168,20 +108,17 @@ def display_tasks_controller():
|
||||
"remark": task.remark,
|
||||
"district": task.district
|
||||
}
|
||||
|
||||
if task.serial_number:
|
||||
task_data["subtasks"] = []
|
||||
grouped_tasks.append(task_data)
|
||||
current_main_task = task_data
|
||||
elif current_main_task:
|
||||
current_main_task["subtasks"].append(task_data)
|
||||
|
||||
log_activity(
|
||||
current_user.username,
|
||||
"Tasks View",
|
||||
f"{work_details.name_of_village}, {work_details.block}"
|
||||
)
|
||||
|
||||
return render_template(
|
||||
'tasks_display.html',
|
||||
work_details=work_details,
|
||||
|
||||
@@ -4,8 +4,8 @@ from flask import request, redirect, url_for, current_app
|
||||
from flask_login import current_user
|
||||
from app import db
|
||||
from app.models import Task, WorkDetail
|
||||
from datetime import datetime
|
||||
from app.service.logger import log_activity
|
||||
|
||||
# keep helper inside controller
|
||||
def to_2_decimal(value):
|
||||
try:
|
||||
@@ -15,7 +15,6 @@ def to_2_decimal(value):
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def upload_controller():
|
||||
if 'file' not in request.files:
|
||||
return "No file part"
|
||||
@@ -31,7 +30,6 @@ def upload_controller():
|
||||
log_activity(current_user.username, "File Upload", f"Uploaded file: {file.filename}")
|
||||
|
||||
work_details_data = pd.read_excel(filepath, nrows=11, header=None, dtype=str)
|
||||
|
||||
work_details_dict = {
|
||||
"name_of_work": work_details_data.iloc[0, 1],
|
||||
"cover_agreement_no": work_details_data.iloc[1, 1],
|
||||
@@ -45,15 +43,11 @@ def upload_controller():
|
||||
"measurement_book": work_details_data.iloc[9, 1],
|
||||
"district": work_details_data.iloc[10, 1]
|
||||
}
|
||||
|
||||
work_details_dict = {k: (None if pd.isna(v) else v) for k, v in work_details_dict.items()}
|
||||
|
||||
work_detail = WorkDetail(**work_details_dict)
|
||||
db.session.add(work_detail)
|
||||
|
||||
data = pd.read_excel(filepath, skiprows=10)
|
||||
data = data.astype(object).where(pd.notna(data), None)
|
||||
|
||||
expected_columns = [
|
||||
"serial_number", "task_name", "unit", "qty", "rate", "boq_amount",
|
||||
"previous_billed_qty", "previous_billing_amount",
|
||||
@@ -61,27 +55,21 @@ def upload_controller():
|
||||
"cumulative_billed_qty", "cumulative_billed_amount",
|
||||
"variation_qty", "variation_amount", "remark"
|
||||
]
|
||||
|
||||
if data.shape[1] == len(expected_columns):
|
||||
data.columns = expected_columns
|
||||
else:
|
||||
data.columns = expected_columns[:data.shape[1]]
|
||||
|
||||
current_main_task_serial = None
|
||||
current_main_task_name = None
|
||||
|
||||
for _, row in data.iterrows():
|
||||
|
||||
task_name = str(row["task_name"]) if row["task_name"] else ""
|
||||
serial_number = str(row["serial_number"]) if row["serial_number"] else None
|
||||
|
||||
if serial_number:
|
||||
current_main_task_serial = serial_number
|
||||
current_main_task_name = task_name
|
||||
parent_id = None
|
||||
else:
|
||||
parent_id = current_main_task_serial
|
||||
|
||||
task = Task(
|
||||
district=work_details_dict.get("district"),
|
||||
block_name=work_details_dict["block"],
|
||||
@@ -104,11 +92,9 @@ def upload_controller():
|
||||
parent_task_name=current_main_task_name if not serial_number else None,
|
||||
remark=row["remark"]
|
||||
)
|
||||
|
||||
db.session.add(task)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
|
||||
log_activity(
|
||||
current_user.username,
|
||||
"Database Insert",
|
||||
@@ -118,97 +104,3 @@ def upload_controller():
|
||||
return redirect(url_for('main.display_tasks'))
|
||||
|
||||
|
||||
# # File upload route
|
||||
# @main.route('/upload', methods=['POST'])
|
||||
# @login_required
|
||||
# def upload():
|
||||
# if 'file' not in request.files:
|
||||
# return "No file part"
|
||||
# file = request.files['file']
|
||||
# if file.filename == '':
|
||||
# return "No selected file"
|
||||
# if file:
|
||||
# filepath = os.path.join(current_app.config['UPLOAD_FOLDER'], file.filename)
|
||||
# file.save(filepath)
|
||||
# log_activity(current_user.username, "File Upload", f"Uploaded file: {file.filename}")
|
||||
|
||||
# # Read work details (first 11 rows)
|
||||
# work_details_data = pd.read_excel(filepath, nrows=11, header=None)
|
||||
# work_details_dict = {
|
||||
|
||||
# "name_of_work": work_details_data.iloc[0, 1],
|
||||
# "cover_agreement_no": work_details_data.iloc[1, 1],
|
||||
# "name_of_contractor": work_details_data.iloc[2, 1],
|
||||
# "name_of_tpi_agency": work_details_data.iloc[3, 1],
|
||||
# "name_of_division": work_details_data.iloc[4, 1],
|
||||
# "name_of_village": work_details_data.iloc[5, 1],
|
||||
# "block": work_details_data.iloc[6, 1],
|
||||
# "scheme_id": work_details_data.iloc[7, 1],
|
||||
# "date_of_billing": work_details_data.iloc[8, 1],
|
||||
# "measurement_book": work_details_data.iloc[9, 1],
|
||||
# "district": work_details_data.iloc[10, 1] # Example: row 11 (index 10), column 2 (index 1)
|
||||
|
||||
# }
|
||||
|
||||
# work_details_dict = {key: (None if pd.isna(value) else value) for key, value in work_details_dict.items()}
|
||||
# work_detail = WorkDetail(**work_details_dict)
|
||||
# db.session.add(work_detail)
|
||||
|
||||
# # Read task data starting from row 12
|
||||
# data = pd.read_excel(filepath, skiprows=10)
|
||||
# data = data.astype(str).replace({"nan": None, "NaT": None, "None": None})
|
||||
|
||||
# expected_columns = [
|
||||
# "serial_number", "task_name", "unit", "qty", "rate", "boq_amount",
|
||||
# "previous_billed_qty", "previous_billing_amount",
|
||||
# "in_this_ra_bill_qty", "in_this_ra_billing_amount",
|
||||
# "cumulative_billed_qty", "cumulative_billed_amount",
|
||||
# "variation_qty", "variation_amount", "remark"
|
||||
# ]
|
||||
|
||||
# if data.shape[1] == len(expected_columns):
|
||||
# data.columns = expected_columns
|
||||
# else:
|
||||
# data.columns = expected_columns[:data.shape[1]] # Truncate
|
||||
|
||||
# current_main_task_serial = None
|
||||
# current_main_task_name = None
|
||||
|
||||
# for _, row in data.iterrows():
|
||||
# task_name = str(row["task_name"]) if row["task_name"] else ""
|
||||
# serial_number = row["serial_number"]
|
||||
|
||||
# if serial_number: # Main task
|
||||
# current_main_task_serial = serial_number
|
||||
# current_main_task_name = task_name
|
||||
# parent_id = None
|
||||
# else: # Subtask
|
||||
# parent_id = current_main_task_serial
|
||||
|
||||
# task = Task(
|
||||
# district=work_details_dict.get("district"),
|
||||
# block_name=work_details_dict["block"],
|
||||
# village_name=work_details_dict["name_of_village"],
|
||||
# serial_number=serial_number,
|
||||
# task_name=task_name,
|
||||
# unit=row["unit"],
|
||||
# qty=row["qty"],
|
||||
# rate=row["rate"],
|
||||
# boq_amount=row["boq_amount"],
|
||||
# previous_billed_qty=row["previous_billed_qty"],
|
||||
# previous_billing_amount=row["previous_billing_amount"],
|
||||
# in_this_ra_bill_qty=row["in_this_ra_bill_qty"],
|
||||
# in_this_ra_billing_amount=row["in_this_ra_billing_amount"],
|
||||
# cumulative_billed_qty=row["cumulative_billed_qty"],
|
||||
# cumulative_billed_amount=row["cumulative_billed_amount"],
|
||||
# variation_qty=row["variation_qty"],
|
||||
# variation_amount=row["variation_amount"],
|
||||
# parent_id=parent_id,
|
||||
# parent_task_name=current_main_task_name if not serial_number else None,
|
||||
# remark=row["remark"]
|
||||
# )
|
||||
# db.session.add(task)
|
||||
|
||||
# db.session.commit()
|
||||
# log_activity(current_user.username, "Database Insert", f"Inserted work details and tasks from {file.filename}")
|
||||
# return redirect(url_for('main.display_tasks'))
|
||||
Reference in New Issue
Block a user