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