BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "Acquisti" (
	"IdFattura"	INTEGER NOT NULL,
	"IdIngrediente"	INTEGER NOT NULL,
	"Quantità"	INTEGER NOT NULL,
	PRIMARY KEY("IdFattura","IdIngrediente")
);
CREATE TABLE IF NOT EXISTS "Birraie" (
	"IdPersona"	INTEGER NOT NULL,
	"Soprannome"	TEXT NOT NULL UNIQUE,
	PRIMARY KEY("IdPersona")
);
CREATE TABLE IF NOT EXISTS "Birrifici" (
	"IdBirrificio"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"Nome"	TEXT,
	"AnnoFondazione"	INTEGER,
	"Motto"	TEXT,
	"Stemma"	BLOB,
	"CapacitàProduttiva"	INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS "BirrificiBirraie" (
	"IdBirrificio"	INTEGER NOT NULL,
	"IdBirraia"	INTEGER NOT NULL,
	PRIMARY KEY("IdBirrificio","IdBirraia")
);
CREATE TABLE IF NOT EXISTS "Clienti" (
	"IdPersona"	INTEGER NOT NULL,
	"IndirizzoSpedizione"	TEXT NOT NULL,
	PRIMARY KEY("IdPersona")
);
CREATE TABLE IF NOT EXISTS "Fatture" (
	"IdFattura"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"IdBirrificio"	INTEGER NOT NULL,
	"IdFornitore"	INTEGER NOT NULL,
	"Data"	TEXT NOT NULL,
	"NumeroFattura"	INTEGER NOT NULL UNIQUE,
	"Importo"	INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS "Fornitori" (
	"IdFornitore"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"RagioneSociale"	TEXT NOT NULL UNIQUE,
	"PartitaIva"	INTEGER NOT NULL UNIQUE,
	"Indirizzo"	TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "Ingredienti" (
	"IdIngrediente"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"IdTipo"	INTEGER NOT NULL,
	"Descrizione"	TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "IngredientiRicette" (
	"IdRicetta"	INTEGER NOT NULL,
	"IdIngrediente"	INTEGER NOT NULL,
	"Quantità"	INTEGER NOT NULL,
	PRIMARY KEY("IdRicetta","IdIngrediente")
);
CREATE TABLE IF NOT EXISTS "Note" (
	"IdNota"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"IdProduzione"	INTEGER NOT NULL,
	"Testo"	TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "NoteDegustazione" (
	"IdNota"	INTEGER NOT NULL,
	"Giudizio"	INTEGER NOT NULL,
	PRIMARY KEY("IdNota")
);
CREATE TABLE IF NOT EXISTS "Persone" (
	"IdPersona"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"Nome"	TEXT NOT NULL,
	"Cognome"	TEXT NOT NULL,
	"Email"	TEXT NOT NULL,
	"CodiceFiscale"	TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS "Prenotazioni" (
	"IdCliente"	INTEGER NOT NULL,
	"IdProduzione"	INTEGER NOT NULL,
	"Stato"	INTEGER,
	"Quantità"	INTEGER NOT NULL,
	PRIMARY KEY("IdProduzione","IdCliente")
);
CREATE TABLE IF NOT EXISTS "Produzioni" (
	"IdProduzione"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"IdRicetta"	INTEGER NOT NULL,
	"DataProduzione"	TEXT NOT NULL,
	"NumeroLotto"	INTEGER NOT NULL,
	"Stato"	INTEGER,
	"NumeroBottiglie"	INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS "Ricette" (
	"IdRicetta"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"IdBirrificio"	INTEGER NOT NULL,
	"IdCreatrice"	INTEGER NOT NULL,
	"IdRicettaMadre"	INTEGER,
	"Nome"	TEXT NOT NULL,
	"DataCreazione"	TEXT NOT NULL,
	"Stato"	INTEGER
);
CREATE TABLE IF NOT EXISTS "TipiIngredienti" (
	"IdTipo"	INTEGER PRIMARY KEY AUTOINCREMENT,
	"Tipo"	TEXT NOT NULL,
	"UnitàDiMisura"	TEXT NOT NULL
);
INSERT INTO "Fornitori" ("IdFornitore","RagioneSociale","PartitaIva","Indirizzo") VALUES (1,'Luppoli per tutti i gusti',121457,'Corso Torre 125'),
 (2,'A tutto Malto',147963,'Piazza Dante 16'),
 (3,'Lievitami',236541,'Corso Pavia 33');
INSERT INTO "Fatture" ("IdFattura","IdBirrificio","IdFornitore","Data","NumeroFattura","Importo") VALUES (1,1,1,'2020-01-03',125,77),
 (2,1,2,'2020-03-12',116,40),
 (3,1,3,'2020-03-18',78,82),
 (4,1,3,'2020-04-02',96,60);
INSERT INTO "Birrifici" ("IdBirrificio","Nome","AnnoFondazione","Motto","Stemma","CapacitàProduttiva") VALUES (1,'Pirati Rossi',2020,'Arrrr','/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUTExMWFhUXGBgYGBcYFxkYGBUeGhoaGBcYFxcYHSggGB0lHRgXITEhJSktLi4uGB8zODMtNygtLisBCgoKDg0OFxAQGi0dHx0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIALgBEgMBIgACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAEAAECAwUGBwj/xAA+EAABAgUCAgkBBwMDBAMBAAABAhEAAwQhMRJBUWEFBhMicYGRofAyB0KxwdHh8RQjUjNighVjcpKissIW/8QAGAEBAQEBAQAAAAAAAAAAAAAAAQACAwT/xAAeEQEBAQEAAgMBAQAAAAAAAAAAARECEiExQVEDE//aAAwDAQACEQMRAD8A8UTLghKEjZ/nGGDRNAONvnrGLTiyWhHD+ItRKSQAw8YnTUBUc+xJg40oSCS7DFwCfSMXqHASei9dw4JPC3zMKZ0SEi5IPhBSarT9LjzfjFqEJHemH6g7ZJtB5dDIBp+i9QJCHAyXx4wQro+Wn7oUeO3kCfcwV/W2AYAWYDezYG+eXjEP6gEEB9t3bzi3oegKqMOwS52AxD/9Md+7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8awJgxZoPCISReLpk0MwiSmaXihRh1LiBMISEIGIvDQpatcVvCMMYkUNChRI4hQ0KJHh3hoeJJCLkscRSIkmAjiezYvkPvbGYskdIEGyXPz3gGYvc3PExYioIPwRnENXXrIZiBvm/rFf9YsHe/wA2gTU+IJkp4Z2gzEkqoUWckeT/AI+EJCxuSeL+MEJl8m8/jQTSdmgaiATz/KDSqpZYfe53G0ak0IQHJv4CAavpcYQG5wAqpJuf5gy1CJs8HiA7hmv4xALIU6QACDlvNxcPClqBJKgw+frBMmcAXDA2Y2wPDfEVMRUuYpgkBtioAO5ZzszgF4tnyJqBlAN3YkthwNJ4c8vAU+sKQzlnDd5zbB5GA5lQYpFompqC31ZAG928uQb+YAXO33hlK+bxBXhG5GVhmneIbRAmIvGsSzUYipUQhogm8IGIQhClwMIrit4ZRgRlGGhoUKKHhoeJFChQ0SKFChRIoUKFEjw8KFEih3h4UCOLw/ZGFCiK2WOUXds0KFAkl9IKigzSbwoUWJJBG8Wy54G2cX/GFCgRkL3PnEZ84G2BChRYlK1QxmPDQoUZSoYrhQoQi8M8KFEjPCeFChRnhQoUSKFChRI0KFCiRQoUKJFChQokUKFCiT//2Q==',0);
INSERT INTO "BirrificiBirraie" ("IdBirrificio","IdBirraia") VALUES (3,1),
 (4,1);
INSERT INTO "Birraie" ("IdPersona","Soprannome") VALUES (3,'MaVe'),
 (4,'GiuLe');
INSERT INTO "Clienti" ("IdPersona","IndirizzoSpedizione") VALUES (1,'Via Ramazzini 14'),
 (2,'Corso Milano 2');
INSERT INTO "Persone" ("IdPersona","Nome","Cognome","Email","CodiceFiscale") VALUES (1,'Antonio','Rossi','a.r@g.i','NNNNRRRR'),
 (2,'Enrico','Bianchi','e.b@g.c','EEEBBB'),
 (3,'Giovanni','Verdi','m.v@l.i','MV'),
 (4,'Giulia','Lelli','g.l@e.c','GL');
INSERT INTO "Acquisti" ("IdFattura","IdIngrediente","Quantità") VALUES (1,1,12),
 (2,2,7);
INSERT INTO "TipiIngredienti" ("IdTipo","Tipo","UnitàDiMisura") VALUES (1,'Luppolo','g / L (mash)'),
 (2,'Malto','g%'),
 (3,'Lievito','g%'),
 (4,'Zuccheri','g%'),
 (5,'Additivi','mg%');
INSERT INTO "Ingredienti" ("IdIngrediente","IdTipo","Descrizione") VALUES (1,1,'Luppolo verde'),
 (2,1,'Amarillo');
INSERT INTO "IngredientiRicette" ("IdRicetta","IdIngrediente","Quantità") VALUES (1,1,4);
INSERT INTO "Ricette" ("IdRicetta","IdBirrificio","IdCreatrice","IdRicettaMadre","Nome","DataCreazione","Stato") VALUES (1,1,3,'','Bionda decisa','2020-01-01','');
INSERT INTO "NoteDegustazione" ("IdNota","Giudizio") VALUES (1,7);
INSERT INTO "Note" ("IdNota","IdProduzione","Testo") VALUES (1,1,'Troppo freddo ad aprile-maggio, meglio farla d''estate');
INSERT INTO "Produzioni" ("IdProduzione","IdRicetta","DataProduzione","NumeroLotto","Stato","NumeroBottiglie") VALUES (1,1,'2020-05-01',12447,NULL,12),
 (2,1,'2020-04-15',12443,0,0);
INSERT INTO "Prenotazioni" ("IdCliente","IdProduzione","Stato","Quantità") VALUES (1,1,'',4),
 (1,2,NULL,6);

CREATE VIEW Query_a
AS SELECT r.IdRicetta, r.Nome
   FROM Ricette r
   JOIN Persone p ON p.IdPersona = r.IdCreatrice
   WHERE p.Nome = 'Giovanni';
CREATE VIEW Query_b
AS SELECT fa.IdBirrificio,
          COUNT(DISTINCT fa.IdFornitore) DiversiFornitori
   FROM Fatture fa
   WHERE fa.Data >= '2020-01-01'
   GROUP BY fa.IdBirrificio
   HAVING COUNT(DISTINCT fa.IdFornitore) >= 3
   ORDER BY COUNT(DISTINCT fa.IdFornitore) DESC;
CREATE VIEW Query_c
AS SELECT fo.RagioneSociale, SUM(fa.Importo) ImportoTotale,
          AVG(fa.Importo) ImportoMedio
   FROM Fornitori fo
   JOIN Fatture fa ON fa.IdFornitore = fo.IdFornitore
   JOIN Birrifici b ON b.IdBirrificio = fa.IdBirrificio
   WHERE b.Nome = 'Pirati Rossi'
   GROUP BY fo.IdFornitore, fo.RagioneSociale
   HAVING SUM(fa.Importo) > 10;
CREATE VIEW Query_d
AS SELECT b.Soprannome
   FROM Birraie b
   WHERE EXISTS (SELECT *
                 FROM Ricette r
                 WHERE r.IdCreatrice = b.IdPersona);
CREATE VIEW Query_e
AS SELECT DISTINCT p1.IdPersona, p1.Nome, p1.Cognome
   FROM Persone p1
   JOIN Prenotazioni pre1
        ON pre1.IdCliente = p1.IdPersona
   JOIN Produzioni pro1
        ON pro1.IdProduzione = pre1.IdProduzione
   JOIN Ricette r1
        ON r1.IdRicetta = pro1.IdRicetta
   WHERE NOT EXISTS (SELECT *
                     FROM Prenotazioni pre2
                     JOIN Produzioni pro2
                          ON pro2.IdProduzione = pre2.IdProduzione
                     JOIN Ricette r2 ON r2.IdRicetta = pro2.IdRicetta
                     WHERE pre2.IdCliente = pre1.IdCliente
                           AND r2.IdBirrificio <> r1.IdBirrificio);
CREATE VIEW Query_f
AS SELECT r1.IdBirrificio, pro1.NumeroLotto
   FROM Produzioni pro1
   JOIN Ricette r1 ON r1.IdRicetta = pro1.IdRicetta
   WHERE pro1.NumeroLotto = (SELECT MAX(pro2.NumeroLotto)
                             FROM Produzioni pro2
                             JOIN Ricette r2
                                  ON r2.IdRicetta = pro2.IdRicetta
                             WHERE r2.IdBirrificio = r1.IdBirrificio);
CREATE VIEW IngredientiUsati (IdIngrediente, Ingrediente,
                              Usati)
AS SELECT i.IdIngrediente IdIngrediente, i.Descrizione Ingrediente,
          SUM(ir.Quantità) Usati
   FROM IngredientiRicette ir
   JOIN Ingredienti i ON i.IdIngrediente = ir.IdIngrediente
   JOIN Produzioni p ON p.IdRicetta = ir.IdRicetta
   WHERE p.Stato = 0
   GROUP BY i.IdIngrediente, i.Descrizione;
CREATE VIEW IngredientiInUso (IdIngrediente, Ingrediente, InUso)
AS SELECT i.IdIngrediente IdIngrediente, i.Descrizione Ingrediente,
          SUM(ir.Quantità) InUso
   FROM IngredientiRicette ir
   JOIN Ingredienti i ON i.IdIngrediente = ir.IdIngrediente
   JOIN Produzioni p ON p.IdRicetta = ir.IdRicetta
   WHERE p.Stato IS NULL
   GROUP BY i.IdIngrediente, i.Descrizione;
CREATE VIEW IngredientiAcquistatiTotali (IdIngrediente, Ingrediente,
                                         Totale)
AS SELECT i.IdIngrediente IdIngrediente, i.Descrizione Ingrediente,
          SUM(a.Quantità) Totale
   FROM Acquisti a
   JOIN Ingredienti i ON i.IdIngrediente = a.IdIngrediente
   GROUP BY i.IdIngrediente, i.Descrizione;
CREATE VIEW Inventario (IdIngrediente, Ingrediente, QuantitàTotale,
                        QuantitàDisponibile)
AS SELECT iat.IdIngrediente IdIngrediente,
          iat.Ingrediente Ingrediente,
          (iat.Totale - COALESCE(iu.Usati, 0)) QuantitàTotale,
          (iat.Totale - COALESCE(iu.Usati, 0)
            - COALESCE(iiu.InUso, 0)) QuantitàDisponibile
   FROM IngredientiAcquistatiTotali iat
   LEFT JOIN IngredientiUsati iu
             ON iu.idIngrediente = iat.IdIngrediente
   LEFT JOIN IngredientiInUso iiu
             ON iiu.idIngrediente = iat.IdIngrediente
   WHERE iat.Totale - COALESCE(iu.Usati, 0) > 0;
COMMIT;
