-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuc_stock_wa.v1.2.1.py
1284 lines (1056 loc) · 49.8 KB
/
euc_stock_wa.v1.2.1.py
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
import logging.config
from pathlib import Path
from tkinter import Menu
import customtkinter as ctk
import os
import tkinter as tk
from tkinter import ttk
from openpyxl import load_workbook, Workbook
from datetime import datetime
import subprocess
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd # Ensure pandas is imported for date operations
# Function to save the workbook path to config.py
def save_config(workbook_path):
with open('config.py', 'w') as config_file:
config_file.write(f"workbook_path = r'{workbook_path}'\n")
logging_conf_path = Path('logging.conf')
if logging_conf_path.exists() and logging_conf_path.stat().st_size > 0:
try:
logging.config.fileConfig(logging_conf_path)
except Exception as e:
logging.error(f"Error configuring logging: {e}", exc_info=True)
else:
logging.basicConfig(level=logging.DEBUG)
def run_inventory_script():
script_path = script_directory / "inventory-levels_4.2v2.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_4.2v2.py' does not exist in the directory.")
def run_build_room_inventory_script():
script_path = script_directory / "inventory-levels_BRv2.2.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_BRv2.2.py' does not exist in the directory.")
def run_darwin_inventory_script():
script_path = script_directory / "inventory-levels_darwinv1.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_darwinv1.py' does not exist in the directory.")
def run_combined_rooms_inventory_script():
script_path = script_directory / "inventory-levels_combinedv1.4.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_combinedv1.4.py' does not exist in the directory.")
def open_spreadsheet():
try:
if os.name == 'nt':
os.startfile(workbook_path)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.run([opener, workbook_path])
except Exception as e:
tk.messagebox.showerror("Error", f"Failed to open the spreadsheet: {e}")
def update_all_sans_location():
"""
Updates the 'Location' column in the 'All_SANs' sheet based on the presence of SANs
in specific timestamp sheets.
"""
# Ensure the 'All_SANs' sheet exists
if 'All_SANs' not in workbook.sheetnames:
tk.messagebox.showerror("Error", "'All_SANs' sheet not found in the workbook.")
return
all_sans_sheet = workbook['All_SANs']
# Ensure the Location column (D) has a header
if all_sans_sheet.max_column < 4:
all_sans_sheet.cell(row=1, column=4, value="Location")
# Mapping of sheets to short locations
location_mapping = {
'4.2_Timestamps': '4.2',
'BR_Timestamps': 'BR',
'Darwin_Timestamps': 'Darwin'
}
# Iterate over rows in All_SANs and update the location
for row_idx, row in enumerate(all_sans_sheet.iter_rows(min_row=2, values_only=True), start=2):
san_number = row[0]
location = None
# Check each timestamp sheet for the SAN
for sheet_name, loc in location_mapping.items():
if sheet_name in workbook.sheetnames:
timestamp_sheet = workbook[sheet_name]
if any(san_number == cell[0] for cell in timestamp_sheet.iter_rows(min_row=2, min_col=1, max_col=1, values_only=True)):
location = loc
break
# Write the location to the Location column (Column D)
all_sans_sheet.cell(row=row_idx, column=4, value=location)
# Save the workbook after updating
workbook.save(workbook_path)
def view_all_sans_log():
"""
Displays the updated All_SANs data in a Treeview widget with columns
'SAN Number', 'Item', 'Time', and 'Location'.
Includes a search box to filter SAN numbers dynamically.
"""
# Update the 'Location' column in the 'All_SANs' sheet
update_all_sans_location()
log_window = tk.Toplevel(root)
log_window.title("SANs In Stock")
log_window.geometry("800x800")
# Search box frame
search_frame = tk.Frame(log_window)
search_frame.pack(fill="x", padx=10, pady=5)
tk.Label(search_frame, text="Search:").pack(side="left", padx=5)
search_var = tk.StringVar()
search_entry = ttk.Entry(search_frame, textvariable=search_var)
search_entry.pack(side="left", fill="x", expand=True, padx=5)
# Treeview and scrollbar
columns = ("SAN Number", "Item", "Time", "Location")
log_tree = ttk.Treeview(log_window, columns=columns, show="headings")
for col in columns:
log_tree.heading(col, text=col)
log_tree.column(col, anchor="w")
log_tree.pack(expand=True, fill="both", padx=10, pady=10)
scrollbar = ttk.Scrollbar(log_window, orient="vertical", command=log_tree.yview)
scrollbar.pack(side="right", fill="y")
log_tree.configure(yscrollcommand=scrollbar.set)
# Load data from the "All_SANs" sheet
def load_data(filter_text=""):
"""
Populates the Treeview with data, filtering by filter_text.
"""
log_tree.delete(*log_tree.get_children()) # Clear current data
if 'All_SANs' in workbook.sheetnames:
all_sans_sheet = workbook['All_SANs']
for row in all_sans_sheet.iter_rows(min_row=2, values_only=True):
san_number, item, timestamp, location = row
# Filter by the search text
if filter_text.lower() in str(san_number).lower():
log_tree.insert('', 'end', values=(san_number, item, timestamp, location))
else:
tk.messagebox.showinfo("Info", "'All_SANs' sheet not found or empty.", parent=log_window)
# Trigger search on text change
def on_search(*args):
filter_text = search_var.get()
load_data(filter_text)
search_var.trace("w", on_search) # Bind dynamic updates to search
# Load initial data
load_data()
root = ctk.CTk()
root.title("Perth EUC Stock")
root.geometry("700x850")
# Unified font settings for the application
custom_font = ("Helvetica", 12)
# Custom styles for Treeview
style = ttk.Style()
style.theme_use("clam")
style.configure("Treeview",
font=("Helvetica", 12),
rowheight=25,
background="#f9f9f9",
foreground="#333",
fieldbackground="#f9f9f9")
style.map("Treeview", background=[("selected", "#3399ff")])
menu_bar = tk.Menu(root)
plots_menu = tk.Menu(menu_bar, tearoff=0)
plots_menu.add_command(label="Basement 4.2 Inventory", command=run_inventory_script)
plots_menu.add_command(label="Build Room Inventory", command=run_build_room_inventory_script)
plots_menu.add_command(label="Darwin Inventory", command=run_darwin_inventory_script)
plots_menu.add_command(label="Combined Inventory", command=run_combined_rooms_inventory_script)
plots_menu.add_command(label="SANs In Stock", command=view_all_sans_log)
plots_menu.add_command(label="Open Spreadsheet", command=open_spreadsheet)
# plots_menu.add_command(label="Headsets In Stock", command=view_headsets_log)
menu_bar.add_cascade(label="Data", menu=plots_menu)
root.config(menu=menu_bar)
script_directory = Path(__file__).parent
def get_file_path():
# Creating a temporary root window for file dialog
temp_root = tk.Tk()
temp_root.withdraw() # Hide the temporary root window
file_path = filedialog.askopenfilename(
title="Select a spreadsheet file",
filetypes=(("Excel files", "*.xlsx"), ("All files", "*.*"))
)
temp_root.destroy() # Destroy the temporary root window to clean up
if not file_path:
tk.messagebox.showerror("Error", "No file selected. Exiting application.")
raise SystemExit # Exit the application if no file is selected
return file_path
# Get the workbook path from user
workbook_path = get_file_path()
save_config(workbook_path) # Save the path to the config file immediately after getting it
workbook = load_workbook(workbook_path)
all_sans_sheet = workbook['All_SANs']
sheets = {
'original': ('4.2_Items', '4.2_Timestamps'),
'backup': ('BR_Items', 'BR_Timestamps'),
'L17': ('L17_Items', 'L17_Timestamps'),
'B4.3': ('B4.3_Items', 'B4.3_Timestamps'),
'Darwin': ('Darwin_Items', 'Darwin_Timestamps')
}
current_sheets = sheets['original']
style = ttk.Style()
style.configure("Treeview", font=('Helvetica', 12,))
vcmd = (root.register(lambda P: P.isdigit() or P == ""), '%P')
class SANInputDialog(tk.Toplevel):
def __init__(self, parent, title=None):
super().__init__(parent)
self.transient(parent)
self.title(title)
self.parent = parent
self.result = None
self.create_widgets()
self.grab_set()
# Center the dialog window on the parent
self.geometry(f"+{parent.winfo_rootx() + parent.winfo_width() // 2 - 100}+{parent.winfo_rooty() + parent.winfo_height() // 2 - 50}")
self.after(10, self.focus_entry) # Ensure the entry field gets focus
self.wait_window(self)
def create_widgets(self):
# Input field
self.entry = ttk.Entry(self, validate="key", validatecommand=vcmd)
self.entry.pack(padx=5, pady=5)
self.entry.bind("<Return>", lambda _: self.on_submit()) # Bind Enter key to submit action
# Buttons frame
button_frame = tk.Frame(self)
button_frame.pack(pady=5)
# Submit button
submit_button = ttk.Button(button_frame, text="Submit", command=self.on_submit)
submit_button.pack(side='left', padx=5)
# Cancel button
cancel_button = ttk.Button(button_frame, text="Cancel", command=self.on_cancel)
cancel_button.pack(side='left', padx=5)
def focus_entry(self):
"""
Force the focus to the entry field.
"""
self.entry.focus_force()
def on_submit(self):
san_input = self.entry.get()
if san_input and len(san_input) >= 5 and len(san_input) <= 6:
self.result = san_input
self.destroy()
else:
tk.messagebox.showerror("Error", "Please enter a valid SAN number.", parent=self)
self.after(10, self.focus_entry) # Reapply focus after showing an error
def on_cancel(self):
self.result = None
self.destroy()
def is_san_unique(san_number):
# Adjust the search to account for the 'SAN' prefix properly
search_string = "SAN" + san_number if not san_number.startswith("SAN") else san_number
unique = all(search_string != row[0] for row in all_sans_sheet.iter_rows(min_row=2, values_only=True))
print(f"Checking SAN {search_string}: Unique - {unique}") # Debug print
return unique
def show_san_input():
dialog = SANInputDialog(root, "Enter SAN Number")
return dialog.result
frame = ctk.CTkFrame(root)
frame.pack(padx=3, pady=3, fill='both', expand=True)
button_width = 120 # Define the width of the buttons
entry_frame = ctk.CTkFrame(frame)
entry_frame.pack(pady=3, fill='x') # Fill horizontally for spacing alignment
# Button frame for location buttons
location_buttons_frame = ctk.CTkFrame(entry_frame)
location_buttons_frame.pack(fill='x', padx=10, pady=5) # Expand frame to fill horizontally
# Use grid to align and space buttons evenly
buttons = [
("Basement 4.2", lambda: switch_sheets('original')),
("Build Room", lambda: switch_sheets('backup')),
("Darwin", lambda: switch_sheets('Darwin')),
("Level 17", lambda: switch_sheets('L17')),
("Basement 4.3", lambda: switch_sheets('B4.3')),
]
# Variable to store the current selected button
selected_button = None
def highlight_button(selected, buttons):
"""
Highlights the selected button by making its label bold and resets others.
:param selected: The selected button widget.
:param buttons: List of all button widgets.
"""
global selected_button
for btn in buttons:
if btn == selected:
btn.configure(font=("Helvetica", 14, "bold")) # Set font to bold
else:
btn.configure(font=("Helvetica", 14, "normal")) # Reset to normal
selected_button = selected
# Create the buttons and store references in a list
button_widgets = []
# Variable to store the currently selected button
selected_button = None
def highlight_button(selected, buttons):
"""
Highlights the selected button by making its label bold and resets others.
:param selected: The selected button widget.
:param buttons: List of all button widgets.
"""
global selected_button
for btn in buttons:
if btn == selected:
btn.configure(font=("Helvetica", 14, "bold")) # Set font to bold
else:
btn.configure(font=("Helvetica", 14, "normal")) # Reset to normal
selected_button = selected
# Create the buttons and store references in a list
button_widgets = []
for col, (text, command) in enumerate(buttons):
# Define the button outside the lambda
btn = ctk.CTkButton(
location_buttons_frame,
text=text,
width=button_width,
font=("Helvetica", 14, "normal"), # Default font is normal
corner_radius=3
)
btn.configure(
command=lambda b=btn, cmd=command: [highlight_button(b, button_widgets), root.after(1, cmd)]
)
btn.grid(row=0, column=col, padx=5, pady=5, sticky="ew")
button_widgets.append(btn)
# # Entry and control frame for "+" and "-" buttons
# control_frame = ctk.CTkFrame(entry_frame)
# control_frame.pack(side='left', padx=10, pady=3)
# button_subtract = ctk.CTkButton(control_frame, text="-", command=lambda: update_count('subtract'), width=button_width, font=("Helvetica", 14), corner_radius=3)
# button_subtract.pack(side='left', padx=3)
# entry_value = tk.Entry(control_frame, font=("Helvetica", 14), justify='center', width=5, validate="key", validatecommand=vcmd)
# entry_value.pack(side='left', padx=3)
# button_add = ctk.CTkButton(control_frame, text="+", command=lambda: update_count('add'), width=button_width, font=("Helvetica", 14), corner_radius=3)
# button_add.pack(side='left', padx=3)
def update_treeview():
tree.delete(*tree.get_children())
workbook = load_workbook(workbook_path)
item_sheet = workbook[current_sheets[0]]
row_count = 0
for row in item_sheet.iter_rows(min_row=2, values_only=True):
if row[0] is not None:
if row_count % 2 == 0:
bg_color = 'white'
else:
bg_color = '#f0f0f0'
tree.insert('', 'end', values=row, tags=('oddrow' if row_count % 2 == 1 else 'evenrow'))
tree.tag_configure('oddrow', background='#f0f0f0')
tree.tag_configure('evenrow', background='white')
row_count += 1
def log_change(item, action, san_number="", timestamp_sheet=None, volume=1): # Added volume parameter with default value of 1
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
if timestamp_sheet is not None:
san_number = f"SAN{san_number}" if san_number and not san_number.startswith('SAN') else san_number
# Conditionally modify the action string to include volume for non-SAN items
if san_number == "":
action_text = f"{action} {volume}"
else:
action_text = action
timestamp_sheet.append([timestamp, item, action_text, san_number]) # Use action_text instead of action
workbook.save(workbook_path)
update_log_view()
logging.info(f"Logged change: Time: {timestamp}, Item: {item}, Action: {action_text}, SAN: {san_number}") # Use action_text
else:
logging.error("No timestamp sheet provided for logging.")
except Exception as e:
logging.error(f"Failed to log change: {e}")
tk.messagebox.showerror("Error", f"Failed to log change: {e}")
def switch_sheets(sheet_type):
global current_sheets
current_sheets = sheets[sheet_type]
update_treeview()
update_log_view()
def update_log_view():
if 'log_view' in globals():
log_view.delete(*log_view.get_children())
log_sheet = workbook[current_sheets[1]]
all_rows = list(log_sheet.iter_rows(min_row=2, values_only=True))
# Adjust the sorting to use the first column (timestamp)
sorted_rows = sorted(all_rows, key=lambda r: datetime.strptime(r[0], "%Y-%m-%d %H:%M:%S") if r[0] else datetime.min, reverse=True)
row_count = 0
for row in sorted_rows:
if row[0] is not None:
log_view.insert('', 'end', values=row, tags=('oddrow' if row_count % 2 == 1 else 'evenrow'))
log_view.tag_configure('oddrow', background='#f0f0f0')
log_view.tag_configure('evenrow', background='white')
row_count += 1
def update_count(operation):
"""
Updates the inventory count and handles SAN addition/removal, including logging
and updating the location in the 'All_SANs' sheet.
"""
selected_item = tree.item(tree.focus())['values'][0] if tree.focus() else None
if selected_item:
input_value = entry_value.get()
if input_value.isdigit():
input_value = int(input_value)
item_sheet = workbook[current_sheets[0]]
timestamp_sheet = workbook[current_sheets[1]]
san_required = any(g in selected_item for g in ["G8", "G9", "G10"])
entered_sans_count = 0 # To keep track of successfully entered SAN numbers if needed
if san_required:
# Loop to input each SAN for SAN-required items
while entered_sans_count < input_value:
# Small delay to ensure dialog focus is handled correctly
root.after(50) # Ensures the UI thread processes events between dialogs
san_number = show_san_input()
if san_number is None: # User cancelled the input
break # Keep the already entered SANs
# Ensure SAN number has the 'SAN' prefix
san_number = "SAN" + san_number if not san_number.startswith("SAN") else san_number
# Determine the location of the SAN based on the current sheet
location_mapping = {
'4.2_Timestamps': '4.2',
'BR_Timestamps': 'BR',
'Darwin_Timestamps': 'Darwin'
}
current_location = None
for sheet_name, loc in location_mapping.items():
if current_sheets[1] == sheet_name:
current_location = loc
break
if operation == 'add':
if is_san_unique(san_number):
# Append the SAN, Item, Timestamp, and Location to the "All_SANs" sheet
all_sans_sheet.append([san_number, selected_item, datetime.now().strftime("%Y-%m-%d %H:%M:%S"), current_location])
# Log each SAN unique number immediately
log_change(selected_item, operation, san_number, timestamp_sheet, volume=1)
workbook.save(workbook_path)
entered_sans_count += 1
else:
tk.messagebox.showerror("Error", "Duplicate or already used SAN number.", parent=root)
elif operation == 'subtract':
# Search for the SAN in all_sans_sheet and remove if found and matches the item
for row in all_sans_sheet.iter_rows(min_row=2):
if row[0].value == san_number and row[1].value == selected_item:
all_sans_sheet.delete_rows(row[0].row)
log_change(selected_item, operation, san_number, timestamp_sheet, volume=1)
workbook.save(workbook_path)
entered_sans_count += 1
break
else: # Executed if the loop completes without breaking (SAN not found or doesn't match item)
tk.messagebox.showerror("Error", f"SAN number {san_number} does not match the selected item.", parent=root)
for row in item_sheet.iter_rows(min_row=2):
if row[0].value == selected_item:
row[1].value = row[2].value or 0 # Update LastCount to the current NewCount
if operation == 'add':
row[2].value = (row[2].value or 0) + (input_value if not san_required else entered_sans_count)
elif operation == 'subtract':
row[2].value = max((row[2].value or 0) - (input_value if not san_required else entered_sans_count), 0)
# Log volume for non-SAN items or after all entered SANs
if (san_required and entered_sans_count > 0) or not san_required:
volume_to_log = input_value if not san_required else entered_sans_count
log_change(selected_item, operation, "", timestamp_sheet, volume=volume_to_log)
workbook.save(workbook_path)
update_treeview()
update_log_view()
# **Add this line to refocus on the entry field after processing**
entry_value.focus_set()
columns = ("Item", "LastCount", "NewCount")
tree = ttk.Treeview(frame, columns=columns, show="headings", selectmode='browse', style="Treeview")
for col in columns:
tree.heading(col, text=col, anchor='w')
tree.column("Item", anchor='w', width=250, stretch=False) # Width of the "Item" column in the treeview
tree.column("LastCount", anchor='w', width=175, stretch=False)
tree.pack(expand=True, fill="both", padx=3, pady=3)
# Frame to hold the controls for the "+" and "-" buttons and the text field
controls_frame = ctk.CTkFrame(root)
controls_frame.pack(pady=10, fill="x") # Positioned between the tree and log view
# Sub-frame to group the buttons and the text field tightly
entry_controls_frame = ctk.CTkFrame(controls_frame)
entry_controls_frame.pack(pady=10, anchor="center") # Center align the sub-frame
# Subtract button on the left
button_subtract = ctk.CTkButton(
entry_controls_frame,
text="-",
command=lambda: update_count('subtract'),
width=50,
font=("Helvetica", 14),
corner_radius=3
)
button_subtract.pack(side="left", padx=5) # Adjust padding to reduce spacing
# Text field for input, centered between the "+" and "-" buttons
entry_value = tk.Entry(
entry_controls_frame,
font=("Helvetica", 14),
justify="center",
width=10, # Adjust the width as needed
validate="key",
validatecommand=vcmd
)
entry_value.pack(side="left", padx=5) # Minimal padding to reduce spacing
# Add button on the right
button_add = ctk.CTkButton(
entry_controls_frame,
text="+",
command=lambda: update_count('add'),
width=50,
font=("Helvetica", 14),
corner_radius=3
)
button_add.pack(side="left", padx=5) # Adjust padding to reduce spacing
# Frame to hold the log view at the bottom
log_view_frame = ctk.CTkFrame(root)
log_view_frame.pack(side=tk.BOTTOM, fill='both', expand=True, padx=10, pady=10)
log_view_columns = ("Timestamp", "Item", "Action", "SAN Number")
log_view = ttk.Treeview(log_view_frame, columns=log_view_columns, show="headings", style="Treeview", height=8)
for col in log_view_columns:
log_view.heading(col, text=col, anchor='w')
log_view.column("Timestamp", anchor='w', width=175, stretch=False)
log_view.column("Item", anchor='w', width=180, stretch=False)
log_view.column("Action", anchor='w', width=100, stretch=False)
scrollbar_log = ttk.Scrollbar(log_view_frame, orient="vertical", command=log_view.yview)
scrollbar_log.pack(side='right', fill='y')
log_view.configure(yscrollcommand=scrollbar_log.set)
log_view.pack(expand=True, fill='both')
# Add the copying functionality here
def add_copy_option(tree):
def copy_selection():
selected_item = tree.item(tree.focus())['values']
item_text = ", ".join(map(str, selected_item))
root.clipboard_clear() # Clear the clipboard
root.clipboard_append(item_text) # Append new value to the clipboard
# Create a menu
context_menu = tk.Menu(tree, tearoff=0)
context_menu.add_command(label="Copy", command=copy_selection)
# Function to show the menu
def show_context_menu(event):
try:
context_menu.tk_popup(event.x_root, event.y_root)
finally:
context_menu.grab_release()
tree.bind("<Button-3>", show_context_menu) # Bind right-click event
# Apply the add_copy_option function to the Treeviews
add_copy_option(tree)
add_copy_option(log_view)
root.after(100, update_treeview)
update_log_view()
root.mainloop()
import logging.config
from pathlib import Path
from tkinter import Menu
import customtkinter as ctk
import os
import tkinter as tk
from tkinter import ttk
from openpyxl import load_workbook, Workbook
from datetime import datetime
import subprocess
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd # Ensure pandas is imported for date operations
# Function to save the workbook path to config.py
def save_config(workbook_path):
with open('config.py', 'w') as config_file:
config_file.write(f"workbook_path = r'{workbook_path}'\n")
logging_conf_path = Path('logging.conf')
if logging_conf_path.exists() and logging_conf_path.stat().st_size > 0:
try:
logging.config.fileConfig(logging_conf_path)
except Exception as e:
logging.error(f"Error configuring logging: {e}", exc_info=True)
else:
logging.basicConfig(level=logging.DEBUG)
def run_inventory_script():
script_path = script_directory / "inventory-levels_4.2v2.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_4.2v2.py' does not exist in the directory.")
def run_build_room_inventory_script():
script_path = script_directory / "inventory-levels_BRv2.2.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_BRv2.2.py' does not exist in the directory.")
def run_darwin_inventory_script():
script_path = script_directory / "inventory-levels_darwinv1.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_darwinv1.py' does not exist in the directory.")
def run_combined_rooms_inventory_script():
script_path = script_directory / "inventory-levels_combinedv1.4.py"
if script_path.exists():
os.system(f"python {script_path}")
else:
tk.messagebox.showerror("Error", "The script 'inventory-levels_combinedv1.4.py' does not exist in the directory.")
def open_spreadsheet():
try:
if os.name == 'nt':
os.startfile(workbook_path)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.run([opener, workbook_path])
except Exception as e:
tk.messagebox.showerror("Error", f"Failed to open the spreadsheet: {e}")
def update_all_sans_location():
"""
Updates the 'Location' column in the 'All_SANs' sheet based on the presence of SANs
in specific timestamp sheets.
"""
# Ensure the 'All_SANs' sheet exists
if 'All_SANs' not in workbook.sheetnames:
tk.messagebox.showerror("Error", "'All_SANs' sheet not found in the workbook.")
return
all_sans_sheet = workbook['All_SANs']
# Ensure the Location column (D) has a header
if all_sans_sheet.max_column < 4:
all_sans_sheet.cell(row=1, column=4, value="Location")
# Mapping of sheets to short locations
location_mapping = {
'4.2_Timestamps': '4.2',
'BR_Timestamps': 'BR',
'Darwin_Timestamps': 'Darwin'
}
# Iterate over rows in All_SANs and update the location
for row_idx, row in enumerate(all_sans_sheet.iter_rows(min_row=2, values_only=True), start=2):
san_number = row[0]
location = None
# Check each timestamp sheet for the SAN
for sheet_name, loc in location_mapping.items():
if sheet_name in workbook.sheetnames:
timestamp_sheet = workbook[sheet_name]
if any(san_number == cell[0] for cell in timestamp_sheet.iter_rows(min_row=2, min_col=1, max_col=1, values_only=True)):
location = loc
break
# Write the location to the Location column (Column D)
all_sans_sheet.cell(row=row_idx, column=4, value=location)
# Save the workbook after updating
workbook.save(workbook_path)
def view_all_sans_log():
"""
Displays the updated All_SANs data in a Treeview widget with columns
'SAN Number', 'Item', 'Time', and 'Location'.
Includes a search box to filter SAN numbers dynamically.
"""
# Update the 'Location' column in the 'All_SANs' sheet
update_all_sans_location()
log_window = tk.Toplevel(root)
log_window.title("SANs In Stock")
log_window.geometry("800x800")
# Search box frame
search_frame = tk.Frame(log_window)
search_frame.pack(fill="x", padx=10, pady=5)
tk.Label(search_frame, text="Search:").pack(side="left", padx=5)
search_var = tk.StringVar()
search_entry = ttk.Entry(search_frame, textvariable=search_var)
search_entry.pack(side="left", fill="x", expand=True, padx=5)
# Treeview and scrollbar
columns = ("SAN Number", "Item", "Time", "Location")
log_tree = ttk.Treeview(log_window, columns=columns, show="headings")
for col in columns:
log_tree.heading(col, text=col)
log_tree.column(col, anchor="w")
log_tree.pack(expand=True, fill="both", padx=10, pady=10)
scrollbar = ttk.Scrollbar(log_window, orient="vertical", command=log_tree.yview)
scrollbar.pack(side="right", fill="y")
log_tree.configure(yscrollcommand=scrollbar.set)
# Load data from the "All_SANs" sheet
def load_data(filter_text=""):
"""
Populates the Treeview with data, filtering by filter_text.
"""
log_tree.delete(*log_tree.get_children()) # Clear current data
if 'All_SANs' in workbook.sheetnames:
all_sans_sheet = workbook['All_SANs']
for row in all_sans_sheet.iter_rows(min_row=2, values_only=True):
san_number, item, timestamp, location = row
# Filter by the search text
if filter_text.lower() in str(san_number).lower():
log_tree.insert('', 'end', values=(san_number, item, timestamp, location))
else:
tk.messagebox.showinfo("Info", "'All_SANs' sheet not found or empty.", parent=log_window)
# Trigger search on text change
def on_search(*args):
filter_text = search_var.get()
load_data(filter_text)
search_var.trace("w", on_search) # Bind dynamic updates to search
# Load initial data
load_data()
root = ctk.CTk()
root.title("Perth EUC Stock")
root.geometry("700x850")
# Unified font settings for the application
custom_font = ("Helvetica", 12)
# Custom styles for Treeview
style = ttk.Style()
style.theme_use("clam")
style.configure("Treeview",
font=("Helvetica", 12),
rowheight=25,
background="#f9f9f9",
foreground="#333",
fieldbackground="#f9f9f9")
style.map("Treeview", background=[("selected", "#3399ff")])
menu_bar = tk.Menu(root)
plots_menu = tk.Menu(menu_bar, tearoff=0)
plots_menu.add_command(label="Basement 4.2 Inventory", command=run_inventory_script)
plots_menu.add_command(label="Build Room Inventory", command=run_build_room_inventory_script)
plots_menu.add_command(label="Darwin Inventory", command=run_darwin_inventory_script)
plots_menu.add_command(label="Combined Inventory", command=run_combined_rooms_inventory_script)
plots_menu.add_command(label="SANs In Stock", command=view_all_sans_log)
plots_menu.add_command(label="Open Spreadsheet", command=open_spreadsheet)
# plots_menu.add_command(label="Headsets In Stock", command=view_headsets_log)
menu_bar.add_cascade(label="Data", menu=plots_menu)
root.config(menu=menu_bar)
script_directory = Path(__file__).parent
def get_file_path():
# Creating a temporary root window for file dialog
temp_root = tk.Tk()
temp_root.withdraw() # Hide the temporary root window
file_path = filedialog.askopenfilename(
title="Select a spreadsheet file",
filetypes=(("Excel files", "*.xlsx"), ("All files", "*.*"))
)
temp_root.destroy() # Destroy the temporary root window to clean up
if not file_path:
tk.messagebox.showerror("Error", "No file selected. Exiting application.")
raise SystemExit # Exit the application if no file is selected
return file_path
# Get the workbook path from user
workbook_path = get_file_path()
save_config(workbook_path) # Save the path to the config file immediately after getting it
workbook = load_workbook(workbook_path)
all_sans_sheet = workbook['All_SANs']
sheets = {
'original': ('4.2_Items', '4.2_Timestamps'),
'backup': ('BR_Items', 'BR_Timestamps'),
'L17': ('L17_Items', 'L17_Timestamps'),
'B4.3': ('B4.3_Items', 'B4.3_Timestamps'),
'Darwin': ('Darwin_Items', 'Darwin_Timestamps')
}
current_sheets = sheets['original']
style = ttk.Style()
style.configure("Treeview", font=('Helvetica', 12,))
vcmd = (root.register(lambda P: P.isdigit() or P == ""), '%P')
class SANInputDialog(tk.Toplevel):
def __init__(self, parent, title=None):
super().__init__(parent)
self.transient(parent)
self.title(title)
self.parent = parent
self.result = None
self.create_widgets()
self.grab_set()
# Center the dialog window on the parent
self.geometry(f"+{parent.winfo_rootx() + parent.winfo_width() // 2 - 100}+{parent.winfo_rooty() + parent.winfo_height() // 2 - 50}")
self.after(10, self.focus_entry) # Ensure the entry field gets focus
self.wait_window(self)
def create_widgets(self):
# Input field
self.entry = ttk.Entry(self, validate="key", validatecommand=vcmd)
self.entry.pack(padx=5, pady=5)
self.entry.bind("<Return>", lambda _: self.on_submit()) # Bind Enter key to submit action
# Buttons frame
button_frame = tk.Frame(self)
button_frame.pack(pady=5)
# Submit button
submit_button = ttk.Button(button_frame, text="Submit", command=self.on_submit)
submit_button.pack(side='left', padx=5)
# Cancel button
cancel_button = ttk.Button(button_frame, text="Cancel", command=self.on_cancel)
cancel_button.pack(side='left', padx=5)
def focus_entry(self):
"""
Force the focus to the entry field.
"""
self.entry.focus_force()
def on_submit(self):
san_input = self.entry.get()
if san_input and len(san_input) >= 5 and len(san_input) <= 6:
self.result = san_input
self.destroy()
else:
tk.messagebox.showerror("Error", "Please enter a valid SAN number.", parent=self)
self.after(10, self.focus_entry) # Reapply focus after showing an error
def on_cancel(self):
self.result = None
self.destroy()
def is_san_unique(san_number):
# Adjust the search to account for the 'SAN' prefix properly
search_string = "SAN" + san_number if not san_number.startswith("SAN") else san_number
unique = all(search_string != row[0] for row in all_sans_sheet.iter_rows(min_row=2, values_only=True))
print(f"Checking SAN {search_string}: Unique - {unique}") # Debug print
return unique
def show_san_input():
dialog = SANInputDialog(root, "Enter SAN Number")
return dialog.result
frame = ctk.CTkFrame(root)
frame.pack(padx=3, pady=3, fill='both', expand=True)
button_width = 120 # Define the width of the buttons
entry_frame = ctk.CTkFrame(frame)
entry_frame.pack(pady=3, fill='x') # Fill horizontally for spacing alignment
# Button frame for location buttons
location_buttons_frame = ctk.CTkFrame(entry_frame)
location_buttons_frame.pack(fill='x', padx=10, pady=5) # Expand frame to fill horizontally
# Use grid to align and space buttons evenly
buttons = [
("Basement 4.2", lambda: switch_sheets('original')),
("Build Room", lambda: switch_sheets('backup')),
("Darwin", lambda: switch_sheets('Darwin')),
("Level 17", lambda: switch_sheets('L17')),
("Basement 4.3", lambda: switch_sheets('B4.3')),
]
# Variable to store the current selected button
selected_button = None
def highlight_button(selected, buttons):
"""
Highlights the selected button by making its label bold and resets others.
:param selected: The selected button widget.
:param buttons: List of all button widgets.
"""
global selected_button
for btn in buttons:
if btn == selected:
btn.configure(font=("Helvetica", 14, "bold")) # Set font to bold
else:
btn.configure(font=("Helvetica", 14, "normal")) # Reset to normal
selected_button = selected
# Create the buttons and store references in a list
button_widgets = []
# Variable to store the currently selected button
selected_button = None
def highlight_button(selected, buttons):
"""
Highlights the selected button by making its label bold and resets others.
:param selected: The selected button widget.