17939d6bcfb5ffb50cfbd5e154da4f77ba96229b
[memberdb.git] / include / expenses.php
1 <?php
2
3 $EXPENSE_TYPES = array(
4     'invoice'          => 'Rechnung',
5     'gains_tax'        => 'Kapitalertragssteuer',
6     'bankcharges'      => 'Bankgebühren',
7     'account_transfer' => 'Umbuchung',
8     'member_fee'       => 'Rückbuchung Mitgliedsbeitrag',
9     'catering'         => 'Raum - Getraenke & Snacks'
10 );
11
12
13 /* DB functions {{{ */
14
15 function db_get_expense_with_id($expense_id)
16 {
17     $sql = 'SELECT * FROM `expenses` WHERE id=? LIMIT 1';
18     return db_select_single($sql, [$expense_id]);
19 }
20
21 function db_create_expense($expense)
22 {
23     unset($expense['id']);
24     $expense['date'] = db_unixtime2datetime(ui_date2unixtime($expense['date']));
25     $expense['value'] = ui_money2float($expense['value']);
26     $expense['created_at'] = db_unixtime2datetime(time());
27     $expense['modified_at'] = db_unixtime2datetime(time());
28     return (db_insert('expenses', $expense));
29 }
30
31 function db_change_expense($expense)
32 {
33     $id = $expense['id'];
34     unset($expense['id']);
35     $expense['date'] = db_unixtime2datetime(ui_date2unixtime($expense['date']));
36     $expense['value'] = ui_money2float($expense['value']);
37     $expense['modified_at'] = db_unixtime2datetime(time());
38     return db_update('expenses', $expense, sprintf('id=%d', (int)$id));
39 }
40
41 function db_delete_expense($expense)
42 {
43     return db_delete('expenses', 'id=?', $expense['id']);
44 }
45
46 /* }}} */
47
48
49 function validate_expense($userdata, &$dbdata, &$validation)
50 {/*{{{*/
51     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
52     $fields = array(
53         'id'          => 'number',
54         'date'        => 'date',
55         'type'        => $EXPENSE_TYPES,
56         'account'     => $ACCOUNT_TYPES,
57         'member_id'   => 'member',
58         'value'       => 'money',
59         'description' => 'text',
60         //      'status'          -  See below...
61     );
62     $orig = $dbdata;
63     $dbdata['status'] = empty($userdata['status']) ? 'open' : 'paid';
64     foreach ($fields as $name => $type) {
65         if (!isset($userdata[$name])) {
66             continue;
67         }
68         $dbdata[$name] = $userdata[$name];
69     }
70
71     if (empty($dbdata['date'])) {
72         $validation[] = 'Bitte ein Datum angeben.';
73     }
74     if ((float)ui_money2float($dbdata['value']) == 0) {
75         $validation[] = 'Bitte einen Betrag angeben.';
76     }
77
78     return (count($validation) == 0);
79 }/*}}}*/
80
81
82 function action_create_expense()
83 {/*{{{*/
84     $expense = array();
85     $expense['date'] = format_date(time());
86     $expense['status'] = 'paid';
87     $log_messages = array();
88     if (isset($_POST['submit'])) {
89         unset($_POST['id']);
90         if (validate_expense($_POST, $expense, $log_messages)) {
91             if (db_create_expense($expense)) {
92                 redirect(link_to('finances'));
93             }
94             echo db_error();
95         }
96     }
97     form_expense($expense, 'create', $log_messages);
98 }/*}}}*/
99
100
101 function action_change_expense()
102 {/*{{{*/
103     if (!isset($_REQUEST['id'])) {
104         redirect(link_to('finances'));
105     }
106     $expense = db_get_expense_with_id($_REQUEST['id']);
107     if (!isset($expense)) {
108         redirect(link_to('finances'));
109     }
110     $expense['date'] = format_date(db_date2unixtime($expense['date']));
111     $expense['value'] = format_money($expense['value']);
112     $log_messages = array();
113     if (isset($_POST['submit'])) {
114         if (validate_expense($_POST, $expense, $log_messages)) {
115             if (db_change_expense($expense)) {
116                 redirect(link_to('finances'));
117             }
118             echo db_error();
119         }
120     }
121
122     form_expense($expense, 'change', $log_messages);
123 }/*}}}*/
124
125
126 function action_delete_expense()
127 {/*{{{*/
128     if (!isset($_REQUEST['id'])) {
129         redirect(link_to('finances'));
130     }
131     $expense = db_get_expense_with_id($_REQUEST['id']);
132     if (!isset($expense)) {
133         redirect(link_to('finances'));
134     }
135     $expense['date'] = format_date(db_date2unixtime($expense['date']));
136     $expense['value'] = format_money($expense['value']);
137     $log_messages = array();
138     if (isset($_POST['submit'])) {
139         if (db_delete_expense($expense)) {
140             redirect(link_to('finances'));
141         }
142         echo db_error();
143     }
144     form_expense($expense, 'delete', $log_messages);
145 }/*}}}*/
146
147
148 function form_expense($expense = array(), $task, $log_messages)
149 {/*{{{*/
150     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
151
152     $readonly = ($task == 'delete');
153
154     ?>
155     <form action="<?= link_to($task . '_expense') ?>" method="post" class="clearfix" id="expense_form"
156           style="<?php if ($_REQUEST['action'] == 'finances') : ?>float: left<?php else : ?>margin-top: 2em<?php endif ?>">
157         <?php if (isset($expense['id'])) : ?><?= html_hidden_field('id', $expense) ?><?php endif ?>
158         <fieldset>
159             <?php if ($task == 'create') : ?>
160                 <legend>Neue Ausgabe</legend>
161             <?php elseif ($task == 'delete') : ?>
162                 <legend>Ausgabe l&ouml;schen</legend>
163             <?php elseif ($task == 'change') : ?>
164                 <legend>Ausgabe &auml;ndern</legend>
165             <?php endif ?>
166             <?php log_messages($log_messages); ?>
167             <?= html_text_field('Datum', 'date', $expense, $readonly) ?>
168             <?= html_list_box('Typ', 'type', $EXPENSE_TYPES, $expense, $readonly) ?>
169             <?= html_member_list_box('Mitglied', 'member_id', $expense, $readonly) ?>
170             <?= html_money_field('Betrag', 'value', $expense, $readonly) ?>
171             <?= html_text_field('Beschreibung', 'description', $expense, $readonly) ?>
172             <?= html_list_box('Quelle', 'account', $ACCOUNT_TYPES, $expense, $readonly) ?>
173             <?= html_checkbox('Bezahlt', 'status', $expense['status'] == 'paid', 1, $readonly) ?>
174         </fieldset>
175         <?php if ($task == 'delete') : ?>
176             <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
177         <?php else : ?>
178             <input class="submit" type="submit" name="submit" value="Speichern"/>
179         <?php endif ?>
180     </form>
181     <?php if ($task == 'change') : ?>
182     <br/>
183     <a href="<?= html_escape(link_to('delete_expense', array('id' => $expense['id']))) ?>#expense_form">Ausgabe l&ouml;schen</a>
184     <a href="<?= html_escape(link_to('finances')) ?>#expense_form">Neue Ausgabe</a>
185 <?php elseif ($task == 'delete') : ?>
186     <br/>
187     <a href="<?= html_escape(link_to('finances')) ?>#expense_form">Neue Ausgabe</a>
188 <?php endif ?>
189     <?php
190 }/*}}}*/