-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathschema.sql
More file actions
10614 lines (7557 loc) · 261 KB
/
schema.sql
File metadata and controls
10614 lines (7557 loc) · 261 KB
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
--
-- Dumped from database version 10.10 (Ubuntu 10.10-0ubuntu0.18.04.1)
-- Dumped by pg_dump version 10.10 (Ubuntu 10.10-0ubuntu0.18.04.1)
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;
--
-- Name: comptaproc; Type: SCHEMA; Schema: -; Owner: prisma
--
CREATE SCHEMA comptaproc;
ALTER SCHEMA comptaproc OWNER TO prisma;
--
-- 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';
--
-- Name: account_type; Type: DOMAIN; Schema: public; Owner: prisma
--
CREATE DOMAIN public.account_type AS character varying(40);
ALTER DOMAIN public.account_type OWNER TO prisma;
--
-- Name: anc_table_account_type; Type: TYPE; Schema: public; Owner: prisma
--
CREATE TYPE public.anc_table_account_type AS (
po_id bigint,
pa_id bigint,
po_name text,
po_description text,
sum_amount numeric(25,4),
card_account text,
name text
);
ALTER TYPE public.anc_table_account_type OWNER TO prisma;
--
-- Name: anc_table_card_type; Type: TYPE; Schema: public; Owner: prisma
--
CREATE TYPE public.anc_table_card_type AS (
po_id bigint,
pa_id bigint,
po_name text,
po_description text,
sum_amount numeric(25,4),
f_id bigint,
card_account text,
name text
);
ALTER TYPE public.anc_table_card_type OWNER TO prisma;
--
-- Name: menu_tree; Type: TYPE; Schema: public; Owner: prisma
--
CREATE TYPE public.menu_tree AS (
code text,
description text
);
ALTER TYPE public.menu_tree OWNER TO prisma;
--
-- Name: account_add(public.account_type, character varying); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.account_add(p_id public.account_type, p_name character varying) RETURNS text
LANGUAGE plpgsql
AS $$
declare
nParent tmp_pcmn.pcm_val_parent%type;
nCount integer;
sReturn text;
begin
sReturn:= format_account(p_id);
select count(*) into nCount from tmp_pcmn where pcm_val=sReturn;
if nCount = 0 then
nParent=account_parent(p_id);
insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent)
values (p_id, p_name,nParent) returning pcm_val into sReturn;
end if;
return sReturn;
end ;
$$;
ALTER FUNCTION comptaproc.account_add(p_id public.account_type, p_name character varying) OWNER TO prisma;
--
-- Name: account_alphanum(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.account_alphanum() RETURNS boolean
LANGUAGE plpgsql
AS $$
declare
l_auto bool;
begin
l_auto := true;
select pr_value into l_auto from parameter where pr_id='MY_ALPHANUM';
if l_auto = 'N' or l_auto is null then
l_auto:=false;
end if;
return l_auto;
end;
$$;
ALTER FUNCTION comptaproc.account_alphanum() OWNER TO prisma;
--
-- Name: account_auto(integer); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.account_auto(p_fd_id integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare
l_auto bool;
begin
select fd_create_account into l_auto from fiche_def where fd_id=p_fd_id;
if l_auto is null then
l_auto:=false;
end if;
return l_auto;
end;
$$;
ALTER FUNCTION comptaproc.account_auto(p_fd_id integer) OWNER TO prisma;
--
-- Name: account_compute(integer); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.account_compute(p_f_id integer) RETURNS public.account_type
LANGUAGE plpgsql
AS $$
declare
class_base fiche_def.fd_class_base%type;
maxcode numeric;
sResult text;
bAlphanum bool;
sName text;
begin
select fd_class_base into class_base
from
fiche_def join fiche using (fd_id)
where
f_id=p_f_id;
raise notice 'account_compute class base %',class_base;
bAlphanum := account_alphanum();
if bAlphanum = false then
raise info 'account_compute : Alphanum is false';
select count (pcm_val) into maxcode from tmp_pcmn where pcm_val_parent = class_base;
if maxcode = 0 then
maxcode:=class_base::numeric;
else
select max (pcm_val) into maxcode from tmp_pcmn where pcm_val_parent = class_base;
maxcode:=maxcode::numeric;
end if;
if maxcode::text = class_base then
maxcode:=class_base::numeric*1000;
end if;
maxcode:=maxcode+1;
raise notice 'account_compute Max code %',maxcode;
sResult:=maxcode::account_type;
else
raise info 'account_compute : Alphanum is true';
-- if alphanum, use name
select ad_value into sName from fiche_detail where f_id=p_f_id and ad_id=1;
raise info 'name is %',sName;
if sName is null then
raise exception 'Cannot compute an accounting without the name of the card for %',p_f_id;
end if;
sResult := class_base||sName;
sResult := substr(sResult,1,40);
raise info 'Result is %',sResult;
end if;
return sResult::account_type;
end;
$$;
ALTER FUNCTION comptaproc.account_compute(p_f_id integer) OWNER TO prisma;
--
-- Name: account_insert(integer, text); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.account_insert(p_f_id integer, p_account text) RETURNS text
LANGUAGE plpgsql
AS $$
declare
nParent tmp_pcmn.pcm_val_parent%type;
sName varchar;
sNew tmp_pcmn.pcm_val%type;
bAuto bool;
nFd_id integer;
sClass_Base fiche_def.fd_class_base%TYPE;
nCount integer;
first text;
second text;
s_account text;
begin
if p_account is not null and length(trim(p_account)) != 0 then
-- if there is coma in p_account, treat normally
if position (',' in p_account) = 0 then
raise info 'p_account is not empty';
s_account := substr( p_account,1 , 40);
select count(*) into nCount from tmp_pcmn where pcm_val=s_account::account_type;
raise notice 'found in tmp_pcm %',nCount;
if nCount !=0 then
raise info 'this account exists in tmp_pcmn ';
perform attribut_insert(p_f_id,5,s_account);
else
-- account doesn't exist, create it
select ad_value into sName from
fiche_detail
where
ad_id=1 and f_id=p_f_id;
nParent:=account_parent(s_account::account_type);
insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (s_account::account_type,sName,nParent);
perform attribut_insert(p_f_id,5,s_account);
end if;
else
raise info 'presence of a comma';
-- there is 2 accounts separated by a comma
first := split_part(p_account,',',1);
second := split_part(p_account,',',2);
-- check there is no other coma
raise info 'first value % second value %', first, second;
if position (',' in first) != 0 or position (',' in second) != 0 then
raise exception 'Too many comas, invalid account';
end if;
perform attribut_insert(p_f_id,5,p_account);
end if;
else
raise info 'A000 : p_account is empty';
select fd_id into nFd_id from fiche where f_id=p_f_id;
bAuto:= account_auto(nFd_id);
select fd_class_base into sClass_base from fiche_def where fd_id=nFd_id;
raise info 'sClass_Base : %',sClass_base;
if bAuto = true and sClass_base similar to '[[:digit:]]*' then
raise info 'account generated automatically';
sNew:=account_compute(p_f_id);
raise info 'sNew %', sNew;
select ad_value into sName from
fiche_detail
where
ad_id=1 and f_id=p_f_id;
nParent:=account_parent(sNew);
sNew := account_add (sNew,sName);
perform attribut_insert(p_f_id,5,sNew);
else
-- if there is an account_base then it is the default
select fd_class_base::account_type into sNew from fiche_def join fiche using (fd_id) where f_id=p_f_id;
if sNew is null or length(trim(sNew)) = 0 then
raise notice 'count is null';
perform attribut_insert(p_f_id,5,null);
else
perform attribut_insert(p_f_id,5,sNew);
end if;
end if;
end if;
return 0;
end;
$$;
ALTER FUNCTION comptaproc.account_insert(p_f_id integer, p_account text) OWNER TO prisma;
--
-- Name: account_parent(public.account_type); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.account_parent(p_account public.account_type) RETURNS public.account_type
LANGUAGE plpgsql
AS $$
declare
sSubParent tmp_pcmn.pcm_val_parent%type;
sResult tmp_pcmn.pcm_val_parent%type;
nCount integer;
begin
if p_account is NULL then
return NULL;
end if;
sSubParent:=p_account;
while true loop
select count(*) into nCount
from tmp_pcmn
where
pcm_val = sSubParent;
if nCount != 0 then
sResult:= sSubParent;
exit;
end if;
sSubParent:= substr(sSubParent,1,length(sSubParent)-1);
if length(sSubParent) <= 0 then
raise exception 'Impossible de trouver le compte parent pour %',p_account;
end if;
raise notice 'sSubParent % % ',sSubParent,length(sSubParent);
end loop;
raise notice 'account_parent : Parent is %',sSubParent;
return sSubParent;
end;
$$;
ALTER FUNCTION comptaproc.account_parent(p_account public.account_type) OWNER TO prisma;
--
-- Name: account_update(integer, public.account_type); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.account_update(p_f_id integer, p_account public.account_type) RETURNS integer
LANGUAGE plpgsql
AS $$
declare
nMax fiche.f_id%type;
nCount integer;
nParent tmp_pcmn.pcm_val_parent%type;
sName varchar;
first text;
second text;
begin
if length(trim(p_account)) != 0 then
-- 2 accounts in card separated by comma
if position (',' in p_account) = 0 then
select count(*) into nCount from tmp_pcmn where pcm_val=p_account;
if nCount = 0 then
select ad_value into sName from
fiche_detail
where
ad_id=1 and f_id=p_f_id;
nParent:=account_parent(p_account);
insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account,sName,nParent);
end if;
else
raise info 'presence of a comma';
-- there is 2 accounts separated by a comma
first := split_part(p_account,',',1);
second := split_part(p_account,',',2);
-- check there is no other coma
raise info 'first value % second value %', first, second;
if position (',' in first) != 0 or position (',' in second) != 0 then
raise exception 'Too many comas, invalid account';
end if;
-- check that both account are in PCMN
end if;
else
-- account is null
update fiche_detail set ad_value=null where f_id=p_f_id and ad_id=5 ;
end if;
update fiche_detail set ad_value=p_account where f_id=p_f_id and ad_id=5 ;
return 0;
end;
$$;
ALTER FUNCTION comptaproc.account_update(p_f_id integer, p_account public.account_type) OWNER TO prisma;
--
-- Name: action_gestion_ins_upd(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.action_gestion_ins_upd() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
NEW.ag_title := substr(trim(NEW.ag_title),1,70);
NEW.ag_hour := substr(trim(NEW.ag_hour),1,5);
NEW.ag_owner := lower(NEW.ag_owner);
return NEW;
end;
$$;
ALTER FUNCTION comptaproc.action_gestion_ins_upd() OWNER TO prisma;
--
-- Name: action_gestion_related_ins_up(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.action_gestion_related_ins_up() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
nTmp bigint;
begin
if NEW.aga_least > NEW.aga_greatest then
nTmp := NEW.aga_least;
NEW.aga_least := NEW.aga_greatest;
NEW.aga_greatest := nTmp;
end if;
if NEW.aga_least = NEW.aga_greatest then
return NULL;
end if;
return NEW;
end;
$$;
ALTER FUNCTION comptaproc.action_gestion_related_ins_up() OWNER TO prisma;
--
-- Name: anc_correct_tvand(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.anc_correct_tvand() RETURNS void
LANGUAGE plpgsql
AS $$
declare
n_count numeric;
i record;
newrow_tva record;
begin
for i in select * from operation_analytique where oa_jrnx_id_source is not null loop
-- Get all the anc accounting from the base operation and insert the missing record for VAT
for newrow_tva in select * from operation_analytique where j_id=i.oa_jrnx_id_source and po_id <> i.po_id loop
-- check if the record is yet present
select count(*) into n_count from operation_analytique where po_id=newrow_tva.po_id and oa_jrnx_id_source=i.oa_jrnx_id_source;
if n_count = 0 then
raise info 'insert operation analytique po_id = % oa_group = % ',i.po_id, i.oa_group;
insert into operation_analytique
(po_id,oa_amount,oa_description,oa_debit,j_id,oa_group,oa_date,oa_jrnx_id_source,oa_positive)
values (newrow_tva.po_id,i.oa_amount,i.oa_description,i.oa_debit,i.j_id,i.oa_group,i.oa_date,i.oa_jrnx_id_source,i.oa_positive);
end if;
end loop;
end loop;
end;
$$;
ALTER FUNCTION comptaproc.anc_correct_tvand() OWNER TO prisma;
--
-- Name: attribut_insert(integer, integer, character varying); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) RETURNS void
LANGUAGE plpgsql
AS $$
begin
insert into fiche_detail (f_id,ad_id, ad_value) values (p_f_id,p_ad_id,p_value);
return;
end;
$$;
ALTER FUNCTION comptaproc.attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) OWNER TO prisma;
--
-- Name: attribute_correct_order(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.attribute_correct_order() RETURNS void
LANGUAGE plpgsql
AS $$
declare
crs_correct cursor for select A.jnt_id,A.jnt_order from jnt_fic_attr as A join jnt_fic_attr as B using (fd_id) where A.jnt_order=B.jnt_order and A.jnt_id > B.jnt_id;
rec record;
begin
open crs_correct;
loop
fetch crs_correct into rec;
if NOT FOUND then
close crs_correct;
return;
end if;
update jnt_fic_attr set jnt_order=jnt_order + 1 where jnt_id = rec.jnt_id;
end loop;
close crs_correct;
perform attribute_correct_order ();
end;
$$;
ALTER FUNCTION comptaproc.attribute_correct_order() OWNER TO prisma;
--
-- Name: card_after_delete(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.card_after_delete() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from action_gestion where f_id_dest = OLD.f_id;
return OLD;
end;
$$;
ALTER FUNCTION comptaproc.card_after_delete() OWNER TO prisma;
--
-- Name: card_class_base(integer); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.card_class_base(p_f_id integer) RETURNS text
LANGUAGE plpgsql
AS $$
declare
n_poste fiche_def.fd_class_base%type;
begin
select fd_class_base into n_poste from fiche_def join fiche using
(fd_id)
where f_id=p_f_id;
if not FOUND then
raise exception 'Invalid fiche card_class_base(%)',p_f_id;
end if;
return n_poste;
end;
$$;
ALTER FUNCTION comptaproc.card_class_base(p_f_id integer) OWNER TO prisma;
--
-- Name: category_card_before_delete(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.category_card_before_delete() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
if OLD.fd_id > 499000 then
return null;
end if;
return OLD;
end;
$$;
ALTER FUNCTION comptaproc.category_card_before_delete() OWNER TO prisma;
--
-- Name: check_balance(integer); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.check_balance(p_grpt integer) RETURNS numeric
LANGUAGE plpgsql
AS $$
declare
amount_jrnx_debit numeric;
amount_jrnx_credit numeric;
amount_jrn numeric;
begin
select coalesce(sum (j_montant),0) into amount_jrnx_credit
from jrnx
where
j_grpt=p_grpt
and j_debit=false;
select coalesce(sum (j_montant),0) into amount_jrnx_debit
from jrnx
where
j_grpt=p_grpt
and j_debit=true;
select coalesce(jr_montant,0) into amount_jrn
from jrn
where
jr_grpt_id=p_grpt;
if ( amount_jrnx_debit != amount_jrnx_credit )
then
return abs(amount_jrnx_debit-amount_jrnx_credit);
end if;
if ( amount_jrn != amount_jrnx_credit)
then
return -1*abs(amount_jrn - amount_jrnx_credit);
end if;
return 0;
end;
$$;
ALTER FUNCTION comptaproc.check_balance(p_grpt integer) OWNER TO prisma;
--
-- Name: check_periode(); Type: FUNCTION; Schema: public; Owner: prisma
--
CREATE FUNCTION public.check_periode() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
nPeriode int;
begin
if periode_exist(to_char(NEW.p_start,'DD.MM.YYYY'),NEW.p_id) <> -1 then
nPeriode:=periode_exist(to_char(NEW.p_start,'DD.MM.YYYY'),NEW.p_id) ;
raise info 'Overlap periode start % periode %',NEW.p_start,nPeriode;
return null;
end if;
if periode_exist(to_char(NEW.p_end,'DD.MM.YYYY'),NEW.p_id) <> -1 then
nPeriode:=periode_exist(to_char(NEW.p_start,'DD.MM.YYYY'),NEW.p_id) ;
raise info 'Overlap periode end % periode %',NEW.p_end,nPeriode;
return null;
end if;
return NEW;
end;
$$;
ALTER FUNCTION public.check_periode() OWNER TO prisma;
--
-- Name: correct_sequence(text, text, text); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.correct_sequence(p_sequence text, p_col text, p_table text) RETURNS integer
LANGUAGE plpgsql
AS $$
declare
last_sequence int8;
max_sequence int8;
n integer;
begin
select count(*) into n from pg_class where relkind='S' and relname=lower(p_sequence);
if n = 0 then
raise exception ' Unknow sequence % ',p_sequence;
end if;
select count(*) into n from pg_class where relkind='r' and relname=lower(p_table);
if n = 0 then
raise exception ' Unknow table % ',p_table;
end if;
execute 'select last_value from '||p_sequence into last_sequence;
raise notice 'Last value of the sequence is %', last_sequence;
execute 'select max('||p_col||') from '||p_table into max_sequence;
if max_sequence is null then
max_sequence := 0;
end if;
raise notice 'Max value of the sequence is %', max_sequence;
max_sequence:= max_sequence +1;
execute 'alter sequence '||p_sequence||' restart with '||max_sequence;
return 0;
end;
$$;
ALTER FUNCTION comptaproc.correct_sequence(p_sequence text, p_col text, p_table text) OWNER TO prisma;
--
-- Name: FUNCTION correct_sequence(p_sequence text, p_col text, p_table text); Type: COMMENT; Schema: comptaproc; Owner: prisma
--
COMMENT ON FUNCTION comptaproc.correct_sequence(p_sequence text, p_col text, p_table text) IS ' Often the primary key is a sequence number and sometimes the value of the sequence is not synchronized with the primary key ( p_sequence : sequence name, p_col : col of the pk,p_table : concerned table';
--
-- Name: create_missing_sequence(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.create_missing_sequence() RETURNS integer
LANGUAGE plpgsql
AS $$
declare
p_sequence text;
nSeq integer;
c1 cursor for select jrn_def_id from jrn_def;
begin
open c1;
loop
fetch c1 into nSeq;
if not FOUND THEN
close c1;
return 0;
end if;
p_sequence:='s_jrn_pj'||nSeq::text;
execute 'create sequence '||p_sequence;
end loop;
close c1;
return 0;
end;
$$;
ALTER FUNCTION comptaproc.create_missing_sequence() OWNER TO prisma;
--
-- Name: drop_index(character varying); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.drop_index(p_constraint character varying) RETURNS void
LANGUAGE plpgsql
AS $$
declare
nCount integer;
begin
select count(*) into nCount from pg_indexes where indexname=p_constraint;
if nCount = 1 then
execute 'drop index '||p_constraint ;
end if;
end;
$$;
ALTER FUNCTION comptaproc.drop_index(p_constraint character varying) OWNER TO prisma;
--
-- Name: drop_it(character varying); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.drop_it(p_constraint character varying) RETURNS void
LANGUAGE plpgsql
AS $$
declare
nCount integer;
begin
select count(*) into nCount from pg_constraint where conname=p_constraint;
if nCount = 1 then
execute 'alter table parm_periode drop constraint '||p_constraint ;
end if;
end;
$$;
ALTER FUNCTION comptaproc.drop_it(p_constraint character varying) OWNER TO prisma;
--
-- Name: extension_ins_upd(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.extension_ins_upd() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
sCode text;
sFile text;
begin
sCode:=trim(upper(NEW.ex_code));
sCode:=replace(sCode,' ','_');
sCode:=substr(sCode,1,15);
sCode=upper(sCode);
NEW.ex_code:=sCode;
sFile:=NEW.ex_file;
sFile:=replace(sFile,';','_');
sFile:=replace(sFile,'<','_');
sFile:=replace(sFile,'>','_');
sFile:=replace(sFile,'..','');
sFile:=replace(sFile,'&','');
sFile:=replace(sFile,'|','');
return NEW;
end;
$$;
ALTER FUNCTION comptaproc.extension_ins_upd() OWNER TO prisma;
--
-- Name: fiche_account_parent(integer); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.fiche_account_parent(p_f_id integer) RETURNS public.account_type
LANGUAGE plpgsql
AS $$
declare
ret tmp_pcmn.pcm_val%TYPE;
begin
select fd_class_base into ret from fiche_def join fiche using (fd_id) where f_id=p_f_id;
if not FOUND then
raise exception '% N''existe pas',p_f_id;
end if;
return ret;
end;
$$;
ALTER FUNCTION comptaproc.fiche_account_parent(p_f_id integer) OWNER TO prisma;
--
-- Name: fiche_attribut_synchro(integer); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.fiche_attribut_synchro(p_fd_id integer) RETURNS void
LANGUAGE plpgsql
AS $$
declare
-- this sql gives the f_id and the missing attribute (ad_id)
list_missing cursor for select f_id,fd_id,ad_id,jnt_order from jnt_fic_attr join fiche as A using (fd_id) where fd_id=p_fd_id and ad_id not in (select ad_id from fiche join fiche_detail using (f_id) where fd_id=jnt_fic_attr.fd_id and A.f_id=f_id);
rec record;
begin
open list_missing;
loop
fetch list_missing into rec;
IF NOT FOUND then
exit;
end if;
-- now we insert into attr_value
insert into fiche_detail (f_id,ad_id,ad_value) values (rec.f_id,rec.ad_id,null);
end loop;
close list_missing;
end;
$$;
ALTER FUNCTION comptaproc.fiche_attribut_synchro(p_fd_id integer) OWNER TO prisma;
--
-- Name: fiche_def_ins_upd(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.fiche_def_ins_upd() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
if position (',' in NEW.fd_class_base) != 0 then
NEW.fd_create_account='f';
end if;
return NEW;
end;$$;
ALTER FUNCTION comptaproc.fiche_def_ins_upd() OWNER TO prisma;
--
-- Name: fiche_detail_qcode_upd(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.fiche_detail_qcode_upd() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
i record;
begin
if NEW.ad_id=23 and NEW.ad_value != OLD.ad_value then
RAISE NOTICE 'new qcode [%] old qcode [%]',NEW.ad_value,OLD.ad_value;
update jrnx set j_qcode=NEW.ad_value where j_qcode = OLD.ad_value;
update op_predef_detail set opd_poste=NEW.ad_value where opd_poste=OLD.ad_value;
raise notice 'TRG fiche_detail update op_predef_detail set opd_poste=% where opd_poste=%;',NEW.ad_value,OLD.ad_value;
for i in select ad_id from attr_def where ad_type = 'card' or ad_id=25 loop
update fiche_detail set ad_value=NEW.ad_value where ad_value=OLD.ad_value and ad_id=i.ad_id;
RAISE NOTICE 'change for ad_id [%] ',i.ad_id;
if i.ad_id=19 then
RAISE NOTICE 'Change in stock_goods OLD[%] by NEW[%]',OLD.ad_value,NEW.ad_value;
update stock_goods set sg_code=NEW.ad_value where sg_code=OLD.ad_value;
end if;
end loop;
end if;
return NEW;
end;
$$;
ALTER FUNCTION comptaproc.fiche_detail_qcode_upd() OWNER TO prisma;
--
-- Name: fill_quant_fin(); Type: FUNCTION; Schema: comptaproc; Owner: prisma
--
CREATE FUNCTION comptaproc.fill_quant_fin() RETURNS void
LANGUAGE plpgsql
AS $$
declare
sBank text;
sCassa text;
sCustomer text;
sSupplier text;
rec record;
recBank record;
recSupp_Cust record;
nCount integer;
nAmount numeric;
nBank integer;
nOther integer;
nSupp_Cust integer;
begin
select p_value into sBank from parm_code where p_code='BANQUE';
select p_value into sCassa from parm_code where p_code='CAISSE';
select p_value into sSupplier from parm_code where p_code='SUPPLIER';
select p_value into sCustomer from parm_code where p_code='CUSTOMER';
for rec in select jr_id,jr_grpt_id from jrn
where jr_def_id in (select jrn_def_id from jrn_def where jrn_def_type='FIN')
and jr_id not in (select jr_id from quant_fin)
loop
-- there are only 2 lines for bank operations
-- first debit
select count(j_id) into nCount from jrnx where j_grpt=rec.jr_grpt_id;
if nCount > 2 then
raise notice 'Trop de valeur pour jr_grpt_id % count %',rec.jr_grpt_id,nCount;
return;
end if;
nBank := 0; nOther:=0;
for recBank in select j_id, j_montant,j_debit,j_qcode,j_poste from jrnx where j_grpt=rec.jr_grpt_id
loop
if recBank.j_poste like sBank||'%' then
-- retrieve f_id for bank
select f_id into nBank from vw_poste_qcode where j_qcode=recBank.j_qcode;
if recBank.j_debit = false then
nAmount=recBank.j_montant*(-1);
else
nAmount=recBank.j_montant;
end if;
else
select f_id into nOther from vw_poste_qcode where j_qcode=recBank.j_qcode;
end if;
end loop;
if nBank != 0 and nOther != 0 then
insert into quant_fin (jr_id,qf_bank,qf_other,qf_amount) values (rec.jr_id,nBank,nOther,nAmount);
end if;
end loop;
for rec in select jr_id,jr_grpt_id from jrn
where jr_def_id in (select jrn_def_id from jrn_def where jrn_def_type='FIN') and jr_id not in (select jr_id from quant_fin)
loop
-- there are only 2 lines for bank operations
-- first debit
select count(j_id) into nCount from jrnx where j_grpt=rec.jr_grpt_id;
if nCount > 2 then
raise notice 'Trop de valeur pour jr_grpt_id % count %',rec.jr_grpt_id,nCount;
return;
end if;
nBank := 0; nOther:=0;
for recBank in select j_id, j_montant,j_debit,j_qcode,j_poste from jrnx where j_grpt=rec.jr_grpt_id
loop
if recBank.j_poste like sCassa||'%' then
-- retrieve f_id for bank
select f_id into nBank from vw_poste_qcode where j_qcode=recBank.j_qcode;
if recBank.j_debit = false then