-
Notifications
You must be signed in to change notification settings - Fork 0
/
20240824142405_finances.add_update_budget_rpc.sql
80 lines (74 loc) · 2.62 KB
/
20240824142405_finances.add_update_budget_rpc.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
CREATE OR REPLACE FUNCTION finances.update_budget_plan(
plan_id NUMERIC,
plan_name TEXT,
plan_total_income NUMERIC,
allocations JSONB
)
RETURNS JSONB AS $$
DECLARE
allocation JSONB;
allocation_id NUMERIC;
allocation_name TEXT;
allocation_percentage NUMERIC;
allocation_amount NUMERIC;
allocation_plan_id NUMERIC;
result JSONB;
BEGIN
BEGIN
UPDATE finances.budget_plan
SET name = plan_name,
total_income = plan_total_income
WHERE id = plan_id;
FOR allocation IN SELECT * FROM jsonb_array_elements(allocations)
LOOP
allocation_id := NULLIF(allocation->>'id', '')::NUMERIC;
allocation_name := allocation->>'name';
allocation_percentage := NULLIF(allocation->>'percentage', '')::NUMERIC;
allocation_amount := NULLIF(allocation->>'amount', '')::NUMERIC;
allocation_plan_id := COALESCE(NULLIF(allocation->>'budget_plan_id', '')::NUMERIC, plan_id);
IF allocation_id IS NULL THEN
INSERT INTO finances.budget_allocation (budget_plan_id, name, percentage, amount)
VALUES (
id,
allocation_name,
allocation_percentage,
allocation_amount
);
ELSE
UPDATE finances.budget_allocation
SET name = allocation_name,
percentage = allocation_percentage,
amount = allocation_amount
WHERE id = allocation_id;
END IF;
END LOOP;
-- Retrieve the created budget plan along with its allocations
result := (
SELECT jsonb_build_object(
'id', bp.id,
'name', bp.name,
'total_income', bp.total_income,
'created_at', bp.created_at,
'updated_at', bp.updated_at,
'allocations', (
SELECT jsonb_agg(
jsonb_build_object(
'id', ba.id,
'name', ba.name,
'percentage', ba.percentage,
'amount', ba.amount
)
)
FROM finances.budget_allocation ba
WHERE ba.budget_plan_id = bp.id
)
)
FROM finances.budget_plan bp
WHERE bp.id = plan_id
);
RETURN result;
EXCEPTION WHEN OTHERS THEN
RAISE EXCEPTION 'An error occurred: %', SQLERRM;
END;
END;
$$ LANGUAGE plpgsql;