updated code
This commit is contained in:
@@ -29,6 +29,54 @@ def recalc_task(task):
|
||||
|
||||
|
||||
|
||||
# def update_tasks_controller():
|
||||
# try:
|
||||
# updates = request.get_json()
|
||||
# update_count = 0
|
||||
|
||||
# formula_fields = [
|
||||
# "previous_billing_amount",
|
||||
# "in_this_ra_billing_amount",
|
||||
# "cumulative_billed_qty",
|
||||
# "cumulative_billed_amount",
|
||||
# "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
|
||||
|
||||
|
||||
|
||||
def update_tasks_controller():
|
||||
try:
|
||||
updates = request.get_json()
|
||||
@@ -42,39 +90,72 @@ def update_tasks_controller():
|
||||
"variation_qty",
|
||||
"variation_amount"
|
||||
]
|
||||
|
||||
numeric_fields = [
|
||||
"qty","rate","boq_amount",
|
||||
"previous_billed_qty",
|
||||
"in_this_ra_bill_qty"
|
||||
]
|
||||
|
||||
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:
|
||||
|
||||
# Skip formula fields
|
||||
if field_name in formula_fields:
|
||||
continue
|
||||
|
||||
current_value = getattr(task, field_name, None)
|
||||
|
||||
# Convert numeric fields
|
||||
if field_name in numeric_fields:
|
||||
try:
|
||||
new_value = float(new_value) if new_value != "" else 0
|
||||
except:
|
||||
new_value = 0
|
||||
|
||||
# Update only if changed
|
||||
if str(current_value) != str(new_value):
|
||||
setattr(task, field_name, new_value)
|
||||
|
||||
# Recalculate formulas
|
||||
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
|
||||
|
||||
|
||||
def display_tasks_controller():
|
||||
work_details = WorkDetail.query.order_by(
|
||||
WorkDetail.uploaded_at.desc()
|
||||
|
||||
Reference in New Issue
Block a user