2021-11-24 17:53:17 +01:00
|
|
|
from random import randint, uniform
|
|
|
|
|
|
|
|
from db import BaseDeDonnees
|
|
|
|
|
|
|
|
class Stock(BaseDeDonnees):
|
|
|
|
"""Gère une table "stock" pour une base de donnée donné."""
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(r"db.sqlite3")
|
|
|
|
|
|
|
|
def creationTable(self) -> None:
|
|
|
|
"""Créer la table qui stocker les stocks."""
|
|
|
|
requete = """
|
|
|
|
CREATE TABLE IF NOT EXISTS stocks (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
type TEXT,
|
|
|
|
nom TEXT,
|
|
|
|
quantite INTEGER,
|
|
|
|
prix REAL,
|
|
|
|
image_url TEXT
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
self.requete(requete)
|
|
|
|
# Ajout d'un stock par défaut si aucun stock n'existe dans la base de donnée
|
2021-11-25 16:21:24 +01:00
|
|
|
if len(self.listeStocks()) == 0:
|
2021-11-25 23:54:01 +01:00
|
|
|
# Créer un dictionnaire d'éléments pour mieux voir ce que l'on ajoute à la base de donnée
|
2021-11-24 17:53:17 +01:00
|
|
|
defaut = {
|
|
|
|
"fruits legumes": [
|
2021-11-25 23:54:01 +01:00
|
|
|
("banane", "img/banane.gif"),
|
|
|
|
("orange", "img/orange.gif"),
|
|
|
|
("betterave", "img/betterave.gif"),
|
|
|
|
("carottes", "img/carottes.gif"),
|
|
|
|
("tomates", "img/tomates.gif"),
|
|
|
|
("citron", "img/citron.gif"),
|
|
|
|
("kiwi", "img/kiwi.gif"),
|
|
|
|
("clementine", "img/clementine.gif"),
|
|
|
|
("pomme", "img/pomme.gif"),
|
|
|
|
("avocat", "img/avocat.gif")
|
2021-11-24 17:53:17 +01:00
|
|
|
],
|
|
|
|
"boulangerie": [
|
2021-11-25 23:54:01 +01:00
|
|
|
("brownie", "img/brownie.gif"),
|
|
|
|
("baguette", "img/baguette.gif"),
|
|
|
|
("pain au chocolat", "img/pain_au_chocolat.gif"),
|
|
|
|
("croissant", "img/croissant.gif"),
|
|
|
|
("macaron", "img/macaron.gif"),
|
|
|
|
("millefeuille", "img/millefeuille.gif"),
|
|
|
|
("paris-brest", "img/paris-brest.gif"),
|
|
|
|
("opera", "img/opera.gif"),
|
|
|
|
("fraisier", "img/fraisier.gif"),
|
|
|
|
("eclair", "img/eclair.gif")
|
2021-11-24 17:53:17 +01:00
|
|
|
],
|
2021-11-26 13:18:04 +01:00
|
|
|
"boucherie poissonnerie": [
|
2021-11-25 23:54:01 +01:00
|
|
|
("saucisson", "img/saucisson.gif"),
|
|
|
|
("côte de boeuf", "img/cote_de_boeuf.gif"),
|
|
|
|
("langue de boeuf", "img/langue_de_boeuf.gif"),
|
|
|
|
("collier de boeuf", "img/collier_de_boeuf.gif"),
|
|
|
|
("entrecote", "img/entrecote.gif"),
|
|
|
|
("cabillaud", "img/cabillaud.gif"),
|
|
|
|
("saumon", "img/saumon.gif"),
|
|
|
|
("colin", "img/colin.gif"),
|
|
|
|
("bar", "img/bar.gif"),
|
|
|
|
("dorade", "img/dorade.gif")
|
2021-11-24 17:53:17 +01:00
|
|
|
],
|
|
|
|
"entretien": [
|
2021-11-26 00:23:44 +01:00
|
|
|
("nettoyant air comprimé", "img/nettoyant_air_comprime.gif"),
|
|
|
|
("nettoyage anti-bactérien", "img/nettoyage_anti-bacterien.gif"),
|
2021-11-25 23:54:01 +01:00
|
|
|
("nettoyant pour écran", "img/nettoyant_pour_ecran.gif"),
|
|
|
|
("nettoyant pour lunettes", "img/nettoyant_pour_lunettes.gif"),
|
|
|
|
("pioche", "img/pioche.gif"),
|
|
|
|
("pelle", "img/pelle.gif"),
|
|
|
|
("lampe torche", "img/lampe_torche.gif"),
|
|
|
|
("gants", "img/gants.gif"),
|
|
|
|
("éponge", "img/eponge.gif"),
|
|
|
|
("essuie-tout", "img/essuie-tout.gif")
|
2021-11-24 17:53:17 +01:00
|
|
|
]
|
|
|
|
}
|
2021-11-25 23:54:01 +01:00
|
|
|
|
|
|
|
# Ajoute le dictionnaire précédemment créer à la base de donnée avec un prix et une quantité aléatoire
|
2021-11-24 17:53:17 +01:00
|
|
|
for type in defaut:
|
|
|
|
for element in defaut[type]:
|
2021-11-25 23:54:01 +01:00
|
|
|
self.ajoutStock(type, element[0], randint(0, 10), round(uniform(2., 30.), 2), element[1])
|
2021-11-24 17:53:17 +01:00
|
|
|
|
|
|
|
def ajoutStock(self, type: str, nom: str, quantite: int, prix: float, imageURL: str) -> list:
|
|
|
|
"""Ajoute un élément dans le stock et retourne l'ID de ce dernier."""
|
|
|
|
requete = """
|
|
|
|
INSERT INTO stocks (
|
|
|
|
type, nom, quantite, prix, image_url
|
|
|
|
) VALUES (
|
|
|
|
?, ?, ?, ?, ?
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
self.requete(requete, [type.lower(), nom.lower(), quantite, prix, imageURL])
|
|
|
|
return self.affichageResultat(self.requete("SELECT last_insert_rowid();"))
|
|
|
|
|
|
|
|
def suppressionStocks(self, id: int) -> None:
|
|
|
|
"""Supprime un stock."""
|
|
|
|
requete = """
|
|
|
|
DELETE FROM stocks
|
|
|
|
WHERE id = ?
|
|
|
|
"""
|
|
|
|
self.requete(requete, id)
|
|
|
|
|
2021-11-25 16:21:24 +01:00
|
|
|
def listeStocks(self) -> list:
|
|
|
|
"""Retourne la liste des éléments en stock sous forme de dictionnaire."""
|
2021-11-24 17:53:17 +01:00
|
|
|
recuperation = [
|
|
|
|
"id",
|
|
|
|
"type",
|
|
|
|
"nom",
|
|
|
|
"quantite",
|
|
|
|
"prix",
|
|
|
|
"image_url"
|
|
|
|
]
|
2021-11-25 16:21:24 +01:00
|
|
|
requete = f"""
|
|
|
|
SELECT {", ".join(recuperation)} FROM stocks
|
|
|
|
"""
|
|
|
|
return [dict(zip(recuperation, element)) for element in self.affichageResultat(self.requete(requete))]
|
2021-11-24 17:53:17 +01:00
|
|
|
|
|
|
|
def stockExistant(self, stock: str) -> bool:
|
2021-11-28 12:02:47 +01:00
|
|
|
"""Vérifie si le stock donnée existe déjà dans la base de donnée."""
|
2021-11-24 17:53:17 +01:00
|
|
|
requete = """
|
|
|
|
SELECT EXISTS (
|
|
|
|
SELECT 1 FROM stocks
|
2021-11-28 12:02:47 +01:00
|
|
|
WHERE nom = ?
|
2021-11-24 17:53:17 +01:00
|
|
|
)
|
|
|
|
"""
|
|
|
|
return True if self.affichageResultat(self.requete(requete, stock.lower()))[0][0] == 1 else False
|
2021-11-28 11:48:31 +01:00
|
|
|
|
|
|
|
def listeTypes(self) -> list:
|
|
|
|
"""Renvoie la liste des types disponibles dans la base de donnée."""
|
|
|
|
requete = """
|
|
|
|
SELECT type FROM stocks
|
|
|
|
"""
|
|
|
|
res = []
|
|
|
|
for i in self.affichageResultat(self.requete(requete)):
|
|
|
|
if i[0] not in res:
|
|
|
|
res.append(i[0])
|
|
|
|
return res
|