This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database-architecture.sql
1430 lines (1092 loc) · 46.1 KB
/
database-architecture.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
--
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- TOC entry 5 (class 2615 OID 16386)
-- Name: accounts; Type: SCHEMA; Schema: -; Owner: admin
--
CREATE SCHEMA accounts;
ALTER SCHEMA accounts OWNER TO admin;
--
-- TOC entry 6 (class 2615 OID 16394)
-- Name: deadlines; Type: SCHEMA; Schema: -; Owner: admin
--
CREATE SCHEMA deadlines;
ALTER SCHEMA deadlines OWNER TO admin;
--
-- TOC entry 9 (class 2615 OID 16422)
-- Name: invoices; Type: SCHEMA; Schema: -; Owner: admin
--
CREATE SCHEMA invoices;
ALTER SCHEMA invoices OWNER TO admin;
--
-- TOC entry 10 (class 2615 OID 16538)
-- Name: jobs; Type: SCHEMA; Schema: -; Owner: admin
--
CREATE SCHEMA jobs;
ALTER SCHEMA jobs OWNER TO admin;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- TOC entry 205 (class 1259 OID 16388)
-- Name: accounts; Type: TABLE; Schema: accounts; Owner: admin
--
CREATE TABLE accounts.accounts (
fiscalcode character varying NOT NULL,
name character varying NOT NULL,
numbervat character varying,
atecocode character varying,
legal_address character varying,
customer_category character varying NOT NULL,
descriptor character varying
);
ALTER TABLE accounts.accounts OWNER TO admin;
--
-- TOC entry 208 (class 1259 OID 16539)
-- Name: accounts_categories; Type: TABLE; Schema: accounts; Owner: admin
--
CREATE TABLE accounts.accounts_categories (
categories character varying NOT NULL,
extended character varying,
description character varying
);
ALTER TABLE accounts.accounts_categories OWNER TO admin;
--
-- TOC entry 209 (class 1259 OID 16552)
-- Name: accounts_contact; Type: TABLE; Schema: accounts; Owner: admin
--
CREATE TABLE accounts.accounts_contact (
fiscalcode character varying NOT NULL,
description character varying,
value character varying NOT NULL
);
ALTER TABLE accounts.accounts_contact OWNER TO admin;
--
-- TOC entry 210 (class 1259 OID 16558)
-- Name: accounts_contact_categories; Type: TABLE; Schema: accounts; Owner: admin
--
CREATE TABLE accounts.accounts_contact_categories (
categories character varying NOT NULL,
description character varying NOT NULL
);
ALTER TABLE accounts.accounts_contact_categories OWNER TO admin;
--
-- TOC entry 211 (class 1259 OID 16579)
-- Name: accounts_fiscaldata; Type: TABLE; Schema: accounts; Owner: admin
--
CREATE TABLE accounts.accounts_fiscaldata (
fiscalcode character varying NOT NULL
);
ALTER TABLE accounts.accounts_fiscaldata OWNER TO admin;
--
-- TOC entry 225 (class 1259 OID 16904)
-- Name: asbestos_advice; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.asbestos_advice (
jobid character varying NOT NULL,
jobstart date NOT NULL,
jobend date NOT NULL,
invoice_override boolean
);
ALTER TABLE deadlines.asbestos_advice OWNER TO admin;
--
-- TOC entry 223 (class 1259 OID 16865)
-- Name: asbestos_advice_account_details; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.asbestos_advice_account_details (
fiscalcode character varying NOT NULL,
notes character varying,
expired boolean DEFAULT false,
anticipated boolean DEFAULT false NOT NULL
);
ALTER TABLE deadlines.asbestos_advice_account_details OWNER TO admin;
--
-- TOC entry 226 (class 1259 OID 16933)
-- Name: asbestos_advice_invoices; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.asbestos_advice_invoices (
job_id character varying NOT NULL,
job_start date NOT NULL,
invoice_id character varying NOT NULL
);
ALTER TABLE deadlines.asbestos_advice_invoices OWNER TO admin;
--
-- TOC entry 224 (class 1259 OID 16875)
-- Name: asbestos_advice_invoices_months; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.asbestos_advice_invoices_months (
customer character varying NOT NULL,
months integer[]
);
ALTER TABLE deadlines.asbestos_advice_invoices_months OWNER TO admin;
--
-- TOC entry 206 (class 1259 OID 16395)
-- Name: rspp; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.rspp (
rspp_jobid character varying NOT NULL,
jobstart date NOT NULL,
jobend date NOT NULL,
invoice_override boolean
);
ALTER TABLE deadlines.rspp OWNER TO admin;
--
-- TOC entry 218 (class 1259 OID 16744)
-- Name: rspp_invoices; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.rspp_invoices (
rspp_id character varying NOT NULL,
rspp_start date NOT NULL,
invoice_id character varying NOT NULL
);
ALTER TABLE deadlines.rspp_invoices OWNER TO admin;
--
-- TOC entry 219 (class 1259 OID 16766)
-- Name: rspp_invoices_months; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.rspp_invoices_months (
customer character varying NOT NULL,
months integer[]
);
ALTER TABLE deadlines.rspp_invoices_months OWNER TO admin;
--
-- TOC entry 215 (class 1259 OID 16653)
-- Name: rssp_account_details; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.rssp_account_details (
fiscalcode character varying NOT NULL,
notes character varying,
expired boolean DEFAULT false,
anticipated boolean DEFAULT false NOT NULL
);
ALTER TABLE deadlines.rssp_account_details OWNER TO admin;
--
-- TOC entry 214 (class 1259 OID 16613)
-- Name: jobs; Type: TABLE; Schema: jobs; Owner: admin
--
CREATE TABLE jobs.jobs (
jobs_id character varying NOT NULL,
jobs_category character varying,
jobs_type character varying,
jobs_description character varying,
customer character varying
);
ALTER TABLE jobs.jobs OWNER TO admin;
--
-- TOC entry 216 (class 1259 OID 16706)
-- Name: jobs_pa; Type: TABLE; Schema: jobs; Owner: admin
--
CREATE TABLE jobs.jobs_pa (
jobs_id character varying NOT NULL,
cig character varying,
decree_number integer,
decree_date date
);
ALTER TABLE jobs.jobs_pa OWNER TO admin;
--
-- TOC entry 228 (class 1259 OID 16982)
-- Name: expired_without_invoice; Type: VIEW; Schema: deadlines; Owner: admin
--
CREATE VIEW deadlines.expired_without_invoice AS
SELECT main.jobid,
main.jobstart,
main.jobend,
main.customer,
main.months,
main.notes,
main.anticipated,
main.expired,
main.invoice_override,
main.jobs_category,
main.jobs_type,
main.jobs_description,
main.fiscalcode,
main.name,
main.numbervat,
main.atecocode,
main.legal_address,
main.customer_category,
main.descriptor,
jp.cig,
jp.decree_number,
jp.decree_date
FROM (( SELECT db_main.jobid,
db_main.jobstart,
db_main.jobend,
j.customer,
db_details.months,
db_details.notes,
db_details.anticipated,
db_details.expired,
db_details.invoice_override,
j.jobs_category,
j.jobs_type,
j.jobs_description,
a.fiscalcode,
a.name,
a.numbervat,
a.atecocode,
a.legal_address,
a.customer_category,
a.descriptor
FROM ( SELECT db11.jobid,
db11.jobstart,
db11.jobend
FROM (( SELECT r.rspp_jobid AS jobid,
r.jobstart,
r.jobend,
array_length(rim.months, 1) AS invoice_number
FROM deadlines.rspp r,
jobs.jobs j_1,
deadlines.rspp_invoices_months rim
WHERE ((r.invoice_override IS NOT TRUE) AND ((j_1.jobs_id)::text = (r.rspp_jobid)::text) AND ((j_1.customer)::text = (rim.customer)::text))
UNION
SELECT aa.jobid,
aa.jobstart,
aa.jobend,
array_length(aaim.months, 1) AS invoice_number
FROM deadlines.asbestos_advice aa,
jobs.jobs j_1,
deadlines.asbestos_advice_invoices_months aaim
WHERE ((aa.invoice_override IS NOT TRUE) AND ((j_1.jobs_id)::text = (aa.jobid)::text) AND ((j_1.customer)::text = (aaim.customer)::text))) db11
LEFT JOIN ( SELECT aai.job_id AS jobid,
aai.job_start AS jobstart,
count(aai.invoice_id) AS invoice_count
FROM deadlines.asbestos_advice_invoices aai
GROUP BY aai.job_id, aai.job_start
UNION
SELECT ri.rspp_id AS jobid,
ri.rspp_start AS jobstart,
count(ri.invoice_id) AS invoice_count
FROM deadlines.rspp_invoices ri
GROUP BY ri.rspp_id, ri.rspp_start) db12 ON ((((db11.jobid)::text = (db12.jobid)::text) AND (db11.jobstart = db12.jobstart))))
WHERE (((db11.jobend + '3 mons'::interval) < (date_trunc('MONTH'::text, now()))::date) AND ((db12.invoice_count IS NULL) OR (db12.invoice_count < db11.invoice_number)))) db_main,
( SELECT aa.jobid,
aa.jobstart,
aaim.months,
aaad.notes,
aaad.anticipated,
aaad.expired,
aa.invoice_override,
j_1.jobs_category,
j_1.jobs_type,
j_1.jobs_description
FROM deadlines.asbestos_advice aa,
deadlines.asbestos_advice_account_details aaad,
deadlines.asbestos_advice_invoices_months aaim,
jobs.jobs j_1
WHERE (((aa.jobid)::text = (j_1.jobs_id)::text) AND ((aaad.fiscalcode)::text = (j_1.customer)::text) AND ((aaim.customer)::text = (j_1.customer)::text))
UNION
SELECT r.rspp_jobid AS jobid,
r.jobstart,
rim.months,
rad.notes,
rad.anticipated,
rad.expired,
r.invoice_override,
j_1.jobs_category,
j_1.jobs_type,
j_1.jobs_description
FROM deadlines.rspp r,
deadlines.rssp_account_details rad,
deadlines.rspp_invoices_months rim,
jobs.jobs j_1
WHERE (((r.rspp_jobid)::text = (j_1.jobs_id)::text) AND ((rad.fiscalcode)::text = (j_1.customer)::text) AND ((rim.customer)::text = (j_1.customer)::text))) db_details,
jobs.jobs j,
accounts.accounts a
WHERE (((db_main.jobid)::text = (j.jobs_id)::text) AND ((a.fiscalcode)::text = (j.customer)::text) AND ((db_main.jobid)::text = (db_details.jobid)::text) AND (db_main.jobstart = db_details.jobstart))) main
LEFT JOIN jobs.jobs_pa jp ON (((main.jobid)::text = (jp.jobs_id)::text)));
ALTER TABLE deadlines.expired_without_invoice OWNER TO admin;
--
-- TOC entry 229 (class 1259 OID 16987)
-- Name: current_month_invoices; Type: VIEW; Schema: deadlines; Owner: admin
--
CREATE VIEW deadlines.current_month_invoices AS
SELECT main.jobid,
main.jobstart,
main.jobend,
main.customer,
main.months,
main.notes,
main.anticipated,
main.expired,
main.invoice_override,
main.jobs_category,
main.jobs_type,
main.jobs_description,
main.fiscalcode,
main.name,
main.numbervat,
main.atecocode,
main.legal_address,
main.customer_category,
main.descriptor,
jp.jobs_id,
jp.cig,
jp.decree_number,
jp.decree_date
FROM (( SELECT valid_invoices.jobid,
valid_invoices.jobstart,
valid_invoices.jobend,
valid_invoices.customer,
valid_invoices.months,
valid_invoices.notes,
valid_invoices.anticipated,
valid_invoices.expired,
valid_invoices.invoice_override,
valid_invoices.jobs_category,
valid_invoices.jobs_type,
valid_invoices.jobs_description,
valid_invoices.fiscalcode,
valid_invoices.name,
valid_invoices.numbervat,
valid_invoices.atecocode,
valid_invoices.legal_address,
valid_invoices.customer_category,
valid_invoices.descriptor
FROM ( SELECT dd.jobid,
dd.jobstart,
dd.jobend,
dd.customer,
dd.months,
dd.notes,
dd.anticipated,
dd.expired,
dd.invoice_override,
dd.jobs_category,
dd.jobs_type,
dd.jobs_description,
a.fiscalcode,
a.name,
a.numbervat,
a.atecocode,
a.legal_address,
a.customer_category,
a.descriptor
FROM ( SELECT j.jobs_id AS jobid,
r.jobstart,
r.jobend,
j.customer,
rim.months,
rad.notes,
rad.anticipated,
rad.expired,
r.invoice_override,
j.jobs_category,
j.jobs_type,
j.jobs_description
FROM deadlines.rspp r,
deadlines.rspp_invoices_months rim,
deadlines.rssp_account_details rad,
jobs.jobs j
WHERE (((r.rspp_jobid)::text = (j.jobs_id)::text) AND ((rim.customer)::text = (j.customer)::text) AND ((rad.fiscalcode)::text = (j.customer)::text))
UNION
SELECT aa.jobid,
aa.jobstart,
aa.jobend,
j.customer,
aaim.months,
aaad.notes,
aaad.anticipated,
aaad.expired,
aa.invoice_override,
j.jobs_category,
j.jobs_type,
j.jobs_description
FROM deadlines.asbestos_advice aa,
deadlines.asbestos_advice_invoices_months aaim,
deadlines.asbestos_advice_account_details aaad,
jobs.jobs j
WHERE (((aa.jobid)::text = (j.jobs_id)::text) AND ((aaim.customer)::text = (j.customer)::text) AND ((aaad.fiscalcode)::text = (j.customer)::text))) dd,
accounts.accounts a
WHERE (((a.fiscalcode)::text = (dd.customer)::text) AND (((dd.jobid)::text, dd.jobstart) IN ( SELECT db2.jobid,
db2.jobstart
FROM (( SELECT dat.jobid,
dat.jobstart,
count(dat.invoice_id) AS invoice_count
FROM ( SELECT ri.invoice_id,
ri.rspp_id AS jobid,
ri.rspp_start AS jobstart
FROM deadlines.rspp_invoices ri
UNION
SELECT aai.invoice_id,
aai.job_id AS jobid,
aai.job_start AS jobstart
FROM deadlines.asbestos_advice_invoices aai) dat
GROUP BY dat.jobid, dat.jobstart) db1
RIGHT JOIN ( SELECT aa.jobid,
aa.jobstart,
aa.jobend,
array_length(aaim.months, 1) AS invoice_number
FROM deadlines.asbestos_advice aa,
jobs.jobs j,
deadlines.asbestos_advice_invoices_months aaim
WHERE (((aa.jobid)::text = (j.jobs_id)::text) AND ((aaim.customer)::text = (j.customer)::text))
UNION
SELECT r.rspp_jobid AS jobid,
r.jobstart,
r.jobend,
array_length(rim.months, 1) AS invoice_number
FROM deadlines.rspp r,
jobs.jobs j,
deadlines.rspp_invoices_months rim
WHERE (((r.rspp_jobid)::text = (j.jobs_id)::text) AND ((rim.customer)::text = (j.customer)::text))) db2 ON ((((db1.jobid)::text = (db2.jobid)::text) AND (db2.jobstart = db2.jobstart))))
WHERE ((db1.invoice_count IS NULL) OR ((db1.invoice_count < db2.invoice_number) AND (NOT (((db2.jobid)::text, db2.jobstart) IN ( SELECT aa.jobid,
aa.jobstart
FROM deadlines.asbestos_advice aa
WHERE (aa.invoice_override IS TRUE)
UNION
SELECT r.rspp_jobid AS jobid,
r.jobstart
FROM deadlines.rspp r
WHERE (r.invoice_override IS TRUE)))))))))) valid_invoices
WHERE ((date_part('month'::text, (date_trunc('MONTH'::text, now()))::date) = ANY ((valid_invoices.months)::double precision[])) AND (((valid_invoices.anticipated IS TRUE) AND ((date_trunc('MONTH'::text, now()))::date >= (valid_invoices.jobstart + '5 mons'::interval))) OR ((valid_invoices.anticipated IS NOT TRUE) AND ((date_trunc('MONTH'::text, now()))::date <= (valid_invoices.jobstart + '5 mons'::interval)))) AND (NOT (((valid_invoices.jobid)::text, valid_invoices.jobstart) IN ( SELECT ewi.jobid,
ewi.jobstart
FROM deadlines.expired_without_invoice ewi))))) main
LEFT JOIN jobs.jobs_pa jp ON (((main.jobid)::text = (jp.jobs_id)::text)));
ALTER TABLE deadlines.current_month_invoices OWNER TO admin;
--
-- TOC entry 222 (class 1259 OID 16808)
-- Name: expiring_next_60_days; Type: VIEW; Schema: deadlines; Owner: admin
--
CREATE VIEW deadlines.expiring_next_60_days AS
SELECT main.name,
main.job_end,
rad.notes
FROM (( SELECT a.name,
a.fiscalcode,
max(r.jobend) AS job_end
FROM accounts.accounts a,
jobs.jobs j,
deadlines.rspp r
WHERE (((j.customer)::text = (a.fiscalcode)::text) AND ((j.jobs_id)::text = (r.rspp_jobid)::text) AND (r.jobend < (now() + '60 days'::interval day)) AND (NOT ((a.name)::text IN ( SELECT a_1.name
FROM accounts.accounts a_1,
jobs.jobs j_1,
deadlines.rspp r_1
WHERE ((r_1.jobend > (now() + '60 days'::interval day)) AND ((j_1.jobs_id)::text = (r_1.rspp_jobid)::text) AND ((a_1.fiscalcode)::text = (j_1.customer)::text))))) AND (NOT ((a.fiscalcode)::text IN ( SELECT rn.fiscalcode
FROM deadlines.rssp_account_details rn
WHERE (rn.expired IS TRUE)))))
GROUP BY a.name, a.fiscalcode
ORDER BY a.name) main
LEFT JOIN deadlines.rssp_account_details rad ON (((rad.fiscalcode)::text = (main.fiscalcode)::text)));
ALTER TABLE deadlines.expiring_next_60_days OWNER TO admin;
--
-- TOC entry 221 (class 1259 OID 16799)
-- Name: missing_invoice_month_customer; Type: VIEW; Schema: deadlines; Owner: admin
--
CREATE VIEW deadlines.missing_invoice_month_customer AS
SELECT DISTINCT a.name AS "Clienti con mese mancante"
FROM accounts.accounts a,
jobs.jobs j,
deadlines.rspp r
WHERE ((NOT ((a.fiscalcode)::text IN ( SELECT rim.customer AS fiscalcode
FROM deadlines.rspp_invoices_months rim
WHERE (array_length(rim.months, 1) > 0)))) AND ((a.fiscalcode)::text = (j.customer)::text) AND ((r.rspp_jobid)::text = (j.jobs_id)::text))
ORDER BY a.name;
ALTER TABLE deadlines.missing_invoice_month_customer OWNER TO admin;
--
-- TOC entry 227 (class 1259 OID 16951)
-- Name: noise_vib_eval; Type: TABLE; Schema: deadlines; Owner: admin
--
CREATE TABLE deadlines.noise_vib_eval (
customer character varying NOT NULL,
job character varying NOT NULL,
noise_eval boolean NOT NULL,
vib_eval boolean NOT NULL,
date date NOT NULL
);
ALTER TABLE deadlines.noise_vib_eval OWNER TO admin;
--
-- TOC entry 230 (class 1259 OID 16992)
-- Name: pa_report; Type: VIEW; Schema: deadlines; Owner: admin
--
CREATE VIEW deadlines.pa_report AS
SELECT
NULL::character varying AS "Ragione Sociale",
NULL::date AS "Scadenza",
NULL::boolean AS "Contratto Terminato";
ALTER TABLE deadlines.pa_report OWNER TO admin;
--
-- TOC entry 220 (class 1259 OID 16794)
-- Name: rspp_not_updated; Type: VIEW; Schema: deadlines; Owner: admin
--
CREATE VIEW deadlines.rspp_not_updated AS
SELECT a.name AS "Ragione Sociale",
max(r.jobend) AS "Scaduto il"
FROM accounts.accounts a,
jobs.jobs j,
deadlines.rspp r
WHERE (((j.customer)::text = (a.fiscalcode)::text) AND ((j.jobs_id)::text = (r.rspp_jobid)::text) AND (r.jobend < now()) AND (NOT ((a.name)::text IN ( SELECT a_1.name
FROM accounts.accounts a_1,
jobs.jobs j_1,
deadlines.rspp r_1
WHERE ((r_1.jobend > now()) AND ((j_1.jobs_id)::text = (r_1.rspp_jobid)::text) AND ((a_1.fiscalcode)::text = (j_1.customer)::text))))) AND (NOT ((a.fiscalcode)::text IN ( SELECT rn.fiscalcode
FROM deadlines.rssp_account_details rn
WHERE (rn.expired IS TRUE)))))
GROUP BY a.name
ORDER BY a.name;
ALTER TABLE deadlines.rspp_not_updated OWNER TO admin;
--
-- TOC entry 3144 (class 0 OID 0)
-- Dependencies: 220
-- Name: VIEW rspp_not_updated; Type: COMMENT; Schema: deadlines; Owner: admin
--
COMMENT ON VIEW deadlines.rspp_not_updated IS 'Mostra gli incarichi RSPP scaduti che non sono stati rinnovati (e non sono stati segnalati come scaduti)';
--
-- TOC entry 207 (class 1259 OID 16532)
-- Name: invoices; Type: TABLE; Schema: invoices; Owner: admin
--
CREATE TABLE invoices.invoices (
invoiceid character varying NOT NULL,
number integer,
emission date,
type character varying,
payed boolean NOT NULL,
description character varying,
taxable real
);
ALTER TABLE invoices.invoices OWNER TO admin;
--
-- TOC entry 217 (class 1259 OID 16719)
-- Name: PA datas; Type: VIEW; Schema: jobs; Owner: admin
--
CREATE VIEW jobs."PA datas" AS
SELECT jp.jobs_id AS job_id,
a.name,
jp.cig,
jp.decree_number,
jp.decree_date
FROM accounts.accounts a,
jobs.jobs_pa jp,
jobs.jobs j
WHERE (((a.fiscalcode)::text = (j.customer)::text) AND ((j.jobs_id)::text = (jp.jobs_id)::text));
ALTER TABLE jobs."PA datas" OWNER TO admin;
--
-- TOC entry 213 (class 1259 OID 16600)
-- Name: job_types; Type: TABLE; Schema: jobs; Owner: admin
--
CREATE TABLE jobs.job_types (
category character varying NOT NULL,
types character varying NOT NULL
);
ALTER TABLE jobs.job_types OWNER TO admin;
--
-- TOC entry 212 (class 1259 OID 16592)
-- Name: jobs_categories; Type: TABLE; Schema: jobs; Owner: admin
--
CREATE TABLE jobs.jobs_categories (
categories character varying NOT NULL
);
ALTER TABLE jobs.jobs_categories OWNER TO admin;
--
-- TOC entry 2929 (class 2606 OID 16546)
-- Name: accounts_categories accounts_categories_pk; Type: CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts_categories
ADD CONSTRAINT accounts_categories_pk PRIMARY KEY (categories);
--
-- TOC entry 2933 (class 2606 OID 16565)
-- Name: accounts_contact_categories accounts_contact_categories_pk; Type: CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts_contact_categories
ADD CONSTRAINT accounts_contact_categories_pk PRIMARY KEY (categories);
--
-- TOC entry 2931 (class 2606 OID 16572)
-- Name: accounts_contact accounts_contact_pk; Type: CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts_contact
ADD CONSTRAINT accounts_contact_pk PRIMARY KEY (fiscalcode, value);
--
-- TOC entry 2935 (class 2606 OID 16586)
-- Name: accounts_fiscaldata accounts_fiscaldata_pk; Type: CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts_fiscaldata
ADD CONSTRAINT accounts_fiscaldata_pk PRIMARY KEY (fiscalcode);
--
-- TOC entry 2921 (class 2606 OID 16434)
-- Name: accounts accounts_pk; Type: CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts
ADD CONSTRAINT accounts_pk PRIMARY KEY (fiscalcode);
--
-- TOC entry 2923 (class 2606 OID 16784)
-- Name: accounts accounts_un; Type: CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts
ADD CONSTRAINT accounts_un UNIQUE (numbervat);
--
-- TOC entry 2959 (class 2606 OID 16940)
-- Name: asbestos_advice_invoices asbestos_advice_invoices_pk; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.asbestos_advice_invoices
ADD CONSTRAINT asbestos_advice_invoices_pk PRIMARY KEY (job_id, job_start, invoice_id);
--
-- TOC entry 2957 (class 2606 OID 16911)
-- Name: asbestos_advice asbestos_advice_pk; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.asbestos_advice
ADD CONSTRAINT asbestos_advice_pk PRIMARY KEY (jobid, jobstart);
--
-- TOC entry 2961 (class 2606 OID 16968)
-- Name: noise_vib_eval noise_vib_eval_pk; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.noise_vib_eval
ADD CONSTRAINT noise_vib_eval_pk PRIMARY KEY (customer, job);
--
-- TOC entry 2951 (class 2606 OID 16782)
-- Name: rspp_invoices_months rspp_invoices_months_pk; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.rspp_invoices_months
ADD CONSTRAINT rspp_invoices_months_pk PRIMARY KEY (customer);
--
-- TOC entry 2955 (class 2606 OID 16882)
-- Name: asbestos_advice_invoices_months rspp_invoices_months_pk_1; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.asbestos_advice_invoices_months
ADD CONSTRAINT rspp_invoices_months_pk_1 PRIMARY KEY (customer);
--
-- TOC entry 2949 (class 2606 OID 16815)
-- Name: rspp_invoices rspp_invoices_pk; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.rspp_invoices
ADD CONSTRAINT rspp_invoices_pk PRIMARY KEY (rspp_id, rspp_start, invoice_id);
--
-- TOC entry 2945 (class 2606 OID 16665)
-- Name: rssp_account_details rspp_notes_pk; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.rssp_account_details
ADD CONSTRAINT rspp_notes_pk PRIMARY KEY (fiscalcode);
--
-- TOC entry 2953 (class 2606 OID 16874)
-- Name: asbestos_advice_account_details rspp_notes_pk_1; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.asbestos_advice_account_details
ADD CONSTRAINT rspp_notes_pk_1 PRIMARY KEY (fiscalcode);
--
-- TOC entry 2925 (class 2606 OID 16668)
-- Name: rspp rspp_pk; Type: CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.rspp
ADD CONSTRAINT rspp_pk PRIMARY KEY (rspp_jobid, jobstart);
--
-- TOC entry 2927 (class 2606 OID 16673)
-- Name: invoices invoices_pk; Type: CONSTRAINT; Schema: invoices; Owner: admin
--
ALTER TABLE ONLY invoices.invoices
ADD CONSTRAINT invoices_pk PRIMARY KEY (invoiceid);
--
-- TOC entry 2939 (class 2606 OID 16612)
-- Name: job_types job_type_pk; Type: CONSTRAINT; Schema: jobs; Owner: admin
--
ALTER TABLE ONLY jobs.job_types
ADD CONSTRAINT job_type_pk PRIMARY KEY (category, types);
--
-- TOC entry 2941 (class 2606 OID 16786)
-- Name: job_types job_types_un; Type: CONSTRAINT; Schema: jobs; Owner: admin
--
ALTER TABLE ONLY jobs.job_types
ADD CONSTRAINT job_types_un UNIQUE (types);
--
-- TOC entry 2937 (class 2606 OID 16599)
-- Name: jobs_categories jobs_categories_pk; Type: CONSTRAINT; Schema: jobs; Owner: admin
--
ALTER TABLE ONLY jobs.jobs_categories
ADD CONSTRAINT jobs_categories_pk PRIMARY KEY (categories);
--
-- TOC entry 2947 (class 2606 OID 16718)
-- Name: jobs_pa jobs_pa_pk; Type: CONSTRAINT; Schema: jobs; Owner: admin
--
ALTER TABLE ONLY jobs.jobs_pa
ADD CONSTRAINT jobs_pa_pk PRIMARY KEY (jobs_id);
--
-- TOC entry 2943 (class 2606 OID 16625)
-- Name: jobs jobs_pk; Type: CONSTRAINT; Schema: jobs; Owner: admin
--
ALTER TABLE ONLY jobs.jobs
ADD CONSTRAINT jobs_pk PRIMARY KEY (jobs_id);
--
-- TOC entry 3113 (class 2618 OID 16995)
-- Name: pa_report _RETURN; Type: RULE; Schema: deadlines; Owner: admin
--
CREATE OR REPLACE VIEW deadlines.pa_report AS
SELECT a.name AS "Ragione Sociale",
max(db.max) AS "Scadenza",
rad.expired AS "Contratto Terminato"
FROM ( SELECT j_1.jobs_id,
max(r.jobend) AS max
FROM deadlines.rspp r,
deadlines.rssp_account_details rad_1,
jobs.jobs j_1
WHERE (((j_1.jobs_id)::text = (r.rspp_jobid)::text) AND ((rad_1.fiscalcode)::text = (j_1.customer)::text))
GROUP BY j_1.jobs_id) db,
jobs.jobs j,
accounts.accounts a,
deadlines.rssp_account_details rad
WHERE (((j.jobs_id)::text = (db.jobs_id)::text) AND ((j.customer)::text = (a.fiscalcode)::text) AND ((a.fiscalcode)::text = (rad.fiscalcode)::text) AND ((a.customer_category)::text = 'pa'::text))
GROUP BY a.fiscalcode, rad.fiscalcode
ORDER BY (max(db.max));
--
-- TOC entry 2965 (class 2606 OID 16566)
-- Name: accounts_contact accounts_contact_fk; Type: FK CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts_contact
ADD CONSTRAINT accounts_contact_fk FOREIGN KEY (description) REFERENCES accounts.accounts_contact_categories(categories) ON UPDATE CASCADE;
--
-- TOC entry 2966 (class 2606 OID 16587)
-- Name: accounts_fiscaldata accounts_fiscaldata_fk; Type: FK CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts_fiscaldata
ADD CONSTRAINT accounts_fiscaldata_fk FOREIGN KEY (fiscalcode) REFERENCES accounts.accounts(fiscalcode) ON UPDATE CASCADE;
--
-- TOC entry 2962 (class 2606 OID 16547)
-- Name: accounts accounts_fk; Type: FK CONSTRAINT; Schema: accounts; Owner: admin
--
ALTER TABLE ONLY accounts.accounts
ADD CONSTRAINT accounts_fk FOREIGN KEY (customer_category) REFERENCES accounts.accounts_categories(categories) ON UPDATE CASCADE;
--
-- TOC entry 2976 (class 2606 OID 16912)
-- Name: asbestos_advice asbestos_advice_fk; Type: FK CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.asbestos_advice
ADD CONSTRAINT asbestos_advice_fk FOREIGN KEY (jobid) REFERENCES jobs.jobs(jobs_id) ON UPDATE CASCADE;
--
-- TOC entry 2977 (class 2606 OID 16941)
-- Name: asbestos_advice_invoices asbestos_advice_invoices_fk; Type: FK CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.asbestos_advice_invoices
ADD CONSTRAINT asbestos_advice_invoices_fk FOREIGN KEY (job_id, job_start) REFERENCES deadlines.asbestos_advice(jobid, jobstart) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- TOC entry 2978 (class 2606 OID 16946)
-- Name: asbestos_advice_invoices asbestos_advice_invoices_fk_1; Type: FK CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.asbestos_advice_invoices
ADD CONSTRAINT asbestos_advice_invoices_fk_1 FOREIGN KEY (invoice_id) REFERENCES invoices.invoices(invoiceid) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- TOC entry 2979 (class 2606 OID 16957)
-- Name: noise_vib_eval noise_vib_eval_fk; Type: FK CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.noise_vib_eval
ADD CONSTRAINT noise_vib_eval_fk FOREIGN KEY (customer) REFERENCES accounts.accounts(fiscalcode) ON UPDATE CASCADE;
--
-- TOC entry 2980 (class 2606 OID 16962)
-- Name: noise_vib_eval noise_vib_eval_job_fk; Type: FK CONSTRAINT; Schema: deadlines; Owner: admin
--
ALTER TABLE ONLY deadlines.noise_vib_eval
ADD CONSTRAINT noise_vib_eval_job_fk FOREIGN KEY (job) REFERENCES jobs.jobs(jobs_id) ON UPDATE CASCADE;