-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.sql
3915 lines (3534 loc) · 149 KB
/
dump.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
--
-- Name: mio; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA mio;
ALTER SCHEMA mio OWNER TO postgres;
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET search_path = public, pg_catalog;
--
-- Name: depcred; Type: DOMAIN; Schema: public; Owner: postgres
--
CREATE DOMAIN depcred AS character varying
CONSTRAINT depcred_check CHECK (((VALUE)::text = ANY ((ARRAY['Deposito'::character varying, 'Credito'::character varying])::text[])));
ALTER DOMAIN public.depcred OWNER TO postgres;
--
-- Name: check_date_bilancio(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION check_date_bilancio() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
data_conto conto.data_creazione%TYPE;
data_bil bilancio.data_partenza%TYPE;
user_conto conto.userid%TYPE;
BEGIN
SELECT userid INTO user_conto FROM conto WHERE numero = NEW.numero_conto;
IF user_conto <> NEW.userid THEN
RAISE EXCEPTION 'CONTO NON APPARTENENTE ALLO STESSO UTENTE';
END IF;
SELECT data_creazione INTO data_conto FROM conto WHERE numero = NEW.numero_conto;
SELECT data_partenza INTO data_bil FROM bilancio WHERE userid = NEW.userid AND nome = NEW.nome_bil;
IF data_conto > data_bil THEN
RAISE EXCEPTION 'BILANCIO IN DATA PRECEDENTE ALLA CREAZIONE DEL CONTO';
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.check_date_bilancio() OWNER TO postgres;
--
-- Name: check_date_spesa_entrata(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION check_date_spesa_entrata() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
data_conto conto.data_creazione%TYPE;
user_conto conto.userid%TYPE;
BEGIN
SELECT userid INTO user_conto FROM conto WHERE numero = NEW.conto;
IF user_conto <> NEW.categoria_user THEN
RAISE EXCEPTION 'CATEGORIA DI UTENTE DIFFERENTE DAL CONTO';
END IF;
SELECT data_creazione INTO data_conto FROM conto WHERE numero = NEW.conto;
IF data_conto > NEW.data THEN
RAISE EXCEPTION 'SPESA/ENTRATA IN DATA PRECEDENTE ALLA CREAZIONE DEL CONTO';
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.check_date_spesa_entrata() OWNER TO postgres;
--
-- Name: check_oncredit_debt_exists(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION check_oncredit_debt_exists() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
a conto.tipo%TYPE;
uservar conto.userid%TYPE;
datavar conto.data_creazione%TYPE;
debtvar conto%ROWTYPE;
BEGIN
IF NEW.tipo = 'Credito' THEN
SELECT * INTO debtvar FROM conto WHERE numero = NEW.conto_di_rif;
IF debtvar.tipo = 'Credito' THEN
RAISE EXCEPTION 'REFERRAL ACCOUNT HAS TYPE Credito';
END IF;
IF debtvar.userid <> NEW.userid THEN
RAISE EXCEPTION 'REFERRAL ACCOUNT DOESNT BELONG TO SAME USER';
END IF;
IF debtvar.data_creazione > NEW.data_creazione THEN
RAISE EXCEPTION 'REFERRAL ACCOUNT HAS A NEWER DATE THEN CREDIT ACCOUNT VALUE %', NEW.tetto_max;
END IF;
NEW.amm_disp = NEW.tetto_max;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.check_oncredit_debt_exists() OWNER TO postgres;
--
-- Name: create_default_user(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION create_default_user() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
--profilo
INSERT INTO profilo (userid) VALUES (NEW.userid);
--categorie di spesa
INSERT INTO categoria_spesa(userid,nome) VALUES
(NEW.userid,'Casa'),
(NEW.userid,'Persona'),
(NEW.userid,'Trasporto'),
(NEW.userid,'Hobbies e Tempo Libero'),
(NEW.userid,'Tributi e Servizi vari');
--categorie di entrata
INSERT INTO categoria_entrata(userid,nome) VALUES
(NEW.userid,'Reddito'),
(NEW.userid,'Proventi Finanziari'),
(NEW.userid,'Proventi Immobiliari'),
(NEW.userid,'Alienazioni');
RETURN NEW;
END;
$$;
ALTER FUNCTION public.create_default_user() OWNER TO postgres;
--
-- Name: def_password(integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION def_password(integer, character varying) RETURNS void
LANGUAGE plpgsql
AS $_$
DECLARE
prof_var profilo%ROWTYPE;
BEGIN
FOR prof_var IN (SELECT * FROM profilo WHERE userid <= $1) LOOP
UPDATE profilo SET username = prof_var.userid, password_hashed = $2 WHERE userid = prof_var.userid;
END LOOP;
END;
$_$;
ALTER FUNCTION public.def_password(integer, character varying) OWNER TO postgres;
--
-- Name: fix_cron(date); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION fix_cron(date) RETURNS void
LANGUAGE plpgsql
AS $_$
DECLARE
conto_var conto%ROWTYPE;
a DECIMAL(19,4);
b DECIMAL(19,4);
spesa_var spesa%ROWTYPE;
entrata_var entrata%ROWTYPE;
bil_var bilancio%ROWTYPE;
cat_var categoria_spesa%ROWTYPE;
BEGIN
--check conti credito, crea relative spese/entrate e aggiorna amm_disp
FOR conto_var IN (SELECT * FROM conto WHERE data_creazione <= $1 AND tipo='Credito') LOOP
WHILE (conto_var.data_creazione + conto_var.scadenza_giorni < $1) LOOP
conto_var.data_creazione := conto_var.data_creazione + conto_var.scadenza_giorni;
END LOOP;
IF (conto_var.data_creazione + conto_var.scadenza_giorni = $1) THEN
SELECT SUM(valore) INTO a FROM spesa WHERE conto = conto_var.numero AND data >= conto_var.data_creazione AND data <= conto_var.data_creazione + conto_var.scadenza_giorni;
IF a IS NOT NULL THEN
EXECUTE 'INSERT INTO spesa(conto,data,descrizione,valore) VALUES ($1,$2,$3,$4)'
USING conto_var.conto_di_rif, conto_var.data_creazione + conto_var.scadenza_giorni,'Addebito da conto di credito n° ' || conto_var.numero, a;
END IF;
IF a IS NOT NULL THEN
EXECUTE 'INSERT INTO entrata(conto,data,descrizione,valore) VALUES ($1,$2,$3,$4)'
USING conto_var.numero, conto_var.data_creazione + conto_var.scadenza_giorni,'Rinnovo conto di Credito', a;
END IF;
conto_var.data_creazione := conto_var.data_creazione + conto_var.scadenza_giorni;
END IF;
END LOOP;
--calcola bilanci
FOR bil_var IN (SELECT * from bilancio) LOOP
WHILE (bil_var.data_partenza + bil_var.periodovalidita <= $1) LOOP
bil_var.data_partenza = bil_var.data_partenza + bil_var.periodovalidita;
END LOOP;
SELECT SUM(valore) INTO a FROM spesa WHERE conto IN (SELECT numero_conto FROM bilancio_conto WHERE userid = bil_var.userid AND nome_bil = bil_var.nome) AND categoria_nome IN (
WITH RECURSIVE rec_cat AS (
SELECT nome,userid,supercat_nome FROM categoria_spesa WHERE userid=1 AND nome IN (SELECT nome_cat FROM bilancio_categoria WHERE userid = bil_var.userid AND nome_bil = bil_var.nome)
UNION ALL
SELECT c.nome,c.userid,c.supercat_nome FROM categoria_spesa AS c JOIN rec_cat AS rc ON c.supercat_nome = rc.nome AND c.userid = rc.userid WHERE c.userid = bil_var.userid
)
SELECT nome FROM rec_cat
) AND data >= bil_var.data_partenza AND data <= $1;
--RAISE NOTICE 'Spese per bilancio % utente % = %', bil_var.nome, bil_var.userid, a;
--RAISE NOTICE 'bil_var.userid = %, bil_var.nome = %, bil_var.data_partenza = %, D1 = %', bil_var.userid , bil_var.nome , bil_var.data_partenza , $1;
IF a IS NULL THEN
UPDATE bilancio SET ammontarerestante = ammontareprevisto WHERE userid = bil_var.userid AND nome = bil_var.nome;
-- RAISE NOTICE 'Bilancio % utente % settato a %', bil_var.nome, bil_var.userid, 10000;
ELSE
UPDATE bilancio SET ammontarerestante = ammontareprevisto - a WHERE userid = bil_var.userid AND nome = bil_var.nome;
-- RAISE NOTICE 'Bilancio % utente % settato a %', bil_var.nome, bil_var.userid, 10000-a;
END IF;
END LOOP;
END;
$_$;
ALTER FUNCTION public.fix_cron(date) OWNER TO postgres;
--
-- Name: fixall_til(date); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION fixall_til(date) RETURNS void
LANGUAGE plpgsql
AS $_$
DECLARE
conto_var conto%ROWTYPE;
a DECIMAL(19,4);
b DECIMAL(19,4);
spesa_var spesa%ROWTYPE;
entrata_var entrata%ROWTYPE;
bil_var bilancio%ROWTYPE;
cat_var categoria_spesa%ROWTYPE;
BEGIN
--check conti credito, crea relative spese/entrate e aggiorna amm_disp
FOR conto_var IN (SELECT * FROM conto WHERE data_creazione <= $1 AND tipo='Credito') LOOP
WHILE (conto_var.data_creazione + conto_var.scadenza_giorni <= $1) LOOP
SELECT SUM(valore) INTO a FROM spesa WHERE conto = conto_var.numero AND data >= conto_var.data_creazione AND data < conto_var.data_creazione + conto_var.scadenza_giorni;
SELECT * INTO spesa_var FROM spesa WHERE conto = conto_var.conto_di_rif AND data = conto_var.data_creazione + conto_var.scadenza_giorni AND descrizione LIKE '%Addebito da conto di credito n°%';
SELECT * INTO entrata_var FROM entrata WHERE conto = conto_var.numero AND data = conto_var.data_creazione + conto_var.scadenza_giorni AND descrizione LIKE '%Rinnovo conto di Credito%';
IF spesa_var IS NULL THEN
IF a IS NOT NULL THEN
EXECUTE 'INSERT INTO spesa(conto,data,descrizione,valore) VALUES ($1,$2,$3,$4)'
USING conto_var.conto_di_rif, conto_var.data_creazione + conto_var.scadenza_giorni,'Addebito da conto di credito n° ' || conto_var.numero, a;
END IF;
ELSE
IF a IS NOT NULL THEN
UPDATE spesa SET valore = a WHERE conto = spesa_var.conto AND id_op = spesa_var.id_op;
ELSE
DELETE FROM spesa WHERE conto = spesa_var.conto AND id_op = spesa_var.id_op;
END IF;
END IF;
IF entrata_var IS NULL THEN
IF a IS NOT NULL THEN
--RAISE NOTICE 'INSERT INTO entrata(conto,data,descrizione,valore) VALUES % % % %', conto_var.numero, conto_var.data_creazione + conto_var.scadenza_giorni,'Rinnovo conto di Credito', a;
EXECUTE 'INSERT INTO entrata(conto,data,descrizione,valore) VALUES ($1,$2,$3,$4)'
USING conto_var.numero, conto_var.data_creazione + conto_var.scadenza_giorni,'Rinnovo conto di Credito', a;
END IF;
ELSE
IF a IS NOT NULL THEN
UPDATE entrata SET valore = a WHERE conto = entrata_var.conto AND id_op = entrata_var.id_op;
ELSE
DELETE FROM entrata WHERE conto = entrata_var.conto AND id_op = entrata_var.id_op;
END IF;
END IF;
conto_var.data_creazione := conto_var.data_creazione + conto_var.scadenza_giorni;
END LOOP;
--RAISE NOTICE 'Conto: % data_Creaz: %', conto_var.numero, conto_var.data_creazione;
SELECT SUM(valore) INTO a FROM spesa WHERE conto = conto_var.numero AND data >= conto_var.data_creazione AND data <= $1;
IF (a IS NULL) THEN
UPDATE conto SET amm_disp = conto_var.tetto_max WHERE numero = conto_var.numero;
ELSE
UPDATE conto SET amm_disp = conto_var.tetto_max-a WHERE numero = conto_var.numero;
END IF;
END LOOP;
--calcola disp conti di deposito
FOR conto_var IN (SELECT * from conto WHERE tipo='Deposito' AND data_creazione <= $1) LOOP
SELECT SUM(valore) INTO a FROM spesa WHERE conto = conto_var.numero AND data <= $1;
SELECT SUM(valore) INTO b FROM entrata WHERE conto = conto_var.numero AND data <= $1;
IF (a IS NOT NULL AND b IS NOT NULL) THEN
UPDATE conto SET amm_disp = b-a WHERE numero = conto_var.numero;
END IF;
IF (a IS NULL AND b IS NOT NULL) THEN
UPDATE conto SET amm_disp = b WHERE numero = conto_var.numero;
END IF;
IF (a IS NOT NULL AND b IS NULL) THEN --non si verificherà mai (trigger dep iniziale e controllo disp)
UPDATE conto SET amm_disp = a WHERE numero = conto_var.numero;
END IF;
END LOOP;
--calcola bilanci
FOR bil_var IN (SELECT * from bilancio) LOOP
WHILE (bil_var.data_partenza + bil_var.periodovalidita <= $1) LOOP
bil_var.data_partenza = bil_var.data_partenza + bil_var.periodovalidita;
END LOOP;
SELECT SUM(valore) INTO a FROM spesa WHERE conto IN (SELECT numero_conto FROM bilancio_conto WHERE userid = bil_var.userid AND nome_bil = bil_var.nome) AND categoria_nome IN (
WITH RECURSIVE rec_cat AS (
SELECT nome,userid,supercat_nome FROM categoria_spesa WHERE userid=1 AND nome IN (SELECT nome_cat FROM bilancio_categoria WHERE userid = bil_var.userid AND nome_bil = bil_var.nome)
UNION ALL
SELECT c.nome,c.userid,c.supercat_nome FROM categoria_spesa AS c JOIN rec_cat AS rc ON c.supercat_nome = rc.nome AND c.userid = rc.userid WHERE c.userid = bil_var.userid
)
SELECT nome FROM rec_cat
) AND data >= bil_var.data_partenza AND data <= $1;
--RAISE NOTICE 'Spese per bilancio % utente % = %', bil_var.nome, bil_var.userid, a;
--RAISE NOTICE 'bil_var.userid = %, bil_var.nome = %, bil_var.data_partenza = %, D1 = %', bil_var.userid , bil_var.nome , bil_var.data_partenza , $1;
IF a IS NULL THEN
UPDATE bilancio SET ammontarerestante = ammontareprevisto WHERE userid = bil_var.userid AND nome = bil_var.nome;
-- RAISE NOTICE 'Bilancio % utente % settato a %', bil_var.nome, bil_var.userid, 10000;
ELSE
UPDATE bilancio SET ammontarerestante = ammontareprevisto - a WHERE userid = bil_var.userid AND nome = bil_var.nome;
-- RAISE NOTICE 'Bilancio % utente % settato a %', bil_var.nome, bil_var.userid, 10000-a;
END IF;
END LOOP;
END;
$_$;
ALTER FUNCTION public.fixall_til(date) OWNER TO postgres;
--
-- Name: get_first_free_conto(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION get_first_free_conto() RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
a INTEGER;
BEGIN
SELECT MAX(numero) INTO a FROM conto;
IF a IS NULL THEN
a:=0;
END IF;
RETURN a+1;
END;
$$;
ALTER FUNCTION public.get_first_free_conto() OWNER TO postgres;
--
-- Name: get_first_free_spentr(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION get_first_free_spentr(integer) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
a INTEGER;
b INTEGER;
BEGIN
SELECT MAX(id_op) INTO a FROM spesa WHERE conto = $1;
SELECT MAX(id_op) INTO b FROM entrata WHERE conto = $1;
IF a IS NULL THEN
a:=0;
END IF;
IF b IS NULL THEN
b:=0;
END IF;
IF a>b THEN
RETURN a+1;
ELSE RETURN b+1;
END IF;
END;
$_$;
ALTER FUNCTION public.get_first_free_spentr(integer) OWNER TO postgres;
--
-- Name: get_first_free_utente(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION get_first_free_utente() RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
a INTEGER;
BEGIN
SELECT MAX(userid) INTO a FROM utente;
IF a IS NULL THEN
a:=0;
END IF;
RETURN a+1;
END;
$$;
ALTER FUNCTION public.get_first_free_utente() OWNER TO postgres;
--
-- Name: get_last_period_start_bil(integer, character varying, date); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION get_last_period_start_bil(integer, character varying, date) RETURNS date
LANGUAGE plpgsql
AS $_$
DECLARE
data DATE;
datab DATE;
periodo INTERVAL;
BEGIN
SELECT data_partenza INTO data FROM bilancio WHERE userid=$1 AND nome = $2;
SELECT periodovalidita INTO periodo FROM bilancio WHERE userid=$1 AND nome = $2;
WHILE (data + periodo <= $3) LOOP
data=data+periodo;
END LOOP;
IF data > $3 THEN
RETURN $3;
ELSE
RETURN data;
END IF;
END;
$_$;
ALTER FUNCTION public.get_last_period_start_bil(integer, character varying, date) OWNER TO postgres;
--
-- Name: get_last_period_start_cred(integer, date); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION get_last_period_start_cred(integer, date) RETURNS date
LANGUAGE plpgsql
AS $_$
DECLARE
data DATE;
datab DATE;
periodo INTERVAL;
BEGIN
SELECT data_creazione INTO data FROM conto WHERE numero = $1;
SELECT scadenza_giorni INTO periodo FROM conto WHERE numero = $1;
WHILE (data + periodo <= $2) LOOP
data=data+periodo;
END LOOP;
IF data > $2 THEN
RETURN $2;
ELSE
RETURN data;
END IF;
END;
$_$;
ALTER FUNCTION public.get_last_period_start_cred(integer, date) OWNER TO postgres;
--
-- Name: initial_deposit(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION initial_deposit() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NEW.tipo = 'Deposito' THEN
IF NEW.amm_disp > 0 THEN
insert into entrata(conto,data,descrizione,valore) VALUES (NEW.numero,NEW.data_creazione,'Deposito Iniziale',NEW.amm_disp);
END IF;
END IF;
IF NEW.tipo = 'Credito' THEN
IF NEW.amm_disp > 0 THEN
insert into entrata(conto,data,descrizione,valore) VALUES (NEW.numero,NEW.data_creazione,'Rinnovo conto di Credito',NEW.amm_disp);
END IF;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.initial_deposit() OWNER TO postgres;
--
-- Name: set_default_amount_bilancio(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION set_default_amount_bilancio() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.ammontarerestante=NEW.ammontareprevisto;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.set_default_amount_bilancio() OWNER TO postgres;
--
-- Name: upd_fixall(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION upd_fixall() RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM fixall_til(current_date);
END;
$$;
ALTER FUNCTION public.upd_fixall() OWNER TO postgres;
--
-- Name: update_account_on_entrata(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION update_account_on_entrata() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
--decommentare
tipo_conto conto.tipo%TYPE;
--
BEGIN
--decommentare per non permettere entrate nei conti di credito
SELECT tipo INTO tipo_conto from conto WHERE numero = NEW.conto;
-- RAISE NOTICE 'conto: % descr: %', New.conto, NEW.descrizione;
IF tipo_conto = 'Credito' AND (NEW.descrizione NOT LIKE 'Rinnovo conto di Credito' OR NEW.descrizione IS NULL) THEN
RAISE EXCEPTION 'NON E POSSIBILE INSERIRE ENTRATE PER I CONTI DI CREDITO';
END IF;
--finedecommentare
--RAISE NOTICE 'operazione: % conto %, descr %, valore %, data %', NEW.id_op, NEW.conto,NEW.descrizione,NEW.valore, NEW.data;
IF NEW.descrizione <> 'Deposito Iniziale' THEN
UPDATE conto SET amm_disp = amm_disp + NEW.valore WHERE numero = NEW.conto;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.update_account_on_entrata() OWNER TO postgres;
--
-- Name: update_account_on_spesa(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION update_account_on_spesa() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
ammontare_disp spesa.valore%TYPE;
BEGIN
IF NEW.valore IS NOT NULL THEN
SELECT amm_disp INTO ammontare_disp FROM conto WHERE numero = NEW.conto;
IF ammontare_disp < NEW.valore AND (NEW.descrizione NOT LIKE '%Addebito da conto di credito n%' OR NEW.descrizione IS NULL) THEN
RAISE EXCEPTION 'DISPONIBILITA SUL CONTO % NON SUFFICIENTE', NEW.conto;
ELSE
UPDATE conto SET amm_disp = amm_disp - NEW.valore WHERE numero = NEW.conto;
/*RAISE NOTICE 'Aggiornamente conto % amm_dispnuovo: % valore op: %', NEW.conto,ammontare_disp,NEW.valore;*/
END IF;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.update_account_on_spesa() OWNER TO postgres;
--
-- Name: update_bilancio_on_spesa(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION update_bilancio_on_spesa() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
bil_var bilancio%ROWTYPE;
user_var utente.userid%TYPE;
BEGIN
SELECT userid INTO user_var FROM conto WHERE numero = NEW.conto;
FOR bil_var IN SELECT * from BILANCIO WHERE nome IN (
SELECT nome_bil FROM bilancio_conto WHERE numero_conto = NEW.conto
)
AND nome IN (
SELECT nome_bil FROM bilancio_categoria WHERE nome_cat IN (
WITH RECURSIVE rec_cat AS (
SELECT nome,userid,supercat_nome FROM categoria_spesa WHERE userid=user_var AND nome=NEW.categoria_nome
UNION ALL
SELECT c.nome,c.userid,c.supercat_nome FROM categoria_spesa AS c JOIN rec_cat AS rc ON c.nome = rc.supercat_nome AND c.userid = rc.userid WHERE c.userid = user_var
)
SELECT nome FROM rec_cat
)
) AND userid = user_var LOOP
IF bil_var.ammontarerestante < NEW.valore THEN
RAISE NOTICE 'ECCEDUTO BILANCIO % DI %', bil_var.nome, NEW.valore - bil_var.ammontarerestante;
END IF;
UPDATE bilancio SET ammontarerestante = ammontarerestante - NEW.valore WHERE nome = bil_var.nome AND userid = bil_var.userid;
RAISE NOTICE 'Bilancio % settato a %', bil_var.nome, bil_var.ammontarerestante - NEW.valore;
END LOOP;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.update_bilancio_on_spesa() OWNER TO postgres;
--
-- Name: update_entrata_id(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION update_entrata_id() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
a INTEGER;
BEGIN
SELECT get_first_free_spentr(NEW.conto) INTO a;
UPDATE entrata SET id_op = a WHERE id_op = 0 AND conto = NEW.conto;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.update_entrata_id() OWNER TO postgres;
--
-- Name: update_spesa_id(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION update_spesa_id() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
a INTEGER;
BEGIN
SELECT get_first_free_spentr(NEW.conto) INTO a;
UPDATE spesa SET id_op = a WHERE id_op = 0 AND conto = NEW.conto;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.update_spesa_id() OWNER TO postgres;
--
-- Name: verifica_appartenenza(integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION verifica_appartenenza(integer, integer) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
a INTEGER;
BEGIN
SELECT COUNT(*) INTO a FROM conto WHERE userid=$1 AND numero=$2;
IF a = 1 THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
$_$;
ALTER FUNCTION public.verifica_appartenenza(integer, integer) OWNER TO postgres;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: bilancio; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE bilancio (
userid integer NOT NULL,
nome character varying(40) NOT NULL,
ammontareprevisto numeric(19,4) NOT NULL,
ammontarerestante numeric(19,4) NOT NULL,
periodovalidita interval NOT NULL,
data_partenza date NOT NULL,
CONSTRAINT bilancio_ammontareprevisto_check CHECK ((ammontareprevisto >= (0)::numeric)),
CONSTRAINT bilancio_periodovalidita_check CHECK ((periodovalidita >= '1 day'::interval))
);
ALTER TABLE public.bilancio OWNER TO postgres;
--
-- Name: bilancio_categoria; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE bilancio_categoria (
userid integer NOT NULL,
nome_bil character varying(40) NOT NULL,
nome_cat character varying(40) NOT NULL
);
ALTER TABLE public.bilancio_categoria OWNER TO postgres;
--
-- Name: bilancio_conto; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE bilancio_conto (
userid integer NOT NULL,
nome_bil character varying(40) NOT NULL,
numero_conto integer NOT NULL
);
ALTER TABLE public.bilancio_conto OWNER TO postgres;
--
-- Name: categoria_entrata; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE categoria_entrata (
userid integer NOT NULL,
nome character varying(40) NOT NULL,
supercat_nome character varying(40)
);
ALTER TABLE public.categoria_entrata OWNER TO postgres;
--
-- Name: categoria_spesa; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE categoria_spesa (
userid integer NOT NULL,
nome character varying(40) NOT NULL,
supercat_nome character varying(40)
);
ALTER TABLE public.categoria_spesa OWNER TO postgres;
--
-- Name: conto; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE conto (
numero integer DEFAULT get_first_free_conto() NOT NULL,
amm_disp numeric(19,4) NOT NULL,
tipo depcred NOT NULL,
tetto_max numeric(19,4),
scadenza_giorni interval,
userid integer NOT NULL,
data_creazione date NOT NULL,
conto_di_rif integer,
CONSTRAINT conto_check CHECK (((tetto_max >= (0)::numeric) AND ((((tipo)::text = 'Credito'::text) AND (tetto_max IS NOT NULL)) OR (((tipo)::text = 'Deposito'::text) AND (tetto_max IS NULL))))),
CONSTRAINT conto_check1 CHECK (((scadenza_giorni >= '1 day'::interval) AND ((((tipo)::text = 'Credito'::text) AND (scadenza_giorni IS NOT NULL)) OR (((tipo)::text = 'Deposito'::text) AND (scadenza_giorni IS NULL))))),
CONSTRAINT conto_check2 CHECK (((((tipo)::text = 'Credito'::text) AND (conto_di_rif IS NOT NULL)) OR (((tipo)::text = 'Deposito'::text) AND (conto_di_rif IS NULL))))
);
ALTER TABLE public.conto OWNER TO postgres;
--
-- Name: entrata; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE entrata (
conto integer NOT NULL,
id_op integer DEFAULT 0 NOT NULL,
data date NOT NULL,
categoria_user integer,
categoria_nome character varying(40),
descrizione character varying(100),
valore numeric(19,4) NOT NULL,
CONSTRAINT entrata_valore_check CHECK ((valore > (0)::numeric))
);
ALTER TABLE public.entrata OWNER TO postgres;
--
-- Name: nazione; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE nazione (
name character varying(80) NOT NULL
);
ALTER TABLE public.nazione OWNER TO postgres;
--
-- Name: profilo; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE profilo (
userid integer NOT NULL,
valuta character varying(3) DEFAULT '€'::character varying NOT NULL,
username character varying(60),
password_hashed character varying(64)
);
ALTER TABLE public.profilo OWNER TO postgres;
--
-- Name: spesa; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE spesa (
conto integer NOT NULL,
id_op integer DEFAULT 0 NOT NULL,
data date NOT NULL,
categoria_user integer,
categoria_nome character varying(40),
descrizione character varying(100),
valore numeric(19,4) NOT NULL,
CONSTRAINT spesa_valore_check CHECK ((valore > (0)::numeric))
);
ALTER TABLE public.spesa OWNER TO postgres;
--
-- Name: rapp_bilancio; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW rapp_bilancio AS
SELECT DISTINCT bl.userid, bl.nome_bil, s.categoria_nome, s.conto, s.id_op, s.data, s.valore, s.descrizione FROM (spesa s JOIN (SELECT blca.userid, blca.nome_bil, blca.nome, blco.numero_conto FROM ((SELECT blc.userid, blc.nome_bil, categoria_spesa.nome FROM bilancio_categoria blc, categoria_spesa WHERE (((categoria_spesa.nome)::text IN (WITH RECURSIVE rec_cat AS (SELECT categoria_spesa.nome, categoria_spesa.userid, categoria_spesa.supercat_nome FROM categoria_spesa WHERE ((categoria_spesa.userid = blc.userid) AND ((categoria_spesa.nome)::text = (blc.nome_cat)::text)) UNION ALL SELECT c.nome, c.userid, c.supercat_nome FROM (categoria_spesa c JOIN rec_cat rc ON ((((c.supercat_nome)::text = (rc.nome)::text) AND (c.userid = rc.userid)))) WHERE (c.userid = blc.userid)) SELECT rec_cat.nome FROM rec_cat)) AND (categoria_spesa.userid = blc.userid)) ORDER BY blc.userid, blc.nome_bil) blca JOIN bilancio_conto blco ON (((blca.userid = blco.userid) AND ((blca.nome_bil)::text = (blco.nome_bil)::text))))) bl ON (((s.conto = bl.numero_conto) AND ((s.categoria_nome)::text = (bl.nome)::text)))) ORDER BY bl.userid, bl.nome_bil, s.data, s.id_op;
ALTER TABLE public.rapp_bilancio OWNER TO postgres;
--
-- Name: rapp_conto; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW rapp_conto AS
SELECT a.data, a.cr, a.de, a.descrizione, a.categoria_nome, a.conto FROM (SELECT entrata.id_op, entrata.data, entrata.valore AS cr, NULL::numeric AS de, entrata.descrizione, entrata.categoria_nome, entrata.conto FROM entrata UNION SELECT spesa.id_op, spesa.data, NULL::numeric AS cr, spesa.valore AS de, spesa.descrizione, spesa.categoria_nome, spesa.conto FROM spesa ORDER BY 2, 1, 3) a;
ALTER TABLE public.rapp_conto OWNER TO postgres;
--
-- Name: rapp_quantitamediaspesa; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW rapp_quantitamediaspesa AS
(SELECT c.userid, (sum(s.valore) / (count(*))::numeric) AS quantitamedia, s.categoria_nome FROM (spesa s JOIN conto c ON ((s.conto = c.numero))) WHERE (((s.descrizione)::text !~~ 'Addebito da conto di credito%'::text) OR (s.descrizione IS NULL)) GROUP BY c.userid, s.categoria_nome ORDER BY c.userid, (sum(s.valore) / (count(*))::numeric) DESC, s.categoria_nome) UNION (SELECT c.userid, (sum(s.valore) / (count(*))::numeric) AS quantitamedia, 'ZTOTALISSIMO'::character varying AS categoria_nome FROM (spesa s JOIN conto c ON ((s.conto = c.numero))) WHERE (((s.descrizione)::text !~~ 'Addebito da conto di credito%'::text) OR (s.descrizione IS NULL)) GROUP BY c.userid ORDER BY c.userid) ORDER BY 1, 2 DESC, 3;
ALTER TABLE public.rapp_quantitamediaspesa OWNER TO postgres;
--
-- Name: rapp_sumcatenperutente; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW rapp_sumcatenperutente AS
SELECT c.userid, sum(e.valore) AS totale, e.categoria_nome FROM (entrata e JOIN conto c ON ((e.conto = c.numero))) WHERE ((((e.descrizione)::text !~~ 'Deposito Iniziale'::text) AND ((e.descrizione)::text !~~ 'Rinnovo conto di Credito'::text)) OR (e.descrizione IS NULL)) GROUP BY c.userid, e.categoria_nome ORDER BY c.userid, sum(e.valore) DESC, e.categoria_nome;
ALTER TABLE public.rapp_sumcatenperutente OWNER TO postgres;
--
-- Name: rapp_sumcatspperconto; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW rapp_sumcatspperconto AS
SELECT s.data, s.conto, sum(s.valore) AS sum_spesa, s.categoria_nome FROM spesa s WHERE (((s.descrizione)::text !~~ 'Addebito da conto di credito%'::text) OR (s.descrizione IS NULL)) GROUP BY s.conto, s.categoria_nome, s.data ORDER BY s.conto, sum(s.valore) DESC, s.categoria_nome;
ALTER TABLE public.rapp_sumcatspperconto OWNER TO postgres;
--
-- Name: rapp_sumcatspperutente; Type: VIEW; Schema: public; Owner: postgres
--
CREATE VIEW rapp_sumcatspperutente AS
SELECT c.userid, sum(s.valore) AS totale, s.categoria_nome FROM (spesa s JOIN conto c ON ((s.conto = c.numero))) WHERE (((s.descrizione)::text !~~ 'Addebito da conto di credito%'::text) OR (s.descrizione IS NULL)) GROUP BY c.userid, s.categoria_nome ORDER BY c.userid, sum(s.valore) DESC, s.categoria_nome;
ALTER TABLE public.rapp_sumcatspperutente OWNER TO postgres;
--
-- Name: utente; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE utente (
userid integer DEFAULT get_first_free_utente() NOT NULL,
nome character varying(40),
cognome character varying(40),
cfiscale character(16) NOT NULL,
indirizzo character varying(70),
citta character varying(70),
nazione_res character varying(80),
email character varying(100),
telefono character varying(20),
CONSTRAINT utente_cfiscale_check CHECK ((cfiscale ~ '[A-Za-z0-9]{16}'::text)),
CONSTRAINT utente_check CHECK (((cognome IS NOT NULL) OR (nome IS NOT NULL))),
CONSTRAINT utente_email_check CHECK ((((email)::text ~ ''::text) OR ((email)::text ~ '^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'::text))),
CONSTRAINT utente_telefono_check CHECK (((telefono)::text ~ '[+]?[0-9]*[/-\\]?[0-9]*'::text))
);
ALTER TABLE public.utente OWNER TO postgres;
--
-- Name: valuta; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE valuta (
simbolo character varying(3) NOT NULL
);
ALTER TABLE public.valuta OWNER TO postgres;
--
-- Data for Name: bilancio; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY bilancio (userid, nome, ammontareprevisto, ammontarerestante, periodovalidita, data_partenza) FROM stdin;
1 Spese Trasporti 150.0000 150.0000 30 days 2013-05-31
1 Spese Tributi 550.0000 550.0000 30 days 2013-05-31
1 Spese Casa e Persona 600.0000 600.0000 15 days 2013-05-31
2 Spese Trasporti 250.0000 250.0000 30 days 2013-05-31
2 Spese Casa e Persona 900.0000 900.0000 15 days 2013-05-31
2 Spese Tributi 350.0000 350.0000 30 days 2013-05-31
\.
--
-- Data for Name: bilancio_categoria; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY bilancio_categoria (userid, nome_bil, nome_cat) FROM stdin;
1 Spese Casa e Persona Casa
1 Spese Casa e Persona Persona
1 Spese Tributi Tributi e Servizi vari
1 Spese Trasporti Trasporto
2 Spese Casa e Persona Casa
2 Spese Casa e Persona Persona
2 Spese Tributi Tributi e Servizi vari
2 Spese Trasporti Trasporto
\.