· 8 years ago · Apr 22, 2018, 10:24 PM
1#####################################
2### CRÉATION DE LA BASE DE DONNÉE ###
3#####################################
4
5-- MySQL Workbench Forward Engineering
6
7SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
8SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
9SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
10
11-- -----------------------------------------------------
12-- Schema TP3
13-- -----------------------------------------------------
14DROP SCHEMA IF EXISTS TP3;
15
16CREATE SCHEMA IF NOT EXISTS `TP3` DEFAULT CHARACTER SET utf8 ;
17USE `TP3` ;
18
19-- -----------------------------------------------------
20-- Table `TP3`.`tblVille`
21-- -----------------------------------------------------
22CREATE TABLE IF NOT EXISTS `TP3`.`tblVille` (
23 `idVille` INT NOT NULL AUTO_INCREMENT,
24 `nomVille` VARCHAR(50) NOT NULL,
25 PRIMARY KEY (`idVille`),
26 INDEX `idx_nomVille` (`nomVille` ASC))
27ENGINE = InnoDB;
28
29-- -----------------------------------------------------
30-- Table `TP3`.`tblAbonne`
31-- -----------------------------------------------------
32CREATE TABLE IF NOT EXISTS `TP3`.`tblAbonne` (
33 `idAbonne` INT NOT NULL AUTO_INCREMENT,
34 `nomAbonne` VARCHAR(50) NOT NULL,
35 `prenomAbonne` VARCHAR(25) NULL DEFAULT NULL,
36 `codePostal` VARCHAR(7) NOT NULL,
37 `telephone` VARCHAR(15) NOT NULL,
38 `courriel` VARCHAR(50) NOT NULL,
39 `typeAbonne` CHAR(1) NOT NULL,
40 `idVille` INT NOT NULL,
41 PRIMARY KEY (`idAbonne`),
42 INDEX `idx_nomAbonne` (`nomAbonne` ASC),
43 INDEX `FK_tblAbonne_idVille` (`idVille` ASC),
44 CONSTRAINT `FK_tblAbonne_idVille`
45 FOREIGN KEY (`idVille`)
46 REFERENCES `TP3`.`tblVille` (`idVille`))
47ENGINE = InnoDB;
48
49-- -----------------------------------------------------
50-- Table `TP3`.`tblVehicule`
51-- -----------------------------------------------------
52CREATE TABLE IF NOT EXISTS `TP3`.`tblVehicule` (
53 `idVehicule` VARCHAR(7) NOT NULL,
54 `marque` VARCHAR(25) NOT NULL,
55 `modele` VARCHAR(25) NOT NULL,
56 `couleur` VARCHAR(15) NOT NULL,
57 PRIMARY KEY (`idVehicule`))
58ENGINE = InnoDB;
59
60-- -----------------------------------------------------
61-- Table `TP3`.`tblMethodPaiement`
62-- -----------------------------------------------------
63CREATE TABLE IF NOT EXISTS `TP3`.`tblMethodPaiement` (
64 `idPaiement` INT NOT NULL AUTO_INCREMENT,
65 `typePaiement` CHAR(1) NOT NULL,
66 `descPaiement` VARCHAR(25) NOT NULL,
67 PRIMARY KEY (`idPaiement`))
68ENGINE = InnoDB;
69
70-- -----------------------------------------------------
71-- Table `TP3`.`tblOccasionnel`
72-- -----------------------------------------------------
73CREATE TABLE IF NOT EXISTS `TP3`.`tblOccasionnel` (
74 `idOccas` VARCHAR(12) NOT NULL,
75 `dateDebutOccas` DATETIME NOT NULL,
76 `dateFinOccas` DATETIME NULL,
77 `idTransaction` INT NULL,
78 PRIMARY KEY (`idOccas`),
79 INDEX `FK_tblOccasionnel_idTransaction` (`idTransaction` ASC),
80 CONSTRAINT `FK_tblOccasionnel_idTransaction`
81 FOREIGN KEY (`idTransaction`)
82 REFERENCES `TP3`.`tblTransaction` (`idTransaction`))
83ENGINE = InnoDB;
84
85-- -----------------------------------------------------
86-- Table `TP3`.`tblTransaction`
87-- -----------------------------------------------------
88CREATE TABLE IF NOT EXISTS `TP3`.`tblTransaction` (
89 `idTransaction` INT NOT NULL AUTO_INCREMENT,
90 `montant` DECIMAL(6,2) NULL,
91 `numPaiement` VARCHAR(16) NULL,
92 `idPaiement` INT NULL,
93 `idAbonnement` INT NULL,
94 `idOccas` VARCHAR(12) NULL,
95 PRIMARY KEY (`idTransaction`),
96 INDEX `FK_tblTransaction_idPaiement` (`idPaiement` ASC),
97 INDEX `FK_tblTransaction_idAbonnement` (`idAbonnement` ASC),
98 INDEX `FK_tblTransaction_idOccas` (`idOccas` ASC),
99 CONSTRAINT `FK_tblTransaction_idPaiement`
100 FOREIGN KEY (`idPaiement`)
101 REFERENCES `TP3`.`tblMethodPaiement` (`idPaiement`),
102 CONSTRAINT `FK_tblTransaction_idAbonne`
103 FOREIGN KEY (`idAbonnement`)
104 REFERENCES `TP3`.`tblAbonnement` (`idAbonnement`),
105 CONSTRAINT `FK_tblTransaction_idOccas`
106 FOREIGN KEY (`idOccas`)
107 REFERENCES `TP3`.`tblOccasionnel` (`idOccas`))
108ENGINE = InnoDB;
109
110-- -----------------------------------------------------
111-- Table `TP3`.`tblAbonnement`
112-- -----------------------------------------------------
113CREATE TABLE IF NOT EXISTS `TP3`.`tblAbonnement` (
114 `idAbonnement` INT NOT NULL AUTO_INCREMENT,
115 `dateDebutAbonne` DATE NOT NULL,
116 `dateFinAbonne` DATE NOT NULL,
117 `idAbonne` INT NOT NULL,
118 `idVehicule` VARCHAR(7) NOT NULL,
119 `idPlace` SMALLINT NOT NULL,
120 `idTransaction` INT NOT NULL,
121 PRIMARY KEY (`idAbonnement`),
122 INDEX `FK_tblAbonnement_idAbonne_tblAbonne` (`idAbonne` ASC),
123 INDEX `FK_tblAbonnement_idVehicule` (`idVehicule` ASC),
124 INDEX `FK_tblAbonnement_idPlace` (`idPlace` ASC),
125 INDEX `FK_tblAbonnement_idTransaction` (`idTransaction` ASC),
126 CONSTRAINT `FK_tblAbonnement_idAbonne_tblAbonne`
127 FOREIGN KEY (`idAbonne`)
128 REFERENCES `TP3`.`tblAbonne` (`idAbonne`),
129 CONSTRAINT `FK_tblAbonnement_idVehicule`
130 FOREIGN KEY (`idVehicule`)
131 REFERENCES `TP3`.`tblVehicule` (`idVehicule`),
132 CONSTRAINT `FK_tblAbonnement_idPlace`
133 FOREIGN KEY (`idPlace`)
134 REFERENCES `TP3`.`tblPlace` (`idPlace`),
135 CONSTRAINT `FK_tblAbonnement_idTransaction`
136 FOREIGN KEY (`idTransaction`)
137 REFERENCES `TP3`.`tblTransaction` (`idTransaction`))
138ENGINE = InnoDB;
139
140-- -----------------------------------------------------
141-- Table `TP3`.`tblPlace`
142-- -----------------------------------------------------
143CREATE TABLE IF NOT EXISTS `TP3`.`tblPlace` (
144 `idPlace` SMALLINT NOT NULL AUTO_INCREMENT,
145 `typePlace` VARCHAR(25) NOT NULL,
146 `idAbonne` INT NULL,
147 PRIMARY KEY (`idPlace`),
148 INDEX `FK_tblPlace_idAbonne` (`idAbonne` ASC),
149 CONSTRAINT `FK_tblPlace_idAbonne`
150 FOREIGN KEY (`idAbonne`)
151 REFERENCES `TP3`.`tblAbonnement` (`idAbonnement`))
152ENGINE = InnoDB;
153
154SET SQL_MODE=@OLD_SQL_MODE;
155SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
156SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
157
158###############################
159### FONCTIONS ET PROCÉDURES ###
160###############################
161
162-- -----------------------------------------------------
163-- Fonctions -------------------------------------------
164-- -----------------------------------------------------
165-- --------------------------------------------------------------------------------------------
166-- Fonction FnNbPlaceAbonne() qui retourne le nombre de place utilisé par un abonnement valide
167-- --------------------------------------------------------------------------------------------
168DROP FUNCTION IF EXISTS FnNbPlaceAbonne;
169
170DELIMITER $$
171CREATE FUNCTION FnNbPlaceAbonne()
172RETURNS int
173BEGIN
174 DECLARE v_nb int;
175 SELECT count(*) as v_nb
176 FROM tblAbonnement as abon
177 WHERE abon.dateFinAbonne > now()
178 into v_nb;
179RETURN v_nb;
180END $$
181DELIMITER ;
182
183-- ------------------------------------------------------------------------------------------
184-- Fonction FnNbPlaceOccas() qui retourne le nombre de place utilisé par un occasionel actif
185-- ------------------------------------------------------------------------------------------
186DROP FUNCTION IF EXISTS FnNbPlaceOccas;
187
188DELIMITER $$
189CREATE FUNCTION FnNbPlaceOccas()
190RETURNS int
191BEGIN
192 DECLARE v_nb int;
193 SELECT count(*) as v_nv
194 FROM tblOccasionnel as ocas
195 WHERE ocas.dateFinOccas is null
196 into v_nb;
197RETURN v_nb;
198END $$
199DELIMITER ;
200
201-- --------------------------------------------------------------
202-- Fonction pour calculer le montant à payer pour un occasionnel
203-- --------------------------------------------------------------
204DROP FUNCTION IF EXISTS FnCalculerMontant;
205
206DELIMITER $$
207CREATE FUNCTION FnCalculerMontant(p_idOccas varchar(12))
208RETURNS DECIMAL(6, 2)
209BEGIN
210 DECLARE v_tHeure int DEFAULT 5;
211 DECLARE v_tJour int DEFAULT 20;
212 DECLARE v_mJour DECIMAL(6, 2);
213 DECLARE v_mHeure DECIMAL(6, 2);
214 DECLARE v_dateDebut DATETIME;
215 DECLARE v_dTemps TIME;
216 DECLARE v_nbJour INT;
217
218 SELECT dateDebutOccas INTO v_dateDebut FROM tblOccasionnel
219 WHERE idOccas = p_idOccas;
220 SET v_dTemps = TIMEDIFF(CURRENT_TIME(), TIME(v_dateDebut));
221
222 IF (TIME(v_dateDebut) > CURRENT_TIME()) THEN
223 SET v_dTemps = TIMEDIFF('23:59:59', v_dTemps);
224 END IF;
225
226 SET v_nbJour = DATEDIFF(NOW(), v_dateDebut);
227 SET v_mJour = v_nbJour * v_tJour;
228 SET v_mHeure = CEIL(time_to_sec(v_dtemps) / (60 * 60)) * v_tHeure;
229
230 IF (v_mHeure > v_tJour)
231 THEN SET v_mHeure = v_tJour;
232 END IF;
233 RETURN (v_mJour + v_mHeure);
234END $$
235DELIMITER ;
236
237-- ---------------------------------------------------------------------------------------
238-- Fonction FnCreditAbonne() qui retourne le nombre de paiement Abonnement fait par crédit
239-- ---------------------------------------------------------------------------------------
240DROP FUNCTION IF EXISTS FnCreditAbonne;
241
242DELIMITER $$
243CREATE FUNCTION FnCreditAbonne(p_dateTrans DATE)
244RETURNS DECIMAL(9,2)
245BEGIN
246 DECLARE v_Montant DECIMAL(9,2) default 0;
247 SELECT SUM(trans.montant)
248 FROM tblTransaction as trans
249 INNER JOIN tblMethodPaiement as methode
250 ON trans.idPaiement = methode.idPaiement
251 INNER JOIN tblAbonnement as abon
252 ON trans.idAbonnement = abon.idAbonnement
253 WHERE methode.typePaiement in ('V','M','X') AND idOccas is null AND abon.dateDebutAbonne = p_dateTrans
254 INTO v_Montant;
255 if (v_Montant is null) then
256 set v_Montant = 0;
257 end if;
258RETURN v_Montant;
259END $$
260DELIMITER ;
261
262-- ---------------------------------------------------------------------------------------
263-- Fonction FnComptantAbonne() qui retourne le nombre de paiement Abonnement fait comptant
264-- ---------------------------------------------------------------------------------------
265DROP FUNCTION IF EXISTS FnComptantAbonne;
266
267DELIMITER $$
268CREATE FUNCTION FnComptantAbonne(p_dateTrans DATE)
269RETURNS DECIMAL(9,2)
270BEGIN
271 DECLARE v_Montant DECIMAL(9,2);
272 SELECT SUM(trans.montant)
273 FROM tblTransaction as trans
274 INNER JOIN tblMethodPaiement as methode
275 ON trans.idPaiement = methode.idPaiement
276 INNER JOIN tblAbonnement as abon
277 ON trans.idAbonnement = abon.idAbonnement
278 WHERE methode.typePaiement in ('A','C') AND idOccas is null AND abon.dateDebutAbonne = p_dateTrans
279 INTO v_Montant;
280 if (v_Montant is null) then
281 set v_Montant = 0;
282 end if;
283RETURN v_Montant;
284END $$
285DELIMITER ;
286
287 -- ---------------------------------------------------------------------------------------
288-- Fonction FnCreditOcca() qui retourne le nombre de paiement Occasionnel fait par crédit
289-- ---------------------------------------------------------------------------------------
290DROP FUNCTION IF EXISTS FnCreditOcca;
291
292DELIMITER $$
293CREATE FUNCTION FnCreditOcca(p_dateTrans DATE)
294RETURNS DECIMAL(9,2)
295BEGIN
296 DECLARE v_Montant DECIMAL(9,2);
297 SELECT SUM(trans.montant)
298 FROM tblTransaction as trans
299 INNER JOIN tblMethodPaiement as methode
300 ON trans.idPaiement = methode.idPaiement
301 INNER JOIN tblOccasionnel as occa
302 ON trans.idOccas = occa.idOccas
303 WHERE methode.typePaiement in ('V','M','X') AND idAbonnement is null AND cast(occa.dateFinOccas as date) = p_dateTrans
304 INTO v_Montant;
305 if (v_Montant is null) then
306 set v_Montant = 0;
307 end if;
308RETURN v_Montant;
309END $$
310DELIMITER ;
311
312-- ---------------------------------------------------------------------------------------
313-- Fonction FnComptantOcca() qui retourne le nombre de paiement Occasionnel fait comptant
314-- ---------------------------------------------------------------------------------------
315DROP FUNCTION IF EXISTS FnComptantOcca;
316
317DELIMITER $$
318CREATE FUNCTION FnComptantOcca(p_dateTrans DATE)
319RETURNS DECIMAL(9,2)
320BEGIN
321 DECLARE v_Montant DECIMAL(9,2);
322 SELECT SUM(trans.montant)
323 FROM tblTransaction as trans
324 INNER JOIN tblMethodPaiement as methode
325 ON trans.idPaiement = methode.idPaiement
326 INNER JOIN tblOccasionnel as occa
327 ON trans.idOccas = occa.idOccas
328 WHERE methode.typePaiement in ('A','C') AND idAbonnement is null AND cast(occa.dateFinOccas as DATE) = p_dateTrans
329 INTO v_Montant;
330 if (v_Montant is null) then
331 set v_Montant = 0;
332 end if;
333RETURN v_Montant;
334END $$
335DELIMITER ;
336
337-- ------------------------------------------------------------
338-- Fonction pour générer des plaques de voiture
339-- ------------------------------------------------------------
340DROP FUNCTION IF EXISTS FnRandomPlaque;
341
342DELIMITER $$
343CREATE FUNCTION FnRandomPlaque()
344 RETURNS VARCHAR(6)
345BEGIN
346 DECLARE plaque VARCHAR(6) DEFAULT "";
347 SELECT concat(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand()*25+1, 1),
348 substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand()*25+1, 1),
349 substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand()*25+1, 1),
350 substring('0123456789', rand()*9+1, 1),
351 substring('0123456789', rand()*9+1, 1),
352 substring('0123456789', rand()*9+1, 1))
353 into @idVehicule;
354
355 SET @rcount = -1;
356 SELECT COUNT(*) INTO @rcount FROM `tblVehicule` WHERE `idVehicule` = @idVehicule ;
357
358 IF @rcount = 0 THEN
359 SET plaque = @idVehicule ;
360 END IF ;
361
362 RETURN plaque ;
363END$$
364DELIMITER ;
365
366-- ---------------------------------------------------------------
367-- Fonction pour retourner le NOMBRE ABONNEMENTS PERSONNE/SOCIETE
368-- ---------------------------------------------------------------
369DROP FUNCTION IF EXISTS FnNbAbon;
370
371DELIMITER $$
372CREATE FUNCTION FnNbAbon(p_DateDebut Date, p_DateFin Date, p_TypeAbonne char)
373 RETURNS int
374BEGIN
375 DECLARE v_nb int;
376SELECT count(*) FROM tblTransaction as trans
377 INNER JOIN tblAbonnement as abonnem
378 ON trans.idTransaction = abonnem.idTransaction
379 INNER JOIN tblAbonne as abon
380 ON abonnem.idAbonne = abon.idAbonne
381 WHERE abon.typeAbonne = p_TypeAbonne AND abonnem.dateDebutAbonne <= p_DateFin AND abonnem.dateDebutAbonne > p_DateDebut
382 INTO v_nb;
383
384 IF (v_nb is null) THEN
385 SET v_nb = 0;
386 END IF ;
387
388 RETURN v_nb ;
389END$$
390DELIMITER ;
391
392-- -----------------------------------------------------------
393-- Fonction pour connaitre SOMME ABONNEMENTS PERSONNE/SOCIETE
394-- -----------------------------------------------------------
395DROP FUNCTION IF EXISTS FnSommeAbon;
396
397DELIMITER $$
398CREATE FUNCTION FnSommeAbon(p_DateDebut Date, p_DateFin Date, p_TypeAbonne char)
399 RETURNS int
400BEGIN
401 DECLARE v_somme int;
402SELECT sum(trans.montant) FROM tblTransaction as trans
403 INNER JOIN tblAbonnement as abonnem
404 ON trans.idTransaction = abonnem.idTransaction
405 INNER JOIN tblAbonne as abon
406 ON abonnem.idAbonne = abon.idAbonne
407 WHERE abon.typeAbonne = p_TypeAbonne AND abonnem.dateDebutAbonne <= p_DateFin AND abonnem.dateDebutAbonne > p_DateDebut
408 INTO v_somme;
409
410 IF (v_somme is null) THEN
411 SET v_somme = 0;
412 END IF ;
413
414 RETURN v_somme ;
415END$$
416DELIMITER ;
417
418-- --------------------------------------------
419-- Fonction pour connaitre NOMBRE OCCASIONNEL
420-- --------------------------------------------
421DROP FUNCTION IF EXISTS FnNbOcca;
422
423DELIMITER $$
424CREATE FUNCTION FnNbOcca(p_DateDebut Date, p_DateFin Date)
425 RETURNS int
426BEGIN
427 DECLARE v_nb int;
428SELECT count(*) FROM tblTransaction as trans
429 INNER JOIN tblOccasionnel as occa
430 ON trans.idOccas = occa.idOccas
431 WHERE CAST(occa.dateFinOccas as Date) <= p_DateFin AND CAST(occa.dateDebutOccas as Date) >= p_DateDebut
432 INTO v_nb;
433
434 IF (v_nb is null) THEN
435 SET v_nb = 0;
436 END IF ;
437
438 RETURN v_nb ;
439END$$
440DELIMITER ;
441
442select FnNbOcca(curdate(),curdate());
443-- --------------------------------------------
444-- Fonction pour connaitre SOMME OCCASIONNEL
445-- --------------------------------------------
446DROP FUNCTION IF EXISTS FnSommeOcca;
447
448DELIMITER $$
449CREATE FUNCTION FnSommeOcca(p_DateDebut Date, p_DateFin Date)
450 RETURNS int
451BEGIN
452 DECLARE v_nb int;
453SELECT sum(trans.montant) FROM tblTransaction as trans
454 INNER JOIN tblOccasionnel as occa
455 ON trans.idOccas = occa.idOccas
456 WHERE CAST(occa.dateFinOccas as Date) <= p_DateFin AND CAST(occa.dateDebutOccas as Date) >= p_DateDebut
457 INTO v_nb;
458
459 IF (v_nb is null) THEN
460 SET v_nb = 0;
461 END IF ;
462
463 RETURN v_nb ;
464END$$
465DELIMITER ;
466
467select FnSommeOcca(curdate(), curdate());
468call SortieOccasionnel('2018042225', 1, '4000000000000002');
469update tblOccasionnel set dateFinOccas = '2018-04-24 10:10:10' where idOccas = '2018042225';
470-- -----------------------------------------------------
471-- Procédures ------------------------------------------
472-- -----------------------------------------------------
473-- ------------------------------------------------------------------------------
474-- Procédure pour la création des places dans tblPlace selon le nombre et le type
475-- ------------------------------------------------------------------------------
476DROP PROCEDURE IF EXISTS InsertPlaces;
477
478DELIMITER $$
479CREATE PROCEDURE `InsertPlaces`(in p_nb int, in p_type varchar(25))
480BEGIN
481 DECLARE v_i int DEFAULT 1;
482
483 WHILE v_i <= p_nb DO
484 INSERT INTO tblPlace (typePlace,idAbonne) VALUES(p_type, null);
485 SET v_i = v_i + 1;
486 END WHILE;
487END $$
488DELIMITER ;
489
490-- -------------------------------------------------------------------------
491-- Procédure CompterPlaceDispo(), va retourner le nombre de place disponible
492-- en tenant compte des places utilisés par les abonnés et occasionnels
493-- -------------------------------------------------------------------------
494DROP PROCEDURE IF EXISTS CompterPlaceDispo;
495
496DELIMITER $$
497CREATE PROCEDURE `CompterPlaceDispo`()
498BEGIN
499 DECLARE v_nbOccas int;
500 DECLARE v_nbAbon int;
501 DECLARE v_nbTotal int;
502
503 SET v_nbOccas = FnNbPlaceOccas();
504 SET v_nbAbon = FnNbPlaceAbonne();
505 SET v_nbTotal = 2500 - v_nbOccas -v_nbAbon;
506 SELECT v_nbTotal as 'Stationnement disponible';
507END$$
508DELIMITER ;
509
510#call CompterPlaceDispo();
511
512-- -----------------------------------------------------------------------------------
513-- Procédure SommesPercuesParType(), va calculer les montants percus par type d'usager
514-- en se basant sur des dates fournis
515-- -----------------------------------------------------------------------------------
516DROP PROCEDURE IF EXISTS SommesPercuesParType;
517
518DELIMITER $$
519CREATE PROCEDURE `SommesPercuesParType`(IN p_dateDebut Date, IN p_dateFin Date)
520BEGIN
521 DECLARE v_type varchar(8);
522 DECLARE v_nbAbon int;
523 DECLARE v_nbOccas int;
524 DECLARE v_somme decimal(9,2);
525 DECLARE v_total int;
526
527 SELECT * FROM tblAbonne as ab
528 INNER JOIN tblAbonnement as abt
529 ON ab.idAbonne = abt.idAbonne
530 WHERE abt.dateDebutAbonne >= p_dateDebut and abt.dateFinAbonne <= p_dateFin;
531END$$
532DELIMITER ;
533
534#call SommesPercuesParType('2018-01-01', curdate());
535
536-- -----------------------------------------------------
537-- Procédure AjoutAbonne(), va faire l'ajout d'un abonné
538-- -----------------------------------------------------
539DROP PROCEDURE IF EXISTS AjoutAbonne;
540
541DELIMITER $$
542CREATE PROCEDURE `AjoutAbonne`(in p_nomAbonne varchar(50), in p_prenomAbonne varchar(25), in p_codePostal varchar(7),
543 in p_telephone varchar(15), in p_courriel varchar(50), in p_typeAbonne char(1), in p_idVille int)
544BEGIN
545 SET @abCount = -1;
546 SELECT COUNT(*) INTO @abCount FROM `tblAbonne` WHERE `nomAbonne` = p_nomAbonne and `prenomAbonne` = p_prenomAbonne
547 or `nomAbonne` = p_nomAbonne and `prenomAbonne` is null;
548 SELECT COUNT(*) INTO @abCount FROM tblAbonne as abon WHERE abon.nomAbonne = p_nomAbonne and abon.prenomAbonne = p_prenomAbonne and abon.telephone = p_telephone
549 or abon.nomAbonne = p_nomAbonne and abon.prenomAbonne is null and abon.telephone = p_telephone;
550 IF @abCount = 0 THEN
551 INSERT INTO tblAbonne (nomAbonne,prenomAbonne,codePostal,telephone,courriel,typeAbonne,idVille)
552 VALUES(p_nomAbonne,p_prenomAbonne,p_codePostal,p_telephone,p_courriel,p_typeAbonne,p_idVille);
553 ELSE
554 SELECT 'Abonné déjà existant';
555 END IF ;
556END$$
557DELIMITER ;
558
559use TP3;
560
561#select * from tblAbonne order by nomAbonne desc;
562#call AjoutAbonne('Leblanc', 'Robby', 'Q4A2O7', '1-195-388-6491', 'nec@Nulla.co.uk', 'S', 3);
563
564-- -----------------------------------------------------------------------------------------------------------------------
565-- Procédure AjoutTransactionAbonne(), va ajouter la transaction pour l'action d'abonnement et retourner l'id de celle-ci
566-- -----------------------------------------------------------------------------------------------------------------------
567DROP PROCEDURE IF EXISTS AjoutTransactionAbonne;
568
569DELIMITER $$
570CREATE PROCEDURE `AjoutTransactionAbonne`(in p_duree int, in p_numPaiement varchar(16), in p_idPaiement int, out LIDTRANSACABON int)
571BEGIN
572 DECLARE v_tMois int DEFAULT 240;
573 DECLARE v_tAn int DEFAULT 2500;
574 DECLARE v_mTotal int;
575 DECLARE v_montant decimal(6,2);
576 IF (p_duree > 11) THEN
577 SET v_mTotal = CEIL(p_duree / 12) * v_tAn;
578 ELSE
579 SET v_mTotal = p_duree * v_tMois;
580 END IF;
581
582 SET v_montant = v_mTotal;
583
584 INSERT INTO tblTransaction (montant,numPaiement,idPaiement,idAbonnement,idOccas)
585 VALUES(v_montant,p_numPaiement,p_idPaiement,null,null);
586
587 SET LIDTRANSACABON = LAST_INSERT_ID();
588END$$
589DELIMITER ;
590
591-- --------------------------------------------------
592-- Procédure AjoutAbonnement(), va créer l'abonnement
593-- --------------------------------------------------
594DROP PROCEDURE IF EXISTS AjoutAbonnement;
595
596DELIMITER $$
597CREATE PROCEDURE `AjoutAbonnement`(in p_dateDebut Date, in p_duree int, in p_idAbonne int, in p_idPaiement int, in p_numPaiement varchar(16)
598 , in p_idVehicule varchar(7), in p_idPlace smallint)
599BEGIN
600 DECLARE v_idTransac int;
601 DECLARE v_dateFin date;
602
603 SET @abtCount = -1;
604 SELECT COUNT(*) INTO @abtCount FROM tblAbonnement where idVehicule = p_idVehicule and dateFinAbonne >= p_dateDebut
605 or idPlace = p_idPlace and dateFinAbonne >= p_dateDebut;
606
607 IF (@abtCount = 0) THEN
608 SET v_dateFin = DATE_ADD(p_dateDebut, INTERVAL p_duree MONTH);
609 -- Appele la procedure pour générer la transaction de l'abonnement
610 call AjoutTransactionAbonne(p_duree, p_numPaiement, p_idPaiement, @LIDTRANSACABON);
611
612 INSERT INTO tblAbonnement (dateDebutAbonne,dateFinAbonne,idAbonne,idVehicule,idPlace,idTransaction)
613 VALUES(p_dateDebut,v_dateFin,p_idAbonne,p_idVehicule,p_idPlace,@LIDTRANSACABON);
614
615 UPDATE tblTransaction set idAbonnement = LAST_INSERT_ID() where idTransaction = @LIDTRANSACABON;
616 ELSE
617 SELECT 'Un abonnement avec la même plaque ou place est toujours actif!';
618 END IF;
619END$$
620DELIMITER ;
621
622#select 'ALLO';
623#SELECT COUNT(*) INTO @abtCount FROM tblAbonnement where idVehicule = 'XJA029' and dateFinAbonne >= '2018-10-20' or idPlace = 526 and dateFinAbonne >= '2018-10-20';
624#SELECT * FROM tblAbonnement where idVehicule = 'XJA028' and dateFinAbonne >= '2018-10-20';
625#SELECT @abtCount;
626#SELECT * from tblAbonnement where idVehicule = 'XJA028';
627#call AjoutAbonnement(curdate(), 10, 1, 4, null, 'XJA028', 414);
628#update tblAbonnement set dateFinAbonne = curdate()-1 where idAbonnement = 15;
629#SELECT * from tblAbonnement where idPlace = 1996;
630
631-- ----------------------------------------------------------------------------------------
632-- Procédure AjoutTransactionOcaas(), va ajouter la transaction pour l'action d'occasionnel
633-- ----------------------------------------------------------------------------------------
634DROP PROCEDURE IF EXISTS AjoutTransactionOccas;
635
636DELIMITER $$
637CREATE PROCEDURE `AjoutTransactionOccas`(in p_idOccas varchar(12), in p_idPaiement int, in p_numPaiement varchar(16), out LIDTRANSACOCCAS int)
638BEGIN
639 DECLARE v_montantOccas decimal(6,2);
640 DECLARE v_dateDebut datetime;
641
642 SELECT dateDebutOccas FROM tblOccasionnel AS occas
643 INNER JOIN tblTransaction as trans
644 ON occas.idOccas = trans.idOccas
645 WHERE occas.idOccas = p_idOccas
646 INTO @v_dateDebut;
647
648 #SET v_montantOccas = FnCalculeMontant(@v_dateDebut, now());
649 SET v_montantOccas = FnCalculerMontant(p_idOccas);
650
651 INSERT INTO tblTransaction (montant,numPaiement,idPaiement,idAbonnement,idOccas)
652 VALUES(v_montantOccas,p_numPaiement,p_idPaiement,null,p_idOccas);
653
654 SET LIDTRANSACOCCAS = LAST_INSERT_ID();
655END$$
656DELIMITER ;
657
658-- ---------------------------------------------------------------
659-- Procédure AjoutOccasionnel(), va créer l'ajout d'un occasionnel
660-- ---------------------------------------------------------------
661DROP PROCEDURE IF EXISTS AjoutOccasionnel;
662
663DELIMITER $$
664CREATE PROCEDURE `AjoutOccasionnel`()
665BEGIN
666
667 DECLARE v_nbIdTotal int DEFAULT 0;
668 DECLARE v_nbIdAvant int DEFAULT 0;
669 DECLARE v_idOccas varchar(12);
670
671 SELECT count(*) into v_nbIdTotal
672 FROM tblOccasionnel;
673 SELECT count(*) into v_nbIdAvant
674 FROM tblOccasionnel WHERE TIMESTAMPDIFF(DAY, dateDebutOccas, now() != 0);
675
676 SET v_idOccas = concat(DATE_FORMAT(curdate(), '%Y%m%d'), v_nbIdTotal - v_nbIdAvant + 1);
677
678 INSERT INTO tblOccasionnel VALUES(v_idOccas,now(),null,null);
679
680END$$
681DELIMITER ;
682
683-- --------------------------------------------------------------------------------------
684-- Procédure SortieOccasionnel(), va mettre à jour l'occasionel lorsque la voiture quitte
685-- --------------------------------------------------------------------------------------
686DROP PROCEDURE IF EXISTS SortieOccasionnel;
687
688DELIMITER $$
689CREATE PROCEDURE `SortieOccasionnel`(in p_idOccas varchar(12), in p_idPaiement int, in p_numPaiement varchar(16))
690BEGIN
691 SET @occasCount = -1;
692 SELECT COUNT(*) INTO @occasCount FROM tblOccasionnel where idOccas = p_idOccas and dateFinOccas is not null;
693 IF (@occasCount = 0) THEN
694 call AjoutTransactionOccas(p_idOccas, p_idPaiement, p_numPaiement, @LIDTRANSACOCCAS);
695
696 UPDATE tblOccasionnel set dateFinOccas = now(), idTransaction = @LIDTRANSACOCCAS WHERE idOccas = p_idOccas;
697 ELSE
698 SELECT 'Cet occasionel a déjà quitté';
699 END IF;
700END$$
701DELIMITER ;
702
703#update tblOccasionnel set dateDebutOccas = '2018-04-21 15:00:00' where idOccas = '201804212';
704#call SortieOccasionnel('201804216', 4, null);
705#call SortieOccasionnel('201804212', 1, '4000000000000002');
706#select * from tblTransaction where idTransaction = 67;
707#select * from tblOccasionnel where idOccas = '201804216';
708#select * from tblOccasionnel;
709
710-- -------------------------------------------------------------------------
711-- Procédure DepotQuotidien(), va retourner les achats par type de paiement et
712-- par type d'utilisateur
713-- -------------------------------------------------------------------------
714DROP PROCEDURE IF EXISTS DepotQuotidien;
715DROP TEMPORARY TABLE IF EXISTS TMP_depot;
716
717DELIMITER $$
718CREATE PROCEDURE `DepotQuotidien`(p_DateVerif Date)
719BEGIN
720 DECLARE v_CrOcca int;
721 DECLARE v_CrAbon int;
722 DECLARE v_CoOcca int;
723 DECLARE v_CoAbon int;
724 set v_CrOcca = FnCreditOcca(p_DateVerif);
725 set v_CrAbon = FnCreditAbonne(p_DateVerif);
726 set v_CoOcca = FnComptantOcca(p_DateVerif);
727 set v_CoAbon = FnComptantAbonne(p_DateVerif);
728
729
730 CREATE TEMPORARY TABLE TMP_depot (
731 type_Depot varchar(25),
732 Abonnements DECIMAL(9,2),
733 Occasionnels DECIMAL(9,2));
734
735 INSERT INTO TMP_depot (type_Depot, Abonnements, Occasionnels)
736 VALUE ('Carte de crédit', v_CrAbon, v_CrOcca);
737 INSERT INTO TMP_depot (type_Depot, Abonnements, Occasionnels)
738 VALUE ('Argent comptant', v_CoAbon, v_CoOcca);
739 INSERT INTO TMP_depot (type_Depot, Abonnements, Occasionnels)
740 VALUE ('Total', (v_CoAbon + v_CrAbon), (v_CoOcca + v_CrOcca));
741
742 SELECT * From TMP_depot;
743
744 DROP TEMPORARY TABLE TMP_depot;
745
746END$$
747DELIMITER ;
748
749call DepotQuotidien(curdate());
750#SELECT SUM(FnCreditOcca(curdate()) + FnComptantOcca(curdate()));
751
752 -- --------------------------------------------------------------------------------
753-- Procédure PlacesAbonnes() donne les places occupées par des abonnements à tout
754-- moment. Ce moment doit être déterminé par une date passé en paramètre.
755-- --------------------------------------------------------------------------------
756DROP PROCEDURE IF EXISTS PlacesAbonnes;
757
758DELIMITER $$
759CREATE PROCEDURE `PlacesAbonnes`(p_Moment Datetime)
760BEGIN
761 SELECT place.idPlace as 'Place',
762 concat(voiture.marque, ' ',voiture.modele, ', ',voiture.couleur, ', ',voiture.idVehicule) as 'Véhicule',
763 abon.dateDebutAbonne as 'Date de début', abon.dateFinAbonne as 'Date de fin'
764 FROM tblAbonnement as abon
765 INNER join tblVehicule as voiture
766 ON abon.idVehicule = voiture.idVehicule
767 INNER join tblPlace as place
768 ON abon.idPlace = place.idPlace
769 UNION
770 SELECT COUNT(*) as 'Total'
771 FROM tblPlace as place
772 INNER join tblAbonnement as abon
773 ON abon.idPlace = place.idPlace
774 INNER join tblVehicule as voiture
775 ON abon.idVehicule = voiture.idVehicule;
776
777END$$
778DELIMITER ;
779
780call PlacesAbonnes(now());
781-- --------------------------------------------------------------------------------
782-- Procédure ventilation donne la ventilation dans un tableau des sommes
783-- perçues par type de personne (physique ou société), les véhicules occasionnels.
784-- --------------------------------------------------------------------------------
785DROP PROCEDURE IF EXISTS ventilation;
786DROP TEMPORARY TABLE IF EXISTS TMP_ventilation;
787
788DELIMITER $$
789CREATE PROCEDURE `ventilation`(p_DateDebut Date, p_DateFin Date)
790BEGIN
791 DECLARE v_NnOcca int;
792 DECLARE v_NbAbonSoc int;
793 DECLARE v_NbAbonPers int;
794 DECLARE v_SommeOcca DEC(9,2);
795 DECLARE v_SommeAbonSoc DEC(9,2);
796 DECLARE v_SommeAbonPers DEC(9,2);
797
798 SET v_NnOcca = FnNbOcca(p_DateDebut, p_DateFin);
799 SET v_NbAbonSoc = FnNbAbon(p_DateDebut, p_DateFin, 's');
800 SET v_NbAbonPers = FnNbAbon(p_DateDebut, p_DateFin, 'p');
801 SET v_SommeOcca = FnSommeOcca(p_DateDebut, p_DateFin);
802 SET v_SommeAbonSoc = FnSommeAbon(p_DateDebut, p_DateFin, 's');
803 SET v_SommeAbonPers = FnSommeAbon(p_DateDebut, p_DateFin, 'p');
804
805 CREATE TEMPORARY TABLE TMP_ventilation (
806 TypeDeClient varchar(25),
807 NbAbonnements INT,
808 SommeAbonnements DECIMAL(9,2),
809 NbOccasionnels INT,
810 SommeOccasionnels DECIMAL(9,2));
811
812 INSERT INTO TMP_ventilation (TypeDeClient, NbAbonnements, SommeAbonnements, NbOccasionnels, SommeOccasionnels)
813 VALUE ('Personne', v_NbAbonPers, v_SommeAbonPers, v_NnOcca, v_SommeOcca);
814 INSERT INTO TMP_ventilation (TypeDeClient, NbAbonnements, SommeAbonnements, NbOccasionnels, SommeOccasionnels)
815 VALUE ('Société', v_NbAbonSoc, v_SommeAbonSoc, null, null);
816 INSERT INTO TMP_ventilation (TypeDeClient, NbAbonnements, SommeAbonnements, NbOccasionnels, SommeOccasionnels)
817 VALUE ('Total', (v_NbAbonPers+v_NbAbonSoc), (v_SommeAbonPers+v_SommeAbonSoc), v_NnOcca, v_SommeOcca);
818
819 SELECT TypeDeClient as '', NbAbonnements, SommeAbonnements, NbOccasionnels, SommeOccasionnels From TMP_ventilation;
820
821 DROP TEMPORARY TABLE TMP_ventilation;
822
823END$$
824DELIMITER ;
825
826call ventilation(curdate()-1, curdate());
827
828###############################
829### INSERTIONS DES DONNÉÉES ###
830###############################
831
832SET FOREIGN_KEY_CHECKS = 1;
833
834use TP3;
835
836-- appele de la procédure pour faire l'insertion des places
837call InsertPlaces(500, 'plein air');
838call InsertPlaces(2000, 'couvert');
839
840-- insertion manuelle, car pas besoin de beaucoup de ville
841INSERT INTO tblVille (nomville) VALUES("Québec"),("Montréal"),("Laval"),("Shannon"),("St-Gabriel-de-Valcartier"),("Val-Bélair"),("Loretteville"),("Charlesbourg"),("Beauport");
842
843-- insertion manuelle avec des données produite par generatedata
844INSERT INTO tblAbonne (`nomAbonne`,`prenomAbonne`,`codePostal`,`telephone`,`courriel`,`typeAbonne`,`idVille`)
845 VALUES ("Harveys",NULL,"Q4A2O7","1-195-388-6491","nec@Nulla.co.uk","S","3"),("Valentine","Cooper","K6A6V3","1-398-119-9628","habitant.morbi@diamluctuslobortis.com","P","3"),("Hedley","Harrell","B9D5S6","1-630-751-9399","leo@cursusaenim.ca","P","8"),("Gary","Roberts","Q2R2V3","1-965-296-3762","Morbi.accumsan.laoreet@fermentum.co.uk","P","7"),("Francis","Austin","P8O0F3","1-892-685-0409","tempor.diam@sociis.org","P","1"),("Edward","Mosley","S8J5W1","1-385-850-5982","et@urnaet.ca","P","3"),("Benedict","Bell","U0K6N4","1-570-196-4909","nunc.sit.amet@penatibusetmagnis.co.uk","P","8"),("Stewart","Soto","B1A4D5","1-893-189-9061","nec.metus.facilisis@enimcondimentumeget.ca","P","1"),("Neville","Marquez","O1J0H8","1-868-749-2997","ipsum.ac@diamnunc.edu","P","3"),("Zachery","Zimmerman","Y6T5A4","1-765-696-4719","et.tristique.pellentesque@dignissimtemporarcu.edu","P","5");
846INSERT INTO tblAbonne (`nomAbonne`,`prenomAbonne`,`codePostal`,`telephone`,`courriel`,`typeAbonne`,`idVille`)
847 VALUES ("Simons",NULL,"Q9R5F5","1-878-506-6536","dictum.eu@a.edu","S","1"),("Steven","Hampton","T0V9T4","1-736-971-6586","Lorem.ipsum@ante.com","P","9"),("Jamal","Ramirez","V3F5Y9","1-826-233-8755","Donec.consectetuer@fringilla.net","P","2"),("Norman","Fields","M9C8J1","1-411-164-1888","a.feugiat@erosnectellus.net","P","2"),("Wyatt","Christensen","J8E3J3","1-746-245-0521","feugiat.Lorem.ipsum@nullaInteger.ca","P","1"),("Bert","Frazier","O0S9T6","1-781-873-6623","Aliquam.fringilla@Quisquelibero.com","P","7"),("Louis","House","I8M6E3","1-974-211-5774","ultrices.mauris@Integervitae.net","P","9"),("Alden","Cote","F3J7B6","1-893-631-0078","tristique.ac@quisturpis.net","P","5"),("Dolan","Fischer","L9Z4M5","1-141-234-6710","risus.Quisque@odiosagittissemper.co.uk","P","4"),("Zane","Manning","L6Y8S3","1-595-890-4447","imperdiet@placerataugueSed.com","P","4");
848INSERT INTO tblAbonne (`nomAbonne`,`prenomAbonne`,`codePostal`,`telephone`,`courriel`,`typeAbonne`,`idVille`)
849 VALUES ("Sears",NULL,"Q7J7T4","1-337-614-9853","lacinia.vitae@malesuada.co.uk","S","6"),("Channing","Chapman","I9D6T3","1-530-608-2930","velit.Quisque@lectus.edu","P","7"),("Declan","Gamble","F4T1A0","1-431-958-2193","Fusce@lectusantedictum.co.uk","P","1"),("Zeph","Grant","U6Q2T3","1-662-303-3018","elit.Aliquam@nibh.ca","P","6"),("Reuben","Crane","Q2M9Z2","1-108-887-7001","dui.semper.et@felisDonec.co.uk","P","1"),("Cairo","Hensley","Z6V4Q0","1-193-858-1665","erat@nonluctussit.org","P","1"),("Acton","Garza","S5P7Q3","1-410-750-3337","neque@Quisque.net","P","1"),("Victor","Beach","B6V2T6","1-361-585-6601","at.risus@nonquamPellentesque.net","P","9"),("Brett","Doyle","O9P4R4","1-929-469-2034","aliquam@semmolestie.edu","P","9"),("Quentin","Haynes","X1D1L4","1-489-636-2755","nisl.Nulla.eu@vitaedolorDonec.edu","P","1");
850INSERT INTO tblAbonne (`nomAbonne`,`prenomAbonne`,`codePostal`,`telephone`,`courriel`,`typeAbonne`,`idVille`)
851 VALUES ("Canac",NULL,"Y1C5C7","1-321-806-6027","In.ornare@adipiscingMaurismolestie.co.uk","S","6"),("Amery","Fletcher","A7V3H7","1-922-766-4167","ut.sem@sitametconsectetuer.edu","P","8"),("Lawrence","Conley","N2J8S1","1-309-234-5089","nec@luctuslobortisClass.net","P","9"),("Herman","Clarke","C4Q6N4","1-594-470-9280","Morbi.metus@dolor.net","P","9"),("Alvin","Garrison","I9N0E9","1-807-832-5544","augue.malesuada.malesuada@Nuncsedorci.co.uk","P","7"),("Jesse","Meyers","G0O1X7","1-593-459-8094","adipiscing.ligula@semper.edu","P","6"),("Martin","Montgomery","L8Z8W1","1-951-412-8004","molestie@Donecconsectetuer.net","P","9"),("Yuli","Wiley","T8I8E4","1-397-165-4584","diam.luctus@nec.org","P","8"),("Kareem","Singleton","Z3H8Q9","1-199-250-4773","et.libero.Proin@leo.edu","P","4"),("Ezekiel","Hardin","N3A6B8","1-425-440-1465","euismod@placerat.co.uk","P","4");
852INSERT INTO tblAbonne (`nomAbonne`,`prenomAbonne`,`codePostal`,`telephone`,`courriel`,`typeAbonne`,`idVille`)
853 VALUES ("Ardenne",NULL,"A0G8T1","1-520-529-5925","Sed.nunc.est@antebibendumullamcorper.com","S","4"),("Donovan","Boyle","V9U4T5","1-116-449-6440","iaculis.odio.Nam@ipsumdolor.ca","P","5"),("Zachary","Wagner","I5I8I3","1-768-867-8790","Integer.aliquam@auguescelerisque.co.uk","P","4"),("Josiah","Pickett","W5L4F1","1-859-110-2038","enim.Suspendisse@leo.ca","P","7"),("Demetrius","Reed","K6E8S2","1-979-137-2222","Sed.nunc.est@laciniaatiaculis.org","P","6"),("Ezra","Strong","H7C2N6","1-403-362-0716","laoreet@metusAliquam.org","P","4"),("Slade","Mann","P7C1V8","1-812-124-1004","venenatis@laciniavitae.org","P","8"),("Emmanuel","George","U2R8C6","1-477-621-3758","feugiat.nec.diam@non.net","P","8"),("John","Warner","P8D4H6","1-276-604-5994","mi@feugiat.co.uk","P","3"),("Lucius","Rocha","U2D1F6","1-191-164-7881","erat.volutpat@posuerevulputatelacus.com","P","7");
854
855INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Lexus","Multi-Segment","violet"),(FnRandomPlaque(),"Fiat","Pickup","grey"),(FnRandomPlaque(),"Lexus","Utilitaire","black"),(FnRandomPlaque(),"Ford","VUS","yellow"),(FnRandomPlaque(),"BMW","Utilitaire","blue"),(FnRandomPlaque(),"Acura","Utilitaire","blue"),(FnRandomPlaque(),"Lexus","Berline","red"),(FnRandomPlaque(),"Dacia","Multi-Segment","violet"),(FnRandomPlaque(),"Peugeot","Multi-Segment","violet"),(FnRandomPlaque(),"General Motors","Berline","blue");
856INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Kia Motors","Hatchback","red"),(FnRandomPlaque(),"Mahindra and Mahindra","Hatchback","yellow"),(FnRandomPlaque(),"Audi","Berline","green"),(FnRandomPlaque(),"Dacia","Hatchback","black"),(FnRandomPlaque(),"Seat","Pickup","indigo"),(FnRandomPlaque(),"Audi","Sport","orange"),(FnRandomPlaque(),"Mitsubishi Motors","Sport","orange"),(FnRandomPlaque(),"Kia Motors","Hatchback","blue"),(FnRandomPlaque(),"Subaru","Hatchback","grey"),(FnRandomPlaque(),"Mahindra and Mahindra","Berline","indigo");
857INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Daihatsu","Utilitaire","indigo"),(FnRandomPlaque(),"Mercedes-Benz","Hatchback","grey"),(FnRandomPlaque(),"Infiniti","Pickup","blue"),(FnRandomPlaque(),"BMW","Pickup","indigo"),(FnRandomPlaque(),"Chrysler","Pickup","blue"),(FnRandomPlaque(),"Lincoln","Utilitaire","grey"),(FnRandomPlaque(),"Suzuki","Berline","red"),(FnRandomPlaque(),"Mahindra and Mahindra","Hatchback","blue"),(FnRandomPlaque(),"BMW","Multi-Segment","blue"),(FnRandomPlaque(),"Dongfeng Motor","Pickup","orange");
858INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Nissan","VUS","violet"),(FnRandomPlaque(),"Subaru","Berline","yellow"),(FnRandomPlaque(),"Daimler","Berline","indigo"),(FnRandomPlaque(),"Daihatsu","Pickup","red"),(FnRandomPlaque(),"Honda","Multi-Segment","violet"),(FnRandomPlaque(),"BMW","Hatchback","yellow"),(FnRandomPlaque(),"Mazda","Pickup","violet"),(FnRandomPlaque(),"Kia Motors","Sport","green"),(FnRandomPlaque(),"Skoda","Sport","violet"),(FnRandomPlaque(),"Skoda","VUS","orange");
859INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Daimler","Berline","blue"),(FnRandomPlaque(),"Toyota","Utilitaire","grey"),(FnRandomPlaque(),"Renault","Multi-Segment","blue"),(FnRandomPlaque(),"Cadillac","Hatchback","red"),(FnRandomPlaque(),"Dodge","Pickup","red"),(FnRandomPlaque(),"Lexus","Berline","red"),(FnRandomPlaque(),"RAM Trucks","Multi-Segment","yellow"),(FnRandomPlaque(),"Daimler","Berline","orange"),(FnRandomPlaque(),"Chevrolet","Berline","green"),(FnRandomPlaque(),"Subaru","Sport","violet");
860INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Renault","Berline","blue"),(FnRandomPlaque(),"Mazda","Hatchback","indigo"),(FnRandomPlaque(),"Fiat","Utilitaire","indigo"),(FnRandomPlaque(),"Chrysler","Sport","blue"),(FnRandomPlaque(),"Mitsubishi Motors","VUS","violet"),(FnRandomPlaque(),"Lexus","Hatchback","grey"),(FnRandomPlaque(),"Chevrolet","VUS","indigo"),(FnRandomPlaque(),"Skoda","Hatchback","red"),(FnRandomPlaque(),"GMC","Utilitaire","orange"),(FnRandomPlaque(),"Subaru","VUS","yellow");
861INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Vauxhall","Sport","grey"),(FnRandomPlaque(),"Jeep","Pickup","yellow"),(FnRandomPlaque(),"Kenworth","VUS","grey"),(FnRandomPlaque(),"Chevrolet","Pickup","black"),(FnRandomPlaque(),"Isuzu","VUS","blue"),(FnRandomPlaque(),"JLR","Multi-Segment","violet"),(FnRandomPlaque(),"Dacia","VUS","indigo"),(FnRandomPlaque(),"Smart","Sport","blue"),(FnRandomPlaque(),"Lexus","Pickup","yellow"),(FnRandomPlaque(),"Mitsubishi Motors","Utilitaire","red");
862INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"GMC","Hatchback","red"),(FnRandomPlaque(),"Cadillac","Multi-Segment","green"),(FnRandomPlaque(),"Kia Motors","Pickup","red"),(FnRandomPlaque(),"Mahindra and Mahindra","Pickup","blue"),(FnRandomPlaque(),"Citroën","VUS","indigo"),(FnRandomPlaque(),"Vauxhall","Hatchback","green"),(FnRandomPlaque(),"Fiat","Pickup","violet"),(FnRandomPlaque(),"Ferrari","Berline","black"),(FnRandomPlaque(),"Smart","Multi-Segment","indigo"),(FnRandomPlaque(),"Volkswagen","Utilitaire","grey");
863INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Volvo","Multi-Segment","orange"),(FnRandomPlaque(),"Hyundai Motors","Berline","black"),(FnRandomPlaque(),"Hyundai Motors","Multi-Segment","green"),(FnRandomPlaque(),"GMC","Utilitaire","orange"),(FnRandomPlaque(),"General Motors","Utilitaire","yellow"),(FnRandomPlaque(),"BMW","Sport","grey"),(FnRandomPlaque(),"Peugeot","Sport","black"),(FnRandomPlaque(),"Seat","Multi-Segment","red"),(FnRandomPlaque(),"Mahindra and Mahindra","Berline","red"),(FnRandomPlaque(),"Lincoln","Pickup","black");
864INSERT INTO `tblVehicule` VALUES (FnRandomPlaque(),"Toyota","Pickup","grey"),(FnRandomPlaque(),"Lincoln","Berline","orange"),(FnRandomPlaque(),"Seat","Utilitaire","yellow"),(FnRandomPlaque(),"Lexus","Multi-Segment","red"),(FnRandomPlaque(),"Mahindra and Mahindra","VUS","grey"),(FnRandomPlaque(),"Subaru","Berline","blue"),(FnRandomPlaque(),"Daimler","Hatchback","indigo"),(FnRandomPlaque(),"MINI","Sport","yellow"),(FnRandomPlaque(),"Peugeot","Berline","yellow"),(FnRandomPlaque(),"Smart","Berline","yellow");
865
866select count(*) from tblVehicule;
867
868-- insertion manuelle, car pas assé de donnée à générer
869INSERT INTO tblMethodPaiement (typePaiement,descPaiement) VALUES("V","Visa"),("M","MasterCard"),("X","Amex"),("A","Comptant"),("C","Chèque");
870
871-- -------------------------------------------------------------------
872-- Procédure pour ajouter des occasionnels en lot selon une qte donnée
873-- -------------------------------------------------------------------
874DELIMITER $$
875CREATE PROCEDURE `AjoutOccasEnLot`(in p_nb int)
876BEGIN
877 DECLARE v_i int DEFAULT 1;
878 WHILE v_i <= p_nb DO
879 call AjoutOccasionnel();
880 SET v_i = v_i + 1;
881 END WHILE;
882END $$
883DELIMITER ;
884
885call AjoutOccasEnLot(150);
886
887#select count(*) from tblOccasionnel;
888
889-- -----------------------------------------------------------------
890-- Procédure pour ajouter des Abonnement en lot selon une qte donnée
891-- -----------------------------------------------------------------
892DELIMITER $$
893CREATE PROCEDURE `AjoutAbonEnLot`(in p_nb int)
894BEGIN
895 DECLARE v_i int DEFAULT 1;
896 DECLARE v_idAbon int;
897 DECLARE v_nbDuree int;
898 DECLARE v_idPaiement int;
899 DECLARE v_idVehicule varchar(6);
900 DECLARE v_idPlace int;
901 WHILE v_i <= p_nb DO
902 SET v_idAbon = FLOOR(RAND()*(50-1+1))+1;
903 SET v_nbDuree = FLOOR(RAND()*(12-1+1))+1;
904 SET v_idPaiement = FLOOR(RAND()*(5-1+1))+1;
905 SET v_idVehicule = (SELECT idVehicule FROM tblVehicule ORDER BY RAND() LIMIT 1);
906 SET v_idPlace = FLOOR(RAND()*(2500-1+1))+1;
907 IF (v_idPaiement = 4) THEN
908 call AjoutAbonnement(curdate(),v_nbDuree,v_idAbon,v_idPaiement,null,v_idVehicule,v_idPlace);
909 ELSE
910 call AjoutAbonnement(curdate(),v_nbDuree,v_idAbon,v_idPaiement,'4000000000000002',v_idVehicule,v_idPlace);
911 END IF;
912 SET v_i = v_i + 1;
913 END WHILE;
914END $$
915DELIMITER ;
916
917call AjoutAbonEnLot(100);
918
919############################
920########## TESTS ###########
921############################
922
923SELECT COUNT(*) as `Nombre d'abonnement` from tblAbonnement;
924SELECT COUNT(*) as `Nombre d'occasionel` from tblOccasionnel;
925call CompterPlaceDispo();
926select FnNbAbon(curdate(), curdate(), 'P');
927#select * from tblAbonne where nomAbonne = 'Harveys';
928#insert into tblAbonne (nomAbonne,prenomAbonne,codePostal,telephone,courriel,typeAbonne,idVille) values('Harveys', null, 'Q4A2O7', '1-195-388-6491', 'nec@Nulla.co.uk', 'S', 3, @UnErreur);