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