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