forked from steffmartin/condominio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
429 lines (401 loc) · 11.3 KB
/
schema.sql
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
START TRANSACTION;
DROP TABLE IF EXISTS
lancamentos,transferencias,movimentos,
contasbancarias,contas,cobrancas,
orcamentos,subcategorias,categorias,periodos,
pessoasjuridicas,pessoasfisicas,pessoa_moradia,pessoas,
moradias,blocos,
autorizacoes,persistent_logins,usuarios,
condominios;
DROP EVENT IF EXISTS atTotalCobrancaDiariamente;
DROP EVENT IF EXISTS delRememberMe;
CREATE TABLE condominios (
idCondominio BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
razaoSocial VARCHAR(100) NULL,
cnpj VARCHAR(14) NULL,
ie VARCHAR(14) NULL,
im VARCHAR(30) NULL,
email VARCHAR(100) NULL,
telefone VARCHAR(10) NULL,
celular VARCHAR(11) NULL,
endereco VARCHAR(100) NULL,
numeroEnd VARCHAR(6) NULL,
complementoEnd VARCHAR(30) NULL,
bairro VARCHAR(30) NULL,
cidade VARCHAR(30) NULL,
estado VARCHAR(2) NULL,
cep VARCHAR(8) NULL,
PRIMARY KEY(idCondominio)
);
CREATE TABLE usuarios (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(50) UNIQUE NULL,
password VARCHAR(100) NULL,
ativo BOOL NULL,
nome VARCHAR(50) NULL,
sobrenome VARCHAR(100) NULL,
email VARCHAR(100) NULL,
idCondominio BIGINT UNSIGNED NULL,
PRIMARY KEY(id),
FOREIGN KEY(idCondominio)
REFERENCES condominios(idCondominio)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE autorizacoes (
id_usuario BIGINT UNSIGNED NOT NULL,
autorizacao VARCHAR(50) NOT NULL,
PRIMARY KEY(id_usuario, autorizacao),
FOREIGN KEY(id_usuario)
REFERENCES usuarios(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE persistent_logins (
series VARCHAR(64) NOT NULL,
username VARCHAR(50) NULL,
token VARCHAR(64) NULL,
last_used TIMESTAMP NULL,
PRIMARY KEY(series),
FOREIGN KEY(username)
REFERENCES usuarios(username)
ON DELETE CASCADE
ON UPDATE NO ACTION
);
CREATE TABLE blocos (
idBloco BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
sigla VARCHAR(3) NULL,
descricao VARCHAR(30) NULL,
idCondominio BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idBloco),
FOREIGN KEY(idCondominio)
REFERENCES condominios(idCondominio)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE moradias (
idMoradia BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
sigla VARCHAR(10) NULL,
tipo VARCHAR(2) NULL,
area FLOAT NULL,
fracaoIdeal FLOAT NULL,
matricula VARCHAR(30) NULL,
vagas INTEGER UNSIGNED NULL,
idBloco BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idMoradia),
FOREIGN KEY(idBloco)
REFERENCES blocos(idBloco)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE pessoas (
idPessoa BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
nome VARCHAR(50) NULL,
email VARCHAR(100) NULL,
telefone VARCHAR(15) NULL,
celular VARCHAR(15) NULL,
endereco VARCHAR(100) NULL,
numeroEnd VARCHAR(6) NULL,
complementoEnd VARCHAR(30) NULL,
bairro VARCHAR(30) NULL,
cidade VARCHAR(30) NULL,
estado VARCHAR(2) NULL,
cep VARCHAR(8) NULL,
idCondominio BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idPessoa),
FOREIGN KEY(idCondominio)
REFERENCES condominios(idCondominio)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE pessoasfisicas (
idPessoa BIGINT UNSIGNED NOT NULL,
sobrenome VARCHAR(100) NULL,
cpf VARCHAR(11) NULL,
rg VARCHAR(14) NULL,
nascimento DATE NULL,
genero CHAR(1) NULL,
PRIMARY KEY(idPessoa),
FOREIGN KEY(idPessoa)
REFERENCES pessoas(idPessoa)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE pessoasjuridicas (
idPessoa BIGINT UNSIGNED NOT NULL,
razaoSocial VARCHAR(100) NULL,
cnpj VARCHAR(14) NULL,
ie VARCHAR(14) NULL,
im VARCHAR(30) NULL,
nomeRepresentante VARCHAR(50) NULL,
sobrenomeRepresentante VARCHAR(100) NULL,
PRIMARY KEY(idPessoa),
FOREIGN KEY(idPessoa)
REFERENCES pessoas(idPessoa)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE contas (
idConta BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
sigla VARCHAR(2) NULL,
descricao VARCHAR(30) NULL,
saldoInicial DECIMAL(9,2) NULL,
saldoAtual DECIMAL(9,2) NULL,
idCondominio BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idConta),
FOREIGN KEY(idCondominio)
REFERENCES condominios(idCondominio)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE contasbancarias (
idConta BIGINT UNSIGNED NOT NULL,
tipo CHAR NULL,
banco VARCHAR(3) NULL,
agencia VARCHAR(5) NULL,
agenciaDv CHAR NULL,
conta VARCHAR(20) NULL,
contaDv CHAR NULL,
PRIMARY KEY(idConta),
FOREIGN KEY(idConta)
REFERENCES contas(idConta)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE categorias (
idCategoria BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tipo CHAR NULL,
descricao VARCHAR(50) NULL,
nivel INTEGER UNSIGNED NULL,
ordem VARCHAR(255) NULL,
idCategoriaPai BIGINT UNSIGNED NULL,
idCondominio BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idCategoria),
FOREIGN KEY(idCondominio)
REFERENCES condominios(idCondominio)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idCategoriaPai)
REFERENCES categorias(idCategoria)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE subcategorias (
idSubcategoria BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
descricao VARCHAR(50) NULL,
idCategoria BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idSubcategoria),
FOREIGN KEY(idCategoria)
REFERENCES categorias(idCategoria)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE pessoa_moradia (
idPessoa BIGINT UNSIGNED NOT NULL,
idMoradia BIGINT UNSIGNED NOT NULL,
tipo CHAR NULL,
participacaoDono FLOAT NULL,
dataEntrada DATE NULL,
dataSaida DATE NULL,
PRIMARY KEY(idPessoa, idMoradia),
FOREIGN KEY(idPessoa)
REFERENCES pessoas(idPessoa)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idMoradia)
REFERENCES moradias(idMoradia)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE cobrancas (
idCobranca BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
idMoradia BIGINT UNSIGNED NOT NULL,
motivoEmissao CHAR NULL,
numero VARCHAR(10) NULL,
parcela VARCHAR(3) NULL,
dataEmissao DATE NULL,
dataVencimento DATE NULL,
valor DECIMAL(9,2) NULL,
desconto DECIMAL(9,2) NULL,
abatimento DECIMAL(9,2) NULL,
outrasDeducoes DECIMAL(9,2) NULL,
jurosMora DECIMAL(9,2) NULL,
multa DECIMAL(9,2) NULL,
outrosAcrescimos DECIMAL(9,2) NULL,
total DECIMAL(9,2) NULL,
descricao VARCHAR(255) NULL,
percentualJurosMes FLOAT NULL,
percentualMulta FLOAT NULL,
situacao CHAR NULL,
dataRecebimento DATE NULL,
motivoBaixa CHAR NULL,
idCondominio BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idCobranca),
FOREIGN KEY(idCondominio)
REFERENCES condominios(idCondominio)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idMoradia)
REFERENCES moradias(idMoradia)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE periodos (
idPeriodo BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
inicio DATE NULL,
fim DATE NULL,
encerrado BOOL NULL,
idCondominio BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idPeriodo),
FOREIGN KEY(idCondominio)
REFERENCES condominios(idCondominio)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE orcamentos (
idOrcamento BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
idPeriodo BIGINT UNSIGNED NOT NULL,
idSubcategoria BIGINT UNSIGNED NOT NULL,
orcado DECIMAL(9,2) NULL,
PRIMARY KEY(idOrcamento),
FOREIGN KEY(idSubcategoria)
REFERENCES subcategorias(idSubcategoria)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idPeriodo)
REFERENCES periodos(idPeriodo)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE movimentos (
idMovimento BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
data DATE NULL,
valor DECIMAL(9,2) NULL,
documento VARCHAR(20) NULL,
descricao VARCHAR(255) NULL,
reducao BOOL NULL,
idConta BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idMovimento),
FOREIGN KEY(idConta)
REFERENCES contas(idConta)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE transferencias (
idMovimento BIGINT UNSIGNED NOT NULL,
idContaInversa BIGINT UNSIGNED NOT NULL,
idMovimentoInverso BIGINT UNSIGNED NULL,
PRIMARY KEY(idMovimento),
FOREIGN KEY(idContaInversa)
REFERENCES contas(idConta)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idMovimento)
REFERENCES movimentos(idMovimento)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idMovimentoInverso)
REFERENCES transferencias(idMovimento)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE lancamentos (
idMovimento BIGINT UNSIGNED NOT NULL,
idPeriodo BIGINT UNSIGNED NOT NULL,
idSubcategoria BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(idMovimento),
FOREIGN KEY(idSubcategoria)
REFERENCES subcategorias(idSubcategoria)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idPeriodo)
REFERENCES periodos(idPeriodo)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(idMovimento)
REFERENCES movimentos(idMovimento)
ON DELETE CASCADE
ON UPDATE CASCADE
);
DELIMITER $$
CREATE TRIGGER atSaldoOnInsertMovimento
BEFORE INSERT ON movimentos
FOR EACH ROW
BEGIN
IF NEW.reducao THEN
UPDATE contas
SET saldoAtual = saldoAtual - NEW.valor
WHERE idConta = NEW.idConta;
ELSE
UPDATE contas
SET saldoAtual = saldoAtual + NEW.valor
WHERE idConta = NEW.idConta;
END IF;
END;$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER atSaldoOnDeleteMovimento
BEFORE DELETE ON movimentos
FOR EACH ROW
BEGIN
IF OLD.reducao THEN
UPDATE contas
SET saldoAtual = saldoAtual + OLD.valor
WHERE idConta = OLD.idConta;
ELSE
UPDATE contas
SET saldoAtual = saldoAtual - OLD.valor
WHERE idConta = OLD.idConta;
END IF;
END;$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER atSaldoOnUpdateMovimento
BEFORE UPDATE ON movimentos
FOR EACH ROW
BEGIN
IF OLD.reducao THEN
UPDATE contas
SET saldoAtual = saldoAtual + OLD.valor
WHERE idConta = OLD.idConta;
ELSE
UPDATE contas
SET saldoAtual = saldoAtual - OLD.valor
WHERE idConta = OLD.idConta;
END IF;
IF NEW.reducao THEN
UPDATE contas
SET saldoAtual = saldoAtual - NEW.valor
WHERE idConta = NEW.idConta;
ELSE
UPDATE contas
SET saldoAtual = saldoAtual + NEW.valor
WHERE idConta = NEW.idConta;
END IF;
END;$$
DELIMITER ;
DELIMITER $$
CREATE EVENT atTotalCobrancaDiariamente
ON SCHEDULE EVERY 1 DAY STARTS (CURRENT_DATE + INTERVAL 1 DAY + INTERVAL 1 MINUTE) DO
BEGIN
UPDATE cobrancas
SET
total = total - multa - jurosMora,
multa = valor * (percentualMulta/100),
jurosMora = valor * (percentualJurosMes/30/100) * DATEDIFF(CURRENT_DATE,dataVencimento),
total = total + multa + jurosMora
WHERE
dataRecebimento IS NULL
AND dataVencimento < CURRENT_DATE;
END;$$
DELIMITER ;
DELIMITER $$
CREATE EVENT delRememberMe
ON SCHEDULE EVERY 1 DAY STARTS (CURRENT_DATE + INTERVAL 1 DAY + INTERVAL 12 HOUR) DO
BEGIN
DELETE FROM persistent_logins
WHERE
(current_timestamp - last_used) > (120960 * 1000);
END;$$
DELIMITER ;
COMMIT;