11CREATE OR REPLACE FUNCTION function_insert_mandate_subdelegate (
2-
32 p_representee_id TEXT ,
43 p_delegate_id TEXT ,
54 p_mandate_id TEXT ,
6-
75 p_sub_delegate_identifier TEXT ,
86 p_sub_delegate_first_name TEXT ,
97 p_sub_delegate_surname TEXT ,
108 p_sub_delegate_legal_name TEXT ,
119 p_sub_delegate_type TEXT ,
12-
1310 p_validity_period_from DATE ,
1411 p_validity_period_through DATE ,
15-
1612 p_created_by TEXT ,
1713 p_created_by_represented_person TEXT ,
1814 p_document_uuid TEXT ,
1915 p_can_display_document_to_delegate BOOLEAN
20- ) RETURNS BOOLEAN AS $$
16+ ) RETURNS BOOLEAN AS
17+ $$
18+ DECLARE
19+ rec_existing_mandate mandate%ROWTYPE;
20+ v_sub_delegate_code TEXT := SUBSTRING (p_sub_delegate_identifier FROM 3 );
21+ v_sub_delegate_country TEXT := SUBSTRING (p_sub_delegate_identifier FROM 1 FOR 2 );
22+
23+ v_sub_delegate_id person .id %TYPE;
24+
25+ v_representee_id person .id %TYPE := p_representee_id::integer ;
26+ v_delegate_id person .id %TYPE := p_delegate_id::integer ;
27+ v_mandate_id mandate .id %TYPE := p_mandate_id::integer ;
28+ v_validity_period_from mandate .validity_period_from %TYPE := p_validity_period_from;
29+
2130BEGIN
22- RETURN TRUE;
31+
32+
33+
34+
35+ SELECT *
36+ INTO rec_existing_mandate
37+ FROM mandate
38+ WHERE representee_id = v_representee_id
39+ AND delegate_id = v_delegate_id
40+ AND id = v_mandate_id;
41+
42+ IF rec_existing_mandate .id IS NULL THEN
43+ RAISE ' There is no mandate where id=% AND representee_id=% AND delegate_id=%' , p_mandate_id, v_representee_id, v_delegate_id USING ERRCODE = ' 23002' ;
44+ end if;
45+
46+ IF rec_existing_mandate .can_sub_delegate = FALSE THEN
47+ RAISE ' Mandate to sub-delegate (id=%) has can_sub_delegate=FALSE' , p_mandate_id USING ERRCODE = ' 23002' ;
48+ end if;
49+
50+ IF p_validity_period_from IS NULL THEN
51+ v_validity_period_from := NOW();
52+ end if;
53+
54+ IF p_validity_period_through IS NOT NULL AND v_validity_period_from > p_validity_period_through THEN
55+ RAISE ' Mandate to sub-delegate (id=%) has validity_period_through before validity_period_from' , p_mandate_id USING ERRCODE = ' 23003' ;
56+ end if;
57+
58+ IF rec_existing_mandate .validity_period_from > v_validity_period_from THEN
59+ RAISE ' Mandate to sub-delegate (id=%) has validity_period_from=% BUT new mandate is set to start=% (which is earlier)' ,
60+ p_mandate_id,
61+ rec_existing_mandate .validity_period_from ,
62+ v_validity_period_from
63+ USING ERRCODE = ' 23004' ;
64+ end if;
65+
66+ IF p_validity_period_through IS NULL AND rec_existing_mandate .validity_period_through IS NOT NULL THEN
67+ RAISE ' Mandate to sub-delegate (id=%) has validity_period_through=% BUT new mandate is set to be valid indefinitely (which is longer)' ,
68+ p_mandate_id,
69+ rec_existing_mandate .validity_period_through
70+ USING ERRCODE = ' 23005' ;
71+ end if;
72+
73+ -- Insert or update person record corresponding to new delegate
74+ INSERT INTO person (type, personal_company_code, personal_company_code_country, first_name, surname, legal_name)
75+ VALUES (p_sub_delegate_type, v_sub_delegate_code, v_sub_delegate_country, p_sub_delegate_first_name, p_sub_delegate_surname, p_sub_delegate_legal_name)
76+ ON CONFLICT (personal_company_code, personal_company_code_country)
77+ DO UPDATE SET first_name = EXCLUDED .first_name ,
78+ surname = EXCLUDED .surname ,
79+ legal_name = EXCLUDED .legal_name
80+ RETURNING id INTO v_sub_delegate_id;
81+
82+
83+ INSERT INTO mandate (representee_id, delegate_id, role, validity_period_from, validity_period_through,
84+ can_sub_delegate, created_by, created_by_represented_person,
85+ original_mandate_id, document_uuid, can_display_document_to_delegate)
86+ VALUES (v_representee_id, v_sub_delegate_id, rec_existing_mandate .role , v_validity_period_from::DATE , p_validity_period_through::DATE ,
87+ FALSE, p_created_by, p_created_by_represented_person,
88+ rec_existing_mandate .id , p_document_uuid, p_can_display_document_to_delegate);
89+
90+ RETURN TRUE;
2391END;
2492$$ LANGUAGE plpgsql;
0 commit comments