-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
1938 lines (1609 loc) · 71 KB
/
app.py
File metadata and controls
1938 lines (1609 loc) · 71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, jsonify, request, redirect, url_for, session
import sqlite3
import os
import time
from werkzeug.utils import secure_filename
from models.branch_model import create_tables
from services.distance_service import get_distance_matrix
from services.map_service import generate_map
from config import DB_PATH, GOOGLE_MAPS_API_KEY, SECRET_KEY
from services.tsp_solver import optimize_daily_route
import re
import time
MAX_DISTANCE_PER_DAY = 180_000 # 180 km in meters
app = Flask(__name__)
app.secret_key = SECRET_KEY
# Global variable to store last route data
last_route_data = None
def store_last_route(route_data):
"""Store the last route data globally"""
global last_route_data
last_route_data = route_data
def get_last_route():
"""Get the last stored route data"""
global last_route_data
return last_route_data
# Try to initialize DB at import time
_DB_INIT_DONE = False
try:
create_tables()
_DB_INIT_DONE = True
except Exception as e:
print(f"⚠️ DB init at import warning: {e}")
@app.before_request
def _ensure_db_initialized_guard():
global _DB_INIT_DONE
if not _DB_INIT_DONE:
try:
create_tables()
_DB_INIT_DONE = True
except Exception as e:
print(f"⚠️ DB init (before_request) warning: {e}")
def get_branches():
"""Get all branches from database"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
SELECT id, name, address, lat, lng, is_hq, visited
FROM branches
ORDER BY is_hq DESC, name
""")
branches = cursor.fetchall()
conn.close()
return branches
def get_non_hq_branches():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("SELECT id, name FROM branches WHERE is_hq = 0 ORDER BY name")
rows = cursor.fetchall()
conn.close()
return rows
# --------------- Auth Helpers ---------------
import hashlib
def hash_password(pw: str) -> str:
return hashlib.sha256(pw.encode("utf-8")).hexdigest()
def current_user():
return session.get("user")
def require_role(*roles):
user = current_user()
if not user or user.get("role") not in roles:
return False
return True
def get_db():
return sqlite3.connect(DB_PATH)
def mark_branch_visited(branch_id):
"""Mark a branch as visited"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("UPDATE branches SET visited = 1 WHERE id = ?", (branch_id,))
conn.commit()
conn.close()
def reset_all_branches():
"""Reset all branches to unvisited before planning"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("UPDATE branches SET visited = 0 WHERE is_hq = 0")
conn.commit()
conn.close()
print("🔄 All branches reset to unvisited")
def ensure_branch_manager_columns():
"""Ensure branch_managers table exists and has required columns (password_hash)."""
try:
conn = get_db()
cur = conn.cursor()
# Make sure tables exist first
try:
create_tables()
except Exception:
pass
cur.execute("PRAGMA table_info(branch_managers)")
cols = [r[1] for r in cur.fetchall()]
if len(cols) == 0:
# Table missing; create via create_tables()
try:
create_tables()
except Exception:
pass
else:
if "password_hash" not in cols:
cur.execute("ALTER TABLE branch_managers ADD COLUMN password_hash TEXT DEFAULT ''")
conn.commit()
if "approved" not in cols:
cur.execute("ALTER TABLE branch_managers ADD COLUMN approved INTEGER DEFAULT 0")
conn.commit()
conn.close()
except Exception as e:
try:
conn.close()
except Exception:
pass
print(f"ensure_branch_manager_columns warning: {e}")
def plan_single_day(branches, distance_matrix, time_matrix, use_tsp_optimization=True):
"""
Plan a single day route visiting as many unvisited branches as possible within 180km
"""
hq_index = next(i for i, b in enumerate(branches) if b[5] == 1)
unvisited = set(i for i, b in enumerate(branches) if b[5] == 0 and (len(b) <= 6 or b[6] == 0))
if not unvisited:
return None # No unvisited branches
day_route = [hq_index] # Start at HQ
day_distance = 0
day_branches_visited = []
print(f"Planning single day route with max {MAX_DISTANCE_PER_DAY/1000}km")
print(f"HQ at index {hq_index}, {len(unvisited)} unvisited branches available")
# Keep adding branches until we can't fit any more
added_branch = True
while added_branch and unvisited:
added_branch = False
current_position = day_route[-1]
best_branch = None
min_distance = float('inf')
# Try each unvisited branch
for branch_idx in unvisited:
# Distance from current position to this branch
leg_distance = distance_matrix[current_position][branch_idx]
# Distance from this branch back to HQ (for return trip)
return_distance = distance_matrix[branch_idx][hq_index]
# Total distance if we add this branch and return to HQ
total_with_this_branch = day_distance + leg_distance + return_distance
#print(f" Branch {branch_idx} ({branches[branch_idx][1]}): "
#f"current {day_distance/1000:.1f}km + leg {leg_distance/1000:.1f}km + return {return_distance/1000:.1f}km = {total_with_this_branch/1000:.1f}km")
# Check if this branch fits within the daily limit
if total_with_this_branch <= MAX_DISTANCE_PER_DAY:
# Among feasible branches, pick the nearest one (greedy)
if leg_distance < min_distance:
min_distance = leg_distance
best_branch = branch_idx
added_branch = True
# Add the best branch if found
if best_branch is not None:
day_route.append(best_branch)
day_distance += min_distance # Add only the leg distance for now
unvisited.remove(best_branch)
day_branches_visited.append(best_branch)
#print(f"✅ Added branch {best_branch} ({branches[best_branch][1]})")
#print(f" Running distance: {day_distance/1000:.1f}km")
else:
print(f"❌ No more branches can fit within {MAX_DISTANCE_PER_DAY/1000}km limit")
# Complete the day by returning to HQ
if len(day_route) > 1: # Only if we visited at least one branch
final_return_distance = distance_matrix[day_route[-1]][hq_index]
day_route.append(hq_index)
day_distance += final_return_distance
#print(f"🏠 Return to HQ: +{final_return_distance/1000:.1f}km")
#print(f"📊 Final distance: {day_distance/1000:.1f}km")
#print(f"📍 Visited {len(day_branches_visited)} branches: {[branches[i][1] for i in day_branches_visited]}")
#print(f"🗺️ Route: {' → '.join([branches[i][1] for i in day_route])}")
# Optimize route order with TSP if requested and beneficial
if use_tsp_optimization and len(day_branches_visited) > 2:
#print(f"🔄 Optimizing route order with TSP...")
try:
optimized_route = optimize_daily_route(
distance_matrix,
day_branches_visited,
hq_index,
MAX_DISTANCE_PER_DAY
)
if optimized_route and len(optimized_route) >= len(day_route):
# Calculate optimized distance
opt_distance = 0
for i in range(len(optimized_route) - 1):
opt_distance += distance_matrix[optimized_route[i]][optimized_route[i + 1]]
print(f" TSP distance: {opt_distance/1000:.1f}km vs original {day_distance/1000:.1f}km")
if opt_distance <= MAX_DISTANCE_PER_DAY and opt_distance < day_distance:
day_route = optimized_route
day_distance = opt_distance
#print(f"✅ Using TSP optimized route (saved {(day_distance - opt_distance)/1000:.1f}km)")
print(f"🗺️ Optimized: {' → '.join([branches[i][1] for i in day_route])}")
else:
print(f"➡️ Keeping original route (TSP didn't improve or exceeded limit)")
except Exception as e:
print(f"⚠️ TSP optimization failed: {e}")
# Don't automatically mark branches as visited - let user confirm them
# for branch_idx in day_branches_visited:
# mark_branch_visited(branches[branch_idx][0])
return day_route
else:
print(f"⚠️ No branches could be visited")
return None
def plan_multi_day(branches, distance_matrix, time_matrix, use_tsp_optimization=True):
"""
Plan multi-day routes visiting as many branches as possible within 180km per day
"""
days = []
hq_index = next(i for i, b in enumerate(branches) if b[5] == 1)
unvisited = set(i for i, b in enumerate(branches) if b[5] == 0)
day_count = 1
print(f"Planning routes with max {MAX_DISTANCE_PER_DAY/1000}km per day")
print(f"HQ at index {hq_index}, {len(unvisited)} branches available")
print(f"Goal: Visit as many branches as possible within distance limit")
while unvisited:
day_route = [hq_index] # Start at HQ
day_distance = 0
day_branches_visited = []
print(f"\n--- Planning Day {day_count} ---")
print(f"Available branches: {len(unvisited)}")
# Keep adding branches until we can't fit any more
added_branch = True
while added_branch and unvisited:
added_branch = False
current_position = day_route[-1]
best_branch = None
min_distance = float('inf')
# Try each unvisited branch
for branch_idx in unvisited:
# Distance from current position to this branch
leg_distance = distance_matrix[current_position][branch_idx]
# Distance from this branch back to HQ (for return trip)
return_distance = distance_matrix[branch_idx][hq_index]
# Total distance if we add this branch and return to HQ
total_with_this_branch = day_distance + leg_distance + return_distance
print(f" Branch {branch_idx} ({branches[branch_idx][1]}): "
f"current {day_distance/1000:.1f}km + leg {leg_distance/1000:.1f}km + return {return_distance/1000:.1f}km = {total_with_this_branch/1000:.1f}km")
# Check if this branch fits within the daily limit
if total_with_this_branch <= MAX_DISTANCE_PER_DAY:
# Among feasible branches, pick the nearest one (greedy)
if leg_distance < min_distance:
min_distance = leg_distance
best_branch = branch_idx
added_branch = True
# Add the best branch if found
if best_branch is not None:
day_route.append(best_branch)
day_distance += min_distance # Add only the leg distance for now
unvisited.remove(best_branch)
day_branches_visited.append(best_branch)
print(f" ✅ Added branch {best_branch} ({branches[best_branch][1]})")
print(f" Running distance: {day_distance/1000:.1f}km")
else:
print(f" ❌ No more branches can fit within {MAX_DISTANCE_PER_DAY/1000}km limit")
# Complete the day by returning to HQ
if len(day_route) > 1: # Only if we visited at least one branch
final_return_distance = distance_matrix[day_route[-1]][hq_index]
day_route.append(hq_index)
day_distance += final_return_distance
print(f" 🏠 Return to HQ: +{final_return_distance/1000:.1f}km")
print(f" 📊 Day {day_count} final distance: {day_distance/1000:.1f}km")
print(f" 📍 Visited {len(day_branches_visited)} branches: {[branches[i][1] for i in day_branches_visited]}")
print(f" 🗺️ Route: {' → '.join([branches[i][1] for i in day_route])}")
# Optimize route order with TSP if requested and beneficial
if use_tsp_optimization and len(day_branches_visited) > 2:
print(f" 🔄 Optimizing route order with TSP...")
try:
optimized_route = optimize_daily_route(
distance_matrix,
day_branches_visited,
hq_index,
MAX_DISTANCE_PER_DAY
)
if optimized_route and len(optimized_route) >= len(day_route):
# Calculate optimized distance
opt_distance = 0
for i in range(len(optimized_route) - 1):
opt_distance += distance_matrix[optimized_route[i]][optimized_route[i + 1]]
print(f" TSP distance: {opt_distance/1000:.1f}km vs original {day_distance/1000:.1f}km")
if opt_distance <= MAX_DISTANCE_PER_DAY and opt_distance < day_distance:
day_route = optimized_route
day_distance = opt_distance
print(f" ✅ Using TSP optimized route (saved {(day_distance - opt_distance)/1000:.1f}km)")
print(f" 🗺️ Optimized: {' → '.join([branches[i][1] for i in day_route])}")
else:
print(f" ➡️ Keeping original route (TSP didn't improve or exceeded limit)")
except Exception as e:
print(f" ⚠️ TSP optimization failed: {e}")
days.append(day_route)
# Don't automatically mark branches as visited - let user confirm them
# for branch_idx in day_branches_visited:
# mark_branch_visited(branches[branch_idx][0])
else:
print(f" ⚠️ No branches could be visited on Day {day_count}")
break # No progress possible
day_count += 1
# Safety check
if day_count > 10:
print("⚠️ Safety limit: stopping after 10 days")
break
# Show final summary
total_branches_visited = sum(len([i for i in route if branches[i][5] == 0]) for route in days)
total_branches_available = len([b for b in branches if b[5] == 0])
print(f"\n🎉 Planning completed!")
print(f"📊 Generated {len(days)} days of routes")
print(f"📍 Visited {total_branches_visited} out of {total_branches_available} branches")
if total_branches_visited < total_branches_available:
remaining = [i for i in range(len(branches)) if branches[i][5] == 0 and i in unvisited]
print(f"⚠️ Remaining unvisited branches: {[branches[i][1] for i in remaining]}")
return days
def debug_distance_matrix(branches, distance_matrix):
'''"""Print distance matrix for debugging"""
n = len(branches)
print(f"\n📊 Distance Matrix ({n}x{n}) in km:")
# Print header
print("From\\To ", end="")
for j in range(min(n, 8)): # Limit to first 8 to avoid clutter
print(f"{j:6}", end="")
if n > 8:
print(" ...")
else:
print()
# Print rows
for i in range(min(n, 8)):
branch_name = branches[i][1][:8] if len(branches[i][1]) > 8 else branches[i][1]
print(f"{i}({branch_name:8})", end="")
for j in range(min(n, 8)):
if distance_matrix[i][j] == 999999:
print(" ∞ ", end="")
else:
dist_km = distance_matrix[i][j] / 1000
print(f"{dist_km:6.1f}", end="")
if n > 8:
print(" ...")
else:
print()
if n > 8:
print(" ⋮")'''
# ------------------ Flask Routes ------------------
@app.route("/")
def index():
# If not logged in, redirect to explicit /login page
user = current_user()
if not user:
return redirect(url_for("login"))
# Route users to their appropriate home pages
role = user.get("role")
if role == "admin":
return redirect(url_for("admin_dashboard"))
if role == "manager":
return redirect(url_for("manager_dashboard"))
# default auditor landing
return render_template("index.html", user=user)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "GET":
return render_template("login.html")
# Ensure tables exist before attempting to query
try:
create_tables()
except Exception as e:
print(f"⚠️ DB init during login warning: {e}")
username = request.form.get("username", "").strip()
password = request.form.get("password", "")
role = request.form.get("role", "auditor") # 'admin' or 'auditor' or 'manager'
if role == "manager":
ensure_branch_manager_columns()
conn = get_db()
cur = conn.cursor()
# Query appropriate table and include 'active' for auditors
if role == "admin":
table = "admins"
cur.execute("SELECT id, username, password_hash FROM admins WHERE username = ?", (username,))
elif role == "manager":
table = "branch_managers"
# For managers, use contact_no as the login identifier (entered in the username field)
ensure_branch_manager_columns()
# Now authenticate managers by their Name (case-insensitive)
cur.execute(
"SELECT id, name, contact_no, branch_id, password_hash, approved "
"FROM branch_managers WHERE name = ? COLLATE NOCASE",
(username,)
)
else:
table = "auditors"
cur.execute("SELECT id, username, password_hash, active FROM auditors WHERE username = ?", (username,))
row = cur.fetchone()
conn.close()
if not row:
return render_template("login.html", error="Invalid credentials")
# Determine password hash index based on role query shape
if role == "manager":
pw_hash = row[4]
else:
pw_hash = row[2]
if pw_hash != hash_password(password):
return render_template("login.html", error="Invalid credentials")
# if auditor, check active flag
if table == "auditors" and (len(row) < 4 or row[3] != 1):
return render_template("login.html", error="Auditor is inactive")
# if manager, require approved flag
if table == "branch_managers":
approved = 0 if len(row) < 6 else (row[5] or 0)
if approved != 1:
return render_template("login.html", error="Manager is pending approval")
# Build session payload per role
if role == "manager":
session["user"] = {
"id": row[0],
"username": row[1], # manager name
"contact_no": row[2],
"branch_id": row[3],
"role": "manager",
}
return redirect(url_for("manager_dashboard"))
else:
session["user"] = {"id": row[0], "username": row[1], "role": role}
if role == "admin":
return redirect(url_for("admin_dashboard"))
return redirect(url_for("index"))
# ----- Branch Manager Registration -----
@app.route("/register_manager", methods=["GET"])
def register_manager_page():
# Publicly accessible to allow managers to register
branches = get_non_hq_branches()
return render_template("register_manager.html", branches=branches)
@app.route("/register_manager", methods=["POST"])
def register_manager_submit():
try:
name = request.form.get("name", "").strip()
contact_no = request.form.get("contact_no", "").strip()
branch_id = request.form.get("branch_id", "").strip()
password = request.form.get("password", "")
if not name or not contact_no or not branch_id:
return render_template("register_manager.html", error="All fields are required", branches=get_non_hq_branches())
if not password:
return render_template("register_manager.html", error="Password is required", branches=get_non_hq_branches())
ensure_branch_manager_columns()
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
# New or update: set password; keep approved default 0 on (re)registration
cur.execute(
"INSERT OR REPLACE INTO branch_managers (id, name, contact_no, branch_id, password_hash, approved) "
"VALUES ((SELECT id FROM branch_managers WHERE branch_id = ?), ?, ?, ?, ?, 0)",
(int(branch_id), name, contact_no, int(branch_id), hash_password(password))
)
conn.commit()
conn.close()
return render_template("register_manager.html", success=True, branches=get_non_hq_branches())
except Exception as e:
return render_template("register_manager.html", error=str(e), branches=get_non_hq_branches())
# ------------- Branch Manager Dashboard -------------
@app.route("/manager", methods=["GET"])
def manager_dashboard():
if not require_role("manager"):
return redirect(url_for("login"))
# Load assigned branch details
user = current_user()
branch = None
try:
conn = get_db()
cur = conn.cursor()
cur.execute("SELECT id, name, address, lat, lng, visited FROM branches WHERE id = ?", (user.get("branch_id"),))
branch = cur.fetchone()
conn.close()
except Exception:
branch = None
return render_template("manager/dashboard.html", user=user, branch=branch)
@app.route("/manager/mark-visited", methods=["POST"])
def manager_mark_visited():
if not require_role("manager"):
return redirect(url_for("login"))
user = current_user()
try:
conn = get_db()
cur = conn.cursor()
cur.execute("UPDATE branches SET visited = 1 WHERE id = ?", (user.get("branch_id"),))
conn.commit()
conn.close()
except Exception as e:
print(f"Manager mark visited failed: {e}")
return redirect(url_for("manager_dashboard"))
@app.route('/manager/profile', methods=['GET'])
def manager_view_profile():
if not require_role("manager"):
return redirect(url_for("login"))
user = current_user()
# Load branch info
branch = None
try:
conn = get_db()
cur = conn.cursor()
cur.execute("SELECT id, name, address, lat, lng, visited FROM branches WHERE id = ?", (user.get("branch_id"),))
branch = cur.fetchone()
conn.close()
except Exception:
branch = None
return render_template('manager/viewprofile.html', user=user, branch=branch)
@app.route('/manager/branch/edit', methods=['GET'])
def manager_branch_edit_page():
if not require_role("manager"):
return redirect(url_for("login"))
user = current_user()
branch = None
try:
conn = get_db()
cur = conn.cursor()
cur.execute("SELECT id, name, address, lat, lng FROM branches WHERE id = ?", (user.get("branch_id"),))
branch = cur.fetchone()
conn.close()
except Exception:
branch = None
return render_template('manager/edit_branchdetails.html', user=user, branch=branch)
@app.route('/manager/branch/update', methods=['POST'])
def manager_branch_update():
if not require_role("manager"):
return redirect(url_for("login"))
user = current_user()
name = (request.form.get('name') or '').strip()
address = (request.form.get('address') or '').strip()
lat = request.form.get('lat')
lng = request.form.get('lng')
errors = []
# Validate
if not name:
errors.append('Branch name is required')
try:
lat_val = float(lat)
lng_val = float(lng)
except Exception:
errors.append('Latitude and Longitude must be valid numbers')
lat_val = None
lng_val = None
if errors:
# Reload a page with errors - default send back to edit page
try:
conn = get_db()
cur = conn.cursor()
cur.execute("SELECT id, name, address, lat, lng FROM branches WHERE id = ?", (user.get("branch_id"),))
branch = cur.fetchone()
conn.close()
except Exception:
branch = None
return render_template('manager/edit_branchdetails.html', user=user, branch=branch, error='; '.join(errors))
# Persist
try:
conn = get_db()
cur = conn.cursor()
cur.execute(
"UPDATE branches SET name = ?, address = ?, lat = ?, lng = ? WHERE id = ?",
(name, address, lat_val, lng_val, user.get('branch_id'))
)
conn.commit()
conn.close()
except Exception as e:
return render_template('manager/edit_branchdetails.html', user=user, branch=(user.get('branch_id'), name, address, lat, lng), error=str(e))
# Redirect back to dashboard after update
return redirect(url_for('manager_dashboard'))
@app.route("/api/branches/list", methods=["GET"])
def api_branches_list():
try:
rows = get_non_hq_branches()
return jsonify({"success": True, "items": [{"id": r[0], "name": r[1]} for r in rows]})
except Exception as e:
return jsonify({"success": False, "error": str(e)})
@app.route("/logout")
def logout():
session.clear()
return redirect(url_for("login"))
@app.route("/admin", methods=["GET"])
def admin_dashboard():
if not require_role("admin"):
return redirect(url_for("login"))
return render_template("admin.html", user=current_user())
@app.route("/admin/register-auditor", methods=["POST"])
def admin_register_auditor():
if not require_role("admin"):
return jsonify({"success": False, "error": "Unauthorized"}), 401
data = request.get_json() or {}
username = data.get("username", "").strip()
password = data.get("password", "")
if not username or not password:
return jsonify({"success": False, "error": "Username and password required"}), 400
conn = get_db()
cur = conn.cursor()
try:
cur.execute(
"INSERT INTO auditors (username, password_hash, created_by_admin_id) VALUES (?, ?, ?)",
(username, hash_password(password), current_user()["id"]),
)
conn.commit()
return jsonify({"success": True, "message": f"Auditor '{username}' created"})
except sqlite3.IntegrityError:
return jsonify({"success": False, "error": "Username already exists"}), 409
finally:
conn.close()
# New endpoint: admin can add branches
@app.route("/admin/add-branch", methods=["POST"])
def admin_add_branch():
if not require_role("admin"):
return jsonify({"success": False, "error": "Unauthorized"}), 401
try:
data = request.get_json() or {}
name = (data.get("name") or "").strip()
address = (data.get("address") or "").strip()
lat = data.get("lat")
lng = data.get("lng")
is_hq = int(data.get("is_hq") or 0)
if not name:
return jsonify({"success": False, "error": "Branch name is required"}), 400
if lat is None or lng is None:
return jsonify({"success": False, "error": "Latitude and longitude required"}), 400
# Validate numeric coordinates
try:
lat = float(lat)
lng = float(lng)
except ValueError:
return jsonify({"success": False, "error": "Invalid latitude or longitude"}), 400
conn = get_db()
cur = conn.cursor()
cur.execute("""
INSERT INTO branches (name, address, lat, lng, is_hq, visited)
VALUES (?, ?, ?, ?, ?, 0)
""", (name, address, lat, lng, is_hq))
conn.commit()
conn.close()
return jsonify({"success": True, "message": f"Branch '{name}' added"})
except Exception as e:
return jsonify({"success": False, "error": str(e)}), 500
@app.route("/admin/delete-branch/<int:branch_id>", methods=["DELETE"])
def admin_delete_branch(branch_id):
if not require_role("admin"):
return jsonify({"success": False, "error": "Unauthorized"}), 401
try:
conn = get_db()
cur = conn.cursor()
# First, check if the branch exists
cur.execute("SELECT name, is_hq FROM branches WHERE id = ?", (branch_id,))
branch = cur.fetchone()
if not branch:
return jsonify({"success": False, "error": "Branch not found"}), 404
branch_name, is_hq = branch[0], branch[1]
# Prevent deletion of HQ if it's the only HQ
if is_hq:
cur.execute("SELECT COUNT(*) FROM branches WHERE is_hq = 1")
hq_count = cur.fetchone()[0]
if hq_count <= 1:
return jsonify({"success": False, "error": "Cannot delete the only headquarters"}), 400
# Delete the branch
cur.execute("DELETE FROM branches WHERE id = ?", (branch_id,))
if cur.rowcount == 0:
return jsonify({"success": False, "error": "Branch not found or already deleted"}), 404
conn.commit()
conn.close()
return jsonify({"success": True, "message": f"Branch '{branch_name}' deleted successfully"})
except Exception as e:
return jsonify({"success": False, "error": str(e)}), 500
@app.route("/api/auditors", methods=["GET"])
def api_list_auditors():
if not require_role("admin"):
return jsonify({"success": False, "error": "Unauthorized"}), 401
try:
conn = get_db()
cur = conn.cursor()
cur.execute("SELECT id, username, active, created_at FROM auditors ORDER BY username")
rows = cur.fetchall()
conn.close()
items = [
{"id": r[0], "username": r[1], "active": r[2], "created_at": r[3]}
for r in rows
]
return jsonify({"success": True, "items": items})
except Exception as e:
return jsonify({"success": False, "error": str(e)})
@app.route("/api/plan", methods=["POST"])
def api_plan_single_day():
"""Plan a single day route"""
try:
# auditors and admins can plan
if not require_role("auditor", "admin"):
return jsonify({"success": False, "error": "Unauthorized"}), 401
print("🚀 Starting single day route planning...")
create_tables()
# Get all branches (don't reset - we want to track visited ones)
branches = get_branches()
if not branches:
print("❌ No branches found in database")
return jsonify({"error": "No branches found in database."})
print(f"📍 Found {len(branches)} branches:")
hq_count = sum(1 for b in branches if b[5] == 1)
branch_count = sum(1 for b in branches if b[5] == 0)
unvisited_count = sum(1 for b in branches if b[5] == 0 and (len(b) <= 6 or b[6] == 0))
for i, branch in enumerate(branches):
visited_status = ""
if len(branch) > 6 and branch[6] == 1:
visited_status = " (visited)"
branch_type = "HQ" if branch[5] == 1 else f"Branch{visited_status}"
print(f" {i}: {branch[1]} ({branch_type}) at ({branch[3]:.4f}, {branch[4]:.4f})")
print(f" Summary: {hq_count} HQ, {unvisited_count} unvisited branches")
if hq_count != 1:
return jsonify({"error": f"Expected exactly 1 HQ, found {hq_count}"})
if unvisited_count == 0:
return jsonify({"error": "All branches have been visited", "all_completed": True})
# Get distance matrix
print(f"\n🗺️ Fetching distance matrix for {len(branches)} locations...")
coords = [(b[3], b[4]) for b in branches]
distance_matrix, time_matrix = get_distance_matrix(coords)
# Validate distance matrix
if not distance_matrix:
return jsonify({"error": "Failed to get distance matrix from Google API"})
if len(distance_matrix) != len(branches):
return jsonify({"error": f"Distance matrix size mismatch: {len(distance_matrix)} vs {len(branches)}"})
# Debug distance matrix
debug_distance_matrix(branches, distance_matrix)
# Plan single day route
print(f"\n🗓️ Planning single day route...")
day_route = plan_single_day(branches, distance_matrix, time_matrix)
if not day_route:
return jsonify({"error": "No route could be generated within distance constraints"})
# Generate map for single day
print(f"\n🗺️ Generating map...")
try:
generate_map(branches, [day_route], GOOGLE_MAPS_API_KEY)
print("✅ Map generated successfully")
except Exception as map_error:
print(f"⚠️ Map generation failed: {map_error}")
# Build JSON response
print(f"\n📋 Building response...")
total_dist = 0
stops = []
for k in range(len(day_route) - 1):
i, j = day_route[k], day_route[k + 1]
leg_distance = distance_matrix[i][j]
total_dist += leg_distance
stops.append({
"name": branches[i][1],
"address": branches[i][2],
"index": i,
"lat": branches[i][3],
"lng": branches[i][4]
})
# Add final stop
final_idx = day_route[-1]
stops.append({
"name": branches[final_idx][1],
"address": branches[final_idx][2],
"index": final_idx,
"lat": branches[final_idx][3],
"lng": branches[final_idx][4]
})
branch_count_this_day = len([i for i in day_route if branches[i][5] == 0])
remaining_branches = sum(1 for b in branches if b[5] == 0 and (len(b) <= 6 or b[6] == 0))
# Get branches that will be visited (excluding HQ)
visited_branches = []
for i in day_route:
if branches[i][5] == 0: # Not HQ
visited_branches.append({
"id": branches[i][0],
"name": branches[i][1],
"address": branches[i][2],
"lat": branches[i][3],
"lng": branches[i][4]
})
result = {
"day": 1,
"distance_m": total_dist,
"distance_km": round(total_dist/1000, 2),
"branches_visited": branch_count_this_day,
"remaining_branches": remaining_branches,
"stops": stops,
"route_indices": day_route,
"visited_branches": visited_branches
}
# Store the route data for future retrieval
route_data = {
"type": "single_day",
"day_route": result,
"has_more_branches": remaining_branches > 0
}
store_last_route(route_data)
print(f"✅ Single day planning completed: {branch_count_this_day} branches, {len(stops)} stops, {total_dist/1000:.1f}km")
print(f"📊 Remaining branches: {remaining_branches}")
return jsonify({"day_route": result, "success": True, "has_more_branches": remaining_branches > 0})
except Exception as e:
print(f"\n❌ Error in api_plan_single_day: {e}")
import traceback
traceback.print_exc()
return jsonify({"error": f"Planning failed: {str(e)}", "success": False})
@app.route("/api/plan-multi", methods=["POST"])
def api_plan_multi_day():
"""Plan all remaining days at once (original behavior)"""
try:
if not require_role("auditor", "admin"):
return jsonify({"success": False, "error": "Unauthorized"}), 401
print("🚀 Starting multi-day route planning...")
create_tables()
# Reset all branches to unvisited before planning
reset_all_branches()
branches = get_branches()
if not branches:
print("❌ No branches found in database")
return jsonify({"error": "No branches found in database."})
print(f"📍 Found {len(branches)} branches:")
hq_count = sum(1 for b in branches if b[5] == 1)
branch_count = sum(1 for b in branches if b[5] == 0)
for i, branch in enumerate(branches):
branch_type = "HQ" if branch[5] == 1 else "Branch"
print(f" {i}: {branch[1]} ({branch_type}) at ({branch[3]:.4f}, {branch[4]:.4f})")
print(f" Summary: {hq_count} HQ, {branch_count} branches")
if hq_count != 1:
return jsonify({"error": f"Expected exactly 1 HQ, found {hq_count}"})
# Get distance matrix
print(f"\n🗺️ Fetching distance matrix for {len(branches)} locations...")
coords = [(b[3], b[4]) for b in branches]
distance_matrix, time_matrix = get_distance_matrix(coords)
# Validate distance matrix
if not distance_matrix:
return jsonify({"error": "Failed to get distance matrix from Google API"})
if len(distance_matrix) != len(branches):
return jsonify({"error": f"Distance matrix size mismatch: {len(distance_matrix)} vs {len(branches)}"})
# Debug distance matrix
debug_distance_matrix(branches, distance_matrix)
# Plan multi-day routes
print(f"\n🗓️ Planning multi-day routes...")
days = plan_multi_day(branches, distance_matrix, time_matrix)