1
+ -- Write the name of the group in the title of the page
2
+ SELECT ' title' as component, name as contents FROM expense_group WHERE id = $id;
3
+
4
+
5
+ -- Handle the form to add a member to the group (we do it at the top of the page to see it right away)
6
+ INSERT INTO group_member(group_id, name)
7
+ SELECT $id, :new_member_name WHERE :new_member_name IS NOT NULL ;
8
+
9
+ -- List of members of the group
10
+ SELECT ' list' as component, ' Membres' as title;
11
+ SELECT name AS title FROM group_member WHERE group_id = $id;
12
+
13
+ -- Form to add a new member to the group
14
+ SELECT ' form' as component, ' Ajouter un membre au groupe' as validate;
15
+ SELECT ' Nom du membre' AS ' label' , ' new_member_name' AS name;
16
+
17
+ SELECT ' title' as component, ' Dépenses' as contents
18
+
19
+ -- Form to add an expense
20
+ SELECT ' form' as component, ' Ajouter une dépense' as title, ' Ajouter' as validate;
21
+ SELECT ' Description' AS name;
22
+ SELECT ' Montant' AS name, ' number' AS type;
23
+ SELECT ' Dépensé par' AS name, ' select' as type,
24
+ json_group_array(json_object(" label" , name, " value" , id)) as options
25
+ FROM group_member WHERE group_id = $id;
26
+
27
+ -- Insert the expense posted by the form into the database
28
+ INSERT INTO expense(spent_by, name, amount)
29
+ SELECT :" Dépensé par" , :Description, :Montant WHERE :Montant IS NOT NULL ;
30
+
31
+ -- List of expenses of the group
32
+ SELECT ' card' as component, ' Dépenses' as title;
33
+ SELECT expense .name as title,
34
+ ' Par ' || group_member .name || ' , le ' || expense .date as description,
35
+ expense .amount || ' €' as footer,
36
+ CASE
37
+ WHEN expense .amount > 100 THEN ' red'
38
+ WHEN expense .amount > 50 THEN ' orange'
39
+ ELSE ' blue'
40
+ END AS color
41
+ FROM expense
42
+ INNER JOIN group_member on expense .spent_by = group_member .id
43
+ WHERE group_member .group_id = $id;
44
+
45
+ -- Show the positive and negative debts of each member
46
+ SELECT ' chart' AS component, ' Dette par personne' AS title, ' bar' AS type, TRUE AS horizontal;
47
+ SELECT member_name AS label, is_owed AS value FROM individual_debts
48
+ WHERE group_id = $id ORDER BY is_owed DESC ;
0 commit comments