-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory_handler.py
More file actions
86 lines (66 loc) · 3.25 KB
/
Copy pathinventory_handler.py
File metadata and controls
86 lines (66 loc) · 3.25 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
import pandas as pd
import os
class InventoryHandler:
DATA_FILE = "data/consolidated_data.csv"
@staticmethod
def load_data():
"""
Charge les données depuis le fichier CSV consolidé.
Returns:
pandas.DataFrame: Un DataFrame contenant les données du fichier CSV.
Raises:
FileNotFoundError: Si le fichier "data/consolidated_data.csv" n'existe pas.
"""
if not os.path.exists(InventoryHandler.DATA_FILE):
raise FileNotFoundError("Fichier consolidé introuvable. Importez d'abord les données.")
return pd.read_csv(InventoryHandler.DATA_FILE)
@staticmethod
def search_by_product(product_name):
"""
Recherche d'un produit dans l'inventaire consolidé.
Args:
product_name (str): Le nom du produit à rechercher.
Returns:
pandas.DataFrame: Un DataFrame contenant les lignes correspondant à la recherche.
Raises:
FileNotFoundError: Si le fichier "data/consolidated_inventory.csv" n'existe pas.
"""
try :
df = pd.read_csv("data/consolidated_inventory.csv")
except FileNotFoundError:
raise FileNotFoundError("Fichier consolidé introuvable. Veuillez d'abord importer les fichiers CSV.")
df = InventoryHandler.load_data()
results = df[df['product'].str.contains(product_name, case=False, na=False)]
return results
@staticmethod
def generate_report(report_type, threshold=None):
"""
Génère un rapport basé sur le type spécifié (par catégorie ou sur les stocks faibles).
Args:
report_type (str): Le type de rapport à générer, soit "category" pour un rapport par catégorie,
soit "low_stock" pour un rapport sur les produits avec un stock faible.
threshold (int, optional): Le seuil de stock pour générer un rapport des stocks faibles. Utilisé uniquement pour
"low_stock".
Returns:
str: Un rapport sous forme de chaîne de caractères.
Raises:
FileNotFoundError: Si le fichier "data/consolidated_inventory.csv" n'existe pas.
"""
try:
df = pd.read_csv("data/consolidated_inventory.csv")
except FileNotFoundError:
raise FileNotFoundError("Fichier consolidé introuvable. Veuillez d'abord importer les fichiers CSV.")
df = InventoryHandler.load_data()
if report_type == "category":
# Générer un rapport par catégorie avec les quantités
report = df.groupby("category").agg({"quantity": "sum"}).reset_index()
report_str = report.to_string(index=False) # Convertir le DataFrame en une chaîne
return report_str # Retourner le rapport sous forme de texte
elif report_type == "low_stock":
if threshold is None:
return "Le seuil de stock doit être spécifié pour un rapport sur les stocks faibles."
else:
low_stock_report = df[df["quantity"] < threshold]
return low_stock_report.to_string(index=False)
else:
return "Type de rapport non valide."