2 --TRUNCATE TABLE "Vente";
3 --TRUNCATE TABLE "Panier";
4 DROP PROCEDURE insertVente
IF EXISTS CASCADE;
5 CREATE PROCEDURE insertVente
7 , IN venteClient
INTEGER
8 , IN venteProduit
INTEGER
9 , IN venteQuantité
DECIMAL
13 DECLARE panierID
INTEGER DEFAULT NULL;
14 DECLARE venteFacturable
BOOLEAN DEFAULT FALSE;
15 DECLARE venteID
INTEGER DEFAULT NULL;
17 -- CorrectnessNote: seule la date et le client
18 -- font partie des critères pour décider
19 -- si le panier existe déjà où non.
21 SELECT ID FROM "Panier"
22 WHERE "Panier".
date = venteDate
23 AND "Panier".client
= venteClient
;
29 WHERE "Client".
ID = venteClient
;
30 INSERT INTO "Panier"(date, client
, facturable
)
31 VALUES (venteDate
, venteClient
, venteFacturable
);
32 SET panierID
= IDENTITY();
35 -- CorrectnessNote: la quantité ne fait pas partie des critères
36 -- pour décider si la vente existe déjà ou non.
37 SET venteID
= SELECT ID FROM "Vente"
38 WHERE "Vente".panier
= panierID
39 AND "Vente".produit
= venteProduit
;
42 INSERT INTO "Vente"(panier
, produit
, quantité
)
43 VALUES (panierID
, venteProduit
, venteQuantité
);
48 DROP PROCEDURE insertVentesParAbonnement
IF EXISTS;
49 CREATE PROCEDURE insertVentesParAbonnement(IN finalDate
DATE)
52 -- ExplanationNote: gère chaque jour l'un après l'autre.
55 FROM UNNEST (SEQUENCE_ARRAY(DATE '2025-04-07', finalDate
, 1 DAY))
56 WITH ORDINALITY as T(day, I
)
58 -- ExplanationNote: pour un jour donné,
59 -- gère chaque abonnement l'un après l'autre.
61 FOR SELECT ID as aboID
63 , produit
as aboProduit
64 , quantité
as aboQuantité
67 JOIN "Période" ON "Période".
ID = "Abonnement".période
68 AND "Période".nom
= DAYNAME(day)
70 CALL insertVente(day, aboClient
, aboProduit
, aboQuantité
);
76 CALL insertVentesParAbonnement(DATE '2025-04-07' + 7 DAY);
78 -- `insertFactureClient(client)`
79 -- crée une facture pour tous les paniers
81 DROP PROCEDURE insertFactureClient
IF EXISTS;
82 CREATE PROCEDURE insertFactureClient(IN selClient
INTEGER)
85 DECLARE factureID
INTEGER DEFAULT NULL;
86 -- Si le client a des paniers `facturable`s sans `facture`.
87 IF CARDINALITY(ARRAY (SELECT ID
89 WHERE client
= selClient
94 -- Crée une nouvelle facture
95 -- et référence celle-ci dans tous les paniers
96 -- non-facturés jusque là.
97 INSERT INTO "Facture"(date)
98 VALUES (CURRENT_DATE);
99 SET factureID
= IDENTITY();
101 SET facture
= factureID
102 WHERE facture
IS NULL
103 AND client
= selClient
111 --call prix_à_date(CURRENT_DATE);
112 SELECT "Prod".nom_complet
, date, euros
113 FROM TABLE (prix_à
_date(CURRENT_DATE)) AS PD
114 JOIN TABLE (choisirUnProduit()) AS "Prod" ON "Prod".
ID = produit
117 CALL insertFactureClient(0);
118 CALL insertFactureClient(1);
119 CALL insertFactureClient(2);
120 SELECT * FROM "Panier" ;
121 SELECT * FROM "Client" ;
124 \p Factures des clients
125 DROP VIEW "Factures" IF EXISTS;
126 CREATE VIEW "Factures" AS
130 ("Facture".
date) as facture_date
,
132 ("Client".
ID) as client
,
134 ("Panier".
ID) as panier
,
136 ("Panier".
date) as panier_date
,
138 -- ("Vente".ID) as ventes,
140 "Produit".
ID as produit
,
141 "Vente".quantité
as quantité
,
142 "PrixÁDate".euros
as euros
144 --("PrixÁDate".euros * "Vente".quantité SEPARATOR ', ') as prix_quantifié
146 JOIN "Facture" ON "Facture".
ID = "Panier".facture
147 JOIN "Client" ON "Client".
ID = "Panier".client
148 JOIN "Vente" ON "Vente".panier
= "Panier".
ID
149 JOIN "Produit" ON "Produit".
ID = "Vente".produit
150 JOIN TABLE (prix_à
_date("Panier".
date)) AS "PrixÁDate" ON "PrixÁDate".produit
= "Produit".
ID
159 SELECT * FROM "Factures";
161 \i
modèles
/Facture.Paniers.
sql
162 \i
modèles
/Facture.Totaux.
sql