import tkinter as tk from tkinter import ttk from tkinter import messagebox from datetime import datetime import serial import time # data file inventory = {} user = {} # Tester le system =============================== def mock_arduino_realine(): return input("Entrer le Tag ID: ") def mock_arduino_realine_user(): return input("Entrer le User ID: ") # =================================================== # Connection avec Arduino #arduino = None # tester arduino_port = 'COM6' baud_rate = 9600 #Vrai Code arduino = serial.Serial('COM6', baudrate=9600, timeout=1) ## DEFINE FUNCTION =========================================================== # Fonction pour ajouter un item: def add_item(): clear_result() identification = identification_entry.get() name = name_entry.get() row = row_entry.get() tablet = tablet_entry.get() dependance = dependance_entry.get() while True: #tag_id = mock_arduino_realine() tag_id = arduino.readline().decode().strip() if tag_id and identification and name and row and tablet and dependance: if tag_id not in inventory: inventory[tag_id] = { 'identification': identification, 'name': name, 'row': row, 'tablet': tablet, 'user_id': None, 'user_name': None, 'status': 'Disponible', 'date': None, 'dependance': dependance } save_inventory() update_table() clear_entries() text = "Item added\n" write_result(text) save_inventory() break else: messagebox.showerror("Erreur", "Item deja dans le systeme") clear_entries() break else: messagebox.showerror("Erreur", "Information Incomplete") clear_entries() break def delete_item(): clear_result() identification_to_delete = identification_entry.get() if identification_to_delete: for tag_id, item in list(inventory.items()): if 'identification' in item and item['identification'] == identification_to_delete: del inventory[tag_id] update_table() save_inventory() messagebox.showinfo("Suppression d'Item", "Item supprimé avec succès.") clear_entries() return else: messagebox.showerror("Erreur", "Aucun item trouvé avec cette identification.") else: messagebox.showerror("Erreur", "Veuillez entrer une identification pour supprimer un item.") # Fonction pour ajouter un user def add_user(): clear_result() user_name = user_name_entry.get() while True: user_id = mock_arduino_realine_user() if user_id and user_name: if user_id not in user: user[user_id] = { 'user_id': user_id, 'user_name': user_name, } text = "Utilisateur ajouter\n" write_result(text) save_user() clear_entries() break else: messagebox.showerror("Erreur", "Utilisateur deja dans le systeme") clear_entries() break else: messagebox.showerror("Erreur", "Information Incomplete") clear_entries() break # Fonction pour choisir un item def choose_item(): clear_result() while True: tag_id = mock_arduino_realine() clear_result() user_id = mock_arduino_realine_user() clear_result() if tag_id in inventory and user_id in user: item = inventory[tag_id] print("Item:", item) # Add this line for debugging if 'status' in item and 'disponible' in item["status"].lower().strip() and item["status"].lower().strip() == 'disponible': item_name = item.get('name', '') or item.get('identification', '') dependance = item.get('dependance', 'N/A') # Update the status and user information inventory[tag_id]["status"] = "Indisponible" inventory[tag_id]["user_id"] = user_id inventory[tag_id]["user_name"] = user[user_id]["user_name"] inventory[tag_id]["date"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") update_table() # Display information including 'dependance' in a messagebox messagebox.showinfo("Item Choisi", f"Item: {item_name}\nN'oublier pas: {dependance}") save_inventory() clear_entries() break else: print("Item status:", item.get('status', '')) # Add this line for debugging messagebox.showerror("Erreur", "L'item n'est pas disponible ou n'est pas dans le systeme") clear_entries() break else: messagebox.showerror("Erreur", "Item ou utilisateur n'est pas dans le systeme") clear_entries() break def find_item(): clear_result() item_name = name_to_find_entry.get() for tag_id, item in inventory.items(): if 'name' in item and item['name'] == item_name and item['status'] == 'Disponible': write_result(f"Identification: {item['identification']}\n") write_result(f"Rangee: {item['row']}\n") write_result(f"Tablette: {item['tablet']}\n") write_result(f"N'oublier pas: {item['dependance']}\n") clear_entries() return # Exit the loop after the first match elif 'identification' in item and item['identification'] == item_name and item['status'] == 'Disponible': write_result(f"Identification: {item['identification']}\n") write_result(f"Rangee: {item['row']}\n") write_result(f"Tablette: {item['tablet']}\n") write_result(f"N'oublier pas: {item['dependance']}\n") clear_entries() return # Exit the loop after the first match else: text = "Aucun article trouvé avec ce nom ou aucun article disponible.\n" print(text) # Debugging statement write_result(text) clear_entries() def count_item(): clear_result() item_name = count_item_entry.get() total_count = 0 available_count = 0 in_use_count = 0 for tag_id, item in inventory.items(): if item.get('name') == item_name or item.get('identification') == item_name: total_count += 1 if item['status'] == 'Disponible': available_count += 1 elif item['status'] == 'Indisponible': in_use_count += 1 text = f"Nombre d'items: {total_count}\n" text += f"Nombre d'item disponible: {available_count}\n" text += f"Nombre d'item non-disponiblle: {in_use_count}\n" write_result(text) clear_entries() def return_item(): clear_result() write_result("Presenter l'item\n") tag_id = mock_arduino_realine() if tag_id in inventory and inventory[tag_id]["status"] == "Indisponible": inventory[tag_id]["status"] = "Disponible" location = f"Rangee: {inventory[tag_id]['row']}, Tablette: {inventory[tag_id]['tablet']}" inventory[tag_id]["user_id"] = None inventory[tag_id]["user_name"] = None inventory[tag_id]["date"] = None update_table() clear_result() write_result("Succès, article retourné.\n") write_result(f"Location: {location}\n") save_inventory() clear_entries() else: messagebox.showerror("Erreur", "Échec de l'opération : Article non utilisé ou absent du système.") clear_entries() # Fonction pour technique: def clear_entries(): name_entry.delete(0, tk.END) identification_entry.delete(0, tk.END) # Added to clear the identification entry row_entry.delete(0, tk.END) tablet_entry.delete(0, tk.END) dependance_entry.delete(0, tk.END) # Added to clear the dependance entry user_name_entry.delete(0, tk.END) count_item_entry.delete(0, tk.END) name_to_find_entry.delete(0, tk.END) def clear_result(): result_text.config(state=tk.NORMAL) result_text.delete(1.0, tk.END) result_text.config(state=tk.DISABLED) def write_result(text): result_text.config(state=tk.NORMAL) result_text.insert(tk.END, f"{text}") result_text.config(state=tk.DISABLED) # Fonction pour le data: def update_table(): for item in tree.get_children(): tree.delete(item) for tag_id, item in inventory.items(): identification = item.get('identification', '') row = item.get('row', '') tablet = item.get('tablet', '') user_id = item.get('user_id', '') user_name = item.get('user_name', '') status = item.get('status', '') date = item.get('date', '') tree.insert("", "end", values=(tag_id, identification, row, tablet, status, user_id, user_name, date)) # Set the headers in the Treeview table tree["columns"] = ("Tag ID", "Identification", "Row", "Tablet", "Status", "User ID", "User Name", "Date") for header in tree["columns"]: tree.heading(header, text=header) def save_inventory(): with open("inventory.txt", "w") as txt_file: for tag_id, item in inventory.items(): identification = item.get('identification', '') name = item.get('name', '') row = item.get('row', '') tablet = item.get('tablet', '') status = item.get('status', '') user_id = item.get('user_id', '') user_name = item.get('user_name', '') date = item.get('date', '') dependance = item.get('dependance', '') # If dependance is stored in the inventory # Ensure all values are present in each line txt_file.write(f"{tag_id},{identification},{name},{row},{tablet},{status},{user_id},{user_name},{date},{dependance}\n") def save_user(): with open("user.txt", "w") as txt_file: for user_id, item in user.items(): txt_file.write(f"{item['user_id']},{item['user_name']}\n") def check_item_stock(): low_stock_threshold = 1 # Adjust this threshold as needed low_stock_items = set() for item_name in set(item.get('name', '') for item in inventory.values()): available_item_counts = sum(1 for i in inventory.values() if i.get('name') == item_name and i.get('status') == 'Disponible') if available_item_counts <= low_stock_threshold: low_stock_items.add(item_name) if low_stock_items: message = f"Les articles suivants sont faible en stock:\n" message += "\n".join(low_stock_items) messagebox.showinfo("Faible Inventaire", message) else: messagebox.showinfo("Somaire d'Inventaire", "Tout les articles sont a un niveau acceptable") def load_inventory(): global inventory try: with open("inventory.txt", "r") as txt_file: inventory = {} for line in txt_file: values = line.strip().split(',') tag_id, identification, name, row, tablet, status, user_id, user_name, date = values[:9] dependance = values[9] if len(values) == 10 else '' inventory[tag_id] = { 'identification': identification, 'name': name, 'row': row, 'tablet': tablet, 'status': status, 'user_id': user_id, 'user_name': user_name, 'date': date, 'dependance': dependance } update_table() check_item_stock() except FileNotFoundError: messagebox.showwarning("Attention", "Liste d'inventaire non trouver") except Exception as e: messagebox.showerror("Erreur", f"Une erreur s'est produite lors de la lecture du fichier d'inventaire: {e}") def load_user(): global user try: with open("user.txt", "r") as txt_file: user = {} for line in txt_file: values = line.strip().split(',') user_id, name = values user[user_id] = { "user_id": user_id, "user_name": name, } except FileNotFoundError: print("User file not found. Creating an empty user file.") with open("user.txt", "w") as txt_file: pass except Exception as e: print("An error occurred while reading the user file:", e) ## Interface ========================================================================= # creer une fenetre tkinter window = tk.Tk() window.title("Systèmw de Gestion d'Inventaire") # Ajouter des Items: tk.Label(window, text="Type d'item:").grid(row=0, column=0) name_entry = tk.Entry(window) name_entry.grid(row=0, column=1) tk.Label(window, text="Identification:").grid(row=1, column=0) identification_entry = tk.Entry(window) identification_entry.grid(row=1, column=1) tk.Label(window, text="Rangée:").grid(row=2, column=0) row_entry = tk.Entry(window) row_entry.grid(row=2, column=1) tk.Label(window, text="Tablette:").grid(row=3, column=0) tablet_entry = tk.Entry(window) tablet_entry.grid(row=3, column=1) tk.Label(window, text="Dependance:").grid(row=4, column=0) dependance_entry = tk.Entry(window) dependance_entry.grid(row=4, column=1) add_button = tk.Button(window, text="Ajouter l'Item", command=add_item) add_button.grid(row=5, column=1) delete_button =tk.Button(window, text = "Suprimer Item", command = delete_item) delete_button.grid(row=5, column=0) # Ajouter Utilisateur: tk.Label(window, text="Nom de l'utilisateur:").grid(row=7, column=0) user_name_entry = tk.Entry(window) user_name_entry.grid(row=7, column=1) add_button = tk.Button(window, text="Ajouter Utilisateur", command=add_user) add_button.grid(row=8, column=1) # Choisir un Item: add_button = tk.Button(window, text="Choisir un Item", command=choose_item) add_button.grid(row=8, column=9) # Retourner un Item: add_button = tk.Button(window, text="Retourner un Item", command=return_item) add_button.grid(row=8, column=8) # Compter Item: tk.Label(window, text="Nom de l'item:").grid(row=5, column=3) count_item_entry = tk.Entry(window) count_item_entry.grid(row=5, column=4) count_button = tk.Button(window, text="Compter l'Item", command=count_item) count_button.grid(row=5, column=5) # Trouver Item: tk.Label(window, text="Name de l'Item Recherché:").grid(row=4, column=3) name_to_find_entry = tk.Entry(window) name_to_find_entry.grid(row=4, column=4) find_button = tk.Button(window, text="Recherché l'item", command=find_item) find_button.grid(row=4, column=5) # Result Textbox: result_text = tk.Text(window, height=4, width=40) result_text.grid(row=5, column=6, columnspan=8) result_text.config(state=tk.DISABLED) # Table: tk.Label(window).grid(row=10, column=1) tree = ttk.Treeview(window, columns=("Tag ID", "Identification", "Row", "Tablet", "Status", "User ID", "User Name", "Date"), show="headings") tree.heading("Tag ID", text="Tag ID") tree.heading("Identification", text="Identification") tree.heading("Row", text="Row") tree.heading("Tablet", text="Tablet") tree.heading("Status", text="Status") tree.heading("User ID", text="User ID") tree.heading("User Name", text="User Name") tree.heading("Date", text="Date") tree.grid(row=21, column=0, columnspan=10) # Load Item: def function(): load_inventory() load_user() load_button = tk.Button(window, text="Télécharger Inventaire", command=function) load_button.grid(row=23, column=0) load_button = tk.Button(window, text="Somaire d'Inventaire", command=check_item_stock) load_button.grid(row=23, column=1) # create menu: menu = tk.Menu(window) window.config(menu=menu) def exit_app(): window.destroy() menu.add_command(label="Sortir", command=exit_app) # Commencer le code window.mainloop()